# Ancestry RF classifer code and scripts

**URL:** https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585
**Category:** Methods
**Created:** [February 26, 2025, 12:52pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585 "2025-02-26T12:52:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![wallbp](https://avatars.discourse-cdn.com/v4/letter/w/ecccb3/32.png) [@wallbp](https://discuss.gnomad.broadinstitute.org/u/wallbp)
#### Post date: [February 26, 2025, 12:52pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/1 "2025-02-26T12:52:25Z")

</div>

Hello, I was wondering if the code / scripts used to train the ancestry RF classifier were available?

---

<div class="post-metadata">

### Author: ![wallbp](https://avatars.discourse-cdn.com/v4/letter/w/ecccb3/32.png) [@wallbp](https://discuss.gnomad.broadinstitute.org/u/wallbp)
#### Post date: [March 11, 2025, 12:28pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/2 "2025-03-11T12:28:02Z")

</div>

Specifically, we’d like to retrain on RNASeq-based SNPs, and looking for the original code. Any GitHub repo or other source would be very helpful!

---

<div class="post-metadata">

### Author: ![kchao](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.gnomad.broadinstitute.org/kchao/32/6_2.png) [@kchao](https://discuss.gnomad.broadinstitute.org/u/kchao)
#### Post date: [March 12, 2025, 8:52pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/3 "2025-03-12T20:52:17Z")

</div>

Our genetic ancestry inference utility functions are in this [script](https://github.com/broadinstitute/gnomad_methods/blob/main/gnomad/sample_qc/ancestry.py), and an example of how we use them is in this v4 sample QC [script](https://github.com/broadinstitute/gnomad_qc/blob/main/gnomad_qc/v4/sample_qc/assign_ancestry.py).

---

<div class="post-metadata">

### Author: ![wallbp](https://avatars.discourse-cdn.com/v4/letter/w/ecccb3/32.png) [@wallbp](https://discuss.gnomad.broadinstitute.org/u/wallbp)
#### Post date: [March 13, 2025, 11:22am UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/4 "2025-03-13T11:22:58Z")

</div>

Thank you for your response!

I am aware of the utility functions, but how the RF classifier was trained is vitally important to our research, as we would like to use a different approach with RNASeq data and our own set of SNVs. I was hoping that the methods of training the RF classifier would be documented in further detail as described in your article here: [https://gnomad.broadinstitute.org/news/2023-11-genetic-ancestry/](https://gnomad.broadinstitute.org/news/2023-11-genetic-ancestry/).

> In gnomAD, we use genetic similarity between samples to infer and create genetic ancestry groups. As described previously3,4,5, we perform a principal component analysis (PCA) on a set of high-quality SNVs to identify clusters of samples based on their genetic similarity, and these clusters roughly correspond to geographic ancestry provided by data contributors. We then train a random forest (RF) classifier on a subset of samples with provided genetic ancestry labels using the principal components from the PCA as features.

How you define “high-quality SNVs”, how clustering was done, and how the RF classifier was trained are all important information for our ability to train our own model, and I was hoping these scripts would be available?

---

<div class="post-metadata">

### Author: ![kchao](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.gnomad.broadinstitute.org/kchao/32/6_2.png) [@kchao](https://discuss.gnomad.broadinstitute.org/u/kchao)
#### Post date: [March 17, 2025, 9:02pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/5 "2025-03-17T21:02:14Z")

</div>

All of the scripts that we use to generate the gnomAD datasets, including the [script](https://github.com/broadinstitute/gnomad_qc/blob/main/gnomad_qc/v4/sample_qc/generate_qc_mt.py) that describes how we defined our set of high quality SNVs are in the linked repository, [gnomad\_qc](https://github.com/broadinstitute/gnomad_qc/tree/main/gnomad_qc/v4). How those variants were selected was also documented in the [v4 blog post](https://gnomad.broadinstitute.org/news/2023-11-gnomad-v4-0/#high-quality-sites-definition).

---

<div class="post-metadata">

### Author: ![wallbp](https://avatars.discourse-cdn.com/v4/letter/w/ecccb3/32.png) [@wallbp](https://discuss.gnomad.broadinstitute.org/u/wallbp)
#### Post date: [March 18, 2025, 11:29am UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/6 "2025-03-18T11:29:10Z")

</div>

I see, thank you for pointing me in the right direction!

Are any scripts / functions used for training the RF classifier described [here](https://gnomad.broadinstitute.org/news/2023-11-gnomad-v4-0/#genetic-ancestry-inference) available as well?

---

<div class="post-metadata">

### Author: ![sanjeevgnomad](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.gnomad.broadinstitute.org/sanjeevgnomad/32/258_2.png) [@sanjeevgnomad](https://discuss.gnomad.broadinstitute.org/u/sanjeevgnomad)
#### Post date: [May 9, 2025, 4:01pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/7 "2025-05-09T16:01:37Z")

</div>

@kchao  
Sorry to hijack the thread.

It’s utterly difficult to follow the script and documentation to perform ancestry analysis.  
Is there any argument type tutorial?

---

<div class="post-metadata">

### Author: ![kchao](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.gnomad.broadinstitute.org/kchao/32/6_2.png) [@kchao](https://discuss.gnomad.broadinstitute.org/u/kchao)
#### Post date: [May 12, 2025, 6:52pm UTC](https://discuss.gnomad.broadinstitute.org/t/ancestry-rf-classifer-code-and-scripts/585/8 "2025-05-12T18:52:40Z")

</div>

All of the code used in the gnomAD quality control pipelines are available in the gnomAD QC [GitHub repository](https://github.com/broadinstitute/gnomad_qc/tree/main).

re: tutorials – The team created an example notebook [here](https://github.com/broadinstitute/gnomad_qc/blob/main/gnomad_qc/example_notebooks/ancestry_classification_using_gnomad_rf.ipynb). Note that the code in this notebook may be slightly out of date and is meant to only be a guide to how these functions could be applied.

Please also refer to our [blog post](https://gnomad.broadinstitute.org/news/2021-09-using-the-gnomad-ancestry-principal-components-analysis-loadings-and-random-forest-classifier-on-your-dataset/), which discusses important caveats around applying gnomAD’s genetic ancestry inference resources.
