bpp-seq3  3.0.0
Phylip.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_PHYLIP_H
6 #define BPP_SEQ_IO_PHYLIP_H
7 
8 
9 #include "../Container/AlignedSequenceContainer.h"
10 #include "../Container/SequenceContainer.h"
11 #include "../Container/VectorSequenceContainer.h"
12 #include "../Sequence.h"
13 #include "AbstractIAlignment.h"
14 #include "AbstractOAlignment.h"
15 
16 // From the STL:
17 #include <iostream>
18 
19 namespace bpp
20 {
28 class Phylip :
29  public AbstractIAlignment2,
30  public AbstractOAlignment
31 {
32 private:
33  /* this class allows two kinds of Phylip format:
34  * traditional, with names limited to 10 chars,
35  * and 'extended', defined by PAML, with names separated from sequences by at least 6 white spaces.
36  */
37  bool extended_;
38  /* tells if sequences are in the seuqential or the interleave format/
39  */
41 
45  unsigned int charsByLine_;
46 
47  std::string namesSplit_;
48 
49 public:
58  Phylip(bool extended = true, bool sequential = true, unsigned int charsByLine = 100, const std::string& split = " ") :
59  extended_(extended), sequential_(sequential), charsByLine_(charsByLine), namesSplit_(split) {}
60 
61  virtual ~Phylip() {}
62 
63 public:
69  void appendAlignmentFromStream(std::istream& input, SequenceContainerInterface& sc) const override;
80  virtual std::unique_ptr<SequenceContainerInterface> readSequences(std::istream& input, std::shared_ptr<const Alphabet> alpha) const override
81  {
82  return readAlignment(input, alpha);
83  }
84  virtual std::unique_ptr<SequenceContainerInterface> readSequences(const std::string& path, std::shared_ptr<const Alphabet> alpha) const override
85  {
86  return readAlignment(path, alpha);
87  }
96  unsigned int getNumberOfSequences(const std::string& path) const;
97 
103  void writeAlignment(std::ostream& output, const SiteContainerInterface& sc) const override;
104  void writeAlignment(const std::string& path, const SiteContainerInterface& sc, bool overwrite) const override
105  {
106  AbstractOAlignment::writeAlignment(path, sc, overwrite);
107  }
116  const std::string getFormatName() const override;
117  const std::string getFormatDescription() const override;
123  const std::string& getSplit() const { return namesSplit_; }
124 
128  void setSplit(const std::string& split) { namesSplit_ = split; }
129 
130 protected:
131  // Reading tools:
132  const std::vector<std::string> splitNameAndSequence(const std::string& s) const;
133  void readSequential (std::istream& in, SequenceContainerInterface& asc) const;
134  void readInterleaved(std::istream& in, SequenceContainerInterface& asc) const;
135  // Writing tools:
136  std::vector<std::string> getSizedNames(const std::vector<std::string>& names) const;
137  void writeSequential(std::ostream& out, const SiteContainerInterface& sc) const;
138  void writeInterleaved(std::ostream& out, const SiteContainerInterface& sc) const;
139 };
140 } // end of namespace bpp.
141 #endif // BPP_SEQ_IO_PHYLIP_H
Partial implementation of the IAlignment and ISequence interface, dedicated to alignment readers.
virtual void readAlignment(std::istream &input, SequenceContainerInterface &sc) const override
Add sequences to a container from a stream.
Partial implementation of the OAlignment interface.
void writeAlignment(std::ostream &output, const SiteContainerInterface &sc) const override=0
Write a container to a stream.
The Phylip & co format.
Definition: Phylip.h:31
void writeSequential(std::ostream &out, const SiteContainerInterface &sc) const
Definition: Phylip.cpp:213
const std::string getFormatDescription() const override
Definition: Phylip.cpp:290
bool sequential_
Definition: Phylip.h:40
virtual std::unique_ptr< SequenceContainerInterface > readSequences(const std::string &path, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a file.
Definition: Phylip.h:84
void readSequential(std::istream &in, SequenceContainerInterface &asc) const
Definition: Phylip.cpp:42
bool extended_
Definition: Phylip.h:37
void writeInterleaved(std::ostream &out, const SiteContainerInterface &sc) const
Definition: Phylip.cpp:233
const std::string & getSplit() const
Definition: Phylip.h:123
virtual std::unique_ptr< SequenceContainerInterface > readSequences(std::istream &input, std::shared_ptr< const Alphabet > alpha) const override
Create a new container from a stream.
Definition: Phylip.h:80
virtual ~Phylip()
Definition: Phylip.h:61
std::string namesSplit_
Definition: Phylip.h:47
unsigned int getNumberOfSequences(const std::string &path) const
Definition: Phylip.cpp:161
void writeAlignment(std::ostream &output, const SiteContainerInterface &sc) const override
Write a container to a stream.
Definition: Phylip.cpp:266
const std::string getFormatName() const override
Definition: Phylip.cpp:286
void writeAlignment(const std::string &path, const SiteContainerInterface &sc, bool overwrite) const override
Write a container to a file.
Definition: Phylip.h:104
std::vector< std::string > getSizedNames(const std::vector< std::string > &names) const
Definition: Phylip.cpp:180
unsigned int charsByLine_
The maximum number of chars to be written on a line.
Definition: Phylip.h:45
const std::vector< std::string > splitNameAndSequence(const std::string &s) const
Definition: Phylip.cpp:21
Phylip(bool extended=true, bool sequential=true, unsigned int charsByLine=100, const std::string &split=" ")
Build a new Phylip file reader.
Definition: Phylip.h:58
void readInterleaved(std::istream &in, SequenceContainerInterface &asc) const
Definition: Phylip.cpp:101
void appendAlignmentFromStream(std::istream &input, SequenceContainerInterface &sc) const override
Append sequences to a container from a stream.
Definition: Phylip.cpp:145
void setSplit(const std::string &split)
Definition: Phylip.h:128
The SequenceContainer interface.
std::vector< std::string > split(const std::string &s, std::size_t n)
This alphabet is used to deal NumericAlphabet.