Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 86 additions & 6 deletions c/tests/test_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ verify_window_errors(tsk_treeseq_t *ts, tsk_flags_t mode)
ts, 1, W, 1, general_stat_error, NULL, 2, windows, options, sigma);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);

windows[0] = -1;
ret = tsk_treeseq_general_stat(
ts, 1, W, 1, general_stat_error, NULL, 2, windows, options, sigma);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);

windows[1] = -1;
ret = tsk_treeseq_general_stat(
ts, 1, W, 1, general_stat_error, NULL, 1, windows, options, sigma);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);

windows[0] = 10;
ret = tsk_treeseq_general_stat(
ts, 1, W, 1, general_stat_error, NULL, 2, windows, options, sigma);
Expand Down Expand Up @@ -438,11 +448,10 @@ verify_node_general_stat_errors(tsk_treeseq_t *ts)
static void
verify_one_way_weighted_func_errors(tsk_treeseq_t *ts, one_way_weighted_method *method)
{
// we don't have any specific errors for this function
// but we might add some in the future
int ret;
tsk_size_t num_samples = tsk_treeseq_get_num_samples(ts);
double *weights = tsk_malloc(num_samples * sizeof(double));
double bad_windows[] = { 0, -1 };
double result;
tsk_size_t j;

Expand All @@ -451,7 +460,10 @@ verify_one_way_weighted_func_errors(tsk_treeseq_t *ts, one_way_weighted_method *
}

ret = method(ts, 0, weights, 0, NULL, 0, &result);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_STATE_DIMS);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INSUFFICIENT_WEIGHTS);

ret = method(ts, 1, weights, 1, bad_windows, 0, &result);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);

free(weights);
}
Expand All @@ -460,12 +472,11 @@ static void
verify_one_way_weighted_covariate_func_errors(
tsk_treeseq_t *ts, one_way_covariates_method *method)
{
// we don't have any specific errors for this function
// but we might add some in the future
int ret;
tsk_size_t num_samples = tsk_treeseq_get_num_samples(ts);
double *weights = tsk_malloc(num_samples * sizeof(double));
double *covariates = NULL;
double bad_windows[] = { 0, -1 };
double result;
tsk_size_t j;

Expand All @@ -474,7 +485,10 @@ verify_one_way_weighted_covariate_func_errors(
}

ret = method(ts, 0, weights, 0, covariates, 0, NULL, 0, &result);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_STATE_DIMS);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INSUFFICIENT_WEIGHTS);

ret = method(ts, 1, weights, 0, covariates, 1, bad_windows, 0, &result);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);

free(weights);
}
Expand Down Expand Up @@ -558,6 +572,28 @@ verify_two_way_stat_func_errors(tsk_treeseq_t *ts, general_sample_stat_method *m
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_SAMPLE_SET_INDEX);
}

static void
verify_two_way_weighted_stat_func_errors(
tsk_treeseq_t *ts, two_way_weighted_method *method)
{
int ret;
tsk_id_t indexes[] = { 0, 0, 0, 1 };
double bad_windows[] = { -1, -1 };
double weights[10];
double result[10];

memset(weights, 0, sizeof(weights));

ret = method(ts, 2, weights, 2, indexes, 0, NULL, result, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);

ret = method(ts, 0, weights, 2, indexes, 0, NULL, result, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INSUFFICIENT_WEIGHTS);

ret = method(ts, 2, weights, 2, indexes, 1, bad_windows, result, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);
}

static void
verify_three_way_stat_func_errors(tsk_treeseq_t *ts, general_sample_stat_method *method)
{
Expand Down Expand Up @@ -1515,6 +1551,46 @@ test_paper_ex_genetic_relatedness_errors(void)
tsk_treeseq_free(&ts);
}

static void
test_paper_ex_genetic_relatedness_weighted(void)
{
tsk_treeseq_t ts;
double weights[] = { 1.2, 0.1, 0.0, 0.0, 3.4, 5.0, 1.0, -1.0 };
tsk_id_t indexes[] = { 0, 0, 0, 1 };
double result[100];
tsk_size_t num_weights;
int ret;

tsk_treeseq_from_text(&ts, 10, paper_ex_nodes, paper_ex_edges, NULL, paper_ex_sites,
paper_ex_mutations, paper_ex_individuals, NULL, 0);

for (num_weights = 1; num_weights < 3; num_weights++) {
ret = tsk_treeseq_genetic_relatedness_weighted(
&ts, num_weights, weights, 2, indexes, 0, NULL, result, TSK_STAT_SITE);
CU_ASSERT_EQUAL_FATAL(ret, 0);
ret = tsk_treeseq_genetic_relatedness_weighted(
&ts, num_weights, weights, 2, indexes, 0, NULL, result, TSK_STAT_BRANCH);
CU_ASSERT_EQUAL_FATAL(ret, 0);
ret = tsk_treeseq_genetic_relatedness_weighted(
&ts, num_weights, weights, 2, indexes, 0, NULL, result, TSK_STAT_NODE);
CU_ASSERT_EQUAL_FATAL(ret, 0);
}

tsk_treeseq_free(&ts);
}

static void
test_paper_ex_genetic_relatedness_weighted_errors(void)
{
tsk_treeseq_t ts;

tsk_treeseq_from_text(&ts, 10, paper_ex_nodes, paper_ex_edges, NULL, paper_ex_sites,
paper_ex_mutations, paper_ex_individuals, NULL, 0);
verify_two_way_weighted_stat_func_errors(
&ts, tsk_treeseq_genetic_relatedness_weighted);
tsk_treeseq_free(&ts);
}

