5 #include "../Tree/PhyloBranch.h"
6 #include "../Tree/PhyloBranch.h"
7 #include "../Tree/PhyloNode.h"
8 #include "../Tree/Tree.h"
9 #include "../Tree/TreeTemplate.h"
30 supportedProperties_(),
31 useTagsAsPropertyNames_(useTagsAsPptNames),
62 return string(
"New Hampshire eXtended parenthesis format. ") +
63 "See http://www.phylosoft.org/NHX/ for more info.";
76 throw IOException (
"Nhx ::read: failed to read from stream");
80 string temp, description;
84 getline(in, temp,
'\n');
85 string::size_type index = temp.find(
";");
86 if (index != string::npos)
88 description += temp.substr(0, index + 1);
94 vector<string> beginnings, endings;
95 beginnings.push_back(
"[&&NHX:");
107 throw IOException (
"Nhx ::read: failed to read from stream");
111 string temp, description;
115 getline(in, temp,
'\n');
116 string::size_type index = temp.find(
";");
117 if (index != string::npos)
119 description += temp.substr(0, index + 1);
125 vector<string> beginnings, endings;
126 beginnings.push_back(
"[&&NHX:");
138 throw IOException (
"Nhx::read: failed to read from stream");
142 string temp, description;
143 string::size_type index;
144 vector<string> beginnings, endings;
145 beginnings.push_back(
"[&&NHX:");
151 getline(in, temp,
'\n');
152 index = temp.find(
";");
153 if (index != string::npos)
155 description += temp.substr(0, index + 1);
158 description = temp.substr(index + 1);
173 throw IOException (
"Nhx::read: failed to read from stream");
177 string temp, description;
178 string::size_type index;
179 vector<string> beginnings, endings;
180 beginnings.push_back(
"[&&NHX:");
186 getline(in, temp,
'\n');
187 index = temp.find(
";");
188 if (index != string::npos)
190 description += temp.substr(0, index + 1);
193 description = temp.substr(index + 1);
211 size_t lastP = elt.rfind(
")"), firstP;
212 size_t beginAnno = string::npos;
213 if (lastP == string::npos)
214 beginAnno = elt.rfind(
"[&&NHX:");
216 beginAnno = elt.find(
"[&&NHX:", lastP + 1);
217 string elementWithoutAnnotation;
218 if (beginAnno != string::npos)
220 size_t endAnno = elt.find(
"]", beginAnno + 7);
221 element.annotation = elt.substr(beginAnno + 7, endAnno - beginAnno - 7);
222 elementWithoutAnnotation = elt.substr(0, beginAnno);
227 elementWithoutAnnotation = elt;
233 bool hasColon =
false;
234 for (colonIndex = elementWithoutAnnotation.size() - 1; colonIndex > 0 && elementWithoutAnnotation[colonIndex] !=
')' && !hasColon; --colonIndex)
236 if (elementWithoutAnnotation[colonIndex] ==
':')
247 elt2 = elementWithoutAnnotation.substr(0, colonIndex + 1);
248 element.length = elementWithoutAnnotation.substr(colonIndex + 2);
253 elt2 = elementWithoutAnnotation;
256 lastP = elt2.rfind(
')');
257 firstP = elt2.find(
'(');
258 if (firstP == string::npos)
268 throw IOException(
"Nhx::getElement(). Invalid format: bad closing parenthesis in " + elt2);
269 element.content = elt2.substr(firstP + 1, lastP - firstP - 1);
304 throw Exception(
"Nhx::parenthesisToNode. At least one node is missing an id (ND tag).");
308 vector<string> elements;
322 for (
size_t i = 0; i < elements.size(); ++i)
337 string::size_type semi = description.rfind(
';');
338 if (semi == string::npos)
339 throw Exception(
"Nhx::parenthesisToTree(). Bad format: no semi-colon found.");
340 string content = description.substr(0, semi);
342 auto tree = make_unique<TreeTemplate<Node>>();
343 tree->setRootNode(node);
346 tree->resetNodesId();
358 shared_ptr<PhyloNode> node(
new PhyloNode());
381 vector<string> elements;
396 for (
size_t i = 0; i < elements.size(); ++i)
410 string::size_type semi = description.rfind(
';');
411 if (semi == string::npos)
412 throw Exception(
"Nhx::parenthesisToPhyloTree(). Bad format: no semi-colon found.");
413 string content = description.substr(0, semi);
415 auto tree = make_unique<PhyloTree>();
421 tree->resetNodesId();
425 Vuint vid = tree->getAllNodesIndexes();
436 vector<shared_ptr<PhyloNode>> vNode;
438 for ( ; !nIT->end(); nIT->next())
443 vNode.push_back(**nIT);
452 std::sort(nid.begin(), nid.end());
454 Vuint::iterator it(nid.begin());
456 uint val = *(it++) + 1;
457 while (it != nid.end() && val == *it)
463 for (
auto& node:vNode)
469 while (it != nid.end() && val == *it)
483 map<string, string> props;
498 if (props.find(it->tag) != props.end())
516 if (props.find(
"ND") != props.end())
518 string prop = props[
"ND"];
534 map<string, string> props;
551 if (props.find(it->tag) != props.end())
559 branch->setProperty(ppt, *unique_ptr<Clonable>(
stringToProperty_(props[it->tag], it->type)));
563 node->setProperty(ppt, *unique_ptr<Clonable>(
stringToProperty_(props[it->tag], it->type)));
571 if (props.find(
"ND") != props.end())
573 string prop = props[
"ND"];
658 if (branch->hasProperty(it->tag))
660 branch->setProperty(it->name, *branch->getProperty(it->tag));
661 branch->deleteProperty(it->tag);
667 if (node->hasProperty(it->tag))
669 node->setProperty(it->name, *node->getProperty(it->tag));
670 node->deleteProperty(it->tag);
675 vector<shared_ptr<PhyloNode>> vs = tree.
getSons(node);
677 for (
unsigned int i = 0; i < vs.size(); ++i)
695 if (branch->hasProperty(it->name))
697 branch->setProperty(it->tag, *branch->getProperty(it->name));
698 branch->deleteProperty(it->name);
704 if (node->hasProperty(it->name))
706 node->setProperty(it->tag, *node->getProperty(it->name));
707 node->deleteProperty(it->name);
712 vector<shared_ptr<PhyloNode>> vs = tree.
getSons(node);
714 for (
unsigned int i = 0; i < vs.size(); ++i)
775 if (branch->hasProperty(ppt))
777 const Clonable* pptObject = branch->getProperty(ppt);
784 if (node->hasProperty(ppt))
786 const Clonable* pptObject = node->getProperty(ppt);
866 s << node->getName();
872 vector<shared_ptr<PhyloNode>> vSons = tree.
getSons(node);
874 for (vector<shared_ptr<PhyloNode>>::const_iterator it = vSons.begin(); it != vSons.end(); it++)
876 if (it != vSons.begin())
887 if (branch && branch->hasLength())
888 s <<
":" << branch->getLength();
901 shared_ptr<PhyloNode> root = tree.
getRoot();
902 std::vector<shared_ptr<PhyloNode>> rSons = tree.
getSons(root);
906 for (
size_t i = 0; i < rSons.size(); ++i)
915 s << root->getName();
917 for (
size_t i = 0; i < rSons.size(); ++i)
929 if (branch && branch->hasLength())
930 s <<
":" << branch->getLength();
946 throw IOException (
"Nhx::writeTree: failed to write to stream");
956 throw IOException (
"Nhx::writeTree: failed to write to stream");
969 throw IOException (
"Nhx::writeTree: failed to write to stream");
976 void Nhx::write_(
const vector<const Tree*>& trees, ostream& out)
const
981 throw IOException (
"Nhx::write: failed to write to stream");
983 for (
unsigned int i = 0; i < trees.size(); i++)
990 void Nhx::write_(
const vector<const PhyloTree*>& trees, ostream& out)
const
997 throw IOException (
"Nhx::write: failed to write to stream");
999 for (
unsigned int i = 0; i < trees.size(); i++)
1014 throw IOException (
"Nhx::write: failed to write to stream");
1016 for (
unsigned int i = 0; i < trees.size(); i++)
1029 if (castedPptObject)
1030 return castedPptObject->
toSTL();
1032 throw Exception(
"Nhx::propertyToString_. Invalid property type, should be of class BppString.");
1037 if (castedPptObject)
1040 throw Exception(
"Nhx::propertyToString_. Invalid property type, should be of class Number<int>.");
1045 if (castedPptObject)
1048 throw Exception(
"Nhx::propertyToString_. Invalid property type, should be of class Number<double>.");
1053 if (castedPptObject)
1056 throw Exception(
"Nhx::propertyToString_. Invalid property type, should be of class BppBoolean.");
1082 return new BppBoolean(TextTools::to<bool>(pptDesc));
size_t getNumberOfNodes() const
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::unique_ptr< NodeIterator > allNodesIterator()=0
virtual void createNode(std::shared_ptr< N > newNodeObject)=0
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
std::shared_ptr< E > getEdgeToFather(const std::shared_ptr< N > nodeObject) const
size_t getNumberOfSons(const std::shared_ptr< N > node) const
const bool getValue() const
const std::string & toSTL() const
const std::string & nextToken()
Nhx(bool useTagsAsPptNames=true)
Build a new Nhx reader/writer.
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const=0
std::string propertiesToParenthesis(const Node &node) const
void changeNamesToTags(Node &node) const
Convert property names from names to tags.
std::set< Property > supportedProperties_
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const override=0
std::unique_ptr< PhyloTree > parenthesisToPhyloTree(const std::string &description) const
const std::string getFormatDescription() const override
void changeTagsToNames(Node &node) const
Convert property names from tag to names.
std::unique_ptr< TreeTemplate< Node > > parenthesisToTree(const std::string &description) const
static std::string propertyToString_(const Clonable *pptObject, short type)
const std::string getFormatName() const override
IOTree::Element getElement(const std::string &elt) const override
void registerProperty(const Property &property)
std::string nodeToParenthesis(const Node &node) const
std::string treeToParenthesis(const TreeTemplate< Node > &tree) const
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
bool useTagsAsPropertyNames_
void write_(const Tree &tree, std::ostream &out) const
static Clonable * stringToProperty_(const std::string &pptDesc, short type)
bool setNodeProperties(Node &node, const std::string properties) const
void checkNodesId_(PhyloTree &tree) const
check and fill all nodes ids.
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
Node * parenthesisToNode(const std::string &description) const
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
virtual Clonable * getNodeProperty(const std::string &name)
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
virtual int getId() const
Get the node's id.
virtual bool hasBranchProperty(const std::string &name) const
virtual void setId(int id)
Set this node's id.
virtual void addSon(size_t pos, Node *node)
virtual const Node * getSon(size_t pos) const
virtual void setBranchProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
virtual void setNodeProperty(const std::string &name, const Clonable &property)
Set/add a node property.
virtual bool hasNoSon() const
virtual void deleteNodeProperty(const std::string &name)
virtual Clonable * getBranchProperty(const std::string &name)
virtual void deleteBranchProperty(const std::string &name)
virtual void setName(const std::string &name)
Give a name or update the name associated to the node.
virtual bool hasNodeProperty(const std::string &name) const
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual size_t getNumberOfSons() const
const std::string & nextToken()
bool hasMoreToken() const
The phylogenetic tree class.
virtual N * getRootNode()
Interface for phylogenetic tree objects.
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)
bool hasSubstring(const std::string &s, const std::string &pattern)
std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
bool isEmpty(const std::string &s)
std::string removeChar(const std::string &s, char c)
bool isDecimalNumber(char c)
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< unsigned int > Vuint