String to expression

Let say ‘sid’ is a sample annotation in my MatrixTable ‘mt’
I would like to write a code like this:

ind_id_path = 'mt.sid'
hl.export_plink(mt, 'path_to_output',  ind_id=ind_id_path)

However, I don’t know how to convert ‘ind_id_path’ from ‘string’ to ‘hail expression’
Is there a way to do so. Note that I would like to keep ‘ind_id_path’ as a variable string.


This would work:

field = 'sid'
hl.export_plink(mt, 'path_to_output',  ind_id=mt[field])

If you had nested fields:

from typing import List
import hail as hl

field_path = ['foo', 'bar', 'baz']

def lookup_field_path(expr, path: List[str]):
    for field in path:
        expr = expr[field]
    return expr

hl.export_plink(mt, '...', ind_id=lookup_field_path(mt, field_path))

Thanks Danking, That helps.