bpp-phyl  2.2.0
IoTree.h
Go to the documentation of this file.
1 //
2 // File: IOTree.h
3 // Created by: Julien Dutheil
4 // Created on: Thu Oct 23 15:19:06 2003
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (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 _IOTREE_H_
41 #define _IOTREE_H_
42 
43 #include "../Tree.h"
44 
45 // From the STL:
46 #include <string>
47 #include <iostream>
48 #include <fstream>
49 #include <sstream>
50 
51 #include <Bpp/Exceptions.h>
52 #include <Bpp/Io/IoFormat.h>
53 
54 namespace bpp
55 {
56 
60 class IOTree:
61  public virtual IOFormat
62 {
63  public:
64  IOTree() {}
65  virtual ~IOTree() {}
66 
67  public:
68  virtual const std::string getDataType() const { return "Tree"; }
69 };
70 
74 class ITree:
75  public virtual IOTree
76 {
77  public:
78  ITree() {}
79  virtual ~ITree() {}
80 
81  public:
89  virtual Tree* read(const std::string& path) const throw (Exception) = 0;
97  virtual Tree* read(std::istream& in) const throw (Exception) = 0;
98 };
99 
103 class OTree:
104  public virtual IOTree
105 {
106  public:
107  OTree() {}
108  virtual ~OTree() {}
109 
110  public:
120  virtual void write(const Tree& tree, const std::string& path, bool overwrite) const throw (Exception) = 0;
128  virtual void write(const Tree& tree, std::ostream& out) const throw (Exception) = 0;
129 };
130 
135  public virtual ITree
136 {
137  public:
139  virtual ~AbstractITree() {}
140 
141  public:
142  virtual Tree* read(std::istream& in) const throw (Exception) = 0;
143  virtual Tree* read(const std::string& path) const throw (Exception)
144  {
145  std::ifstream input(path.c_str(), std::ios::in);
146  Tree* tree = read(input);
147  input.close();
148  return tree;
149  }
150 
151 };
152 
157  public virtual OTree
158 {
159  public:
161  virtual ~AbstractOTree() {}
162 
163  public:
164  void write(const Tree& tree, std::ostream& out) const throw (Exception) = 0;
165  virtual void write(const Tree& tree, const std::string& path, bool overwrite) const throw (Exception)
166  {
167  try {
168  // Open file in specified mode
169  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out|std::ios::app));
170  write(tree, output);
171  output.close();
172  }
173  catch (IOException e)
174  {
175  std::stringstream ss ;
176  ss << e.what() <<"\nProblem writing tree to file "<< path <<"\n Is the file path correct and do \
177 you have the proper authorizations? ";
178  throw (IOException ( ss.str() ) );
179  }
180 
181  }
182 };
183 
184 
185 
186 
187 
188 
189 
194  public virtual IOTree
195 {
196  public:
198  virtual ~IMultiTree() {}
199 
200  public:
208  virtual void read(const std::string& path, std::vector<Tree*>& trees) const throw (Exception) = 0;
216  virtual void read(std::istream& in, std::vector<Tree*>& trees) const throw (Exception) = 0;
217 };
218 
223  public virtual IOTree
224 {
225  public:
227  virtual ~OMultiTree() {}
228 
229  public:
239  virtual void write(const std::vector<Tree*>& trees, const std::string& path, bool overwrite) const throw (Exception) = 0;
247  virtual void write(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception) = 0;
248 };
249 
254  public virtual IMultiTree
255 {
256  public:
258  virtual ~AbstractIMultiTree() {}
259 
260  public:
261  virtual void read(std::istream& in, std::vector<Tree*>& trees) const throw (Exception) = 0;
262  virtual void read(const std::string& path, std::vector<Tree*>& trees) const throw (Exception)
263  {
264  std::ifstream input(path.c_str(), std::ios::in);
265  read(input, trees);
266  input.close();
267  }
268 
269 };
270 
275  public virtual OMultiTree
276 {
277  public:
279  virtual ~AbstractOMultiTree() {}
280 
281  public:
282  void write(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception) = 0;
283  virtual void write(const std::vector<Tree*>& trees, const std::string& path, bool overwrite) const throw (Exception)
284  {
285  // Open file in specified mode
286  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out|std::ios::app));
287  write(trees, output);
288  output.close();
289  }
290 };
291 
292 } //end of namespace bpp.
293 
294 #endif //_IOTREE_H_
295 
void write(const std::vector< Tree *> &trees, std::ostream &out) const =0
Write trees to a stream.
ITree()
Definition: IoTree.h:78
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
virtual void write(const std::vector< Tree *> &trees, const std::string &path, bool overwrite) const
Write trees to a file.
Definition: IoTree.h:283
virtual ~OMultiTree()
Definition: IoTree.h:227
virtual ~IOTree()
Definition: IoTree.h:65
virtual void read(const std::string &path, std::vector< Tree *> &trees) const
Read trees from a file.
Definition: IoTree.h:262
General interface for tree writers.
Definition: IoTree.h:103
General interface for tree readers.
Definition: IoTree.h:74
virtual ~IMultiTree()
Definition: IoTree.h:198
Interface for phylogenetic tree objects.
Definition: Tree.h:148
virtual void write(const Tree &tree, const std::string &path, bool overwrite) const
Write a tree to a file.
Definition: IoTree.h:165
IOTree()
Definition: IoTree.h:64
virtual ~AbstractIMultiTree()
Definition: IoTree.h:258
virtual Tree * read(const std::string &path) const =0
Read a tree from a file.
virtual void read(const std::string &path, std::vector< Tree *> &trees) const =0
Read trees from a file.
General interface for tree I/O.
Definition: IoTree.h:60
virtual ~ITree()
Definition: IoTree.h:79
General interface for tree writers.
Definition: IoTree.h:222
virtual void write(const Tree &tree, const std::string &path, bool overwrite) const =0
Write a tree to a file.
General interface for multiple trees readers.
Definition: IoTree.h:193
virtual void write(const std::vector< Tree *> &trees, const std::string &path, bool overwrite) const =0
Write trees to a file.
virtual Tree * read(const std::string &path) const
Read a tree from a file.
Definition: IoTree.h:143
virtual ~OTree()
Definition: IoTree.h:108
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.
virtual ~AbstractITree()
Definition: IoTree.h:139
virtual const std::string getDataType() const
Definition: IoTree.h:68
virtual ~AbstractOTree()
Definition: IoTree.h:161
Partial implementation of the OTree interface.
Definition: IoTree.h:156
virtual ~AbstractOMultiTree()
Definition: IoTree.h:279
Partial implementation of the OTree interface.
Definition: IoTree.h:274
Partial implementation of the IMultiTree interface.
Definition: IoTree.h:253