From 60c06a151b49cc7ff289673ead93a059b0cd6857 Mon Sep 17 00:00:00 2001 From: Nils Homer Date: Fri, 9 Jun 2023 11:28:47 -0700 Subject: [PATCH] Do not require comment to have 4 segments (#26) * Attempt to fix 25 --- src/bin/commands/demux.rs | 46 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/bin/commands/demux.rs b/src/bin/commands/demux.rs index d144092..5e69edc 100644 --- a/src/bin/commands/demux.rs +++ b/src/bin/commands/demux.rs @@ -190,25 +190,32 @@ impl ReadSet { // Else check it's a 4-part name... fix the read number at the front and // check to see if there's a real sample barcode on the back let sep_count = chars.iter().filter(|c| **c == Self::COLON).count(); - ensure!( - sep_count == 3, - "Comment in did not have 4 segments: {}", - String::from_utf8(header.to_vec())? - ); - let first_colon_idx = chars.iter().position(|ch| *ch == Self::COLON).unwrap(); - - // Illumina, in the unmatched FASTQs, can place a "0" in the index position, sigh - let remainder = if chars.last().unwrap().is_ascii_digit() { - &chars[first_colon_idx + 1..chars.len() - 1] + if sep_count < 3 { + writer.write_all(chars)?; + if *chars.last().unwrap() != Self::COLON { + writer.write_all(&[Self::COLON])?; + } } else { - &chars[first_colon_idx + 1..chars.len()] - }; - - write!(writer, "{}:", read_num)?; - writer.write_all(remainder)?; - - if *remainder.last().unwrap() != Self::COLON { - writer.write_all(&[Self::PLUS])?; + ensure!( + sep_count == 3, + "Comment in did not have 4 segments: {}", + String::from_utf8(header.to_vec())? + ); + let first_colon_idx = chars.iter().position(|ch| *ch == Self::COLON).unwrap(); + + // Illumina, in the unmatched FASTQs, can place a "0" in the index position, sigh + let remainder = if chars.last().unwrap().is_ascii_digit() { + &chars[first_colon_idx + 1..chars.len() - 1] + } else { + &chars[first_colon_idx + 1..chars.len()] + }; + + write!(writer, "{}:", read_num)?; + writer.write_all(remainder)?; + + if *remainder.last().unwrap() != Self::COLON { + writer.write_all(&[Self::PLUS])?; + } } } } @@ -1648,13 +1655,13 @@ mod tests { } #[test] - #[should_panic(expected = "4 segments")] fn test_write_header_comment_too_few_parts() { let mut out = Vec::new(); let header = b"q1 0:0"; let barcode_segs = [seg(b"ACGT", SegmentType::SampleBarcode), seg(b"GGTT", SegmentType::SampleBarcode)]; let umi_segs = [seg(b"AACCGGTT", SegmentType::MolecularBarcode)]; + let expected = "@q1:AACCGGTT 0:0:ACGT+GGTT".to_string(); ReadSet::write_header_internal( &mut out, 1, @@ -1663,6 +1670,7 @@ mod tests { umi_segs.iter().filter(|_| true), ) .unwrap(); + assert_eq!(String::from_utf8(out).unwrap(), expected); } // ############################################################################################