# Gnomad allele frequency query

**URL:** <https://discuss.hail.is/t/gnomad-allele-frequency-query/1890>\
**Category:** Hail Query & hailctl\
**Created:** [January 14, 2021, 2:31pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890 "2021-01-14T14:31:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnnyr/32/572_2.png) [@johnnyr](https://discuss.hail.is/u/johnnyr)\
**Post date:** [January 14, 2021, 2:31pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/1 "2021-01-14T14:31:33Z")

</div>

hi, as I am still learning hail (python), I have a very basic question, because I do not yet fully understand hail tables and how to query them.

I want to get allele frequencies for all gnomAD populations for a subset of variants and export it as table or vcf.

I tried exemplarily for one random variant:

ht = hl.read\_table(‘data/gnomad.genomes.v3.1.sites.ht/’)  
favorite\_locus = hl.parse\_locus(‘chr1:156129878’, ‘GRCh38’)

When comparing it with the data on the gnomAD-browser, I can see that:

ht.filter(ht.locus == favorite\_locus).freq[0].show()  
±---------------±-----------±----------±----------±----------±------------------------+  
| locus | alleles | .AC | .AF | .AN | .homozygote\_count |  
±---------------±-----------±----------±----------±----------±------------------------+  
| locus | array | int32 | float64 | int32 | int32 |  
±---------------±-----------±----------±----------±----------±------------------------+  
| chr1:156129878 | [“T”,“G”] | 27542 | 1,81e-01 | 151952 | 4757 |  
±---------------±-----------±----------±----------±----------±------------------------+

gives the information for total AF and

ht.filter(ht.locus == favorite\_locus).freq[2].show()

±---------------±-----------±----------±----------±----------±------------------------+  
| locus | alleles | .AC | .AF | .AN | .homozygote\_count |  
±---------------±-----------±----------±----------±----------±------------------------+  
| locus | array | int32 | float64 | int32 | int32 |  
±---------------±-----------±----------±----------±----------±------------------------+  
| chr1:156129878 | [“T”,“G”] | 4832 | 7,11e-02 | 67994 | 180 |  
±---------------±-----------±----------±----------±----------±------------------------+

for NFE AF.

Can someone explain me how to use the “meta” information to more easily query for populations. Or more general, how to get names for the information stored in an array of a row-field ?

Thanks a lot for you help!

---

<div class="post-metadata">

**Author:** ![nawatts](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@nawatts](https://discuss.hail.is/u/nawatts)\
**Post date:** [January 14, 2021, 4:56pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/2 "2021-01-14T16:56:30Z")

</div>

The gnomAD Hail Tables have a global field named `freq_meta`. Each entry in `freq_meta` describes the `freq` entry at the same index (`freq_meta[0]` contains information about `freq[0]`, `freq_meta[1]` about `freq[1]`, etc.).

To get `freq_meta` as a Python list, use `hl.eval(ht.globals.freq_meta)`.

---

<div class="post-metadata">

**Author:** ![johnnyr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnnyr/32/572_2.png) [@johnnyr](https://discuss.hail.is/u/johnnyr)\
**Post date:** [January 15, 2021, 8:59am UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/3 "2021-01-15T08:59:33Z")

</div>

Thanks! That already helps. Again, sorry for the basic questions, but can you tell me how to combine this information to:  
a) generate a ht with e.g. freq[0] and freq[2]  
and  
b) finally export it to a tab, tsv or csv delimited text file which looks like this:

locus | alleles | gnomAD.AC | gnomAD.AF | gnomAD.AN | gnomAD.homozygote\_count | nfe.AC | nfe.AF | nfe.AN | nfe.homozygote\_count

Thanks!

---

<div class="post-metadata">

**Author:** ![nawatts](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@nawatts](https://discuss.hail.is/u/nawatts)\
**Post date:** [January 15, 2021, 3:04pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/4 "2021-01-15T15:04:23Z")

</div>

Something along the lines of:

```auto
ht.select(
  "locus",
  "alleles",
  gnomad_ac=ht.freq[0].AC,
  gnomad_af=ht.freq[0].AF,
  gnomad_an=ht.freq[0].AN,
  gnomad_homozygote_count=ht.freq[0].homozygote_count,
).export("/path/to/export.tsv")

```

To include more columns, include expressions for them in the `select` call.

---

<div class="post-metadata">

**Author:** ![johnnyr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnnyr/32/572_2.png) [@johnnyr](https://discuss.hail.is/u/johnnyr)\
**Post date:** [January 17, 2021, 6:42pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/5 "2021-01-17T18:42:25Z")

</div>

Thanks a lot, that really helped.  
It worked without the “locus”, “alleles” part.

---

<div class="post-metadata">

**Author:** ![kvn95ss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/kvn95ss/32/541_2.png) [@kvn95ss](https://discuss.hail.is/u/kvn95ss)\
**Post date:** [March 2, 2021, 11:46am UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/6 "2021-03-02T11:46:24Z")

</div>

Hello,

I want to extract the counts but for the whole genome, not just few lociis. I downloaded gnomad v211 liftover ht and ran the command, I met the following error -

```auto
2021-03-02 17:09:23 Hail: ERROR: Analysis exception: 'Table.select': cannot overwrite key field 'locus' with annotate, select or drop; use key_by to modify keys.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<decorator-gen-970>", line 2, in select
  File "/root/anaconda3/envs/hail/lib/python3.7/site-packages/hail/typecheck/check.py", line 614, in wrapper
    return __original_func(*args_, **kwargs_)
  File "/root/anaconda3/envs/hail/lib/python3.7/site-packages/hail/table.py", line 922, in select
    self._row)
  File "/root/anaconda3/envs/hail/lib/python3.7/site-packages/hail/utils/misc.py", line 423, in get_select_exprs
    check_keys(caller, name, protected_key)
  File "/root/anaconda3/envs/hail/lib/python3.7/site-packages/hail/utils/misc.py", line 397, in check_keys
    raise ExpressionException(msg)
hail.expr.expressions.base_expression.ExpressionException: 'Table.select': cannot overwrite key field 'locus' with annotate, select or drop; use key_by to modify keys.

```

I get disk space error when I try the command without `"locus","alleles"` part, maybe need to set a tmp directory(?)

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [March 2, 2021, 1:10pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/7 "2021-03-02T13:10:00Z")

</div>

What is the code you ran?

---

<div class="post-metadata">

**Author:** ![nawatts](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@nawatts](https://discuss.hail.is/u/nawatts)\
**Post date:** [March 2, 2021, 2:32pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/8 "2021-03-02T14:32:00Z")

</div>

Sorry, my [earlier example](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/4) incorrectly included the `locus` and `alleles` key fields in the select. It should have been:

```auto
ht.select(
  gnomad_ac=ht.freq[0].AC,
  gnomad_af=ht.freq[0].AF,
  gnomad_an=ht.freq[0].AN,
  gnomad_homozygote_count=ht.freq[0].homozygote_count,
).export("/path/to/export.tsv")

```

---

<div class="post-metadata">

**Author:** ![kvn95ss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/kvn95ss/32/541_2.png) [@kvn95ss](https://discuss.hail.is/u/kvn95ss)\
**Post date:** [March 3, 2021, 4:55am UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/9 "2021-03-03T04:55:04Z")

</div>

Hello @nawatts and @tpoterba ,

I tried this command

```auto
 ht.select(
   gnomad_ac=ht.freq[0].AC,
   gnomad_af=ht.freq[0].AF,
   gnomad_an=ht.freq[0].AN,
   gnomad_homozygote_count=ht.freq[0].homozygote_count,
 ).export("gnomad_v211_genome_counts.tsv")

```

and it worked without issue, but the locus and alleles are printed as

```auto
chr1:10067 ["T","TAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCC"]

```

Could I get them in this format?

```auto
chr\tStart\tEnd\tRef\tAlt\tAC\Homozygotes

```

Since I need the end positions, I would rather try to get the output from hail itself, rather than parsing this file myself.

---

<div class="post-metadata">

**Author:** ![nawatts](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@nawatts](https://discuss.hail.is/u/nawatts)\
**Post date:** [March 3, 2021, 1:41pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/10 "2021-03-03T13:41:29Z")

</div>

That would look something like:

```auto
ht.select(
  chr=ht.locus.contig,
  tStart=ht.locus.position,
  tRef=ht.alleles[0],
  tAlt=ht.alleles[1],
  tAC=ht.freq[0].AC,
  Homozygotes=ht.freq[0].homozygote_count,
).export("file.tsv")

```

I’m not sure what `tEnd` would refer to. The gnomAD variants have a single locus for a position.

---

<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:** [March 3, 2021, 3:02pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/11 "2021-03-03T15:02:51Z")

</div>

@kvn95ss, If your table uses the locus and alleles and key fields (check `ht.describe()`), you’ll need to. run `ht = ht.key_by()` first.

---

<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:** [March 31, 2021, 1:03pm UTC](https://discuss.hail.is/t/gnomad-allele-frequency-query/1890/12 "2021-03-31T13:03:51Z")

</div>

A post was split to a new topic: [How do I create a locus and allele keyed table from chromosome, start position, end position, reference allele and alt allele?](https://discuss.hail.is/t/how-do-i-create-a-locus-and-allele-keyed-table-from-chromosome-start-position-end-position-reference-allele-and-alt-allele/1992)
