|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from hypothesis import given, settings |
| 4 | +from hypothesis.extra.numpy import arrays |
| 5 | +from statsmodels.multivariate.pca import PCA |
| 6 | + |
| 7 | +from sgkit.stats.pc_relate import ( |
| 8 | + _collapse_ploidy, |
| 9 | + _impute_genotype_call_with_variant_mean, |
| 10 | + gramian, |
| 11 | + pc_relate, |
| 12 | +) |
| 13 | +from sgkit.testing import simulate_genotype_call_dataset |
| 14 | + |
| 15 | + |
| 16 | +def test_pc_relate__genotype_inputs_checks() -> None: |
| 17 | + g_wrong_ploidy = simulate_genotype_call_dataset(100, 10, n_ploidy=3) |
| 18 | + with pytest.raises(ValueError, match="PC Relate only works for diploid genotypes"): |
| 19 | + pc_relate(g_wrong_ploidy) |
| 20 | + |
| 21 | + g_non_biallelic = simulate_genotype_call_dataset(100, 10, n_allele=3) |
| 22 | + with pytest.raises( |
| 23 | + ValueError, match="PC Relate only works for biallelic genotypes" |
| 24 | + ): |
| 25 | + pc_relate(g_non_biallelic) |
| 26 | + |
| 27 | + g_no_pcs = simulate_genotype_call_dataset(100, 10) |
| 28 | + with pytest.raises( |
| 29 | + ValueError, match="Input dataset must contain sample_pcs variable" |
| 30 | + ): |
| 31 | + pc_relate(g_no_pcs) |
| 32 | + |
| 33 | + with pytest.raises(ValueError, match="Input dataset must contain call_genotype"): |
| 34 | + pc_relate(g_no_pcs.drop_vars("call_genotype")) |
| 35 | + |
| 36 | + with pytest.raises( |
| 37 | + ValueError, match="Input dataset must contain call_genotype_mask" |
| 38 | + ): |
| 39 | + pc_relate(g_no_pcs.drop_vars("call_genotype_mask")) |
| 40 | + |
| 41 | + |
| 42 | +def test_pc_relate__maf_inputs_checks() -> None: |
| 43 | + g = simulate_genotype_call_dataset(100, 10) |
| 44 | + with pytest.raises(ValueError, match=r"MAF must be between \(0.0, 1.0\)"): |
| 45 | + pc_relate(g, maf=-1) |
| 46 | + with pytest.raises(ValueError, match=r"MAF must be between \(0.0, 1.0\)"): |
| 47 | + pc_relate(g, maf=1.0) |
| 48 | + with pytest.raises(ValueError, match=r"MAF must be between \(0.0, 1.0\)"): |
| 49 | + pc_relate(g, maf=0.0) |
| 50 | + |
| 51 | + |
| 52 | +@given(arrays(np.int8, (3, 5))) |
| 53 | +@settings(max_examples=10) |
| 54 | +def test_gramian_is_symmetric(a: np.ndarray) -> None: |
| 55 | + b = gramian(a) |
| 56 | + assert np.allclose(b, b.T) |
| 57 | + |
| 58 | + |
| 59 | +def test_collapse_ploidy() -> None: |
| 60 | + g = simulate_genotype_call_dataset(1000, 10, missing_pct=0.1) |
| 61 | + assert g.call_genotype.shape == (1000, 10, 2) |
| 62 | + assert g.call_genotype_mask.shape == (1000, 10, 2) |
| 63 | + |
| 64 | + # Sprinkle some tests data, this is a bit verbose, but in tests verbosity is not bad: |
| 65 | + g.call_genotype.loc[dict(variants=1, samples=1, ploidy=0)] = 1 |
| 66 | + g.call_genotype.loc[dict(variants=1, samples=1, ploidy=1)] = 1 |
| 67 | + g.call_genotype_mask.loc[dict(variants=1, samples=1, ploidy=0)] = 0 |
| 68 | + g.call_genotype_mask.loc[dict(variants=1, samples=1, ploidy=1)] = 0 |
| 69 | + |
| 70 | + g.call_genotype.loc[dict(variants=2, samples=2, ploidy=0)] = 0 |
| 71 | + g.call_genotype.loc[dict(variants=2, samples=2, ploidy=1)] = 1 |
| 72 | + g.call_genotype_mask.loc[dict(variants=2, samples=2, ploidy=0)] = 0 |
| 73 | + g.call_genotype_mask.loc[dict(variants=2, samples=2, ploidy=1)] = 0 |
| 74 | + |
| 75 | + g.call_genotype.loc[dict(variants=3, samples=3, ploidy=0)] = -1 |
| 76 | + g.call_genotype.loc[dict(variants=3, samples=3, ploidy=1)] = 1 |
| 77 | + g.call_genotype_mask.loc[dict(variants=3, samples=3, ploidy=0)] = 1 |
| 78 | + g.call_genotype_mask.loc[dict(variants=3, samples=3, ploidy=1)] = 0 |
| 79 | + |
| 80 | + call_g, call_g_mask = _collapse_ploidy(g) |
| 81 | + assert call_g.shape == (1000, 10) |
| 82 | + assert call_g_mask.shape == (1000, 10) |
| 83 | + assert call_g.isel(variants=1, samples=1) == 2 |
| 84 | + assert call_g.isel(variants=2, samples=2) == 1 |
| 85 | + assert call_g.isel(variants=3, samples=3) == -1 |
| 86 | + assert call_g_mask.isel(variants=1, samples=1) == 0 |
| 87 | + assert call_g_mask.isel(variants=3, samples=3) == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_impute_genotype_call_with_variant_mean() -> None: |
| 91 | + g = simulate_genotype_call_dataset(1000, 10, missing_pct=0.1) |
| 92 | + call_g, call_g_mask = _collapse_ploidy(g) |
| 93 | + # Sprinkle some tests data |
| 94 | + call_g.loc[dict(variants=2)] = 1 |
| 95 | + call_g.loc[dict(variants=2, samples=1)] = 2 |
| 96 | + call_g_mask.loc[dict(variants=2)] = False |
| 97 | + call_g_mask.loc[dict(variants=2, samples=[0, 9])] = True |
| 98 | + imputed_call_g = _impute_genotype_call_with_variant_mean(call_g, call_g_mask) |
| 99 | + assert imputed_call_g.isel(variants=2, samples=1) == 2 |
| 100 | + assert (imputed_call_g.isel(variants=2, samples=slice(2, 9)) == 1).all() |
| 101 | + assert (imputed_call_g.isel(variants=2, samples=[0, 9]) == (7 + 2) / 8).all() |
| 102 | + |
| 103 | + |
| 104 | +def test_pc_relate__values_within_range() -> None: |
| 105 | + n_samples = 100 |
| 106 | + g = simulate_genotype_call_dataset(1000, n_samples) |
| 107 | + call_g, _ = _collapse_ploidy(g) |
| 108 | + pcs = PCA(call_g, ncomp=2).loadings |
| 109 | + g["sample_pcs"] = (("components", "samples"), pcs.T) |
| 110 | + phi = pc_relate(g) |
| 111 | + assert phi.pc_relate_phi.shape == (n_samples, n_samples) |
| 112 | + data_np = phi.pc_relate_phi.data.compute() # to be able to use fancy indexing below |
| 113 | + upper_phi = data_np[np.triu_indices_from(data_np, 1)] |
| 114 | + assert (upper_phi > -0.5).all() and (upper_phi < 0.5).all() |
| 115 | + |
| 116 | + |
| 117 | +def test_pc_relate__identical_sample_should_be_05() -> None: |
| 118 | + n_samples = 100 |
| 119 | + g = simulate_genotype_call_dataset(1000, n_samples, missing_pct=0.1) |
| 120 | + call_g, _ = _collapse_ploidy(g) |
| 121 | + pcs = PCA(call_g, ncomp=2).loadings |
| 122 | + g["sample_pcs"] = (("components", "samples"), pcs.T) |
| 123 | + # add identical sample |
| 124 | + g.call_genotype.loc[dict(samples=8)] = g.call_genotype.isel(samples=0) |
| 125 | + phi = pc_relate(g) |
| 126 | + assert phi.pc_relate_phi.shape == (n_samples, n_samples) |
| 127 | + assert np.allclose(phi.pc_relate_phi.isel(sample_x=8, sample_y=0), 0.5, atol=0.1) |
0 commit comments