libsequence  1.9.5
generic.cc
1 #include <unordered_map>
2 #include <algorithm>
3 #include <cstdint>
4 #include <limits>
5 #include <vector>
6 
7 namespace Sequence
8 {
9  double
11  const std::unordered_map<std::int32_t, std::int32_t>& counts,
12  const std::size_t nsam)
13  {
14  if (counts.empty() || !nsam)
15  {
16  return std::numeric_limits<double>::quiet_NaN();
17  }
18  double hom = 0.0;
19  for (auto&& c : counts)
20  {
21  hom += static_cast<double>(c.second * (c.second - 1));
22  }
23  hom /= static_cast<double>(nsam * (nsam - 1));
24  return 1.0 - hom;
25  }
26 } // namespace Sequence
double diversity_from_counts(const std::unordered_map< std::int32_t, std::int32_t > &counts, const std::size_t nsam)
Calculate heterozygosity/diversity from count data.
Definition: generic.cc:10
The namespace in which this library resides.