bpp-seq3  3.0.0
AbstractIAlignment.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_SEQ_IO_ABSTRACTIALIGNMENT_H
6 #define BPP_SEQ_IO_ABSTRACTIALIGNMENT_H
7 
8 
9 #include "../Alphabet/Alphabet.h"
10 #include "../Container/AlignedSequenceContainer.h"
11 #include "../Container/VectorSiteContainer.h"
12 #include "AbstractISequence.h"
13 
14 // From the STL:
15 #include <string>
16 #include <iostream>
17 #include <fstream>
18 
19 namespace bpp
20 {
25  public virtual IAlignment
26 {
27 public:
29  virtual ~AbstractIAlignment() {}
30 
31 public:
45  virtual void readAlignment(std::istream& input, SequenceContainerInterface& sc) const override
46  {
47  appendAlignmentFromStream(input, sc);
48  }
49 
57  virtual void readAlignment(const std::string& path, SequenceContainerInterface& sc) const override
58  {
59  appendAlignmentFromFile(path, sc);
60  }
61 
62  std::unique_ptr<SiteContainerInterface> readAlignment(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
63  {
64  return readAlignmentFromFile(path, alpha);
65  }
66 
67  std::unique_ptr<SiteContainerInterface> readAlignment(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
68  {
69  return readAlignmentFromStream(input, alpha);
70  }
71 
74 protected:
84  virtual void appendAlignmentFromStream(std::istream& input, SequenceContainerInterface& sc) const = 0;
85 
93  virtual void appendAlignmentFromFile(const std::string& path, SequenceContainerInterface& sc) const
94  {
95  std::ifstream input(path.c_str(), std::ios::in);
96  if (!input)
97  throw IOException("AbstractIAlignment::appendAlignmentFromFile: can't read file " + path);
98  appendAlignmentFromStream(input, sc);
99  input.close();
100  }
101 
110  virtual std::unique_ptr<SiteContainerInterface> readAlignmentFromStream(std::istream& input, std::shared_ptr<const Alphabet> alpha) const
111  {
112  auto asc = std::unique_ptr<SiteContainerInterface>(new AlignedSequenceContainer(alpha));
113  appendAlignmentFromStream(input, *asc);
114  return asc;
115  }
116 
125  virtual std::unique_ptr<SiteContainerInterface> readAlignmentFromFile(const std::string& path, std::shared_ptr<const Alphabet> alpha) const
126  {
127  auto asc = std::unique_ptr<SiteContainerInterface>(new AlignedSequenceContainer(alpha));
128  appendAlignmentFromFile(path, *asc);
129  return asc;
130  }
131 };
132 
133 
140  public virtual AbstractIAlignment,
141  // In case of diamond inheritance
142  public virtual ISequence
143 {
144 public:
146  virtual ~AbstractIAlignment2() {}
147 
148 public:
157  void readSequences(std::istream& input, SequenceContainerInterface& sc) const override
158  {
159  appendAlignmentFromStream(input, sc);
160  }
161 
162  void readSequences(const std::string& path, SequenceContainerInterface& sc) const override
163  {
164  appendAlignmentFromFile(path, sc);
165  }
166 
167  std::unique_ptr<SequenceContainerInterface> readSequences(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
168  {
169  auto sic = readAlignment(input, alpha);
170  std::unique_ptr<SequenceContainerInterface> sec = std::move(sic);
171  return sec;
172  }
173 
174  std::unique_ptr<SequenceContainerInterface> readSequences(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
175  {
176  auto sic = readAlignment(path, alpha);
177  std::unique_ptr<SequenceContainerInterface> sec = std::move(sic);
178  return sec;
179  }
181 };
182 
183 
185  public virtual IProbabilisticAlignment
186 {
187 public:
190 
191 public:
204  void readAlignment(std::istream& input, ProbabilisticSequenceContainerInterface& sc) const override
205  {
206  appendAlignmentFromStream(input, sc);
207  }
208 
215  void readAlignment(const std::string& path, ProbabilisticSequenceContainerInterface& sc) const override
216  {
217  appendAlignmentFromFile(path, sc);
218  }
219 
220  std::unique_ptr<ProbabilisticSiteContainerInterface> readAlignment(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
221  {
222  return readAlignmentFromFile(path, alpha);
223  }
224 
225  std::unique_ptr<ProbabilisticSiteContainerInterface> readAlignment(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
226  {
227  return readAlignmentFromStream(input, alpha);
228  }
229 
232 protected:
241  virtual void appendAlignmentFromStream(std::istream& input, ProbabilisticSequenceContainerInterface& sc) const = 0;
242 
250  virtual void appendAlignmentFromFile(const std::string& path, ProbabilisticSequenceContainerInterface& sc) const
251  {
252  std::ifstream input(path.c_str(), std::ios::in);
253  if (!input)
254  throw IOException("AbstractIProbabilisticAlignment::appendAlignmentFromFile: can't read file " + path);
255  appendAlignmentFromStream(input, sc);
256  input.close();
257  }
258 
266  virtual std::unique_ptr<ProbabilisticSiteContainerInterface> readAlignmentFromStream(std::istream& input, std::shared_ptr<const Alphabet>& alpha) const
267  {
268  auto vpsc = std::unique_ptr<ProbabilisticVectorSiteContainer>(new ProbabilisticVectorSiteContainer(alpha));
269  appendAlignmentFromStream(input, *vpsc);
270  return vpsc;
271  }
272 
280  virtual std::unique_ptr<ProbabilisticSiteContainerInterface> readAlignmentFromFile(const std::string& path, std::shared_ptr<const Alphabet>& alpha) const
281  {
282  auto vpsc = std::unique_ptr<ProbabilisticVectorSiteContainer>(new ProbabilisticVectorSiteContainer(alpha));
283  appendAlignmentFromFile(path, *vpsc);
284  return vpsc;
285  }
286 };
287 
288 
290  public virtual AbstractIProbabilisticAlignment,
291  // In case of diamond inheritance
292  public virtual IProbabilisticSequence
293 {
294 public:
297 
298 public:
307  void readSequences(std::istream& input, ProbabilisticSequenceContainerInterface& sc) const override
308  {
309  appendAlignmentFromStream(input, sc);
310  }
311 
312  void readSequences(const std::string& path, ProbabilisticSequenceContainerInterface& sc) const override
313  {
314  appendAlignmentFromFile(path, sc);
315  }
316 
317  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequences(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
318  {
319  auto sic = readAlignment(input, alpha);
320  std::unique_ptr<ProbabilisticSequenceContainerInterface> sec = std::move(sic);
321  return sec;
322  }
323 
324  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequences(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
325  {
326  auto sic = readAlignment(path, alpha);
327  std::unique_ptr<ProbabilisticSequenceContainerInterface> sec = std::move(sic);
328  return sec;
329  }
331 };
332 } // end of namespace bpp.
333 #endif // BPP_SEQ_IO_ABSTRACTIALIGNMENT_H
Partial implementation of the IAlignment and ISequence interface, dedicated to alignment readers.
std::unique_ptr< SequenceContainerInterface > readSequences(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
void readSequences(const std::string &path, SequenceContainerInterface &sc) const override
Create a new container from a file.
std::unique_ptr< SequenceContainerInterface > readSequences(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
void readSequences(std::istream &input, SequenceContainerInterface &sc) const override
Create a new container from a stream.
Partial implementation of the IAlignment interface, dedicated to alignment readers.
virtual std::unique_ptr< SiteContainerInterface > readAlignmentFromFile(const std::string &path, std::shared_ptr< const Alphabet > alpha) const
Read sequences from a file.
virtual void appendAlignmentFromStream(std::istream &input, SequenceContainerInterface &sc) const =0
Append sequences to a container from a stream.
virtual void readAlignment(std::istream &input, SequenceContainerInterface &sc) const override
Add sequences to a container from a stream.
std::unique_ptr< SiteContainerInterface > readAlignment(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
virtual std::unique_ptr< SiteContainerInterface > readAlignmentFromStream(std::istream &input, std::shared_ptr< const Alphabet > alpha) const
Read sequences from a stream.
std::unique_ptr< SiteContainerInterface > readAlignment(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
virtual void appendAlignmentFromFile(const std::string &path, SequenceContainerInterface &sc) const
Append sequences to a container from a file.
virtual void readAlignment(const std::string &path, SequenceContainerInterface &sc) const override
Add sequences to a container from a file.
void readSequences(std::istream &input, ProbabilisticSequenceContainerInterface &sc) const override
Create a new container from a stream.
std::unique_ptr< ProbabilisticSequenceContainerInterface > readSequences(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
std::unique_ptr< ProbabilisticSequenceContainerInterface > readSequences(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
void readSequences(const std::string &path, ProbabilisticSequenceContainerInterface &sc) const override
Create a new container from a file.
void readAlignment(const std::string &path, ProbabilisticSequenceContainerInterface &sc) const override
Add sequences to a container from a file.
virtual void appendAlignmentFromFile(const std::string &path, ProbabilisticSequenceContainerInterface &sc) const
Append sequences to a container from a file.
virtual void appendAlignmentFromStream(std::istream &input, ProbabilisticSequenceContainerInterface &sc) const =0
Append sequences to a container from a stream.
void readAlignment(std::istream &input, ProbabilisticSequenceContainerInterface &sc) const override
Add sequences to a container from a stream.
std::unique_ptr< ProbabilisticSiteContainerInterface > readAlignment(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
virtual std::unique_ptr< ProbabilisticSiteContainerInterface > readAlignmentFromStream(std::istream &input, std::shared_ptr< const Alphabet > &alpha) const
Read sequences from a stream.
virtual std::unique_ptr< ProbabilisticSiteContainerInterface > readAlignmentFromFile(const std::string &path, std::shared_ptr< const Alphabet > &alpha) const
Read sequences from a file.
std::unique_ptr< ProbabilisticSiteContainerInterface > readAlignment(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
The IAlignment interface.
Definition: ISequence.h:87
The IProbabilisticSequence interface.
Definition: ISequence.h:208
The IProbabilisticSequence interface.
Definition: ISequence.h:150
The ISequence interface.
Definition: ISequence.h:29
The SequenceContainer interface.
This alphabet is used to deal NumericAlphabet.
TemplateAlignedSequenceContainer< Sequence, Site > AlignedSequenceContainer
TemplateVectorSiteContainer< ProbabilisticSite, ProbabilisticSequence > ProbabilisticVectorSiteContainer