~ubuntu-branches/ubuntu/saucy/libbpp-seq/saucy-proposed

« back to all changes in this revision

Viewing changes to src/Bpp/Seq/Container/VectorSiteContainer.cpp

  • Committer: Package Import Robot
  • Author(s): Julien Dutheil
  • Date: 2013-03-05 14:31:00 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130305143100-2jox395vr1ghao46
Tags: 2.1.0-1
* 'omics' tools now in bpp-seq-omics
* Extended BppO support
* StateProperties renamed to AlphabetIndex

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
//
6
6
 
7
7
/*
8
 
Copyright or © or Copr. CNRS, (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
 
*/
 
8
   Copyright or © or Copr. CNRS, (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
39
 
40
40
#include "VectorSiteContainer.h"
41
41
 
53
53
  const std::vector<const Site*>& vs,
54
54
  const Alphabet* alpha,
55
55
  bool checkPositions)
56
 
  throw (Exception):
 
56
throw (Exception) :
57
57
  AbstractSequenceContainer(alpha),
58
58
  sites_(0),
59
59
  names_(0),
60
60
  comments_(0),
61
61
  sequences_(0)
62
62
{
63
 
  if (vs.size() == 0) throw Exception("VectorSiteContainer::VectorSiteContainer. Empty site set.");
64
 
  //Seq names and comments:
65
 
  unsigned int nbSeq = vs[0]->size();
 
63
  if (vs.size() == 0)
 
64
    throw Exception("VectorSiteContainer::VectorSiteContainer. Empty site set.");
 
65
  // Seq names and comments:
 
66
  size_t nbSeq = vs[0]->size();
66
67
  names_.resize(nbSeq);
67
68
  comments_.resize(nbSeq);
68
 
  for (unsigned int i = 0; i < nbSeq; i++)
 
69
  for (size_t i = 0; i < nbSeq; i++)
69
70
  {
70
71
    names_[i]    = "Seq_" + TextTools::toString(i);
71
 
    comments_[i] = new Comments(); 
 
72
    comments_[i] = new Comments();
72
73
  }
73
 
  //Now try to add each site:
74
 
  for (unsigned int i = 0; i < vs.size(); i++)
 
74
  // Now try to add each site:
 
75
  for (size_t i = 0; i < vs.size(); i++)
75
76
  {
76
 
    addSite(*vs[i], checkPositions); //This may throw an exception if position argument already exists or is size is not valid.
 
77
    addSite(*vs[i], checkPositions); // This may throw an exception if position argument already exists or is size is not valid.
77
78
  }
78
 
  
 
79
 
79
80
  sequences_.resize(nbSeq);
80
81
}
81
82
 
82
83
/******************************************************************************/
83
 
  
84
 
VectorSiteContainer::VectorSiteContainer(unsigned int size, const Alphabet* alpha):
 
84
 
 
85
VectorSiteContainer::VectorSiteContainer(size_t size, const Alphabet* alpha) :
85
86
  AbstractSequenceContainer(alpha),
86
87
  sites_(0),
87
88
  names_(size),
88
89
  comments_(size),
89
90
  sequences_(size)
