bpp-seq3  3.0.0
ProbabilisticSymbolList.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 
9 
10 using namespace bpp;
11 using namespace std;
12 
13 /****************************************************************************************/
14 
15 ProbabilisticSymbolList::ProbabilisticSymbolList(std::shared_ptr<const Alphabet>& alpha) :
16  alphabet_(alpha), content_(alpha->getResolvedChars().size(), 0)
17 {
18  content_.setRowNames(alphabet_->getResolvedChars());
19 }
20 
21 ProbabilisticSymbolList::ProbabilisticSymbolList(const DTable& list, std::shared_ptr<const Alphabet>& alpha) :
22  alphabet_(alpha), content_(alpha->getResolvedChars().size(), 0)
23 {
24  content_.setRowNames(alpha->getResolvedChars());
25  setContent(list);
26 }
27 
28 ProbabilisticSymbolList::ProbabilisticSymbolList(const vector< vector<double>>& list, std::shared_ptr<const Alphabet>& alpha) :
29  alphabet_(alpha), content_(alpha->getResolvedChars().size(), 0)
30 {
31  content_.setRowNames(alpha->getResolvedChars());
32  setContent(list);
33 }
34 
35 /****************************************************************************************/
36 
38  alphabet_(list.getAlphabet()), content_(list.getContent())
39 {}
40 
42  alphabet_(list.alphabet_), content_(list.content_)
43 {}
44 
46  alphabet_(list.getAlphabet()), content_(list.getAlphabet()->getResolvedChars().size(), list.size())
47 {
48  size_t nbc = getAlphabet()->getResolvedChars().size();
49  content_.setRowNames(alphabet_->getResolvedChars());
50 
51  for (size_t i = 0; i < size(); ++i)
52  {
53  for (size_t s = 0; s < nbc; ++s)
54  {
55  content_(s, i) = list.getStateValueAt(i, static_cast<int>(s));
56  }
57  }
58 }
59 
61 {
62  alphabet_ = list.getAlphabet();
63  setContent(list.getContent());
64  return *this;
65 }
66 
68 {
69  alphabet_ = list.alphabet_;
70  content_ = list.content_;
71  return *this;
72 }
73 
74 /****************************************************************************************/
75 
77 {
78  if (list.hasRowNames())
79  {
80  if (list.getRowNames().size() != alphabet_->getResolvedChars().size())
81  throw DimensionException("ProbabilisticSymbolList::setContent. ", list.getRowNames().size(), alphabet_->getResolvedChars().size());
82 
83  std::vector<std::string> column_names = list.getRowNames();
84  std::vector<std::string> resolved_chars = alphabet_->getResolvedChars();
85  for (std::size_t i = 0; i < list.getRowNames().size(); ++i)
86  {
87  if (column_names[i] != resolved_chars[i])
88  throw Exception("ProbabilisticSymbolList::setContent. Row names / resolved characters of alphabet mismatch at " + TextTools::toString(column_names[i]) + " and " + TextTools::toString(resolved_chars[i]) + ".");
89  }
90  }
91  else // DTable has no row names
92 
93  {
94  if (list.getNumberOfRows() != alphabet_->getResolvedChars().size())
95  throw DimensionException("ProbabilisticSymbolList::setContent. ", list.getNumberOfRows(), alphabet_->getResolvedChars().size());
96  }
97 
98  content_ = list; // final check passes, content_ becomes DTable
99 
100  if (!list.hasRowNames())
101  {
102  content_.setRowNames(alphabet_->getResolvedChars());
103  }
104 }
105 
106 /****************************************************************************************/
107 
109 {
110  stringstream ss;
111  ss.precision(10);
112 
113  for (size_t j = 0; j < content_.getNumberOfColumns(); ++j)
114  {
115  if (j != 0)
116  ss << "|";
117 
118  for (size_t i = 0; i < content_.getNumberOfRows(); ++i)
119  {
120  ss << content_.getRowName(i) << "(";
121  ss << content_(i, j) << ")";
122  }
123  }
124 
125  string st;
126  ss >> st;
127  return st;
128 }
129 
130 /****************************************************************************************/
131 
132 void ProbabilisticSymbolList::setContent(const std::vector<std::vector<double>>& list)
133 {
134  if (list.size() == 0)
135  return;
136 
137  if (list[0].size() != alphabet_->getResolvedChars().size())
138  throw DimensionException("BasicProbabilisticSymbolList::setContent. ", list[0].size(), alphabet_->getResolvedChars().size());
139 
140  content_ = list; // final check passes, content_ becomes DTable
141  content_.setRowNames(alphabet_->getResolvedChars());
142 }
143 
144 /****************************************************************************************/
145 
146 void ProbabilisticSymbolList::addElement(const std::vector<double>& element)
147 {
148  // now we add this 'row', to the content DTable, padding the end
149  // with 0's should its length be smaller than the width of this DTable
150  if (element.size() < content_.getNumberOfRows())
151  {
152  std::vector<double> padded_element(element);
153 
154  padded_element.resize(content_.getNumberOfRows(), 0.);
155 
156  content_.addColumn(padded_element);
157  }
158  else
159  {
160  if (element.size() > content_.getNumberOfRows())
161  {
162  throw BadSizeException("ProbabilisticSymbolList::addElement: too long element: ",
163  element.size(),
165  }
166  else
167  content_.addColumn(element);
168  }
169 }
170 
171 void ProbabilisticSymbolList::addElement(size_t pos, const std::vector<double>& element)
172 {
173  // now we add this 'row', to the content DTable, padding the end
174  // with 0's should its length be smaller than the width of this DTable
175  if (element.size() < content_.getNumberOfRows())
176  {
177  std::vector<double> padded_element(element);
178 
179  padded_element.resize(content_.getNumberOfRows(), 0.);
180  content_.addColumn(padded_element, (int)pos);
181  }
182  else
183  {
184  content_.addColumn(element, (int)pos);
185  }
186 }
187 
188 void ProbabilisticSymbolList::setElement(size_t pos, const std::vector<double>& element)
189 {
190  if (element.size() < content_.getNumberOfRows())
191  {
192  std::vector<double> padded_element(element);
193 
194  padded_element.resize(content_.getNumberOfRows(), 0.);
195  content_.setColumn(padded_element, pos);
196  }
197  else
198  {
199  content_.setColumn(element, pos);
200  }
201 }
The CruxSymbolList interface.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual double getStateValueAt(size_t position, int state) const =0
get value of a state at a position
The ProbabilisticSymbolList interface.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
ProbabilisticSymbolList object.
ProbabilisticSymbolList & operator=(const ProbabilisticSymbolListInterface &list)
The generic assignment operator.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
DTable content_
The list content.
size_t size() const override
Get the number of elements in the list.
ProbabilisticSymbolList(std::shared_ptr< const Alphabet > &alpha)
Build a new void ProbabilisticSymbolList object with the specified alphabet.
void addElement(const std::vector< double > &element) override
std::string toString() const override
Convert the list as a string.
std::shared_ptr< const Alphabet > alphabet_
The Alphabet attribute must be initialized in the constructor and then can never be changed.
void setElement(size_t pos, const std::vector< double > &element) override
size_t getNumberOfColumns() const
void setRowNames(const std::vector< std::string > &rowNames)
void setColumn(const std::vector< T > &newColumn, size_t pos)
std::string getRowName(size_t index) const
std::vector< std::string > getRowNames()
void addColumn(const std::vector< T > &newColumn, int pos=-1)
size_t getNumberOfRows() const
bool hasRowNames() const
virtual const std::vector< T > & getContent() const =0
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.