31 for (
size_t i = 0; i < site.
size(); i++)
46 for (
size_t i = 0; i < site.
size(); i++)
63 throw EmptySiteException(
"CodonSiteTools::isMonoSitePolymorphic: Incorrect specified site", &site);
69 vector<int> pos1, pos2, pos3;
70 auto ca = dynamic_pointer_cast<const CodonAlphabet>(site.
getAlphabet());
71 for (
size_t i = 0; i < site.
size(); i++)
73 pos1.push_back(ca->getFirstPosition(site[i]));
74 pos2.push_back(ca->getSecondPosition(site[i]));
75 pos3.push_back(ca->getThirdPosition(site[i]));
77 shared_ptr<const Alphabet> na = ca->getNucleicAlphabet();
78 Site s1(pos1, na), s2(pos2, na), s3(pos3, na);
102 if (site.
size() == 0)
103 throw EmptySiteException(
"CodonSiteTools::isSynonymousPolymorphic: Incorrect specified site", &site);
110 map<int, size_t> counts;
112 map<int, size_t> aas;
114 for (
auto it = counts.begin(); it != counts.end(); ++it)
135 if (site.
size() == 0)
136 throw EmptySiteException(
"CodonSiteTools::generateCodonSiteWithoutRareVariant: Incorrect specified site", &site);
140 auto noRareVariant = make_unique<Site>(site);
141 return noRareVariant;
146 map<int, double> freqcodon;
148 auto ca = dynamic_pointer_cast<const CodonAlphabet>(site.
getAlphabet());
149 shared_ptr<const Alphabet> na = ca->getNucleicAlphabet();
151 for (map<int, double>::iterator it = freqcodon.begin(); it != freqcodon.end(); it++)
153 if (it->second > freqmin && !gCode.
isStop(it->first))
155 newcodon = it->first;
159 vector<int> pos1, pos2, pos3;
160 for (
size_t i = 0; i < site.
size(); i++)
162 pos1.push_back(ca->getFirstPosition(site[i]));
163 pos2.push_back(ca->getSecondPosition(site[i]));
164 pos3.push_back(ca->getThirdPosition(site[i]));
166 Site s1(pos1, na), s2(pos2, na), s3(pos3, na);
167 map<int, double> freq1;
169 map<int, double> freq2;
171 map<int, double> freq3;
174 for (
size_t i = 0; i < site.
size(); i++)
176 if (freq1[s1.getValue(i)] > freqmin && freq2[s2.getValue(i)] > freqmin && freq3[s3.
getValue(i)] > freqmin)
181 codon.push_back(newcodon);
183 auto alphaPtr = dynamic_pointer_cast<const Alphabet>(ca);
184 auto noRareVariant = make_unique<Site>(codon, alphaPtr);
185 return noRareVariant;
209 vector<int> ci = ca->getPositions(i);
210 vector<int> cj = ca->getPositions(j);
212 switch (numberOfDifferences(i, j, *ca))
225 vector<double> path(2, 0);
226 vector<double> weight(2, 1);
230 int trans1 = ca->getCodon(ci[0], cj[1], ci[2]);
231 int trans2 = ca->getCodon(ci[0], ci[1], cj[2]);
233 if (!gCode.
isStop(trans1))
242 if (!gCode.
isStop(trans2))
254 int trans1 = ca->getCodon(cj[0], ci[1], ci[2]);
255 int trans2 = ca->getCodon(ci[0], ci[1], cj[2]);
256 if (!gCode.
isStop(trans1))
265 if (!gCode.
isStop(trans2))
277 int trans1 = ca->getCodon(cj[0], ci[1], ci[2]);
278 int trans2 = ca->getCodon(ci[0], cj[1], ci[2]);
279 if (!gCode.
isStop(trans1))
288 if (!gCode.
isStop(trans2))
302 for (
size_t k = 0; k < 2; k++)
304 nbdif += path[k] * weight[k];
311 vector<double> path(6, 0);
312 vector<double> weight(6, 1);
314 int trans100 = ca->getCodon(cj[0], ci[1], ci[2]);
315 int trans010 = ca->getCodon(ci[0], cj[1], ci[2]);
316 int trans001 = ca->getCodon(ci[0], ci[1], cj[2]);
318 int trans110 = ca->getCodon(cj[0], cj[1], ci[2]);
319 int trans101 = ca->getCodon(cj[0], ci[1], cj[2]);
320 int trans011 = ca->getCodon(ci[0], cj[1], cj[2]);
322 if (!gCode.
isStop(trans100))
326 path[0]++; path[1]++;
328 if (!gCode.
isStop(trans110))
337 if (!gCode.
isStop(trans101))
349 weight[0] = 0; weight[1] = 0;
351 if (!gCode.
isStop(trans010))
355 path[2]++; path[3]++;
357 if (!gCode.
isStop(trans110))
366 if (!gCode.
isStop(trans011))
378 weight[2] = 0; weight[3] = 0;
380 if (!gCode.
isStop(trans001))
384 path[4]++; path[5]++;
386 if (!gCode.
isStop(trans101))
395 if (!gCode.
isStop(trans011))
407 weight[4] = 0; weight[5] = 0;
413 for (
size_t k = 0; k < 6; k++)
415 nbdif += path[k] * weight[k];
435 if (site.
size() == 0)
436 throw EmptySiteException(
"CodonSiteTools::piSynonymous: Incorrect specified site", &site);
443 map<int, double> freq;
446 for (map<int, double>::iterator it1 = freq.begin(); it1 != freq.end(); it1++)
448 for (map<int, double>::iterator it2 = freq.begin(); it2 != freq.end(); it2++)
450 pi += (it1->second) * (it2->second) * (numberOfSynonymousDifferences(it1->first, it2->first, gCode, minchange));
453 double n =
static_cast<double>(site.
size());
454 return pi * n / (n - 1);
467 if (site.
size() == 0)
468 throw EmptySiteException(
"CodonSiteTools::piSynonymous: Incorrect specified site", &site);
473 if (isSynonymousPolymorphic(site, gCode))
476 map<int, double> freq;
478 auto ca = dynamic_pointer_cast<const CodonAlphabet>(site.
getAlphabet());
480 for (map<int, double>::iterator it1 = freq.begin(); it1 != freq.end(); it1++)
482 for (map<int, double>::iterator it2 = freq.begin(); it2 != freq.end(); it2++)
484 double nbtot =
static_cast<double>(numberOfDifferences(it1->first, it2->first, *ca));
485 double nbsyn = numberOfSynonymousDifferences(it1->first, it2->first, gCode, minchange);
486 pi += (it1->second) * (it2->second) * (nbtot - nbsyn);
490 double n =
static_cast<double>(site.
size());
491 return pi * n / (n - 1);
501 if (ca->isUnresolved(i))
503 double nbsynpos = 0.0;
504 vector<int> codon = ca->getPositions(i);
506 for (
size_t pos = 0; pos < 3; ++pos)
508 for (
int an = 0; an < 4; ++an)
510 if (an == codon[pos])
512 vector<int> mutcodon = codon;
514 int intcodon = ca->getCodon(mutcodon[0], mutcodon[1], mutcodon[2]);
515 if (gCode.
isStop(intcodon))
520 if (((codon[pos] == 0 || codon[pos] == 2) && (mutcodon[pos] == 1 || mutcodon[pos] == 3)) ||
521 ((codon[pos] == 1 || codon[pos] == 3) && (mutcodon[pos] == 0 || mutcodon[pos] == 2)))
523 nbsynpos = nbsynpos + 1 / (ratio + 2);
527 nbsynpos = nbsynpos + ratio / (ratio + 2);
542 throw AlphabetException(
"CodonSiteTools::meanNumberOfSynonymousPositions: alphabet is not CodonAlphabet", alphabet);
546 if (site.
size() == 0)
547 throw EmptySiteException(
"CodonSiteTools::meanNumberOfSynonymousPositions: Incorrect specified site", &site);
552 map<int, double> freqs;
555 for (
const auto& it : freqs)
557 int state = it.first;
558 if (!alphabet->isUnresolved(state) &&
559 !alphabet->isGap(state))
561 double freq = it.second;
563 nbSyn += freq * numberOfSynonymousPositions(state, gCode, ratio);
566 return nbSyn / total;
577 if (site.
size() == 0)
578 throw EmptySiteException(
"CodonSiteTools::numberOfSubstitutions: Incorrect specified site", &site);
582 unique_ptr<Site> newsite;
583 if (freqmin > 1. /
static_cast<double>(site.
size()))
586 newsite = make_unique<Site>(site);
590 vector<int> pos1, pos2, pos3;
592 auto ca = dynamic_pointer_cast<const CodonAlphabet>(site.
getAlphabet());
594 for (
size_t i = 0; i < newsite->size(); i++)
596 pos1.push_back(ca->getFirstPosition(newsite->getValue(i)));
597 pos2.push_back(ca->getSecondPosition(newsite->getValue(i)));
598 pos3.push_back(ca->getThirdPosition(newsite->getValue(i)));
601 shared_ptr<const Alphabet> na = ca->getNucleicAlphabet();
603 Site s1(pos1, na), s2(pos2, na), s3(pos3, na);
622 if (site.
size() == 0)
623 throw EmptySiteException(
"CodonSiteTools::numberOfNonSynonymousSubstitutions: Incorrect specified site", &site);
627 unique_ptr<Site> newsite;
628 if (freqmin > 1. /
static_cast<double>(site.
size()))
629 newsite = generateCodonSiteWithoutRareVariant(site, gCode, freqmin);
631 newsite = make_unique<Site>(site);
635 map<int, size_t>
count;
640 auto ca = dynamic_pointer_cast<const CodonAlphabet>(site.
getAlphabet());
642 for (map<int, size_t>::iterator it1 =
count.begin(); it1 !=
count.end(); it1++)
645 for (map<int, size_t>::iterator it2 =
count.begin(); it2 !=
count.end(); it2++)
647 size_t Ntot = numberOfDifferences(it1->first, it2->first, *ca);
648 size_t Ns = (size_t)numberOfSynonymousDifferences(it1->first, it2->first, gCode,
true);
649 if (Nmin > Ntot - Ns && it1->first != it2->first)
656 return NaSup - Nminmin;
673 if (siteIn.
size() == 0)
674 throw EmptySiteException(
"CodonSiteTools::getFixedDifferences Incorrect specified site", &siteIn);
675 if (siteOut.
size() == 0)
676 throw EmptySiteException(
"CodonSiteTools::getFixedDifferences Incorrect specified site", &siteOut);
680 size_t Ntot = numberOfDifferences(i, j, *ca);
681 size_t Ns =
static_cast<size_t>(numberOfSynonymousDifferences(i, j, gCode,
true));
682 size_t Na = Ntot - Ns;
684 vector<int> pos1in, pos2in, pos3in, pos1out, pos2out, pos3out;
686 for (
size_t k = 0; k < siteIn.
size(); k++)
688 pos1in.push_back(ca->getFirstPosition(siteIn[k]));
689 pos2in.push_back(ca->getSecondPosition(siteIn[k]));
690 pos3in.push_back(ca->getThirdPosition(siteIn[k]));
691 pos1out.push_back(ca->getFirstPosition(siteOut[k]));
692 pos2out.push_back(ca->getSecondPosition(siteOut[k]));
693 pos3out.push_back(ca->getThirdPosition(siteOut[k]));
695 shared_ptr<const Alphabet> na = ca->getNucleicAlphabet();
697 Site s1in(pos1in, na), s2in(pos2in, na), s3in(pos3in, na);
698 Site s1out(pos1out, na), s2out(pos2out, na), s3out(pos3out, na);
767 if (ca->getSecondPosition(i) == ca->getSecondPosition(j))
800 for (
size_t i = 0; i < site.
size(); i++)
810 for (
size_t i = 0; i < site.
size(); i++)
const Alphabet & alphabet() const override
Get the alphabet associated to the list.
const T & getValue(size_t pos) const override
checked access to a character in list.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
size_t size() const override
Get the number of elements in the list.
The alphabet exception base class.
Exception thrown when two alphabets do not match.
virtual bool equals(const Alphabet &alphabet) const =0
Comparison of alphabets.
int getThirdPosition(int codon) const
Get the int code of the third position of a codon given its int description.
int getFirstPosition(int codon) const
Get the int code of the first position of a codon given its int description.
int getSecondPosition(int codon) const
Get the int code of the second position of a codon given its int description.
Exception sent when a empty site is found.
Partial implementation of the Transliterator interface for genetic code object.
bool isFourFoldDegenerated(int codon) const
virtual std::shared_ptr< const CodonAlphabet > getCodonAlphabet() const
Alias for getSourceAlphabet return a pointer toward a CodonAlphabet.
int translate(int state) const override
Translate a given state coded as a int from source alphabet to target alphabet.
std::shared_ptr< const Alphabet > getSourceAlphabet() const override
Get the source alphabet.
const Alphabet & sourceAlphabet() const override
Get the source alphabet.
bool areSynonymous(int i, int j) const
Tell if two codons are synonymous, that is, if they encode the same amino-acid.
virtual bool isStop(int state) const =0
Tells is a particular codon is a stop codon.
std::size_t count(const std::string &s, const std::string &pattern)
This alphabet is used to deal NumericAlphabet.