pzweuj commited on
Commit
3e51a60
·
verified ·
1 Parent(s): 6a99d5f

Add bin/download_bundle.py

Browse files
Files changed (1) hide show
  1. bin/download_bundle.py +89 -0
bin/download_bundle.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Download the SchemaBio reference bundle (hg19 or hg38) from Hugging Face.
4
+
5
+ Always downloaded:
6
+ - reference genome (*.fa.gz, *.fai)
7
+ - bed/
8
+ - schema_bundle/
9
+ - VEP cache tarball (homo_sapiens_merged_vep_*.tar.gz)
10
+
11
+ Optional:
12
+ --index bwa|minibwa|all aligner index (default: none)
13
+ bwa -> {genome}/*.fa.amb/.ann/.bwt/.pac/.sa
14
+ minibwa -> everything under {genome}/minibwa/
15
+ --extract-vep extract the VEP cache into <outdir>/vep (default: no)
16
+
17
+ Examples:
18
+ python download_bundle.py -g hg38 -o /data/database
19
+ python download_bundle.py -g hg19 -o /data/database --index bwa --extract-vep
20
+
21
+ Requires: pip install huggingface_hub
22
+ Set HF_ENDPOINT=https://hf-mirror.com to use a mirror.
23
+ """
24
+ import argparse, fnmatch, os, sys, tarfile
25
+
26
+ REPO = "pzweuj/SchemaBio_Bundle"
27
+ BWA_EXT = (".amb", ".ann", ".bwt", ".pac", ".sa")
28
+
29
+
30
+ def select(files, genome, index):
31
+ pats = [f"{genome}/Homo_sapiens.*.fa.gz", f"{genome}/Homo_sapiens.*.fa.fai",
32
+ f"{genome}/homo_sapiens_merged_vep_*.tar.gz",
33
+ f"{genome}/bed/*", f"{genome}/schema_bundle/*"]
34
+ out = [f for f in files if any(fnmatch.fnmatch(f, p) for p in pats)]
35
+ if index in ("bwa", "all"):
36
+ out += [f for f in files if f.startswith(f"{genome}/") and f.count("/") == 1
37
+ and f.endswith(tuple(".fa" + e for e in BWA_EXT))]
38
+ if index in ("minibwa", "all"):
39
+ mb = [f for f in files if f.startswith(f"{genome}/minibwa/")]
40
+ if not mb:
41
+ print(f"[warn] no minibwa index found under {genome}/minibwa/ yet, skipped", file=sys.stderr)
42
+ out += mb
43
+ return sorted(set(out))
44
+
45
+
46
+ def main():
47
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
48
+ ap.add_argument("-g", "--genome", required=True, choices=["hg19", "hg38"])
49
+ ap.add_argument("-o", "--outdir", default=".", help="output directory (default: .)")
50
+ ap.add_argument("--index", default="none", choices=["none", "bwa", "minibwa", "all"],
51
+ help="aligner index to download (default: none)")
52
+ ap.add_argument("--extract-vep", action="store_true", help="extract VEP cache after download")
53
+ ap.add_argument("--revision", default="main")
54
+ ap.add_argument("--dry-run", action="store_true", help="only list files")
55
+ a = ap.parse_args()
56
+
57
+ try:
58
+ from huggingface_hub import HfApi, hf_hub_download
59
+ except ImportError:
60
+ sys.exit("huggingface_hub not installed: pip install huggingface_hub")
61
+
62
+ files = [f.path for f in HfApi().list_repo_tree(REPO, repo_type="dataset", recursive=True,
63
+ revision=a.revision) if hasattr(f, "size")]
64
+ todo = select(files, a.genome, a.index)
65
+ print(f"{len(todo)} files to download for {a.genome} (index={a.index})")
66
+ for f in todo:
67
+ print(" " + f)
68
+ if a.dry_run:
69
+ return
70
+
71
+ os.makedirs(a.outdir, exist_ok=True)
72
+ for i, f in enumerate(todo, 1):
73
+ print(f"[{i}/{len(todo)}] {f}", flush=True)
74
+ hf_hub_download(REPO, f, repo_type="dataset", revision=a.revision, local_dir=a.outdir)
75
+
76
+ if a.extract_vep:
77
+ for f in todo:
78
+ if os.path.basename(f).startswith("homo_sapiens_merged_vep_"):
79
+ src = os.path.join(a.outdir, f)
80
+ dst = os.path.join(a.outdir, a.genome, "vep")
81
+ os.makedirs(dst, exist_ok=True)
82
+ print(f"extracting {src} -> {dst}", flush=True)
83
+ with tarfile.open(src) as t:
84
+ t.extractall(dst)
85
+ print("done")
86
+
87
+
88
+ if __name__ == "__main__":
89
+ main()