You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! Thank you for making the pg_gpu tool. I am using version 0.1.1dev564 on a VCF-derived zarr store. From the code (diversity.py, and _ac_contribution) and the docs, allele code 0 (REF) is treated as ancestral and every non-zero code is derived. This affects the derived-allele statistics: theta_H, theta_L, Fay and Wu's H, etc. I could not find anything in the docs that reads an ancestral-allele field.
Is there a way to supply ancestral alleles, like an AA tag in the INFO field, which bio2zarr stores as variant_AA? If not, is the intended workflow to polarize the data before conversion, by swapping the REF and ALT at sites where the ancestral allele is known so that code 0 is ancestral? For sites with unknown ancestral state, is there a recommended way to leave them out of the derived allele statistics?
You've read the code right. Allele code 0 is treated as ancestral everywhere, and every other code as derived. Nothing reads an ancestral-allele field today, so there's no way to hand pg_gpu an AA tag and have it polarize for you.
So for now there are two ways to do it.
1. Polarize before conversion. If you rewrite the VCF so that REF is the ancestral allele (swap REF/ALT and recode the genotypes at the sites where the ancestral allele is ALT, and drop the sites where it's unknown), then code 0 is ancestral and everything downstream is right. This is the simplest route, and it's the only one that works with the streaming path.
2. Polarize in Python after loading.from_zarr can load INFO arrays next to the genotypes, and that includes variant_AA and the allele strings from a bio2zarr store:
importcupyascpimportnumpyasnpfrompg_gpuimportHaplotypeMatrixhm=HaplotypeMatrix.from_zarr("data.vcz", fields=["AA", "allele"])
aa=np.char.upper(hm.fields["AA"].astype(str))
alleles=np.char.upper(hm.fields["allele"].astype(str)) # column 0 is REFanc_is_ref=aa==alleles[:, 0]
anc_is_alt=aa==alleles[:, 1]
known=anc_is_ref|anc_is_alt# False where AA is '.', 'N', etc.hap=cp.asnumpy(hm.haplotypes) # (n_haplotypes, n_variants), -1 = missingflip=np.where(anc_is_alt)[0]
sub=hap[:, flip]
hap[:, flip] =np.where(sub>=0, 1-sub, sub) # swap 0 and 1, keep missingpolarized=HaplotypeMatrix(
hap[:, known], cp.asnumpy(hm.positions)[known],
hm.chrom_start, hm.chrom_end,
sample_sets=hm.sample_sets, phased=hm.phased)
A few caveats on that recipe: it's for biallelic sites (a multiallelic site needs its codes remapped, not just swapped), fields= only works on the eager loader, and if your AA tag uses lower case for low-confidence calls you may want to treat those as unknown rather than upper-casing them like I did here.
Leaving out sites with unknown ancestral state. That's the known mask above. If you only want to drop sites and not flip anything, hm.filter(variants=known) does it in one line.
Following up: this is now built in on main (#448), so the recipe above is no longer needed.
frompg_gpuimportHaplotypeMatrixhm=HaplotypeMatrix.from_zarr("data.vcz", ancestral="AA")
# or straight from the VCFhm=HaplotypeMatrix.from_vcf("data.vcf.gz", ancestral="AA")
ancestral= takes the name of any INFO tag. Each site is recoded so that allele code 0 is the allele the tag names, which is what theta_h, theta_l, Fay and Wu's H and the unfolded SFS read as ancestral. A few things worth knowing:
Sites where the tag gives no usable state (., N, or anything that isn't one of the site's alleles) are dropped, and you get an UnknownAncestralWarning with the count. That answers your last question: they're left out for you.
Lower-case values (the usual mark for a low-confidence call) count as known. Pass ancestral_lowercase_known=False to drop those too.
Values in the Ensembl form (A|||) are handled.
Multiallelic sites work: the ancestral allele's code and code 0 change places and the other codes stay put.
from_zarr needs a store made by vcf_to_zarr (bio2zarr), since that is what carries variant_AA and the allele strings. It works for stores too big to load at once as well.
One caveat for now: pg_gpu doesn't yet keep track of whether a matrix has been polarized, so nothing warns you if you run a derived-allele statistic on data loaded without ancestral=. That part is tracked in #439.
If you already have the ancestral alleles as an array, hm.polarize(...) does the same recode on a loaded matrix.
Activity
andrewkern commentedon Nov 17, 2026
Hi, and thanks for trying pg_gpu!
You've read the code right. Allele code 0 is treated as ancestral everywhere, and every other code as derived. Nothing reads an ancestral-allele field today, so there's no way to hand pg_gpu an
AAtag and have it polarize for you.So for now there are two ways to do it.
1. Polarize before conversion. If you rewrite the VCF so that REF is the ancestral allele (swap REF/ALT and recode the genotypes at the sites where the ancestral allele is ALT, and drop the sites where it's unknown), then code 0 is ancestral and everything downstream is right. This is the simplest route, and it's the only one that works with the streaming path.
2. Polarize in Python after loading.
from_zarrcan load INFO arrays next to the genotypes, and that includesvariant_AAand the allele strings from a bio2zarr store:A few caveats on that recipe: it's for biallelic sites (a multiallelic site needs its codes remapped, not just swapped),
fields=only works on the eager loader, and if yourAAtag uses lower case for low-confidence calls you may want to treat those as unknown rather than upper-casing them like I did here.Leaving out sites with unknown ancestral state. That's the
knownmask above. If you only want to drop sites and not flip anything,hm.filter(variants=known)does it in one line.andrewkern commentedon Nov 20, 2026
Following up: this is now built in on
main(#448), so the recipe above is no longer needed.ancestral=takes the name of any INFO tag. Each site is recoded so that allele code 0 is the allele the tag names, which is whattheta_h,theta_l, Fay and Wu's H and the unfolded SFS read as ancestral. A few things worth knowing:.,N, or anything that isn't one of the site's alleles) are dropped, and you get anUnknownAncestralWarningwith the count. That answers your last question: they're left out for you.ancestral_lowercase_known=Falseto drop those too.A|||) are handled.from_zarrneeds a store made byvcf_to_zarr(bio2zarr), since that is what carriesvariant_AAand the allele strings. It works for stores too big to load at once as well.One caveat for now: pg_gpu doesn't yet keep track of whether a matrix has been polarized, so nothing warns you if you run a derived-allele statistic on data loaded without
ancestral=. That part is tracked in #439.If you already have the ancestral alleles as an array,
hm.polarize(...)does the same recode on a loaded matrix.