File size: 10,876 Bytes
c49fca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
license: mit
language:
  - en
pretty_name: Clinvar Annotations
---

part of 

# 🧬 Genomic Reasoning Agent
### LLM-driven agentic system for personal genomic variant interpretation

[![HuggingFace](https://img.shields.io/badge/πŸ€—_HuggingFace-Space-yellow)](https://huggingface.co/spaces/mioulin/genomic-reasoning-agent)
[![Dataset](https://img.shields.io/badge/πŸ€—_Dataset-genomic--qa-blue)](https://huggingface.co/datasets/mioulin/genomic-reasoning-qa)
[![Model](https://img.shields.io/badge/πŸ€—_Model-genomic--reasoning--llm-green)](https://huggingface.co/mioulin/genomic-reasoning-grpo)
[![License: MIT](https://img.shields.io/badge/License-MIT-purple)](LICENSE)

---

## Overview

This project builds a **multi-step reasoning agent** that interprets personal genomic data from 23andMe against biomedical knowledge databases (ClinVar, GWAS Catalog, gnomAD). The agent is trained with **GRPO** (Group Relative Policy Optimization) using fully verifiable reward signals β€” no human labelers needed.

The core insight mirrors DeepSeek-R1's approach to mathematics: **genomic variant interpretation has verifiable ground truth** (ClinVar classifications, GWAS p-values, population frequencies), making it ideal for RL-based reasoning training.

```
23andMe SNPs (631K)
        ↓
  ClinVar Annotation          ← rs1801133 β†’ MTHFR β†’ Pathogenic
        ↓
 Tool-Using LLM Agent         ← 5 tools: lookup / scan / haplotype / stats / reward
        ↓
   GRPO Training Loop         ← verifiable reward from ClinVar ground truth
        ↓
  Reasoning Model             ← factual Β· calibrated Β· evidence-grounded
        ↓
   HF Spaces Demo             ← upload 23andMe β†’ ask questions β†’ reasoning trace
```

---

## Motivation

Standard LLMs hallucinate on genomic questions. This project trains a model that:

- **Cites sources** (ClinVar review status, GWAS p-values, PubMed IDs)
- **Shows reasoning chains** (mechanism β†’ evidence β†’ conclusion)
- **Calibrates uncertainty** (risk factor β‰  diagnosis)
- **Uses tools** to look up live databases rather than relying on memorised weights

The training signal is 100% verifiable β€” reward is computed by checking responses against ClinVar annotations, not scored by humans.

---

## Repository Structure

```
genomic-reasoning-agent/
β”œβ”€β”€ genomic_pipeline.py              # Step 1: Parse 23andMe .txt β†’ DataFrame
β”œβ”€β”€ clinvar_pipeline_full.py         # Step 2: Annotate variants via ClinVar API
β”œβ”€β”€ genomic_agent_huggingface.py     # Step 3: smolagents tool-using agent (5 tools)
β”œβ”€β”€ train_grpo.py                    # Step 4: GRPO training with TRL
β”œβ”€β”€ app.py                           # HF Spaces Gradio UI
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ clinvar_annotations.json     # 12 variants with full ClinVar metadata
β”‚   └── genomic_qa_dataset.json      # 36 Q&A pairs (3 task types Γ— 12 variants)
└── README.md
```

---

## Pipeline: Step by Step

### Step 1 β€” Parse 23andMe

Reads the raw `.txt` export (Build GRCh37) into a DataFrame of 631,455 SNPs.

```python
from genomic_pipeline import parse_23andme
df = parse_23andme("genome_23andme.txt")
# β†’ 631,455 SNPs across chromosomes 1–22, X, Y, MT
# β†’ 104,617 heterozygous (16.6%) | 522,909 homozygous (82.8%)
```

### Step 2 β€” ClinVar Annotation

Queries NCBI E-utilities and GWAS Catalog for each rsID. Builds a Q&A dataset with verifiable answers.

```python
from clinvar_pipeline_full import query_clinvar_batch, build_qa_dataset
annotations = query_clinvar_batch(rsids, email="your@email.com")
qa_dataset  = build_qa_dataset(annotations, genome_df)
# β†’ 12 variants annotated
# β†’ 36 Q&A pairs: variant_interpretation / genotype_interpretation / pathway_reasoning
```

**Sample annotation:**

| rsID | Gene | Genotype | Significance | Condition |
|------|------|----------|-------------|-----------|
| rs1801133 | MTHFR | GG | Pathogenic/Likely pathogenic | Homocystinuria |
| rs429358 + rs7412 | APOE | TT / CC | risk factor | Alzheimer disease |
| rs9939609 | FTO | AT | risk factor | Obesity |
| rs762551 | CYP1A2 | AC | drug response | Caffeine metabolism |

### Step 3 β€” Tool-Using Agent (smolagents)

A `ToolCallingAgent` with 5 tools that plans multi-step queries across databases.

```python
from smolagents import ToolCallingAgent, InferenceClientModel
from genomic_agent_huggingface import (
    VariantLookupTool,    # rsID β†’ ClinVar + genotype
    GeneScannerTool,      # gene/trait β†’ all patient variants
    HaplotypeCallerTool,  # APOE Ξ΅2/Ξ΅3/Ξ΅4 from two SNPs
    GenomeStatsTool,      # 631K SNPs overview
    RewardEvaluatorTool,  # GRPO reward score (used during training)
)
model = InferenceClientModel("meta-llama/Llama-3.1-8B-Instruct")
agent = ToolCallingAgent(tools=[...], model=model, max_steps=10)
answer = agent.run("What is my APOE haplotype and what does it mean?")
```

**6-step reasoning trace for "Give me a genomic health summary":**

```
Step 1 [genome_stats]       β†’ 631,455 SNPs | 16.6% heterozygous
Step 2 [variant_lookup]     β†’ rs1801133 | MTHFR | GG | Pathogenic
Step 3 [call_haplotype]     β†’ APOE Ξ΅3/Ξ΅3 | Neutral Alzheimer's risk
Step 4 [scan_gene_variants] β†’ dopamine: ANKK1 GG (risk), COMT GG (Val/Val)
Step 5 [scan_gene_variants] β†’ caffeine: CYP1A2 AC (intermediate), ADORA2A CT
Step 6 [evaluate_reasoning] β†’ reward: 0.93 / 1.00 (excellent)
```

### Step 4 β€” GRPO Training

Trains the base LLM to reason better about genomic questions using reinforcement learning with verifiable rewards β€” no human annotation required.

```python
from train_grpo import train, TrainingConfig
config = TrainingConfig(
    model_name="meta-llama/Llama-3.1-8B-Instruct",
    num_epochs=3,
    num_generations=4,        # G: completions per question
    beta=0.04,                # KL penalty
    use_lora=True,
)
trainer = train(config)
```

**Reward function β€” 6 verifiable components:**

| Component | Weight | Verifiable against |
|-----------|--------|--------------------|
| Factual accuracy | 0.35 | ClinVar clinical significance |
| Condition coverage | 0.25 | ClinVar associated conditions |
| Gene mention | 0.15 | ClinVar gene annotation |
| Reasoning chain | 0.15 | Presence of causal language |
| Uncertainty calibration | 0.05 | Hedging language |
| Response completeness | 0.05 | Word count |

**Training progression (simulated):**

```
untrained  reward=0.06  |β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘|
epoch_1    reward=0.56  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘|
epoch_3    reward=0.71  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘|
epoch_5    reward=0.93  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘|
epoch_10   reward=1.00  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ|
```

**GRPO advantage formula:**

```
advantage_i = (reward_i βˆ’ mean(rewards)) / std(rewards)
No critic network. No value function. No human labeler.
Just relative comparison within each group of G=4 completions.
```

---

## Key Results from Real Genome Data

Running the full pipeline on a real 23andMe export (Zalina Dezhina, v5 chip):

| Variant | Gene | Genotype | Clinical Note |
|---------|------|----------|---------------|
| πŸ”΄ rs1801133 | MTHFR | GG | Pathogenic β€” folate metabolism (p.Ala222Val) |
| 🟑 rs9939609 | FTO | **AT** | Risk factor β€” obesity, 1 risk allele (40.4% population) |
| 🧠 APOE | β€” | **Ξ΅3/Ξ΅3** | Neutral β€” most common haplotype, no elevated AD risk |
| πŸ’Š rs762551 | CYP1A2 | **AC** | Drug response β€” intermediate caffeine metabolizer |
| 🟑 rs1800497 | ANKK1 | GG | Risk factor β€” reward pathway / DRD2 association |
| 🟒 rs6265 | BDNF | CC | Benign (Val/Val) β€” better episodic memory |

> ⚠️ This is a research and portfolio project, not medical advice.
> All interpretations are for educational purposes only.

---

## Tech Stack

| Layer | Technology |
|-------|-----------|
| Genome parsing | pandas, Python |
| Variant annotation | NCBI E-utilities (ClinVar), EBI GWAS Catalog |
| Agentic framework | [smolagents](https://github.com/huggingface/smolagents) (HuggingFace) |
| RL training | [TRL](https://github.com/huggingface/trl) GRPOTrainer |
| Fine-tuning | LoRA (PEFT) + 4-bit quantization (bitsandbytes) |
| Base model | meta-llama/Llama-3.1-8B-Instruct |
| Demo UI | Gradio (HF Spaces) |
| Evaluation | Per-task reward breakdown, 3 task types |

---

## HuggingFace Deployment

**Spaces (interactive demo):**
```
1. Create new Space β†’ Gradio SDK
2. Upload: genomic_agent_huggingface.py, app.py, data/
3. Add secret: HF_TOKEN
4. Upload your 23andMe .txt β†’ ask questions in chat
```

**Model Hub (trained weights):**
```python
trainer.push_to_hub("mioulin/genomic-reasoning-llm")
```

**Dataset Hub:**
```python
from datasets import Dataset
Dataset.from_list(qa_dataset).push_to_hub("mioulin/genomic-reasoning-qa")
```

---

## Connection to ML Scientist Role

This project was built to demonstrate the exact skills required for ML Scientist roles in AIΓ—biology:

- **Agentic systems** β€” 5-tool ToolCallingAgent with multi-step planning
- **RL/RLHF training** β€” GRPO with verifiable reward, no human labelers
- **Biomedical data integration** β€” ClinVar, GWAS Catalog, gnomAD, PubMed
- **Evaluation framework** β€” 6-component reward breakdown across 3 task types
- **Real scientific domain** β€” 631,455 SNPs from real 23andMe genome
- **Reasoning over evidence** β€” multi-hop: SNP β†’ gene β†’ pathway β†’ phenotype

---

## Running Locally

```bash
git clone https://huggingface.co/spaces/mioulin/genomic-reasoning-agent
cd genomic-reasoning-agent
pip install smolagents trl transformers accelerate peft datasets gradio
# Annotate your genome
python clinvar_pipeline_full.py \
  --genome your_23andme.txt \
  --output data/clinvar_annotations.json \
  --email your@email.com
# Run agent (requires HF token for model inference)
export HF_TOKEN=hf_...
python genomic_agent_huggingface.py
# Train with GRPO (requires GPU)
python train_grpo.py \
  --model meta-llama/Llama-3.1-8B-Instruct \
  --epochs 3 --lora --G 4
```

---

## Author

**Zalina Dezhina, PhD**  
[![HuggingFace](https://img.shields.io/badge/πŸ€—-mioulin-yellow)](https://huggingface.co/mioulin)

## Citation

```bibtex
@misc{dezhina2026genomic,
  title  = {Genomic Reasoning Agent: GRPO Training on Personal SNP Data},
  author = {Dezhina, Zalina},
  year   = {2026},
  url    = {https://huggingface.co/spaces/mioulin/genomic-reasoning-agent}
}
```

---

## License

MIT β€” research and educational use only.
Not intended for clinical or medical decision-making.

---

*Built with 🧬 smolagents · TRL · HuggingFace · ClinVar · 23andMe*