|
| 1 | +from typing import Hashable |
| 2 | + |
| 3 | +import dask.array as da |
| 4 | +import numpy as np |
| 5 | +import xarray as xr |
| 6 | +from xarray import DataArray, Dataset |
| 7 | + |
| 8 | +from .aggregation import count_variant_alleles |
| 9 | + |
| 10 | + |
| 11 | +def diversity( |
| 12 | + ds: Dataset, allele_counts: Hashable = "variant_allele_count", |
| 13 | +) -> DataArray: |
| 14 | + """Compute diversity from allele counts. |
| 15 | +
|
| 16 | + Because we're not providing any arguments on windowing, etc, |
| 17 | + we return the total over the whole region. Maybe this isn't |
| 18 | + the behaviour we want, but it's a starting point. Note that |
| 19 | + this is different to the tskit default behaviour where we |
| 20 | + normalise by the size of windows so that results |
| 21 | + in different windows are comparable. However, we don't have |
| 22 | + any information about the overall length of the sequence here |
| 23 | + so we can't normalise by it. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + ds : Dataset |
| 28 | + Genotype call dataset. |
| 29 | + allele_counts : Hashable |
| 30 | + allele counts to use or calculate. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + DataArray |
| 35 | + diversity value. |
| 36 | + """ |
| 37 | + if len(ds.samples) < 2: |
| 38 | + return xr.DataArray(np.nan) |
| 39 | + if allele_counts not in ds: |
| 40 | + ds = count_variant_alleles(ds) |
| 41 | + ac = ds[allele_counts] |
| 42 | + an = ac.sum(axis=1) |
| 43 | + n_pairs = an * (an - 1) / 2 |
| 44 | + n_same = (ac * (ac - 1) / 2).sum(axis=1) |
| 45 | + n_diff = n_pairs - n_same |
| 46 | + pi = n_diff / n_pairs |
| 47 | + return pi.sum() # type: ignore[no-any-return] |
| 48 | + |
| 49 | + |
| 50 | +def divergence( |
| 51 | + ds1: Dataset, ds2: Dataset, allele_counts: Hashable = "variant_allele_count", |
| 52 | +) -> DataArray: |
| 53 | + """Compute divergence between two genotype call datasets. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + ds1 : Dataset |
| 58 | + Genotype call dataset. |
| 59 | + ds2 : Dataset |
| 60 | + Genotype call dataset. |
| 61 | + allele_counts : Hashable |
| 62 | + allele counts to use or calculate. |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + DataArray |
| 67 | + divergence value between the two datasets. |
| 68 | + """ |
| 69 | + if allele_counts not in ds1: |
| 70 | + ds1 = count_variant_alleles(ds1) |
| 71 | + ac1 = ds1[allele_counts] |
| 72 | + if allele_counts not in ds2: |
| 73 | + ds2 = count_variant_alleles(ds2) |
| 74 | + ac2 = ds2[allele_counts] |
| 75 | + an1 = ds1[allele_counts].sum(axis=1) |
| 76 | + an2 = ds2[allele_counts].sum(axis=1) |
| 77 | + |
| 78 | + n_pairs = an1 * an2 |
| 79 | + n_same = (ac1 * ac2).sum(axis=1) |
| 80 | + n_diff = n_pairs - n_same |
| 81 | + div = n_diff / n_pairs |
| 82 | + return div.sum() # type: ignore[no-any-return] |
| 83 | + |
| 84 | + |
| 85 | +def Fst( |
| 86 | + ds1: Dataset, ds2: Dataset, allele_counts: Hashable = "variant_allele_count", |
| 87 | +) -> DataArray: |
| 88 | + """Compute Fst between two genotype call datasets. |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + ds1 : Dataset |
| 93 | + Genotype call dataset. |
| 94 | + ds2 : Dataset |
| 95 | + Genotype call dataset. |
| 96 | + allele_counts : Hashable |
| 97 | + allele counts to use or calculate. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + DataArray |
| 102 | + fst value between the two datasets. |
| 103 | + """ |
| 104 | + total_div = diversity(ds1) + diversity(ds2) |
| 105 | + gs = divergence(ds1, ds2) |
| 106 | + den = total_div + 2 * gs # type: ignore[operator] |
| 107 | + fst = 1 - (2 * total_div / den) |
| 108 | + return fst # type: ignore[no-any-return] |
| 109 | + |
| 110 | + |
| 111 | +def Tajimas_D( |
| 112 | + ds: Dataset, allele_counts: Hashable = "variant_allele_count", |
| 113 | +) -> DataArray: |
| 114 | + """Compute Tajimas' D for a genotype call dataset. |
| 115 | +
|
| 116 | + Parameters |
| 117 | + ---------- |
| 118 | + ds : Dataset |
| 119 | + Genotype call dataset. |
| 120 | + allele_counts : Hashable |
| 121 | + allele counts to use or calculate. |
| 122 | +
|
| 123 | + Returns |
| 124 | + ------- |
| 125 | + DataArray |
| 126 | + Tajimas' D value. |
| 127 | + """ |
| 128 | + if allele_counts not in ds: |
| 129 | + ds = count_variant_alleles(ds) |
| 130 | + ac = ds[allele_counts] |
| 131 | + |
| 132 | + # count segregating |
| 133 | + S = ((ac > 0).sum(axis=1) > 1).sum() |
| 134 | + |
| 135 | + # assume number of chromosomes sampled is constant for all variants |
| 136 | + n = ac.sum(axis=1).max() |
| 137 | + |
| 138 | + # (n-1)th harmonic number |
| 139 | + a1 = (1 / da.arange(1, n)).sum() |
| 140 | + |
| 141 | + # calculate Watterson's theta (absolute value) |
| 142 | + theta = S / a1 |
| 143 | + |
| 144 | + # calculate diversity |
| 145 | + div = diversity(ds) |
| 146 | + |
| 147 | + # N.B., both theta estimates are usually divided by the number of |
| 148 | + # (accessible) bases but here we want the absolute difference |
| 149 | + d = div - theta |
| 150 | + |
| 151 | + # calculate the denominator (standard deviation) |
| 152 | + a2 = (1 / (da.arange(1, n) ** 2)).sum() |
| 153 | + b1 = (n + 1) / (3 * (n - 1)) |
| 154 | + b2 = 2 * (n ** 2 + n + 3) / (9 * n * (n - 1)) |
| 155 | + c1 = b1 - (1 / a1) |
| 156 | + c2 = b2 - ((n + 2) / (a1 * n)) + (a2 / (a1 ** 2)) |
| 157 | + e1 = c1 / a1 |
| 158 | + e2 = c2 / (a1 ** 2 + a2) |
| 159 | + d_stdev = np.sqrt((e1 * S) + (e2 * S * (S - 1))) |
| 160 | + |
| 161 | + if d_stdev == 0: |
| 162 | + return xr.DataArray(np.nan) |
| 163 | + |
| 164 | + # finally calculate Tajima's D |
| 165 | + D = d / d_stdev |
| 166 | + return D # type: ignore[no-any-return] |
0 commit comments