bpp-seq3  3.0.0
VectorProbabilisticSequenceContainer.h
Go to the documentation of this file.
1 //
2 // File: VectorProbabilisticSequenceContainer.h
3 // Authors:
4 // Laurent Guéguen
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef BPP_SEQ_CONTAINER_VECTOR_PROBABILISTIC_SEQUENCECONTAINER_H
41 #define BPP_SEQ_CONTAINER_VECTOR_PROBABILISTIC_SEQUENCECONTAINER_H
42 
43 #include <Bpp/Exceptions.h>
44 
45 #include "../Alphabet/Alphabet.h"
46 #include "../ProbabilisticSequence.h"
48 #include "VectorMappedContainer.h"
49 
50 // From the STL:
51 #include <algorithm>
52 #include <vector>
53 
54 namespace bpp
55 {
65  virtual public VectorMappedContainer<ProbabilisticSequence>
66 {
67 public:
68 
75  const std::vector<std::shared_ptr<ProbabilisticSequence>>& vs, const Alphabet* alpha);
76 
83 
96 
105 
106 
107  void clear()
108  {
110  }
111 
112 public:
113 
114  std::string toString(const std::string& name) const
115  {
116  return getSequence(name).toString();
117  }
118 
119  std::string toString(size_t sequenceIndex) const
120  {
121  return getSequence(sequenceIndex).toString();
122  }
123 
124  const Comments& getComments(const std::string& name) const
125  {
126  return getSequence(name).getComments();
127  }
128 
129  virtual const Comments& getComments(size_t sequenceIndex) const
130  {
131  return getSequence(sequenceIndex).getComments();
132  }
133 
134  void setComments(const std::string& name, const Comments& comments)
135  {
136  size_t pos = getSequencePosition(name);
137  setComments(pos, comments);
138  }
139 
140  const std::string& getName(size_t sequenceIndex) const
141  {
142  return getSequence(sequenceIndex).getName();
143  }
144 
145  size_t getSequencePosition(const std::string& name) const
146  {
147  return getObjectPosition(name);
148  }
149 
156  {
157  return new VectorProbabilisticSequenceContainer(*this);
158  }
159 
167  bool hasSequence(const std::string& name) const
168  {
169  return hasObject(name);
170  }
171 
172  const ProbabilisticSequence& getSequence(const std::string& name) const
173  {
174  return *getObject(name);
175  }
176 
177  void setSequence(const std::string& name, const ProbabilisticSequence& sequence, bool checkName = true)
178  {
179  setSequence(getSequencePosition(name), sequence, checkName);
180  }
181 
182  std::shared_ptr<ProbabilisticSequence> removeSequence(const std::string& name)
183  {
184  return removeSequence(getSequencePosition(name));
185  }
186 
187  size_t getNumberOfSequences() const { return getSize(); }
188 
189  std::vector<std::string> getSequenceNames() const
190  {
191  return getObjectNames();
192  }
193 
194  void setSequenceNames(const std::vector<std::string>& names, bool checkNames = true);
195 
197 
201  const ProbabilisticSequence& getSequence(size_t sequenceIndex) const
202  {
203  return *getObject(sequenceIndex);
204  }
205 
206  void setSequence(size_t sequenceIndex, const ProbabilisticSequence& sequence, bool checkName = true)
207  {
208  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
209  throw AlphabetMismatchException("VectorProbabilisticSequenceContainer::setSequence : Alphabets don't match", getAlphabet(), sequence.getAlphabet());
210 
211  addObject(std::shared_ptr<ProbabilisticSequence>(sequence.clone()), sequenceIndex, sequence.getName(), checkName);
212  }
213 
214  std::shared_ptr<ProbabilisticSequence> removeSequence(size_t sequenceIndex)
215  {
216  return removeObject(sequenceIndex);
217  }
218 
219  void setComments(size_t sequenceIndex, const Comments& comments)
220  {
221  getSequence_(sequenceIndex).setComments(comments);
222  }
223 
244  virtual void addSequence(const ProbabilisticSequence& sequence, bool checkName = true)
245  {
246  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
247  throw AlphabetMismatchException("VectorProbabilisticSequenceContainer::addSequence : Alphabets don't match", getAlphabet(), sequence.getAlphabet());
248 
249  appendObject(std::shared_ptr<ProbabilisticSequence>(sequence.clone()), sequence.getName(), checkName);
250  }
251 
267  virtual void addSequence(const std::shared_ptr<ProbabilisticSequence> sequence, bool checkName = true)
268  {
269  if (sequence->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
270  throw AlphabetMismatchException("VectorProbabilisticSequenceContainer::addSequence : Alphabets don't match", getAlphabet(), sequence->getAlphabet());
271 
272  appendObject(sequence, sequence->getName(), checkName);
273  }
274 
291  virtual void addSequence(const ProbabilisticSequence& sequence, size_t sequenceIndex, bool checkName = true)
292  {
293  addObject(std::shared_ptr<ProbabilisticSequence>(sequence.clone()), sequenceIndex, sequence.getName(), checkName);
294  }
295 
296 protected:
303  {
304  return *getObject(i);
305  }
306 
307 
308  ProbabilisticSequence& getSequence_(const std::string& name)
309  {
310  return *getObject(name);
311  }
312 
313  double getStateValueAt(size_t siteIndex, const std::string& sequenceName, int state) const
314  {
315  return getSequence(sequenceName).getStateValueAt(siteIndex, state);
316  }
317 
318  double operator()(size_t siteIndex, const std::string& sequenceName, int state) const
319  {
320  return getSequence(sequenceName)(siteIndex, state);
321  }
322 
323  double getStateValueAt(size_t siteIndex, size_t sequenceIndex, int state) const
324  {
325  if (sequenceIndex >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorProbabilisticSequenceContainer::getStateValueAt.", sequenceIndex, 0, getNumberOfSequences() - 1);
326  const ProbabilisticSequence& seq = getSequence(sequenceIndex);
327 
328  if (siteIndex >= seq.size())
329  throw IndexOutOfBoundsException("VectorProbabilisticSequenceContainer::getStateValueAt.", siteIndex, 0, seq.size() - 1);
330 
331  return (getSequence(sequenceIndex))[siteIndex][getAlphabet()->getStateIndex(state)-1];
332  }
333 
334  double operator()(size_t siteIndex, size_t sequenceIndex, int state) const
335  {
336  return (getSequence(sequenceIndex))[siteIndex][getAlphabet()->getStateIndex(state)-1];
337  }
338 
339 };
340 } // end of namespace bpp.
341 #endif // BPP_SEQ_CONTAINER_VECTOR_PROBABILISTIC_SEQUENCECONTAINER_H
Partial implementation of the OrderedSequenceContainer interface.
const Alphabet * getAlphabet() const
Get container's alphabet.
Exception thrown when two alphabets do not match.
The Alphabet interface.
Definition: Alphabet.h:133
virtual std::string getAlphabetType() const =0
Identification method.
virtual size_t getStateIndex(int state) const =0
virtual const std::string & getName() const =0
Get the name of this sequence.
virtual void setComments(const Comments &comments)=0
Set the comments.
virtual const Comments & getComments() const =0
Get the comments.
virtual std::string toString() const =0
Convert the list as a string.
virtual const Alphabet * getAlphabet() const =0
Get the alphabet associated to the list.
virtual double getStateValueAt(size_t siteIndex, int state) const
virtual size_t size() const =0
Get the number of elements in the list.
virtual void setComments(const std::string &name, const Comments &comments)=0
Set the comments of a particular sequence.
The probabilistic sequence interface.
virtual ProbabilisticSequence * clone() const =0
The template VectorMappedContainer class.
const std::shared_ptr< ProbabilisticSequence > getObject(size_t objectIndex) const
void appendObject(std::shared_ptr< ProbabilisticSequence > object, const std::string &name, bool check=true)
size_t getObjectPosition(const std::string &name) const
void clear()
Delete all objects in the container.
std::shared_ptr< ProbabilisticSequence > removeObject(size_t objectIndex)
void addObject(std::shared_ptr< ProbabilisticSequence > object, size_t objectIndex, const std::string &name, bool check=false)
The VectorProbabilisticSequenceContainer class.
VectorProbabilisticSequenceContainer * createEmptyContainer() const
Return a copy of this container, but with no data inside.
std::vector< std::string > getSequenceNames() const
Get all the names of the sequences in the container.
virtual const Comments & getComments(size_t sequenceIndex) const
const std::string & getName(size_t sequenceIndex) const
Get the name of a particular row of the alignement (aka sequence).
double getStateValueAt(size_t siteIndex, const std::string &sequenceName, int state) const
get value of a state in a position
ProbabilisticSequence & getSequence_(const std::string &name)
void setComments(const std::string &name, const Comments &comments)
Set the comments of a particular sequence.
void clear()
Delete all objects in the container.
std::string toString(const std::string &name) const
Convert a particular sequence to a string.
virtual void addSequence(const ProbabilisticSequence &sequence, size_t sequenceIndex, bool checkName=true)
Add a sequence to the container at a particular position.
VectorProbabilisticSequenceContainer(const Alphabet *alpha)
Build an empty container that will contain sequences of a particular alphabet.
size_t getNumberOfSequences() const
Get the number of sequences in the container.
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
void setSequence(size_t sequenceIndex, const ProbabilisticSequence &sequence, bool checkName=true)
VectorProbabilisticSequenceContainer(const std::vector< std::shared_ptr< ProbabilisticSequence >> &vs, const Alphabet *alpha)
Build a container with shared Sequences.
const ProbabilisticSequence & getSequence(size_t sequenceIndex) const
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
virtual void addSequence(const ProbabilisticSequence &sequence, bool checkName=true)
Add a sequence at the end of the container.
double operator()(size_t siteIndex, size_t sequenceIndex, int state) const
void setComments(size_t sequenceIndex, const Comments &comments)
const ProbabilisticSequence & getSequence(const std::string &name) const
void setSequenceNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
std::shared_ptr< ProbabilisticSequence > removeSequence(const std::string &name)
void setSequence(const std::string &name, const ProbabilisticSequence &sequence, bool checkName=true)
std::shared_ptr< ProbabilisticSequence > removeSequence(size_t sequenceIndex)
VectorProbabilisticSequenceContainer * clone() const
double getStateValueAt(size_t siteIndex, size_t sequenceIndex, int state) const
const Comments & getComments(const std::string &name) const
Get comments of a particular sequence.
virtual void addSequence(const std::shared_ptr< ProbabilisticSequence > sequence, bool checkName=true)
Add a sequence at the end of the container.
VectorProbabilisticSequenceContainer & operator=(const VectorProbabilisticSequenceContainer &vsc)
Assign from a VectorProbabilisticSequenceContainer.
double operator()(size_t siteIndex, const std::string &sequenceName, int state) const
This alphabet is used to deal NumericAlphabet.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Commentable.h:58