From f35722f9801e379268a5cc50069aedb10cad25bf Mon Sep 17 00:00:00 2001 From: ivan-aksamentov Date: Tue, 4 Jun 2024 22:18:08 +0200 Subject: [PATCH] feat: warn when standalone ref and tree ref don't match exactly Following conversation in https://github.com/nextstrain/nextclade/pull/1455#issuecomment-2146589157 Let's add a warning if the reference sequence provided any of the possible ways (fasta in dataset files, through CLI argument, Web URL param, or Web "Customization" interface) does not exactly match (as in string comparison) the `.root_sequence.nuc` in Auspice JSON. The warning message is the following. Please suggest improvements (paste a full quote into reply message or feel free to modify in the code).
Click to expand > Nextclade detected that reference sequence provided does not exactly match reference (root) sequence in Auspice JSON. > > This could be due to one of the reasons: > > - Nextclade dataset author provided reference sequence and reference tree that are incompatible > - The reference tree has been constructed incorrectly > - The reference sequence provided using `--input-ref` CLI argument is not compatible with the reference tree in the dataset > - The reference tree provided using `--input-tree` CLI argument is not compatible with the reference sequence in the dataset > - The reference sequence provided using `&input-ref` parameter in Nextclade Web URL is not compatible with the reference tree in the dataset > - The reference tree provided using `&input-tree` parameter in Nextclade Web URL is not compatible with the reference sequence in the dataset > > This warning signals that there is a potential for failures if the mismatch is not intended.
--- .../src/dataset/dataset_download.rs | 32 ++++++++++++++++- packages/nextclade/src/run/nextclade_wasm.rs | 14 +++++++- packages/nextclade/src/tree/tree.rs | 34 +++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/nextclade-cli/src/dataset/dataset_download.rs b/packages/nextclade-cli/src/dataset/dataset_download.rs index 9fb3916be..d4c75dd60 100644 --- a/packages/nextclade-cli/src/dataset/dataset_download.rs +++ b/packages/nextclade-cli/src/dataset/dataset_download.rs @@ -12,7 +12,7 @@ use nextclade::io::fasta::{read_one_fasta, read_one_fasta_str}; use nextclade::io::file::create_file_or_stdout; use nextclade::io::fs::{ensure_dir, has_extension, read_file_to_string}; use nextclade::run::nextclade_wasm::{NextcladeParams, NextcladeParamsOptional}; -use nextclade::tree::tree::AuspiceTree; +use nextclade::tree::tree::{check_ref_seq_mismatch, AuspiceTree}; use nextclade::utils::fs::list_files_recursive; use nextclade::utils::option::OptionMapRefFallible; use nextclade::utils::string::{format_list, surround_with_quotes, Indent}; @@ -143,6 +143,12 @@ pub fn dataset_zip_load( verify_dataset_files(&virus_properties, zip.file_names()); + if let Some(tree) = &tree { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + Ok(NextcladeParams { ref_record, gene_map, @@ -283,6 +289,12 @@ pub fn dataset_dir_load( .collect_vec(); verify_dataset_files(&virus_properties, dataset_dir_files.iter()); + if let Some(tree) = &tree { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + Ok(NextcladeParams { ref_record, gene_map, @@ -325,6 +337,12 @@ pub fn dataset_json_load( .map_ref_fallible(GeneMap::from_path) .wrap_err("When parsing genome annotation")?; + if let (Some(tree), Some(ref_record)) = (&tree, &ref_record) { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + NextcladeParamsOptional { ref_record, gene_map, @@ -370,6 +388,12 @@ pub fn dataset_individual_files_load( .map_ref_fallible(AuspiceTree::from_path) .wrap_err("When reading reference tree JSON")?; + if let Some(tree) = &tree { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + Ok(NextcladeParams { ref_record, gene_map, @@ -439,6 +463,12 @@ pub fn dataset_str_download_and_load( .map_ref_fallible(AuspiceTree::from_str) .wrap_err("When reading reference tree from dataset")?; + if let Some(tree) = &tree { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + Ok(NextcladeParams { ref_record, gene_map, diff --git a/packages/nextclade/src/run/nextclade_wasm.rs b/packages/nextclade/src/run/nextclade_wasm.rs index 5b9ec9054..cb6ab44dc 100644 --- a/packages/nextclade/src/run/nextclade_wasm.rs +++ b/packages/nextclade/src/run/nextclade_wasm.rs @@ -16,7 +16,7 @@ use crate::run::nextclade_run_one::nextclade_run_one; use crate::run::params::{NextcladeInputParams, NextcladeInputParamsOptional}; use crate::translate::translate_genes::Translation; use crate::translate::translate_genes_ref::translate_genes_ref; -use crate::tree::tree::{AuspiceGraph, AuspiceTree, CladeNodeAttrKeyDesc}; +use crate::tree::tree::{check_ref_seq_mismatch, AuspiceGraph, AuspiceTree, CladeNodeAttrKeyDesc}; use crate::tree::tree_builder::graph_attach_new_nodes_in_place; use crate::tree::tree_preprocess::graph_preprocess_in_place; use crate::types::outputs::NextcladeOutputs; @@ -133,6 +133,12 @@ impl NextcladeParams { .map_ref_fallible(GeneMap::from_str) .wrap_err("When parsing genome annotation")?; + if let (Some(tree), Some(ref_record)) = (&tree, &ref_record) { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + NextcladeParamsOptional { ref_record, gene_map, @@ -160,6 +166,12 @@ impl NextcladeParams { .transpose()? .unwrap_or_default(); + if let Some(tree) = &tree { + if let Some(tree_ref) = tree.root_sequence() { + check_ref_seq_mismatch(&ref_record.seq, tree_ref)?; + } + } + Ok(Self { ref_record, gene_map, diff --git a/packages/nextclade/src/tree/tree.rs b/packages/nextclade/src/tree/tree.rs index 73415ea76..a5a6ba49c 100644 --- a/packages/nextclade/src/tree/tree.rs +++ b/packages/nextclade/src/tree/tree.rs @@ -12,6 +12,7 @@ use crate::graph::traits::{HasDivergence, HasName}; use crate::io::fs::read_file_to_string; use crate::io::json::json_parse; use eyre::{eyre, Report, WrapErr}; +use log::warn; use schemars::JsonSchema; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::collections::BTreeMap; @@ -668,4 +669,37 @@ impl AuspiceTree { pub fn map_nodes_mut(&mut self, action: fn((usize, &mut AuspiceTreeNode))) { Self::map_nodes_mut_rec(0, &mut self.tree, action); } + + pub fn root_sequence(&self) -> Option<&str> { + self + .root_sequence + .as_ref() + .and_then(|map| map.get("nuc")) + .map(String::as_str) + } +} + +pub fn check_ref_seq_mismatch( + standalone_ref_seq: impl AsRef, + tree_ref_seq: impl AsRef, +) -> Result<(), Report> { + if standalone_ref_seq.as_ref() != tree_ref_seq.as_ref() { + warn!( + r#"Nextclade detected that reference sequence provided does not exactly match reference (root) sequence in Auspice JSON. + + This could be due to one of the reasons: + + - Nextclade dataset author provided reference sequence and reference tree that are incompatible + - The reference tree has been constructed incorrectly + - The reference sequence provided using `--input-ref` CLI argument is not compatible with the reference tree in the dataset + - The reference tree provided using `--input-tree` CLI argument is not compatible with the reference sequence in the dataset + - The reference sequence provided using `&input-ref` parameter in Nextclade Web URL is not compatible with the reference tree in the dataset + - The reference tree provided using `&input-tree` parameter in Nextclade Web URL is not compatible with the reference sequence in the dataset + + This warning signals that there is a potential for failures if the mismatch is not intended. + "# + ); + } + + Ok(()) }