bpp-seq3  3.0.0
AbstractISequence.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_ABSTRACTISEQUENCE_H
6 #define BPP_SEQ_IO_ABSTRACTISEQUENCE_H
7 
8 
9 #include "../Alphabet/Alphabet.h"
10 #include "../Container/VectorSequenceContainer.h"
11 #include "ISequence.h"
12 
13 // From the STL:
14 #include <string>
15 #include <iostream>
16 #include <fstream>
17 
18 namespace bpp
19 {
24  public virtual ISequence
25 {
26 public:
28  virtual ~AbstractISequence() {}
29 
30 public:
37 public:
45  void readSequences(std::istream& input, SequenceContainerInterface& sc) const override
46  {
47  appendSequencesFromStream(input, sc);
48  }
49 
57  void readSequences(const std::string& path, SequenceContainerInterface& sc) const override
58  {
59  appendSequencesFromFile(path, sc);
60  }
61 
62  std::unique_ptr<SequenceContainerInterface> readSequences(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
63  {
64  return readSequencesFromStream(input, alpha);
65  }
66 
67  std::unique_ptr<SequenceContainerInterface> readSequences(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
68  {
69  return readSequencesFromFile(path, alpha);
70  }
73 protected:
83  virtual void appendSequencesFromStream(std::istream& input, SequenceContainerInterface& sc) const = 0;
84 
92  virtual void appendSequencesFromFile(const std::string& path, SequenceContainerInterface& sc) const
93  {
94  std::ifstream input(path.c_str(), std::ios::in);
95  if (!input)
96  throw IOException("AbstractIAlignment::appendSequencesFromFile: can't read file " + path);
97  appendSequencesFromStream(input, sc);
98  input.close();
99  }
100 
109  std::unique_ptr<SequenceContainerInterface> readSequencesFromStream(std::istream& input, std::shared_ptr<const Alphabet>& alpha) const
110  {
111  auto vsc = std::unique_ptr<SequenceContainerInterface>(new VectorSequenceContainer(alpha));
112  appendSequencesFromStream(input, *vsc);
113  return vsc;
114  }
115 
123  std::unique_ptr<SequenceContainerInterface> readSequencesFromFile(const std::string& path, std::shared_ptr<const Alphabet>& alpha) const
124  {
125  auto vsc = std::unique_ptr<SequenceContainerInterface>(new VectorSequenceContainer(alpha));
126  appendSequencesFromFile(path, *vsc);
127  return vsc;
128  }
129 };
130 
131 
136  public virtual IProbabilisticSequence
137 {
138 public:
141 
142 public:
149 public:
157  void readSequences(std::istream& input, ProbabilisticSequenceContainerInterface& sc) const override
158  {
159  appendSequencesFromStream(input, sc);
160  }
161 
169  void readSequences(const std::string& path, ProbabilisticSequenceContainerInterface& sc) const override
170  {
171  appendSequencesFromFile(path, sc);
172  }
173 
174  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequences(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
175  {
176  return readSequencesFromStream(input, alpha);
177  }
178 
179  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequences(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
180  {
181  return readSequencesFromFile(path, alpha);
182  }
185 protected:
195  virtual void appendSequencesFromStream(std::istream& input, ProbabilisticSequenceContainerInterface& sc) const = 0;
196 
204  virtual void appendSequencesFromFile(const std::string& path, ProbabilisticSequenceContainerInterface& sc) const
205  {
206  std::ifstream input(path.c_str(), std::ios::in);
207  if (!input)
208  throw IOException("AbstractIProbabilisticSequences::appendSequencesFromFile: can't read file " + path);
209  appendSequencesFromStream(input, sc);
210  input.close();
211  }
212 
221  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequencesFromStream(std::istream& input, std::shared_ptr<const Alphabet>& alpha) const
222  {
223  auto vsc = std::unique_ptr<ProbabilisticSequenceContainerInterface>(new ProbabilisticVectorSequenceContainer(alpha));
224  appendSequencesFromStream(input, *vsc);
225  return vsc;
226  }
227 
235  std::unique_ptr<ProbabilisticSequenceContainerInterface> readSequencesFromFile(const std::string& path, std::shared_ptr<const Alphabet>& alpha) const
236  {
237  auto vsc = std::unique_ptr<ProbabilisticSequenceContainerInterface>(new ProbabilisticVectorSequenceContainer(alpha));
238  appendSequencesFromFile(path, *vsc);
239  return vsc;
240  }
241 };
242 } // end of namespace bpp.
243 #endif // BPP_SEQ_IO_ABSTRACTISEQUENCE_H
Partial implementation of the IProbabilisticSequence interface.
std::unique_ptr< ProbabilisticSequenceContainerInterface > readSequencesFromStream(std::istream &input, std::shared_ptr< const Alphabet > &alpha) const
Read sequences from a stream.
std::unique_ptr< ProbabilisticSequenceContainerInterface > readSequencesFromFile(const std::string &path, std::shared_ptr< const Alphabet > &alpha) const
Append sequences to a container from a file.
void readSequences(const std::string &path, ProbabilisticSequenceContainerInterface &sc) const override
Add sequences to a container from a file.
virtual void appendSequencesFromStream(std::istream &input, ProbabilisticSequenceContainerInterface &sc) const =0
Append sequences to a container from a stream.
void readSequences(std::istream &input, ProbabilisticSequenceContainerInterface &sc) const override
Add sequences to a container from a stream.
virtual void appendSequencesFromFile(const std::string &path, ProbabilisticSequenceContainerInterface &sc) const
Append sequences to a 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.
std::unique_ptr< ProbabilisticSequenceContainerInterface > readSequences(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
Partial implementation of the ISequence interface.
std::unique_ptr< SequenceContainerInterface > readSequencesFromFile(const std::string &path, std::shared_ptr< const Alphabet > &alpha) const
Append sequences to a container from a file.
std::unique_ptr< SequenceContainerInterface > readSequences(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
virtual void appendSequencesFromFile(const std::string &path, SequenceContainerInterface &sc) const
Append sequences to a container from a file.
std::unique_ptr< SequenceContainerInterface > readSequencesFromStream(std::istream &input, std::shared_ptr< const Alphabet > &alpha) const
Read sequences from a stream.
void readSequences(std::istream &input, SequenceContainerInterface &sc) const override
Add sequences to a container from a stream.
virtual void appendSequencesFromStream(std::istream &input, SequenceContainerInterface &sc) const =0
Append sequences to a container from a stream.
void readSequences(const std::string &path, SequenceContainerInterface &sc) const override
Add sequences to a 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.
The IProbabilisticSequence interface.
Definition: ISequence.h:150
The ISequence interface.
Definition: ISequence.h:29
The SequenceContainer interface.
This alphabet is used to deal NumericAlphabet.
TemplateVectorSequenceContainer< ProbabilisticSequence > ProbabilisticVectorSequenceContainer
TemplateVectorSequenceContainer< Sequence > VectorSequenceContainer