aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPjotr Prins2021-01-01 15:43:47 +0000
committerPjotr Prins2021-01-01 16:19:08 +0000
commitee01616a8c5ab5449325599dfaea32341f049784 (patch)
tree5633b1c9006fff820c3422de3c9711204af30ebe
parenta86633121cd92e37b2353f4bba8f2c37fa970d05 (diff)
downloadbh20-seq-resource-ee01616a8c5ab5449325599dfaea32341f049784.tar.gz
bh20-seq-resource-ee01616a8c5ab5449325599dfaea32341f049784.tar.lz
bh20-seq-resource-ee01616a8c5ab5449325599dfaea32341f049784.zip
update-from-genbank.py
-rw-r--r--workflows/pull-data/genbank/README.md2
-rwxr-xr-xworkflows/pull-data/genbank/genbank-fetch-ids5
-rwxr-xr-xworkflows/pull-data/genbank/genbank-fetch-ids.py69
-rwxr-xr-xworkflows/pull-data/genbank/update-from-genbank.py43
4 files changed, 56 insertions, 63 deletions
diff --git a/workflows/pull-data/genbank/README.md b/workflows/pull-data/genbank/README.md
index d7c294b..479ddc9 100644
--- a/workflows/pull-data/genbank/README.md
+++ b/workflows/pull-data/genbank/README.md
@@ -6,7 +6,7 @@ sparql-fetch-ids > pubseq_ids.txt
# --- get list of missing genbank IDs
genbank-fetch-ids.py --skip pubseq_ids.txt > genbank_ids.txt
# --- fetch XML
-update-from-genbank.py --ids genbank_ids.txt --outdir ~/tmp/genbank
+update-from-genbank.py --ids genbank_ids.txt --out ~/tmp/genbank
# --- Transform to YAML and FASTA
transform-genbank-xml2yamlfa --dir ~/tmp/genbank id --outdir ~/tmp/pubseq
```
diff --git a/workflows/pull-data/genbank/genbank-fetch-ids b/workflows/pull-data/genbank/genbank-fetch-ids
deleted file mode 100755
index 24fe4c7..0000000
--- a/workflows/pull-data/genbank/genbank-fetch-ids
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/bin/sh
-#
-# genbank-fetch-ids > genbank_ids.tx
-
-curl 'https://www.ncbi.nlm.nih.gov/genomes/VirusVariation/vvsearch2/?q=*:*&fq=%7B!tag=SeqType_s%7DSeqType_s:(%22Nucleotide%22)&fq=VirusLineageId_ss:(2697049)&cmd=download&sort=SourceDB_s%20desc,CreateDate_dt%20desc,id%20asc&dlfmt=acc&fl=id'
diff --git a/workflows/pull-data/genbank/genbank-fetch-ids.py b/workflows/pull-data/genbank/genbank-fetch-ids.py
index e62a611..1962daa 100755
--- a/workflows/pull-data/genbank/genbank-fetch-ids.py
+++ b/workflows/pull-data/genbank/genbank-fetch-ids.py
@@ -1,46 +1,33 @@
#!/usr/bin/env python3
#
-# - bulk download genbank data and matadata, preparing the FASTA and
-# the YAML files
+# Find all genbank IDs
#
-# update-from-genbank.py --max 10 --skip ids.txt --outdir ~/tmp/genbank
+# genbank-fetch-ids.py --max 1000 --skip pubseq_ids.txt
#
-# See directory .guix-run and README.md
+# See also directory .guix-run and README.md
BATCH_SIZE=5000
import argparse
-parser = argparse.ArgumentParser()
-parser.add_argument('--max', type=int, help='Max queries', required=False)
-parser.add_argument('--skip', type=str, help='File with ids to skip, 1 id per line', required=False)
-parser.add_argument('--outdir', type=str, help='Output directory', required=True)
-args = parser.parse_args()
-
-from Bio import Entrez
-Entrez.email = 'another_email@gmail.com' # FIXME
-
-import xml.etree.ElementTree as ET
import json
import os
import requests
-
+import sys
+import xml.etree.ElementTree as ET
from datetime import date, datetime
from dateutil.parser import parse
-import sys
-# sys.path.append('../')
-from utils import is_integer, chunks, check_and_get_ontology_dictionaries
+parser = argparse.ArgumentParser()
+parser.add_argument('--max', type=int, help='Max queries', required=False)
+parser.add_argument('--skip', type=str, help='File with ids to skip, 1 id per line', required=False)
+args = parser.parse_args()
-num_ids_for_request = 100 # batch calls
-min_acceptable_collection_date = datetime(2019, 12, 1)
+from Bio import Entrez
+Entrez.email = 'another_email@gmail.com' # FIXME
-outdir = args.outdir
+# min_acceptable_collection_date = datetime(2019, 12, 1)
today_date = date.today().strftime("%Y.%m.%d")
-path_ncbi_virus_accession = 'sequences.{}.acc'.format(today_date)
-
-if not os.path.exists(outdir):
- raise Exception(f"Output directory {outdir} does not exist!")
skip = set()
if args.skip:
@@ -51,11 +38,6 @@ if args.skip:
print(f"Skip size is {len(skip)}",file=sys.stderr)
-# ----------------------------------------------------------------------
-"""
-Download section for genbank XML
-"""
-
# Try to search several strings
TERMS = ['SARS-CoV-2', 'SARS-CoV2', 'SARS CoV2', 'SARSCoV2', 'txid2697049[Organism]']
# Remove mRNAs, ncRNAs, Proteins, and predicted models (more information here: https://en.wikipedia.org/wiki/RefSeq) starting with
@@ -85,30 +67,3 @@ for term in TERMS:
for id in ids:
print(id)
-
-sys.exit(2)
-
-with open(path_ncbi_virus_accession) as f:
- tmp_list = [line.strip('\n') for line in f]
-
-new_ids_set = set(tmp_list)
-if len(accession_to_consider_set) > 0:
- new_ids_set = new_ids_set.intersection(accession_to_consider_set)
-
-new_ids = len(new_ids_set.difference(id_set))
-id_set.update(new_ids_set)
-
-print('DB: NCBI Virus', today_date, '-->', new_ids, 'new IDs from', len(tmp_list), '---> Total unique IDs:', len(id_set))
-
-id_set = id_set.difference(accession_to_ignore_set)
-print('There are {} missing IDs to download.'.format(len(id_set)))
-
-os.makedirs(outdir)
-for i, id_x_list in enumerate(chunks(list(id_set), num_ids_for_request)):
- path_metadata_xxx_xml = os.path.join(outdir, 'metadata_{}.xml'.format(i))
- print('Requesting {} ids --> {}'.format(len(id_x_list), path_metadata_xxx_xml))
-
- with open(path_metadata_xxx_xml, 'w') as fw:
- fw.write(
- Entrez.efetch(db='nuccore', id=id_x_list, retmode='xml').read()
- )
diff --git a/workflows/pull-data/genbank/update-from-genbank.py b/workflows/pull-data/genbank/update-from-genbank.py
new file mode 100755
index 0000000..6d6d90c
--- /dev/null
+++ b/workflows/pull-data/genbank/update-from-genbank.py
@@ -0,0 +1,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