bpp-phyl3  3.0.0
Nhx.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_IO_NHX_H
6 #define BPP_PHYL_IO_NHX_H
7 
8 
9 #include "../Tree/PhyloTree.h"
10 #include "../Tree/TreeTemplate.h"
11 #include "IoTree.h"
12 
13 // From the STL:
14 #include <set>
15 
16 namespace bpp
17 {
53 class Nhx :
54  public AbstractITree,
55  public AbstractOTree,
56  public AbstractIMultiTree,
57  public AbstractOMultiTree,
58  public AbstractIPhyloTree,
59  public AbstractOPhyloTree,
62 {
63 public:
64  struct Property
65  {
66 public:
70  std::string name;
74  std::string tag;
78  bool onBranch;
82  short type;
83 
84 public:
85  Property(const std::string& pptName, const std::string& pptTag, bool pptOnBranch = false, short pptType = 0) :
86  name(pptName),
87  tag(pptTag),
88  onBranch(pptOnBranch),
89  type(pptType) {}
90 
91  bool operator<(const Property& ppt) const
92  {
93  return name < ppt.name;
94  }
95  };
96 
97 private:
98  std::set<Property> supportedProperties_;
100  mutable bool hasIds_;
101 
102 public:
110  Nhx(bool useTagsAsPptNames = true);
111  virtual ~Nhx() {}
112 
113 public:
119  const std::string getFormatName() const override;
120  const std::string getFormatDescription() const override;
121  /* @} */
122 
129 
130  std::unique_ptr<TreeTemplate<Node>> readTreeTemplate(std::istream& in) const override;
131 
133 
134  std::unique_ptr<PhyloTree> readPhyloTree(std::istream& in) const override;
135 
144 
145  void writeTree(const Tree& tree, std::ostream& out) const override
146  {
147  write_(tree, out);
148  }
149 
151 
152  void writePhyloTree(const PhyloTree& tree, std::ostream& out) const override
153  {
154  write_(tree, out);
155  }
164 
165  void readTrees(std::istream& in, std::vector<std::unique_ptr<Tree>>& trees) const override;
166 
168 
169  void readPhyloTrees(std::istream& in, std::vector<std::unique_ptr<PhyloTree>>& trees) const override;
170 
179 
180  void writeTrees(const std::vector<const Tree*>& trees, std::ostream& out) const override
181  {
182  write_(trees, out);
183  }
184 
186 
187  void writePhyloTrees(const std::vector<const PhyloTree*>& trees, std::ostream& out) const override
188  {
189  write_(trees, out);
190  }
193  std::unique_ptr<TreeTemplate<Node>> parenthesisToTree(const std::string& description) const;
194 
195  std::unique_ptr<PhyloTree> parenthesisToPhyloTree(const std::string& description) const;
196 
197  std::string treeToParenthesis(const TreeTemplate<Node>& tree) const;
198 
199  std::string treeToParenthesis(const PhyloTree& tree) const;
200 
201  void registerProperty(const Property& property)
202  {
203  supportedProperties_.insert(property);
204  }
205 
215  void changeTagsToNames(Node& node) const;
216  void changeTagsToNames(PhyloTree& tree, std::shared_ptr<PhyloNode> node) const;
217 
227  void changeNamesToTags(Node& node) const;
228  void changeNamesToTags(PhyloTree& tree, std::shared_ptr<PhyloNode> node) const;
229 
232 
233 protected:
234  void write_(const Tree& tree, std::ostream& out) const;
235 
236  void write_(const PhyloTree& tree, std::ostream& out) const;
237 
238  template<class N>
239  void write_(const TreeTemplate<N>& tree, std::ostream& out) const;
240 
241  void write_(const std::vector<const Tree*>& trees, std::ostream& out) const;
242 
243  void write_(const std::vector<const PhyloTree*>& trees, std::ostream& out) const;
244 
245  template<class N>
246  void write_(const std::vector<TreeTemplate<N>*>& trees, std::ostream& out) const;
247 
248  IOTree::Element getElement(const std::string& elt) const override;
249 
250 private:
251  Node* parenthesisToNode(const std::string& description) const;
252 
253  std::shared_ptr<PhyloNode> parenthesisToNode(PhyloTree& tree, std::shared_ptr<PhyloNode> father, const std::string& description) const;
254 
255 public:
256  std::string propertiesToParenthesis(const Node& node) const;
257 
258  std::string propertiesToParenthesis(const PhyloTree& tree, const std::shared_ptr<PhyloNode> node) const;
259 
260 protected:
261  std::string nodeToParenthesis(const Node& node) const;
262 
263  std::string nodeToParenthesis(const PhyloTree& tree, const std::shared_ptr<PhyloNode> node) const;
264 
265  bool setNodeProperties(Node& node, const std::string properties) const;
266 
267  bool setNodeProperties(PhyloTree& tree, std::shared_ptr<PhyloNode> node, const std::string properties) const;
268 
269 protected:
270  static std::string propertyToString_(const Clonable* pptObject, short type);
271  static Clonable* stringToProperty_(const std::string& pptDesc, short type);
272 
276  void checkNodesId_(PhyloTree& tree) const;
277 };
278 } // end of namespace bpp.
279 #endif // BPP_PHYL_IO_NHX_H
Partial implementation of the IMultiTree interface.
Definition: IoTree.h:477
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const override=0
Read trees from a stream.
Partial implementation of the IMultiTree interface.
Definition: IoTree.h:456
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
Read trees from a stream.
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
Read a tree from a stream.
Partial implementation of the ITree interface.
Definition: IoTree.h:189
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const =0
Partial implementation of the OTree interface.
Definition: IoTree.h:520
virtual void writePhyloTrees(const std::vector< const PhyloTree * > &trees, std::ostream &out) const =0
Write trees to a stream.
Partial implementation of the OTree interface.
Definition: IoTree.h:498
virtual void writeTrees(const std::vector< const Tree * > &trees, std::ostream &out) const =0
Write trees to a stream.
Partial implementation of the OTree interface.
Definition: IoTree.h:290
virtual void writePhyloTree(const PhyloTree &tree, std::ostream &out) const =0
Write a tree to a stream.
Partial implementation of the OTree interface.
Definition: IoTree.h:256
virtual void writeTree(const Tree &tree, std::ostream &out) const =0
Write a tree to a stream.
The so-called 'Nhx - New Hampshire eXtended' parenthetic format.
Definition: Nhx.h:62
Nhx(bool useTagsAsPptNames=true)
Build a new Nhx reader/writer.
Definition: Nhx.cpp:29
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const=0
std::string propertiesToParenthesis(const Node &node) const
Definition: Nhx.cpp:725
void changeNamesToTags(Node &node) const
Convert property names from names to tags.
Definition: Nhx.cpp:619
std::set< Property > supportedProperties_
Definition: Nhx.h:98
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const override=0
bool useTagsAsPropertyNames() const
Definition: Nhx.h:231
std::unique_ptr< PhyloTree > parenthesisToPhyloTree(const std::string &description) const
Definition: Nhx.cpp:407
const std::string getFormatDescription() const override
Definition: Nhx.cpp:60
void changeTagsToNames(Node &node) const
Convert property names from tag to names.
Definition: Nhx.cpp:590
std::unique_ptr< TreeTemplate< Node > > parenthesisToTree(const std::string &description) const
Definition: Nhx.cpp:334
static std::string propertyToString_(const Clonable *pptObject, short type)
Definition: Nhx.cpp:1024
const std::string getFormatName() const override
Definition: Nhx.cpp:56
IOTree::Element getElement(const std::string &elt) const override
Definition: Nhx.cpp:203
void writeTree(const Tree &tree, std::ostream &out) const override
Write a tree to a stream.
Definition: Nhx.h:145
void registerProperty(const Property &property)
Definition: Nhx.h:201
bool hasIds_
Definition: Nhx.h:100
std::string nodeToParenthesis(const Node &node) const
Definition: Nhx.cpp:802
std::string treeToParenthesis(const TreeTemplate< Node > &tree) const
Definition: Nhx.cpp:827
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
bool useTagsAsPropertyNames_
Definition: Nhx.h:99
void write_(const Tree &tree, std::ostream &out) const
Definition: Nhx.cpp:940
virtual ~Nhx()
Definition: Nhx.h:111
void writePhyloTree(const PhyloTree &tree, std::ostream &out) const override
Write a tree to a stream.
Definition: Nhx.h:152
static Clonable * stringToProperty_(const std::string &pptDesc, short type)
Definition: Nhx.cpp:1066
bool setNodeProperties(Node &node, const std::string properties) const
Definition: Nhx.cpp:479
void checkNodesId_(PhyloTree &tree) const
check and fill all nodes ids.
Definition: Nhx.cpp:431
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
void writePhyloTrees(const std::vector< const PhyloTree * > &trees, std::ostream &out) const override
Write trees to a stream.
Definition: Nhx.h:187
void writeTrees(const std::vector< const Tree * > &trees, std::ostream &out) const override
Write trees to a stream.
Definition: Nhx.h:180
void useTagsAsPropertyNames(bool yn)
Definition: Nhx.h:230
Node * parenthesisToNode(const std::string &description) const
Definition: Nhx.cpp:288
The phylogenetic node class.
Definition: Node.h:59
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
std::string tag
The tag of the property, as it will be found in the tree file.
Definition: Nhx.h:74
bool onBranch
Tells if the property is a branch property instead of a node property.
Definition: Nhx.h:78
bool operator<(const Property &ppt) const
Definition: Nhx.h:91
std::string name
The name of the property, which will be used in parsed trees.
Definition: Nhx.h:70
Property(const std::string &pptName, const std::string &pptTag, bool pptOnBranch=false, short pptType=0)
Definition: Nhx.h:85
short type
The type of the property. 0 is string, 1 is integer, 2 is double, 3 is boolean.
Definition: Nhx.h:82