bpp-seq3  3.0.0
BppOSequenceReaderFormat.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Text/KeyvalTools.h>
6 #include <memory>
7 #include <string>
8 
10 #include "Clustal.h"
11 #include "Dcse.h"
12 #include "Fasta.h"
13 #include "GenBank.h"
14 #include "Mase.h"
15 #include "NexusIoSequence.h"
16 #include "Phylip.h"
17 
18 using namespace bpp;
19 using namespace std;
20 
21 unique_ptr<ISequence> BppOSequenceReaderFormat::read(const std::string& description)
22 {
23  unparsedArguments_.clear();
24  string format = "";
25  KeyvalTools::parseProcedure(description, format, unparsedArguments_);
26  unique_ptr<ISequence> iSeq;
27  if (format == "Mase")
28  {
29  iSeq.reset(new Mase());
30  }
31  else if (format == "Phylip")
32  {
33  bool sequential = true, extended = true;
34  string split = " ";
35  string order = ApplicationTools::getStringParameter("order", unparsedArguments_, "sequential", "", true, warningLevel_);
36  if (order == "sequential")
37  sequential = true;
38  else if (order == "interleaved")
39  sequential = false;
40  else
41  throw Exception("BppOAlignmentReaderFormat::read. Invalid argument 'order' for phylip format: " + order);
42 
43  string type = ApplicationTools::getStringParameter("type", unparsedArguments_, "extended", "", true, warningLevel_);
44  if (type == "extended")
45  {
46  extended = true;
47  split = ApplicationTools::getStringParameter("split", unparsedArguments_, "spaces", "", true, warningLevel_);
48  if (split == "spaces")
49  split = " ";
50  else if (split == "tab")
51  split = "\t";
52  else
53  throw Exception("BppOAlignmentReaderFormat::read. Invalid argument 'split' for phylip format: " + split);
54  }
55  else if (type == "classic")
56  extended = false;
57  else
58  throw Exception("BppOAlignmentReaderFormat::read. Invalid argument 'type' for phylip format: " + type);
59 
60  iSeq.reset(new Phylip(extended, sequential, 100, split));
61  }
62  else if (format == "Fasta")
63  {
64  bool strictNames = ApplicationTools::getBooleanParameter("strict_names", unparsedArguments_, false, "", true, warningLevel_);
65  bool extended = ApplicationTools::getBooleanParameter("extended", unparsedArguments_, false, "", true, warningLevel_);
66  iSeq.reset(new Fasta(100, true, extended, strictNames));
67  }
68  else if (format == "Clustal")
69  {
70  unsigned int extraSpaces = ApplicationTools::getParameter<unsigned int>("extraSpaces", unparsedArguments_, 0, "", true, warningLevel_);
71  iSeq.reset(new Clustal(true, extraSpaces));
72  }
73  else if (format == "Dcse")
74  {
75  iSeq.reset(new DCSE());
76  }
77  else if (format == "GenBank")
78  {
79  iSeq.reset(new GenBank()); // This is required to remove a strict-aliasing warning in gcc 4.4
80  }
81  else if (format == "Nexus")
82  {
83  iSeq.reset(new NexusIOSequence());
84  }
85  else
86  {
87  throw IOException("Sequence format '" + format + "' unknown.");
88  }
89 
90  return iSeq;
91 }
static bool getBooleanParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
static std::string getStringParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
std::unique_ptr< ISequence > read(const std::string &description)
Read a ISequence object from a string.
The clustal sequence file format.
Definition: Clustal.h:26
Support for the Dedicated Comparative Sequence Editor format.
Definition: Dcse.h:27
The fasta sequence file format.
Definition: Fasta.h:32
The GenBank sequence file format.
Definition: GenBank.h:23
static void parseProcedure(const std::string &desc, std::string &name, std::map< std::string, std::string > &args)
The mase sequence file format.
Definition: Mase.h:107
The Nexus format reader for sequences.
The Phylip & co format.
Definition: Phylip.h:31
std::vector< std::string > split(const std::string &s, std::size_t n)
This alphabet is used to deal NumericAlphabet.