Repository navigation
NaN values in two-locus (LD) statistics - #3515
apragsdale wants to merge 7 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3515 +/- ##
==========================================
+ Coverage 91.72% 91.73% +0.01%
==========================================
Files 37 37
Lines 30839 30893 +54
Branches 4921 4938 +17
==========================================
+ Hits 28286 28339 +53
Misses 2223 2223
- Partials 330 331 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
There are some corner cases in site mode with multi-allelic sites, in which the normalization weights in the two-way Consider the example below with four samples in two sample sets, with one site:
import tskit
tables = tskit.Tree.generate_star(4).tree_sequence.dump_tables()
tables.sites.add_row(position=0, ancestral_state="A")
tables.mutations.add_row(site=0, node=0, derived_state="C")
tables.mutations.add_row(site=0, node=2, derived_state="T")
ts = tables.tree_sequence()
ts.ld_matrix(sample_sets=[[0, 1], [2, 3]], indexes=(0, 1)) # two-way r2 of the site with itselfA is the only allele carried by both sample sets, so C and T are dropped (following rules laid out above). In each set, half the samples carry A, so r2 should be 1. r2 is a weighted average over alleles, and the weights add up to 1:
After dropping C and T, only 1/2 of the weight is left:
For one-way statistics (or two-way with the same sample set twice), a dropped allele always has weight 0, so dividing by the remaining weight doesn't change behavior as we'd divide by 1.. |
At multiallelic sites, alleles not carried by any sample in a sample set no longer contribute to that set's statistics or their normalisation. If either site is monomorphic in the sample set, the summary function is evaluated on the fixed allele, giving nan for ratio statistics and zero otherwise. Two-way statistics are unchanged.
For two-way statistics, an allele contributes to the result for a pair of sample sets only if it is carried by both sets, matching the one-way rule when the same set is passed twice. Each site-mode result is now divided by the total weight of the allele pairs used, so it is a weighted average over those pairs; this replaces counting the alleles used per result.
With no branches that can produce polymorphism, branch-mode statistics are an empty sum, so every statistic is zero.
Only alleles carried by both sample sets are now used for two-way statistics, which changes the expected values for disjoint sets.
|
Ping me (elsewhere) when I should have a look at this? |
Description
The ratio LD statistics (
r2,randD_prime) have terms likep(1-p)in the denominator, so when an allele has frequency 0 or 1, it results in a NaN value. In either site or branch mode, summing over that NaN leads to a whole result being NaN, which can be unexpected (see issues #3441, #3474).There are a number of ways this can happen:
ld_matrixoutput with "ghost" alleles #3474)ld_matrix#3441)The proposed desired behavior is that alleles and branches that do not or cannot contribute to polymorphism in a sample should not be considered and not affect results.
I tried to write out a number of "rules" (proposed) that the tests in this PR try to address. Note that these are just the tests. Working on fixes, but thought it'd be helpful to get feedback first:
Site mode
Branch mode
If no branches contribute (for example, a sample set with just one sample), every statistic returns NaN, since nothing is computed. This changes the behavior of non-ratio statistics, which currently return 0, but I think this change makes behavior more consistent across statistics.If no branches contribute in branch mode, we return zero. Since this is a sum, it indicates that we had nothing to compute, rather than a nan. It's also much cleaner, code-wise.Multi-population statistic
Some tricky situations/inconsistencies
randD_primereturn zero when a site doesn't carry a mutation, whiler2returns NaN. If we follow point 3 above, all should be NaN.Eventually.... this PR
Fixes #3441.
Fixes #3474
PR Checklist: