Pass outside variable in HQL expression


#1

I am having trouble to pass a python variable inside the HQL expression? Here’s an example:
The following query that works:
vds.query_samples(‘samples.map(s => sa.pheno.008 ).counter()’)

But I want to pass the code in as a variable in a loop, I couldn’t get it to work. I tried:
code="sa.pheno.008"
vds.query_samples(‘samples.map(s => code ).counter()’)

I also tried adding quotes, using the “let” syntax, etc. None of them works. I couldn’t find an example in the tutorial, either. Could you point me to the the correct way to do that?

Thanks!


#2

If the Python variable is a string or int or float, you can pass it directly into the string.

for example:

x = 5
vds.query_samples('samples.map(s => sa.pheno == {}).collect()'.format(x))

I think in this case, your Python variable is the name of the annotation. If your variable is "sa.pheno.`008`" then you can insert this with the same format syntax:

x = "sa.pheno.`008`"
vds.query_samples('samples.map(s => {}).counter()'.format(x))

#3

That works the magic, thanks a lot!