Skip to content

Commit cca387f

Browse files
feat: Migrate biorad_bioplex_manager to use SeriesData.get_unread (#1100)
Co-authored-by: joshua-benchling <jherna@benchling.com>
1 parent c708524 commit cca387f

6 files changed

Lines changed: 7458 additions & 129 deletions

File tree

src/allotropy/allotrope/schema_mappers/adm/multi_analyte_profiling/benchling/_2024/_09/multi_analyte_profiling.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Analyte:
7070
statistics: list[StatisticsDocument] | None = None
7171
statistic_datum_role: TStatisticDatumRole | None = None
7272
fluorescence: float | None = None
73+
custom_info: dict[str, Any] | None = None
7374

7475

7576
@dataclass(frozen=True)
@@ -253,42 +254,45 @@ def _get_measurement_document(
253254
assay_bead_count=TQuantityValueNumber(value=measurement.assay_bead_count),
254255
analyte_aggregate_document=AnalyteAggregateDocument(
255256
analyte_document=[
256-
AnalyteDocumentItem(
257-
analyte_identifier=analyte.identifier,
258-
analyte_name=analyte.name,
259-
assay_bead_identifier=analyte.assay_bead_identifier,
260-
assay_bead_count=TQuantityValueNumber(
261-
value=analyte.assay_bead_count
262-
),
263-
fluorescence=quantity_or_none(
264-
TQuantityValueRelativeFluorescenceUnit,
265-
analyte.fluorescence,
266-
has_statistic_datum_role=analyte.statistic_datum_role,
267-
),
268-
statistics_aggregate_document=(
269-
StatisticsAggregateDocument(
270-
statistics_document=[
271-
StatisticsDocumentItem(
272-
statistical_feature=statistic.statistical_feature,
273-
statistic_dimension_aggregate_document=StatisticDimensionAggregateDocument(
274-
statistic_dimension_document=[
275-
StatisticDimensionDocumentItem(
276-
statistical_value=TQuantityValueModel(
277-
value=dimension.value,
278-
unit=dimension.unit,
279-
has_statistic_datum_role=dimension.statistic_datum_role,
280-
),
281-
)
282-
for dimension in statistic.statistic_dimensions
283-
]
284-
),
285-
)
286-
for statistic in analyte.statistics
287-
]
288-
)
289-
if analyte.statistics
290-
else None
257+
add_custom_information_document(
258+
AnalyteDocumentItem(
259+
analyte_identifier=analyte.identifier,
260+
analyte_name=analyte.name,
261+
assay_bead_identifier=analyte.assay_bead_identifier,
262+
assay_bead_count=TQuantityValueNumber(
263+
value=analyte.assay_bead_count
264+
),
265+
fluorescence=quantity_or_none(
266+
TQuantityValueRelativeFluorescenceUnit,
267+
analyte.fluorescence,
268+
has_statistic_datum_role=analyte.statistic_datum_role,
269+
),
270+
statistics_aggregate_document=(
271+
StatisticsAggregateDocument(
272+
statistics_document=[
273+
StatisticsDocumentItem(
274+
statistical_feature=statistic.statistical_feature,
275+
statistic_dimension_aggregate_document=StatisticDimensionAggregateDocument(
276+
statistic_dimension_document=[
277+
StatisticDimensionDocumentItem(
278+
statistical_value=TQuantityValueModel(
279+
value=dimension.value,
280+
unit=dimension.unit,
281+
has_statistic_datum_role=dimension.statistic_datum_role,
282+
),
283+
)
284+
for dimension in statistic.statistic_dimensions
285+
]
286+
),
287+
)
288+
for statistic in analyte.statistics
289+
]
290+
)
291+
if analyte.statistics
292+
else None
293+
),
291294
),
295+
custom_info_doc=analyte.custom_info,
292296
)
293297
for analyte in measurement.analytes
294298
]

src/allotropy/parsers/biorad_bioplex_manager/biorad_bioplex_manager_parser.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def create_data(self, named_file_contents: NamedFileContents) -> Data:
3333

3434
samples_dict = SampleMetadata.create_samples(reader["Samples"])
3535
# Only parse system-level metadata once, from the first well.
36-
system_metadata = SystemMetadata.create(reader["Wells"][0])
36+
wells = reader["Wells"].findall("Well")
37+
system_metadata = SystemMetadata.create(wells[0])
38+
plate_dimensions = reader["PlateDimensions"]
39+
plate_well_count = plate_dimensions.get_attr("TotalWells")
40+
plate_dimensions.get_unread()
3741

3842
return Data(
3943
create_metadata(
@@ -44,12 +48,12 @@ def create_data(self, named_file_contents: NamedFileContents) -> Data:
4448
well,
4549
samples_dict[well.name],
4650
system_metadata,
47-
experimental_data_id=reader["NativeDocumentLocation"].text,
48-
experiment_type=reader["Description"].text,
49-
plate_well_count=int(
50-
reader.get_attribute("PlateDimensions", "TotalWells")
51-
),
51+
experimental_data_id=reader[
52+
"NativeDocumentLocation"
53+
].get_text_or_none(),
54+
experiment_type=reader["Description"].get_text_or_none(),
55+
plate_well_count=int(plate_well_count),
5256
)
53-
for well in [Well.create(well_xml) for well_xml in reader["Wells"]]
57+
for well in [Well.create(well_xml) for well_xml in wells]
5458
],
5559
)

src/allotropy/parsers/biorad_bioplex_manager/biorad_bioplex_manager_reader.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77
)
88
from allotropy.named_file_contents import NamedFileContents
99
from allotropy.parsers.biorad_bioplex_manager import constants
10+
from allotropy.parsers.utils.strict_xml_element import StrictXmlElement
1011

1112

1213
class BioradBioplexReader:
1314
def __init__(self, named_file_contents: NamedFileContents) -> None:
1415
contents = named_file_contents.contents.read()
16+
# Ensure contents is bytes for StrictXmlElement.create_from_bytes()
17+
if isinstance(contents, str):
18+
contents = contents.encode("utf-8")
1519
try:
16-
xml_tree = Et.ElementTree(Et.fromstring(contents)) # noqa: S314
17-
self.root = xml_tree.getroot()
18-
self.children = {child.tag: child for child in self.root}
20+
self.root = StrictXmlElement.create_from_bytes(contents)
21+
# Create a mapping of child tags to StrictXmlElement objects
22+
self.children = {}
23+
for child_tag in constants.EXPECTED_TAGS:
24+
child_element = self.root.find_or_none(child_tag)
25+
if child_element is not None:
26+
self.children[child_tag] = child_element
1927
except Et.ParseError as err:
2028
# Return all expected tags if XML parsing fails
2129
msg = "Error parsing xml"
@@ -31,12 +39,12 @@ def _validate(self) -> None:
3139
msg = f"Missing expected tags in xml: {missing_tags}"
3240
raise AllotropeConversionError(msg)
3341

34-
def __getitem__(self, child_tag: str) -> Et.Element:
42+
def __getitem__(self, child_tag: str) -> StrictXmlElement:
3543
return get_key_or_error("child tag of root", child_tag, self.children)
3644

3745
def get_attribute(self, child_tag: str, attribute: str) -> str:
3846
try:
39-
return self[child_tag].attrib[attribute]
40-
except KeyError as e:
41-
msg = f"Unable to find '{attribute}' in {self[child_tag].attrib}"
47+
return self[child_tag].get_attr(attribute)
48+
except Exception as e:
49+
msg = f"Unable to find '{attribute}' in {self[child_tag].element.attrib}"
4250
raise AllotropeConversionError(msg) from e

0 commit comments

Comments
 (0)