Repository navigation
Polarize allele codes by an ancestral-allele tag - #448
Conversation
pg_gpu reads allele code 0 as the ancestral allele, and a VCF gives code 0 to REF. HaplotypeMatrix.from_zarr and from_vcf take ancestral='AA' (an INFO tag) and recode each site so that code 0 is the allele the tag names; HaplotypeMatrix.polarize does the same for an array of ancestral alleles or allele codes. At a site whose ancestral allele has code a > 0, codes 0 and a change places, in one GPU kernel that serves biallelic and multiallelic sites. A site with no known ancestral allele is dropped with an UnknownAncestralWarning; the bounds, the mask and the span do not change. The streaming source drops those sites up front and the chunk builder recodes each block, so the streaming path gives the matrix of the eager one. No matrix records that it is polarized, and the genotype loaders do not take ancestral=. Part of #439
nspope
left a comment
There was a problem hiding this comment.
Looks good, two minor issues to fix
|
|
||
| pg_gpu reads allele code 0 as the ancestral allele, and a VCF gives code | ||
| 0 to REF. ``HaplotypeMatrix.from_zarr`` and ``HaplotypeMatrix.from_vcf`` | ||
| now take ``ancestral='AA'`` (any INFO tag) and recode each site so that |
There was a problem hiding this comment.
| now take ``ancestral='AA'`` (any INFO tag) and recode each site so that | |
| now take an ``ancestral`` argument (typically `'AA'`, but any INFO tag in the VCF may be used) and recode each site so that |
| ``variant_allele``, as ``vcf_to_zarr`` writes, and works on the eager | ||
| and the streaming path. A store from ``to_zarr`` has neither array and | ||
| raises. | ||
| * No matrix records that it is polarized, and no statistic warns on data |
There was a problem hiding this comment.
| * No matrix records that it is polarized, and no statistic warns on data | |
| * The ancestral state must be set explicitly for polarized statistics to be correct. No matrix records that it is polarized, and no statistic warns on data |
| state (``theta_h``, ``theta_l``, Fay and Wu's H, Zeng's E, ``max_daf``, | ||
| ``daf_histogram``, the unfolded ``sfs`` and ``joint_sfs``, and the sign of | ||
| ``ihs`` and ``nsl``) depend on it. A VCF gives code 0 to REF; load with | ||
| ``ancestral='AA'`` (``HaplotypeMatrix.from_vcf`` and ``from_zarr``) to |
There was a problem hiding this comment.
| ``ancestral='AA'`` (``HaplotypeMatrix.from_vcf`` and ``from_zarr``) to | |
| the ``ancestral='INFO tag'`` (for example `'AA'`) argument to ``HaplotypeMatrix.from_vcf`` and ``from_zarr`` to |
| The ancestral allele of each site: either integer allele codes | ||
| (-1 for unknown), or allele strings as a VCF ``AA`` tag gives | ||
| them, which need ``alleles``. It is aligned with the | ||
| underlying variant axis, as ``fields`` are, not with the view |
There was a problem hiding this comment.
"It is aligned with the underlying variant axis" could be stated more plainly, like "The length is expected to match the number of variants in the HaplotypeMatrix, not the view (subset) of variants from applying an accessible mask"
| if haps.dtype != cp.int8 or not haps.flags.c_contiguous: | ||
| raise ValueError("haps must be a C-contiguous int8 array") | ||
| n_var = haps.shape[1] | ||
| codes = cp.ascontiguousarray(cp.asarray(codes, dtype=cp.int8)) |
There was a problem hiding this comment.
this is casting to int8 without a range check. The callers (that I can see) do a check before invoking it, but it'd be safer to check here. Otherwise this function could silently give garbage via overflow.
| read_ancestral_codes(store, ancestral, | ||
| data.get('variant_indices'), | ||
| ancestral_lowercase_known), | ||
| context=f"from_zarr(ancestral={ancestral!r})") |
There was a problem hiding this comment.
Say the store read above (lines 1000-1004) failed (filesystem issues, or whatever). Then store is None. But, read_ancestral_codes doesn't handle that gracefully, so you'll get an obscure error. For example,
from pg_gpu.zarr_io import read_ancestral_codes
store = None
read_ancestral_codes(store, "AA")
# TypeError: argument of type 'NoneType' is not a container or iterableswap_ancestral checks that the codes are integers that fit int8 before the cast. The eager loader raises a clear error when ancestral= is given and the store does not open as a group. The changelog, the features page and the polarize docstring take the reviewer's wording.
Part of #439 (the first of the two PRs planned there). Asked for in #438.
The problem
pg_gpu reads allele code 0 as the ancestral allele. A VCF gives code 0 to REF, which is not the ancestral allele in general. Nothing read an ancestral-allele field, so the statistics of a derived state (the unfolded SFS,
theta_h,theta_l, Fay and Wu's H, Zeng's E,max_daf, the sign ofihsandnsl) took REF as ancestral, with no error.What this PR adds
HaplotypeMatrix.from_zarr(..., ancestral='AA')andHaplotypeMatrix.from_vcf(..., ancestral='AA'), for any INFO tag.from_zarrworks on the eager and the streaming path (host and kvikio).HaplotypeMatrix.polarize(ancestral, alleles=None), for an array that the caller has: integer allele codes, or allele strings matched against the site's alleles.How it works
zarr_io.ancestral_codesmatches the tag against the site's allele strings and gives one int8 per variant: the code of the ancestral allele, or -1 for unknown. The match ignores case. Of a value in the Ensembl form (A|||) the part before the first|counts.swap_ancestralis aRawKernel: at a site whose ancestral allele has codea > 0, codes 0 andachange places, and negative values (missing calls) stay. The same rule serves biallelic and multiallelic sites, and a multiallelic site keeps its other codes.UnknownAncestralWarningthat gives the count. A site is unknown when the value is empty or is none of the site's alleles (which covers.,N,-). All sites unknown raises, because that means a wrong tag or wrong alleles.ZarrGenotypeSource.set_ancestralreads the strings once (after the eager-or-streaming decision, not in the size probe), drops the unknown variants from the source, and keeps the source's bounds. The chunk builder recodes each block. The source's row range can then have gaps, so_zarr_row_rangealso returns the rows of the block that are the source's own.ancestral_lowercase_known=Falsemakes it unknown.fields['allele'], if the matrix has it, changes its columns with the codes, so the strings keep naming the codes and a second polarization by the same tag changes nothing.Limits, by design
polarizedflag yet. No matrix records that it is polarized, and no statistic warns on data that are not. That is the second PR. Until then, a secondpolarizecall with the same integer codes swaps the codes back; the docstring says so.ancestral=. I found no genotype statistic that reads the ancestral state.from_zarrneeds a VCZ store withvariant_<tag>andvariant_allele, asvcf_to_zarrwrites. A store fromto_zarror a scikit-allel store has neither and raises a clear error.from_vcfreads at most three ALT alleles per site (the reader's default), so a later ALT allele as the ancestral state counts as unknown.allele(AC,AF) keep their column order.Tests
tests/test_ancestral.py(new). A tree sequence is the truth: the fixtures write it to a VCF whose REF is a random allele of each site, with the true ancestral allele inAA(lower case at some sites, a placeholder at others).theta_h,theta_l, Fay and Wu's H, Zeng's E,max_dafandihsequal their values on the tree sequence. A control shows that the load with noancestral=gives a different matrix and SFS.polarize, and the errors.Full GPU suite: 3545 passed. Lint and the pipeline-map check pass.
Docs
Changelog, quickstart, the features page (it now says that code 0 is read as ancestral), the API page (the new warning), and the pipeline map.