42 #include "../Container/SiteContainerTools.h" 43 #include "../Alphabet/AlphabetTools.h" 44 #include <Bpp/Text/TextTools.h> 45 #include <Bpp/Text/KeyvalTools.h> 46 #include <Bpp/Io/FileTools.h> 60 string::size_type index = s.find(
" ");
61 if(index == string::npos)
throw Exception(
"NexusIOSequence::splitNameAndSequence_(). No sequence name found.");
62 v[0] = TextTools::removeSurroundingWhiteSpaces(s.substr(0, index));
63 v[1] = TextTools::removeFirstWhiteSpaces(s.substr(index + 1));
73 if (!input) {
throw IOException (
"NexusIOSequence::read(). Fail to open file"); }
77 while (TextTools::toUpper(line) !=
"BEGIN DATA;")
80 throw Exception(
"NexusIOSequence::appendFromStream(). No data block was found.");
81 line = TextTools::removeSurroundingWhiteSpaces(FileTools::getNextLine(input));
85 string cmdName =
"", cmdArgs =
"";
86 while (cmdName !=
"DIMENSIONS")
89 throw Exception(
"NexusIOSequence::appendFromStream(). No DIMENSIONS command was found.");
91 cmdName = TextTools::toUpper(cmdName);
93 map<string, string> args;
94 KeyvalTools::multipleKeyvals(cmdArgs, args,
" ");
95 map<string, string> argsUp;
96 for (map<string, string>::iterator it = args.begin(); it != args.end(); it++)
97 argsUp[TextTools::toUpper(it->first)] = it->second;
98 if (argsUp[
"NTAX"] ==
"")
99 throw Exception(
"NexusIOSequence::appendFromStream(). DIMENSIONS command does not have a NTAX argument.");
100 unsigned int ntax = TextTools::to<unsigned int>(argsUp[
"NTAX"]);
103 while (cmdName !=
"FORMAT")
106 throw Exception(
"NexusIOSequence::appendFromStream(). No FORMAT command was found.");
108 cmdName = TextTools::toUpper(cmdName);
110 if (TextTools::hasSubstring(cmdArgs,
"TRANSPOSE"))
111 throw Exception(
"NexusIOSequence::appendFromStream(). TRANSPOSE option is not supported.");
114 bool matchChar = TextTools::hasSubstring(TextTools::toUpper(cmdArgs),
"MATCHCHAR");
124 while (!TextTools::startsWith(TextTools::toUpper(line),
"MATRIX"))
127 throw Exception(
"NexusIOSequence::appendFromStream(). No MATRIX command was found.");
128 line = TextTools::removeSurroundingWhiteSpaces(FileTools::getNextLine(input));
130 line = FileTools::getNextLine(input);
132 vector<string> names, seqs;
134 bool commandFinished =
false;
135 for (
unsigned int i = 0; i < ntax && !input.eof(); i++)
137 if (TextTools::endsWith(line,
";"))
140 throw IOException(
"NexusIOSequence::appendFromStream. Early end of MATRIX command, some sequences are missing.");
143 commandFinished =
true;
144 line = line.substr(0, line.size() - 1);
147 vector<string> v = splitNameAndSequence_(line);
148 names.push_back(v[0]);
149 seqs.push_back(v[1]);
150 line = FileTools::getNextLine(input);
154 commandFinished = TextTools::removeSurroundingWhiteSpaces(line) ==
";";
155 while (!commandFinished)
157 for (
unsigned int i = 0; i < ntax && !input.eof(); i++)
159 if (TextTools::endsWith(line,
";"))
162 throw IOException(
"NexusIOSequence::appendFromStream. Early end of MATRIX command, some sequences are missing.");
165 commandFinished =
true;
166 line = line.substr(0, line.size() - 1);
170 vector<string> v = splitNameAndSequence_(line);
171 if (v[0] != names[i])
172 throw IOException(
"NexusIOSequence::appendFromStream. Bad file, the sequences are not in the same order in interleaved blocks, or one taxon is missing.");
174 line = FileTools::getNextLine(input);
175 commandFinished = TextTools::removeSurroundingWhiteSpaces(line) ==
";";
178 for (
unsigned int i = 0; i < names.size(); i++)
193 delete resolvedAlignment;
205 return "Nexus file format.";
The SiteContainer interface.
const std::string getFormatName() const
Aligned sequences container.
This alphabet is used to deal NumericAlphabet.
const std::string getFormatDescription() const
virtual const Sequence & getSequence(size_t sequenceIndex) const =0
Retrieve a sequence object from the container.
A basic implementation of the Sequence interface.
void appendAlignmentFromStream(std::istream &input, SiteContainer &sc) const
Append sequences to a container from a stream.
virtual void addSequence(const Sequence &sequence, bool checkName)=0
Add a sequence to the container.
virtual const Alphabet * getAlphabet() const =0
Get sequence container's alphabet.
const std::vector< std::string > splitNameAndSequence_(const std::string &s) const
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.