42 #include "../StateMap.h" 45 #include <Bpp/Numeric/Prob/Simplex.h> 59 sFreq_(gCode->getSourceAlphabet()->getSize() - gCode->getNumberOfStopCodons(), method, allowNullFreqs,
"Full.")
62 double r = 1. /
static_cast<double>(
sFreq_.dimension());
64 for (
size_t i = 0; i <
sFreq_.dimension(); i++)
70 addParameters_(
sFreq_.getParameters());
75 const GeneticCode* gCode,
76 const vector<double>& initFreqs,
78 unsigned short method,
82 sFreq_(gCode->getSourceAlphabet()->getSize() - gCode->getNumberOfStopCodons(), method, allowNullFreqs,
"Full.")
85 throw Exception(
"FullCodonFrequenciesSet(constructor). There must be " + TextTools::toString(gCode->getSourceAlphabet()->getSize()) +
" frequencies.");
88 for (
size_t i = 0; i <
getAlphabet()->getSize(); i++)
90 if (!
pgc_->isStop(static_cast<int>(i)))
95 for (
size_t i = 0; i <
getAlphabet()->getSize(); i++)
97 if (!gCode->isStop(static_cast<int>(i)))
98 vd.push_back(initFreqs[i] / sum);
101 sFreq_.setFrequencies(vd);
102 addParameters_(
sFreq_.getParameters());
123 sFreq_.setNamespace(nameSpace);
124 AbstractFrequenciesSet::setNamespace(nameSpace);
129 if (frequencies.size() !=
getAlphabet()->getSize())
130 throw DimensionException(
"FullFrequenciesSet::setFrequencies", frequencies.size(),
getAlphabet()->getSize());
133 for (
size_t i = 0; i <
getAlphabet()->getSize(); i++)
135 if (!
pgc_->isStop(static_cast<int>(i)))
136 sum += frequencies[i];
140 for (
size_t i = 0; i <
getAlphabet()->getSize(); i++)
142 if (!
pgc_->isStop(static_cast<int>(i)))
143 vd.push_back(frequencies[i] / sum);
146 sFreq_.setFrequencies(vd);
147 setParametersValues(
sFreq_.getParameters());
153 sFreq_.matchParametersValues(parameters);
161 for (
size_t j = 0; j <
getAlphabet()->getSize(); j++)
163 if (
pgc_->isStop(static_cast<int>(j)))
178 const GeneticCode* gencode,
180 unsigned short method) :
186 const ProteicAlphabet* ppa =
dynamic_cast<const ProteicAlphabet*
>(
pgc_->getTargetAlphabet());
191 vector<int> vc =
pgc_->getSynonymous(aa);
192 vS_.push_back(Simplex(vc.size(), method, 0,
""));
194 Simplex& si =
vS_[i];
196 si.setNamespace(
"FullPerAA." + ppa->getAbbr(aa) +
"_");
197 addParameters_(si.getParameters());
200 ppfs_->setNamespace(
"FullPerAA." +
ppfs_->getName() +
".");
201 addParameters_(
ppfs_->getParameters());
212 const ProteicAlphabet* ppa =
dynamic_cast<const ProteicAlphabet*
>(
pgc_->getTargetAlphabet());
217 vector<int> vc =
pgc_->getSynonymous(aa);
218 vS_.push_back(Simplex(vc.size(), method, 0,
""));
220 Simplex& si =
vS_[i];
221 si.setNamespace(
"FullPerAA." + ppa->getAbbr(aa) +
"_");
222 addParameters_(si.getParameters());
232 ppfs_(ffs.ppfs_->clone()),
240 CodonFrequenciesSet::operator=(ffs);
251 if (dynamic_cast<AbstractFrequenciesSet*>(
ppfs_.get()))
253 for (
size_t i = 0; i <
vS_.size(); i++)
255 vS_[i].matchParametersValues(parameters);
266 std::vector<int> vc =
pgc_->getSynonymous(aa);
267 for (
size_t j = 0; j < vc.size(); j++)
271 static_cast<double>(vc.size()) *
ppfs_->getFrequencies()[i] *
vS_[i].prob(j);
279 if (frequencies.size() !=
getAlphabet()->getSize())
280 throw DimensionException(
"FullPerAAFrequenciesSet::setFrequencies", frequencies.size(),
getAlphabet()->getSize());
289 std::vector<int> vc =
pgc_->getSynonymous(aa);
293 for (
size_t j = 0; j < vc.size(); j++)
295 size_t index =
pgc_->getSourceAlphabet()->getStateIndex(vc[j]);
296 vp.push_back(frequencies[index-1]);
297 s += frequencies[index-1];
301 vS_[i].setFrequencies(vp);
303 matchParametersValues(
vS_[i].getParameters());
305 bigS += s /
static_cast<double>(vc.size());
306 vaa.push_back(s / static_cast<double>(vc.size()));
310 ppfs_->setFrequencies(vaa);
311 matchParametersValues(
ppfs_->getParameters());
317 const ProteicAlphabet* ppa =
dynamic_cast<const ProteicAlphabet*
>(
pgc_->getTargetAlphabet());
319 AbstractFrequenciesSet::setNamespace(prefix);
320 ppfs_->setNamespace(prefix +
ppfs_->getName() +
".");
321 for (
size_t i = 0; i <
vS_.size(); i++)
323 vS_[i].setNamespace(prefix + ppa->getAbbr(static_cast<int>(i)) +
"_");
332 const GeneticCode* gCode,
333 const vector<double>& initFreqs,
334 const string& name) :
345 size_t size = gCode->getSourceAlphabet()->getSize() - gCode->getNumberOfStopCodons();
347 for (
size_t i = 0; i < gCode->getSourceAlphabet()->getSize(); i++)
349 getFreq_(i) = (gCode->isStop(static_cast<int>(i))) ? 0 : 1. / static_cast<double>(size);
355 const CodonAlphabet* ca =
dynamic_cast<const CodonAlphabet*
>(
getAlphabet());
356 if (frequencies.size() != ca->getSize())
357 throw DimensionException(
"FixedFrequenciesSet::setFrequencies", frequencies.size(), ca->getSize());
360 for (
size_t i = 0; i < frequencies.size(); i++)
362 if (!(
pgc_->isStop(static_cast<int>(i))))
363 sum += frequencies[i];
366 for (
size_t i = 0; i < ca->getSize(); i++)
368 getFreq_(i) = (
pgc_->isStop(static_cast<int>(i))) ? 0 : frequencies[i] / sum;
378 const GeneticCode* gCode,
379 const std::vector<FrequenciesSet*>& freqvector,
381 const string& mgmtStopFreq) :
387 if (mgmtStopFreq ==
"uniform")
389 else if (mgmtStopFreq ==
"linear")
394 vector<int> vspcod = gCode->getStopCodonsAsInt();
395 for (
size_t ispcod = 0; ispcod < vspcod.size(); ispcod++)
398 int nspcod = vspcod[ispcod];
399 for (
size_t ph = 0; ph < 3; ph++)
401 size_t nspcod0 =
static_cast<size_t>(nspcod) - pow * static_cast<size_t>(
getAlphabet()->getNPosition(nspcod, 2 - ph));
402 for (
size_t dec = 0; dec < 4; dec++)
404 size_t vois = nspcod0 + pow * dec;
405 if (!
pgc_->isStop(static_cast<int>(vois)))
406 mStopNeigh_[nspcod].push_back(static_cast<int>(vois));
421 mStopNeigh_(iwfs.mStopNeigh_),
422 mgmtStopFreq_(iwfs.mgmtStopFreq_),
448 for (
size_t i = 0; i < s; i++)
453 std::map<int, Vint>::iterator mStopNeigh_it(
mStopNeigh_.begin());
456 int stNb = mStopNeigh_it->first;
457 Vint vneigh = mStopNeigh_it->second;
459 for (
size_t vn = 0; vn < vneigh.size(); vn++)
463 double x =
getFreq_(static_cast<size_t>(stNb)) / sneifreq;
464 for (
size_t vn = 0; vn < vneigh.size(); vn++)
468 getFreq_(static_cast<size_t>(stNb)) = 0;
472 for (
size_t i = 0; i < s; i++)
480 for (
size_t i = 0; i < s; i++)
482 if (!
pgc_->isStop(static_cast<int>(i)))
486 for (
size_t i = 0; i < s; i++)
488 if (
pgc_->isStop(static_cast<int>(i)))
501 const GeneticCode* gCode,
504 const string& mgmtStopFreq) :
510 if (mgmtStopFreq ==
"uniform")
512 else if (mgmtStopFreq ==
"linear")
517 vector<int> vspcod = gCode->getStopCodonsAsInt();
518 for (
size_t ispcod = 0; ispcod < vspcod.size(); ispcod++)
521 int nspcod = vspcod[ispcod];
522 for (
int ph = 0; ph < 3; ph++)
524 size_t nspcod0 =
static_cast<size_t>(nspcod) - pow * static_cast<size_t>(
getAlphabet()->getNPosition(nspcod, static_cast<unsigned int>(2 - ph)));
525 for (
size_t dec = 0; dec < 4; dec++)
527 size_t vois = nspcod0 + pow * dec;
528 if (!
pgc_->isStop(static_cast<int>(vois)))
529 mStopNeigh_[nspcod].push_back(static_cast<int>(vois));
546 mStopNeigh_(iwfs.mStopNeigh_),
547 mgmtStopFreq_(iwfs.mgmtStopFreq_),
573 for (
size_t i = 0; i < s; i++)
578 std::map<int, Vint>::iterator mStopNeigh_it(
mStopNeigh_.begin());
581 int stNb = mStopNeigh_it->first;
582 Vint vneigh = mStopNeigh_it->second;
584 for (
size_t vn = 0; vn < vneigh.size(); vn++)
588 double x =
getFreq_(static_cast<size_t>(stNb)) / sneifreq;
589 for (
size_t vn = 0; vn < vneigh.size(); vn++)
593 getFreq_(static_cast<size_t>(stNb)) = 0;
597 for (
size_t i = 0; i < s; i++)
605 for (
size_t i = 0; i < s; i++)
607 if (!
pgc_->isStop(static_cast<int>(i)))
611 for (
unsigned int i = 0; i < s; i++)
613 if (
pgc_->isStop(static_cast<int>(i)))
629 else if (option ==
F1X4)
631 else if (option ==
F3X4)
633 vector<FrequenciesSet*> v_AFS(3);
639 else if (option ==
F61)
642 throw Exception(
"FrequenciesSet::getFrequencySetForCodons(). Unvalid codon frequency set argument.");
virtual void updateFrequencies()
virtual int getAlphabetStateAsInt(size_t index) const =0
A generic FrequenciesSet for Full Codon alphabets.
void setNamespace(const std::string &prefix)
CodonFromUniqueFrequenciesSet & operator=(const CodonFromUniqueFrequenciesSet &iwfs)
This class implements a state map where all resolved states are modeled.
const CodonAlphabet * getAlphabet() const
FullCodonFrequenciesSet(const GeneticCode *gCode, bool allowNullFreqs=false, unsigned short method=1, const std::string &name="Full")
Construction with uniform frequencies on the letters of the alphabet. The stop codon frequencies are ...
the Frequencies in codons are the product of Independent Frequencies in letters with the frequencies ...
FrequenciesSet integrating ProteinFrequenciesSet inside CodonFrequenciesSet. In this case...
std::vector< Simplex > vS_
virtual size_t getNumberOfModelStates() const =0
const WordAlphabet * getAlphabet() const
CodonFromIndependentFrequenciesSet(const GeneticCode *gCode, const std::vector< FrequenciesSet *> &freqvector, const std::string &name="Codon", const std::string &mgmtStopFreq="quadratic")
Constructor from a CodonAlphabet* and a vector of different FrequenciesSet*. Throws an Exception if t...
FrequenciesSet useful for homogeneous and stationary models, protein implementation.
FullPerAACodonFrequenciesSet & operator=(const FullPerAACodonFrequenciesSet &ffs)
CodonFromUniqueFrequenciesSet(const GeneticCode *gCode, FrequenciesSet *pfreq, const std::string &name="Codon", const std::string &mgmtStopFreq="quadratic")
Constructor from a CodonAlphabet* and a FrequenciesSet* repeated three times.
void setFrequencies(const std::vector< double > &frequencies)
the given frequencies are normalized such thaat the sum of the frequencies on the non-stop codons equ...
FixedCodonFrequenciesSet(const GeneticCode *gCode, const std::vector< double > &initFreqs, const std::string &name="Fixed")
void setFrequencies(const std::vector< double > &frequencies)
the given frequencies are normalized such that the sum of the frequencies on the non-stop codons equa...
void fireParameterChanged(const ParameterList ¶meters)
Parametrize a set of state frequencies.
std::map< int, Vint > mStopNeigh_
virtual void updateFrequencies()
WordFromIndependentFrequenciesSet & operator=(const WordFromIndependentFrequenciesSet &iwfs)
Parametrize a set of state frequencies for codons.
CodonFromIndependentFrequenciesSet & operator=(const CodonFromIndependentFrequenciesSet &iwfs)
Nucleotide FrequenciesSet using three independent parameters (theta, theta1, theta2) to modelize the ...
const CodonAlphabet * getAlphabet() const
FrequenciesSet useful for homogeneous and stationary models, codon implementation.
Parametrize a set of state frequencies for proteins.
const CodonAlphabet * getAlphabet() const
void updateFrequencies()
Update the frequencies given the parameters.
const StateMap & getStateMap() const
std::map< int, Vint > mStopNeigh_
FullCodonFrequenciesSet & operator=(const FullCodonFrequenciesSet &fcfs)
double & getFreq_(size_t i)
FullPerAACodonFrequenciesSet(const GeneticCode *gencode, ProteinFrequenciesSet *ppfs, unsigned short method=1)
Create a new FullPerAACodonFrequenciesSet object.
void setFrequencies(const std::vector< double > &frequencies)
the given frequencies are normalized such thaat the sum of the frequencies on the non-stop codons equ...
void fireParameterChanged(const ParameterList ¶meters)
static FrequenciesSet * getFrequenciesSetForCodons(short option, const GeneticCode *gCode, const std::string &mgmtStopFreq="quadratic", unsigned short method=1)
A helper function that provide frequencies set for codon models according to PAML option...
void setNamespace(const std::string &nameSpace)
const CodonAlphabet * getAlphabet() const
unsigned short mgmtStopFreq_
the Frequencies in words are the product of Independent Frequencies in letters
unsigned short mgmtStopFreq_
Map the states of a given alphabet which have a model state.
AbstractFrequenciesSet & operator=(const AbstractFrequenciesSet &af)
Simplex sFreq_
Simplex to handle the probabilities and the parameters.
std::auto_ptr< ProteinFrequenciesSet > ppfs_
WordFromUniqueFrequenciesSet & operator=(const WordFromUniqueFrequenciesSet &iwfs)
the Frequencies in codons are the product of the frequencies for a unique FrequenciesSet in letters...
Basic implementation of the FrequenciesSet interface.
const CodonAlphabet * getAlphabet() const