10 #include "../StringSequenceTools.h"
11 #include "../Container/SequenceContainer.h"
22 throw IOException(
"Pasta::nextSequence : can't read from istream input");
25 vector<double> tokens;
28 string linebuffer =
"";
33 c =
static_cast<char>(input.peek());
45 getline(input, linebuffer);
50 seqname = string(linebuffer.begin() + 1, linebuffer.end());
67 bool res = (!input.eof());
70 if (strictNames_ || extended_)
72 size_t pos = seqname.find_first_of(
" \t\n");
75 if (pos != string::npos)
77 seqcmt = seqname.substr(pos + 1);
78 seqname = seqname.substr(0, pos);
91 seqcmts.push_back(seqcmt);
106 vector<double>::const_iterator i = tokens.begin();
107 while (i != tokens.end())
111 vector<double> row(size, 0.);
112 for (
const auto j:permutationMap)
114 if (i == tokens.end())
115 throw Exception(
"Pasta::nextSequence : input is incomplete");
133 for (
const auto& i : tokens)
138 vector<double> pair_v{1. - i, i};
159 throw IOException(
"Pasta::appendAlignmentFromStream: can't read from istream input");
170 bool hasLabels =
false;
171 vector<string> labels;
172 vector<size_t> permutationMap;
174 while (!input.eof() && hasSeq)
180 if (extended_ && c ==
'#')
189 if (extended_ && header)
193 line.erase(line.begin());
194 cmts.push_back(line);
209 if (!header && c !=
'>')
215 getline(input, line);
225 vector<string> resolved_chars = container.
getAlphabet()->getResolvedChars();
228 for (
const auto& i : labels)
232 for (
size_t j = 0; j < resolved_chars.size(); ++j)
234 if (i == resolved_chars[j])
237 throw Exception(
"Pasta::appendSequencesFromStream. Label " + i +
" found twice.");
239 permutationMap.push_back(j);
247 for (
const auto& i2 : resolved_chars)
252 throw Exception(
"Pasta::appendSequencesFromStream. Label " + i +
" is not found in alphabet " + states +
".");
258 if (c ==
'>' && last_c ==
'\n')
262 auto tmpseq = make_unique<ProbabilisticSequence>(alphaPtr);
263 hasSeq = nextSequence(input, *tmpseq, hasLabels, permutationMap);
267 if (extended_ && cmts.size())
279 throw IOException(
"Pasta::writeSequence : can't write to ostream output");
284 vector<string> resolved_chars = seq.
getAlphabet()->getResolvedChars();
285 for (
auto state : resolved_chars)
287 output << state <<
"\t";
293 output <<
">" << seq.
getName();
298 for (
size_t i = 0; i < seq.
getComments().size(); ++i)
308 const vector<vector<double>>& data = seq.
getContent();
321 throw IOException(
"Pasta::writeSequence : can't write to ostream output");
326 vector<string> resolved_chars = seq.
getAlphabet()->getResolvedChars();
327 for (
auto state : resolved_chars)
329 output << state <<
"\t";
335 output <<
">" << seq.
getName();
340 for (
size_t i = 0; i < seq.
getComments().size(); ++i)
349 int nbc = (int)seq.
getAlphabet()->getResolvedChars().size();
351 for (
size_t i = 0; i < seq.
size(); ++i)
353 for (
int s = 0; s < nbc; ++s)
const std::string & getName() const override
Get the name of this sequence.
void setName(const std::string &name) override
Set the name of this sequence.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
size_t size() const override
Get the number of elements in the list.
void writeSequence(std::ostream &output, const ProbabilisticSequence &seq, bool header) const
void appendAlignmentFromStream(std::istream &input, ProbabilisticSequenceContainerInterface &psc) const override
Append sequences to a container from a stream.
bool nextSequence(std::istream &input, ProbabilisticSequence &seq, bool hasLabels, const std::vector< size_t > &permutationMap) const
A basic implementation of the ProbabilisticSequence interface.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
const std::vector< std::vector< double > > & getContent() const override
void setContent(const std::vector< std::vector< double >> &list) override
A basic implementation of the Sequence interface.
double getStateValueAt(size_t sitePosition, int state) const override
get value of a state at a position
const std::string & nextToken()
bool hasMoreToken() const
const std::vector< std::vector< T > > & getData() const
void addColumn(const std::vector< T > &newColumn, int pos=-1)
The SequenceContainer interface.
virtual void addSequence(const HashType &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr)=0
Add a sequence to the container.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get a pointer toward the container's alphabet.
bool isWhiteSpaceCharacter(char c)
This alphabet is used to deal NumericAlphabet.
std::vector< std::string > Comments
Declaration of Comments type.