Creating a Variant representation on a V0.2 Table


#1

I’m trying to create a Hail Table keyed on a variant so I can convert it to a matrix table. I have the chr, pos, alt, and ref fields, and must create a variant from this.

The equivalent v01 code is the following

.annotate("variant=Variant(chr, pos, ref, alt)").key_by('variant')

I believe v02 does not have the Variant class available anymore, and see that I can create a locus and an allele array and key by those two.

I tried

.annotate(locus=hl.Locus(ht.chr, ht.pos))
        .annotate(alleles=[ht.ref, ht.alt])
        .key_by('locus', 'alleles')

But have trouble creating the locus with this error:
TypeError: __init__: parameter 'contig': expected (str or int), found hail.expr.expressions.typed_expressions.StringExpression: <StringExpression of type str>

It looks like I have to pass in a literal value, but I only have access to the expression values and can’t find a way to convert it. What would you recommend?

Also, is the rest of the logic (creating an array of ref and alt alleles and keying on the locus-allele array pair the right idea to then import into a Matrix Table?

Thanks!


#2

I unblocked myself with the following hack:

ht = ht.key_by(**hl.parse_variant(ht.chr + hl.literal(':') + hl.str(ht.pos) + hl.literal(':') + ht.ref + hl.literal(':') + ht.alt))

I think this works? But would love to find out the right way to do it.


#3

That totally works. This would too:

key_by(locus = hl.locus(ht.chr, ht.pos), [ht.ref, ht.alt])

#4

Sweet! And there’s an alleles= in there too, right? That looks to work.


#5

oh, yeah, oops.