Skip to content

Polarization follow-ups: keep sites with unknown ancestral state, and a polarized flag on GenotypeMatrix #450

Open

Description

@andrewkern

Two things from the plan on #439 were left out of the two PRs that implemented it (#448 and #449). Recording them here so they are not lost when #439 closes.

1. An option to keep sites with no known ancestral allele

Today a recode (ancestral= on the loaders, or HaplotypeMatrix.polarize) drops every site whose ancestral allele is unknown and emits an UnknownAncestralWarning with the count. The accessible mask and the span stay as they were.

That is the right default for the derived-state statistics, but it costs something for everything else. A site with unknown ancestral state is still a perfectly good site for pi, FST or the folded SFS, and dropping it lowers per-base values by about the share of sites dropped (the warning says so). On some data that share is large.

What a "keep" option needs:

  • A per-site record on the matrix of which sites are polarized, carried through subsets the way the accessible mask index is.
  • The derived-state statistics skip the unmarked sites; everything else uses all of them. That touches the scalar functions, the scatter and fused kernels, and the streaming paths, so it is a real piece of work and not a flag.
  • A rule for the denominators: a per-base theta_h over the polarized sites should divide by the bases that could have been polarized, not by all accessible bases.
  • A decision on what polarized means for such a matrix. Probably: the flag stays True and the per-site record says where.

A cheaper intermediate, if the full version is too much: let the caller ask for the dropped sites to come off the accessible mask as well, so per-base statistics on the polarized matrix are at least self-consistent.

2. A polarized flag on GenotypeMatrix

HaplotypeMatrix and its stream now carry polarized, and the derived-state statistics warn without it. GenotypeMatrix has no such flag, and its loaders do not take ancestral=.

One genotype statistic reads the derived state: daf_histogram on a GenotypeMatrix counts the allele that the dosage counts, with no warning. I did not find another, but I only looked at what the swap experiment in tests/test_unpolarized_warning.py reaches, and that experiment runs on haplotype matrices.

What it needs:

  • ancestral= on GenotypeMatrix.from_vcf and from_zarr. The recode has to happen on the raw (n_variants, n_samples, ploidy) block before the dosage count, with the same swap_ancestral kernel; flipping to 2 - dosage afterwards is only right at biallelic sites. Note that the dosage counts the highest present allele above 0, so check what a recoded site with no copy of code 0 gives.
  • The flag on GenotypeMatrix and StreamingGenotypeMatrix, kept through subsets, saved by to_zarr, and carried by GenotypeMatrix.from_haplotype_matrix (which today drops it; to_haplotype_matrix gives False).
  • The same swap experiment on genotype matrices, to find every statistic that should warn rather than trusting the one I know about.

Smaller items in the same area

  • from_vcf reads at most three ALT alleles per site (the reader's default), so a fourth or later ALT as the ancestral state counts as unknown.
  • Per-allele fields other than allele (AC, AF) are not reordered by a recode.
  • A handful of statistics depend on code 0 only at sites with three or more alleles and do not warn (mu_sfs, eta1_star, minus_eta1_star, FrequencySpectrum with the pi weight). The docs name them; whether they should warn is open.
  • The windowed local_pca_jackknife values differed between the two multiallelic test matrices in that experiment while plain local_pca did not. Not explained yet.

Related: #439, #448, #449.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions