-
Notifications
You must be signed in to change notification settings - Fork 35
[WIP] Popgen stats #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[WIP] Popgen stats #100
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ pytest-datadir | |
hypothesis | ||
statsmodels | ||
zarr | ||
msprime | ||
sphinx | ||
sphinx_rtd_theme |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
from typing import Hashable | ||
|
||
import dask.array as da | ||
import numpy as np | ||
import xarray as xr | ||
from xarray import DataArray, Dataset | ||
|
||
from .aggregation import count_variant_alleles | ||
|
||
|
||
def diversity( | ||
ds: Dataset, allele_counts: Hashable = "variant_allele_count", | ||
) -> DataArray: | ||
"""Compute diversity from allele counts. | ||
|
||
Because we're not providing any arguments on windowing, etc, | ||
we return the total over the whole region. Maybe this isn't | ||
the behaviour we want, but it's a starting point. Note that | ||
this is different to the tskit default behaviour where we | ||
normalise by the size of windows so that results | ||
in different windows are comparable. However, we don't have | ||
any information about the overall length of the sequence here | ||
so we can't normalise by it. | ||
|
||
Parameters | ||
---------- | ||
ds : Dataset | ||
Genotype call dataset. | ||
allele_counts : Hashable | ||
allele counts to use or calculate. | ||
|
||
Returns | ||
------- | ||
DataArray | ||
diversity value. | ||
""" | ||
if len(ds.samples) < 2: | ||
return xr.DataArray(np.nan) | ||
if allele_counts not in ds: | ||
ds_new = count_variant_alleles(ds) | ||
else: | ||
ds_new = ds | ||
ac = ds_new[allele_counts] | ||
an = ac.sum(axis=1) | ||
n_pairs = an * (an - 1) / 2 | ||
n_same = (ac * (ac - 1) / 2).sum(axis=1) | ||
n_diff = n_pairs - n_same | ||
pi = n_diff / n_pairs | ||
return pi.sum() # type: ignore[no-any-return] | ||
|
||
|
||
def divergence( | ||
ds1: Dataset, ds2: Dataset, allele_counts: Hashable = "variant_allele_count", | ||
) -> DataArray: | ||
"""Compute divergence between two genotype call datasets. | ||
|
||
Parameters | ||
---------- | ||
ds1 : Dataset | ||
Genotype call dataset. | ||
ds2 : Dataset | ||
Genotype call dataset. | ||
allele_counts : Hashable | ||
allele counts to use or calculate. | ||
|
||
Returns | ||
------- | ||
DataArray | ||
divergence value between the two datasets. | ||
""" | ||
if allele_counts not in ds1: | ||
ds1_new = count_variant_alleles(ds1) | ||
else: | ||
ds1_new = ds1 | ||
ac1 = ds1_new[allele_counts] | ||
if allele_counts not in ds2: | ||
ds2_new = count_variant_alleles(ds2) | ||
else: | ||
ds2_new = ds2 | ||
ac2 = ds2_new[allele_counts] | ||
an1 = ds1_new[allele_counts].sum(axis=1) | ||
an2 = ds2_new[allele_counts].sum(axis=1) | ||
|
||
n_pairs = an1 * an2 | ||
n_same = (ac1 * ac2).sum(axis=1) | ||
n_diff = n_pairs - n_same | ||
div = n_diff / n_pairs | ||
return div.sum() # type: ignore[no-any-return] | ||
|
||
|
||
def Fst( | ||
ds1: Dataset, ds2: Dataset, allele_counts: Hashable = "variant_allele_count", | ||
) -> DataArray: | ||
"""Compute Fst between two genotype call datasets. | ||
|
||
Parameters | ||
---------- | ||
ds1 : Dataset | ||
Genotype call dataset. | ||
ds2 : Dataset | ||
Genotype call dataset. | ||
allele_counts : Hashable | ||
allele counts to use or calculate. | ||
|
||
Returns | ||
------- | ||
DataArray | ||
fst value between the two datasets. | ||
""" | ||
total_div = diversity(ds1) + diversity(ds2) | ||
gs = divergence(ds1, ds2) | ||
den = total_div + 2 * gs # type: ignore[operator] | ||
fst = 1 - (2 * total_div / den) | ||
return fst # type: ignore[no-any-return] | ||
|
||
|
||
def Tajimas_D( | ||
ds: Dataset, allele_counts: Hashable = "variant_allele_count", | ||
) -> DataArray: | ||
"""Compute Tajimas' D for a genotype call dataset. | ||
|
||
Parameters | ||
---------- | ||
ds : Dataset | ||
Genotype call dataset. | ||
allele_counts : Hashable | ||
allele counts to use or calculate. | ||
|
||
Returns | ||
------- | ||
DataArray | ||
Tajimas' D value. | ||
""" | ||
if allele_counts not in ds: | ||
ds_new = count_variant_alleles(ds) | ||
else: | ||
ds_new = ds | ||
ac = ds_new[allele_counts] | ||
|
||
# count segregating | ||
S = ((ac > 0).sum(axis=1) > 1).sum() | ||
|
||
# assume number of chromosomes sampled is constant for all variants | ||
n = ac.sum(axis=1).max() | ||
|
||
# (n-1)th harmonic number | ||
a1 = (1 / da.arange(1, n)).sum() | ||
|
||
# calculate Watterson's theta (absolute value) | ||
theta = S / a1 | ||
|
||
# calculate diversity | ||
div = diversity(ds_new) | ||
|
||
# N.B., both theta estimates are usually divided by the number of | ||
# (accessible) bases but here we want the absolute difference | ||
d = div - theta | ||
|
||
# calculate the denominator (standard deviation) | ||
a2 = (1 / (da.arange(1, n) ** 2)).sum() | ||
b1 = (n + 1) / (3 * (n - 1)) | ||
b2 = 2 * (n ** 2 + n + 3) / (9 * n * (n - 1)) | ||
c1 = b1 - (1 / a1) | ||
c2 = b2 - ((n + 2) / (a1 * n)) + (a2 / (a1 ** 2)) | ||
e1 = c1 / a1 | ||
e2 = c2 / (a1 ** 2 + a2) | ||
d_stdev = np.sqrt((e1 * S) + (e2 * S * (S - 1))) | ||
|
||
if d_stdev == 0: | ||
return xr.DataArray(np.nan) | ||
|
||
# finally calculate Tajima's D | ||
D = d / d_stdev | ||
return D # type: ignore[no-any-return] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import msprime # type: ignore | ||
import numpy as np | ||
import pytest | ||
|
||
from sgkit import Fst, Tajimas_D, create_genotype_call_dataset, divergence, diversity | ||
|
||
|
||
def ts_to_dataset(ts, samples=None): | ||
""" | ||
Convert the specified tskit tree sequence into an sgkit dataset. | ||
Note this just generates haploids for now. With msprime 1.0, we'll be | ||
able to generate diploid/whatever-ploid individuals easily. | ||
""" | ||
if samples is None: | ||
samples = ts.samples() | ||
tables = ts.dump_tables() | ||
alleles = [] | ||
genotypes = [] | ||
for var in ts.variants(samples=samples): | ||
alleles.append(var.alleles) | ||
genotypes.append(var.genotypes) | ||
alleles = np.array(alleles).astype("S") | ||
genotypes = np.expand_dims(genotypes, axis=2) | ||
|
||
df = create_genotype_call_dataset( | ||
variant_contig_names=["1"], | ||
variant_contig=np.zeros(len(tables.sites), dtype=int), | ||
variant_position=tables.sites.position.astype(int), | ||
variant_alleles=alleles, | ||
sample_id=np.array([f"tsk_{u}" for u in samples]).astype("U"), | ||
call_genotype=genotypes, | ||
) | ||
return df | ||
|
||
|
||
@pytest.mark.parametrize("size", [2, 3, 10, 100]) | ||
def test_diversity(size): | ||
ts = msprime.simulate(size, length=100, mutation_rate=0.05, random_seed=42) | ||
ds = ts_to_dataset(ts) # type: ignore[no-untyped-call] | ||
div = diversity(ds).compute() | ||
ts_div = ts.diversity(span_normalise=False) | ||
np.testing.assert_allclose(div, ts_div) | ||
|
||
|
||
@pytest.mark.parametrize("size", [2, 3, 10, 100]) | ||
def test_divergence(size): | ||
ts = msprime.simulate(size, length=100, mutation_rate=0.05, random_seed=42) | ||
subset_1 = ts.samples()[: ts.num_samples // 2] | ||
subset_2 = ts.samples()[ts.num_samples // 2 :] | ||
ds1 = ts_to_dataset(ts, subset_1) # type: ignore[no-untyped-call] | ||
ds2 = ts_to_dataset(ts, subset_2) # type: ignore[no-untyped-call] | ||
div = divergence(ds1, ds2).compute() | ||
ts_div = ts.divergence([subset_1, subset_2], span_normalise=False) | ||
np.testing.assert_allclose(div, ts_div) | ||
|
||
|
||
@pytest.mark.parametrize("size", [2, 3, 10, 100]) | ||
def test_Fst(size): | ||
ts = msprime.simulate(size, length=100, mutation_rate=0.05, random_seed=42) | ||
subset_1 = ts.samples()[: ts.num_samples // 2] | ||
subset_2 = ts.samples()[ts.num_samples // 2 :] | ||
ds1 = ts_to_dataset(ts, subset_1) # type: ignore[no-untyped-call] | ||
ds2 = ts_to_dataset(ts, subset_2) # type: ignore[no-untyped-call] | ||
fst = Fst(ds1, ds2).compute() | ||
ts_fst = ts.Fst([subset_1, subset_2]) | ||
np.testing.assert_allclose(fst, ts_fst) | ||
|
||
|
||
@pytest.mark.parametrize("size", [2, 3, 10, 100]) | ||
def test_Tajimas_D(size): | ||
ts = msprime.simulate(size, length=100, mutation_rate=0.05, random_seed=42) | ||
ds = ts_to_dataset(ts) # type: ignore[no-untyped-call] | ||
ts_d = ts.Tajimas_D() | ||
d = Tajimas_D(ds).compute() | ||
np.testing.assert_allclose(d, ts_d) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.