`hl.eval` errors out on a StringExpression

Trying to extract sample ID from a VCF file imported as a matrix table. I did so by accessing the column key,

vcf_mt = hl.import_vcf(‘./test.vcf’)
sample = vcf_mt.s

I want to use hl.eval() on the above variable sample but it errors out.

>>> sample
<StringExpression of type str>
>>> sample.show()
| s       |
| str     |
| "test" |
>>> hl.eval(sample)
hail.expr.expressions.base_expression.ExpressionException: scope violation: 'eval' expects an expression indexed by []
    Found indices ['column'], with unexpected indices ['column']. Invalid fields:
        's' (indices ['column'])

I understand the variable sample has an index, but how do I evaluate this StringExpression?

I’m basically trying to parse the sample ID from the matrix table.


Instead of hl.eval(), I think you would rather want to use sample.collect(), which will give you a list with all the sample IDs.


@simeoncarstens is right; you basically never want to use hl.eval unless you’re playing with expressions e.g.:

x = hl.literal(hl.dict([(1, 'a'), (10, 'b')])
get_one = x[1]

x.field.collect() or x.aggregate(hl.agg.collect(x.field)) are what you’re looking for.

1 Like

sample.collect() is exactly what I was looking for.

Thanks for the details on hl.eval!