Skip to content
Open
10 changes: 6 additions & 4 deletions c/tests/test_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -3066,8 +3066,8 @@ test_two_site_correlated_multiallelic(void)
-0.008818342151675485, -0.008818342151675485 };
double truth_pi2_unbiased[4] = { 0.0582010582010582, 0.0582010582010582,
0.0582010582010582, 0.0582010582010582 };
double truth_D2_unbiased_disjoint[4] = { 0.007407407407407407, 0.007407407407407407,
0.007407407407407407, 0.007407407407407407 };
double truth_D2_unbiased_disjoint[4] = { 0.06666666666666667, 0.06666666666666667,
0.06666666666666667, 0.06666666666666667 };

tsk_treeseq_from_text(
&ts, 20, nodes, edges, NULL, tree_sites, mutations, NULL, NULL, 0);
Expand Down Expand Up @@ -3176,6 +3176,7 @@ test_two_site_correlated_multiallelic(void)
assert_arrays_almost_equal(result_size, result, truth_D2_unbiased);

// two disjoint sample sets with 5 and 4 samples {0,1,2,3,4}{5,6,7,8}
// only alleles carried by both sample sets are used
sample_set_sizes[0] = 5;
sample_set_sizes[1] = 4;
tsk_memset(result, 0, sizeof(*result) * result_size);
Expand Down Expand Up @@ -3258,8 +3259,8 @@ test_two_site_uncorrelated_multiallelic(void)
0.005291005291005289, -0.015873015873015872 };
double truth_pi2_unbiased[4] = { 0.06349206349206349, 0.06216931216931215,
0.06216931216931215, 0.06349206349206349 };
double truth_D2_unbiased_disjoint[4] = { 0.008333333333333333,
-0.0027777777777777775, -0.0027777777777777775, 0.03518518518518518 };
double truth_D2_unbiased_disjoint[4]
= { 0.075, -0.008333333333333333, -0.008333333333333333, 0.035185185185185174 };

tsk_treeseq_from_text(
&ts, 20, nodes, edges, NULL, tree_sites, mutations, NULL, NULL, 0);
Expand Down Expand Up @@ -3369,6 +3370,7 @@ test_two_site_uncorrelated_multiallelic(void)
assert_arrays_almost_equal(result_size, result, truth_D2_unbiased);

// two disjoint sample sets with 5 and 4 samples {0,1,2,3,4}{5,6,7,8}
// only alleles carried by both sample sets are used
sample_set_sizes[0] = 5;
sample_set_sizes[1] = 4;
tsk_memset(result, 0, sizeof(*result) * result_size);
Expand Down
166 changes: 153 additions & 13 deletions c/tskit/trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,12 @@ typedef struct {
double *weights;
double *norm;
double *result_tmp;
// For each result, whether to skip calculation involving the ancestral allele
bool *skip_ancestral;
// Running sum of the total weight over the allele pairs used
double *weight_sum;
// For each result, whether the current pair of alleles is used
bool *pair_used;
tsk_bitset_t AB_samples;
} two_locus_work_t;

Expand All @@ -2387,8 +2393,13 @@ two_locus_work_init(tsk_size_t max_alleles, tsk_size_t num_samples,
out->norm = tsk_malloc(result_dim * sizeof(*out->norm));
out->result_tmp
= tsk_malloc(result_dim * max_alleles * max_alleles * sizeof(*out->result_tmp));
out->skip_ancestral = tsk_malloc(result_dim * sizeof(*out->skip_ancestral));
out->weight_sum = tsk_malloc(result_dim * sizeof(*out->weight_sum));
out->pair_used = tsk_malloc(result_dim * sizeof(*out->pair_used));
tsk_memset(&out->AB_samples, 0, sizeof(out->AB_samples));
if (out->weights == NULL || out->norm == NULL || out->result_tmp == NULL) {
if (out->weights == NULL || out->norm == NULL || out->result_tmp == NULL
|| out->skip_ancestral == NULL || out->weight_sum == NULL
|| out->pair_used == NULL) {
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);
goto out;
}
Expand All @@ -2406,15 +2417,38 @@ two_locus_work_free(two_locus_work_t *work)
tsk_safe_free(work->weights);
tsk_safe_free(work->norm);
tsk_safe_free(work->result_tmp);
tsk_safe_free(work->skip_ancestral);
tsk_safe_free(work->weight_sum);
tsk_safe_free(work->pair_used);
tsk_bitset_free(&work->AB_samples);
}

