Study Documentation
Study
A class to manage and manipulate study-related data, including specimen records, plate layouts, and sample distributions.
This class provides functionalities for loading specimen records, sorting and randomizing specimen order, distributing specimens across plates, and exporting data to various formats.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the study. |
plates |
list
|
List of Plate objects used in the study. |
total_plates |
int
|
Total number of plates in the study. |
specimen_records_df |
DataFrame
|
Pandas DataFrame holding specimen records. |
records_file_path |
str
|
Path to the file containing specimen records. |
_column_with_group_index |
str
|
Column name in specimen_records_df that holds group indices. |
Examples:
>>> study = Study(study_name="Cancer")
>>> study.load_specimen_records("specimens.csv", sample_group_id_column="GroupID")
>>> study.randomize_order(case_control=True)
>>> qc_plate = QCPlate(QC_config="./data/plate_config_dynamic.toml")
>>> study.randomize_order()
>>> study.distribute_samples_to_plates()
>>> study.to_layout_lists()
Source code in src/plate_planner/study.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
__getitem__(index)
Retrieve a specific plate from the study by its index.
This method allows for direct access to a plate in the study using the indexing syntax.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
The index of the plate to retrieve. |
required |
Returns:
Type | Description |
---|---|
Union[Plate, QCPlate]
|
Union[Plate, QCPlate]: The plate at the specified index. |
Raises:
Type | Description |
---|---|
IndexError
|
If the index is out of range of the plates list. |
Source code in src/plate_planner/study.py
__init__(study_name=None)
Initializes a new instance of the Study class.
This constructor sets up a study with a specified name. If no name is provided, a default name is generated using the current date.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
study_name |
Optional[str]
|
The name for the study. If None, a default name in the format "Study_YYYY-MM-DD" is assigned, where YYYY-MM-DD represents the current date. |
None
|
Examples:
>>> study2 = Study()
>>> study2.name
"Study_2024-01-21" # Example output; actual output will vary based on the current date.
Source code in src/plate_planner/study.py
__iter__()
Initialize the iterator for the Study class.
This method sets up the class to iterate over its plates, resetting the internal counter to zero. It allows the Study instance to be used in a loop (e.g., a for loop), facilitating iteration over its plates.
Returns:
Type | Description |
---|---|
Union[Plate, QCPlate]
|
Iterator[Union[Plate, QCPlate]]: An iterator that yields either |
Union[Plate, QCPlate]
|
instances, allowing the Study instance to be used in a loop. |
Source code in src/plate_planner/study.py
__len__()
Return the total number of plates in the Study.
This method enables the use of the len() function on the Study instance.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of plates in the study. |
Source code in src/plate_planner/study.py
__next__()
Proceed to the next plate in the Study class during iteration.
This method returns the next plate in the study, which can be either a Plate
or a QCPlate
instance.
It is automatically called in each iteration of a loop. When all plates have been iterated over,
it raises the StopIteration exception.
Returns:
Type | Description |
---|---|
Union[Plate, QCPlate]
|
Union[Plate, QCPlate]: The next plate in the study, either a |
Raises:
Type | Description |
---|---|
StopIteration
|
If all plates have been iterated over. |
Source code in src/plate_planner/study.py
__repr__()
Return an unambiguous string representation of the Study instance.
This method is useful for debugging and logging purposes, as it represents the Study object in a clear and concise way.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representation of the Study instance. |
Source code in src/plate_planner/study.py
__str__()
Return a readable string representation of the Study instance.
This method provides a user-friendly string representation of the Study, which includes its name, the number of study specimens, and the total number of plates.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string describing the Study instance. |
Source code in src/plate_planner/study.py
distribute_samples_to_plates(plate_layout, allow_group_split=False, N_samples_desired_plate=None)
Distributes specimens across multiple plates based on a specified layout, with an option to keep group integrity.
This method iterates through the study's specimen records and distributes them across multiple plates according to the provided plate layout. It supports options to either keep specimen groups together or allow splitting them across different plates. The method can also handle a specified number of samples per plate if desired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plate_layout |
Union[Plate, QCPlate]
|
The layout template for the plates. This can be an instance of 'Plate' or 'QCPlate'. |
required |
allow_group_split |
bool
|
If False (default), keeps specimens within the same group on the same plate. If True, allows splitting groups across plates. |
False
|
N_samples_desired_plate |
Optional[int]
|
The desired number of samples per plate. If not specified, fills each plate to its capacity. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the group column is not defined in the specimen records. |
Examples:
>>> study = Study(...)
>>> study.load_specimen_records("specimens.csv", sample_group_id_column="GroupID")
>>> plate_layout = Plate(...) # Assume Plate is properly initialized
>>> study.distribute_samples_to_plates(plate_layout, allow_group_split=False)
# This will distribute the specimens across plates, keeping groups together.
Source code in src/plate_planner/study.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
|
load_sample_file(records_file, sample_group_id_column=None, sample_id_column=None)
Loads specimen records from a specified file into the study.
This method reads specimen data from a file (Excel or CSV) and stores it in a DataFrame. It also identifies or sets the column used for grouping specimens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
records_file |
str
|
The path to the file containing specimen records. |
required |
sample_group_id_column |
Optional[str]
|
The column name in the file that represents the group ID of samples. If None, the method attempts to find a suitable column automatically. |
None
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If the specified records_file does not exist. |
Examples:
>>> study = Study(study_name="Oncology Study")
>>> study.load_specimen_records("specimens.xlsx", sample_group_id_column="PatientGroup")
>>> study.specimen_records_df.shape
(200, 5) # Example output, indicating 200 rows and 5 columns in the DataFrame.
Source code in src/plate_planner/study.py
plot_attribute_plate_distributions(attribute, normalize=False, colormap='tab20b', plt_style='ggplot')
Plots a stacked bar chart for a specified attribute across different plates.
This method retrieves distribution data for the given attribute and plots it as a stacked bar chart. Each bar in the chart represents a different category of the attribute, with segments in the bar showing the count or proportion from each plate. The method supports normalization of the data and allows for customization of the plot's colormap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attribute |
str
|
The attribute for which the distributions are plotted. |
required |
normalize |
bool
|
If True, normalizes the counts within each category to proportions that sum to 100%. Defaults to False. |
False
|
colormap |
str
|
The name of the matplotlib colormap to use for the plot. Defaults to 'tab20b'. |
'tab20b'
|
Returns:
Type | Description |
---|---|
matplotlib.figure.Figure: The figure object containing the bar chart. |
Source code in src/plate_planner/study.py
position_sample_within_groups(sortby_column, sample_value, position_index)
Repositions a specific sample within each group based on a specified value and index.
This method allows altering the position of a sample within each group in the specimen records DataFrame. It locates a sample based on the 'sortby_column' and 'sample_value', then repositions this sample within its group to the specified 'position_index'. The method is useful for customizing the order of samples within groups based on specific criteria or requirements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sortby_column |
str
|
The column name in the specimen records DataFrame to identify the sample. |
required |
sample_value |
Any
|
The value in the 'sortby_column' that identifies the sample to reposition. |
required |
position_index |
int
|
The new index within the group where the sample should be positioned. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If no group column is defined in the Study instance. |
Examples:
>>> study = Study(...)
>>> study.load_specimen_records("specimens.csv", sample_group_id_column="GroupID")
>>> study.position_sample_within_groups("PatientID", 12345, 2)
# This will move the sample with PatientID 12345 to the index 2 position within its respective group.
Source code in src/plate_planner/study.py
randomize_order(case_control=None, reproducible=True)
Randomizes the order of specimen records in the study, optionally maintaining group integrity.
This method either randomizes the entire order of specimens or maintains the order within groups, depending on the 'case_control' flag. It also allows for reproducible randomization using a fixed seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
case_control |
Optional[bool]
|
If True, maintains group order (samples within a group are not shuffled). If False, shuffles all samples regardless of group. If None, the behavior is determined based on the presence of a group index column. |
None
|
reproducible |
bool
|
If True, uses a fixed seed for randomization to ensure reproducibility. |
True
|
Examples:
>>> study = Study(study_name="Diabetes Study")
>>> study.load_specimen_records("patients.csv", sample_group_id_column="GroupID")
>>> study.randomize_order(case_control=True)
>>> study.specimen_records_df.head(3) # Example output showing randomized order within groups.
Source code in src/plate_planner/study.py
sort_records_within_groups(sortby_column)
Sorts specimen records within each group based on a specified column.
This method groups the specimen records by a predefined group index column and then sorts each group's records based on the specified 'sortby_column'. The sorted groups are then concatenated back into the main DataFrame. This is useful for organizing records in a manner that respects the grouping while ordering the records within each group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sortby_column |
str
|
The column name in the specimen records DataFrame to sort by within each group. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If no group column is defined in the Study instance. |
Examples:
>>> study = Study(...)
>>> study.load_specimen_records("specimens.csv", sample_group_id_column="GroupID")
>>> study.sort_records_within_groups("Age")
# This will sort the specimen records within each group based on the "Age" column.
Source code in src/plate_planner/study.py
to_dataframe()
Converts the data from all plates in the study into a single Pandas DataFrame.
This method iterates over each plate in the study and converts its data to a DataFrame. These DataFrames are then concatenated into a single DataFrame representing the entire study.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing data from all plates in the study. |
Examples:
>>> study = Study(...)
>>> study_df = study.to_dataframe()
>>> study_df.head()
# Displays the first few rows of the combined DataFrame for the study.
Source code in src/plate_planner/study.py
to_layout_figures(annotation_metadata_key, color_metadata_key, file_format=None, folder_path=None, plate_name='Plate', **kwargs)
Creates and visual representations of each plate in the study as figures.
This method iterates over each plate in the study, generating a figure based on specified metadata keys for annotation and coloring. If the file format is specified, the figures are saved in the specified file format in a designated folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
annotation_metadata_key |
str
|
The metadata key used for annotating elements in the figure. |
required |
color_metadata_key |
str
|
The metadata key used for coloring elements in the figure. |
required |
file_format |
str
|
The format in which to save the figures (default is 'pdf'). |
None
|
folder_path |
Optional[str]
|
The path to the folder where the figures will be saved. If None, the current working directory is used. |
None
|
plate_name |
str
|
A base name for the figure files. |
'Plate'
|
**kwargs |
Additional keyword arguments passed to the |
{}
|
Examples:
>>> study = Study(...)
>>> study.to_layout_figures(annotation_metadata_key="sample_id",
color_metadata_key="status",
file_format="png",
folder_path="/path/to/figures",
plate_name="study_plate")
# This will create and save figures for each plate in the '/path/to/figures' directory,
# with annotations and colorings based on 'sample_id' and 'status'.
Source code in src/plate_planner/study.py
to_layout_lists(metadata_keys=[], file_format='csv', folder_path=None, plate_name='plate')
Exports the layout of each plate in the study to files in the specified format.
This method iterates over each plate in the study and exports its layout to a file. The files are saved in a specified format (CSV by default) and stored in a designated folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata_keys |
list
|
A list of metadata keys to include in the exported files. |
[]
|
file_format |
str
|
The file format for the exported layouts (e.g., 'csv'). |
'csv'
|
folder_path |
str
|
The path to the folder where the layout files will be saved. If None, the current working directory is used. |
None
|
plate_name |
str
|
A base name for the layout files. |
'plate'
|
Examples:
>>> study = Study(...)
>>> study.to_layout_lists(metadata_keys=["sample_type", "concentration"],
file_format="csv",
folder_path="/path/to/layouts",
plate_name="experiment_plate")
# This will save layout files for each plate in the '/path/to/layouts' directory.