~ubuntu-branches/ubuntu/raring/libbpp-phyl/raring

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Io/IoTree.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
50
#include <Bpp/Exceptions.h>
 
51
#include <Bpp/Io/IoFormat.h>
 
52
 
 
53
namespace bpp
 
54
{
 
55
 
 
56
/**
 
57
 * @brief General interface for tree I/O.
 
58
 */
 
59
class IOTree:
 
60
  public virtual IOFormat
 
61
{
 
62
        public:
 
63
                IOTree() {}
 
64
                virtual ~IOTree() {}
 
65
 
 
66
        public:
 
67
                virtual const std::string getDataType() const { return "Tree"; }
 
68
};
 
69
 
 
70
/**
 
71
 * @brief General interface for tree readers.
 
72
 */
 
73
class ITree:
 
74
  public virtual IOTree
 
75
{
 
76
        public:
 
77
                ITree() {}
 
78
                virtual ~ITree() {}
 
79
 
 
80
        public:
 
81
    /**
 
82
     * @brief Read a tree from a file.
 
83
     *
 
84
     * @param path The file path.
 
85
     * @return A new tree object.
 
86
     * @throw Exception If an error occured.
 
87
     */
 
88
                virtual Tree* read(const std::string& path) const throw (Exception) = 0;
 
89
    /**
 
90
     * @brief Read a tree from a stream.
 
91
     *
 
92
     * @param in The input stream.
 
93
     * @return A new tree object.
 
94
     * @throw Exception If an error occured.
 
95
     */
 
96
                virtual Tree* read(std::istream& in) const throw (Exception) = 0;
 
97
};
 
98
 
 
99
/**
 
100
 * @brief General interface for tree writers.
 
101
 */
 
102
class OTree:
 
103
  public virtual IOTree
 
104
{
 
105
        public:
 
106
                OTree() {}
 
107
                virtual ~OTree() {}
 
108
 
 
109
        public:
 
110
    /**
 
111
     * @brief Write a tree to a file.
 
112
     *
 
113
     * @param tree A tree object.
 
114
     * @param path The file path.
 
115
     * @param overwrite Tell if existing file must be overwritten.
 
116
     * Otherwise append to the file.
 
117
     * @throw Exception If an error occured.
 
118
     */
 
119
                virtual void write(const Tree& tree, const std::string& path, bool overwrite) const throw (Exception) = 0;
 
120
    /**
 
121
     * @brief Write a tree to a stream.
 
122
     *
 
123
     * @param tree A tree object.
 
124
     * @param out The output stream.
 
125
     * @throw Exception If an error occured.
 
126
     */
 
127
                virtual void write(const Tree& tree, std::ostream& out) const throw (Exception) = 0;
 
128
};
 
129
 
 
130
/**
 
131
 * @brief Partial implementation of the ITree interface.
 
132
 */
 
133
class AbstractITree:
 
134
  public virtual ITree
 
135
{
 
136
        public:
 
137
                AbstractITree() {}
 
138
                virtual ~AbstractITree() {}
 
139
 
 
140
        public:
 
141
                virtual Tree* read(std::istream& in) const throw (Exception) = 0;
 
142
                virtual Tree* read(const std::string& path) const throw (Exception)
 
143
                {
 
144
      std::ifstream input(path.c_str(), std::ios::in);
 
145
                        Tree* tree = read(input);
 
146
                        input.close();
 
147
                        return tree;
 
148
                }
 
149
 
 
150
};
 
151
 
 
152
/**
 
153
 * @brief Partial implementation of the OTree interface.
 
154
 */
 
155
class AbstractOTree:
 
156
  public virtual OTree
 
157
{
 
158
        public:
 
159
                AbstractOTree() {}
 
160
                virtual ~AbstractOTree() {}
 
161
 
 
162
        public:
 
163
                void write(const Tree& tree, std::ostream& out) const throw (Exception) = 0;
 
164
                virtual void write(const Tree& tree, const std::string& path, bool overwrite) const throw (Exception)
 
165
                {
 
166
                        // Open file in specified mode
 
167
      std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out|std::ios::app));
 
168
                        write(tree, output);
 
169
                        output.close();
 
170
                }
 
