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

fix(cli): prevent sort command from generating duplicate sequences #1264

Merged
merged 1 commit into from
Oct 3, 2023
Merged
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
47 changes: 28 additions & 19 deletions packages_rs/nextclade-cli/src/cli/nextclade_seq_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ fn writer_thread(
}

for dataset in datasets {
let name = &dataset.name;

results_csv.map_mut_fallible(|results_csv| {
results_csv.write(&SeqSortCsvEntry {
seq_name: &record.fasta_record.seq_name,
Expand All @@ -197,24 +195,22 @@ fn writer_thread(
num_hits: Some(dataset.n_hits),
})
})?;
}

let names = name
.split('/')
.scan(PathBuf::new(), |name, component| {
*name = name.join(component);
Some(name.clone())
})
.unique()
.map(path_to_string)
.collect::<Result<Vec<String>, Report>>()?;

for name in names {
let filepath = get_filepath(&name, &template, output_dir)?;

if let Some(filepath) = filepath {
let writer = get_or_insert_writer(&mut writers, filepath)?;
writer.write(&record.fasta_record.seq_name, &record.fasta_record.seq, false)?;
}
let names = datasets
.iter()
.map(|dataset| get_all_prefix_names(&dataset.name))
.collect::<Result<Vec<Vec<String>>, Report>>()?
.into_iter()
.flatten()
.unique();

for name in names {
let filepath = get_filepath(&name, &template, output_dir)?;

if let Some(filepath) = filepath {
let writer = get_or_insert_writer(&mut writers, filepath)?;
writer.write(&record.fasta_record.seq_name, &record.fasta_record.seq, false)?;
}
}
}
Expand All @@ -224,6 +220,19 @@ fn writer_thread(
Ok(())
}

pub fn get_all_prefix_names(name: impl AsRef<str>) -> Result<Vec<String>, Report> {
name
.as_ref()
.split('/')
.scan(PathBuf::new(), |name, component| {
*name = name.join(component);
Some(name.clone())
})
.unique()
.map(path_to_string)
.collect()
}

struct StatsPrinter {
enabled: bool,
stats: BTreeMap<String, usize>,
Expand Down