bpp-phyl3  3.0.0
IoTree.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_IOTREE_H
6 #define BPP_PHYL_IO_IOTREE_H
7 
8 
9 #include "../Tree/PhyloTree.h"
10 #include "../Tree/Tree.h"
11 
12 // From the STL:
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <sstream>
17 #include <memory>
18 
19 #include <Bpp/Exceptions.h>
20 #include <Bpp/Io/IoFormat.h>
21 
22 namespace bpp
23 {
27 class IOTree :
28  public virtual IOFormat
29 {
30 protected:
31  struct Element
32  {
33 public:
34  std::string content;
35  std::string length;
36  std::string annotation;
37  bool isLeaf;
38 
39 public:
41  length(),
42  annotation(),
43  isLeaf(false) {}
44  };
45 
46 public:
47  IOTree() {}
48  virtual ~IOTree() {}
49 
50 public:
51  virtual const std::string getDataType() const { return "Tree"; }
52 };
53 
57 class ITree :
58  public virtual IOTree
59 {
60  /*
61  * @brief Basic element for branch description.
62  *
63  */
64 
65 public:
66  ITree() {}
67  virtual ~ITree() {}
68 
69 public:
77  virtual std::unique_ptr<Tree> readTree(const std::string& path) const = 0;
78 
86  virtual std::unique_ptr<Tree> readTree(std::istream& in) const = 0;
87 };
88 
92 class IPhyloTree :
93  public virtual IOTree
94 {
95 public:
97  virtual ~IPhyloTree() {}
98 
99 public:
107  virtual std::unique_ptr<PhyloTree> readPhyloTree(const std::string& path) const = 0;
108 
116  virtual std::unique_ptr<PhyloTree> readPhyloTree(std::istream& in) const = 0;
117 };
118 
119 
123 class OTree :
124  public virtual IOTree
125 {
126 public:
127  OTree() {}
128  virtual ~OTree() {}
129 
130 public:
140  virtual void writeTree(const Tree& tree, const std::string& path, bool overwrite) const = 0;
141 
149  virtual void writeTree(const Tree& tree, std::ostream& out) const = 0;
150 };
151 
155 class OPhyloTree :
156  public virtual IOTree
157 {
158 public:
160  virtual ~OPhyloTree() {}
161 
162 public:
172  virtual void writePhyloTree(const PhyloTree& tree, const std::string& path, bool overwrite) const = 0;
173 
181  virtual void writePhyloTree(const PhyloTree& tree, std::ostream& out) const = 0;
182 };
183 
188  public virtual ITree
189 {
190 public:
192  virtual ~AbstractITree() {}
193 
194 public:
195  std::unique_ptr<Tree> readTree(std::istream& in) const override
196  {
197  auto tree = readTreeTemplate(in);
198  return std::unique_ptr<Tree>(tree.release());
199  }
200 
201  std::unique_ptr<Tree> readTree(const std::string& path) const override
202  {
203  std::ifstream input(path.c_str(), std::ios::in);
204  auto tree = readTree(input);
205  input.close();
206  return tree;
207  }
208 
209  virtual std::unique_ptr<TreeTemplate<Node>> readTreeTemplate(std::istream& in) const = 0;
210 
211  virtual std::unique_ptr<TreeTemplate<Node>> readTreeTemplate(const std::string& path) const
212  {
213  std::ifstream input(path.c_str(), std::ios::in);
214  auto tree = readTreeTemplate(input);
215  input.close();
216  return tree;
217  }
218 
219  virtual Element getElement(const std::string& elt) const
220  {
221  return Element();
222  }
223 };
224 
226  public virtual IPhyloTree
227 {
228 public:
230  virtual ~AbstractIPhyloTree() {}
231 
232 public:
233  std::unique_ptr<PhyloTree> readPhyloTree(std::istream& in) const override = 0;
234 
235  std::unique_ptr<PhyloTree> readPhyloTree(const std::string& path) const override
236  {
237  std::ifstream input(path.c_str(), std::ios::in);
238  if (!input)
239  throw IOException ("AbstractIPhyloTree::readPhyloTree(path): failed to read from path " + path);
240  auto tree = readPhyloTree(input);
241  input.close();
242  return tree;
243  }
244 
245  Element getElement(const std::string& elt) const
246  {
247  return Element();
248  }
249 };
250 
255  public virtual OTree
256 {
257 public:
259  virtual ~AbstractOTree() {}
260 
261 public:
262  virtual void writeTree(const Tree& tree, std::ostream& out) const = 0;
263  virtual void writeTree(const Tree& tree, const std::string& path, bool overwrite) const
264  {
265  try
266  {
267  // Open file in specified mode
268 
269  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
270  if (!output)
271  throw Exception("Problem opening file " + path + "in write Tree.");
272  writeTree(tree, output);
273  output.close();
274  }
275  catch (IOException& e)
276  {
277  std::stringstream ss;
278  ss << e.what() << "\nProblem writing tree to file " << path << "\n Is the file path correct and do \
279 you have the proper authorizations? ";
280  throw (IOException ( ss.str() ) );
281  }
282  }
283 };
284 
289  public virtual OPhyloTree
290 {
291 public:
293  virtual ~AbstractOPhyloTree() {}
294 
295 public:
296  virtual void writePhyloTree(const PhyloTree& tree, std::ostream& out) const = 0;
297  virtual void writePhyloTree(const PhyloTree& tree, const std::string& path, bool overwrite) const
298  {
299  try
300  {
301  // Open file in specified mode
302 
303  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
304  if (!output)
305  throw Exception("Problem opening file " + path + "in writePhyloTree.");
306  writePhyloTree(tree, output);
307  output.close();
308  }
309  catch (IOException& e)
310  {
311  std::stringstream ss;
312  ss << e.what() << "\nProblem writing tree to file " << path << "\n Is the file path correct and do \
313 you have the proper authorizations? ";
314  throw (IOException ( ss.str() ) );
315  }
316  }
317 };
318 
319 
323 class IMultiTree :
324  public virtual IOTree
325 {
326 public:
328  virtual ~IMultiTree() {}
329 
330 public:
338  virtual void readTrees(const std::string& path, std::vector<std::unique_ptr<Tree>>& trees) const = 0;
339 
347  virtual void readTrees(std::istream& in, std::vector<std::unique_ptr<Tree>>& trees) const = 0;
348 };
349 
351  public virtual IOTree
352 {
353 public:
355  virtual ~IMultiPhyloTree() {}
356 
357 public:
365  virtual void readPhyloTrees(const std::string& path, std::vector<std::unique_ptr<PhyloTree>>& trees) const = 0;
366 
374  virtual void readPhyloTrees(std::istream& in, std::vector<std::unique_ptr<PhyloTree>>& trees) const = 0;
375 };
376 
380 class OMultiTree :
381  public virtual IOTree
382 {
383 public:
385  virtual ~OMultiTree() {}
386 
387 public:
397  virtual void writeTrees(
398  const std::vector<const Tree*>& trees,
399  const std::string& path,
400  bool overwrite) const = 0;
401 
409  virtual void writeTrees(
410  const std::vector<const Tree*>& trees,
411  std::ostream& out) const = 0;
412 };
413 
418  public virtual IOTree
419 {
420 public:
422  virtual ~OMultiPhyloTree() {}
423 
424 public:
434  virtual void writePhyloTrees(
435  const std::vector<const PhyloTree*>& trees,
436  const std::string& path,
437  bool overwrite) const = 0;
438 
446  virtual void writePhyloTrees(
447  const std::vector<const PhyloTree*>& trees,
448  std::ostream& out) const = 0;
449 };
450 
455  public virtual IMultiTree
456 {
457 public:
459  virtual ~AbstractIMultiTree() {}
460 
461 public:
462  virtual void readTrees(std::istream& in, std::vector<std::unique_ptr<Tree>>& trees) const override = 0;
463 
464  virtual void readTrees(const std::string& path, std::vector<std::unique_ptr<Tree>>& trees) const override
465  {
466  std::ifstream input(path.c_str(), std::ios::in);
467  readTrees(input, trees);
468  input.close();
469  }
470 };
471 
476  public virtual IMultiPhyloTree
477 {
478 public:
481 
482 public:
483  virtual void readPhyloTrees(std::istream& in, std::vector<std::unique_ptr<PhyloTree>>& trees) const override = 0;
484 
485  virtual void readPhyloTrees(const std::string& path, std::vector<std::unique_ptr<PhyloTree>>& trees) const override
486  {
487  std::ifstream input(path.c_str(), std::ios::in);
488  readPhyloTrees(input, trees);
489  input.close();
490  }
491 };
492 
497  public virtual OMultiTree
498 {
499 public:
501  virtual ~AbstractOMultiTree() {}
502 
503 public:
504  virtual void writeTrees(const std::vector<const Tree*>& trees, std::ostream& out) const = 0;
505  virtual void writeTrees(const std::vector<const Tree*>& trees, const std::string& path, bool overwrite) const
506 
507  {
508  // Open file in specified mode
509  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
510  writeTrees(trees, output);
511  output.close();
512  }
513 };
514 
519  public virtual OMultiPhyloTree
520 {
521 public:
524 
525 public:
526  virtual void writePhyloTrees(const std::vector<const PhyloTree*>& trees, std::ostream& out) const = 0;
527 
528  virtual void writePhyloTrees(const std::vector<const PhyloTree*>& trees, const std::string& path, bool overwrite) const
529 
530  {
531  // Open file in specified mode
532  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
533  writePhyloTrees(trees, output);
534  output.close();
535  }
536 };
537 } // end of namespace bpp.
538 #endif // BPP_PHYL_IO_IOTREE_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.
virtual ~AbstractIMultiPhyloTree()
Definition: IoTree.h:480
virtual void readPhyloTrees(const std::string &path, std::vector< std::unique_ptr< PhyloTree >> &trees) const override
Read trees from a file.
Definition: IoTree.h:485
Partial implementation of the IMultiTree interface.
Definition: IoTree.h:456
virtual void readTrees(const std::string &path, std::vector< std::unique_ptr< Tree >> &trees) const override
Read trees from a file.
Definition: IoTree.h:464
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
Read trees from a stream.
virtual ~AbstractIMultiTree()
Definition: IoTree.h:459
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
Read a tree from a stream.
std::unique_ptr< PhyloTree > readPhyloTree(const std::string &path) const override
Read a tree from a file.
Definition: IoTree.h:235
virtual ~AbstractIPhyloTree()
Definition: IoTree.h:230
Element getElement(const std::string &elt) const
Definition: IoTree.h:245
Partial implementation of the ITree interface.
Definition: IoTree.h:189
std::unique_ptr< Tree > readTree(const std::string &path) const override
Read a tree from a file.
Definition: IoTree.h:201
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(const std::string &path) const
Definition: IoTree.h:211
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const =0
virtual Element getElement(const std::string &elt) const
Definition: IoTree.h:219
std::unique_ptr< Tree > readTree(std::istream &in) const override
Read a tree from a stream.
Definition: IoTree.h:195
virtual ~AbstractITree()
Definition: IoTree.h:192
Partial implementation of the OTree interface.
Definition: IoTree.h:520
virtual ~AbstractOMultiPhyloTree()
Definition: IoTree.h:523
virtual void writePhyloTrees(const std::vector< const PhyloTree * > &trees, std::ostream &out) const =0
Write trees to a stream.
virtual void writePhyloTrees(const std::vector< const PhyloTree * > &trees, const std::string &path, bool overwrite) const
Write trees to a file.
Definition: IoTree.h:528
Partial implementation of the OTree interface.
Definition: IoTree.h:498
virtual ~AbstractOMultiTree()
Definition: IoTree.h:501
virtual void writeTrees(const std::vector< const Tree * > &trees, std::ostream &out) const =0
Write trees to a stream.
virtual void writeTrees(const std::vector< const Tree * > &trees, const std::string &path, bool overwrite) const
Write trees to a file.
Definition: IoTree.h:505
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.
virtual ~AbstractOPhyloTree()
Definition: IoTree.h:293
virtual void writePhyloTree(const PhyloTree &tree, const std::string &path, bool overwrite) const
Write a tree to a file.
Definition: IoTree.h:297
Partial implementation of the OTree interface.
Definition: IoTree.h:256
virtual ~AbstractOTree()
Definition: IoTree.h:259
virtual void writeTree(const Tree &tree, std::ostream &out) const =0
Write a tree to a stream.
virtual void writeTree(const Tree &tree, const std::string &path, bool overwrite) const
Write a tree to a file.
Definition: IoTree.h:263
const char * what() const noexcept override
virtual void readPhyloTrees(const std::string &path, std::vector< std::unique_ptr< PhyloTree >> &trees) const =0
Read trees from a file.
virtual ~IMultiPhyloTree()
Definition: IoTree.h:355
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const =0
Read trees from a stream.
General interface for multiple trees readers.
Definition: IoTree.h:325
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const =0
Read trees from a stream.
virtual void readTrees(const std::string &path, std::vector< std::unique_ptr< Tree >> &trees) const =0
Read trees from a file.
virtual ~IMultiTree()
Definition: IoTree.h:328
General interface for tree I/O.
Definition: IoTree.h:29
IOTree()
Definition: IoTree.h:47
virtual ~IOTree()
Definition: IoTree.h:48
virtual const std::string getDataType() const
Definition: IoTree.h:51
General interface for tree readers.
Definition: IoTree.h:94
virtual std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const =0
Read a tree from a stream.
virtual ~IPhyloTree()
Definition: IoTree.h:97
virtual std::unique_ptr< PhyloTree > readPhyloTree(const std::string &path) const =0
Read a tree from a file.
General interface for tree readers.
Definition: IoTree.h:59
virtual ~ITree()
Definition: IoTree.h:67
virtual std::unique_ptr< Tree > readTree(std::istream &in) const =0
Read a tree from a stream.
ITree()
Definition: IoTree.h:66
virtual std::unique_ptr< Tree > readTree(const std::string &path) const =0
Read a tree from a file.
General interface for tree writers.
Definition: IoTree.h:419
virtual ~OMultiPhyloTree()
Definition: IoTree.h:422
virtual void writePhyloTrees(const std::vector< const PhyloTree * > &trees, const std::string &path, bool overwrite) const =0
Write trees to a file.
virtual void writePhyloTrees(const std::vector< const PhyloTree * > &trees, std::ostream &out) const =0
Write trees to a stream.
General interface for tree writers.
Definition: IoTree.h:382
virtual void writeTrees(const std::vector< const Tree * > &trees, const std::string &path, bool overwrite) const =0
Write trees to a file.
virtual ~OMultiTree()
Definition: IoTree.h:385
virtual void writeTrees(const std::vector< const Tree * > &trees, std::ostream &out) const =0
Write trees to a stream.
General interface for tree writers.
Definition: IoTree.h:157
virtual ~OPhyloTree()
Definition: IoTree.h:160
virtual void writePhyloTree(const PhyloTree &tree, const std::string &path, bool overwrite) const =0
Write a tree to a file.
virtual void writePhyloTree(const PhyloTree &tree, std::ostream &out) const =0
Write a tree to a stream.
General interface for tree writers.
Definition: IoTree.h:125
virtual void writeTree(const Tree &tree, std::ostream &out) const =0
Write a tree to a stream.
virtual ~OTree()
Definition: IoTree.h:128
virtual void writeTree(const Tree &tree, const std::string &path, bool overwrite) const =0
Write a tree to a file.
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
std::string annotation
Definition: IoTree.h:36
std::string length
Definition: IoTree.h:35
std::string content
Definition: IoTree.h:34