90
91
{
91
 
  //Seq names and comments:
92
 
  for (unsigned int i = 0; i < size; i++)
 
92
  // Seq names and comments:
 
93
  for (size_t i = 0; i < size; i++)
93
94
  {
94
95
    names_[i]    = string("Seq_") + TextTools::toString(i);
95
96
    comments_[i] = new Comments();
97
98
}
98
99
 
99
100
/******************************************************************************/
100
 
  
101
 
VectorSiteContainer::VectorSiteContainer(const std::vector<std::string>& names, const Alphabet* alpha):
 
101
 
 
102
VectorSiteContainer::VectorSiteContainer(const std::vector<std::string>& names, const Alphabet* alpha) :
102
103
  AbstractSequenceContainer(alpha),
103
104
  sites_(0),
104
105
  names_(names.size()),
105
106
  comments_(names.size()),
106
107
  sequences_(names.size())
107
108
{
108
 
  //Seq names and comments:
109
 
  for (unsigned int i = 0; i < names.size(); i++)
 
109
  // Seq names and comments:
 
110
  for (size_t i = 0; i < names.size(); i++)
110
111
  {
111
112
    names_[i]    = names[i];
112
113
    comments_[i] = new Comments();
115
116
 
116
117
/******************************************************************************/
117
118
 
118
 
VectorSiteContainer::VectorSiteContainer(const Alphabet* alpha):
 
119
VectorSiteContainer::VectorSiteContainer(const Alphabet* alpha) :
119
120
  AbstractSequenceContainer(alpha),
120
121
  sites_(0),
121
122
  names_(0),
125
126
 
126
127
/******************************************************************************/
127
128
 
128
 
VectorSiteContainer::VectorSiteContainer(const VectorSiteContainer& vsc):
 
129
VectorSiteContainer::VectorSiteContainer(const VectorSiteContainer& vsc) :
129
130
  AbstractSequenceContainer(vsc),
130
131
  sites_(0),
131
132
  names_(vsc.names_),
132
133
  comments_(vsc.getNumberOfSequences()),
133
134
  sequences_(vsc.getNumberOfSequences())
134
135
{
135
 
  //Now try to add each site:
136
 
  for (unsigned int i = 0; i < vsc.getNumberOfSites(); i++)
137
 
    addSite(vsc.getSite(i), false); //We assume that positions are correct.
 
136
  // Now try to add each site:
 
137
  for (size_t i = 0; i < vsc.getNumberOfSites(); i++)
 
138
  {
 
139
    addSite(vsc.getSite(i), false); // We assume that positions are correct.
 
140
  }
138
141
  // Seq comments:
139
 
  for (unsigned int i = 0; i < vsc.getNumberOfSequences(); i++)
 
142
  for (size_t i = 0; i < vsc.getNumberOfSequences(); i++)
 
143
  {
140
144
    comments_[i] = new Comments(vsc.getComments(i));
 
145
  }
141
146
}
142
147
 
143
148
/******************************************************************************/
144
149
 
145
 
VectorSiteContainer::VectorSiteContainer(const SiteContainer& sc):
 
150
VectorSiteContainer::VectorSiteContainer(const SiteContainer& sc) :
146
151
  AbstractSequenceContainer(sc),
147
152
  sites_(0),
148
153
  names_(sc.getSequencesNames()),
149
154
  comments_(sc.getNumberOfSequences()),
150
155
  sequences_(sc.getNumberOfSequences())
151
156
{
152
 
  //Now try to add each site:
153
 
  for (unsigned int i = 0; i < sc.getNumberOfSites(); i++)
154
 
    addSite(sc.getSite(i), false); //We assume that positions are correct.
 
157
  // Now try to add each site:
 
158
  for (size_t i = 0; i < sc.getNumberOfSites(); i++)
 
159
  {
 
160
    addSite(sc.getSite(i), false); // We assume that positions are correct.
 
161
  }
155
162
  // Seq comments:
156
 
  for (unsigned int i = 0; i < sc.getNumberOfSequences(); i++)
 
163
  for (size_t i = 0; i < sc.getNumberOfSequences(); i++)
 
164
  {
157
165
    comments_[i] = new Comments(sc.getComments(i));
 
166
  }
158
167
}
159
168
 
160
169
/******************************************************************************/
161
170
 
162
 
VectorSiteContainer::VectorSiteContainer(const OrderedSequenceContainer& osc):
 
171
VectorSiteContainer::VectorSiteContainer(const OrderedSequenceContainer& osc) :
163
172
  AbstractSequenceContainer(osc),
164
173
  sites_(0),
165
174
  names_(0),
166
175
  comments_(0),
167
176
  sequences_(0)
168
177
{
169
 
  for (unsigned int i = 0; i < osc.getNumberOfSequences(); i++)
 
178
  for (size_t i = 0; i < osc.getNumberOfSequences(); i++)
 
179
  {
170
180
    addSequence(osc.getSequence(i), false);
 
181
  }
171
182
  reindexSites();
172
183
}
173
184
 
174
185
/******************************************************************************/
175
186
 
176
 
VectorSiteContainer::VectorSiteContainer(const SequenceContainer& sc):
 
187
VectorSiteContainer::VectorSiteContainer(const SequenceContainer& sc) :
177
188
  AbstractSequenceContainer(sc),
178
189
  sites_(0),
179
190
  names_(0),
181
192
  sequences_(0)
182
193
{
183
194
  vector<string> names = sc.getSequencesNames();
184
 
  for (unsigned int i = 0 ; i < names.size() ; i++)
 
195
  for (size_t i = 0; i < names.size(); i++)
 
196
  {
185
197
    addSequence(sc.getSequence(names[i]), false);
 
198
  }
186
199
  reindexSites();
187
200
}
188
201
 
192
205
{
193
206
  clear();
194
207
  AbstractSequenceContainer::operator=(vsc);
195
 
  //Seq names:
 
208
  // Seq names:
196
209
  names_.resize(vsc.getNumberOfSequences());
197
210
  setSequencesNames(vsc.getSequencesNames(), true);
198
 
  //Now try to add each site:
199
 
  for (unsigned int i = 0; i < vsc.getNumberOfSites(); i++)
200
 
    addSite(vsc.getSite(i), false); //We assume that positions are correct.
201
 
  //Seq comments:
202
 
  unsigned int nbSeq = vsc.getNumberOfSequences();
 
211
  // Now try to add each site:
 
212
  for (size_t i = 0; i < vsc.getNumberOfSites(); i++)
 
213
  {
 
214
    addSite(vsc.getSite(i), false); // We assume that positions are correct.
 
215
  }
 
216
  // Seq comments:
 
217
  size_t nbSeq = vsc.getNumberOfSequences();
203
218
  comments_.resize(nbSeq);
204
 
  for (unsigned int i = 0; i < nbSeq; i++)
 
219
  for (size_t i = 0; i < nbSeq; i++)
 
220
  {
205
221
    comments_[i] = new Comments(vsc.getComments(i));
 
222
  }
206
223
  sequences_.resize(nbSeq);
207
224
 
208
225
  return *this;
214
231
{
215
232
  clear();
216
233
  AbstractSequenceContainer::operator=(sc);
217
 
  //Seq names:
 
234
  // Seq names:
218
235
  names_.resize(sc.getNumberOfSequences());
219
236
  setSequencesNames(sc.getSequencesNames(), true);
220
 
  //Now try to add each site:
221
 
  for (unsigned int i = 0; i < sc.getNumberOfSites(); i++)
222
 
    addSite(sc.getSite(i), false); //We assume that positions are correct.
223
 
  //Seq comments:
224
 
  unsigned int nbSeq = sc.getNumberOfSequences();
 
237
  // Now try to add each site:
 
238
  for (size_t i = 0; i < sc.getNumberOfSites(); i++)
 
239
  {
 
240
    addSite(sc.getSite(i), false); // We assume that positions are correct.
 
241
  }
 
242
  // Seq comments:
 
243
  size_t nbSeq = sc.getNumberOfSequences();
225
244
  comments_.resize(nbSeq);
226
 
  for (unsigned int i = 0; i < nbSeq; i++)
 
245
  for (size_t i = 0; i < nbSeq; i++)
 
246
  {
227
247
    comments_[i] = new Comments(sc.getComments(i));
 
248
  }
228
249
  sequences_.resize(nbSeq);
229
250
 
230
251
  return *this;
237
258
  clear();
238
259
  AbstractSequenceContainer::operator=(osc);
239
260
 
240
 
  unsigned int nbSeq = osc.getNumberOfSequences();
241
 
  for (unsigned int i = 0; i < nbSeq; i++)
 
261
  size_t nbSeq = osc.getNumberOfSequences();
 
262
  for (size_t i = 0; i < nbSeq; i++)
 
263
  {
242
264
    addSequence(osc.getSequence(i), false);
 
265
  }
243
266
  reindexSites();
244
267
 
245
268
  return *this;
253
276
  AbstractSequenceContainer::operator=(sc);
254
277
 
255
278
  vector<string> names = sc.getSequencesNames();
256
 
  for (unsigned int i = 0 ; i < names.size() ; i++)
 
279
  for (size_t i = 0; i < names.size(); i++)
 
280
  {
257
281
    addSequence(sc.getSequence(names[i]), false);
 
282
  }
258
283
  reindexSites();
259
284
 
260
285
  return *this;
262
287
 
263
288
/******************************************************************************/
264
289
 
265
 
const Site& VectorSiteContainer::getSite(unsigned int i) const throw (IndexOutOfBoundsException)
 
290
const Site& VectorSiteContainer::getSite(size_t i) const throw (IndexOutOfBoundsException)
266
291
{
267
292
  if (i >= getNumberOfSites())
268
293
    throw IndexOutOfBoundsException("VectorSiteContainer::getSite.", i, 0, getNumberOfSites() - 1);
271
296
 
272
297
/******************************************************************************/
273
298
 
274
 
void VectorSiteContainer::setSite(unsigned int pos, const Site& site, bool checkPositions) throw (Exception)
 
299
void VectorSiteContainer::setSite(size_t pos, const Site& site, bool checkPositions) throw (Exception)
275
300
{
276
 
  if (pos >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::setSite.", pos, 0, getNumberOfSites() - 1);
 
301
  if (pos >= getNumberOfSites())
 
302
    throw IndexOutOfBoundsException("VectorSiteContainer::setSite.", pos, 0, getNumberOfSites() - 1);
277
303
 
278
 
    // Check size:
279
 
  if (site.size() != getNumberOfSequences()) throw SiteException("AlignedSequenceContainer::addSite. Site does not have the appropriate length", &site);
 
304
  // Check size:
 
305
  if (site.size() != getNumberOfSequences())
 
306
    throw SiteException("AlignedSequenceContainer::addSite. Site does not have the appropriate length", &site);
280
307
 
281
308
  // New site's alphabet and site container's alphabet matching verification
282
309
  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
287
314
  {
288
315
    int position = site.getPosition();
289
316
    // For all positions in vector : throw exception if position already exists
290
 
    for (unsigned int i = 0; i < sites_.size(); i++)
 
317
    for (size_t i = 0; i < sites_.size(); i++)
291
318
    {
292
 
      if (sites_[i]->getPosition() == position) throw SiteException("VectorSiteContainer::setSite: Site position already exists in container", &site);
 
319
      if (sites_[i]->getPosition() == position)
 
320
        throw SiteException("VectorSiteContainer::setSite: Site position already exists in container", &site);
293
321
    }
294
322
  }
295
 
    delete sites_[pos];
296
 
    sites_[pos] = dynamic_cast<Site *>(site.clone());
 
323
  delete sites_[pos];
 
324
  sites_[pos] = dynamic_cast<Site*>(site.clone());
297
325
}
298
326
 
299
327
/******************************************************************************/
300
328
 
301
 
Site* VectorSiteContainer::removeSite(unsigned int i) throw (IndexOutOfBoundsException)
 
329
Site* VectorSiteContainer::removeSite(size_t i) throw (IndexOutOfBoundsException)
302
330
{
303
 
  if (i >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::removeSite.", i, 0, getNumberOfSites() - 1);
 
331
  if (i >= getNumberOfSites())
 
332
    throw IndexOutOfBoundsException("VectorSiteContainer::removeSite.", i, 0, getNumberOfSites() - 1);
304
333
  Site* site = sites_[i];
305
334
  sites_.erase(sites_.begin() + i);
306
335
  return site;
308
337
 
309
338
/******************************************************************************/
310
339
 
311
 
void VectorSiteContainer::deleteSite(unsigned int i) throw (IndexOutOfBoundsException)
 
340
void VectorSiteContainer::deleteSite(size_t i) throw (IndexOutOfBoundsException)
312
341
{
313
342
  if (i >= getNumberOfSites())
314
343
    throw IndexOutOfBoundsException("VectorSiteContainer::deleteSite.", i, 0, getNumberOfSites() - 1);
315
344
  delete sites_[i];
316
 
  sites_.erase(sites_.begin() + i);  
 
345
  sites_.erase(sites_.begin() + i);
317
346
}
318
347
 
319
348
/******************************************************************************/
320
349
 
321
 
void VectorSiteContainer::deleteSites(unsigned int siteIndex, unsigned int length) throw (IndexOutOfBoundsException)
 
350
void VectorSiteContainer::deleteSites(size_t siteIndex, size_t length) throw (IndexOutOfBoundsException)
322
351
{
323
352
  if (siteIndex + length > getNumberOfSites())
324
353
    throw IndexOutOfBoundsException("VectorSiteContainer::deleteSites.", siteIndex + length, 0, getNumberOfSites() - 1);
325
 
  for (unsigned int i = siteIndex; i < siteIndex + length; ++i)
 
354
  for (size_t i = siteIndex; i < siteIndex + length; ++i)
 
355
  {
326
356
    delete sites_[i];
327
 
  sites_.erase(sites_.begin() + siteIndex, sites_.begin() + siteIndex + length);  
 
357
  }
 
358
  sites_.erase(sites_.begin() + siteIndex, sites_.begin() + siteIndex + length);
328
359
}
329
360
 
330
361
/******************************************************************************/
332
363
void VectorSiteContainer::addSite(const Site& site, bool checkPositions) throw (Exception)
333
364
{
334
365
  // Check size:
335
 
  if (site.size() != getNumberOfSequences()) throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
 
366
  if (site.size() != getNumberOfSequences())
 
367
    throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
336
368
 
337
369
  // New site's alphabet and site container's alphabet matching verification
338
370
  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
340
372
    throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), site.getAlphabet());
341
373
  }
342
374
 
343
 
    // Check position:
 
375
  // Check position:
344
376
  if (checkPositions)
345
377
  {
346
378
    int position = site.getPosition();
347
379
    // For all positions in vector : throw exception if position already exists
348
 
    for (unsigned int i = 0; i < sites_.size(); i++)
 
380
    for (size_t i = 0; i < sites_.size(); i++)
349
381
    {
350
 
      if (sites_[i]->getPosition() == position) throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
 
382
      if (sites_[i]->getPosition() == position)
 
383
        throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
351
384
    }
352
385
  }
353
 
  
354
 
  sites_.push_back(dynamic_cast<Site *>(site.clone()));  
 
386
 
 
387
  sites_.push_back(dynamic_cast<Site*>(site.clone()));
355
388
}
356
389
 
357
390
/******************************************************************************/
359
392
void VectorSiteContainer::addSite(const Site& site, int position, bool checkPositions) throw (Exception)
360
393
{
361
394
  // Check size:
362
 
  if (site.size() != getNumberOfSequences()) throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
 
395
  if (site.size() != getNumberOfSequences())
 
396
    throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
363
397
 
364
398
  // New site's alphabet and site container's alphabet matching verification
365
399
  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
371
405
  if (checkPositions)
372
406
  {
373
407
    // For all positions in vector : throw exception if position already exists
374
 
    for (unsigned int i = 0; i < sites_.size(); i++)
 
408
    for (size_t i = 0; i < sites_.size(); i++)
375
409
    {
376
 
      if (sites_[i]->getPosition() == position) throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
 
410
      if (sites_[i]->getPosition() == position)
 
411
        throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
377
412
    }
378
413
  }
379
414
  Site* copy = dynamic_cast<Site*>(site.clone());
380
415
  copy->setPosition(position);
381
 
  sites_.push_back(copy);  
 
416
  sites_.push_back(copy);
382
417
}
383
418
 
384
419
/******************************************************************************/
385
420
 
386
 
void VectorSiteContainer::addSite(const Site& site, unsigned int siteIndex, bool checkPositions) throw (Exception)
 
421
void VectorSiteContainer::addSite(const Site& site, size_t siteIndex, bool checkPositions) throw (Exception)
387
422
{
388
 
  if (siteIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
 
423
  if (siteIndex >= getNumberOfSites())
 
424
    throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
389
425
 
390
426
  // Check size:
391
 
  if (site.size() != getNumberOfSequences()) throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
 
427
  if (site.size() != getNumberOfSequences())
 
428
    throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
392
429
 
393
430
  // New site's alphabet and site container's alphabet matching verification
394
431
  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
401
438
  {
402
439
    int position = site.getPosition();
403
440
    // For all positions in vector : throw exception if position already exists
404
 
    for (unsigned int i = 0; i < sites_.size(); i++)
 
441
    for (size_t i = 0; i < sites_.size(); i++)
405
442
    {
406
 
      if (sites_[i]->getPosition() == position) throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
 
443
      if (sites_[i]->getPosition() == position)
 
444
        throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
407
445
    }
408
446
  }
409
447
 
410
 
  //insert(begin() + pos, new Site(site));
 
448
  // insert(begin() + pos, new Site(site));
411
449
  sites_.insert(sites_.begin() + siteIndex, dynamic_cast<Site*>(site.clone()));
412
450
}
413
451
 
414
452
/******************************************************************************/
415
453
 
416
 
void VectorSiteContainer::addSite(const Site& site, unsigned int siteIndex, int position, bool checkPositions) throw (Exception)
 
454
void VectorSiteContainer::addSite(const Site& site, size_t siteIndex, int position, bool checkPositions) throw (Exception)
417
455
{
418
 
  if (siteIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
 
456
  if (siteIndex >= getNumberOfSites())
 
457
    throw IndexOutOfBoundsException("VectorSiteContainer::addSite", siteIndex, 0, getNumberOfSites() - 1);
419
458
 
420
459
  // Check size:
421
 
  if (site.size() != getNumberOfSequences()) throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
 
460
  if (site.size() != getNumberOfSequences())
 
461
    throw SiteException("VectorSiteContainer::addSite. Site does not have the appropriate length", &site);
422
462
 
423
463
  // New site's alphabet and site container's alphabet matching verification
424
464
  if (site.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
430
470
  if (checkPositions)
431
471
  {
432
472
    // For all positions in vector : throw exception if position already exists
433
 
    for (unsigned int i = 0; i < sites_.size(); i++)
 
473
    for (size_t i = 0; i < sites_.size(); i++)
434
474
    {
435
 
      if (sites_[i]->getPosition() == position) throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
 
475
      if (sites_[i]->getPosition() == position)
 
476
        throw SiteException("VectorSiteContainer::addSite. Site position already exists in container", &site);
436
477
    }
437
478
  }
438
479
 
439
 
  Site* copy = dynamic_cast<Site *>(site.clone());
 
480
  Site* copy = dynamic_cast<Site*>(site.clone());
440
481
  copy->setPosition(position);
441
482
  sites_.insert(sites_.begin() + siteIndex, copy);
442
483
}
443
484
 
444
485
/******************************************************************************/
445
486
 
446
 
unsigned int VectorSiteContainer::getNumberOfSites() const
 
487
size_t VectorSiteContainer::getNumberOfSites() const
447
488
{
448
489
  return sites_.size();
449
490
}
452
493
 
453
494
void VectorSiteContainer::reindexSites()
454
495
{
455
 
  int pos = 1; //first position is 1.
456
 
  for (vector<Site *>::iterator i = sites_.begin(); i < sites_.end(); i++)
 
496
  int pos = 1; // first position is 1.
 
497
  for (vector<Site*>::iterator i = sites_.begin(); i < sites_.end(); i++)
457
498
  {
458
499
    (*i)->setPosition(pos++);
459
 
  }  
 
500
  }
460
501
}
461
502
 
462
503
/******************************************************************************/
464
505
Vint VectorSiteContainer::getSitePositions() const
465
506
{
466
507
  Vint positions(sites_.size());
467
 
  for (unsigned int i = 0; i < sites_.size(); i++)
 
508
  for (size_t i = 0; i < sites_.size(); i++)
 
509
  {
468
510
    positions[i] = sites_[i]->getPosition();
 
511
  }
469
512
  return positions;
470
513
}
471
514
 
472
515
/******************************************************************************/
473
516
 
474
 
const Sequence& VectorSiteContainer::getSequence(unsigned int i) const throw (IndexOutOfBoundsException)
 
517
const Sequence& VectorSiteContainer::getSequence(size_t i) const throw (IndexOutOfBoundsException)
475
518
{
476
 
  if (i >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
 
519
  if (i >= getNumberOfSequences())
 
520
    throw IndexOutOfBoundsException("VectorSiteContainer::getSequence.", i, 0, getNumberOfSequences() - 1);
477
521
 
478
 
        // Main loop : for all sites
479
 
        unsigned int n = getNumberOfSites();
480
 
        vector<int> sequence(n);
481
 
  for (unsigned int j = 0; j < n; j++)
 
522
  // Main loop : for all sites
 
523
  size_t n = getNumberOfSites();
 
524
  vector<int> sequence(n);
 
525
  for (size_t j = 0; j < n; j++)
482
526
  {
483
527
    sequence[j] = sites_[j]->getContent()[i];
484
528
  }
485
 
  if (sequences_[i]) delete sequences_[i];
 
529
  if (sequences_[i])
 
530
    delete sequences_[i];
486
531
  sequences_[i] = new BasicSequence(names_[i], sequence, *comments_[i], getAlphabet());
487
532
  return *sequences_[i];
488
533
}
491
536
 
492
537
const Sequence& VectorSiteContainer::getSequence(const string& name) const throw (SequenceNotFoundException)
493
538
{
494
 
  //Look for sequence name:
495
 
  unsigned int pos = getSequencePosition(name);
 
539
  // Look for sequence name:
 
540
  size_t pos = getSequencePosition(name);
496
541
  return getSequence(pos);
497
542
}
498
543
 
500
545
 
501
546
bool VectorSiteContainer::hasSequence(const string& name) const
502
547
{
503
 
  //Look for sequence name:
504
 
  for (unsigned int pos = 0; pos < names_.size(); pos++) {
505
 
    if (names_[pos] == name) return true;
 
548
  // Look for sequence name:
 
549
  for (size_t pos = 0; pos < names_.size(); pos++)
 
550
  {
 
551
    if (names_[pos] == name)
 
552
      return true;
506
553
  }
507
554
  return false;
508
555
}
509
556
 
510
557
/******************************************************************************/
511
558
 
512
 
unsigned int VectorSiteContainer::getSequencePosition(const string& name) const throw (SequenceNotFoundException)
 
559
size_t VectorSiteContainer::getSequencePosition(const string& name) const throw (SequenceNotFoundException)
513
560
{
514
 
  //Look for sequence name:
515
 
  for (unsigned int pos = 0; pos < names_.size(); pos++) {
516
 
    if (names_[pos] == name) return pos;
 
561
  // Look for sequence name:
 
562
  for (size_t pos = 0; pos < names_.size(); pos++)
 
563
  {
 
564
    if (names_[pos] == name)
 
565
      return pos;
517
566
  }
518
567
  throw SequenceNotFoundException("VectorSiteContainer::getSequencePosition().", name);
519
568
}
522
571
 
523
572
void VectorSiteContainer::setSequence(const string& name, const Sequence& sequence, bool checkNames) throw (Exception)
524
573
{
525
 
  //Look for sequence name:
526
 
  unsigned int pos = getSequencePosition(name);
 
574
  // Look for sequence name:
 
575
  size_t pos = getSequencePosition(name);
527
576
  setSequence(pos, sequence, checkNames);
528
577
}
529
578
 
530
579
/******************************************************************************/
531
580
 
532
 
void VectorSiteContainer::setSequence(unsigned int pos, const Sequence& sequence, bool checkNames)
533
 
    throw (Exception)
 
581
void VectorSiteContainer::setSequence(size_t pos, const Sequence& sequence, bool checkNames)
 
582
throw (Exception)
534
583
{
535
584
  if (pos >= getNumberOfSequences())
536
 
    throw BadIntegerException("VectorSiteContainer::setSequence", pos);
 
585
    throw IndexOutOfBoundsException("VectorSiteContainer::setSequence", pos, 0, getNumberOfSequences() - 1);
537
586
 
538
587
  // New sequence's alphabet and site container's alphabet matching verification
539
588
  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
540
589
    throw AlphabetMismatchException("VectorSiteContainer::addSite", getAlphabet(), sequence.getAlphabet());
541
590
 
542
 
  //If the container has only one sequence, we set the size to the size of this sequence:
543
 
  if (getNumberOfSequences() == 1) realloc(sequence.size());
 
591
  // If the container has only one sequence, we set the size to the size of this sequence:
 
592
  if (getNumberOfSequences() == 1)
 
593
    realloc(sequence.size());
544
594
 
545
595
  if (sequence.size() != sites_.size())
546
596
    throw SequenceException("VectorSiteContainer::setSequence. Sequence has not the appropriate length.", &sequence);
547
597
 
548
598
  if (checkNames)
549
599
  {
550
 
    for (unsigned int i = 0; i < names_.size(); i++)
 
600
    for (size_t i = 0; i < names_.size(); i++)
 
601
    {
551
602
      if (i != pos && sequence.getName() == names_[i])
552
603
        throw SequenceException("VectorSiteContainer::settSequence. Name already exists in container.", &sequence);
 
604
    }
553
605
  }
554
 
  //Update name:
 
606
  // Update name:
555
607
  names_[pos] = sequence.getName();
556
 
  //Update elements at each site:
557
 
  for (unsigned int i = 0; i < sites_.size(); i++)
 
608
  // Update elements at each site:
 
609
  for (size_t i = 0; i < sites_.size(); i++)
 
610
  {
558
611
    sites_[i]->setElement(pos, sequence.getValue(i));
559
 
  //Update comments:
560
 
  if (comments_[pos]) delete comments_[pos];
 
612
  }
 
613
  // Update comments:
 
614
  if (comments_[pos])
 
615
    delete comments_[pos];
561
616
  comments_[pos] = new Comments(sequence.getComments());
562
 
  //Update sequences:
563
 
  if (sequences_[pos]) delete sequences_[pos];
 
617
  // Update sequences:
 
618
  if (sequences_[pos])
 
619
    delete sequences_[pos];
564
620
  sequences_[pos] = 0;
565
621
}
566
622
 
567
623
/******************************************************************************/
568
624
 
569
 
Sequence * VectorSiteContainer::removeSequence(unsigned int i) throw (IndexOutOfBoundsException)
 
625
Sequence* VectorSiteContainer::removeSequence(size_t i) throw (IndexOutOfBoundsException)
570
626
{
571
627
  if (i >= getNumberOfSequences())
572
628
    throw IndexOutOfBoundsException("VectorSiteContainer::removeSequence.", i, 0, getNumberOfSequences() - 1);
573
629
 
574
 
  getSequence(i); //Actuallizes pointer.
 
630
  getSequence(i); // Actuallizes pointer.
575
631
  Sequence* sequence = sequences_[i];
576
 
  for (unsigned int j = 0; j < sites_.size(); j++)
 
632
  for (size_t j = 0; j < sites_.size(); j++)
577
633
  {
578
 
    //For each site:
 
634
    // For each site:
579
635
    sites_[j]->deleteElement(i);
580
636
  }
581
 
  
582
 
  //Now actualize names and comments:
 
637
 
 
638
  // Now actualize names and comments:
583
639
  names_.erase(names_.begin() + i);
584
 
  if (comments_[i]) delete comments_[i];
 
640
  if (comments_[i])
 
641
    delete comments_[i];
585
642
  comments_.erase(comments_.begin() + i);
586
643
  // We remove the sequence, so the destruction of the sequence is up to the user:
587
 
  //if (sequences_[i] != 0) delete sequences_[i];
 
644
  // if (sequences_[i] != 0) delete sequences_[i];
588
645
  sequences_.erase(sequences_.begin() + i);
589
646
  return sequence;
590
647
}
591
648
 
592
649
/******************************************************************************/
593
650
 
594
 
Sequence * VectorSiteContainer::removeSequence(const string& name) throw (SequenceNotFoundException)
 
651
Sequence* VectorSiteContainer::removeSequence(const string& name) throw (SequenceNotFoundException)
595
652
{
596
 
  //Look for sequence name:
597
 
  unsigned int pos = getSequencePosition(name);
 
653
  // Look for sequence name:
 
654
  size_t pos = getSequencePosition(name);
598
655
  return removeSequence(pos);
599
656
}
600
657
 
601
658
/******************************************************************************/
602
659
 
603
 
void VectorSiteContainer::deleteSequence(unsigned int i) throw (IndexOutOfBoundsException)
 
660
void VectorSiteContainer::deleteSequence(size_t i) throw (IndexOutOfBoundsException)
604
661
{
605
662
  if (i >= getNumberOfSequences())
606
663
    throw IndexOutOfBoundsException("VectorSiteContainer::demeteSequence.", i, 0, getNumberOfSequences() - 1);
607
 
  for (unsigned int j = 0; j < sites_.size(); j++)
 
664
  for (size_t j = 0; j < sites_.size(); j++)
 
665
  {
608
666
    sites_[j]->deleteElement(i);
 
667
  }
609
668
 
610
 
  //Now actualize names and comments:
 
669
  // Now actualize names and comments:
611
670
  names_.erase(names_.begin() + i);
612
 
  if (comments_[i]) delete comments_[i];
 
671
  if (comments_[i])
 
672
    delete comments_[i];
613
673
  comments_.erase(comments_.begin() + i);
614
 
  if (sequences_[i]) delete sequences_[i];
 
674
  if (sequences_[i])
 
675
    delete sequences_[i];
615
676
  sequences_.erase(sequences_.begin() + i);
616
677
}
617
678
 
619
680
 
620
681
void VectorSiteContainer::deleteSequence(const string& name) throw (SequenceNotFoundException)
621
682
{
622
 
  //Look for sequence name:
623
 
  unsigned int pos = getSequencePosition(name);
624
 
  deleteSequence(pos);  
 
683
  // Look for sequence name:
 
684
  size_t pos = getSequencePosition(name);
 
685
  deleteSequence(pos);
625
686
}
626
687
 
627
688
/******************************************************************************/
628
689
 
629
690
void VectorSiteContainer::addSequence(const Sequence& sequence, bool checkNames) throw (Exception)
630
691
{
631
 
  //If the container has no sequence, we set the size to the size of this sequence:
632
 
  if (getNumberOfSequences() == 0) realloc(sequence.size());
633
 
    
 
692
  // If the container has no sequence, we set the size to the size of this sequence:
 
693
  if (getNumberOfSequences() == 0)
 
694
    realloc(sequence.size());
 
695
 
634
696
  // New sequence's alphabet and site container's alphabet matching verification
635
697
  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
636
698
    throw AlphabetMismatchException("VectorSiteContainer::addSequence", getAlphabet(), sequence.getAlphabet());
638
700
  if (sequence.size() != sites_.size())
639
701
    throw SequenceException("VectorSiteContainer::addSequence. Sequence has not the appropriate length: " + TextTools::toString(sequence.size()) + ", should be " + TextTools::toString(sites_.size()) + ".", &sequence);
640
702
 
641
 
  if (checkNames) {
642
 
    for (unsigned int i = 0; i < names_.size(); i++) {
643
 
      if (sequence.getName() == names_[i]) throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
 
703
  if (checkNames)
 
704
  {
 
705
    for (size_t i = 0; i < names_.size(); i++)
 
706
    {
 
707
      if (sequence.getName() == names_[i])
 
708
        throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
644
709
    }
645
710
  }
646
 
  
647
 
  //Append name:
 
711
 
 
712
  // Append name:
648
713
  names_.push_back(sequence.getName());
649
 
    
650
 
  //Append elements at each site:
651
 
  for (unsigned int i = 0; i < sites_.size(); i++) {
 
714
 
 
715
  // Append elements at each site:
 
716
  for (size_t i = 0; i < sites_.size(); i++)
 
717
  {
652
718
    sites_[i]->addElement(sequence.getValue(i));
653
719
  }
654
 
    
655
 
  //Append comments:
 
720
 
 
721
  // Append comments:
656
722
  comments_.push_back(new Comments(sequence.getComments()));
657
 
    
658
 
  //Sequences pointers:
 
723
 
 
724
  // Sequences pointers:
659
725
  sequences_.push_back(0);
660
726
}
661
727
 
662
728
/******************************************************************************/
663
729
 
664
730
void VectorSiteContainer::addSequence(
665
 
  const Sequence & sequence,
666
 
  unsigned int pos,
 
731
  const Sequence& sequence,
 
732
  size_t pos,
667
733
  bool checkNames)
668
 
  throw (Exception)
 
734
throw (Exception)
669
735
{
670
 
  if (pos >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::addSequence.", pos, 0, getNumberOfSequences() - 1);
671
 
  if (sequence.size() != sites_.size()) throw SequenceNotAlignedException("VectorSiteContainer::setSequence", &sequence);
 
736
  if (pos >= getNumberOfSequences())
 
737
    throw IndexOutOfBoundsException("VectorSiteContainer::addSequence.", pos, 0, getNumberOfSequences() - 1);
 
738
  if (sequence.size() != sites_.size())
 
739
    throw SequenceNotAlignedException("VectorSiteContainer::setSequence", &sequence);
672
740
 
673
741
  // New sequence's alphabet and site container's alphabet matching verification
674
742
  if (sequence.getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
678
746
 
679
747
  if (checkNames)
680
748
  {
681
 
    for (unsigned int i = 0; i < names_.size(); i++)
 
749
    for (size_t i = 0; i < names_.size(); i++)
682
750
    {
683
 
      if (sequence.getName() == names_[i]) throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
 
751
      if (sequence.getName() == names_[i])
 
752
        throw SequenceException("VectorSiteContainer::addSequence. Name already exists in container.", &sequence);
684
753
    }
685
754
  }
686
755
 
687
 
  for (unsigned int i = 0; i < sites_.size(); i++)
 
756
  for (size_t i = 0; i < sites_.size(); i++)
688
757
  {
689
 
    //For each site:
 
758
    // For each site:
690
759
    sites_[i]->addElement(pos, sequence.getValue(i));
691
760
  }
692
 
  //Actualize names and comments:
693
 
  names_.insert(names_.begin() + pos, sequence.getName());  
 
761
  // Actualize names and comments:
 
762
  names_.insert(names_.begin() + pos, sequence.getName());
694
763
  comments_.insert(comments_.begin() + pos, new Comments(sequence.getComments()));
695
764
  sequences_.insert(sequences_.begin() + pos, 0);
696
765
}
699
768
 
700
769
void VectorSiteContainer::clear()
701
770
{
702
 
  //Must delete all sites in the container:
703
 
  for (unsigned int i = 0; i < sites_.size(); i++) delete sites_[i];
704
 
 
705
 
  //must delete all comments too:
706
 
  for (unsigned int i = 0; i < comments_.size(); i++) if (comments_[i] != 0) delete comments_[i];
707
 
 
708
 
  //Delete all sequences retrieved:
709
 
  for (unsigned int i = 0; i < sequences_.size(); i++) if (sequences_[i] != 0) delete(sequences_[i]);
 
771
  // Must delete all sites in the container:
 
772
  for (size_t i = 0; i < sites_.size(); i++)
 
773
  {
 
774
    delete sites_[i];
 
775
  }
 
776
 
 
777
  // must delete all comments too:
 
778
  for (size_t i = 0; i < comments_.size(); i++)
 
779
  {
 
780
    if (comments_[i] != 0)
 
781
      delete comments_[i];
 
782
  }
 
783
 
 
784
  // Delete all sequences retrieved:
 
785
  for (size_t i = 0; i < sequences_.size(); i++)
 
786
  {
 
787
    if (sequences_[i] != 0)
 
788
      delete (sequences_[i]);
 
789
  }
710
790
 
711
791
  // Delete all sites pointers
712
792
  sites_.clear();
717
797
 
718
798
/******************************************************************************/
719
799
 
720
 
void VectorSiteContainer::realloc(unsigned int n)
 
800
void VectorSiteContainer::realloc(size_t n)
721
801
{
722
802
  clear();
723
803
  sites_.resize(n);
724
 
  for (unsigned int i = 0; i < n; i++)
 
804
  for (size_t i = 0; i < n; i++)
 
805
  {
725
806
    sites_[i] = new Site(getAlphabet());
 
807
  }
726
808
  reindexSites();
727
809
}
728
810
 
738
820
void VectorSiteContainer::setSequencesNames(
739
821
  const vector<string>& names,
740
822
  bool checkNames)
741
 
  throw (Exception)
 
823
throw (Exception)
742
824
{
743
825
  if (names.size() != getNumberOfSequences())
744
 
    throw BadIntegerException("VectorSiteContainer::setSequenceNames: bad number of names.", names.size());
 
826
    throw IndexOutOfBoundsException("VectorSiteContainer::setSequenceNames: bad number of names.", names.size(), getNumberOfSequences(), getNumberOfSequences());
745
827
  if (checkNames)
746
828
  {
747
 
    for (unsigned int i = 0; i < names.size(); i++)
 
829
    for (size_t i = 0; i < names.size(); i++)
748
830
    {
749
831
      // For all names in vector : throw exception if name already exists
750
 
      for (unsigned int j = 0; j < i; j++)
 
832
      for (size_t j = 0; j < i; j++)
751
833
      {
752
834
        if (names[j] == names[i])
753
835
          throw Exception("VectorSiteContainer::setSequencesNames : Sequence's name already exists in container");
754
836
      }
755
837
    }
756
838
  }
757
 
  for (unsigned int i = 0; i < names.size(); i++)
 
839
  for (size_t i = 0; i < names.size(); i++)
758
840
  {
759
841
    names_[i] = names[i];
760
842
  }
762
844
 
763
845
/******************************************************************************/
764
846
 
765
 
void VectorSiteContainer::setComments(unsigned int sequenceIndex, const Comments & comments) throw (IndexOutOfBoundsException)
 
847
void VectorSiteContainer::setComments(size_t sequenceIndex, const Comments& comments) throw (IndexOutOfBoundsException)
766
848
{
767
849
  comments_[sequenceIndex] = new Comments(comments);
768
850
}
770
852
/******************************************************************************/
771
853
 
772
854
VectorSiteContainer* VectorSiteContainer::createEmptyContainer() const
773
 
 
855
{
774
856
  VectorSiteContainer* vsc = new VectorSiteContainer(getAlphabet());
775
857
  vsc->setGeneralComments(getGeneralComments());
776
858
  return vsc;