static void
test_paper_ex_Y2_errors(void)
{
Expand Down Expand Up @@ -2108,6 +2184,10 @@ main(int argc, char **argv)
{ "test_paper_ex_genetic_relatedness_errors",
test_paper_ex_genetic_relatedness_errors },
{ "test_paper_ex_genetic_relatedness", test_paper_ex_genetic_relatedness },
{ "test_paper_ex_genetic_relatedness_weighted",
test_paper_ex_genetic_relatedness_weighted },
{ "test_paper_ex_genetic_relatedness_weighted_errors",
test_paper_ex_genetic_relatedness_weighted_errors },
{ "test_paper_ex_Y2_errors", test_paper_ex_Y2_errors },
{ "test_paper_ex_Y2", test_paper_ex_Y2 },
{ "test_paper_ex_f2_errors", test_paper_ex_f2_errors },
Expand Down
4 changes: 4 additions & 0 deletions c/tskit/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ tsk_strerror_internal(int err)
"statistic. "
"(TSK_ERR_STAT_SPAN_NORMALISE_UNSUPPORTED)";
break;
case TSK_ERR_INSUFFICIENT_WEIGHTS:
ret = "Insufficient weights provided (at least 1 required). "
"(TSK_ERR_INSUFFICIENT_WEIGHTS)";
break;

/* Mutation mapping errors */
case TSK_ERR_GENOTYPES_ALL_MISSING:
Expand Down
4 changes: 4 additions & 0 deletions c/tskit/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ The TSK_STAT_SPAN_NORMALISE option was passed to a statistic that does
not support it.
*/
#define TSK_ERR_STAT_SPAN_NORMALISE_UNSUPPORTED -912
/**
Insufficient weights were provided.
*/
#define TSK_ERR_INSUFFICIENT_WEIGHTS -913
/** @} */

/**
Expand Down
86 changes: 84 additions & 2 deletions c/tskit/trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,11 @@ typedef struct {
const tsk_id_t *set_indexes;
} sample_count_stat_params_t;

typedef struct {
double *total_weights;
const tsk_id_t *index_tuples;
} indexed_weight_stat_params_t;

static int
tsk_treeseq_sample_count_stat(const tsk_treeseq_t *self, tsk_size_t num_sample_sets,
const tsk_size_t *sample_set_sizes, const tsk_id_t *sample_sets,
Expand Down Expand Up @@ -2634,6 +2639,10 @@ tsk_treeseq_trait_covariance(const tsk_treeseq_t *self, tsk_size_t num_weights,
ret = TSK_ERR_NO_MEMORY;
goto out;
}
if (num_weights == 0) {
ret = TSK_ERR_INSUFFICIENT_WEIGHTS;
goto out;
}

// center weights
for (j = 0; j < num_samples; j++) {
Expand Down Expand Up @@ -2705,7 +2714,7 @@ tsk_treeseq_trait_correlation(const tsk_treeseq_t *self, tsk_size_t num_weights,
}

if (num_weights < 1) {
ret = TSK_ERR_BAD_STATE_DIMS;
ret = TSK_ERR_INSUFFICIENT_WEIGHTS;
goto out;
}

Expand Down Expand Up @@ -2818,7 +2827,7 @@ tsk_treeseq_trait_linear_model(const tsk_treeseq_t *self, tsk_size_t num_weights
}

if (num_weights < 1) {
ret = TSK_ERR_BAD_STATE_DIMS;
ret = TSK_ERR_INSUFFICIENT_WEIGHTS;
goto out;
}

Expand Down Expand Up @@ -3025,6 +3034,79 @@ tsk_treeseq_genetic_relatedness(const tsk_treeseq_t *self, tsk_size_t num_sample
return ret;
}

static int
genetic_relatedness_weighted_summary_func(tsk_size_t state_dim, const double *state,
tsk_size_t result_dim, double *result, void *params)
{
indexed_weight_stat_params_t args = *(indexed_weight_stat_params_t *) params;
const double *x = state;
tsk_id_t i, j;
tsk_size_t k;
double meanx, ni, nj;

meanx = state[state_dim - 1] / args.total_weights[state_dim - 1];
for (k = 0; k < result_dim; k++) {
i = args.index_tuples[2 * k];
j = args.index_tuples[2 * k + 1];
ni = args.total_weights[i];
nj = args.total_weights[j];
result[k] = (x[i] - ni * meanx) * (x[j] - nj * meanx) / 2;
}
return 0;
}

int
tsk_treeseq_genetic_relatedness_weighted(const tsk_treeseq_t *self,
tsk_size_t num_weights, const double *weights, tsk_size_t num_index_tuples,
const tsk_id_t *index_tuples, tsk_size_t num_windows, const double *windows,
double *result, tsk_flags_t options)
{
int ret = 0;
tsk_size_t num_samples = self->num_samples;
size_t j, k;
indexed_weight_stat_params_t args;
const double *row;
double *new_row;
double *total_weights = tsk_calloc((num_weights + 1), sizeof(*total_weights));
double *new_weights
= tsk_malloc((num_weights + 1) * num_samples * sizeof(*new_weights));

if (total_weights == NULL || new_weights == NULL) {
ret = TSK_ERR_NO_MEMORY;
goto out;
}
if (num_weights == 0) {
ret = TSK_ERR_INSUFFICIENT_WEIGHTS;
goto out;
}

// Add a column of ones to W
for (j = 0; j < num_samples; j++) {
row = GET_2D_ROW(weights, num_weights, j);
new_row = GET_2D_ROW(new_weights, num_weights + 1, j);
for (k = 0; k < num_weights; k++) {
new_row[k] = row[k];
total_weights[k] += row[k];
}
new_row[num_weights] = 1.0;
}
total_weights[num_weights] = (double) num_samples;

args.total_weights = total_weights;
args.index_tuples = index_tuples;
ret = tsk_treeseq_general_stat(self, num_weights + 1, new_weights, num_index_tuples,
genetic_relatedness_weighted_summary_func, &args, num_windows, windows, options,
result);
if (ret != 0) {
goto out;
}

out:
tsk_safe_free(total_weights);
tsk_safe_free(new_weights);
return ret;
}

static int
Y2_summary_func(tsk_size_t TSK_UNUSED(state_dim), const double *state,
tsk_size_t result_dim, double *result, void *params)
Expand Down
11 changes: 11 additions & 0 deletions c/tskit/trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,17 @@ int tsk_treeseq_trait_linear_model(const tsk_treeseq_t *self, tsk_size_t num_wei
const double *weights, tsk_size_t num_covariates, const double *covariates,
tsk_size_t num_windows, const double *windows, tsk_flags_t options, double *result);

/* Two way weighted stats with covariates */

typedef int two_way_weighted_method(const tsk_treeseq_t *self, tsk_size_t num_weights,
const double *weights, tsk_size_t num_index_tuples, const tsk_id_t *index_tuples,
tsk_size_t num_windows, const double *windows, double *result, tsk_flags_t options);

int tsk_treeseq_genetic_relatedness_weighted(const tsk_treeseq_t *self,
tsk_size_t num_weights, const double *weights, tsk_size_t num_index_tuples,
const tsk_id_t *index_tuples, tsk_size_t num_windows, const double *windows,
double *result, tsk_flags_t options);

/* One way sample set stats */

typedef int one_way_sample_stat_method(const tsk_treeseq_t *self,
Expand Down
1 change: 1 addition & 0 deletions docs/python-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ Single site
TreeSequence.Fst
TreeSequence.genealogical_nearest_neighbours
TreeSequence.genetic_relatedness
TreeSequence.genetic_relatedness_weighted
TreeSequence.general_stat
TreeSequence.segregating_sites
TreeSequence.sample_count_stat
Expand Down
7 changes: 7 additions & 0 deletions docs/stats.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ appears beside the listed method.
* Multi-way
* {meth}`~TreeSequence.divergence`
* {meth}`~TreeSequence.genetic_relatedness`
{meth}`~TreeSequence.genetic_relatedness_weighted`
* {meth}`~TreeSequence.f4`
{meth}`~TreeSequence.f3`
{meth}`~TreeSequence.f2`
Expand Down Expand Up @@ -593,6 +594,12 @@ and boolean expressions (e.g., {math}`(x > 0)`) are interpreted as 0/1.
where {math}`m = \frac{1}{n}\sum_{k=1}^n x_k` with {math}`n` the total number
of samples.

`genetic_relatedness_weighted`
: {math}`f(w_i, w_j, x_i, x_j) = \frac{1}{2}(x_i - w_i m) (x_j - w_j m)`,

where {math}`m = \frac{1}{n}\sum_{k=1}^n x_k` with {math}`n` the total number
of samples, and {math}`w_j = \sum_{k=1}^n W_kj` is the sum of the weights in the {math}`j`th column of the weight matrix.

`Y2`
: {math}`f(x_1, x_2) = \frac{x_1 (n_2 - x_2) (n_2 - x_2 - 1)}{n_1 n_2 (n_2 - 1)}`

Expand Down
8 changes: 7 additions & 1 deletion python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

**Features**

- Add ``TreeSequence.impute_unknown_mutations_time`` method to return an array of mutation times based on the times of associated nodes (:user:`duncanMR`, :pr:`2760`, :issue:`2758`)
- Add ``TreeSequence.genetic_relatedness_weighted`` stats method.
(:user:`petrelharp`, :user:`brieuclehmann`, :user:`jeromekelleher`,
:pr:`2785`, :pr:`1246`)

- Add ``TreeSequence.impute_unknown_mutations_time`` method to return an
array of mutation times based on the times of associated nodes
(:user:`duncanMR`, :pr:`2760`, :issue:`2758`)

- Add ``asdict`` to all dataclasses. These are returned when you access a row or
other tree sequence object. (:user:`benjeffery`, :pr:`2759`, :issue:`2719`)
Expand Down
Loading