bpp-phyl  2.2.0
Nhx.h
Go to the documentation of this file.
1 //
2 // File: Nhx.h
3 // Created by: Bastien Boussau
4 // Created on: Tu Oct 19 10:24:03 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for phylogenetic data analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _NHX_H_
41 #define _NHX_H_
42 
43 #include "IoTree.h"
44 #include "../TreeTemplate.h"
45 
46 //From the STL:
47 #include <set>
48 
49 namespace bpp
50 {
51 
87 class Nhx:
88  public AbstractITree,
89  public AbstractOTree,
90  public AbstractIMultiTree,
91  public AbstractOMultiTree
92 {
93  private:
94  struct Element
95  {
96  public:
97  std::string content;
98  std::string length;
99  std::string annotation;
100  bool isLeaf;
101 
102  public:
103  Element() : content(), length(), annotation(), isLeaf(false) {}
104  };
105 
106  public:
107  struct Property
108  {
109  public:
113  std::string name;
117  std::string tag;
121  bool onBranch;
125  short type;
126 
127  public:
128  Property(const std::string& pptName, const std::string& pptTag, bool pptOnBranch = false, short pptType = 0):
129  name(pptName), tag(pptTag), onBranch(pptOnBranch), type(pptType) {}
130 
131  bool operator<(const Property& ppt) const {
132  return (name < ppt.name);
133  }
134 
135  };
136 
137  private:
138  std::set<Property> supportedProperties_;
140  mutable bool hasIds_;
141 
142  public:
143 
151  Nhx(bool useTagsAsPptNames = true);
152  virtual ~Nhx() {}
153 
154  public:
155 
161  const std::string getFormatName() const;
162  const std::string getFormatDescription() const;
163  /* @} */
164 
170  TreeTemplate<Node>* read(const std::string& path) const throw (Exception)
171  {
172  return dynamic_cast<TreeTemplate<Node>*>(AbstractITree::read(path));
173  }
174 
175  TreeTemplate<Node>* read(std::istream& in) const throw (Exception);
183  void write(const Tree& tree, const std::string& path, bool overwrite = true) const throw (Exception)
184  {
185  AbstractOTree::write(tree, path, overwrite);
186  }
187 
188  void write(const Tree& tree, std::ostream& out) const throw (Exception)
189  {
190  write_(tree, out);
191  }
199  void read(const std::string& path, std::vector<Tree*>& trees) const throw (Exception)
200  {
201  AbstractIMultiTree::read(path, trees);
202  }
203  void read(std::istream& in, std::vector<Tree*>& trees) const throw (Exception);
211  void write(const std::vector<Tree*>& trees, const std::string& path, bool overwrite = true) const throw (Exception)
212  {
213  AbstractOMultiTree::write(trees, path, overwrite);
214  }
215  void write(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception)
216  {
217  write_(trees, out);
218  }
221  TreeTemplate<Node>* parenthesisToTree(const std::string& description) const throw (Exception);
222 
223  std::string treeToParenthesis(const TreeTemplate<Node>& tree) const;
224 
225  void registerProperty(const Property& property) {
226  supportedProperties_.insert(property);
227  }
228 
238  void changeTagsToNames(Node& node) const;
239 
249  void changeNamesToTags(Node& node) const;
250 
253 
254  protected:
255  void write_(const Tree& tree, std::ostream& out) const throw (Exception);
256 
257  template<class N>
258  void write_(const TreeTemplate<N>& tree, std::ostream& out) const throw (Exception);
259 
260  void write_(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception);
261 
262  template<class N>
263  void write_(const std::vector<TreeTemplate<N>*>& trees, std::ostream& out) const throw (Exception);
264 
265  Element getElement(const std::string& elt) const throw (IOException);
266 
267  Node* parenthesisToNode(const std::string& description) const;
268 
269  std::string propertiesToParenthesis(const Node& node) const;
270 
271  std::string nodeToParenthesis(const Node& node) const;
272 
273  bool setNodeProperties(Node& node, const std::string properties) const;
274 
275  static std::string propertyToString_(const Clonable* pptObject, short type) throw (Exception);
276  static Clonable* stringToProperty_(const std::string& pptDesc, short type) throw (Exception);
277 };
278 
279 } //end of namespace bpp.
280 
281 #endif //_NHX_H_
282 
void write(const std::vector< Tree *> &trees, std::ostream &out) const =0
Write trees to a stream.
short type
The type of the property. 0 is string, 1 is integer, 2 is double, 3 is boolean.
Definition: Nhx.h:125
void changeNamesToTags(Node &node) const
Convert property names from names to tags.
Definition: Nhx.cpp:553
Element getElement(const std::string &elt) const
Definition: Nhx.cpp:206
void write(const Tree &tree, std::ostream &out) const =0
Write a tree to a stream.
Partial implementation of the ITree interface.
Definition: IoTree.h:134
bool hasIds_
Definition: Nhx.h:140
The so-called &#39;Nhx - New Hampshire eXtended&#39; parenthetic format.
Definition: Nhx.h:87
bool onBranch
Tells if the property is a branch property instead of a node property.
Definition: Nhx.h:121
TreeTemplate< Node > * parenthesisToTree(const std::string &description) const
Definition: Nhx.cpp:334
The phylogenetic tree class.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
void registerProperty(const Property &property)
Definition: Nhx.h:225
std::string nodeToParenthesis(const Node &node) const
Definition: Nhx.cpp:433
void write(const Tree &tree, const std::string &path, bool overwrite=true) const
Write a tree to a file.
Definition: Nhx.h:183
bool useTagsAsPropertyNames_
Definition: Nhx.h:139
std::string tag
The tag of the property, as it will be found in the tree file.
Definition: Nhx.h:117
void useTagsAsPropertyNames(bool yn)
Definition: Nhx.h:251
const std::string getFormatDescription() const
Definition: Nhx.cpp:91
std::set< Property > supportedProperties_
Definition: Nhx.h:138
std::string name
The name of the property, which will be used in parsed trees.
Definition: Nhx.h:113
std::string content
Definition: Nhx.h:97
void write(const std::vector< Tree *> &trees, std::ostream &out) const
Write trees to a stream.
Definition: Nhx.h:215
const std::string getFormatName() const
Definition: Nhx.cpp:87
std::string length
Definition: Nhx.h:98
void write(const Tree &tree, std::ostream &out) const
Write a tree to a stream.
Definition: Nhx.h:188
Node * parenthesisToNode(const std::string &description) const
Definition: Nhx.cpp:287
std::string propertiesToParenthesis(const Node &node) const
Definition: Nhx.cpp:404
bool isLeaf
Definition: Nhx.h:100
void changeTagsToNames(Node &node) const
Convert property names from tag to names.
Definition: Nhx.cpp:533
std::string treeToParenthesis(const TreeTemplate< Node > &tree) const
Definition: Nhx.cpp:457
The phylogenetic node class.
Definition: Node.h:90
void read(const std::string &path, std::vector< Tree *> &trees) const
Read trees from a file.
Definition: Nhx.h:199
virtual Tree * read(std::istream &in) const =0
Read a tree from a stream.
virtual void read(std::istream &in, std::vector< Tree *> &trees) const =0
Read trees from a stream.
TreeTemplate< Node > * read(const std::string &path) const
Read a tree from a file.
Definition: Nhx.h:170
Nhx(bool useTagsAsPptNames=true)
Build a new Nhx reader/writer.
Definition: Nhx.cpp:60
void write_(const Tree &tree, std::ostream &out) const
Definition: Nhx.cpp:131
bool setNodeProperties(Node &node, const std::string properties) const
Definition: Nhx.cpp:490
Property(const std::string &pptName, const std::string &pptTag, bool pptOnBranch=false, short pptType=0)
Definition: Nhx.h:128
bool useTagsAsPropertyNames() const
Definition: Nhx.h:252
Partial implementation of the OTree interface.
Definition: IoTree.h:156
void write(const std::vector< Tree *> &trees, const std::string &path, bool overwrite=true) const
Write trees to a file.
Definition: Nhx.h:211
bool operator<(const Property &ppt) const
Definition: Nhx.h:131
std::string annotation
Definition: Nhx.h:99
Partial implementation of the OTree interface.
Definition: IoTree.h:274
virtual ~Nhx()
Definition: Nhx.h:152
Partial implementation of the IMultiTree interface.
Definition: IoTree.h:253
static Clonable * stringToProperty_(const std::string &pptDesc, short type)
Definition: Nhx.cpp:386
static std::string propertyToString_(const Clonable *pptObject, short type)
Definition: Nhx.cpp:353