11#include "../Tree/PhyloBranch.h"
12#include "../Tree/PhyloNode.h"
13#include "../Tree/PhyloDAG.h"
26const string ExtendedNewick::getFormatName()
const {
return "ExtendedNewick"; }
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!");
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);
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)
149 throw IOException(
"ExtendedNewick::getElement(). Invalid format: bad closing parenthesis in " + elt2);
167shared_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
175 size_t poshash = annot.find(
"#");
177 shared_ptr<PhyloNode> node;
181 if (poshash != string::npos)
183 string evId = annot.substr(poshash + 1);
184 string label = annot.substr(0, poshash);
186 if (mapEvent.find(evId) != mapEvent.end())
187 node = mapEvent[evId];
190 node = std::make_shared<PhyloNode>(label);
194 node->setProperty(
"event", *event);
196 mapEvent[evId] = node;
202 node = std::make_shared<PhyloNode>(annot);
206 shared_ptr<PhyloBranch> branch(father ?
new PhyloBranch() : 0);
210 dag.
link(father, node, branch);
217 if (annot.size() != 0)
227 vector<string> elements;
240 ostringstream realName;
248 node->setName(realName.str());
258 for (
size_t i = 0; i < elements.size(); i++)
260 parenthesisToNode(dag, node, elements[i], nodeCounter, branchCounter, mapEvent, withId, verbose);
288 string::size_type semi = description.rfind(
';');
289 if (semi == string::npos)
290 throw Exception(
"ExtendedNewick::parenthesisToPhyloDAG(). Bad format: no semi-colon found.");
291 string content = description.substr(0, semi);
292 unsigned int nodeCounter = 0;
293 unsigned int branchCounter = 0;
294 map<std::string, shared_ptr<PhyloNode>> mapEvent;
296 auto dag = make_unique<PhyloDAG>();
297 shared_ptr<PhyloNode> root =
parenthesisToNode(*dag, 0, content, nodeCounter, branchCounter, mapEvent, withId, verbose);
301 (*ApplicationTools::message) <<
" nodes loaded.";
317 throw IOException (
"ExtendedNewick::writePhyloDAG: failed to write to stream");
330 throw IOException (
"ExtendedNewick::write: failed to write to stream");
332 for (
unsigned int i = 0; i < dags.size(); i++)
343 shared_ptr<PhyloNode> node = dag.
getSon(edge);
345 if (std::find(writtenNodes.begin(), writtenNodes.end(), node) != writtenNodes.end())
347 s << node->getName();
348 if (edge->hasLength())
349 s <<
":" << edge->getLength();
359 for (vector<shared_ptr<PhyloBranch>>::const_iterator it = vEdges.begin(); it != vEdges.end(); it++)
361 if (it != vEdges.begin())
368 s << node->getName();
377 if (edge->hasLength())
378 s <<
":" << edge->getLength();
380 writtenNodes.push_back(node);
392 shared_ptr<PhyloNode> root = dag.
getRoot();
394 std::vector<shared_ptr<PhyloNode>> writtenNodes;
400 for (
size_t i = 0; i < rEdges.size(); ++i)
409 s << root->getName();
411 for (
size_t i = 0; i < rEdges.size(); ++i)
std::shared_ptr< N > getSon(const std::shared_ptr< E > edge) const
size_t getNumberOfSons(const std::shared_ptr< N > node) const
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
bool isLeaf(const Nref node) const
virtual void link(std::shared_ptr< N > nodeObjectA, std::shared_ptr< N > nodeObjectB, std::shared_ptr< E > edgeObject=00)=0
virtual NodeIndex setNodeIndex(const std::shared_ptr< N > nodeObject, NodeIndex index)=0
virtual std::shared_ptr< N > getRoot() const=0
virtual bool hasNodeIndex(const std::shared_ptr< N > nodeObject) const=0
virtual EdgeIndex setEdgeIndex(const std::shared_ptr< E > edgeObject, EdgeIndex index)=0
virtual NodeIndex getNodeIndex(const std::shared_ptr< N > nodeObject) const=0
virtual std::vector< std::shared_ptr< E > > getOutgoingEdges(const std::shared_ptr< N > node) const=0
virtual void createNode(std::shared_ptr< N > newNodeObject)=0
std::shared_ptr< PhyloNode > 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
void write_(const PhyloDAG &tree, std::ostream &out) const
const std::string getFormatDescription() const override
void readPhyloDAGs(std::istream &in, std::vector< std::unique_ptr< PhyloDAG > > &dags) const override
Read dags from a stream.
Element getElement(const std::string &elt) const override
std::string edgeToParenthesis(const PhyloDAG &dag, std::shared_ptr< PhyloBranch > edge, std::vector< std::shared_ptr< PhyloNode > > &writtenNodes, bool writeId=false) const
Get the ExtendedNewick description of a subdag.
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
Read a DAG from a stream.
std::unique_ptr< PhyloDAG > parenthesisToPhyloDAG(const std::string &description, bool withId, bool verbose=false) const
const std::string & nextToken()
static const NodeEvent hybridizationEvent
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.