pyhaplogrep
English · 中文说明
Pure-Python mtDNA haplogroup classification — no Java, no dependencies.
pyhaplogrep assigns a mitochondrial DNA (mtDNA) haplogroup from a raw
genotyping chip, an MT VCF, or a consensus FASTA. It re-implements the
HaploGrep assignment approach in a single dependency-free Python module,
driven by the standard PhyloTree tree.xml + mutation weights.
It exists because running HaploGrep3
requires a ~90 MB Java runtime. pyhaplogrep runs on plain Python 3 and ships
its tree data (~2 MB) inside the package.
Install
pip install .
# or, for development:
pip install -e .
Only Python ≥ 3.7 and the standard library are required.
Usage
Three input modes (choose exactly one):
# 1) Raw consumer chip — 4 columns: rsid, chr, pos, genotype (most accurate)
pyhaplogrep --sample me --chip mychip.txt
# 2) MT VCF produced from a BAM (e.g. bwa + bcftools mpileup)
pyhaplogrep --sample me --vcf sampleMT.vcf
# 3) MT consensus FASTA (must be rCRS length, 16569 bp)
pyhaplogrep --sample me --fasta consensus.fa
The tree data and rCRS reference are bundled, so no extra paths are needed.
To use a different tree:
pyhaplogrep --sample me --chip mychip.txt --tree /path/to/tree_dir # needs tree.xml (+weights.txt)
Run without installing: python -m pyhaplogrep --sample me --chip mychip.txt
(from a checkout, with src/ on PYTHONPATH, or after pip install -e .).
Output
Example (illustrative values, not from a real sample):
sample=me
haplogroup=X2c1
rank=1
quality=0.8800
private=153A 1234A
found_w=120.0 extra_w=16.0 n_obs=18
top3=X2c1:0.8800 | X2c:0.8500 | X2:0.8200
CSV: me,X2c1,1,0.8800,"153A 1234A"
haplogroup — best assignment.
quality — confidence found_w / (found_w + extra_w) in [0,1]; higher =
the clade’s defining mutations are well-covered and few off-clade mutations
are observed. (Note: this is a recomputed confidence, not numerically
identical to HaploGrep’s quality — it usually reads a bit lower, but ranks
the same way.)
private — observed mutations not on the assigned clade’s path, i.e.
private/back mutations relative to the haplogroup.
top3 — the three best-scoring candidates.
CSV: — one-line machine-readable summary.
How it works
- Parse
tree.xml into nodes: each node = a haplogroup with its defining
mutations (<poly>); the path to a node is the union of all ancestor
mutations. Load per-mutation weights from weights.txt.
- Turn the input into a set of observed mutations vs rCRS (chip genotypes
/ VCF ALT / FASTA differences).
- For each node score
found / (found + extra), where found = weight of its
path mutations that are observed, extra = weight of observed mutations
not on its path.
- Rank by score (desc), then
found (desc), then depth (asc — at equal
evidence prefer the shallower node, i.e. the end of the evidence). This
avoids over-classifying into under-supported subclades.
A deeper clade is only chosen when its additional defining mutations are
actually observed; uncovered/reference mutations never push the call deeper.
Accuracy
Validated against HaploGrep3 on a panel of 18 real consumer-chip samples
(23mofang / GeneBox, .snp + .txt):
18/18 haplogroups identical to HaploGrep3, with identical private
mutation sets. Results are deterministic across runs.
HaploGrep3’s quality numbers are typically a few points higher due to a
different weighting/aggregation; the haplogroup calls themselves match.
Reproduce with scripts/validate_vs_haplogrep.py (edit the paths at the top).
Data & licensing
- Code: MIT (see
LICENSE).
- Bundled data (
src/pyhaplogrep/data/): PhyloTree rCRS build 17,
per-mutation weights (Soares et al. 2013 / HaploGrep3), and the rCRS
(NC_012920.1). See NOTICE for full attribution and citations. The data is
redistributed from the MIT-licensed HaploGrep3 distribution.
Citation
If you use the tree or weights, please cite the upstream sources listed in
NOTICE — notably van Oven & Kayser 2009 (PhyloTree) and Soares et al. 2013
(weights). For HaploGrep itself: Kloss-Brandstätter et al. 2011.
Limitations
- Assignment resolution is bounded by data coverage: a sparse chip cannot
resolve deep subclades.
top3 and quality help judge confidence.
--fasta compares position-by-position and is only reliable when the
sequence is full rCRS length (16569). Shorter consensus sequences (e.g.
MyHeritage 16565) trigger a warning; use --chip or --vcf instead.
- Tree is fixed at PhyloTree build 17 (
[email protected]).
中文说明
纯 Python 线粒体(mtDNA)单倍群分型 —— 无需 Java、无第三方依赖。
pyhaplogrep 可从消费级基因芯片、MT VCF 或共识 FASTA 中判定线粒体单倍群。它在单个纯 Python 模块里复刻了 HaploGrep 的判定思路,由标准 PhyloTree 的 tree.xml 加突变权重驱动。
之所以做它:跑 HaploGrep3 需要约 90 MB 的 Java 运行时;本工具只需 Python 3 与标准库,包内自带约 2 MB 的树数据。
安装
pip install .
pip install -e . # 开发用
仅需 Python ≥ 3.7 与标准库。
用法
三种输入方式(三选一):
# 1) 原始芯片:4 列 rsid chr pos 基因型(最准确)
pyhaplogrep --sample me --chip mychip.txt
# 2) 由 BAM 生成的 MT VCF(如 bwa + bcftools mpileup)
pyhaplogrep --sample me --vcf sampleMT.vcf
# 3) MT 共识 FASTA(须为 rCRS 全长 16569 bp)
pyhaplogrep --sample me --fasta consensus.fa
树数据与 rCRS 参考已内置,无需额外路径。换用其它树:
pyhaplogrep --sample me --chip mychip.txt --tree /path/to/tree_dir # 需含 tree.xml(+weights.txt)
免安装运行:python -m pyhaplogrep --sample me --chip mychip.txt(在检出目录且 src/ 在 PYTHONPATH,或 pip install -e . 之后)。
输出
示例(数值仅示意,非真实样本):
sample=me
haplogroup=X2c1
rank=1
quality=0.8800
private=153A 1234A
found_w=120.0 extra_w=16.0 n_obs=18
top3=X2c1:0.8800 | X2c:0.8500 | X2:0.8200
CSV: me,X2c1,1,0.8800,"153A 1234A"
haplogroup —— 最优判定。
quality —— 置信度 found_w / (found_w + extra_w),取值 [0,1];越高表示该分支定义突变覆盖得好、且分支外突变少。(这是重算的置信度,与 HaploGrep 的 quality 数值并不完全相同:通常略低,但排序一致。)
private —— 观测到但不在所判分支路径上的突变,即相对该单倍群的私有/回复突变。
top3 —— 得分最高的三个候选。
CSV: —— 单行机读摘要。
原理
- 解析
tree.xml 为节点:每个节点 = 一个单倍群及其定义突变(<poly>);到某节点的路径 = 所有祖先突变之并集。按 weights.txt 载入每个突变的权重。
- 把输入转换为相对 rCRS 的观测突变集合(芯片基因型 / VCF 的 ALT / FASTA 与 rCRS 的差异)。
- 对每个节点打分
found / (found + extra),其中 found = 其路径上被观测到的突变权重,extra = 被观测到但不属于其路径的突变权重。
- 按 分数(降)→
found(降)→深度(升,同等证据时偏好更浅的节点,即证据的末端) 排序。避免落入证据不足的深支。
只有当更深支的定义突变真正被观测到时才会下潜到更深单倍群;未覆盖/参考型突变不会推动下潜。
准确度
在一组 18 个真实消费级芯片样本(23魔方 / GeneBox,.snp + .txt)上与 HaploGrep3 对拍:
18/18 单倍群判定与 HaploGrep3 完全一致,私有突变集合一致,结果确定可复现。
HaploGrep3 的 quality 数值通常因权重/聚合方式不同而略高,但单倍群判定本身一致。
复现:见 scripts/validate_vs_haplogrep.py(在文件顶部改路径)。
数据与许可
- 代码:MIT(见
LICENSE)。
- 内置数据(
src/pyhaplogrep/data/):PhyloTree rCRS build 17、每个突变的权重(Soares et al. 2013 / HaploGrep3)、rCRS(NC_012920.1)。完整署名与引用见 NOTICE。数据再分发自 MIT 许可的 HaploGrep3 发行包。
引用
若使用本树或权重,请引用 NOTICE 中列出的上游来源——尤其是 van Oven & Kayser 2009(PhyloTree)与 Soares et al. 2013(权重)。HaploGrep 本身请引用 Kloss-Brandstätter et al. 2011。
局限
- 判定分辨率受数据覆盖限制:稀疏芯片无法解析到很深的亚支;可用
top3 与 quality 判断置信度。
--fasta 逐位比对,仅在序列为 rCRS 全长(16569)时才可靠;更短的共识序列(如 MyHeritage 16565)会触发告警,请改用 --chip 或 --vcf。
- 树固定为 PhyloTree build 17(
[email protected])。
评论