From 0a6a1094f7387daa92c7bbcbb1c08e2c0c3620aa Mon Sep 17 00:00:00 2001 From: Manuel Holtgrewe Date: Mon, 27 Mar 2023 13:11:12 +0200 Subject: [PATCH] fix: cases of empty sequence (#61) --- src/mapper/altseq.rs | 12 ++++++++++-- src/sequences.rs | 10 ++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/mapper/altseq.rs b/src/mapper/altseq.rs index b05607f..250880f 100644 --- a/src/mapper/altseq.rs +++ b/src/mapper/altseq.rs @@ -782,7 +782,11 @@ impl AltSeqToHgvsp { aa_end = aa_start.clone(); reference = "".to_owned(); - alternative = insertion.chars().next().unwrap().to_string(); + alternative = insertion + .chars() + .next() + .map(|c| c.to_string()) + .unwrap_or_default(); is_ext = true; } else if *is_frameshift { // frameshift @@ -793,7 +797,11 @@ impl AltSeqToHgvsp { aa_end = aa_start.clone(); reference = "".to_owned(); - alternative = insertion.chars().next().unwrap().to_string(); + alternative = insertion + .chars() + .next() + .map(|c| c.to_string()) + .unwrap_or_default(); fsext_len = insertion .find('*') diff --git a/src/sequences.rs b/src/sequences.rs index 002262e..8aca509 100644 --- a/src/sequences.rs +++ b/src/sequences.rs @@ -541,12 +541,14 @@ pub fn aa_to_aa3(seq: &str) -> Result { #[allow(dead_code)] pub fn aa1_to_aa3(seq: &str) -> Result { let mut result = String::new(); + if seq.is_empty() { + return Ok(result); + } for i in 0..seq.len() { - let aa3 = AA1_TO_AA3_LUT.get(&seq[i..(i + 1)]).ok_or(anyhow::anyhow!( - "Invalid 1-letter amino acid: {}", - &seq[i..(i + 1)] - ))?; + let aa3 = AA1_TO_AA3_LUT + .get(&seq[i..(i + 1)]) + .ok_or_else(|| anyhow::anyhow!("Invalid 1-letter amino acid: {}", &seq[i..(i + 1)]))?; result.push_str(aa3); }