171
};
 
172
 
 
173
 
 
174
 
 
175
 
 
176
 
 
177
 
 
178
 
 
179
/**
 
180
 * @brief General interface for multiple trees readers.
 
181
 */
 
182
class IMultiTree:
 
183
  public virtual IOTree
 
184
{
 
185
        public:
 
186
                IMultiTree() {}
 
187
                virtual ~IMultiTree() {}
 
188
 
 
189
        public:
 
190
    /**
 
191
     * @brief Read trees from a file.
 
192
     *
 
193
     * @param path The file path.
 
194
     * @param trees The output trees container.
 
195
     * @throw Exception If an error occured.
 
196
     */
 
197
                virtual void read(const std::string& path, std::vector<Tree*>& trees) const throw (Exception) = 0;
 
198
    /**
 
199
     * @brief Read trees from a stream.
 
200
     *
 
201
     * @param in The input stream.
 
202
     * @param trees The output trees container.
 
203
     * @throw Exception If an error occured.
 
204
     */
 
205
                virtual void read(std::istream& in, std::vector<Tree*>& trees) const throw (Exception) = 0;
 
206
};
 
207
 
 
208
/**
 
209
 * @brief General interface for tree writers.
 
210
 */
 
211
class OMultiTree:
 
212
  public virtual IOTree
 
213
{
 
214
        public:
 
215
                OMultiTree() {}
 
216
                virtual ~OMultiTree() {}
 
217
 
 
218
        public:
 
219
    /**
 
220
     * @brief Write trees to a file.
 
221
     *
 
222
     * @param trees A vector of tree objects.
 
223
     * @param path The file path.
 
224
     * @param overwrite Tell if existing file must be overwritten.
 
225
     * Otherwise append to the file.
 
226
     * @throw Exception If an error occured.
 
227
     */
 
228
                virtual void write(const std::vector<Tree*>& trees, const std::string& path, bool overwrite) const throw (Exception) = 0;
 
229
    /**
 
230
     * @brief Write trees to a stream.
 
231
     *
 
232
     * @param trees A vector of tree objects.
 
233
     * @param out The output stream.
 
234
     * @throw Exception If an error occured.
 
235
     */
 
236
                virtual void write(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception) = 0;
 
237
};
 
238
 
 
239
/**
 
240
 * @brief Partial implementation of the IMultiTree interface.
 
241
 */
 
242
class AbstractIMultiTree:
 
243
  public virtual IMultiTree
 
244
{
 
245
        public:
 
246
                AbstractIMultiTree() {}
 
247
                virtual ~AbstractIMultiTree() {}
 
248
 
 
249
        public:
 
250
                virtual void read(std::istream& in, std::vector<Tree*>& trees) const throw (Exception) = 0;
 
251
                virtual void read(const std::string& path, std::vector<Tree*>& trees) const throw (Exception)
 
252
                {
 
253
      std::ifstream input(path.c_str(), std::ios::in);
 
254
                        read(input, trees);
 
255
                        input.close();
 
256
                }
 
257
 
 
258
};
 
259
 
 
260
/**
 
261
 * @brief Partial implementation of the OTree interface.
 
262
 */
 
263
class AbstractOMultiTree:
 
264
  public virtual OMultiTree
 
265
{
 
266
        public:
 
267
                AbstractOMultiTree() {}
 
268
                virtual ~AbstractOMultiTree() {}
 
269
 
 
270
        public:
 
271
                void write(const std::vector<Tree*>& trees, std::ostream& out) const throw (Exception) = 0;
 
272
                virtual void write(const std::vector<Tree*>& trees, const std::string& path, bool overwrite) const throw (Exception)
 
273
                {
 
274
                        // Open file in specified mode
 
275
      std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out|std::ios::app));
 
276
                        write(trees, output);
 
277
                        output.close();
 
278
                }
 
279
};
 
280
 
 
281
} //end of namespace bpp.
 
282
 
 
283
#endif  //_IOTREE_H_
 
284