// Returns true if an allele is carried by the sample set(s) used for result
// indexed by k, i.e. whether allele is seen in sample set k for one-way
// statistics or in both sample sets of the k-th index pair for two-way
// statistics. Alleles that are not carried are not used, so that alleles
// carried only by samples outside the sample set(s) do not affect the result.
static bool
allele_carried(const tsk_size_t *allele_counts, tsk_size_t allele_offset, tsk_size_t k,
const tsk_id_t *set_indexes)
{
tsk_size_t i, j;

if (set_indexes == NULL) {
return allele_counts[allele_offset + k] > 0;
}
i = (tsk_size_t) set_indexes[2 * k];
j = (tsk_size_t) set_indexes[2 * k + 1];
return allele_counts[allele_offset + i] > 0 && allele_counts[allele_offset + j] > 0;
}

static int
compute_general_normed_two_site_stat_result(const tsk_bitset_t *state,
const tsk_size_t *allele_counts, tsk_size_t a_off, tsk_size_t b_off,
tsk_size_t num_a_alleles, tsk_size_t num_b_alleles, tsk_size_t state_dim,
tsk_size_t result_dim, general_stat_func_t *f, sample_count_stat_params_t *f_params,
norm_func_t *norm_f, bool polarised, two_locus_work_t *restrict work, double *result)
tsk_size_t num_a_alleles, tsk_size_t num_b_alleles, const tsk_size_t *num_a_carried,
const tsk_size_t *num_b_carried, tsk_size_t state_dim, tsk_size_t result_dim,
general_stat_func_t *f, sample_count_stat_params_t *f_params, norm_func_t *norm_f,
bool polarised, two_locus_work_t *restrict work, double *result)
{
int ret = 0;
// Sample sets and b sites are rows, a sites are columns
Expand All @@ -2423,16 +2457,51 @@ compute_general_normed_two_site_stat_result(const tsk_bitset_t *state,
// a2 [s1, s2, s3] [s1, s2, s3] [s1, s2, s3]
// a3 [s1, s2, s3] [s1, s2, s3] [s1, s2, s3]
tsk_size_t k, mut_a, mut_b, result_row_len = num_b_alleles * result_dim;
uint8_t is_polarised = polarised ? 1 : 0;
const tsk_id_t *set_indexes = f_params->set_indexes;
double *restrict hap_row, *restrict result_tmp_row;
double *restrict norm = work->norm;
double *restrict weights = work->weights;
double *restrict result_tmp = work->result_tmp;
bool *restrict skip_ancestral = work->skip_ancestral;
double *restrict weight_sum = work->weight_sum;
bool *restrict pair_used = work->pair_used;
bool any_pair_used;
tsk_bitset_t AB_samples = work->AB_samples;

for (mut_a = is_polarised; mut_a < num_a_alleles; mut_a++) {
// Only alleles carried by the sample set(s) for each result are used (from
// allele_carried). If either site has fewer than two such alleles (i.e., it
// is monomorphic in the sample set), we keep the ancestral allele even for
// polarised statistics, so that the summary function is evaluated on the
// fixed allele: this gives nan for ratio statistics r2, r and D_prime
// (0 / 0) and zero for all others.
for (k = 0; k < result_dim; k++) {
skip_ancestral[k] = polarised && num_a_carried[k] >= 2 && num_b_carried[k] >= 2;
weight_sum[k] = 0;
}

for (mut_a = 0; mut_a < num_a_alleles; mut_a++) {
result_tmp_row = GET_2D_ROW(result_tmp, result_row_len, mut_a);
for (mut_b = is_polarised; mut_b < num_b_alleles; mut_b++) {
for (mut_b = 0; mut_b < num_b_alleles; mut_b++) {
// For each result, check whether this pair of alleles is used. Both
// alleles must be carried by the sample set(s) for that result, and
// neither can be the ancestral allele if it is skipped for that result.
any_pair_used = false;
for (k = 0; k < result_dim; k++) {
pair_used[k] = allele_carried(allele_counts, a_off + (mut_a * state_dim),
k, set_indexes)
&& allele_carried(allele_counts,
b_off + (mut_b * state_dim), k, set_indexes)
&& !(skip_ancestral[k] && (mut_a == 0 || mut_b == 0));
any_pair_used = any_pair_used || pair_used[k];
}
// The summary function computes all results at once, so we skip
// the haplotype counts and summary function if no result uses this
// pair of alleles, avoiding the bitset operation and summary function
// calculation.
if (!any_pair_used) {
result_tmp_row += result_dim;
continue;
}
for (k = 0; k < state_dim; k++) {
tsk_bitset_intersect(state, a_off + (mut_a * state_dim) + k, state,
b_off + (mut_b * state_dim) + k, &AB_samples);
Expand All @@ -2447,17 +2516,26 @@ compute_general_normed_two_site_stat_result(const tsk_bitset_t *state,
if (ret != 0) {
goto out;
}
ret = norm_f(result_dim, weights, num_a_alleles - is_polarised,
num_b_alleles - is_polarised, norm, f_params);
ret = norm_f(
result_dim, weights, num_a_alleles, num_b_alleles, norm, f_params);
if (ret != 0) {
goto out;
}
for (k = 0; k < result_dim; k++) {
result[k] += result_tmp_row[k] * norm[k];
if (pair_used[k]) {
result[k] += result_tmp_row[k] * norm[k];
weight_sum[k] += norm[k];
}
}
result_tmp_row += result_dim; // Advance to the next column
}
}
// Divide by the total weight of the allele pairs used, so that each result
// is a weighted average over those pairs. For uniform ("total") weighting,
// this is the mean over the allele pairs used.
for (k = 0; k < result_dim; k++) {
result[k] /= weight_sum[k];
}
out:
return ret;
}
Expand Down Expand Up @@ -2651,6 +2729,29 @@ get_mutation_sample_sets(const tsk_bitset_t *allele_samples, tsk_size_t num_samp
}
}

// For each site and result, count the alleles carried by the sample set(s) used
// for that result (see allele_carried). The result is an array of length
// (n_sites * result_dim).
static void
get_num_carried_alleles(tsk_size_t n_sites, const tsk_size_t *num_alleles,
const tsk_size_t *site_offsets, const tsk_size_t *allele_counts,
tsk_size_t num_sample_sets, tsk_size_t result_dim, const tsk_id_t *set_indexes,
tsk_size_t *num_carried_alleles)
{
tsk_size_t s, m, k;

for (s = 0; s < n_sites; s++) {
for (m = 0; m < num_alleles[s]; m++) {
for (k = 0; k < result_dim; k++) {
if (allele_carried(allele_counts,
site_offsets[s] + (m * num_sample_sets), k, set_indexes)) {
num_carried_alleles[(s * result_dim) + k]++;
}
}
}
}
}

static int
tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
tsk_size_t num_sample_sets, const tsk_size_t *sample_set_sizes,
Expand All @@ -2667,6 +2768,7 @@ tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
double *result_row;
const tsk_size_t num_samples = self->num_samples;
tsk_size_t *num_alleles = NULL, *site_offsets = NULL, *allele_counts = NULL;
tsk_size_t *num_carried_alleles = NULL;
tsk_size_t result_row_len = n_cols * result_dim;
tsk_size_t max_ss_size = 0, max_alleles = 0, n_alleles = 0;
two_locus_work_t work;
Expand All @@ -2686,7 +2788,8 @@ tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
// depends on n_sites
num_alleles = tsk_malloc(n_sites * sizeof(*num_alleles));
site_offsets = tsk_malloc(n_sites * sizeof(*site_offsets));
if (num_alleles == NULL || site_offsets == NULL) {
num_carried_alleles = tsk_calloc(n_sites * result_dim, sizeof(*num_carried_alleles));
if (num_alleles == NULL || site_offsets == NULL || num_carried_alleles == NULL) {
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);
goto out;
}
Expand Down Expand Up @@ -2726,6 +2829,8 @@ tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
}
get_mutation_sample_sets(&allele_samples, num_sample_sets, sample_set_sizes,
sample_sets, self->sample_index_map, &allele_sample_sets, allele_counts);
get_num_carried_alleles(n_sites, num_alleles, site_offsets, allele_counts,
num_sample_sets, result_dim, f_params->set_indexes, num_carried_alleles);
// For each row/column pair, fill in the sample set in the result matrix.
for (i = 0; i < n_rows; i++) {
result_row = GET_2D_ROW(result, result_row_len, i);
Expand All @@ -2740,7 +2845,9 @@ tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
// at least one site is multiallelic
ret = compute_general_normed_two_site_stat_result(&allele_sample_sets,
allele_counts, site_offsets[row_idx[i]], site_offsets[col_idx[j]],
num_alleles[row_idx[i]], num_alleles[col_idx[j]], state_dim,
num_alleles[row_idx[i]], num_alleles[col_idx[j]],
&(num_carried_alleles[row_idx[i] * result_dim]),
&(num_carried_alleles[col_idx[j] * result_dim]), state_dim,
result_dim, f, f_params, norm_f, polarised, &work,
&(result_row[j * result_dim]));
}
Expand All @@ -2757,6 +2864,7 @@ tsk_treeseq_two_site_count_stat(const tsk_treeseq_t *self, tsk_size_t state_dim,
tsk_safe_free(num_alleles);
tsk_safe_free(site_offsets);
tsk_safe_free(allele_counts);
tsk_safe_free(num_carried_alleles);
two_locus_work_free(&work);
tsk_bitset_free(&allele_samples);
tsk_bitset_free(&allele_sample_sets);
Expand Down Expand Up @@ -3088,6 +3196,21 @@ advance_collect_edges(iter_state *s, tsk_id_t index)
return ret;
}

// Returns true if both branches can produce polymorphism in sample set s, that is,
// each branch subtends at least one, but not all, of the samples in the set. The
// weights for sample set s are the counts of AB, Ab and aB samples.
static bool
branches_vary_in_set(
const double *weights, tsk_size_t s, const tsk_size_t *sample_set_sizes)
{
const double *row = GET_2D_ROW(weights, 3, s);
double n = (double) sample_set_sizes[s];
double count_a = row[0] + row[1];
double count_b = row[0] + row[2];

return count_a > 0 && count_a < n && count_b > 0 && count_b < n;
}

static int
compute_two_tree_branch_state_update(const tsk_treeseq_t *ts, tsk_id_t c,
const iter_state *A_state, const iter_state *B_state, tsk_size_t state_dim,
Expand All @@ -3100,6 +3223,9 @@ compute_two_tree_branch_state_update(const tsk_treeseq_t *ts, tsk_id_t c,
double *restrict B_branch_len = B_state->branch_len;
double *weights_row;
tsk_size_t n, k, a_row, b_row;
bool varies;
const tsk_id_t *set_indexes = f_params->set_indexes;
const tsk_size_t *sample_set_sizes = f_params->sample_set_sizes;
const double *restrict A_branch_len = A_state->branch_len;
const tsk_bitset_t *restrict A_state_samples = A_state->node_samples;
const tsk_bitset_t *restrict B_state_samples = B_state->node_samples;
Expand Down Expand Up @@ -3134,7 +3260,21 @@ compute_two_tree_branch_state_update(const tsk_treeseq_t *ts, tsk_id_t c,
goto out;
}
for (k = 0; k < result_dim; k++) {
result[k] += result_tmp[k] * a_len * b_len;
// Only pairs of branches that can produce polymorphism in the
// sample set(s) for result k contribute, where k indexes a single
// sample set for one-way statistics, or k indexes a pair of sample
// sets for two-way statistics.
if (set_indexes == NULL) {
varies = branches_vary_in_set(weights, k, sample_set_sizes);
} else {
varies = branches_vary_in_set(
weights, (tsk_size_t) set_indexes[2 * k], sample_set_sizes)
&& branches_vary_in_set(weights,
(tsk_size_t) set_indexes[2 * k + 1], sample_set_sizes);
}
if (varies) {
result[k] += result_tmp[k] * a_len * b_len;
}
}
}
out:
Expand Down
Loading
Loading