~ubuntu-branches/ubuntu/raring/libbpp-phyl/raring

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Model/YNGKP_M8.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: YNGKP_M8.cpp
 
3
// Created by:  Laurent Gueguen
 
4
// Created on: May 2010
 
5
//
 
6
 
 
7
/*
 
8
Copyright or © or Copr. CNRS, (November 16, 2004)
 
9
This software is a computer program whose purpose is to provide classes
 
10
for phylogenetic data analysis.
 
11
 
 
12
This software is governed by the CeCILL  license under French law and
 
13
abiding by the rules of distribution of free software.  You can  use, 
 
14
modify and/ or redistribute the software under the terms of the CeCILL
 
15
license as circulated by CEA, CNRS and INRIA at the following URL
 
16
"http://www.cecill.info". 
 
17
 
 
18
As a counterpart to the access to the source code and  rights to copy,
 
19
modify and redistribute granted by the license, users are provided only
 
20
with a limited warranty  and the software's author,  the holder of the
 
21
economic rights,  and the successive licensors  have only  limited
 
22
liability. 
 
23
 
 
24
In this respect, the user's attention is drawn to the risks associated
 
25
with loading,  using,  modifying and/or developing or reproducing the
 
26
software by the user in light of its specific status of free software,
 
27
that may mean  that it is complicated to manipulate,  and  that  also
 
28
therefore means  that it is reserved for developers  and  experienced
 
29
professionals having in-depth computer knowledge. Users are therefore
 
30
encouraged to load and test the software's suitability as regards their
 
31
requirements in conditions enabling the security of their systems and/or 
 
32
data to be ensured and,  more generally, to use and operate it in the 
 
33
same conditions as regards security. 
 
34
 
 
35
The fact that you are presently reading this means that you have had
 
36
knowledge of the CeCILL license and that you accept its terms.
 
37
*/
 
38
 
 
39
#include "YNGKP_M8.h"
 
40
#include "YN98.h"
 
41
#include "FrequenciesSet.h"
 
42
 
 
43
#include <Bpp/Numeric/Prob.all>
 
44
#include <Bpp/Text/TextTools.h>
 
45
 
 
46
using namespace bpp;
 
47
 
 
48
using namespace std;
 
49
 
 
50
/******************************************************************************/
 
51
 
 
52
YNGKP_M8::YNGKP_M8(const GeneticCode* gc, FrequenciesSet* codonFreqs, unsigned int nbclass) :
 
53
  AbstractMixedSubstitutionModel(gc->getSourceAlphabet(), "YNGKP_M8."), pmixmodel_(0),
 
54
  synfrom_(-1), synto_(-1),
 
55
  mapParNamesFromPmodel_(), lParPmodel_()
 
56
{
 
57
  if (nbclass<=0)
 
58
    throw Exception("Bad number of classes for model YNGKP_M8: " + TextTools::toString(nbclass));
 
59
 
 
60
  // build the submodel
 
61
  
 
62
  BetaDiscreteDistribution* pbdd=new BetaDiscreteDistribution(nbclass,2,2);
 
63
 
 
64
  vector<double> val; val.push_back(2);
 
65
  vector<double> prob; prob.push_back(1);
 
66
  SimpleDiscreteDistribution* psdd=new SimpleDiscreteDistribution(val,prob);
 
67
 
 
68
  vector<DiscreteDistribution*> v_distr;
 
69
  v_distr.push_back(pbdd); v_distr.push_back(psdd);
 
70
  prob.clear(); prob.push_back(0.5); prob.push_back(0.5);
 
71
  
 
72
  MixtureOfDiscreteDistributions* pmodd=new MixtureOfDiscreteDistributions(v_distr, prob);
 
73
  
 
74
  map<string, DiscreteDistribution*> mpdd;
 
75
  mpdd["omega"]=pmodd;
 
76
 
 
77
  pmixmodel_= new MixtureOfASubstitutionModel(gc->getSourceAlphabet(),
 
78
                                              new YN98(gc, codonFreqs),
 
79
                                              mpdd);
 
80
  delete pbdd;
 
81
 
 
82
  // mapping the parameters
 
83
  
 
84
  ParameterList pl=pmixmodel_->getParameters();
 
85
  for (unsigned int i=0;i<pl.size();i++)
 
86
    lParPmodel_.addParameter(Parameter(pl[i]));
 
87
 
 
88
  vector<std::string> v=dynamic_cast<YN98*>(pmixmodel_->getNModel(0))->getFreq().getParameters().getParameterNames();
 
89
 
 
90
  for (unsigned int i=0;i<v.size();i++)
 
91
    mapParNamesFromPmodel_[v[i]]=getParameterNameWithoutNamespace("YNGKP_M8."+v[i].substr(5));
 
92
 
 
93
  mapParNamesFromPmodel_["YN98.kappa"]="kappa";
 
94
  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"]="p0";
 
95
  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"]="p";
 
96
  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"]="q";
 
97
  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Simple.V1"]="omegas";
 
98
 
 
99
  // specific parameters
 
100
  
 
101
  string st;
 
102
  for (map<string,string>::iterator it=mapParNamesFromPmodel_.begin(); it!= mapParNamesFromPmodel_.end(); it++){
 
103
    st=pmixmodel_->getParameterNameWithoutNamespace(it->first);
 
104
    if (it->second!="omegas")
 
105
      addParameter_(Parameter("YNGKP_M8."+it->second, pmixmodel_->getParameterValue(st),
 
106
                              pmixmodel_->getParameter(st).hasConstraint()? pmixmodel_->getParameter(st).getConstraint()->clone():0,true));
 
107
  }
 
108
 
 
109
  addParameter_(Parameter("YNGKP_M8.omegas",2.,new ExcludingPositiveReal(1),true));
 
110
  
 
111
  // look for synonymous codons
 
112
  for (synfrom_=1;synfrom_<(int)gc->getSourceAlphabet()->getSize();synfrom_++){
 
113
    for (synto_=0;synto_<synfrom_;synto_++)
 
114
      if ((gc->areSynonymous(synfrom_,synto_))
 
115
          && (pmixmodel_->getNModel(0)->Qij(synfrom_,synto_)!=0) 
 
116
          && (pmixmodel_->getNModel(1)->Qij(synfrom_,synto_)!=0))
 
117
        break;
 
118
    if (synto_<synfrom_)
 
119
      break;
 
120
  }
 
121
 
 
122
  if (synto_==(int)gc->getSourceAlphabet()->getSize())
 
123
    throw Exception("Impossible to find synonymous codons");
 
124
  
 
125
  // update Matrices
 
126
  updateMatrices();
 
127
}
 
