bpp-phyl  2.1.0
Bpp/Phyl/Model/FrequenciesSet/CodonFrequenciesSet.cpp
Go to the documentation of this file.
00001 //
00002 // File: CodonFrequenciesSet.cpp
00003 // Created by: Laurent Gueguen
00004 // Created on: lundi 2 avril 2012, à 14h 15
00005 //
00006 
00007 /*
00008    Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
00009 
00010    This software is a computer program whose purpose is to provide classes
00011    for phylogenetic data analysis.
00012 
00013    This software is governed by the CeCILL  license under French law and
00014    abiding by the rules of distribution of free software.  You can  use,
00015    modify and/ or redistribute the software under the terms of the CeCILL
00016    license as circulated by CEA, CNRS and INRIA at the following URL
00017    "http://www.cecill.info".
00018 
00019    As a counterpart to the access to the source code and  rights to copy,
00020    modify and redistribute granted by the license, users are provided only
00021    with a limited warranty  and the software's author,  the holder of the
00022    economic rights,  and the successive licensors  have only  limited
00023    liability.
00024 
00025    In this respect, the user's attention is drawn to the risks associated
00026    with loading,  using,  modifying and/or developing or reproducing the
00027    software by the user in light of its specific status of free software,
00028    that may mean  that it is complicated to manipulate,  and  that  also
00029    therefore means  that it is reserved for developers  and  experienced
00030    professionals having in-depth computer knowledge. Users are therefore
00031    encouraged to load and test the software's suitability as regards their
00032    requirements in conditions enabling the security of their systems and/or
00033    data to be ensured and,  more generally, to use and operate it in the
00034    same conditions as regards security.
00035 
00036    The fact that you are presently reading this means that you have had
00037    knowledge of the CeCILL license and that you accept its terms.
00038  */
00039 
00040 #include "CodonFrequenciesSet.h"
00041 #include "NucleotideFrequenciesSet.h"
00042 
00043 using namespace bpp;
00044 using namespace std;
00045 
00046 // ////////////////////////////
00047 // FullCodonFrequenciesSet
00048 
00049 FullCodonFrequenciesSet::FullCodonFrequenciesSet(const GeneticCode* gCode, bool allowNullFreqs, const string& name) :
00050   AbstractFrequenciesSet(
00051       gCode->getSourceAlphabet()->getSize(),
00052       gCode->getSourceAlphabet(),
00053       "Full.",
00054       name),
00055   pgc_(gCode)
00056 {
00057   size_t size = gCode->getSourceAlphabet()->getSize() - gCode->getNumberOfStopCodons();
00058   size_t j = 0;
00059 
00060   for (size_t i = 0; i < gCode->getSourceAlphabet()->getSize() - 1; i++)
00061   {
00062     if (gCode->isStop(static_cast<int>(i)))
00063     {
00064       getFreq_(i) = 0;
00065     }
00066     else
00067     {
00068       addParameter_(new Parameter(
00069                       "Full.theta" + TextTools::toString(i + 1),
00070                       1. / static_cast<double>(size - j),
00071                       allowNullFreqs ?
00072                       &Parameter::PROP_CONSTRAINT_IN :
00073                       &FrequenciesSet::FREQUENCE_CONSTRAINT_MILLI));
00074       getFreq_(i) = 1. / static_cast<double>(size);
00075       j++;
00076     }
00077   }
00078   size_t i = gCode->getSourceAlphabet()->getSize() - 1;
00079   getFreq_(i) = (gCode->isStop(static_cast<int>(i))) ? 0 : 1. / static_cast<double>(size);
00080 }
00081 
00082 
00083 FullCodonFrequenciesSet::FullCodonFrequenciesSet(
00084     const GeneticCode* gCode,
00085     const vector<double>& initFreqs,
00086     bool allowNullFreqs,
00087     const string& name) :
00088   AbstractFrequenciesSet(gCode->getSourceAlphabet()->getSize(), gCode->getSourceAlphabet(), "Full.", name),
00089   pgc_(gCode)
00090 {
00091   if (initFreqs.size() != gCode->getSourceAlphabet()->getSize())
00092     throw Exception("FullCodonFrequenciesSet(constructor). There must be " + TextTools::toString(gCode->getSourceAlphabet()->getSize()) + " frequencies.");
00093   double sum = 0.0;
00094 
00095   for (size_t i = 0; i < initFreqs.size(); i++)
00096   {
00097     if (!gCode->isStop(static_cast<int>(i)))
00098     {
00099       sum += initFreqs[i];
00100     }
00101   }
00102 
00103   double y = 1;
00104   for (size_t i = 0; i < gCode->getSourceAlphabet()->getSize() - 1; i++)
00105   {
00106     if (gCode->isStop(static_cast<int>(i)))
00107     {
00108       getFreq_(i) = 0;
00109     }
00110     else
00111     {
00112       addParameter_(new Parameter(
00113                       "Full.theta" + TextTools::toString(i + 1),
00114                       initFreqs[i] / sum / y,
00115                       allowNullFreqs ?
00116                       &Parameter::PROP_CONSTRAINT_IN :
00117                       &FrequenciesSet::FREQUENCE_CONSTRAINT_MILLI));
00118       getFreq_(i) = initFreqs[i] / sum;
00119       y -= initFreqs[i] / sum;
00120     }
00121   }
00122   size_t i = gCode->getSourceAlphabet()->getSize() - 1;
00123   getFreq_(i) = (gCode->isStop(static_cast<int>(i))) ? 0 : initFreqs[i] / sum;
00124 }
00125 
00126 FullCodonFrequenciesSet::FullCodonFrequenciesSet(const FullCodonFrequenciesSet& fcfs):
00127   AbstractFrequenciesSet(fcfs),
00128   pgc_(fcfs.pgc_)
00129 {}
00130 
00131 FullCodonFrequenciesSet& FullCodonFrequenciesSet::operator=(const FullCodonFrequenciesSet& fcfs) {
00132   AbstractFrequenciesSet::operator=(fcfs);
00133   pgc_ = fcfs.pgc_;
00134   return *this;
00135 }
00136 
00137 void FullCodonFrequenciesSet::setFrequencies(const vector<double>& frequencies)
00138 {
00139   if (frequencies.size() != getAlphabet()->getSize())
00140     throw DimensionException("FullFrequenciesSet::setFrequencies", frequencies.size(), getAlphabet()->getSize());
00141   const CodonAlphabet* alphabet = getAlphabet();
00142 
00143   double sum = 0.0;
00144   size_t i;
00145   for (i = 0; i < frequencies.size(); i++)
00146   {
00147     if (!(pgc_->isStop(static_cast<int>(i))))
00148       sum += frequencies[i];
00149   }
00150 
00151   double y = 1;
00152   for (i = 0; i < alphabet->getSize() - 1; i++)
00153   {
00154     if (pgc_->isStop(static_cast<int>(i)))
00155     {
00156       getFreq_(i) = 0;
00157     }
00158     else
00159     {
00160       getParameter_("theta" + TextTools::toString(i + 1)).setValue(frequencies[i] / sum / y);
00161       y -= frequencies[i] / sum;
00162       getFreq_(i) = frequencies[i] / sum;
00163     }
00164   }
00165   i = alphabet->getSize() - 1;
00166   getFreq_(i) = (pgc_->isStop(static_cast<int>(i))) ? 0 : frequencies[i] / sum;
00167 }
00168 
00169 void FullCodonFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
00170 {
00171   const CodonAlphabet* alphabet = getAlphabet();
00172   double y = 1;
00173   size_t i;
00174   for (i = 0; i < alphabet->getSize() - 1; i++)
00175   {
00176     if (!(pgc_->isStop(static_cast<int>(i))))
00177     {
00178       getFreq_(i) = getParameter_("theta" + TextTools::toString(i + 1)).getValue() * y;
00179       y *= 1 - getParameter_("theta" + TextTools::toString(i + 1)).getValue();
00180     }
00181   }
00182 
00183   i = alphabet->getSize() - 1;
00184   getFreq_(i) = (pgc_->isStop(static_cast<int>(i))) ? 0 : y;
00185 }
00186 
00187 
00188 // ////////////////////////////
00189 // FullPerAACodonFrequenciesSet
00190 
00191 FullPerAACodonFrequenciesSet::FullPerAACodonFrequenciesSet(
00192     const GeneticCode* gencode,
00193     ProteinFrequenciesSet* ppfs) :
00194   AbstractFrequenciesSet(gencode->getSourceAlphabet()->getSize(), gencode->getSourceAlphabet(), "FullPerAA.", "FullPerAA"),
00195   pgc_(gencode),
00196   ppfs_(ppfs),
00197   vS_()
00198 {
00199   const ProteicAlphabet* ppa = dynamic_cast<const ProteicAlphabet*>(pgc_->getTargetAlphabet());
00200 
00201   for (size_t i = 0; i < ppa->getSize(); i++)
00202   {
00203     vector<int> vc = pgc_->getSynonymous(static_cast<int>(i));
00204     vS_.push_back(Simplex(vc.size(), 1, ""));
00205 
00206     Simplex& si = vS_[i];
00207     si.setNamespace("FullPerAA." + ppa->getAbbr(static_cast<int>(i)) + "_");
00208     addParameters_(si.getParameters());
00209   }
00210 
00211   ppfs_->setNamespace("FullPerAA." + ppfs_->getName() + ".");
00212   addParameters_(ppfs_->getParameters());
00213 
00214   updateFrequencies();
00215 }
00216 
00217 FullPerAACodonFrequenciesSet::FullPerAACodonFrequenciesSet(const GeneticCode* gencode) :
00218   AbstractFrequenciesSet(gencode->getSourceAlphabet()->getSize(), gencode->getSourceAlphabet(), "FullPerAA.", "FullPerAA"),
00219   pgc_(gencode),
00220   ppfs_(new FixedProteinFrequenciesSet(dynamic_cast<const ProteicAlphabet*>(gencode->getTargetAlphabet()), "FullPerAA.")),
00221   vS_()
00222 {
00223   const ProteicAlphabet* ppa = dynamic_cast<const ProteicAlphabet*>(pgc_->getTargetAlphabet());
00224 
00225   for (size_t i = 0; i < ppa->getSize(); i++)
00226   {
00227     vector<int> vc = pgc_->getSynonymous(static_cast<int>(i));
00228     vS_.push_back(Simplex(vc.size(), 1, ""));
00229 
00230     Simplex& si = vS_[i];
00231     si.setNamespace("FullPerAA." + ppa->getAbbr(static_cast<int>(i)) + "_");
00232     addParameters_(si.getParameters());
00233   }
00234 
00235   updateFrequencies();
00236 }
00237 
00238 FullPerAACodonFrequenciesSet::FullPerAACodonFrequenciesSet(const FullPerAACodonFrequenciesSet& ffs) :
00239   CodonFrequenciesSet(ffs),
00240   AbstractFrequenciesSet(ffs),
00241   pgc_(ffs.pgc_),
00242   ppfs_(ffs.ppfs_->clone()),
00243   vS_(ffs.vS_)
00244 {
00245   updateFrequencies();
00246 }
00247 
00248 FullPerAACodonFrequenciesSet& FullPerAACodonFrequenciesSet::operator=(const FullPerAACodonFrequenciesSet& ffs)
00249 {
00250   CodonFrequenciesSet::operator=(ffs);
00251   AbstractFrequenciesSet::operator=(ffs);
00252   pgc_ = ffs.pgc_;
00253   ppfs_.reset(ffs.ppfs_->clone());
00254   vS_ = ffs.vS_;
00255 
00256   return *this;
00257 }
00258 
00259 void FullPerAACodonFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
00260 {
00261   if (dynamic_cast<AbstractFrequenciesSet*>(ppfs_.get()))
00262     (dynamic_cast<AbstractFrequenciesSet*>(ppfs_.get()))->matchParametersValues(parameters);
00263   for (size_t i = 0; i < vS_.size(); i++)
00264   {
00265     vS_[i].matchParametersValues(parameters);
00266   }
00267   updateFrequencies();
00268 }
00269 
00270 void FullPerAACodonFrequenciesSet::updateFrequencies()
00271 {
00272   const ProteicAlphabet* ppa = dynamic_cast<const ProteicAlphabet*>(pgc_->getTargetAlphabet());
00273 
00274   for (size_t i = 0; i < ppa->getSize(); i++)
00275   {
00276     std::vector<int> vc = pgc_->getSynonymous(static_cast<int>(i));
00277     for (size_t j = 0; j < vc.size(); j++)
00278     {
00279       getFreq_(vc[j]) = (ppfs_->getFrequencies())[i] * vS_[i].prob(j);
00280     }
00281   }
00282 }
00283 
00284 void FullPerAACodonFrequenciesSet::setFrequencies(const vector<double>& frequencies)
00285 {
00286   if (frequencies.size() != getAlphabet()->getSize())
00287     throw DimensionException("FullParAAFrequenciesSet::setFrequencies", frequencies.size(), getAlphabet()->getSize());
00288 
00289   const ProteicAlphabet* ppa = dynamic_cast<const ProteicAlphabet*>(pgc_->getTargetAlphabet());
00290 
00291   vector<double> vaa;
00292   double S = 0;
00293   for (size_t i = 0; i < ppa->getSize(); i++)
00294   {
00295     vector<double> vp;
00296     double s = 0;
00297     std::vector<int> vc = pgc_->getSynonymous(static_cast<int>(i));
00298     for (size_t j = 0; j < vc.size(); j++)
00299     {
00300       vp.push_back(frequencies[vc[j]]);
00301       s += frequencies[vc[j]];
00302     }
00303     S += s;
00304     vaa.push_back(s);
00305     vp /= s;
00306     vS_[i].setFrequencies(vp);
00307     matchParametersValues(vS_[i].getParameters());
00308   }
00309 
00310   vaa /= S; // to avoid counting of stop codons
00311   ppfs_->setFrequencies(vaa);
00312   matchParametersValues(ppfs_->getParameters());
00313   updateFrequencies();
00314 }
00315 
00316 void FullPerAACodonFrequenciesSet::setNamespace(const std::string& prefix)
00317 {
00318   const ProteicAlphabet* ppa = dynamic_cast<const ProteicAlphabet*>(pgc_->getTargetAlphabet());
00319 
00320   AbstractFrequenciesSet::setNamespace(prefix);
00321   ppfs_->setNamespace(prefix + ppfs_->getName() + ".");
00322   for (size_t i = 0; i < vS_.size(); i++)
00323   {
00324     vS_[i].setNamespace(prefix + ppa->getAbbr(static_cast<int>(i)) + "_");
00325   }
00326 }
00327 
00328 
00329 // ///////////////////////////////////////////
00330 // / FixedCodonFrequenciesSet
00331 
00332 FixedCodonFrequenciesSet::FixedCodonFrequenciesSet(
00333     const GeneticCode* gCode,
00334     const vector<double>& initFreqs,
00335     const string& name) :
00336   AbstractFrequenciesSet(gCode->getSourceAlphabet()->getSize(), gCode->getSourceAlphabet(), "Fixed.", name),
00337   pgc_(gCode)
00338 {
00339   setFrequencies(initFreqs);
00340 }
00341 
00342 FixedCodonFrequenciesSet::FixedCodonFrequenciesSet(const GeneticCode* gCode, const string& name) :
00343   AbstractFrequenciesSet(gCode->getSourceAlphabet()->getSize(), gCode->getSourceAlphabet(), "Fixed.", name),
00344   pgc_(gCode)
00345 {
00346   size_t size = gCode->getSourceAlphabet()->getSize() - gCode->getNumberOfStopCodons();
00347 
00348   for (size_t i = 0; i < gCode->getSourceAlphabet()->getSize(); i++)
00349   {
00350     getFreq_(i) = (gCode->isStop(static_cast<int>(i))) ? 0 : 1. / static_cast<double>(size);
00351   }
00352 }
00353 
00354 void FixedCodonFrequenciesSet::setFrequencies(const vector<double>& frequencies)
00355 {
00356   const CodonAlphabet* ca = dynamic_cast<const CodonAlphabet*>(getAlphabet());
00357   if (frequencies.size() != ca->getSize())
00358     throw DimensionException("FixedFrequenciesSet::setFrequencies", frequencies.size(), ca->getSize());
00359   double sum = 0.0;
00360 
00361   for (size_t i = 0; i < frequencies.size(); i++)
00362   {
00363     if (!(pgc_->isStop(static_cast<int>(i))))
00364       sum += frequencies[i];
00365   }
00366 
00367   for (size_t i = 0; i < ca->getSize(); i++)
00368   {
00369     getFreq_(i) = (pgc_->isStop(static_cast<int>(i))) ? 0 : frequencies[i] / sum;
00370   }
00371 }
00372 
00373 
00374 // ///////////////////////////////////////////////////////////////////
00375 // // CodonFromIndependentFrequenciesSet
00376 
00377 
00378 CodonFromIndependentFrequenciesSet::CodonFromIndependentFrequenciesSet(
00379   const GeneticCode* gCode,
00380   const std::vector<FrequenciesSet*>& freqvector,
00381   const string& name,
00382   const string& mgmtStopFreq) :
00383   WordFromIndependentFrequenciesSet(gCode->getSourceAlphabet(), freqvector, "", name),
00384   mStopNeigh_(),
00385   mgmtStopFreq_(2),
00386   pgc_(gCode)
00387 {
00388   if (mgmtStopFreq == "uniform")
00389     mgmtStopFreq_ = 0;
00390   else if (mgmtStopFreq == "linear")
00391     mgmtStopFreq_ = 1;
00392   
00393   // fill the map of the stop codons
00394 
00395   vector<int> vspcod = gCode->getStopCodonsAsInt();
00396   for (size_t ispcod = 0; ispcod < vspcod.size(); ispcod++)
00397   {
00398     size_t pow = 1;
00399     int nspcod = vspcod[ispcod];
00400     for (int ph = 0; ph < 3; ph++)
00401     {
00402       size_t nspcod0 = nspcod - pow * getAlphabet()->getNPosition(nspcod, 2 - ph);
00403       for (size_t dec = 0; dec < 4; dec++)
00404       {
00405         size_t vois = nspcod0 + pow * dec;
00406         if (!pgc_->isStop(static_cast<int>(vois)))
00407           mStopNeigh_[nspcod].push_back(static_cast<int>(vois));
00408       }
00409       pow *= 4;
00410     }
00411   }
00412 
00413   updateFrequencies();
00414 }
00415 
00416 const CodonAlphabet* CodonFromIndependentFrequenciesSet::getAlphabet() const
00417 {
00418   return dynamic_cast<const CodonAlphabet*>(WordFromIndependentFrequenciesSet::getAlphabet());
00419 }
00420 
00421 CodonFromIndependentFrequenciesSet::CodonFromIndependentFrequenciesSet(const CodonFromIndependentFrequenciesSet& iwfs) :
00422   WordFromIndependentFrequenciesSet(iwfs),
00423   mStopNeigh_(iwfs.mStopNeigh_),
00424   mgmtStopFreq_(iwfs.mgmtStopFreq_),
00425   pgc_(iwfs.pgc_)
00426 {
00427   updateFrequencies();
00428 }
00429 
00430 CodonFromIndependentFrequenciesSet& CodonFromIndependentFrequenciesSet::operator=(const CodonFromIndependentFrequenciesSet& iwfs)
00431 {
00432   WordFromIndependentFrequenciesSet::operator=(iwfs);
00433   mStopNeigh_ = iwfs.mStopNeigh_;
00434   mgmtStopFreq_ = iwfs.mgmtStopFreq_;
00435   pgc_ = iwfs.pgc_;
00436   return *this;
00437 }
00438 
00439 void CodonFromIndependentFrequenciesSet::updateFrequencies()
00440 {
00441   WordFromIndependentFrequenciesSet::updateFrequencies();
00442 
00443   size_t s = getAlphabet()->getSize();
00444 
00445   if (mgmtStopFreq_ != 0)
00446   {
00447     // The frequencies of the stop codons are distributed to all
00448     // neighbour non-stop codons
00449     double f[64];
00450     for (size_t i = 0; i < s; i++)
00451     {
00452       f[i] = 0;
00453     }
00454 
00455     std::map<int, Vint>::iterator mStopNeigh_it(mStopNeigh_.begin());
00456     while (mStopNeigh_it != mStopNeigh_.end())
00457     {
00458       int stNb = mStopNeigh_it->first;
00459       Vint vneigh = mStopNeigh_it->second;
00460       double sneifreq = 0;
00461       for (size_t vn = 0; vn < vneigh.size(); vn++)
00462       {
00463         sneifreq += pow(getFreq_(vneigh[vn]), mgmtStopFreq_);
00464       }
00465       double x = getFreq_(stNb) / sneifreq;
00466       for (size_t vn = 0; vn < vneigh.size(); vn++)
00467       {
00468         f[vneigh[vn]] += pow(getFreq_(vneigh[vn]), mgmtStopFreq_) * x;
00469       }
00470       getFreq_(stNb) = 0;
00471       mStopNeigh_it++;
00472     }
00473 
00474     for (size_t i = 0; i < s; i++)
00475     {
00476       getFreq_(i) += f[i];
00477     }
00478   }
00479   else
00480   {
00481     double sum = 0.;
00482     for (size_t i = 0; i < s; i++)
00483       if (!pgc_->isStop(static_cast<int>(i)))
00484     sum += getFreq_(i);
00485  
00486     for (size_t i = 0; i < s; i++)
00487       if (pgc_->isStop(static_cast<int>(i)))
00488         getFreq_(i) = 0;
00489       else
00490         getFreq_(i) /= sum;
00491   }
00492 }
00493 
00494 // ///////////////////////////////////////////////////////////////////
00495 // // CodonFromUniqueFrequenciesSet
00496 
00497 
00498 CodonFromUniqueFrequenciesSet::CodonFromUniqueFrequenciesSet(
00499     const GeneticCode* gCode,
00500     FrequenciesSet* pfreq,
00501     const string& name,
00502     const string& mgmtStopFreq) :
00503   WordFromUniqueFrequenciesSet(gCode->getSourceAlphabet(), pfreq, "", name),
00504   mStopNeigh_(),
00505   mgmtStopFreq_(2),
00506   pgc_(gCode)
00507 {
00508   if (mgmtStopFreq == "uniform")
00509     mgmtStopFreq_ = 0;
00510   else if (mgmtStopFreq == "linear")
00511     mgmtStopFreq_ = 1;
00512   
00513   // fill the map of the stop codons
00514 
00515   vector<int> vspcod = gCode->getStopCodonsAsInt();
00516   for (size_t ispcod = 0; ispcod < vspcod.size(); ispcod++)
00517   {
00518     size_t pow = 1;
00519     int nspcod = vspcod[ispcod];
00520     for (int ph = 0; ph < 3; ph++)
00521     {
00522       size_t nspcod0 = nspcod - pow * getAlphabet()->getNPosition(nspcod, 2 - ph);
00523       for (size_t dec = 0; dec < 4; dec++)
00524       {
00525         size_t vois = nspcod0 + pow * dec;
00526         if (!pgc_->isStop(static_cast<int>(vois)))
00527           mStopNeigh_[nspcod].push_back(static_cast<int>(vois));
00528       }
00529       pow *= 4;
00530     }
00531   }
00532 
00533   updateFrequencies();
00534 }
00535 
00536 const CodonAlphabet* CodonFromUniqueFrequenciesSet::getAlphabet() const
00537 {
00538   return dynamic_cast<const CodonAlphabet*>(WordFromUniqueFrequenciesSet::getAlphabet());
00539 }
00540 
00541 
00542 CodonFromUniqueFrequenciesSet::CodonFromUniqueFrequenciesSet(const CodonFromUniqueFrequenciesSet& iwfs) :
00543   WordFromUniqueFrequenciesSet(iwfs),
00544   mStopNeigh_(iwfs.mStopNeigh_),
00545   mgmtStopFreq_(iwfs.mgmtStopFreq_),
00546   pgc_(iwfs.pgc_)
00547 {
00548   updateFrequencies();
00549 }
00550 
00551 CodonFromUniqueFrequenciesSet& CodonFromUniqueFrequenciesSet::operator=(const CodonFromUniqueFrequenciesSet& iwfs)
00552 {
00553   WordFromUniqueFrequenciesSet::operator=(iwfs);
00554   mStopNeigh_ = iwfs.mStopNeigh_;
00555   mgmtStopFreq_ = iwfs.mgmtStopFreq_;
00556   pgc_ = iwfs.pgc_;
00557   return *this;
00558 }
00559 
00560 void CodonFromUniqueFrequenciesSet::updateFrequencies()
00561 {
00562   WordFromUniqueFrequenciesSet::updateFrequencies();
00563 
00564   size_t s = getAlphabet()->getSize();
00565 
00566   if (mgmtStopFreq_!=0)
00567   {
00568     // The frequencies of the stop codons are distributed to all
00569     // neighbour non-stop codons
00570     double f[64];
00571     for (size_t i = 0; i < s; i++)
00572     {
00573       f[i] = 0;
00574     }
00575 
00576     std::map<int, Vint>::iterator mStopNeigh_it(mStopNeigh_.begin());
00577     while (mStopNeigh_it != mStopNeigh_.end())
00578     {
00579       int stNb = mStopNeigh_it->first;
00580       Vint vneigh = mStopNeigh_it->second;
00581       double sneifreq = 0;
00582       for (size_t vn = 0; vn < vneigh.size(); vn++)
00583       {
00584         sneifreq += pow(getFreq_(vneigh[vn]), mgmtStopFreq_);
00585       }
00586       double x = getFreq_(stNb) / sneifreq;
00587       for (size_t vn = 0; vn < vneigh.size(); vn++)
00588       {
00589         f[vneigh[vn]] += pow(getFreq_(vneigh[vn]), mgmtStopFreq_) * x;
00590       }
00591       getFreq_(stNb) = 0;
00592       mStopNeigh_it++;
00593     }
00594 
00595     for (size_t i = 0; i < s; i++)
00596     {
00597       getFreq_(i) += f[i];
00598     }
00599   }
00600   else
00601   {
00602     double sum = 0.;
00603     for (size_t i = 0; i < s; i++)
00604       if (!pgc_->isStop(static_cast<int>(i)))
00605         sum += getFreq_(i);
00606  
00607     for (unsigned int i = 0; i < s; i++)
00608       if (pgc_->isStop(static_cast<int>(i)))
00609         getFreq_(i) = 0;
00610       else
00611         getFreq_(i) /= sum;
00612   }
00613 }
00614 
00615 /*********************************************************************/
00616 
00617 FrequenciesSet* CodonFrequenciesSet::getFrequenciesSetForCodons(short option, const GeneticCode* gCode, const string& mgmtStopFreq)
00618 {
00619   FrequenciesSet* codonFreqs;
00620 
00621   if (option == F0)
00622     codonFreqs = new FixedCodonFrequenciesSet(gCode, "F0");
00623   else if (option == F1X4)
00624     codonFreqs = new CodonFromUniqueFrequenciesSet(gCode, new FullNucleotideFrequenciesSet(gCode->getSourceAlphabet()->getNucleicAlphabet()), "F1X4", mgmtStopFreq);
00625   else if (option == F3X4)
00626   {
00627     vector<FrequenciesSet*> v_AFS(3);
00628     v_AFS[0] = new FullNucleotideFrequenciesSet(gCode->getSourceAlphabet()->getNucleicAlphabet());
00629     v_AFS[1] = new FullNucleotideFrequenciesSet(gCode->getSourceAlphabet()->getNucleicAlphabet());
00630     v_AFS[2] = new FullNucleotideFrequenciesSet(gCode->getSourceAlphabet()->getNucleicAlphabet());
00631     codonFreqs = new CodonFromIndependentFrequenciesSet(gCode, v_AFS, "F3X4", mgmtStopFreq);
00632   }
00633   else if (option == F61)
00634     codonFreqs = new FullCodonFrequenciesSet(gCode, "F61");
00635   else
00636     throw Exception("FrequenciesSet::getFrequencySetForCodons(). Unvalid codon frequency set argument.");
00637 
00638   return codonFreqs;
00639 }
00640 
00641 /******************************************************************************/
00642 
00643 const short CodonFrequenciesSet::F0   = 0;
00644 const short CodonFrequenciesSet::F1X4 = 1;
00645 const short CodonFrequenciesSet::F3X4 = 2;
00646 const short CodonFrequenciesSet::F61  = 3;
00647 
00648 /******************************************************************************/
00649 
 All Classes Namespaces Files Functions Variables Typedefs Friends