From 99c45b782ee324b56812aeaf0f62d688895443d4 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 8 Jul 2019 14:19:25 +0200 Subject: [PATCH] allow translation of empty and multiline sequences --- src/dna/mod.rs | 15 ++++++++++++++- src/main.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/dna/mod.rs b/src/dna/mod.rs index d0d777a..1b11efa 100644 --- a/src/dna/mod.rs +++ b/src/dna/mod.rs @@ -75,10 +75,23 @@ impl<'a> From<&'a [u8]> for Strand { } } +impl<'a> From<&'a Vec> for Strand { + fn from(lines: &Vec) -> Self { + Strand(lines.iter() + .flat_map(|s| s.as_bytes()) + .map(Nucleotide::from) + .collect()) + } +} + impl Strand { /// A reading frame of this strand, 1-indexed. pub fn frame<'a>(&'a self, start: usize) -> Frame<'a> { - Frame(&self.0[start - 1..]) + Frame(if self.0.len() > start - 1 { + &self.0[start - 1..] + } else { + &[] + }) } /// The reverse strand of this one. diff --git a/src/main.rs b/src/main.rs index d2fab9d..ffc9748 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ fn translate(args: args::Translate) -> Result<()> { for record in fasta::Reader::new(io::stdin(), true).records() { let fasta::Record { header, sequence } = record?; - let forward = Strand::from(sequence[0].as_bytes()); + let forward = Strand::from(&sequence); let reverse = forward.reversed(); for &(name, frame, reversed) in &frames { let strand = if reversed { &reverse } else { &forward };