9 #include "../Alphabet/AlphabetTools.h"
10 #include "../Container/SiteContainerTools.h"
26 string::size_type index = s.find(
" ");
27 if (index == string::npos)
28 throw Exception(
"NexusIOSequence::splitNameAndSequence_(). No sequence name found.");
42 throw IOException (
"NexusIOSequence::read(). Fail to open file");
50 throw Exception(
"NexusIOSequence::appendFromStream(). No data block was found.");
55 string cmdName =
"", cmdArgs =
"";
56 while (cmdName !=
"DIMENSIONS")
59 throw Exception(
"NexusIOSequence::appendFromStream(). No DIMENSIONS command was found.");
63 map<string, string> args;
65 map<string, string> argsUp;
66 for (map<string, string>::iterator it = args.begin(); it != args.end(); it++)
70 if (argsUp[
"NTAX"] ==
"")
71 throw Exception(
"NexusIOSequence::appendFromStream(). DIMENSIONS command does not have a NTAX argument.");
72 size_t ntax = TextTools::to<size_t>(argsUp[
"NTAX"]);
75 while (cmdName !=
"FORMAT")
78 throw Exception(
"NexusIOSequence::appendFromStream(). No FORMAT command was found.");
83 throw Exception(
"NexusIOSequence::appendFromStream(). TRANSPOSE option is not supported.");
89 auto alignment = make_unique<AlignedSequenceContainer>(alphaPtr);
96 throw Exception(
"NexusIOSequence::appendFromStream(). No MATRIX command was found.");
101 vector<string> names, seqs;
103 bool commandFinished =
false;
104 for (
size_t i = 0; i < ntax && !input.eof(); ++i)
109 throw IOException(
"NexusIOSequence::appendFromStream. Early end of MATRIX command, some sequences are missing.");
112 commandFinished =
true;
113 line = line.substr(0, line.size() - 1);
116 vector<string> v = splitNameAndSequence_(line);
117 names.push_back(v[0]);
118 seqs.push_back(v[1]);
124 while (!commandFinished)
126 for (
size_t i = 0; i < ntax && !input.eof(); ++i)
131 throw IOException(
"NexusIOSequence::appendFromStream. Early end of MATRIX command, some sequences are missing.");
134 commandFinished =
true;
135 line = line.substr(0, line.size() - 1);
139 vector<string> v = splitNameAndSequence_(line);
140 if (v[0] != names[i])
141 throw IOException(
"NexusIOSequence::appendFromStream. Bad file, the sequences are not in the same order in interleaved blocks, or one taxon is missing.");
147 for (
size_t i = 0; i < names.size(); ++i)
149 auto seqPtr = make_unique<Sequence>(names[i], seqs[i], alphaPtr);
150 alignment->addSequence(seqPtr->getName(), seqPtr);
156 auto resolvedAlignment =
158 for (
size_t i = 0; i < resolvedAlignment->getNumberOfSequences(); ++i)
160 auto seqPtr = unique_ptr<Sequence>(resolvedAlignment->sequence(i).clone());
166 for (
size_t i = 0; i < alignment->getNumberOfSequences(); ++i)
168 auto seqPtr = unique_ptr<Sequence>(alignment->sequence(i).clone());
182 return "Nexus file format.";
void appendAlignmentFromStream(std::istream &input, SequenceContainerInterface &sc) const override
Append sequences to a container from a stream.
const std::vector< std::string > splitNameAndSequence_(const std::string &s) const
const std::string getFormatDescription() const override
const std::string getFormatName() const override
The SequenceContainer interface.
virtual void addSequence(const HashType &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr)=0
Add a sequence to the container.
std::string removeSurroundingWhiteSpaces(const std::string &s)
std::string toUpper(const std::string &s)
bool hasSubstring(const std::string &s, const std::string &pattern)
bool startsWith(const std::string &s, const std::string &pattern)
bool endsWith(const std::string &s, const std::string &pattern)
std::string removeFirstWhiteSpaces(const std::string &s)
This alphabet is used to deal NumericAlphabet.