From 7e321a78c4390396134d4b3cf8e6ffbdf4104eea Mon Sep 17 00:00:00 2001 From: Emilia Date: Sat, 10 Aug 2024 00:46:49 -0400 Subject: [PATCH] refactor: Well struct Will require bump to lib version!! --- plate-tool-lib/src/csv/auto.rs | 11 +- plate-tool-lib/src/csv/conversion.rs | 10 +- plate-tool-lib/src/lib.rs | 2 + plate-tool-lib/src/transfer_region.rs | 157 ++++++++++++++------------ plate-tool-lib/src/transfer_volume.rs | 11 ++ plate-tool-lib/src/well.rs | 40 +++++++ 6 files changed, 146 insertions(+), 85 deletions(-) create mode 100644 plate-tool-lib/src/transfer_volume.rs create mode 100644 plate-tool-lib/src/well.rs diff --git a/plate-tool-lib/src/csv/auto.rs b/plate-tool-lib/src/csv/auto.rs index 5d08b6c..6a2ff52 100644 --- a/plate-tool-lib/src/csv/auto.rs +++ b/plate-tool-lib/src/csv/auto.rs @@ -7,6 +7,7 @@ use super::{string_well_to_pt, TransferRecord, read_csv}; use crate::plate::{PlateFormat, PlateType}; use crate::plate_instances::PlateInstance; use crate::transfer::Transfer; +use crate::well::Well; use crate::transfer_region::{CustomRegion, Region, TransferRegion}; use std::collections::HashSet; @@ -107,7 +108,7 @@ fn find_unique_plates(records: &[TransferRecord]) -> UniquePlates { fn guess_plate_size(plate_filtered_records: &[&TransferRecord], which: PlateType) -> PlateFormat { let mut guess = PlateFormat::W96; // Never guess smaller than 96 for record in plate_filtered_records { - if let Some((row, col)) = string_well_to_pt(match which { + if let Some(Well {col, row} ) = string_well_to_pt(match which { PlateType::Source => &record.source_well, PlateType::Destination => &record.destination_well, }) { @@ -169,8 +170,8 @@ fn get_transfer_for_pair( return None; } - let mut source_wells: HashSet<(u8, u8)> = HashSet::new(); - let mut destination_wells: HashSet<(u8, u8)> = HashSet::new(); + let mut source_wells: HashSet = HashSet::new(); + let mut destination_wells: HashSet = HashSet::new(); for record in filtered_records { let source_point_opt = string_well_to_pt(&record.source_well); let destination_point_opt = string_well_to_pt(&record.destination_well); @@ -183,8 +184,8 @@ fn get_transfer_for_pair( destination_wells.insert(destination_point); } } - let source_wells_vec: Vec<(u8, u8)> = source_wells.into_iter().collect(); - let destination_wells_vec: Vec<(u8, u8)> = destination_wells.into_iter().collect(); + let source_wells_vec: Vec = source_wells.into_iter().collect(); + let destination_wells_vec: Vec = destination_wells.into_iter().collect(); let custom_region: Region = Region::Custom(CustomRegion::new(source_wells_vec, destination_wells_vec)); diff --git a/plate-tool-lib/src/csv/conversion.rs b/plate-tool-lib/src/csv/conversion.rs index 48a9e37..398d322 100644 --- a/plate-tool-lib/src/csv/conversion.rs +++ b/plate-tool-lib/src/csv/conversion.rs @@ -1,4 +1,4 @@ -use crate::transfer::Transfer; +use crate::{transfer::Transfer, well::Well}; use crate::util::*; use super::{alternative_formats::EchoClientTransferRecord, mangle_headers::mangle_headers, transfer_record::TransferRecordDeserializeIntermediate, TransferRecord}; @@ -22,9 +22,9 @@ pub fn transfer_to_records( for d_well in dest_wells { records.push(TransferRecord { source_plate: src_barcode.to_string(), - source_well: format!("{}{}", num_to_letters(s_well.0).unwrap(), s_well.1), + source_well: format!("{}{}", num_to_letters(s_well.col).unwrap(), s_well.row), destination_plate: dest_barcode.to_string(), - destination_well: format!("{}{}", num_to_letters(d_well.0).unwrap(), d_well.1), + destination_well: format!("{}{}", num_to_letters(d_well.col).unwrap(), d_well.row), volume: tr.volume, concentration: None, }) @@ -53,7 +53,7 @@ pub fn records_to_echo_client_csv(trs: Vec) -> Result Option<(u8, u8)> { +pub fn string_well_to_pt(input: &str) -> Option { lazy_static! { static ref REGEX: Regex = Regex::new(r"([A-Z,a-z]+)(\d+)").unwrap(); @@ -62,7 +62,7 @@ pub fn string_well_to_pt(input: &str) -> Option<(u8, u8)> { } if let Some(c1) = REGEX.captures(input) { if let (Some(row), Some(col)) = (letters_to_num(&c1[1]), c1[2].parse::().ok()) { - return Some((row, col)) + return Some((row, col).into()) } else { return None } diff --git a/plate-tool-lib/src/lib.rs b/plate-tool-lib/src/lib.rs index a85a4bb..44e3f0c 100644 --- a/plate-tool-lib/src/lib.rs +++ b/plate-tool-lib/src/lib.rs @@ -3,4 +3,6 @@ pub mod plate; pub mod plate_instances; pub mod transfer; pub mod transfer_region; +pub mod transfer_volume; +pub mod well; pub mod util; diff --git a/plate-tool-lib/src/transfer_region.rs b/plate-tool-lib/src/transfer_region.rs index 31b4a57..29463c1 100644 --- a/plate-tool-lib/src/transfer_region.rs +++ b/plate-tool-lib/src/transfer_region.rs @@ -2,30 +2,32 @@ use serde::{Deserialize, Serialize}; use super::plate::Plate; +use super::well::Well; + #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub struct CustomRegion { - src: Vec<(u8, u8)>, - dest: Vec<(u8, u8)>, + src: Vec, + dest: Vec, } impl CustomRegion { - pub fn new(src: Vec<(u8, u8)>, dest: Vec<(u8, u8)>) -> Self { + pub fn new(src: Vec, dest: Vec) -> Self { CustomRegion { src, dest } } } #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub enum Region { - Rect((u8, u8), (u8, u8)), - Point((u8, u8)), + Rect(Well, Well), + Point(Well), Custom(CustomRegion), } impl Default for Region { fn default() -> Self { - Region::Point((1, 1)) + Region::Point(Well { row: 1, col: 1 }) } } -impl TryFrom for ((u8, u8), (u8, u8)) { +impl TryFrom for (Well, Well) { type Error = &'static str; fn try_from(region: Region) -> Result { if let Region::Rect(c1, c2) = region { @@ -37,12 +39,11 @@ impl TryFrom for ((u8, u8), (u8, u8)) { } } -type Corner = (u8, u8); -type Rectangle = (Corner, Corner); +type Rectangle = (Well, Well); impl Region { pub fn new_custom(transfers: &Vec) -> Self { - let mut src_pts: Vec<(u8, u8)> = Vec::with_capacity(transfers.len()); - let mut dest_pts: Vec<(u8, u8)> = Vec::with_capacity(transfers.len()); + let mut src_pts: Vec = Vec::with_capacity(transfers.len()); + let mut dest_pts: Vec = Vec::with_capacity(transfers.len()); for transfer in transfers { src_pts.push(transfer.0); @@ -80,10 +81,10 @@ impl Default for TransferRegion { } impl TransferRegion { - pub fn get_source_wells(&self) -> Vec<(u8, u8)> { + pub fn get_source_wells(&self) -> Vec { match &self.source_region { Region::Rect(c1, c2) => { - let mut wells = Vec::<(u8, u8)>::new(); + let mut wells = Vec::::new(); let (ul, br) = standardize_rectangle(c1, c2); let (interleave_i, interleave_j) = self.interleave_source; // NOTE: This will panic if either is 0! @@ -93,12 +94,12 @@ impl TransferRegion { let (interleave_i, interleave_j) = (i8::max(interleave_i, 1), i8::max(interleave_j, 1)); - for i in (ul.0..=br.0).step_by(i8::abs(interleave_i) as usize) { - for j in (ul.1..=br.1).step_by(i8::abs(interleave_j) as usize) { + for i in (ul.col..=br.col).step_by(i8::abs(interleave_i) as usize) { + for j in (ul.row..=br.row).step_by(i8::abs(interleave_j) as usize) { // NOTE: It looks like we're ignoring negative interleaves, // because it wouldn't make a difference here---the same // wells will still be involved in the transfer. - wells.push((i, j)) + wells.push(Well { col: i, row: j }) } } wells @@ -108,14 +109,14 @@ impl TransferRegion { } } - pub fn get_destination_wells(&self) -> Vec<(u8, u8)> { + pub fn get_destination_wells(&self) -> Vec { match &self.source_region { Region::Custom(c) => c.dest.clone(), _ => { let map = self.calculate_map(); let source_wells = self.get_source_wells(); - let mut wells = Vec::<(u8, u8)>::new(); + let mut wells = Vec::::new(); for well in source_wells { if let Some(mut dest_wells) = map(well) { @@ -129,14 +130,14 @@ impl TransferRegion { } #[allow(clippy::type_complexity)] // Resolving gives inherent associated type error - pub fn calculate_map(&self) -> Box Option> + '_> { + pub fn calculate_map(&self) -> Box Option> + '_> { // By validating first, we have a stronger guarantee that // this function will not panic. :) // log::debug!("Validating: {:?}", self.validate()); if let Err(msg) = self.validate() { eprintln!("{}", msg); eprintln!("This transfer will be empty."); - return Box::new(|(_, _)| None); + return Box::new(|_| None); } // log::debug!("What is ild? {:?}", self); @@ -144,10 +145,10 @@ impl TransferRegion { let il_dest = self.interleave_dest; let il_source = self.interleave_source; - let source_corners: ((u8, u8), (u8, u8)) = match self.source_region { - Region::Point((x, y)) => ((x, y), (x, y)), + let source_corners: (Well, Well) = match self.source_region { + Region::Point(well) => (well, well), Region::Rect(c1, c2) => (c1, c2), - Region::Custom(_) => ((0, 0), (0, 0)), + Region::Custom(_) => (Well { col: 0, row: 0 }, Well { col: 0, row: 0 }), }; let (source_ul, _) = standardize_rectangle(&source_corners.0, &source_corners.1); // This map is not necessarily injective or surjective, @@ -157,43 +158,43 @@ impl TransferRegion { // Non-replicate transfers: match &self.dest_region { - Region::Point((x, y)) => { - Box::new(move |(i, j)| { - if source_wells.contains(&(i, j)) { + Region::Point(well) => { + Box::new(move |Well { col: i, row: j }| { + if source_wells.contains(&Well { col: i, row: j }) { // Validity here already checked by self.validate() - Some(vec![( - x + i - .checked_sub(source_ul.0) - .expect("Point cannot have been less than UL") - .checked_div(il_source.0.unsigned_abs()) - .expect("Source interleave cannot be 0") - .mul(il_dest.0.unsigned_abs()), - y + j - .checked_sub(source_ul.1) - .expect("Point cannot have been less than UL") - .checked_div(il_source.1.unsigned_abs()) - .expect("Source interleave cannot be 0") - .mul(il_dest.1.unsigned_abs()), - )]) + Some(vec![Well { + col: well.col + + i.checked_sub(source_ul.col) + .expect("Point cannot have been less than UL") + .checked_div(il_source.0.unsigned_abs()) + .expect("Source interleave cannot be 0") + .mul(il_dest.0.unsigned_abs()), + row: well.row + + j.checked_sub(source_ul.row) + .expect("Point cannot have been less than UL") + .checked_div(il_source.1.unsigned_abs()) + .expect("Source interleave cannot be 0") + .mul(il_dest.1.unsigned_abs()), + }]) } else { None } }) } Region::Rect(c1, c2) => { - Box::new(move |(i, j)| { - if source_wells.contains(&(i, j)) { + Box::new(move |Well { col: i, row: j }| { + if source_wells.contains(&Well { col: i, row: j }) { let possible_destination_wells = create_dense_rectangle(c1, c2); let (d_ul, d_br) = standardize_rectangle(c1, c2); let (s_ul, s_br) = standardize_rectangle(&source_corners.0, &source_corners.1); let s_dims = ( - s_br.0.checked_sub(s_ul.0).unwrap() + 1, - s_br.1.checked_sub(s_ul.1).unwrap() + 1, + s_br.col.checked_sub(s_ul.col).unwrap() + 1, + s_br.row.checked_sub(s_ul.row).unwrap() + 1, ); let d_dims = ( - d_br.0.checked_sub(d_ul.0).unwrap() + 1, - d_br.1.checked_sub(d_ul.1).unwrap() + 1, + d_br.col.checked_sub(d_ul.col).unwrap() + 1, + d_br.row.checked_sub(d_ul.row).unwrap() + 1, ); let number_used_src_wells = ( // Number of used source wells @@ -222,34 +223,34 @@ impl TransferRegion { .unwrap() as u8, ); let i = i - .saturating_sub(s_ul.0) + .saturating_sub(s_ul.col) .saturating_div(il_source.0.unsigned_abs()); let j = j - .saturating_sub(s_ul.1) + .saturating_sub(s_ul.row) .saturating_div(il_source.1.unsigned_abs()); Some( possible_destination_wells .into_iter() - .filter(|(x, _)| { - x.checked_sub(d_ul.0).unwrap() + .filter(|Well { col, .. }| { + col.checked_sub(d_ul.col).unwrap() % (number_used_src_wells.0 * il_dest.0.unsigned_abs()) // Counter along x == (il_dest.0.unsigned_abs() *i) % (number_used_src_wells.0 * il_dest.0.unsigned_abs()) }) - .filter(|(_, y)| { - y.checked_sub(d_ul.1).unwrap() + .filter(|Well { row, .. }| { + row.checked_sub(d_ul.row).unwrap() % (number_used_src_wells.1 * il_dest.1.unsigned_abs()) // Counter along u == (il_dest.1.unsigned_abs() *j) % (number_used_src_wells.1 * il_dest.1.unsigned_abs()) }) - .filter(|(x, y)| { + .filter(|Well { col, row }| { // How many times have we replicated? < How many are we allowed // to replicate? - x.checked_sub(d_ul.0).unwrap().div_euclid( + col.checked_sub(d_ul.col).unwrap().div_euclid( number_used_src_wells.0 * il_dest.0.unsigned_abs(), ) < count.0 - && y.checked_sub(d_ul.1).unwrap().div_euclid( + && row.checked_sub(d_ul.row).unwrap().div_euclid( number_used_src_wells.1 * il_dest.1.unsigned_abs(), ) < count.1 }) @@ -260,14 +261,14 @@ impl TransferRegion { } }) } - Region::Custom(c) => Box::new(move |(i, j)| { + Region::Custom(c) => Box::new(move |Well { col: i, row: j }| { let src = c.src.clone(); let dest = c.dest.clone(); - let points: Vec<(u8, u8)> = src + let points: Vec = src .iter() .enumerate() - .filter(|(_index, (x, y))| *x == i && *y == j) + .filter(|(_index, Well { col, row })| *col == i && *row == j) .map(|(index, _)| dest[index]) .collect(); if points.is_empty() { @@ -296,15 +297,15 @@ impl TransferRegion { // later Region::Rect(s1, s2) => { // Check if all source wells exist: - if s1.0 == 0 || s1.1 == 0 || s2.0 == 0 || s2.1 == 0 { + if s1.col == 0 || s1.row == 0 || s2.col == 0 || s2.row == 0 { return Err("Source region is out-of-bounds! (Too small)"); } // Sufficient to check if the corners are in-bounds let source_max = self.source_plate.size(); - if s1.0 > source_max.0 || s2.0 > source_max.0 { + if s1.col > source_max.0 || s2.col > source_max.0 { return Err("Source region is out-of-bounds! (Too tall)"); } - if s1.1 > source_max.1 || s2.1 > source_max.1 { + if s1.row > source_max.1 || s2.row > source_max.1 { // log::debug!("s1.1: {}, max.1: {}", s1.1, source_max.1); return Err("Source region is out-of-bounds! (Too wide)"); } @@ -325,28 +326,34 @@ impl TransferRegion { } } -fn create_dense_rectangle(c1: &(u8, u8), c2: &(u8, u8)) -> Vec<(u8, u8)> { +fn create_dense_rectangle(c1: &Well, c2: &Well) -> Vec { // Creates a vector of every point between two corners let (c1, c2) = standardize_rectangle(c1, c2); - let mut points = Vec::<(u8, u8)>::new(); - for i in c1.0..=c2.0 { - for j in c1.1..=c2.1 { - points.push((i, j)); + let mut points = Vec::::new(); + for i in c1.col..=c2.col { + for j in c1.row..=c2.row { + points.push(Well { col: i, row: j }); } } points } -fn standardize_rectangle(c1: &(u8, u8), c2: &(u8, u8)) -> ((u8, u8), (u8, u8)) { - let upper_left_i = u8::min(c1.0, c2.0); - let upper_left_j = u8::min(c1.1, c2.1); - let bottom_right_i = u8::max(c1.0, c2.0); - let bottom_right_j = u8::max(c1.1, c2.1); +fn standardize_rectangle(c1: &Well, c2: &Well) -> (Well, Well) { + let upper_left_i = u8::min(c1.col, c2.col); + let upper_left_j = u8::min(c1.row, c2.row); + let bottom_right_i = u8::max(c1.col, c2.col); + let bottom_right_j = u8::max(c1.row, c2.row); ( - (upper_left_i, upper_left_j), - (bottom_right_i, bottom_right_j), + Well { + col: upper_left_i, + row: upper_left_j, + }, + Well { + col: bottom_right_i, + row: bottom_right_j, + }, ) } @@ -363,7 +370,7 @@ impl fmt::Display for TransferRegion { let mut source_string = String::new(); for i in 1..=source_dims.0 { for j in 1..=source_dims.1 { - if source_wells.contains(&(i, j)) { + if source_wells.contains(&Well { col: i, row: j }) { source_string.push('x') } else { source_string.push('.') @@ -379,7 +386,7 @@ impl fmt::Display for TransferRegion { let mut dest_string = String::new(); for i in 1..=dest_dims.0 { for j in 1..=dest_dims.1 { - if dest_wells.contains(&(i, j)) { + if dest_wells.contains(&Well{col: i, row: j}) { dest_string.push('x') } else { dest_string.push('.') diff --git a/plate-tool-lib/src/transfer_volume.rs b/plate-tool-lib/src/transfer_volume.rs new file mode 100644 index 0000000..b3b7bbf --- /dev/null +++ b/plate-tool-lib/src/transfer_volume.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; +use serde::{Serialize, Deserialize}; + +/* +#[non_exhaustive] +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub enum TransferVolume { + Single(f32), + WellMap(HashMap<>), +} +*/ diff --git a/plate-tool-lib/src/well.rs b/plate-tool-lib/src/well.rs new file mode 100644 index 0000000..c6b9baf --- /dev/null +++ b/plate-tool-lib/src/well.rs @@ -0,0 +1,40 @@ +use lazy_static::lazy_static; +use regex::Regex; + +use serde::{Deserialize, Serialize}; + +use super::util::letters_to_num; + +#[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug, Hash)] +pub struct Well { + pub row: u8, + pub col: u8, +} + +impl Well { + pub fn string_well_to_pt(input: &str) -> Option { + lazy_static! { + static ref REGEX: Regex = Regex::new(r"([A-Z,a-z]+)(\d+)").unwrap(); + + // Can this be removed? + static ref REGEX_ALT: Regex = Regex::new(r"(\d+)").unwrap(); + } + if let Some(c1) = REGEX.captures(input) { + if let (Some(row), Some(col)) = (letters_to_num(&c1[1]), c1[2].parse::().ok()) { + return Some(Well { row, col }); + } else { + return None; + } + } + None + } +} + +impl Into for (u8, u8) { + fn into(self) -> Well { + Well { + col: self.0, + row: self.1, + } + } +}