43 #include <Bpp/Utils/MapTools.h> 44 #include <Bpp/Numeric/NumTools.h> 45 #include <Bpp/Numeric/VectorTools.h> 59 for (
size_t i = 0; i < site.
size(); i++)
72 for (
size_t i = 0; i < site.
size(); i++)
85 for (
size_t i = 0; i < site.
size(); i++)
98 for (
size_t i = 0; i < site.
size(); i++)
111 for (
size_t i = 0; i < site.
size(); i++)
130 for (
size_t i = 0; i < site1.
size(); i++)
132 if (site1[i] != site2[i])
144 if (site.size() == 0)
145 throw EmptySiteException(
"SiteTools::isConstant: Incorrect specified site, size must be > 0", &site);
148 int gap = site.getAlphabet()->getGapCharacterCode();
152 int unknown = site.getAlphabet()->getUnknownCharacterCode();
154 while (i < site.size() && (s == gap || s == unknown))
159 if (s == unknown || s == gap)
161 if (unresolvedRaisesException)
162 throw EmptySiteException(
"SiteTools::isConstant: Site is only made of gaps or generic characters.");
166 while (i < site.size())
168 if (site[i] != s && site[i] != gap && site[i] != unknown)
177 while (i < site.size() && s == gap)
184 if (unresolvedRaisesException)
189 while (i < site.size())
191 if (site[i] != s && site[i] != gap)
205 if (site.size() == 0)
206 throw EmptySiteException(
"SiteTools::variabilityShannon: Incorrect specified site, size must be > 0", &site);
208 getFrequencies(site, p, resolveUnknown);
211 for (
int i = 0; i < static_cast<int>(site.getAlphabet()->getSize()); i++)
225 if (site1.size() == 0)
226 throw EmptySiteException(
"SiteTools::mutualInformation: Incorrect specified site, size must be > 0", &site1);
227 if (site2.size() == 0)
228 throw EmptySiteException(
"SiteTools::mutualInformation: Incorrect specified site, size must be > 0", &site2);
229 if (site1.size() != site2.size())
230 throw DimensionException(
"SiteTools::mutualInformation: sites must have the same size!", site1.size(), site2.size());
231 vector<double> p1(site1.getAlphabet()->getSize());
232 vector<double> p2(site2.getAlphabet()->getSize());
233 map<int, map<int, double> > p12;
234 getCounts(site1, site2, p12, resolveUnknown);
235 double mi = 0, tot = 0, pxy;
237 for (
size_t i = 0; i < site1.getAlphabet()->getSize(); i++)
239 for (
size_t j = 0; j < site2.getAlphabet()->getSize(); j++)
241 pxy = p12[
static_cast<int>(i)][static_cast<int>(j)];
247 for (
size_t i = 0; i < site1.getAlphabet()->getSize(); i++)
251 for (
size_t j = 0; j < site2.getAlphabet()->getSize(); j++)
255 for (
size_t i = 0; i < site1.getAlphabet()->getSize(); i++)
257 for (
size_t j = 0; j < site2.getAlphabet()->getSize(); j++)
259 pxy = p12[
static_cast<int>(i)][static_cast<int>(j)] / tot;
261 mi += pxy * log(pxy / (p1[i] * p2[j]));
272 if (site1.size() == 0)
273 throw EmptySiteException(
"SiteTools::jointEntropy: Incorrect specified site, size must be > 0", &site1);
274 if (site2.size() == 0)
275 throw EmptySiteException(
"SiteTools::jointEntropy: Incorrect specified site, size must be > 0", &site2);
276 if (site1.size() != site2.size())
277 throw DimensionException(
"SiteTools::jointEntropy: sites must have the same size!", site1.size(), site2.size());
278 map<int, map<int, double> > p12;
279 getCounts(site1, site2, p12, resolveUnknown);
280 double tot = 0, pxy, h = 0;
282 for (
size_t i = 0; i < site1.getAlphabet()->getSize(); i++)
284 for (
size_t j = 0; j < site2.getAlphabet()->getSize(); j++)
286 pxy = p12[
static_cast<int>(i)][static_cast<int>(j)];
290 for (
size_t i = 0; i < site1.getAlphabet()->getSize(); i++)
292 for (
size_t j = 0; j < site2.getAlphabet()->getSize(); j++)
294 pxy = p12[
static_cast<int>(i)][static_cast<int>(j)] / tot;
307 if (site.size() == 0)
308 throw EmptySiteException(
"SiteTools::variabilityFactorial: Incorrect specified site, size must be > 0", &site);
311 vector<size_t> c = MapTools::getValues(p);
312 size_t s = VectorTools::sum(c);
313 long double l =
static_cast<long double>(NumTools::fact(s)) / static_cast<long double>(VectorTools::sum(VectorTools::fact(c)));
314 return (static_cast<double>(std::log(l)));
322 if (site.size() == 0)
323 throw EmptySiteException(
"SiteTools::heterozygosity: Incorrect specified site, size must be > 0", &site);
325 getFrequencies(site, p);
326 vector<double> c = MapTools::getValues(p);
327 double n = VectorTools::norm<double, double>(MapTools::getValues(p));
336 if (site.size() == 0)
337 throw EmptySiteException(
"SiteTools::getNumberOfDistinctCharacters(): Incorrect specified site, size must be > 0", &site);
341 map<int, size_t> counts;
344 for (map<int, size_t>::iterator it = counts.begin(); it != counts.end(); it++)
357 if (site.size() == 0)
358 throw EmptySiteException(
"SiteTools::hasSingleton: Incorrect specified site, size must be > 0", &site);
362 map<int, size_t> counts;
363 getCounts(site, counts);
364 for (map<int, size_t>::iterator it = counts.begin(); it != counts.end(); it++)
377 if (site.size() == 0)
378 throw EmptySiteException(
"SiteTools::isParsimonyInformativeSite: Incorrect specified site, size must be > 0", &site);
382 map<int, size_t> counts;
385 for (map<int, size_t>::iterator it = counts.begin(); it != counts.end(); it++)
400 if (site.size() == 0)
401 throw EmptySiteException(
"SiteTools::isTriplet: Incorrect specified site, size must be > 0", &site);
virtual bool isGap(int state) const =0
This alphabet is used to deal NumericAlphabet.
virtual bool isUnresolved(int state) const =0
Exception sent when a empty site is found.
virtual size_t size() const
Get the number of elements in the list.
virtual const Alphabet * getAlphabet() const
Get the alphabet associated to the list.
virtual int getUnknownCharacterCode() const =0
virtual std::string getAlphabetType() const =0
Identification method.