# How can i find the allele balance of only Heterozygous or Homozygous

**URL:** <https://discuss.hail.is/t/how-can-i-find-the-allele-balance-of-only-heterozygous-or-homozygous/1869>\
**Category:** Hail Query & hailctl\
**Created:** [December 26, 2020, 9:23am UTC](https://discuss.hail.is/t/how-can-i-find-the-allele-balance-of-only-heterozygous-or-homozygous/1869 "2020-12-26T09:23:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haseeb1](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Haseeb1](https://discuss.hail.is/u/Haseeb1)\
**Post date:** [December 26, 2020, 9:23am UTC](https://discuss.hail.is/t/how-can-i-find-the-allele-balance-of-only-heterozygous-or-homozygous/1869/1 "2020-12-26T09:23:17Z")

</div>

Hi,  
I want to annotate a row field that only contains allele balance of Hets, and then i want to annotate more row fields like “minHetAB”, “maxHetAB” and “medianHetAB”.

---

<div class="post-metadata">

**Author:** ![Haseeb1](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Haseeb1](https://discuss.hail.is/u/Haseeb1)\
**Post date:** [December 29, 2020, 7:25am UTC](https://discuss.hail.is/t/how-can-i-find-the-allele-balance-of-only-heterozygous-or-homozygous/1869/2 "2020-12-29T07:25:17Z")

</div>

Any updates @tpoterba

---

<div class="post-metadata">

**Author:** ![olszewskip](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@olszewskip](https://discuss.hail.is/u/olszewskip)\
**Post date:** [January 4, 2021, 2:18pm UTC](https://discuss.hail.is/t/how-can-i-find-the-allele-balance-of-only-heterozygous-or-homozygous/1869/3 "2021-01-04T14:18:06Z")

</div>

Hi! Not sure I fully understand, but perhaps the following could be helpful?

Annotate each entry with allele balance AB, based on its AD field, allowing for multiallelics. Genotypes which are not hets get AB=null, others get relative AD of the less covered allele. (I suppose one can still worry about nans from 0/0.)

```auto
GT_AD = hl.enumerate(
    mt.GT.one_hot_alleles(hl.len(mt.alleles))
).filter(
    lambda _: _[1] > 0
).map(
    lambda _: mt.AD[_[0]]
)
mt = mt.annotate_entries(
    HetAB = hl.case().when(
        mt.GT.is_het(),
        hl.min(mt.GT_AD) / hl.sum(mt.GT_AD)
    ).or_missing()
)

```

Given those entry fields one could aggregate them over columns, which - as far as I understand would simply omit the null ABs:

```auto
mt = mt.annotate_rows(
    minHetAB = hl.agg.min(mt.HetAB),
    maxHetAB = hl.agg.max(mt.HetAB),
)

```

In this approach, one would expect maxHetAB to be near its largest allowed value of 0.5 for all rows where its not missing.
