bpp-phyl  2.2.0
UserProteinSubstitutionModel.cpp
Go to the documentation of this file.
1 //
2 // File: UserProteinSubstitutionModel.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Aug 26 16:27 2005
5 //
6 
7 /*
8 Copyright or © 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 
41 
42 #include <Bpp/Io/FileTools.h>
43 #include <Bpp/App/ApplicationTools.h>
44 #include <Bpp/Text/TextTools.h>
45 #include <Bpp/Text/StringTokenizer.h>
46 #include <Bpp/Numeric/VectorTools.h>
47 
48 //From SeqLib:
49 #include <Bpp/Seq/Container/SequenceContainerTools.h>
50 
51 using namespace bpp;
52 
53 // From the STL:
54 #include <iostream>
55 #include <fstream>
56 #include <string>
57 
58 using namespace std;
59 
60 /******************************************************************************/
61 
63  const ProteicAlphabet* alpha, const std::string& path, const std::string& prefix) :
64  AbstractParameterAliasable(prefix),
65  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), prefix),
66  path_(path),
67  freqSet_(0)
68 {
69  readFromFile();
71  updateMatrices();
72 }
73 
75  const ProteicAlphabet* alpha, const std::string& path,
76  ProteinFrequenciesSet* freqSet, const std::string& prefix,
77  bool initFreqs) :
78  AbstractParameterAliasable(prefix),
79  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), prefix),
80  path_(path),
81  freqSet_(freqSet)
82 {
83  readFromFile();
84  freqSet_->setNamespace(prefix+freqSet_->getNamespace());
85  if (initFreqs) freqSet->setFrequencies(freq_);
86  else freq_ = freqSet_->getFrequencies();
87  addParameters_(freqSet_->getParameters());
88  updateMatrices();
89 }
90 
91 /******************************************************************************/
92 
94 {
95  if (TextTools::hasSubstring(freqSet_->getNamespace(), "+F.") )
96  return "Empirical+F";
97  else
98  return "Empirical";
99 }
100 
101 /******************************************************************************/
102 
104 {
105  ifstream in(path_.c_str(), ios::in);
106  //Read exchangeability matrix:
107  for (unsigned int i = 1; i < 20; i++)
108  {
109  string line = FileTools::getNextLine(in);
110  StringTokenizer st(line);
111  for(unsigned int j = 0; j < i; j++) {
112  double s = TextTools::toDouble(st.nextToken());
113  exchangeability_(i,j) = exchangeability_(j,i) = s;
114  }
115  }
116  //Read frequencies:
117  unsigned int fCount = 0;
118  while (in && fCount < 20)
119  {
120  string line = FileTools::getNextLine(in);
121  StringTokenizer st(line);
122  while(st.hasMoreToken() && fCount < 20)
123  {
124  freq_[fCount] = TextTools::toDouble(st.nextToken());
125  fCount++;
126  }
127  }
128  double sf = VectorTools::sum(freq_);
129  if (sf - 1 > 0.000001)
130  {
131  ApplicationTools::displayMessage("WARNING!!! Frequencies sum to " + TextTools::toString(sf) + ", frequencies have been scaled.");
132  sf *= 1./sf;
133  }
134 
135  //Now build diagonal of the exchangeability matrix:
136  for (unsigned int i = 0; i < 20; i++)
137  {
138  double sum = 0;
139  for(unsigned int j = 0; j < 20; j++)
140  {
141  if(j!=i) sum += exchangeability_(i,j);
142  }
143  exchangeability_(i,i) = -sum;
144  }
145 
146  //Closing stream:
147  in.close();
148 }
149 
150 /******************************************************************************/
151 
152 void UserProteinSubstitutionModel::setFreqFromData(const SequenceContainer& data, double pseudoCount)
153 {
154  map<int, int> counts;
155  SequenceContainerTools::getCounts(data, counts);
156  double t = 0;
157  for (int i = 0; i < static_cast<int>(size_); i++)
158  {
159  t += (counts[i] + pseudoCount);
160  }
161  for (size_t i = 0; i < size_; ++i) freq_[i] = (static_cast<double>(counts[static_cast<int>(i)]) + pseudoCount) / t;
163  //Update parameters and re-compute generator and eigen values:
164  matchParametersValues(freqSet_->getParameters());
165 }
166 
167 /******************************************************************************/
168 
std::string getName() const
Get the name of the model.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
virtual void setFrequencies(const std::vector< double > &frequencies)=0
Set the parameters in order to match a given set of frequencies.
FrequenciesSet useful for homogeneous and stationary models, protein implementation.
STL namespace.
virtual const std::vector< double > getFrequencies() const =0
Vdouble freq_
The vector of equilibrium frequencies.
virtual void updateMatrices()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
UserProteinSubstitutionModel(const ProteicAlphabet *alpha, const std::string &path, const std::string &prefix)
Build a protein model from a PAML file, with original equilibrium frequencies.
Partial implementation of the ReversibleSubstitutionModel interface.
Parametrize a set of state frequencies for proteins.
size_t size_
The size of the generator, i.e. the number of states.
void setFreqFromData(const SequenceContainer &data, double pseudoCount=0)
Set equilibrium frequencies equal to the frequencies estimated from the data.