128
 
 
129
YNGKP_M8::YNGKP_M8(const YNGKP_M8& mod2) : AbstractMixedSubstitutionModel(mod2),
 
130
                                           pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
 
131
                                           synfrom_(mod2.synfrom_), synto_(mod2.synto_),
 
132
                                           mapParNamesFromPmodel_(mod2.mapParNamesFromPmodel_),
 
133
                                           lParPmodel_(mod2.lParPmodel_)
 
134
{
 
135
  
 
136
}
 
137
 
 
138
YNGKP_M8& YNGKP_M8::operator=(const YNGKP_M8& mod2)
 
139
{
 
140
  AbstractMixedSubstitutionModel::operator=(mod2);
 
141
 
 
142
  pmixmodel_=new MixtureOfASubstitutionModel(*mod2.pmixmodel_);
 
143
  mapParNamesFromPmodel_=mod2.mapParNamesFromPmodel_;
 
144
  lParPmodel_=mod2.lParPmodel_;
 
145
  synfrom_=mod2.synfrom_;
 
146
  synto_=mod2.synto_;
 
147
  
 
148
  return *this;
 
149
}
 
150
 
 
151
YNGKP_M8::~YNGKP_M8()
 
152
{
 
153
  if (pmixmodel_)
 
154
    delete pmixmodel_;
 
155
}
 
156
 
 
157
void YNGKP_M8::updateMatrices()
 
158
{
 
159
  map<string,string>::iterator it;
 
160
  
 
161
  for (it=mapParNamesFromPmodel_.begin();it!=mapParNamesFromPmodel_.end();it++)
 
162
    lParPmodel_.setParameterValue(it->first,getParameter(it->second).getValue());
 
163
  
 
164
  pmixmodel_->matchParametersValues(lParPmodel_);
 
165
 
 
166
  // homogeneization of the synonymous substittion rates
 
167
 
 
168
  Vdouble vd;
 
169
 
 
170
  for (unsigned int i=0;i<pmixmodel_->getNumberOfModels();i++)
 
171
    vd.push_back(1/pmixmodel_->getNModel(i)->Qij(synfrom_,synto_));
 
172
 
 
173
  pmixmodel_->setVRates(vd);
 
174
}
 
175
 
 
176
void YNGKP_M8::setFreq(std::map<int,double>& m)
 
177
{
 
178
  pmixmodel_->setFreq(m);
 
179
  map<string,string>::iterator it;
 
180
 
 
181
  ParameterList pl;
 
182
  for (it=mapParNamesFromPmodel_.begin();it!=mapParNamesFromPmodel_.end();it++)
 
183
    pl.addParameter(Parameter(getNamespace()+it->second,pmixmodel_->getParameterValue(it->first)));
 
184
 
 
185
  matchParametersValues(pl);
 
186
}