Skip to content
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Unreleased

- updated Rust version to 1.90 (see #168) and dependencies (see #169)
- add more informative error message for the `build` command when input sequences contain gap characters, see #172.

## 1.2.1

Expand Down
68 changes: 67 additions & 1 deletion packages/pangraph/src/commands/build/build_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::pangraph::strand::Strand::Forward;
use crate::tree::clade::postorder;
use crate::tree::neighbor_joining::build_tree_using_neighbor_joining;
use crate::utils::progress_bar::ProgressBar;
use crate::{make_internal_error, make_internal_report};
use crate::{make_error, make_internal_error, make_internal_report};
use color_eyre::owo_colors::{AnsiColors, OwoColorize};
use color_eyre::{Help, SectionExt};
use eyre::{Report, WrapErr};
Expand Down Expand Up @@ -67,6 +67,9 @@ pub fn build_run(args: &PangraphBuildArgs) -> Result<(), Report> {

let fastas = read_many_fasta(input_fastas)?;

// Ensure that no input sequences contain gap characters
ensure_no_gap_characters(&fastas)?;

// TODO: adjust fasta letter case if `upper_case` is set
// TODO: check for duplicate fasta names

Expand All @@ -79,6 +82,33 @@ pub fn build_run(args: &PangraphBuildArgs) -> Result<(), Report> {
Ok(())
}

/// Ensure that no input sequences contain gap characters (`-`).
/// If any sequences with gaps are found, an error is returned listing the offending sequences.
fn ensure_no_gap_characters(fastas: &[FastaRecord]) -> Result<(), Report> {
let sequences_with_gaps: Vec<&str> = fastas
.iter()
.filter_map(|record| record.seq.contains_str("-").then_some(record.seq_name.as_str()))
.collect();

if sequences_with_gaps.is_empty() {
return Ok(());
}

let names_listing = sequences_with_gaps.iter().join("\n");

make_error!(
"Detected gap character '-' in {} input sequences: {}. Pangraph does not support gap characters in the input.",
sequences_with_gaps.len(),
names_listing
)
.with_section(|| names_listing.header("Sequences containing gaps:"))
.with_section(|| {
"Remove gap characters from the input FASTA before running `pangraph build`."
.color(AnsiColors::Cyan)
.header("Suggestion:")
})
}

pub fn build(fastas: Vec<FastaRecord>, args: &PangraphBuildArgs, verify: bool) -> Result<Pangraph, Report> {
// If verification is requested, we need to keep a copy of the original FASTA records
// to compare them with the sequences reconstructed from the graph.
Expand Down Expand Up @@ -171,3 +201,39 @@ pub fn build(fastas: Vec<FastaRecord>, args: &PangraphBuildArgs, verify: bool) -

Ok(graph)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::representation::seq::Seq;

#[test]
fn ensure_no_gap_characters_accepts_clean_sequences() {
let record = FastaRecord {
seq_name: "clean_seq".to_owned(),
seq: Seq::from_str("AACCGGTT"),
..Default::default()
};

ensure_no_gap_characters(std::slice::from_ref(&record)).unwrap();
}

#[test]
fn ensure_no_gap_characters_rejects_sequences_with_gaps() {
let clean = FastaRecord {
seq_name: "clean".to_owned(),
seq: Seq::from_str("ACGT"),
..Default::default()
};

let gappy = FastaRecord {
seq_name: "gappy".to_owned(),
seq: Seq::from_str("AC-GT"),
..Default::default()
};

let fastas = vec![clean, gappy];

assert!(ensure_no_gap_characters(&fastas).is_err());
}
}