From e3678c264edd1f12b8e27a7dfce85d78de7b1821 Mon Sep 17 00:00:00 2001 From: Adrian Tan Date: Tue, 21 Mar 2017 13:21:50 -0400 Subject: [PATCH] fixed bug in bcf_ip2g --- hts_utils.cpp | 40 +++++++++-- hts_utils.h | 5 ++ test.cpp | 193 +++++++++++++++++++++++++++++--------------------- 3 files changed, 153 insertions(+), 85 deletions(-) diff --git a/hts_utils.cpp b/hts_utils.cpp index e8c67638..40fc7cfc 100644 --- a/hts_utils.cpp +++ b/hts_utils.cpp @@ -925,6 +925,11 @@ uint32_t bcf_ag2p(uint32_t no_alleles, uint32_t no_genotypes) /** * Gets genotype from genotype index and ploidy. + * + * The genotype index is computed by a summation of a series which is + * monotonically decreasing. This allows you to compute the inverse function + * from index to the ordered genotypes by using a "water rapids algorithm" with + * decreasing height of each mini water fall. */ std::vector bcf_ip2g(int32_t genotype_index, uint32_t no_ploidy) { @@ -933,22 +938,23 @@ std::vector bcf_ip2g(int32_t genotype_index, uint32_t no_ploidy) int32_t pth = no_ploidy; int32_t max_allele_index = genotype_index; int32_t leftover_genotype_index = genotype_index; - + while (pth>0) { + int32_t old_pth = pth; + for (int32_t allele_index=0; allele_index <= max_allele_index; ++allele_index) { int32_t i = choose(pth+allele_index-1, pth); - if (i>=leftover_genotype_index) + if (i>=leftover_genotype_index || allele_index==max_allele_index) { if (i>leftover_genotype_index) --allele_index; leftover_genotype_index -= choose(pth+allele_index-1, pth); --pth; max_allele_index = allele_index; - genotype[pth] = allele_index; + genotype[pth] = allele_index; break; - } } } @@ -980,6 +986,32 @@ uint32_t bcf_g2i(int32_t* g, uint32_t n) } } +/** + * Gets index of a genotype of n ploidy. + */ +uint32_t bcf_g2i(std::vector& g) +{ + int32_t n = g.size(); + + if (n==1) + { + return g[0]; + } + if (n==2) + { + return g[0] + (((g[1]+1)*(g[1]))>>1); + } + else + { + uint32_t index = 0; + for (uint32_t i=0; i bcf_ip2g(int32_t genotype_index, uint32_t no_ploidy); */ uint32_t bcf_g2i(int32_t* g, uint32_t p); +/** + * Gets index of a genotype of n ploidy. + */ +uint32_t bcf_g2i(std::vector& g); + /** * Gets index of a diploid genotype. */ diff --git a/test.cpp b/test.cpp index 93ca8768..ff1fde25 100644 --- a/test.cpp +++ b/test.cpp @@ -75,83 +75,83 @@ class Igor : Program }; - /** - * n choose r. - */ - uint32_t choose(uint32_t n, uint32_t r) - { - if (r>n) - { - return 0; - } - else if (r==n) - { - return 1; - } - else if (r==0) - { - return 1; - } - else - { - if (r>(n>>1)) - { - r = n-r; - } - - uint32_t num = n; - uint32_t denum = 1; - - for (uint32_t i=1; in) +// { +// return 0; +// } +// else if (r==n) +// { +// return 1; +// } +// else if (r==0) +// { +// return 1; +// } +// else +// { +// if (r>(n>>1)) +// { +// r = n-r; +// } +// +// uint32_t num = n; +// uint32_t denum = 1; +// +// for (uint32_t i=1; i genotypes = bcf_ip2g(index, no_ploidy); + + for (int32_t j=0; j