bpp-phyl  2.2.0
MvaFrequenciesSet.cpp
Go to the documentation of this file.
1 //
2 // File: MvaFrequenciesSet.cpp
3 // Created by: Mathieu Groussin
4 // Created on: Sat Jan 12 2013
5 //
6 
7 /*
8  Copyright or (c) or Copr. Bio++ Development Team, (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 #include "MvaFrequenciesSet.h"
40 
41 using namespace bpp;
42 
43 #include <cmath>
44 using namespace std;
45 
46 MvaFrequenciesSet::MvaFrequenciesSet(const ProteicAlphabet* alpha) :
47  AbstractFrequenciesSet(new CanonicalStateMap(alpha, false), "MVA.", "MVAprotein"),
48  tPpalAxes_(),
49  rowCoords_(),
50  nbrOfAxes_(0),
51  model_(),
52  columnWeights_(),
53  paramValues_()
54 {}
55 
57 {
58  setNbrOfAxes(coala->getNbrOfAxes());
64 }
65 
67 {
68  for (unsigned int i = 0; i < nbrOfAxes_; i++)
69  {
70  const vector<double> rCoords = rowCoords_.col(i);
71  double maxCoord = VectorTools::max(rCoords);
72  double minCoord = VectorTools::min(rCoords);
73  double sd = VectorTools::sd<double, double>(rCoords);
74  IntervalConstraint* constraint = new IntervalConstraint(minCoord - sd, maxCoord + sd, true, true);
75  if (paramValues_.find("RootAxPos" + TextTools::toString(i)) != paramValues_.end())
76  addParameter_(new Parameter("MVA.RootAxPos" + TextTools::toString(i), TextTools::toDouble(paramValues_["RootAxPos" + TextTools::toString(i)].substr(0, 8)), constraint));
77  else
78  addParameter_(new Parameter("MVA.RootAxPos" + TextTools::toString(i), 0., constraint));
79  }
80 }
81 
82 void MvaFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
83 {
85 }
86 
87 void MvaFrequenciesSet::updateFrequencies() throw (Exception)
88 {
89  if (nbrOfAxes_ == 0)
90  throw Exception("The number of axes kept by the MVA analysis was not set. You should initialize it with the setNbrOfAxes function");
91  vector<double> positions;
92 
93  for (unsigned int i = 0; i < nbrOfAxes_; i++)
94  {
95  positions.push_back(getParameter("RootAxPos" + TextTools::toString(i)).getValue());
96  }
97 
98  vector<double> tmpFreqs(20, 0.0);
99  vector<double> freqs(20, 0.0);
100 
101  computeReverseCOA(positions, tmpFreqs);
102  computeCoordsFirstSpaceCOA(tmpFreqs, freqs);
103 
104  setFrequencies_(freqs);
105 
106  bool norm = false;
107  for (unsigned int i = 0; i < 20; i++)
108  {
109  if (getFreq_(i) < 0.001)
110  {
111  norm = true;
112  getFreq_(i) = 0.001;
113  }
114  if (getFreq_(i) > 0.5)
115  {
116  norm = true;
117  getFreq_(i) = 0.5;
118  }
119  }
120  if (norm == true)
121  {
122  double s = VectorTools::sum(getFrequencies());
123  for (size_t i = 0; i < 20; ++i)
124  {
125  getFreq_(i) = getFreq_(i) / s;
126  }
127  }
128 }
129 
130 void MvaFrequenciesSet::setFrequencies(const vector<double>& frequencies) throw (DimensionException, Exception)
131 {}
132 
133 void MvaFrequenciesSet::computeReverseCOA(const std::vector<double>& positions, std::vector<double>& tmpFreqs) throw (Exception)
134 {
135  for (unsigned int i = 0; i < 20; i++)
136  {
137  for (unsigned int j = 0; j < nbrOfAxes_; j++)
138  {
139  tmpFreqs[i] = tmpFreqs[i] + tPpalAxes_(j, i) * positions[j];
140  }
141  }
142 }
143 
144 void MvaFrequenciesSet::computeCoordsFirstSpaceCOA(std::vector<double>& tmpFreqs, std::vector<double>& freqs) throw (Exception)
145 {
146  if (freqs.size() != tmpFreqs.size())
147  throw Exception("MvaFrequenciesSet::computeCoordsFirstSpaceCOA : error in the size of the vectors");
148  // The vector of amino acid frequencies is calculated from the original column weights
149  for (unsigned int i = 0; i < tmpFreqs.size(); i++)
150  {
151  freqs[i] = (tmpFreqs[i] + 1) * columnWeights_[i];
152  }
153 }
void fireParameterChanged(const ParameterList &parameters)
size_t getNbrOfAxes() const
Definition: CoalaCore.h:85
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
void setVectorOfColumnWeights(const std::vector< double > &cw)
RowMatrix< double > rowCoords_
void setFrequencies_(const std::vector< double > &frequencies)
void computeReverseCOA(const std::vector< double > &positions, std::vector< double > &tmpFreqs)
STL namespace.
void setNbrOfAxes(const size_t &nAxes)
MvaFrequenciesSet(const ProteicAlphabet *alpha)
Constructor.
void computeCoordsFirstSpaceCOA(std::vector< double > &tmpFreqs, std::vector< double > &freqs)
const std::vector< double > & getColumnWeights() const
Definition: CoalaCore.h:88
std::map< std::string, std::string > paramValues_
double & getFreq_(size_t i)
void initSet(CoalaCore *coala)
const RowMatrix< double > & getTppalAxesMatrix() const
Definition: CoalaCore.h:86
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
const std::vector< double > getFrequencies() const
void setTransposeMatrixOfPpalAxes(const RowMatrix< double > &matrix)
void setMatrixOfRowCoords(const RowMatrix< double > &matrix)
This class is the core class inherited by the Coala class. COaLA is a branch-heterogeneous amino-acid...
Definition: CoalaCore.h:66
Basic implementation of the FrequenciesSet interface.
const RowMatrix< double > & getRowCoordinates() const
Definition: CoalaCore.h:87