From ca142ef5943bdae03d2698681a8ec82af9ab407a Mon Sep 17 00:00:00 2001 From: Emilia Date: Sat, 10 Aug 2024 01:40:25 -0400 Subject: [PATCH] Revert "refactor: Well struct" This reverts commit 7e321a78c4390396134d4b3cf8e6ffbdf4104eea. --- 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, 85 insertions(+), 146 deletions(-) delete mode 100644 plate-tool-lib/src/transfer_volume.rs delete 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 6a2ff52..5d08b6c 100644 --- a/plate-tool-lib/src/csv/auto.rs +++ b/plate-tool-lib/src/csv/auto.rs @@ -7,7 +7,6 @@ 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; @@ -108,7 +107,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(Well {col, row} ) = string_well_to_pt(match which { + if let Some((row, col)) = string_well_to_pt(match which { PlateType::Source => &record.source_well, PlateType::Destination => &record.destination_well, }) { @@ -170,8 +169,8 @@ fn get_transfer_for_pair( return None; } - let mut source_wells: HashSet = HashSet::new(); - let mut destination_wells: HashSet = HashSet::new(); + let mut source_wells: HashSet<(u8, u8)> = HashSet::new(); + let mut destination_wells: HashSet<(u8, u8)> = 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); @@ -184,8 +183,8 @@ fn get_transfer_for_pair( destination_wells.insert(destination_point); } } - let source_wells_vec: Vec = source_wells.into_iter().collect(); - let destination_wells_vec: Vec = destination_wells.into_iter().collect(); + 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 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 398d322..48a9e37 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, well::Well}; +use crate::transfer::Transfer; 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.col).unwrap(), s_well.row), + source_well: format!("{}{}", num_to_letters(s_well.0).unwrap(), s_well.1), destination_plate: dest_barcode.to_string(), - destination_well: format!("{}{}", num_to_letters(d_well.col).unwrap(), d_well.row), + destination_well: format!("{}{}", num_to_letters(d_well.0).unwrap(), d_well.1), volume: tr.volume, concentration: None, }) @@ -53,7 +53,7 @@ pub fn records_to_echo_client_csv(trs: Vec) -> Result Option { +pub fn string_well_to_pt(input: &str) -> Option<(u8, u8)> { 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 { } 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).into()) + return Some((row, col)) } else { return None } diff --git a/plate-tool-lib/src/lib.rs b/plate-tool-lib/src/lib.rs index 44e3f0c..a85a4bb 100644 --- a/plate-tool-lib/src/lib.rs +++ b/plate-tool-lib/src/lib.rs @@ -3,6 +3,4 @@ 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 29463c1..31b4a57 100644 --- a/plate-tool-lib/src/transfer_region.rs +++ b/plate-tool-lib/src/transfer_region.rs @@ -2,32 +2,30 @@ use serde::{Deserialize, Serialize}; use super::plate::Plate; -use super::well::Well; - #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub struct CustomRegion { - src: Vec, - dest: Vec, + src: Vec<(u8, u8)>, + dest: Vec<(u8, u8)>, } impl CustomRegion { - pub fn new(src: Vec, dest: Vec) -> Self { + pub fn new(src: Vec<(u8, u8)>, dest: Vec<(u8, u8)>) -> Self { CustomRegion { src, dest } } } #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub enum Region { - Rect(Well, Well), - Point(Well), + Rect((u8, u8), (u8, u8)), + Point((u8, u8)), Custom(CustomRegion), } impl Default for Region { fn default() -> Self { - Region::Point(Well { row: 1, col: 1 }) + Region::Point((1, 1)) } } -impl TryFrom for (Well, Well) { +impl TryFrom for ((u8, u8), (u8, u8)) { type Error = &'static str; fn try_from(region: Region) -> Result { if let Region::Rect(c1, c2) = region { @@ -39,11 +37,12 @@ impl TryFrom for (Well, Well) { } } -type Rectangle = (Well, Well); +type Corner = (u8, u8); +type Rectangle = (Corner, Corner); impl Region { pub fn new_custom(transfers: &Vec) -> Self { - let mut src_pts: Vec = Vec::with_capacity(transfers.len()); - let mut dest_pts: Vec = Vec::with_capacity(transfers.len()); + let mut src_pts: Vec<(u8, u8)> = Vec::with_capacity(transfers.len()); + let mut dest_pts: Vec<(u8, u8)> = Vec::with_capacity(transfers.len()); for transfer in transfers { src_pts.push(transfer.0); @@ -81,10 +80,10 @@ impl Default for TransferRegion { } impl TransferRegion { - pub fn get_source_wells(&self) -> Vec { + pub fn get_source_wells(&self) -> Vec<(u8, u8)> { match &self.source_region { Region::Rect(c1, c2) => { - let mut wells = Vec::::new(); + let mut wells = Vec::<(u8, u8)>::new(); let (ul, br) = standardize_rectangle(c1, c2); let (interleave_i, interleave_j) = self.interleave_source; // NOTE: This will panic if either is 0! @@ -94,12 +93,12 @@ impl TransferRegion { let (interleave_i, interleave_j) = (i8::max(interleave_i, 1), i8::max(interleave_j, 1)); - 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) { + 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) { // 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(Well { col: i, row: j }) + wells.push((i, j)) } } wells @@ -109,14 +108,14 @@ impl TransferRegion { } } - pub fn get_destination_wells(&self) -> Vec { + pub fn get_destination_wells(&self) -> Vec<(u8, u8)> { 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::::new(); + let mut wells = Vec::<(u8, u8)>::new(); for well in source_wells { if let Some(mut dest_wells) = map(well) { @@ -130,14 +129,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); @@ -145,10 +144,10 @@ impl TransferRegion { let il_dest = self.interleave_dest; let il_source = self.interleave_source; - let source_corners: (Well, Well) = match self.source_region { - Region::Point(well) => (well, well), + let source_corners: ((u8, u8), (u8, u8)) = match self.source_region { + Region::Point((x, y)) => ((x, y), (x, y)), Region::Rect(c1, c2) => (c1, c2), - Region::Custom(_) => (Well { col: 0, row: 0 }, Well { col: 0, row: 0 }), + Region::Custom(_) => ((0, 0), (0, 0)), }; let (source_ul, _) = standardize_rectangle(&source_corners.0, &source_corners.1); // This map is not necessarily injective or surjective, @@ -158,43 +157,43 @@ impl TransferRegion { // Non-replicate transfers: match &self.dest_region { - Region::Point(well) => { - Box::new(move |Well { col: i, row: j }| { - if source_wells.contains(&Well { col: i, row: j }) { + Region::Point((x, y)) => { + Box::new(move |(i, j)| { + if source_wells.contains(&(i, j)) { // Validity here already checked by self.validate() - 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()), - }]) + 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()), + )]) } else { None } }) } Region::Rect(c1, c2) => { - Box::new(move |Well { col: i, row: j }| { - if source_wells.contains(&Well { col: i, row: j }) { + Box::new(move |(i, j)| { + if source_wells.contains(&(i, 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.col.checked_sub(s_ul.col).unwrap() + 1, - s_br.row.checked_sub(s_ul.row).unwrap() + 1, + s_br.0.checked_sub(s_ul.0).unwrap() + 1, + s_br.1.checked_sub(s_ul.1).unwrap() + 1, ); let d_dims = ( - d_br.col.checked_sub(d_ul.col).unwrap() + 1, - d_br.row.checked_sub(d_ul.row).unwrap() + 1, + d_br.0.checked_sub(d_ul.0).unwrap() + 1, + d_br.1.checked_sub(d_ul.1).unwrap() + 1, ); let number_used_src_wells = ( // Number of used source wells @@ -223,34 +222,34 @@ impl TransferRegion { .unwrap() as u8, ); let i = i - .saturating_sub(s_ul.col) + .saturating_sub(s_ul.0) .saturating_div(il_source.0.unsigned_abs()); let j = j - .saturating_sub(s_ul.row) + .saturating_sub(s_ul.1) .saturating_div(il_source.1.unsigned_abs()); Some( possible_destination_wells .into_iter() - .filter(|Well { col, .. }| { - col.checked_sub(d_ul.col).unwrap() + .filter(|(x, _)| { + x.checked_sub(d_ul.0).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(|Well { row, .. }| { - row.checked_sub(d_ul.row).unwrap() + .filter(|(_, y)| { + y.checked_sub(d_ul.1).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(|Well { col, row }| { + .filter(|(x, y)| { // How many times have we replicated? < How many are we allowed // to replicate? - col.checked_sub(d_ul.col).unwrap().div_euclid( + x.checked_sub(d_ul.0).unwrap().div_euclid( number_used_src_wells.0 * il_dest.0.unsigned_abs(), ) < count.0 - && row.checked_sub(d_ul.row).unwrap().div_euclid( + && y.checked_sub(d_ul.1).unwrap().div_euclid( number_used_src_wells.1 * il_dest.1.unsigned_abs(), ) < count.1 }) @@ -261,14 +260,14 @@ impl TransferRegion { } }) } - Region::Custom(c) => Box::new(move |Well { col: i, row: j }| { + Region::Custom(c) => Box::new(move |(i, j)| { let src = c.src.clone(); let dest = c.dest.clone(); - let points: Vec = src + let points: Vec<(u8, u8)> = src .iter() .enumerate() - .filter(|(_index, Well { col, row })| *col == i && *row == j) + .filter(|(_index, (x, y))| *x == i && *y == j) .map(|(index, _)| dest[index]) .collect(); if points.is_empty() { @@ -297,15 +296,15 @@ impl TransferRegion { // later Region::Rect(s1, s2) => { // Check if all source wells exist: - if s1.col == 0 || s1.row == 0 || s2.col == 0 || s2.row == 0 { + if s1.0 == 0 || s1.1 == 0 || s2.0 == 0 || s2.1 == 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.col > source_max.0 || s2.col > source_max.0 { + if s1.0 > source_max.0 || s2.0 > source_max.0 { return Err("Source region is out-of-bounds! (Too tall)"); } - if s1.row > source_max.1 || s2.row > source_max.1 { + if s1.1 > source_max.1 || s2.1 > source_max.1 { // log::debug!("s1.1: {}, max.1: {}", s1.1, source_max.1); return Err("Source region is out-of-bounds! (Too wide)"); } @@ -326,34 +325,28 @@ impl TransferRegion { } } -fn create_dense_rectangle(c1: &Well, c2: &Well) -> Vec { +fn create_dense_rectangle(c1: &(u8, u8), c2: &(u8, u8)) -> Vec<(u8, u8)> { // Creates a vector of every point between two corners let (c1, c2) = standardize_rectangle(c1, c2); - 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 }); + 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)); } } points } -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); +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); ( - Well { - col: upper_left_i, - row: upper_left_j, - }, - Well { - col: bottom_right_i, - row: bottom_right_j, - }, + (upper_left_i, upper_left_j), + (bottom_right_i, bottom_right_j), ) } @@ -370,7 +363,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(&Well { col: i, row: j }) { + if source_wells.contains(&(i, j)) { source_string.push('x') } else { source_string.push('.') @@ -386,7 +379,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(&Well{col: i, row: j}) { + if dest_wells.contains(&(i, 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 deleted file mode 100644 index b3b7bbf..0000000 --- a/plate-tool-lib/src/transfer_volume.rs +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index c6b9baf..0000000 --- a/plate-tool-lib/src/well.rs +++ /dev/null @@ -1,40 +0,0 @@ -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, - } - } -}