From 90d3b2cb055142085c92802e5bec957243b6453b Mon Sep 17 00:00:00 2001 From: Martin Larralde <martin.larralde@embl.de> Date: Tue, 18 Jul 2023 02:29:30 +0200 Subject: [PATCH] Avoid error on missing symbols in `CountMatrix.__init__` --- lightmotif-py/lightmotif/lib.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lightmotif-py/lightmotif/lib.rs b/lightmotif-py/lightmotif/lib.rs index bde5e98..6236381 100644 --- a/lightmotif-py/lightmotif/lib.rs +++ b/lightmotif-py/lightmotif/lib.rs @@ -124,22 +124,18 @@ impl CountMatrix { let mut data: Option<DenseMatrix<u32, <lightmotif::Dna as Alphabet>::K>> = None; for s in lightmotif::Dna::symbols() { let key = String::from(s.as_char()); - let column = values - .get_item(&key) - .ok_or(PyKeyError::new_err(key))? - .downcast::<PyList>()?; - - if data.is_none() { - data = Some(DenseMatrix::new(column.len())); - } - - let matrix = data.as_mut().unwrap(); - if matrix.rows() != column.len() { - return Err(PyValueError::new_err("Invalid number of rows")); - } - - for (i, x) in column.iter().enumerate() { - matrix[i][s.as_index()] = x.extract::<u32>()?; + if let Some(res) = values.get_item(&key) { + let column = res.downcast::<PyList>()?; + if data.is_none() { + data = Some(DenseMatrix::new(column.len())); + } + let matrix = data.as_mut().unwrap(); + if matrix.rows() != column.len() { + return Err(PyValueError::new_err("Invalid number of rows")); + } + for (i, x) in column.iter().enumerate() { + matrix[i][s.as_index()] = x.extract::<u32>()?; + } } } -- GitLab