bpp-phyl3  3.0.0
MvaFrequencySet.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "MvaFrequencySet.h"
6 
7 using namespace bpp;
8 
9 #include <cmath>
10 using namespace std;
11 
12 MvaFrequencySet::MvaFrequencySet(shared_ptr<const ProteicAlphabet> alpha) :
14  make_shared<CanonicalStateMap>(alpha, false),
15  "MVA.",
16  "MVAprotein"),
17  tPpalAxes_(),
18  rowCoords_(),
19  nbrOfAxes_(0),
20  model_(),
21  columnWeights_(),
22  paramValues_()
23 {}
24 
26 {
27  setNbrOfAxes(coala.getNbrOfAxes());
33 }
34 
36 {
37  for (unsigned int i = 0; i < nbrOfAxes_; i++)
38  {
39  const vector<double> rCoords = rowCoords_.col(i);
40  double maxCoord = VectorTools::max(rCoords);
41  double minCoord = VectorTools::min(rCoords);
42  double sd = VectorTools::sd<double, double>(rCoords);
43  auto constraint = std::make_shared<IntervalConstraint>(minCoord - sd, maxCoord + sd, true, true);
44  if (paramValues_.find("RootAxPos" + TextTools::toString(i)) != paramValues_.end())
45  addParameter_(new Parameter(getNamespace() + "RootAxPos" + TextTools::toString(i), TextTools::toDouble(paramValues_["RootAxPos" + TextTools::toString(i)].substr(0, 8)), constraint));
46  else
47  addParameter_(new Parameter(getNamespace() + "RootAxPos" + TextTools::toString(i), 0., constraint));
48  }
49 }
50 
52 {
54 }
55 
57 {
58  if (nbrOfAxes_ == 0)
59  throw Exception("The number of axes kept by the MVA analysis was not set. You should initialize it with the setNbrOfAxes function");
60  vector<double> positions;
61 
62  for (unsigned int i = 0; i < nbrOfAxes_; i++)
63  {
64  positions.push_back(getParameterValue("RootAxPos" + TextTools::toString(i)));
65  }
66 
67  vector<double> tmpFreqs(20, 0.0);
68  vector<double> freqs(20, 0.0);
69 
70  computeReverseCOA(positions, tmpFreqs);
71  computeCoordsFirstSpaceCOA(tmpFreqs, freqs);
72 
73  setFrequencies_(freqs);
74 
75  bool norm = false;
76  for (unsigned int i = 0; i < 20; i++)
77  {
78  if (getFreq_(i) < 0.001)
79  {
80  norm = true;
81  getFreq_(i) = 0.001;
82  }
83  if (getFreq_(i) > 0.5)
84  {
85  norm = true;
86  getFreq_(i) = 0.5;
87  }
88  }
89  if (norm == true)
90  {
91  double s = VectorTools::sum(getFrequencies());
92  for (size_t i = 0; i < 20; ++i)
93  {
94  getFreq_(i) = getFreq_(i) / s;
95  }
96  }
97 }
98 
99 void MvaFrequencySet::setFrequencies(const vector<double>& frequencies)
100 {}
101 
102 void MvaFrequencySet::computeReverseCOA(const std::vector<double>& positions, std::vector<double>& tmpFreqs)
103 {
104  for (unsigned int i = 0; i < 20; i++)
105  {
106  for (unsigned int j = 0; j < nbrOfAxes_; j++)
107  {
108  tmpFreqs[i] = tmpFreqs[i] + tPpalAxes_(j, i) * positions[j];
109  }
110  }
111 }
112 
113 void MvaFrequencySet::computeCoordsFirstSpaceCOA(std::vector<double>& tmpFreqs, std::vector<double>& freqs)
114 {
115  if (freqs.size() != tmpFreqs.size())
116  throw Exception("MvaFrequencySet::computeCoordsFirstSpaceCOA : error in the size of the vectors");
117  // The vector of amino acid frequencies is calculated from the original column weights
118  for (unsigned int i = 0; i < tmpFreqs.size(); i++)
119  {
120  freqs[i] = (tmpFreqs[i] + 1) * columnWeights_[i];
121  }
122 }
Basic implementation of the FrequencySet interface.
Definition: FrequencySet.h:102
void setFrequencies_(const std::vector< double > &frequencies)
Definition: FrequencySet.h:181
double & getFreq_(size_t i)
Definition: FrequencySet.h:179
const Vdouble & getFrequencies() const override
Definition: FrequencySet.h:148
void addParameter_(Parameter *parameter)
std::string getNamespace() const override
double getParameterValue(const std::string &name) const override
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
This class is the core class inherited by the Coala class. COaLA is a branch-heterogeneous amino-acid...
Definition: CoalaCore.h:32
const std::vector< double > & getColumnWeights() const
Definition: CoalaCore.h:52
const RowMatrix< double > & getTppalAxesMatrix() const
Definition: CoalaCore.h:50
size_t getNbrOfAxes() const
Definition: CoalaCore.h:49
const RowMatrix< double > & getRowCoordinates() const
Definition: CoalaCore.h:51
RowMatrix< double > tPpalAxes_
void setMatrixOfRowCoords(const RowMatrix< double > &matrix)
RowMatrix< double > rowCoords_
MvaFrequencySet(std::shared_ptr< const ProteicAlphabet > alpha)
Constructor.
void initSet(const CoalaCore &coala)
void computeReverseCOA(const std::vector< double > &positions, std::vector< double > &tmpFreqs)
std::vector< double > columnWeights_
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
void computeCoordsFirstSpaceCOA(std::vector< double > &tmpFreqs, std::vector< double > &freqs)
void fireParameterChanged(const ParameterList &parameters) override
void setTransposeMatrixOfPpalAxes(const RowMatrix< double > &matrix)
std::map< std::string, std::string > paramValues_
void setNbrOfAxes(const size_t &nAxes)
void setVectorOfColumnWeights(const std::vector< double > &cw)
std::vector< double > col(size_t j) const
static T sum(const std::vector< T > &v1)
static T min(const std::vector< T > &v)
static T max(const std::vector< T > &v)
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
std::string toString(T t)
Defines the basic types of data flow nodes.