bpp-seq3  3.0.0
Sequence.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Text/TextTools.h>
6 
8 #include "Sequence.h" // class's header file
9 #include "StringSequenceTools.h"
10 
11 using namespace bpp;
12 
13 // From the STL:
14 #include <iostream>
15 
16 using namespace std;
17 
18 /******************************************************************************/
19 
20 void Sequence::setContent(const std::string& sequence)
21 {
22  // Remove blanks in sequence
23  auto alphaPtr = getAlphabet();
25  // Warning, an exception may be thrown here!
26 }
27 
28 /******************************************************************************/
29 
30 void Sequence::setToSizeR(size_t newSize)
31 {
32  // Size verification
33  size_t seqSize = content_.size();
34  if (newSize == seqSize)
35  return;
36 
37  if (newSize < seqSize)
38  {
39  content_.resize(newSize);
40  return;
41  }
42 
43  // Add gaps up to specified size
44  int gap = getAlphabet()->getGapCharacterCode();
45  while (content_.size() < newSize)
46  content_.push_back(gap);
47 }
48 
49 /******************************************************************************/
50 
51 void Sequence::setToSizeL(size_t newSize)
52 {
53  // Size verification
54  size_t seqSize = content_.size();
55  if (newSize == seqSize)
56  return;
57 
58  if (newSize < seqSize)
59  {
60  // We must truncate sequence from the left.
61  // This is a very unefficient method!
62  content_.erase(content_.begin(), content_.begin() + static_cast<ptrdiff_t>(seqSize - newSize));
63  return;
64  }
65 
66  // Add gaps up to specified size
67  int gap = getAlphabet()->getGapCharacterCode();
68  content_.insert(content_.begin(), newSize - seqSize, gap);
69 }
70 
71 /******************************************************************************/
72 
74 {
75  if (seq.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
76  throw AlphabetMismatchException("Sequence::append", getAlphabet(), seq.getAlphabet());
77  // Check list for incorrect characters
78  for (auto i : seq.getContent())
79  {
80  content_.push_back(i);
81  }
82 }
83 
84 void Sequence::append(const std::vector<int>& content)
85 {
86  // Check list for incorrect characters
87  for (auto i : content)
88  {
89  if (!getAlphabet()->isIntInAlphabet(i))
90  throw BadIntException(i, "Sequence::append", getAlphabet());
91  }
92  // Sequence is valid:
93  for (auto i : content)
94  {
95  content_.push_back(i);
96  }
97 }
98 
99 void Sequence::append(const std::vector<std::string>& content)
100 {
101  // Check list for incorrect characters
102  for (auto i : content)
103  {
104  if (!getAlphabet()->isCharInAlphabet(i))
105  throw BadCharException(i, "Sequence::append", getAlphabet());
106  }
107 
108  // BasicSequence is valid:
109  for (auto i : content)
110  {
111  content_.push_back(getAlphabet()->charToInt(i));
112  }
113 }
114 
115 void Sequence::append(const std::string& content)
116 {
117  auto alphaPtr = getAlphabet();
118  append(StringSequenceTools::codeSequence(content, alphaPtr));
119 }
120 
121 /******************************************************************************/
Exception thrown when two alphabets do not match.
An alphabet exception thrown when trying to specify a bad char to the alphabet.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
The sequence interface.
Definition: Sequence.h:34
void setToSizeL(size_t newSize) override
Set up the size of a sequence from the left side.
Definition: Sequence.cpp:51
void setToSizeR(size_t newSize) override
Set up the size of a sequence from the right side.
Definition: Sequence.cpp:30
void append(const SequenceInterface &seq) override
Append the content of the sequence.
Definition: Sequence.cpp:73
static std::vector< int > codeSequence(const std::string &sequence, std::shared_ptr< const Alphabet > &alphabet)
Convert a string sequence to a vector of int.
virtual const std::vector< T > & getContent() const =0
std::string removeWhiteSpaces(const std::string &s)
This alphabet is used to deal NumericAlphabet.