---
name: variant-population-frequency-99ac941a2a57
description: "Query gnomAD for variant allele frequency across populations. Uses FAVOR to convert rsID→variant_id first, then queries gnomAD. "
license: MIT license
metadata:
    skill-author: PJLab
---

# gnomAD Population Frequency

## Usage

### Tool Description

```tex
Step 1: Query FAVOR to convert rsID → chr-pos-ref-alt format.
API: GET https://api.genohub.org/v1/rsids/{rs_id}
Step 2: Query gnomAD GraphQL API with the variant_id.
API: POST https://gnomad.broadinstitute.org/api (GraphQL)
Args:
    rs_id (str): dbSNP rsID (e.g. "rs7412")
Return:
    Overall AF, population-specific AF (exome + genome), homozygote counts.
```

### Query Example

```python
import requests

rs_id = "rs7412"

# ── Step 1: FAVOR 获取 variant_id (chr-pos-ref-alt) ──
# 注意：FAVOR 可能返回多个变异（多等位基因位点），需遍历所有结果
favor_url = f"https://api.genohub.org/v1/rsids/{rs_id}"
favor_resp = requests.get(favor_url, timeout=30).json()
if not isinstance(favor_resp, list):
    favor_resp = [favor_resp]

variant_ids = [item.get("variant_vcf", "") for item in favor_resp if item.get("variant_vcf")]
print(f"[FAVOR] 该rsID对应 {len(variant_ids)} 个变异: {variant_ids}")

# ── Step 2: gnomAD 查询人群频率（遍历所有变异） ──
query = """
query VariantQuery($variantId: String!) {
  variant(variantId: $variantId, dataset: gnomad_r4) {
    variant_id
    rsid
    exome {
      ac
      an
      af
      ac_hom
      populations { id ac an ac_hom }
    }
    genome {
      ac
      an
      af
      ac_hom
      populations { id ac an ac_hom }
    }
  }
}
"""

for variant_vcf in variant_ids:
    print(f"\n── 查询 {variant_vcf} ──")
    resp = requests.post(
        "https://gnomad.broadinstitute.org/api",
        json={"query": query, "variables": {"variantId": variant_vcf}},
        timeout=30
    ).json()

    v = resp.get("data", {}).get("variant", {})
    if not v:
        print(f"[gnomAD] {variant_vcf}: 未找到数据")
        continue
    print(f"[gnomAD] variant: {v.get('variant_id')}, rsid: {v.get('rsid')}")

    for source in ["exome", "genome"]:
        d = v.get(source, {})
        if d:
            print(f"[gnomAD] {source}: AF={d.get('af')}, AC={d.get('ac')}, AN={d.get('an')}, Hom={d.get('ac_hom')}")
            for pop in (d.get("populations") or []):
                if pop.get("an", 0) > 0:
                    af = pop["ac"] / pop["an"]
                    print(f"  {pop['id']}: AF={af:.6f}, AC={pop['ac']}, AN={pop['an']}")
```


---
Source attribution: InternScience
Original project: https://github.com/InternScience/scp/blob/cea5398564032aea65a78e246d06c30ae945e03f/skills/variant-population-frequency/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2024 Anthropic, PBC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: InternScience
Original project: https://github.com/InternScience/scp/blob/cea5398564032aea65a78e246d06c30ae945e03f/skills/variant-population-frequency/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Analyst, Data Scientist, ML / AI Engineer
Specialties: API
Tools: Favor, Gnomad
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 858629f3a6489182f8d11fb42015bf85e7b7f266970df01ed39569da06c5eeac
