bpp-phyl3  3.0.0
IoDAG.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_IODAGRAPH_H
6 #define BPP_PHYL_IO_IODAGRAPH_H
7 
8 
9 // From the STL:
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include <sstream>
14 #include <memory>
15 
16 #include <Bpp/Exceptions.h>
17 #include <Bpp/Io/IoFormat.h>
18 
19 namespace bpp
20 {
24 class IODAG :
25  public virtual IOFormat
26 {
27 protected:
28  struct Element
29  {
30 public:
31  std::string content;
32  std::string length;
33  std::string annotation;
34  bool isLeaf;
35 
36 public:
38  length(),
39  annotation(),
40  isLeaf(false) {}
41  };
42 
43 public:
44  IODAG() {}
45  virtual ~IODAG() {}
46 
47 public:
48  virtual const std::string getDataType() const { return "DAG"; }
49 };
50 
54 class IPhyloDAG :
55  public virtual IODAG
56 {
57 public:
58  IPhyloDAG() {}
59  virtual ~IPhyloDAG() {}
60 
61 public:
69  virtual std::unique_ptr<PhyloDAG> readPhyloDAG(const std::string& path) const = 0;
70 
78  virtual std::unique_ptr<PhyloDAG> readPhyloDAG(std::istream& in) const = 0;
79 };
80 
81 
82 
87 class OPhyloDAG :
88  public virtual IODAG
89 {
90 public:
91  OPhyloDAG() {}
92  virtual ~OPhyloDAG() {}
93 
94 public:
104  virtual void writePhyloDAG(const PhyloDAG& dag, const std::string& path, bool overwrite) const = 0;
105 
113  virtual void writePhyloDAG(const PhyloDAG& dag, std::ostream& out) const = 0;
114 };
115 
121  public virtual IPhyloDAG
122 {
123 public:
125  virtual ~AbstractIPhyloDAG() {}
126 
127 public:
128  std::unique_ptr<PhyloDAG> readPhyloDAG(std::istream& in) const override = 0;
129 
130  std::unique_ptr<PhyloDAG> readPhyloDAG(const std::string& path) const override
131  {
132  std::ifstream input(path.c_str(), std::ios::in);
133  if (!input)
134  throw IOException ("AbstractIPhyloDAG::readPhyloDAG(path): failed to read from path " + path);
135  auto dag = readPhyloDAG(input);
136  input.close();
137  return dag;
138  }
139 
140  virtual Element getElement(const std::string& elt) const
141  {
142  return Element();
143  }
144 };
145 
150  public virtual OPhyloDAG
151 {
152 public:
154  virtual ~AbstractOPhyloDAG() {}
155 
156 public:
157  virtual void writePhyloDAG(const PhyloDAG& dag, std::ostream& out) const = 0;
158  virtual void writePhyloDAG(const PhyloDAG& dag, const std::string& path, bool overwrite) const
159  {
160  try
161  {
162  // Open file in specified mode
163 
164  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
165  if (!output)
166  throw Exception("Problem opening file " + path + "in writePhyloDAG.");
167  writePhyloDAG(dag, output);
168  output.close();
169  }
170  catch (IOException& e)
171  {
172  std::stringstream ss;
173  ss << e.what() << "\nProblem writing dag to file " << path << "\n Is the file path correct and do \
174 you have the proper authorizations? ";
175  throw (IOException ( ss.str() ) );
176  }
177  }
178 };
179 
180 
185  public virtual IODAG
186 {
187 public:
189  virtual ~IMultiPhyloDAG() {}
190 
191 public:
199  virtual void readPhyloDAGs(const std::string& path, std::vector<std::unique_ptr<PhyloDAG>>& dags) const = 0;
200 
208  virtual void readPhyloDAGs(std::istream& in, std::vector<std::unique_ptr<PhyloDAG>>& dags) const = 0;
209 };
210 
215  public virtual IODAG
216 {
217 public:
219  virtual ~OMultiPhyloDAG() {}
220 
221 public:
230  virtual void writePhyloDAGs(
231  const std::vector<const PhyloDAG*>& dags,
232  const std::string& path,
233  bool overwrite) const = 0;
234 
242  virtual void writePhyloDAGs(
243  const std::vector<const PhyloDAG*>& dags,
244  std::ostream& out) const = 0;
245 };
246 
251  public virtual IMultiPhyloDAG
252 {
253 public:
256 
257 public:
258  virtual void readPhyloDAGs(std::istream& in, std::vector<std::unique_ptr<PhyloDAG>>& dags) const override = 0;
259 
260  virtual void readPhyloDAGs(const std::string& path, std::vector<std::unique_ptr<PhyloDAG>>& dags) const override
261  {
262  std::ifstream input(path.c_str(), std::ios::in);
263  readPhyloDAGs(input, dags);
264  input.close();
265  }
266 };
267 
272  public virtual OMultiPhyloDAG
273 {
274 public:
277 
278 public:
279  virtual void writePhyloDAGs(const std::vector<const PhyloDAG*>& dags, std::ostream& out) const = 0;
280 
281  virtual void writePhyloDAGs(const std::vector<const PhyloDAG*>& dags, const std::string& path, bool overwrite) const
282 
283  {
284  // Open file in specified mode
285  std::ofstream output(path.c_str(), overwrite ? (std::ios::out) : (std::ios::out | std::ios::app));
286  writePhyloDAGs(dags, output);
287  output.close();
288  }
289 };
290 } // end of namespace bpp.
291 #endif // BPP_PHYL_IO_IODAGRAPH_H
Partial implementation of the IMultiDAG interface.
Definition: IoDAG.h:252
virtual void readPhyloDAGs(std::istream &in, std::vector< std::unique_ptr< PhyloDAG >> &dags) const override=0
Read dags from a stream.
virtual ~AbstractIMultiPhyloDAG()
Definition: IoDAG.h:255
virtual void readPhyloDAGs(const std::string &path, std::vector< std::unique_ptr< PhyloDAG >> &dags) const override
Read dags from a file.
Definition: IoDAG.h:260
Partial implementation of the IDAG interface.
Definition: IoDAG.h:122
std::unique_ptr< PhyloDAG > readPhyloDAG(std::istream &in) const override=0
Read a DAG from a stream.
virtual Element getElement(const std::string &elt) const
Definition: IoDAG.h:140
virtual ~AbstractIPhyloDAG()
Definition: IoDAG.h:125
std::unique_ptr< PhyloDAG > readPhyloDAG(const std::string &path) const override
Read a DAG from a file.
Definition: IoDAG.h:130
Partial implementation of the ODAG interface.
Definition: IoDAG.h:273
virtual void writePhyloDAGs(const std::vector< const PhyloDAG * > &dags, std::ostream &out) const =0
Write dags to a stream.
virtual ~AbstractOMultiPhyloDAG()
Definition: IoDAG.h:276
virtual void writePhyloDAGs(const std::vector< const PhyloDAG * > &dags, const std::string &path, bool overwrite) const
Write dags to a file.
Definition: IoDAG.h:281
Partial implementation of the ODAG interface.
Definition: IoDAG.h:151
virtual ~AbstractOPhyloDAG()
Definition: IoDAG.h:154
virtual void writePhyloDAG(const PhyloDAG &dag, const std::string &path, bool overwrite) const
Write a DAG to a file.
Definition: IoDAG.h:158
virtual void writePhyloDAG(const PhyloDAG &dag, std::ostream &out) const =0
Write a PhyloDAG to a stream.
const char * what() const noexcept override
General interface for multiple trees readers.
Definition: IoDAG.h:186
virtual void readPhyloDAGs(const std::string &path, std::vector< std::unique_ptr< PhyloDAG >> &dags) const =0
Read dags from a file.
virtual void readPhyloDAGs(std::istream &in, std::vector< std::unique_ptr< PhyloDAG >> &dags) const =0
Read dags from a stream.
virtual ~IMultiPhyloDAG()
Definition: IoDAG.h:189
General interface for DAG I/O.
Definition: IoDAG.h:26
virtual ~IODAG()
Definition: IoDAG.h:45
IODAG()
Definition: IoDAG.h:44
virtual const std::string getDataType() const
Definition: IoDAG.h:48
General interface for DAG readers.
Definition: IoDAG.h:56
virtual ~IPhyloDAG()
Definition: IoDAG.h:59
virtual std::unique_ptr< PhyloDAG > readPhyloDAG(std::istream &in) const =0
Read a DAG from a stream.
virtual std::unique_ptr< PhyloDAG > readPhyloDAG(const std::string &path) const =0
Read a DAG from a file.
General interface for tree writers.
Definition: IoDAG.h:216
virtual void writePhyloDAGs(const std::vector< const PhyloDAG * > &dags, const std::string &path, bool overwrite) const =0
Write dags to a file.
virtual void writePhyloDAGs(const std::vector< const PhyloDAG * > &dags, std::ostream &out) const =0
Write dags to a stream.
virtual ~OMultiPhyloDAG()
Definition: IoDAG.h:219
General interface for DAG writers.
Definition: IoDAG.h:89
virtual void writePhyloDAG(const PhyloDAG &dag, std::ostream &out) const =0
Write a PhyloDAG to a stream.
virtual ~OPhyloDAG()
Definition: IoDAG.h:92
virtual void writePhyloDAG(const PhyloDAG &dag, const std::string &path, bool overwrite) const =0
Write a DAG to a file.
Defines the basic types of data flow nodes.
std::string length
Definition: IoDAG.h:32
std::string annotation
Definition: IoDAG.h:33
std::string content
Definition: IoDAG.h:31