bpp-phyl  2.1.0
Bpp/Phyl/Model/Codon/YNGKP_M7.cpp
Go to the documentation of this file.
00001 //
00002 // File: YNGKP_M7.cpp
00003 // Created by:  Laurent Gueguen
00004 // Created on: May 2010
00005 //
00006 
00007 /*
00008    Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
00009    This software is a computer program whose purpose is to provide classes
00010    for phylogenetic data analysis.
00011 
00012    This software is governed by the CeCILL  license under French law and
00013    abiding by the rules of distribution of free software.  You can  use,
00014    modify and/ or redistribute the software under the terms of the CeCILL
00015    license as circulated by CEA, CNRS and INRIA at the following URL
00016    "http://www.cecill.info".
00017 
00018    As a counterpart to the access to the source code and  rights to copy,
00019    modify and redistribute granted by the license, users are provided only
00020    with a limited warranty  and the software's author,  the holder of the
00021    economic rights,  and the successive licensors  have only  limited
00022    liability.
00023 
00024    In this respect, the user's attention is drawn to the risks associated
00025    with loading,  using,  modifying and/or developing or reproducing the
00026    software by the user in light of its specific status of free software,
00027    that may mean  that it is complicated to manipulate,  and  that  also
00028    therefore means  that it is reserved for developers  and  experienced
00029    professionals having in-depth computer knowledge. Users are therefore
00030    encouraged to load and test the software's suitability as regards their
00031    requirements in conditions enabling the security of their systems and/or
00032    data to be ensured and,  more generally, to use and operate it in the
00033    same conditions as regards security.
00034 
00035    The fact that you are presently reading this means that you have had
00036    knowledge of the CeCILL license and that you accept its terms.
00037  */
00038 
00039 #include "YNGKP_M7.h"
00040 #include "YN98.h"
00041 
00042 #include <Bpp/Numeric/NumConstants.h>
00043 #include <Bpp/Numeric/Prob/BetaDiscreteDistribution.h>
00044 
00045 #include <Bpp/Text/TextTools.h>
00046 
00047 using namespace bpp;
00048 
00049 using namespace std;
00050 
00051 /******************************************************************************/
00052 
00053 YNGKP_M7::YNGKP_M7(const GeneticCode* gc, FrequenciesSet* codonFreqs, unsigned int nclass) :
00054   AbstractBiblioMixedSubstitutionModel("YNGKP_M7."),
00055   pmixmodel_(0),
00056   synfrom_(-1),
00057   synto_(-1)
00058 {
00059   if (nclass <= 0)
00060     throw Exception("Bad number of classes for model YNGKP_M7: " + TextTools::toString(nclass));
00061 
00062   // build the submodel
00063 
00064   BetaDiscreteDistribution* pbdd = new BetaDiscreteDistribution(nclass, 2, 2);
00065 
00066   map<string, DiscreteDistribution*> mpdd;
00067   mpdd["omega"] = pbdd;
00068 
00069   pmixmodel_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), new YN98(gc, codonFreqs), mpdd));
00070   delete pbdd;
00071 
00072   // mapping the parameters
00073 
00074   ParameterList pl = pmixmodel_->getParameters();
00075   for (size_t i = 0; i < pl.size(); i++)
00076   {
00077     lParPmodel_.addParameter(Parameter(pl[i]));
00078   }
00079 
00080   vector<std::string> v = dynamic_cast<YN98*>(pmixmodel_->getNModel(0))->getFrequenciesSet()->getParameters().getParameterNames();
00081 
00082   for (size_t i = 0; i < v.size(); i++)
00083   {
00084     mapParNamesFromPmodel_[v[i]] = getParameterNameWithoutNamespace("YNGKP_M7." + v[i].substr(5));
00085   }
00086 
00087   mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
00088   mapParNamesFromPmodel_["YN98.omega_Beta.alpha"] = "p";
00089   mapParNamesFromPmodel_["YN98.omega_Beta.beta"] = "q";
00090 
00091   // specific parameters
00092 
00093   string st;
00094   for (map<string, string>::iterator it = mapParNamesFromPmodel_.begin(); it != mapParNamesFromPmodel_.end(); it++)
00095   {
00096     st = pmixmodel_->getParameterNameWithoutNamespace(it->first);
00097     addParameter_(new Parameter("YNGKP_M7." + it->second, pmixmodel_->getParameterValue(st),
00098                             pmixmodel_->getParameter(st).hasConstraint() ? pmixmodel_->getParameter(st).getConstraint()->clone() : 0, true));
00099   }
00100 
00101   // look for synonymous codons
00102   for (synfrom_ = 1; synfrom_ < (int)gc->getSourceAlphabet()->getSize(); synfrom_++)
00103   {
00104     for (synto_ = 0; synto_ < synfrom_; synto_++)
00105     {
00106       if ((gc->areSynonymous(synfrom_, synto_))
00107           && (pmixmodel_->getNModel(0)->Qij(synfrom_, synto_) != 0)
00108           && (pmixmodel_->getNModel(1)->Qij(synfrom_, synto_) != 0))
00109         break;
00110     }
00111     if (synto_ < synfrom_)
00112       break;
00113   }
00114 
00115   if (synto_ == (int)gc->getSourceAlphabet()->getSize())
00116     throw Exception("Impossible to find synonymous codons");
00117 
00118   // update Matrices
00119 
00120   updateMatrices();
00121 }
00122 
00123 YNGKP_M7::YNGKP_M7(const YNGKP_M7& mod2) : AbstractBiblioMixedSubstitutionModel(mod2),
00124   pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
00125   synfrom_(mod2.synfrom_),
00126   synto_(mod2.synto_)
00127 {}
00128 
00129 YNGKP_M7& YNGKP_M7::operator=(const YNGKP_M7& mod2)
00130 {
00131   AbstractBiblioMixedSubstitutionModel::operator=(mod2);
00132 
00133   pmixmodel_.reset(new MixtureOfASubstitutionModel(*mod2.pmixmodel_));
00134   synfrom_ = mod2.synfrom_;
00135   synto_ = mod2.synto_;
00136 
00137   return *this;
00138 }
00139 
00140 YNGKP_M7::~YNGKP_M7() {}
00141 
00142 void YNGKP_M7::updateMatrices()
00143 {
00144   AbstractBiblioSubstitutionModel::updateMatrices();
00145 
00146   // homogeneization of the synonymous substitution rates
00147 
00148   Vdouble vd;
00149 
00150   for (unsigned int i = 0; i < pmixmodel_->getNumberOfModels(); i++)
00151   {
00152     vd.push_back(1 / pmixmodel_->getNModel(i)->Qij(synfrom_, synto_));
00153   }
00154 
00155   pmixmodel_->setVRates(vd);
00156 }
00157 
 All Classes Namespaces Files Functions Variables Typedefs Friends