-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419: Sync vendored scripts
- Loading branch information
Showing
20 changed files
with
216 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[cram] | ||
shell = /bin/bash | ||
indent = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
name: CI | ||
|
||
on: | ||
- push | ||
- pull_request | ||
- workflow_dispatch | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
shellcheck: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: nextstrain/.github/actions/shellcheck@master | ||
|
||
cram: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
- run: pip install cram | ||
- run: cram tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
bin="$(dirname "$0")" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Fetch metadata and nucleotide sequences from NCBI Entrez and output to a GenBank file. | ||
""" | ||
import json | ||
import argparse | ||
from Bio import SeqIO, Entrez | ||
|
||
# To use the efetch API, the docs indicate only around 10,000 records should be fetched per request | ||
# https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch | ||
# However, in my testing with HepB, the max records returned was 9,999 | ||
# - Jover, 16 August 2023 | ||
BATCH_SIZE = 9999 | ||
|
||
Entrez.email = "[email protected]" | ||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument('--term', required=True, type=str, | ||
help='Genbank search term. Replace spaces with "+", e.g. "Hepatitis+B+virus[All+Fields]complete+genome[All+Fields]"') | ||
parser.add_argument('--output', required=True, type=str, help='Output file (Genbank)') | ||
return parser.parse_args() | ||
|
||
|
||
def get_esearch_history(term): | ||
""" | ||
Search for the provided *term* via ESearch and store the results using the | ||
Entrez history server.¹ | ||
Returns the total count of returned records, query key, and web env needed | ||
to access the records from the server. | ||
¹ https://www.ncbi.nlm.nih.gov/books/NBK25497/#chapter2.Using_the_Entrez_History_Server | ||
""" | ||
handle = Entrez.esearch(db="nucleotide", term=term, retmode="json", usehistory="y", retmax=0) | ||
esearch_result = json.loads(handle.read())['esearchresult'] | ||
print(f"Search term {term!r} returned {esearch_result['count']} IDs.") | ||
return { | ||
"count": int(esearch_result["count"]), | ||
"query_key": esearch_result["querykey"], | ||
"web_env": esearch_result["webenv"] | ||
} | ||
|
||
|
||
def fetch_from_esearch_history(count, query_key, web_env): | ||
""" | ||
Fetch records in batches from Entrez history server using the provided | ||
*query_key* and *web_env* and yields them as a BioPython SeqRecord iterator. | ||
""" | ||
print(f"Fetching GenBank records in batches of n={BATCH_SIZE}") | ||
|
||
for start in range(0, count, BATCH_SIZE): | ||
handle = Entrez.efetch( | ||
db="nucleotide", | ||
query_key=query_key, | ||
webenv=web_env, | ||
retstart=start, | ||
retmax=BATCH_SIZE, | ||
rettype="gb", | ||
retmode="text") | ||
|
||
yield SeqIO.parse(handle, "genbank") | ||
|
||
|
||
if __name__=="__main__": | ||
args = parse_args() | ||
|
||
with open(args.output, "w") as output_handle: | ||
for batch_results in fetch_from_esearch_history(**get_esearch_history(args.term)): | ||
SeqIO.write(batch_results, output_handle, "genbank") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
url="${1#s3://}" | ||
|
17 changes: 17 additions & 0 deletions
17
vendored/tests/transform-strain-names/transform-strain-names.t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Look for strain name in "strain" or a list of backup fields. | ||
|
||
If strain entry exists, do not do anything. | ||
|
||
$ echo '{"strain": "i/am/a/strain", "strain_s": "other"}' \ | ||
> | $TESTDIR/../../transform-strain-names \ | ||
> --strain-regex '^.+$' \ | ||
> --backup-fields strain_s accession | ||
{"strain":"i/am/a/strain","strain_s":"other"} | ||
|
||
If strain entry does not exists, search the backup fields | ||
|
||
$ echo '{"strain_s": "other"}' \ | ||
> | $TESTDIR/../../transform-strain-names \ | ||
> --strain-regex '^.+$' \ | ||
> --backup-fields accession strain_s | ||
{"strain_s":"other","strain":"other"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Verifies strain name pattern in the 'strain' field of the NDJSON record from | ||
stdin. Adds a 'strain' field to the record if it does not already exist. | ||
Outputs the modified records to stdout. | ||
""" | ||
import argparse | ||
import json | ||
import re | ||
from sys import stderr, stdin, stdout | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
) | ||
parser.add_argument("--strain-regex", default="^.+$", | ||
help="Regex pattern for strain names. " + | ||
"Strain names that do not match the pattern will be dropped.") | ||
parser.add_argument("--backup-fields", nargs="*", | ||
help="List of backup fields to use as strain name if the value in 'strain' " + | ||
"does not match the strain regex pattern. " + | ||
"If multiple fields are provided, will use the first field that has a non-empty string.") | ||
|
||
args = parser.parse_args() | ||
|
||
strain_name_pattern = re.compile(args.strain_regex) | ||
|
||
for index, record in enumerate(stdin): | ||
record = json.loads(record) | ||
|
||
# Verify strain name matches the strain regex pattern | ||
if strain_name_pattern.match(record.get('strain', '')) is None: | ||
# Default to empty string if not matching pattern | ||
record['strain'] = '' | ||
# Use non-empty value of backup fields if provided | ||
if args.backup_fields: | ||
for field in args.backup_fields: | ||
if record.get(field): | ||
record['strain'] = str(record[field]) | ||
break | ||
|
||
if record['strain'] == '': | ||
print(f"WARNING: Record number {index} has an empty string as the strain name.", file=stderr) | ||
|
||
|
||
json.dump(record, stdout, allow_nan=False, indent=None, separators=',:') | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
: "${PAT_GITHUB_DISPATCH:=}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
bin="$(dirname "$0")" | ||
|