# Use \`hl.struct\` for aggregate\_entries on grouped matrix table?

**URL:** <https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273>\
**Category:** Hail Query & hailctl\
**Created:** [February 5, 2020, 4:58pm UTC](https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273 "2020-02-05T16:58:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhchung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/jhchung/32/269_2.png) [@jhchung](https://discuss.hail.is/u/jhchung)\
**Post date:** [February 5, 2020, 4:58pm UTC](https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273/1 "2020-02-05T16:58:30Z")

</div>

Hi,

I’m wondering if it is possible to pass a `hl.struct` for a grouped matrix table like a [regular MatrixTable](https://hail.is/docs/0.2/hail.MatrixTable.html?highlight=aggregate_entries#hail.MatrixTable.aggregate_entries). I would like to create a gene-summary table and include multiple items per entry. For example, total number of variants with alternate allele, flag if an individual has any alternate allele, number of missing variants, number of non-missing variants.

```auto
# This works
non_ref_by_transcripts = (
  mt
  .group_rows_by(mt.transcript_id)
  .aggregate_entries(
     n_non_ref = hl.agg.count_where(mt.GT.is_non_ref())
)

# Add flag if entry has any alternate allele
non_ref_by_transcripts = non_ref_by_transcripts.annotate_entries(
  has_non_ref = hl.if_else(non_ref_by_transcripts.n_non_ref >= 1, 1, 0)
)

# Count number of variants with missing genotype calls.
missing_by_transcripts = (
  mt
  .group_rows_by(mt.transcript_id)
  .aggregate_entries(
     n_missing_vars = hl.agg.count_where(hl.is_missing(mt.GT))
)

```

```auto
# This doesn't work
mt_transcripts = (
  mt
  .group_rows_by(mt.transcript_id)
  .aggregate_entries(
     hl.struct(
       n_non_ref = hl.agg.count_where(mt.GT.is_non_ref()),
       n_missing_vars = hl.agg.count_where(hl.is_missing(mt.GT)),
       n_non_missing_vars = hl.agg.count_where(hl.is_defined(mt.GT))
    )
)

```

Error message:

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-34-b7ec0ba0b168> in <module>
      5 n_non_ref = hl.agg.count_where(mt.GT.is_non_ref()),
      6 n_missing=hl.agg.count_where(hl.is_missing(mt.GT)),
----> 7 n_non_missing=hl.agg.count_where(hl.is_defined(mt.GT)))
      8 )
      9 .result()

TypeError: aggregate_entries() takes 1 positional argument but 2 were given

```

Alternatively, is there is a way to annotate entries with an other MatrixTable? I.e. Create different MTs for each feature I want to calculate and merge them together.

---

<div class="post-metadata">

**Author:** ![johnc1231](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnc1231/32/286_2.png) [@johnc1231](https://discuss.hail.is/u/johnc1231)\
**Post date:** [February 5, 2020, 5:04pm UTC](https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273/2 "2020-02-05T17:04:13Z")

</div>

Looks like the difference here is you aren’t using a keyword argument when you call the `aggregate_entries` that doesn’t work. Try like

```auto
.aggregate_entries(
    my_struct = hl.struct( .....)
)

```

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [February 5, 2020, 5:12pm UTC](https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273/3 "2020-02-05T17:12:33Z")

</div>

Or just provide multiple entries:

```auto
mt_transcripts = (
  mt
  .group_rows_by(mt.transcript_id)
  .aggregate_entries(
       n_non_ref = hl.agg.count_where(mt.GT.is_non_ref()),
       n_missing_vars = hl.agg.count_where(hl.is_missing(mt.GT)),
       n_non_missing_vars = hl.agg.count_where(hl.is_defined(mt.GT))
)

```

---

<div class="post-metadata">

**Author:** ![jhchung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/jhchung/32/269_2.png) [@jhchung](https://discuss.hail.is/u/jhchung)\
**Post date:** [February 5, 2020, 5:15pm UTC](https://discuss.hail.is/t/use-hl-struct-for-aggregate-entries-on-grouped-matrix-table/1273/4 "2020-02-05T17:15:18Z")

</div>

Hi @johnc1231 and and @danking,

Both of those options seem to work. I think the second solution fits best with what I’m thinking about.

Thanks!
