refactor: Well struct

Will require bump to lib version!!
This commit is contained in:
Emilia Allison 2024-08-10 00:46:49 -04:00
parent 06baf0a053
commit 7e321a78c4
Signed by: emilia
GPG Key ID: 7A3F8997BFE894E0
6 changed files with 146 additions and 85 deletions

View File

@ -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<Well> = HashSet::new();
let mut destination_wells: HashSet<Well> = 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<Well> = source_wells.into_iter().collect();
let destination_wells_vec: Vec<Well> = destination_wells.into_iter().collect();
let custom_region: Region =
Region::Custom(CustomRegion::new(source_wells_vec, destination_wells_vec));

View File

@ -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<TransferRecord>) -> Result<String, Bo
}
/// Converts "spreadsheet format" well identification to coordinates
pub fn string_well_to_pt(input: &str) -> Option<(u8, u8)> {
pub fn string_well_to_pt(input: &str) -> Option<Well> {
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::<u8>().ok()) {
return Some((row, col))
return Some((row, col).into())
} else {
return None
}

View File

@ -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;

View File

@ -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<Well>,
dest: Vec<Well>,
}
impl CustomRegion {
pub fn new(src: Vec<(u8, u8)>, dest: Vec<(u8, u8)>) -> Self {
pub fn new(src: Vec<Well>, dest: Vec<Well>) -> 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<Region> for ((u8, u8), (u8, u8)) {
impl TryFrom<Region> for (Well, Well) {
type Error = &'static str;
fn try_from(region: Region) -> Result<Self, Self::Error> {
if let Region::Rect(c1, c2) = region {
@ -37,12 +39,11 @@ impl TryFrom<Region> 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<Rectangle>) -> 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<Well> = Vec::with_capacity(transfers.len());
let mut dest_pts: Vec<Well> = 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<Well> {
match &self.source_region {
Region::Rect(c1, c2) => {
let mut wells = Vec::<(u8, u8)>::new();
let mut wells = Vec::<Well>::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<Well> {
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::<Well>::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<dyn Fn((u8, u8)) -> Option<Vec<(u8, u8)>> + '_> {
pub fn calculate_map(&self) -> Box<dyn Fn(Well) -> Option<Vec<Well>> + '_> {
// 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)
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()),
y + j
.checked_sub(source_ul.1)
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<Well> = 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<Well> {
// 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::<Well>::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('.')

View File

@ -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<>),
}
*/

View File

@ -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<Self> {
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::<u8>().ok()) {
return Some(Well { row, col });
} else {
return None;
}
}
None
}
}
impl Into<Well> for (u8, u8) {
fn into(self) -> Well {
Well {
col: self.0,
row: self.1,
}
}
}