11 #include "../Tree/PhyloBranch.h"
12 #include "../Tree/PhyloNode.h"
13 #include "../Tree/PhyloDAG.h"
32 return string(
"Extended Newick Format. ");
45 throw IOException (
"ExtendedNewick::readPhyloDAG: failed to read from stream");
49 string temp, description;
51 while (getline(in, temp,
'\n'))
53 string::size_type index = temp.find(
";");
54 if (index != string::npos)
56 description += temp.substr(0, index + 1);
66 throw IOException(
"ExtendedNewick::read: no dag was found!");
67 return parenthesisToPhyloDAG(description, verbose_);
78 throw IOException (
"ExtendedNewick::readPhyloDAGs(vector): failed to read from stream");
82 string temp, description;
83 string::size_type index;
85 while (getline(in, temp,
'\n'))
87 index = temp.find(
";");
88 if (index != string::npos)
90 description += temp.substr(0, index + 1);
93 dags.push_back(parenthesisToPhyloDAG(description, verbose_));
94 description = temp.substr(index + 1);
112 bool hasColon =
false;
113 for (colonIndex = elt.size(); colonIndex > 0 && elt[colonIndex] !=
')'; colonIndex--)
115 if (elt[colonIndex] ==
':')
127 elt2 = elt.substr(0, colonIndex);
136 string::size_type lastP = elt2.rfind(
')');
137 string::size_type firstP = elt2.find(
'(');
138 if (firstP == string::npos)
148 throw IOException(
"ExtendedNewick::getElement(). Invalid format: bad closing parenthesis in " + elt2);
167 shared_ptr<PhyloNode>
ExtendedNewick::parenthesisToNode(PhyloDAG& dag, std::shared_ptr<PhyloNode> father,
const std::string& description,
unsigned int& nodeCounter,
unsigned int& branchCounter, std::map<std::string, std::shared_ptr<PhyloNode> >& mapEvent,
bool withId,
bool verbose)
const
174 string annot = elt.annot;
175 size_t poshash = elf.find(
"#");
177 shared_ptr<PhyloNode> node;
181 if (poshash != string::pos)
183 string evId = annot.substr(poshash+1);
184 label = annot.substr(0, poshash);
186 if (mapEvent.find(evId)!=mapEvent.end())
190 node = std::make_shared<PhyloNode>();
192 dag.createNode(node);
198 shared_ptr<PhyloBranch> branch(father ?
new PhyloBranch() : 0);
202 dag.link(father, node, branch);
214 dag.setNodeIndex(node,
id);
222 vector<string> elements;
235 ostringstream realName;
244 node->setName(realName.str());
245 dag.setNodeIndex(node,
static_cast<PhyloDAG::NodeIndex
>(
248 dag.setEdgeIndex(branch,
static_cast<PhyloDAG::NodeIndex
>(
257 for (
size_t i = 0; i < elements.size(); i++)
260 parenthesisToNode(dag, node, elements[i], nodeCounter, branchCounter, mapEvent, withId, verbose);
266 if (dag.hasIndex(noode))
267 dag.setNodeIndex(node, nodeCounter);
269 dag.setEdgeIndex(branch, branchCounter);
283 string::size_type semi = description.rfind(
';');
284 if (semi == string::npos)
285 throw Exception(
"ExtendedNewick::parenthesisToPhyloDAG(). Bad format: no semi-colon found.");
286 string content = description.substr(0, semi);
287 unsigned int nodeCounter = 0;
288 unsigned int branchCounter = 0;
289 map<std::string, shared_ptr<PhyloNode> > mapEvent;
291 auto dag = make_unique<PhyloDAG>();
292 shared_ptr<PhyloNode> root =
parenthesisToNode(*dag, 0, content, nodeCounter, branchCounter, mapEvent, withId, verbose);
296 (*ApplicationTools::message) <<
" nodes loaded.";
312 throw IOException (
"ExtendedNewick::writePhyloDAG: failed to write to stream");
325 throw IOException (
"ExtendedNewick::write: failed to write to stream");
327 for (
unsigned int i = 0; i < dags.size(); i++)
379 shared_ptr<PhyloNode> root = dag.getRoot();
381 std::vector<shared_ptr<PhyloNode>> rSons = dag.getSons(root);
385 for (
size_t i = 0; i < rSons.size(); ++i)
394 s << root->getName();
396 for (
size_t i = 0; i < rSons.size(); ++i)
422 shared_ptr<PhyloNode> root = dag.getRoot();
424 std::vector<shared_ptr<PhyloNode>> rSons = dag.getSons(root);
428 for (
size_t i = 0; i < rSons.size(); ++i)
437 s << root->getName();
439 for (
size_t i = 0; i < rSons.size(); ++i)
void write_(const PhyloDAG &tree, std::ostream &out) const
const std::string getFormatDescription() const override
Element getElement(const std::string &elt) const override
std::string dagToParenthesis(const PhyloDAG &dag, bool writeId=false) const
Get the parenthesis description of a tree.
std::unique_ptr< PhyloDAG > readPhyloDAG(std::istream &in) const override=0
virtual void readPhyloDAGs(std::istream &in, std::vector< std::unique_ptr< PhyloDAG >> &dags) const override=0
std::shared_ptr< PhyloNode > parenthesisToNode(PhyloDAG &dag, std::shared_ptr< PhyloNode > father, const std::string &description, unsigned int &nodeCounter, unsigned int &branchCounter, map< str::string, shared_ptr< PhyloNode > > &mapEvent, bool withId, bool verbose) const
std::unique_ptr< PhyloDAG > parenthesisToPhyloDAG(const std::string &description, bool withId, bool verbose=false) const
std::string nodeToParenthesis(const PhyloDAG &dag, std::shared_ptr< PhyloNode > node, bool writeId=false) const
Get the ExtendedNewick description of a subdag.
const std::string getFormatName() const override
const std::string & nextToken()
size_t numberOfRemainingTokens() const
bool hasMoreToken() const
const std::string & getToken(size_t pos) const
int toInt(const std::string &s, char scientificNotation='e')
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
std::string removeSurroundingWhiteSpaces(const std::string &s)
std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
bool isEmpty(const std::string &s)
Defines the basic types of data flow nodes.