Hl.export_bgen makes all genotypes missing?

Hi,

When I export a matrix table to a bgen (something like hl.export_bgen(mt_filtered, bgen_path)), the resulting genotypes in the bgen are all missing. Would this be because I do not have GP entries in the matrix table? Is there a way to use GT entries and export hard-called genotypes to a bgen file?

Many thanks for your help!

You can specify whatever you want for the GP field, so long as it is an array of floats:

hl.export_bgen(mt_filtered, bgen_path, gp = ...)

Documented here: Hail | Import / Export

How do you want the to compute the probabilities from the genotypes? Just 0s and 1s?

Ah, thanks for the quick reply. Should have read the doc more carefully. I was able to generate GP entries with 0s and 1s using GT, which seems to work, unless there’s a cleaner way of doing this?

mt_filtered = mt_filtered.annotate_entries(
	GP = hl.case()
	.when(mt_filtered.GT.is_hom_ref(),hl.literal([1.00,0.00,0.00]))
	.when(mt_filtered.GT.is_het(),hl.literal([0.00,1.00,0.00]))
	.when(mt_filtered.GT.is_hom_var(),hl.literal([0.00,0.00,1.00]))
	.or_missing()
)

hl.export_bgen(mt_filtered, bgen_path)

I think your solution is splendid. I’d probably do this:

GPs = hl.literal([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
mt_filtered = mt_filtered.annotate_entries(
    GP = GPs[mt_filtered.GT.n_alt_alleles()]
)

Is this the same thing you’d get from doing mt_filtered.GT.one_hot_alleles(3)?

That’s a bit different. For autosomal het that’s [1, 1] indicating one copy of each allele.