bpp-phyl  2.2.0
PatternTools.cpp
Go to the documentation of this file.
1 //
2 // File: PatternTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Mar 20 13:36:54 2003
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for phylogenetic data 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 #include "PatternTools.h"
41 
42 #include "TreeTemplateTools.h"
43 
44 // From SeqLib:
45 #include <Bpp/Seq/Container/VectorSiteContainer.h>
46 #include <Bpp/Seq/SiteTools.h>
47 
48 using namespace bpp;
49 
50 // From the STL:
51 #include <iostream>
52 #include <map>
53 #include <algorithm>
54 
55 using namespace std;
56 
57 /******************************************************************************/
58 
59 SiteContainer* PatternTools::getSequenceSubset(const SiteContainer& sequenceSet, const Node& node) throw (Exception)
60 {
61  VectorSiteContainer * sequenceSubset = new VectorSiteContainer(sequenceSet.getAlphabet());
62  vector<const Node *> leaves = TreeTemplateTools::getLeaves(node);
63  for (vector<const Node *>::iterator i = leaves.begin(); i < leaves.end(); i++)
64  {
65  const Sequence* newSeq = &sequenceSet.getSequence((* i)->getName());
66  if (newSeq == NULL) throw SequenceNotFoundException("PatternToolsERROR: leaf name not found in sequence file: ", (* i) -> getName());
67  sequenceSubset -> addSequence(* newSeq);
68  }
69  return sequenceSubset;
70 }
71 
72 /******************************************************************************/
73 
74 SiteContainer* PatternTools::getSequenceSubset(const SiteContainer& sequenceSet, const vector<string>& names) throw (Exception)
75 {
76  VectorSiteContainer* sequenceSubset = new VectorSiteContainer(sequenceSet.getAlphabet());
77  for (unsigned int i = 0; i < names.size(); i++)
78  {
79  const Sequence* newSeq = &sequenceSet.getSequence(names[i]);
80  if (!newSeq) throw SequenceNotFoundException("PatternTools ERROR: name not found in sequence file: ", names[i]);
81  sequenceSubset->addSequence(*newSeq);
82  }
83  return sequenceSubset;
84 }
85 
86 /******************************************************************************/
87 
88 SiteContainer* PatternTools::shrinkSiteSet(const SiteContainer& siteSet) throw (Exception)
89 {
90  if (siteSet.getNumberOfSites() == 0) throw Exception("PatternTools::shrinkSiteSet siteSet is void.");
91  vector<const Site *> sites;
92  for (unsigned int i = 0; i < siteSet.getNumberOfSites(); i++)
93  {
94  const Site* currentSite = &siteSet.getSite(i);
95  bool siteExists = false;
96  for (unsigned int j = 0; !siteExists && j < sites.size(); j++)
97  {
98  if (SiteTools::areSitesIdentical(* currentSite, * sites[j])) siteExists = true;
99  }
100  if (!siteExists) sites.push_back(currentSite);
101  }
102  SiteContainer* result = new VectorSiteContainer(sites, siteSet.getAlphabet(), false); //We do not check positions here.
103  result->setSequencesNames(siteSet.getSequencesNames(), false);
104  return result;
105 }
106 
107 /******************************************************************************/
108 
109 Vint PatternTools::getIndexes(const SiteContainer& sequences1, const SiteContainer& sequences2)
110 {
111  size_t nbSites = sequences1.getNumberOfSites();
112  Vint indexes(nbSites);
113  for (size_t i = 0; i < nbSites; i++) {
114  //For each site in sequences1,
115  indexes[i] = -1;
116  const Site* site = &sequences1.getSite(i);
117  for (size_t j = 0; j < sequences2.getNumberOfSites(); j++)
118  {
119  if (SiteTools::areSitesIdentical(*site, sequences2.getSite(j)))
120  {
121  indexes[i] = static_cast<int>(j);
122  break;
123  }
124  }
125  }
126  return indexes;
127 }
128 
129 /******************************************************************************/
130 
STL namespace.
static SiteContainer * shrinkSiteSet(const SiteContainer &sequenceSet)
Compress a site container by removing duplicated sites.
static Vint getIndexes(const SiteContainer &sequences1, const SiteContainer &sequences2)
Look for the occurence of each site in sequences1 in sequences2 and send the position of the first oc...
static std::vector< N * > getLeaves(N &node)
Retrieve all leaves from a subtree.
static SiteContainer * getSequenceSubset(const SiteContainer &sequenceSet, const Node &node)
Extract the sequences corresponding to a given subtree.
The phylogenetic node class.
Definition: Node.h:90