|
3 | 3 | from datetime import datetime |
4 | 4 | import numpy as np |
5 | 5 | import pandas as pd |
| 6 | +import re |
6 | 7 | import warnings |
7 | 8 | from scipy.stats import zscore |
8 | 9 | from sklearn.linear_model import LogisticRegression |
@@ -202,6 +203,102 @@ def _well_to_row_and_col(well): |
202 | 203 | return ord(well[0].upper()) - 64, int(well[1:]) |
203 | 204 |
|
204 | 205 |
|
| 206 | +def _parse_and_validate_well_384(well): |
| 207 | + """Validate input 384-well plate well ID and parse into (row, column). |
| 208 | +
|
| 209 | + Parameters |
| 210 | + ---------- |
| 211 | + well : str |
| 212 | + Well identifier like 'A1', 'P24', 'A01', etc. |
| 213 | +
|
| 214 | + Returns |
| 215 | + ------- |
| 216 | + tuple |
| 217 | + (row_number, column_number) where row A=1, B=2, etc. |
| 218 | +
|
| 219 | + Raises |
| 220 | + ------ |
| 221 | + ValueError |
| 222 | + If well format is invalid or outside 384-well plate bounds. |
| 223 | + """ |
| 224 | + |
| 225 | + VALID_384_WELL_ROWS = set('ABCDEFGHIJKLMNOP') |
| 226 | + MAX_384_WELL_COL = 24 |
| 227 | + |
| 228 | + # Check type and basic format using regex |
| 229 | + # Format: single letter A-Pa-p followed by 1-2 digits |
| 230 | + if not isinstance(well, str) or not re.match(r'^[A-Pa-p]\d{1,2}$', well): |
| 231 | + raise ValueError( |
| 232 | + "Well must be a letter A-P followed by a number 1-24, " |
| 233 | + "e.g., A1 or A01") |
| 234 | + |
| 235 | + row_letter = well[0].upper() |
| 236 | + col_str = well[1:] |
| 237 | + |
| 238 | + # Validate row is A-P |
| 239 | + if row_letter not in VALID_384_WELL_ROWS: |
| 240 | + raise ValueError( |
| 241 | + f"Well row '{row_letter}' is invalid. " |
| 242 | + f"Row must be A-P for a 384-well plate.") |
| 243 | + |
| 244 | + # Parse and validate column |
| 245 | + col = int(col_str) |
| 246 | + if col < 1 or col > MAX_384_WELL_COL: |
| 247 | + raise ValueError( |
| 248 | + f"Well column {col} is invalid. " |
| 249 | + f"Column must be 1-24 for a 384-well plate.") |
| 250 | + |
| 251 | + row = ord(row_letter) - 64 |
| 252 | + return (row, col) |
| 253 | + |
| 254 | + |
| 255 | +def sort_by_interleaved_plates(df, well_column): |
| 256 | + """Sort DataFrame rows by interleaved 96-well plate order. |
| 257 | +
|
| 258 | + Sorts wells in the order of four interleaved 96-well plates within a |
| 259 | + 384-well plate. The order is: quadrant 1, then quadrant 2, then quadrant 3, |
| 260 | + then quadrant 4. Within each quadrant, wells are sorted by row first, |
| 261 | + then by column. |
| 262 | +
|
| 263 | + Quadrant mapping (based on 384-well position): |
| 264 | + - Quadrant 1: Odd rows (A, C, E, ...) + Odd columns (1, 3, 5, ...) |
| 265 | + - Quadrant 2: Odd rows + Even columns (2, 4, 6, ...) |
| 266 | + - Quadrant 3: Even rows (B, D, F, ...) + Odd columns |
| 267 | + - Quadrant 4: Even rows + Even columns |
| 268 | +
|
| 269 | + Parameters |
| 270 | + ---------- |
| 271 | + df : pd.DataFrame |
| 272 | + DataFrame containing well identifiers. |
| 273 | + well_column : str |
| 274 | + Name of the column containing 384-well IDs. |
| 275 | +
|
| 276 | + Returns |
| 277 | + ------- |
| 278 | + pd.DataFrame |
| 279 | + DataFrame sorted by quadrant, then row, then column. |
| 280 | +
|
| 281 | + Raises |
| 282 | + ------ |
| 283 | + ValueError |
| 284 | + If well_column doesn't exist or contains invalid well IDs. |
| 285 | + """ |
| 286 | + if well_column not in df.columns: |
| 287 | + raise ValueError( |
| 288 | + f"Column '{well_column}' not found in DataFrame.") |
| 289 | + |
| 290 | + def _get_sort_key(well): |
| 291 | + row, col = _parse_and_validate_well_384(well) |
| 292 | + # not necessary to validate plate position output since we know the |
| 293 | + # well is a valid one if we got here |
| 294 | + quadrant = int(_plate_position(well)) |
| 295 | + return (quadrant, row, col) |
| 296 | + |
| 297 | + sort_keys = df[well_column].apply(_get_sort_key) |
| 298 | + sorted_indices = sort_keys.sort_values().index |
| 299 | + return df.loc[sorted_indices].reset_index(drop=True) |
| 300 | + |
| 301 | + |
205 | 302 | def _decompress_well(well): |
206 | 303 | """Returns a 96 well plate ID from a compressed 384 well plate ID""" |
207 | 304 |
|
|
0 commit comments