bpp-phyl  2.2.0
SitePatterns.cpp
Go to the documentation of this file.
1 //
2 // File: SitePatterns.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Nov 29 15:37 2005
5 // from file PatternTools.cpp
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #include "SitePatterns.h"
42 
43 // From the SeqLib library:
44 #include <Bpp/Seq/SiteTools.h>
45 #include <Bpp/Seq/Container/VectorSiteContainer.h>
46 
47 using namespace bpp;
48 using namespace std;
49 
50 /******************************************************************************/
51 
52 SitePatterns::SitePatterns(const SiteContainer* sequences, bool own) :
53  names_(sequences->getSequencesNames()),
54  sites_(),
55  weights_(),
56  indices_(),
57  sequences_(sequences),
58  alpha_(sequences->getAlphabet()),
59  own_(own)
60 {
61  size_t nbSites = sequences->getNumberOfSites();
62  vector<SortableSite> ss(nbSites);
63  for (size_t i = 0; i < nbSites; i++)
64  {
65  const Site* currentSite = &sequences->getSite(i);
66  SortableSite* ssi = &ss[i];
67  ssi->siteS = currentSite->toString();
68  ssi->siteP = currentSite;
69  ssi->originalPosition = i;
70  }
71 
72  if (nbSites > 0)
73  {
74  // Quick sort according to site contents:
75  sort(ss.begin(), ss.end());
76 
77  // Now build patterns:
78 
79  SortableSite* ss0 = &ss[0];
80  const Site* previousSite = ss0->siteP;
81  indices_.resize(nbSites);
82  indices_[ss0->originalPosition] = 0;
83  sites_.push_back(previousSite);
84  weights_.push_back(1);
85 
86  unsigned int currentPos = 0;
87  for (unsigned int i = 1; i < nbSites; i++)
88  {
89  SortableSite* ssi = &ss[i];
90  const Site* currentSite = ssi->siteP;
91  bool siteExists = SiteTools::areSitesIdentical(*currentSite, *previousSite);
92  if (siteExists)
93  {
94  weights_[currentPos]++;
95  }
96  else
97  {
98  sites_.push_back(currentSite);
99  weights_.push_back(1);
100  currentPos++;
101  }
102  indices_[ssi->originalPosition] = currentPos;
103  previousSite = currentSite;
104  }
105  }
106 }
107 
108 /******************************************************************************/
109 
110 SiteContainer* SitePatterns::getSites() const
111 {
112  SiteContainer* sites = new VectorSiteContainer(sites_, alpha_);
113  sites->setSequencesNames(names_, false);
114  return sites;
115 }
116 
117 /******************************************************************************/
118 
SitePatterns(const SiteContainer *sequences, bool own=false)
Build a new SitePattern object.
std::vector< const Site * > sites_
Definition: SitePatterns.h:110
std::vector< size_t > indices_
Definition: SitePatterns.h:112
STL namespace.
std::vector< std::string > names_
Class used for site pattern sorting.
Definition: SitePatterns.h:109
std::vector< unsigned int > weights_
Definition: SitePatterns.h:111
Class used for site pattern sorting.
Definition: SitePatterns.h:76
SiteContainer * getSites() const
const Alphabet * alpha_
Definition: SitePatterns.h:114