bpp-seq3  3.0.0
VectorSequenceContainer.cpp
Go to the documentation of this file.
1 //
2 // File: VectorSequenceContainer.cpp
3 // Authors:
4 // Guillaume Deuchst
5 // Julien Dutheil
6 // Last modified: 2003-07-30 00:00:00
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide classes
13  for sequences analysis.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #include <Bpp/Text/TextTools.h>
43 
45 
46 using namespace bpp;
47 using namespace std;
48 
52  const std::vector<std::shared_ptr<Sequence>>& vs,
53  const Alphabet* alpha) :
56 {
57  for (auto i = vs.begin(); i < vs.end(); i++)
58  {
59  addSequence(*i);
60  }
61 }
62 
66  const VectorSequenceContainer& vsc) :
69 {
70  size_t max = vsc.getNumberOfSequences();
71  for (size_t i = 0; i < max; i++)
72  {
73  addSequence(vsc.getSequence(i), false);
74  }
75 }
76 
78  const OrderedSequenceContainer& osc) :
79  AbstractSequenceContainer(osc.getAlphabet()),
81 {
82  // Sequences insertion
83  for (size_t i = 0; i < osc.getNumberOfSequences(); i++)
84  {
85  addSequence(osc.getSequence(i), false);
86  }
87 }
88 
90  const SequenceContainer& sc) :
91  AbstractSequenceContainer(sc.getAlphabet()),
93 {
94  // Sequences insertion
95  for (auto name: sc.getSequenceNames())
96  {
97  addSequence(sc.getSequence(name), false);
98  }
99 
101 }
102 
106  const VectorSequenceContainer& vsc)
107 {
108  clear();
111 
112  return *this;
113 }
114 
116  const OrderedSequenceContainer& osc)
117 {
118  clear();
120 
121  // Sequences insertion
122  size_t max = osc.getNumberOfSequences();
123  for (unsigned int i = 0; i < max; i++)
124  {
125  addSequence(osc.getSequence(i), false);
126  }
127 
128  return *this;
129 }
130 
131 /******************************************************************************/
132 
134  const SequenceContainer& sc)
135 {
136  clear();
138 
139  for (auto name: sc.getSequenceNames())
140  {
141  addSequence(sc.getSequence(name), false);
142  }
143 
144  return *this;
145 }
146 
147 bool VectorSequenceContainer::hasSequenceByName(const std::string& name) const
148 {
149  auto nbseq=getSize();
150  for (size_t i=0;i<nbseq;i++)
151  if (getSequence(i).getName()==name)
152  return true;
153  return false;
154 }
155 
156 const Sequence& VectorSequenceContainer::getSequenceByName(const std::string& name) const
157 {
158  auto nbseq=getSize();
159  for (size_t i=0;i<nbseq;i++)
160  {
161  const Sequence& seq = getSequence(i);
162  if (seq.getName()==name)
163  return seq;
164  }
165  throw Exception("VectorSequenceContainer::getSequenceByName: Unknown sequence name: " + name);
166 }
167 
168 std::vector<std::string> VectorSequenceContainer::getSequenceNames() const
169 {
170  std::vector<std::string> vs;
171  auto nbseq=getSize();
172  for (size_t i=0;i<nbseq;i++)
173  vs.push_back(getSequence(i).getName());
174 
175  return vs;
176 }
177 
178 std::shared_ptr<Sequence> VectorSequenceContainer::removeSequenceByName(const std::string& name)
179 {
180  auto nbseq=getSize();
181  for (size_t i=0;i<nbseq;i++)
182  {
183  const Sequence& seq = getSequence(i);
184  if (seq.getName()==name)
185  return removeSequence(i);
186  }
187  throw Exception("VectorSequenceContainer::removeSequenceByName: Unknown sequence name: " + name);
188 }
189 
191 {
192  auto nbseq=getSize();
193  for (size_t i=0;i<nbseq;i++)
194  {
195  const Sequence& seq = getSequence(i);
196  if (seq.getName()==name)
197  deleteSequence(i);
198  }
199  throw Exception("VectorSequenceContainer::deleteSequenceByName: Unknown sequence name: " + name);
200 }
201 
202 
203 /******************************************************************************/
204 
206  const std::vector<std::string>& names,
207  bool checkNames)
208 {
209  if (names.size() != getNumberOfSequences())
210  throw IndexOutOfBoundsException("VectorSequenceContainer::setSequenceNames : bad number of names", names.size(), getNumberOfSequences(), getNumberOfSequences());
211  if (checkNames)
212  {
213  for (size_t i = 0; i < names.size(); i++)
214  {
215  // For all names in vector : throw exception if name already exists
216  for (size_t j = 0; j < i; j++)
217  {
218  if (names[j] == names[i])
219  throw Exception("VectorSiteContainer::setSequenceNames : Sequence's name already exists in container");
220  }
221  }
222  }
223  for (size_t i = 0; i < names.size(); i++)
224  {
225  getSequence_(i).setName(names[i]);
226  }
227 
228  setObjectNames(names);
229 }
230 
231 /******************************************************************************/
232 
234 {
237  return vsc;
238 }
239 
240 /******************************************************************************/
Partial implementation of the OrderedSequenceContainer interface.
AbstractSequenceContainer & operator=(const AbstractSequenceContainer &sc)
virtual const std::string & getName(size_t sequenceIndex) const
Get the name of a particular row of the alignement (aka sequence).
void setGeneralComments(const Comments &comments)
Set the comments of this container.
const Alphabet * getAlphabet() const
Get container's alphabet.
const Comments & getGeneralComments() const
Get the comments of this container.
The Alphabet interface.
Definition: Alphabet.h:133
virtual void setName(const std::string &name)=0
Set the name of this sequence.
virtual const std::string & getName() const =0
Get the name of this sequence.
The OrderedSequenceContainer interface.
virtual const Sequence & getSequence(const std::string &name) const=0
Retrieve a sequence object from the container.
virtual const Sequence & getSequence(size_t sequenceIndex) const =0
Retrieve a sequence object from the container.
virtual std::shared_ptr< Sequence > removeSequence(const std::string &name)=0
Extract (and remove) a sequence from the container.
The SequenceContainer interface.
virtual const Sequence & getSequence(const std::string &name) const =0
Retrieve a sequence object from the container.
The sequence interface.
Definition: Sequence.h:71
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.
virtual std::vector< std::string > getSequenceNames() const =0
Get all the names of the sequences in the container.
virtual const Comments & getGeneralComments() const =0
Get the comments of this container.
The template VectorMappedContainer class.
VectorMappedContainer< T > & operator=(const VectorMappedContainer &vsc)
void setObjectNames(const std::vector< std::string > &names)
The VectorSequenceContainer class.
std::vector< std::string > getSequenceNames() const
get Sequences proper names (may be different from the keys used to store them in the map),...
virtual void addSequence(const Sequence &sequence, bool checkName=true)
Add a sequence at the end of the container.
size_t getNumberOfSequences() const
Get the number of sequences in the container.
void deleteSequenceByName(const std::string &name)
remove & return a Sequence with this name.
VectorSequenceContainer(const std::vector< std::shared_ptr< Sequence >> &vs, const Alphabet *alpha)
Build a container with shared Sequences.
const Sequence & getSequence(const std::string &name) const
get the Sequence with this name in the map (same as getSequenceByKey).
void deleteSequence(const std::string &name)
delete the Sequence with this name in the map (same as deleteSequenceByKey).
VectorSequenceContainer & operator=(const VectorSequenceContainer &vsc)
Assign from a VectorSequenceContainer.
std::shared_ptr< Sequence > removeSequenceByName(const std::string &name)
remove & return a Sequence with this name.
VectorSequenceContainer * createEmptyContainer() const
Return a copy of this container, but with no data inside.
bool hasSequenceByName(const std::string &name) const
void setSequenceNames(const std::vector< std::string > &names, bool checkNames=true)
set the proper names of the Sequences, in the order of the vector.
const Sequence & getSequenceByName(const std::string &name) const
void clear()
Delete all objects in the container.
This alphabet is used to deal NumericAlphabet.