Count the number of alleles at each site

Hi All,

I would like to add a row-wise annotation (info field) to my matrix table called ‘NAL’ that contains the number of alleles at a site (the length of the alleles array).

I have attempted to achieve this using the command
mt = mt.annotate_rows(info=mt.info.annotate(NAL=mt.alleles.aggregate(hl.agg.count())))

However, this results in the following error

Traceback (most recent call last):
  File "/home/alee9/.local/lib/python3.10/site-packages/hail/typecheck/check.py", line 594, in arg_check
    return checker.check(arg, function_name, arg_name)
  File "/home/alee9/.local/lib/python3.10/site-packages/hail/typecheck/check.py", line 359, in check
    raise TypecheckFailure
hail.typecheck.check.TypecheckFailure

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<decorator-gen-572>", line 2, in aggregate
  File "/home/alee9/.local/lib/python3.10/site-packages/hail/typecheck/check.py", line 586, in wrapper
    args_, kwargs_ = check_all(__original_func, args, kwargs, checkers, is_method=is_method)
  File "/home/alee9/.local/lib/python3.10/site-packages/hail/typecheck/check.py", line 551, in check_all
    args_.append(arg_check(args[i], name, arg_name, checker))
  File "/home/alee9/.local/lib/python3.10/site-packages/hail/typecheck/check.py", line 596, in arg_check
    raise TypeError("{fname}: parameter '{argname}': "
TypeError: aggregate: parameter 'f': expected 1-argument function, found hail.expr.expressions.typed_expressions.Int64Expression: <Int64Expression of type int64>

I suspect that the command that I am using is not correct and would be grateful for any help in constructing the correct command.

Thanks in advance

I’d maybe try something like:

mt = mt.annotate_rows(info=mt.info.annotate(NAL=hl.len(mt.alleles)))

mt.alleles is usually a row field, so you actually don’t need to perform an aggregation over the samples/columns.

1 Like

Hi Benjamin, thanks for your swift reply. It works perfectly, and gives exactly what I wanted.

1 Like