Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize ncbi_id in fetch_sequences #146

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ingest/bin/fetch-from-genbank
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ set -euo pipefail

bin="$(dirname "$0")"


main() {
fetch | "$bin"/csv-to-ndjson
local ncbi_taxon_id="${1:?NCBI taxon id is required.}"
fetch "$ncbi_taxon_id" | "$bin"/csv-to-ndjson
}

fetch() {
curl "$("$bin"/genbank-url)" \
curl "$("$bin"/genbank-url --ncbi-taxon-id "$1")" \
--fail --silent --show-error --http1.1 \
--header 'User-Agent: https://github.com/nextstrain/monkeypox (hello@nextstrain.org)'
}
Expand Down
116 changes: 71 additions & 45 deletions ingest/bin/genbank-url
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,80 @@ and observing the network activity at
https://www.ncbi.nlm.nih.gov/labs/virus/vssi/#/virus?SeqType_s=Nucleotide&VirusLineage_ss=Monkeypox%20virus,%20taxid:10244
"""
from urllib.parse import urlencode
import argparse

endpoint = "https://www.ncbi.nlm.nih.gov/genomes/VirusVariation/vvsearch2/"
params = {
# Search criteria
'fq': [
'{!tag=SeqType_s}SeqType_s:("Nucleotide")', # Nucleotide sequences (as opposed to protein)
'VirusLineageId_ss:(10244)', # NCBI Taxon id for Monkeypox
],
def parse_args():
parser = argparse.ArgumentParser(
description="Given an NCBI taxon ID, generate URL to download "
"all viral sequences and their curated metadata from GenBank via NCBI Virus."
)
parser.add_argument(
"--ncbi-taxon-id",
help="NCBI Taxon ID.",
default="10244",
required=True
)
return parser.parse_args()

# Unclear, but seems necessary.
'q': '*:*',
def build_query_url(ncbi_taxon_id: str):
"""
Generate URL to download all viral sequences and their curated metadata
from GenBank via NCBI Virus.
"""
endpoint = "https://www.ncbi.nlm.nih.gov/genomes/VirusVariation/vvsearch2/"
params = {
# Search criteria
'fq': [
'{!tag=SeqType_s}SeqType_s:("Nucleotide")', # Nucleotide sequences (as opposed to protein)
f'VirusLineageId_ss:({ncbi_taxon_id})', # NCBI Taxon id for Monkeypox
],

# Response format
'cmd': 'download',
'dlfmt': 'csv',
'fl': ','.join(
':'.join(names) for names in [
# Pairs of (output column name, source data field).
('genbank_accession', 'id'),
('genbank_accession_rev', 'AccVer_s'),
('database', 'SourceDB_s'),
('strain', 'Isolate_s'),
('region', 'Region_s'),
('location', 'CountryFull_s'),
('collected', 'CollectionDate_s'),
('submitted', 'CreateDate_dt'),
('length', 'SLen_i'),
('host', 'Host_s'),
('isolation_source', 'Isolation_csv'),
('bioproject_accession', 'BioProject_s'),
('biosample_accession', 'BioSample_s'),
('sra_accession', 'SRALink_csv'),
('title', 'Definition_s'),
('authors', 'Authors_csv'),
('submitting_organization', 'SubmitterAffilFull_s'),
('publications', 'PubMed_csv'),
('sequence', 'Nucleotide_seq'),
]
),
# Unclear, but seems necessary.
'q': '*:*',

# Stable sort with GenBank accessions.
# Columns are source data fields, not our output columns.
'sort': 'id asc',
# Response format
'cmd': 'download',
'dlfmt': 'csv',
'fl': ','.join(
':'.join(names) for names in [
# Pairs of (output column name, source data field).
('genbank_accession', 'id'),
('genbank_accession_rev', 'AccVer_s'),
('database', 'SourceDB_s'),
('strain', 'Isolate_s'),
('region', 'Region_s'),
('location', 'CountryFull_s'),
('collected', 'CollectionDate_s'),
('submitted', 'CreateDate_dt'),
('length', 'SLen_i'),
('host', 'Host_s'),
('isolation_source', 'Isolation_csv'),
('bioproject_accession', 'BioProject_s'),
('biosample_accession', 'BioSample_s'),
('sra_accession', 'SRALink_csv'),
('title', 'Definition_s'),
('authors', 'Authors_csv'),
('submitting_organization', 'SubmitterAffilFull_s'),
('publications', 'PubMed_csv'),
('sequence', 'Nucleotide_seq'),
]
),

# This isn't Entrez, but include the same email parameter it requires just
# to be nice.
'email': 'hello@nextstrain.org',
}
query = urlencode(params, doseq = True, encoding = "utf-8")
# Stable sort with GenBank accessions.
# Columns are source data fields, not our output columns.
'sort': 'id asc',

print(f"{endpoint}?{query}")
# This isn't Entrez, but include the same email parameter it requires just
# to be nice.
'email': 'hello@nextstrain.org',
}
query = urlencode(params, doseq = True, encoding = "utf-8")

print(f"{endpoint}?{query}")

def main():
args = parse_args()
build_query_url(args.ncbi_taxon_id)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion ingest/workflow/snakemake_rules/fetch_sequences.smk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rule fetch_from_genbank:
genbank_ndjson="data/genbank.ndjson",
shell:
"""
./bin/fetch-from-genbank > {output.genbank_ndjson}
./bin/fetch-from-genbank 10244 > {output.genbank_ndjson}
"""


Expand Down