aboutsummaryrefslogtreecommitdiff
path: root/workflows/pull-data/genbank/update-from-genbank.py
blob: 6d6d90ca13932604422f7efacf9c560100b103ad (plain)
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
#!/usr/bin/env python3
#
# bulk download genbank data and matadata, preparing the FASTA and the
# YAML files
#
#   update-from-genbank.py --max 10 --ids ids.txt --out ~/tmp/genbank-xml
#
# See also directory .guix-run and README.md

import argparse
import os
import sys
from utils import chunks

from Bio import Entrez
Entrez.email = 'another_email@gmail.com' # FIXME

BATCH=100

parser = argparse.ArgumentParser()
parser.add_argument('--max', type=int, help='Max queries', required=False)
parser.add_argument('--ids', type=str, help='File with ids to fetch, 1 id per line', required=True)
parser.add_argument('--out', type=str, help='Directory to write to', required=True)
args = parser.parse_args()

ids = set()
with open(args.ids) as f:
    content = f.readlines()
    for line in content:
        ids.add(line.strip())

dir = args.out
if not os.path.exists(dir):
    raise Exception(f"Directory {dir} does not exist")

request_num = min(BATCH,args.max)
for i, idsx in enumerate(chunks(list(ids), request_num)):
    xmlfn = os.path.join(dir, f"metadata_{i}.xml")
    print(f"Fetching {xmlfn} ({i*request_num})",file=sys.stderr)
    with open(xmlfn, 'w') as f:
        f.write(Entrez.efetch(db='nuccore', id=idsx, retmode='xml').read())
    if i*request_num >= args.max:
        break