bpp-phyl3  3.0.0
Newick.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_NEWICK_H
6 #define BPP_PHYL_IO_NEWICK_H
7 
8 
9 #include "../Tree/PhyloTree.h"
10 #include "../Tree/TreeTemplate.h"
11 #include "IoTree.h"
12 
13 namespace bpp
14 {
49 class Newick :
50  public AbstractITree,
51  public AbstractOTree,
52  public AbstractIMultiTree,
53  public AbstractOMultiTree,
54  public AbstractIPhyloTree,
55  public AbstractOPhyloTree,
58 {
59 protected:
61  bool writeId_;
64  bool verbose_;
65 
66 public:
76  Newick(bool allowComments = false, bool writeId = false, bool verbose = false) :
77  allowComments_(allowComments),
78  writeId_(writeId),
79  useBootstrap_(true),
80  bootstrapPropertyName_("bootstrap"),
81  verbose_(verbose) {}
82 
83  virtual ~Newick() {}
84 
85 public:
86  void enableExtendedBootstrapProperty(const std::string& propertyName)
87  {
88  useBootstrap_ = false;
89  bootstrapPropertyName_ = propertyName;
90  }
91 
93  {
94  useBootstrap_ = true;
95  bootstrapPropertyName_ = "bootstrap";
96  }
97 
103  const std::string getFormatName() const override;
104  const std::string getFormatDescription() const override;
105  /* @} */
106 
113 
114  std::unique_ptr<TreeTemplate<Node>> readTreeTemplate(std::istream& in) const override;
115 
117 
118  std::unique_ptr<PhyloTree> readPhyloTree(std::istream& in) const override;
119 
120 private:
121  std::shared_ptr<PhyloNode> parenthesisToNode(
122  PhyloTree& tree,
123  std::shared_ptr<PhyloNode> father,
124  const std::string& description,
125  unsigned int& nodeCounter,
126  bool bootstrap,
127  const std::string& propertyName,
128  bool withId,
129  bool verbose) const;
130 
131 public:
132  std::unique_ptr<PhyloTree> parenthesisToPhyloTree(
133  const std::string& description,
134  bool bootstrap = false,
135  const std::string& propertyName = "",
136  bool withId = false,
137  bool verbose = false) const;
138 
147 public:
149 
150  void writeTree(const Tree& tree, std::ostream& out) const override
151  {
152  write_(tree, out);
153  }
154 
156 
157  void writePhyloTree(const PhyloTree& tree, std::ostream& out) const override
158  {
159  write_(tree, out);
160  }
170 
171  void readTrees(
172  std::istream& in,
173  std::vector<std::unique_ptr<Tree>>& trees) const override;
174 
176 
177  void readPhyloTrees(
178  std::istream& in,
179  std::vector<std::unique_ptr<PhyloTree>>& trees) const override;
180 
190 
191  void writeTrees(const std::vector<const Tree*>& trees, std::ostream& out) const override
192  {
193  write_(trees, out);
194  }
195 
197 
198  void writePhyloTrees(const std::vector<const PhyloTree*>& trees, std::ostream& out) const override
199  {
200  write_(trees, out);
201  }
204 protected:
205  void write_(const Tree& tree, std::ostream& out) const;
206 
207  void write_(const PhyloTree& tree, std::ostream& out) const;
208 
209  template<class N>
210  void write_(const TreeTemplate<N>& tree, std::ostream& out) const;
211 
212  void write_(const std::vector<const Tree*>& trees, std::ostream& out) const;
213 
214  void write_(const std::vector<const PhyloTree*>& trees, std::ostream& out) const;
215 
216  template<class N>
217  void write_(const std::vector<TreeTemplate<N>*>& trees, std::ostream& out) const;
218 
219  IOTree::Element getElement(const std::string& elt) const override;
220 
232  std::string nodeToParenthesis(const PhyloTree& tree, std::shared_ptr<PhyloNode> node, bool writeId = false) const;
233 
234 /* @brief Get the parenthesis description of a subtree.
235  *
236  * @param tree The tree
237  * @param node The node defining the subtree.
238  * @param bootstrap Tell is bootstrap values must be written.
239  * If so, the content of the property with name "bootstrap" will be written as bootstrap value.
240  * The property should be a Number<double> object.
241  * Otherwise, the content of the property with name 'propertyName' will be written.
242  * In this later case, the property should be a String object.
243  * @param propertyName The name of the property to use. Only used if bootstrap = false.
244  * @return A string in the parenthesis format.
245  */
246 
247  std::string nodeToParenthesis(const PhyloTree& tree, std::shared_ptr<PhyloNode> node, bool bootstrap, const std::string& propertyName) const;
248 
259  std::string treeToParenthesis(const PhyloTree& tree, bool writeId = false) const;
260 
273  std::string treeToParenthesis(const PhyloTree& tree, bool bootstrap, const std::string& propertyName) const;
274 };
275 } // end of namespace bpp.
276 #endif // BPP_PHYL_IO_NEWICK_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 'newick' parenthetic format.
Definition: Newick.h:58
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const=0
void writeTrees(const std::vector< const Tree * > &trees, std::ostream &out) const override
Write trees to a stream.
Definition: Newick.h:191
void enableExtendedBootstrapProperty(const std::string &propertyName)
Definition: Newick.h:86
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const override=0
virtual ~Newick()
Definition: Newick.h:83
std::shared_ptr< PhyloNode > parenthesisToNode(PhyloTree &tree, std::shared_ptr< PhyloNode > father, const std::string &description, unsigned int &nodeCounter, bool bootstrap, const std::string &propertyName, bool withId, bool verbose) const
Definition: Newick.cpp:233
std::unique_ptr< PhyloTree > parenthesisToPhyloTree(const std::string &description, bool bootstrap=false, const std::string &propertyName="", bool withId=false, bool verbose=false) const
Definition: Newick.cpp:336
void writePhyloTree(const PhyloTree &tree, std::ostream &out) const override
Write a tree to a stream.
Definition: Newick.h:157
std::string nodeToParenthesis(const PhyloTree &tree, std::shared_ptr< PhyloNode > node, bool writeId=false) const
Get the Newick description of a subtree.
Definition: Newick.cpp:483
void write_(const Tree &tree, std::ostream &out) const
Definition: Newick.cpp:359
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
IOTree::Element getElement(const std::string &elt) const override
Definition: Newick.cpp:170
const std::string getFormatDescription() const override
Definition: Newick.cpp:34
void writeTree(const Tree &tree, std::ostream &out) const override
Write a tree to a stream.
Definition: Newick.h:150
std::string bootstrapPropertyName_
Definition: Newick.h:63
Newick(bool allowComments=false, bool writeId=false, bool verbose=false)
Build a new Newick reader/writer.
Definition: Newick.h:76
void writePhyloTrees(const std::vector< const PhyloTree * > &trees, std::ostream &out) const override
Write trees to a stream.
Definition: Newick.h:198
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
std::string treeToParenthesis(const PhyloTree &tree, bool writeId=false) const
Get the parenthesis description of a tree.
Definition: Newick.cpp:576
const std::string getFormatName() const override
Definition: Newick.cpp:30
bool useBootstrap_
Definition: Newick.h:62
bool verbose_
Definition: Newick.h:64
void disableExtendedBootstrapProperty()
Definition: Newick.h:92
bool writeId_
Definition: Newick.h:61
bool allowComments_
Definition: Newick.h:60
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.