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

Use "accession" column as ID column #12

Merged
merged 2 commits into from
Oct 12, 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
59 changes: 47 additions & 12 deletions Snakefile
victorlin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
if not config:
configfile: "config/config_dengue.yaml"

serotypes = ['all', 'denv1', 'denv2', 'denv3', 'denv4']

rule all:
Expand Down Expand Up @@ -73,8 +76,8 @@ rule decompress:
sequences = "data/sequences_{serotype}.fasta.zst",
metadata = "data/metadata_{serotype}.tsv.zst"
output:
sequences = "results/sequences_{serotype}.fasta",
metadata = "results/metadata_{serotype}.tsv"
sequences = "data/sequences_{serotype}.fasta",
metadata = "data/metadata_{serotype}.tsv"
shell:
"""
zstd -d -c {input.sequences} > {output.sequences}
Expand All @@ -90,20 +93,22 @@ rule filter:
- excluding strains with missing region, country or date metadata
"""
input:
sequences = "results/sequences_{serotype}.fasta",
metadata = "results/metadata_{serotype}.tsv",
sequences = "data/sequences_{serotype}.fasta",
metadata = "data/metadata_{serotype}.tsv",
exclude = files.dropped_strains
output:
sequences = "results/filtered_{serotype}.fasta"
params:
group_by = "year region",
sequences_per_group = filter_sequences_per_group,
min_length = 5000
min_length = 5000,
strain_id = config.get("strain_id_field", "strain"),
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--metadata-id-columns {params.strain_id} \
--exclude {input.exclude} \
--output {output.sequences} \
--group-by {params.group_by} \
Expand Down Expand Up @@ -158,20 +163,22 @@ rule refine:
input:
tree = "results/tree-raw_{serotype}.nwk",
alignment = "results/aligned_{serotype}.fasta",
metadata = "results/metadata_{serotype}.tsv"
metadata = "data/metadata_{serotype}.tsv"
output:
tree = "results/tree_{serotype}.nwk",
node_data = "results/branch-lengths_{serotype}.json"
node_data = "results/branch-lengths_{serotype}.json",
params:
coalescent = "const",
date_inference = "marginal",
clock_filter_iqd = 4
clock_filter_iqd = 4,
strain_id = config.get("strain_id_field", "strain"),
shell:
"""
augur refine \
--tree {input.tree} \
--alignment {input.alignment} \
--metadata {input.metadata} \
--metadata-id-columns {params.strain_id} \
--output-tree {output.tree} \
--output-node-data {output.node_data} \
--timetree \
Expand Down Expand Up @@ -223,17 +230,19 @@ rule traits:
"""
input:
tree = "results/tree_{serotype}.nwk",
metadata = "results/metadata_{serotype}.tsv"
metadata = "data/metadata_{serotype}.tsv"
output:
node_data = "results/traits_{serotype}.json",
params:
columns = traits_columns,
sampling_bias_correction = 3
sampling_bias_correction = 3,
strain_id = config.get("strain_id_field", "strain"),
shell:
"""
augur traits \
--tree {input.tree} \
--metadata {input.metadata} \
--metadata-id-columns {params.strain_id} \
--output {output.node_data} \
--columns {params.columns} \
--confidence \
Expand Down Expand Up @@ -262,26 +271,52 @@ rule export:
"""Exporting data files for for auspice"""
input:
tree = "results/tree_{serotype}.nwk",
metadata = "results/metadata_{serotype}.tsv",
metadata = "data/metadata_{serotype}.tsv",
branch_lengths = "results/branch-lengths_{serotype}.json",
traits = "results/traits_{serotype}.json",
clades = "results/clades_{serotype}.json",
nt_muts = "results/nt-muts_{serotype}.json",
aa_muts = "results/aa-muts_{serotype}.json",
auspice_config = files.auspice_config
output:
auspice_json = "auspice/dengue_{serotype}.json"
auspice_json = "results/raw_dengue_{serotype}.json",
root_sequence = "results/raw_dengue_{serotype}_root-sequence.json",
params:
strain_id = config.get("strain_id_field", "strain"),
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--metadata-id-columns {params.strain_id} \
--node-data {input.branch_lengths} {input.traits} {input.clades} {input.nt_muts} {input.aa_muts} \
--auspice-config {input.auspice_config} \
--include-root-sequence \
--output {output.auspice_json}
"""

rule final_strain_name:
input:
auspice_json="results/raw_dengue_{serotype}.json",
metadata="data/metadata_{serotype}.tsv",
root_sequence="results/raw_dengue_{serotype}_root-sequence.json",
output:
auspice_json="auspice/dengue_{serotype}.json",
root_sequence="auspice/dengue_{serotype}_root-sequence.json",
params:
strain_id=config.get("strain_id_field", "strain"),
display_strain_field=config.get("display_strain_field", "strain"),
shell:
"""
python3 bin/set_final_strain_name.py \
--metadata {input.metadata} \
--metadata-id-columns {params.strain_id} \
--input-auspice-json {input.auspice_json} \
--display-strain-name {params.display_strain_field} \
--output {output.auspice_json}
cp {input.root_sequence} {output.root_sequence}
"""

rule clean:
"""Removing directories: {params}"""
params:
Expand Down
38 changes: 38 additions & 0 deletions bin/set_final_strain_name.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking

Noting that this is the third copy of this script (after monkeypox and rsv).

Seems like with GenBank data, this will be a common pattern. Maybe we should push on nextstrain/augur#1264 or nextstrain/auspice#1668 to solve this across the board.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, agree that pushing the linked issue and PR would be a more streamlined solution for the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pandas as pd
import json, argparse
from augur.io import read_metadata

def replace_name_recursive(node, lookup):
if node["name"] in lookup:
node["name"] = lookup[node["name"]]

if "children" in node:
for child in node["children"]:
replace_name_recursive(child, lookup)

if __name__=="__main__":
parser = argparse.ArgumentParser(
description="Swaps out the strain names in the Auspice JSON with the final strain name",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument('--input-auspice-json', type=str, required=True, help="input auspice_json")
parser.add_argument('--metadata', type=str, required=True, help="input data")
parser.add_argument('--metadata-id-columns', nargs="+", help="names of possible metadata columns containing identifier information, ordered by priority. Only one ID column will be inferred.")
parser.add_argument('--display-strain-name', type=str, required=True, help="field to use as strain name in auspice")
parser.add_argument('--output', type=str, metavar="JSON", required=True, help="output Auspice JSON")
args = parser.parse_args()

metadata = read_metadata(args.metadata, id_columns=args.metadata_id_columns)
name_lookup = {}
for ri, row in metadata.iterrows():
strain_id = row.name
name_lookup[strain_id] = args.display_strain_name if pd.isna(row[args.display_strain_name]) else row[args.display_strain_name]

with open(args.input_auspice_json, 'r') as fh:
data = json.load(fh)

replace_name_recursive(data['tree'], name_lookup)

with open(args.output, 'w') as fh:
json.dump(data, fh)
2 changes: 2 additions & 0 deletions config/config_dengue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
strain_id_field: "accession"
display_strain_field: "strain"
Binary file modified example_data/sequences_all.fasta.zst
Binary file not shown.
Binary file modified example_data/sequences_denv1.fasta.zst
Binary file not shown.
Binary file modified example_data/sequences_denv2.fasta.zst
Binary file not shown.
Binary file modified example_data/sequences_denv3.fasta.zst
Binary file not shown.
Binary file modified example_data/sequences_denv4.fasta.zst
Binary file not shown.