From de67200a4351d6fd23e1ce67129b3831b3d29b8e Mon Sep 17 00:00:00 2001
From: Martin Larralde <martin.larralde@embl.de>
Date: Fri, 14 Jun 2024 23:20:39 +0200
Subject: [PATCH] Fix striping of empty sequence panicking with AVX2
 implementation

---
 lightmotif/src/pli/platform/avx2.rs |  5 +++++
 lightmotif/src/seq.rs               | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/lightmotif/src/pli/platform/avx2.rs b/lightmotif/src/pli/platform/avx2.rs
index d4ff09f..b8f262c 100644
--- a/lightmotif/src/pli/platform/avx2.rs
+++ b/lightmotif/src/pli/platform/avx2.rs
@@ -406,6 +406,11 @@ unsafe fn stripe_avx2<A>(
     let mut matrix = std::mem::take(striped).into_matrix();
     matrix.resize(src_stride);
 
+    // Early exit if sequence is too empty (no allocated matrix).
+    if length == 0 {
+        return;
+    }
+
     /// Get a pointer to the matrix
     let mut out = matrix[0].as_mut_ptr();
     let out_stride = matrix.stride();
diff --git a/lightmotif/src/seq.rs b/lightmotif/src/seq.rs
index 1179a22..023a1d1 100644
--- a/lightmotif/src/seq.rs
+++ b/lightmotif/src/seq.rs
@@ -347,6 +347,18 @@ mod test {
     use crate::abc::Dna;
     use crate::abc::Nucleotide::*;
 
+    #[test]
+    fn test_empty() {
+        let seq = EncodedSequence::<Dna>::from_str("").unwrap();
+
+        let pli = Pipeline::generic();
+        let striped = <Pipeline<_, _> as Stripe<Dna, U2>>::stripe(&pli, &seq);
+        assert_eq!(striped.matrix().rows(), 0);
+
+        let striped = seq.to_striped();
+        assert_eq!(striped.matrix().rows(), 0);
+    }
+
     #[test]
     fn test_stripe() {
         let pli = Pipeline::generic();
-- 
GitLab