9 #include "../Container/SequenceContainerTools.h"
26 string::size_type index = s.find(namesSplit_);
27 if (index == string::npos)
28 throw Exception(
"No sequence name found.");
48 getline(in, temp,
'\n');
60 v = splitNameAndSequence(temp);
72 auto seqPtr = make_unique<Sequence>(name, seq, alphaPtr);
82 throw Exception(
"First sequence in file has no name!");
95 auto seqPtr = make_unique<Sequence>(name, seq, alphaPtr);
107 getline(in, temp,
'\n');
109 unsigned int nbSequences = TextTools::to<unsigned int>(st.
nextToken());
113 vector<string> names, seqs;
117 vector<string> v = splitNameAndSequence(temp);
118 names.push_back(v[0]);
119 seqs.push_back(v[1]);
120 getline(in, temp,
'\n');
127 for (
size_t i = 0; i < names.size(); ++i)
130 throw IOException(
"Phylip::readInterleaved. Bad file,there are not the same number of sequence in each block.");
132 getline(in, temp,
'\n');
136 for (
size_t i = 0; i < names.size(); ++i)
138 auto seqPtr = make_unique<Sequence>(names[i], seqs[i], alphaPtr);
150 throw IOException (
"Phylip::read: fail to open file");
154 readSequential (input, sc);
156 readInterleaved(input, sc);
164 ifstream file (path.c_str(), ios::in);
167 throw IOException (
"Phylip::getNumberOfSequences: failed to open file");
182 vector<string> sizedNames(names.size());
188 for (
size_t i = 0; i < names.size(); i++)
190 if (names[i].size() > sizeMax)
191 sizeMax = names[i].size();
194 for (
size_t i = 0; i < names.size(); i++)
202 for (
unsigned int i = 0; i < names.size(); i++)
206 cout <<
"Warning: names have been truncated to 10 characters. They may be ambiguous sequence names then." << endl;
220 vector<string> names = getSizedNames(seqNames);
224 out << names[i] << seq[0] << endl;
225 for (
size_t j = 1; j < seq.size(); ++j)
227 out << string(names[i].size(),
' ') << seq[j] << endl;
240 vector<string> names = getSizedNames(seqNames);
248 for (
size_t i = 0; i < names.size(); ++i)
250 out << names[i] << seqs[i][0] << endl;
254 for (
size_t j = 1; j < seqs[0].size(); ++j)
258 out << seqs[i][j] << endl;
270 throw Exception(
"Phylip::write. SequenceContainer appear to contain no sequence.");
275 throw IOException (
"Phylip::write : failed to open file");
279 writeSequential (output, sc);
281 writeInterleaved(output, sc);
286 const std::string
Phylip::getFormatName()
const {
return "Phylip file, " + string(extended_ ?
"extended," :
"") + string(sequential_ ?
"sequential" :
"interleaved"); }
292 return "Phylip file format, sequential and interleaved. PAML extension also supported.";
void writeSequential(std::ostream &out, const SiteContainerInterface &sc) const
const std::string getFormatDescription() const override
void readSequential(std::istream &in, SequenceContainerInterface &asc) const
void writeInterleaved(std::ostream &out, const SiteContainerInterface &sc) const
unsigned int getNumberOfSequences(const std::string &path) const
void writeAlignment(std::ostream &output, const SiteContainerInterface &sc) const override
Write a container to a stream.
const std::string getFormatName() const override
std::vector< std::string > getSizedNames(const std::vector< std::string > &names) const
const std::vector< std::string > splitNameAndSequence(const std::string &s) const
void readInterleaved(std::istream &in, SequenceContainerInterface &asc) const
void appendAlignmentFromStream(std::istream &input, SequenceContainerInterface &sc) const override
Append sequences to a container from a stream.
const std::string & nextToken()
The SequenceContainer interface.
virtual void addSequence(const HashType &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr)=0
Add a sequence to the container.
virtual const SequenceType & sequence(const HashType &sequenceKey) const override=0
Retrieve a sequence object from the container.
virtual std::vector< std::string > getSequenceNames() const =0
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get a pointer toward the container's alphabet.
std::string removeWhiteSpaces(const std::string &s)
std::string removeSurroundingWhiteSpaces(const std::string &s)
bool isEmpty(const std::string &s)
std::vector< std::string > split(const std::string &s, std::size_t n)
std::string removeFirstWhiteSpaces(const std::string &s)
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
This alphabet is used to deal NumericAlphabet.