|
2 | 2 |
|
3 | 3 | from allotropy.exceptions import AllotropeConversionError |
4 | 4 | from allotropy.named_file_contents import NamedFileContents |
| 5 | +from allotropy.parsers.agilent_gen5.agilent_gen5_structure import ( |
| 6 | + Gen5DataContext, |
| 7 | + get_concentrations, |
| 8 | + get_identifiers, |
| 9 | + get_kinetic_measurements, |
| 10 | + get_temperature, |
| 11 | + HeaderData, |
| 12 | + KineticData, |
| 13 | + ReadData, |
| 14 | +) |
5 | 15 | from allotropy.parsers.agilent_gen5.constants import ( |
6 | 16 | MULTIPLATE_FILE_ERROR, |
| 17 | + NO_MEASUREMENTS_ERROR, |
7 | 18 | NO_PLATE_DATA_ERROR, |
8 | 19 | ) |
9 | 20 | from allotropy.parsers.lines_reader import SectionLinesReader |
@@ -59,3 +70,107 @@ def __init__(self, named_file_contents: NamedFileContents) -> None: |
59 | 70 | lines = list(plate_reader.pop_until_empty()) |
60 | 71 | self.sections[lines[0].split("\t")[0].strip(":")] = lines |
61 | 72 | plate_reader.drop_empty() |
| 73 | + |
| 74 | + def get_required_section(self, section_name: str) -> list[str]: |
| 75 | + """Get a required section, raises error if not found.""" |
| 76 | + section = self.sections.get(section_name) |
| 77 | + if section is None: |
| 78 | + msg = f"Required section '{section_name}' not found." |
| 79 | + raise AllotropeConversionError(msg) |
| 80 | + return section |
| 81 | + |
| 82 | + @property |
| 83 | + def procedure_details(self) -> list[str]: |
| 84 | + return self.get_required_section("Procedure Details") |
| 85 | + |
| 86 | + @property |
| 87 | + def layout_section(self) -> list[str] | None: |
| 88 | + return self.sections.get("Layout") |
| 89 | + |
| 90 | + @property |
| 91 | + def wavelength_section(self) -> list[str] | None: |
| 92 | + return self.sections.get("Wavelength") |
| 93 | + |
| 94 | + @property |
| 95 | + def time_section(self) -> list[str] | None: |
| 96 | + return self.sections.get("Time") |
| 97 | + |
| 98 | + @property |
| 99 | + def actual_temperature_section(self) -> list[str] | None: |
| 100 | + return self.sections.get("Actual Temperature") |
| 101 | + |
| 102 | + def _validate_result_sections(self, result_sections: list[list[str]]) -> None: |
| 103 | + """Validates whether all the result sections dimensions are consistent.""" |
| 104 | + first_section = result_sections[0] |
| 105 | + |
| 106 | + for section in result_sections[1:]: |
| 107 | + if not first_section[0] == section[0] and len(first_section) == len( |
| 108 | + section |
| 109 | + ): |
| 110 | + msg = "All result tables should have the same dimensions." |
| 111 | + raise AllotropeConversionError(msg) |
| 112 | + |
| 113 | + def get_results_section(self) -> list[str] | None: |
| 114 | + """Returns a valid Results Matrix from the reader sections if found. |
| 115 | +
|
| 116 | + Checks for Results in the reader sections, if not found, creates the results matrix with all |
| 117 | + sections that are correctly formatted as a results table (excluding the Layout section). If |
| 118 | + no tables with results are found, returns None |
| 119 | + """ |
| 120 | + if "Results" in self.sections: |
| 121 | + return self.sections["Results"] |
| 122 | + |
| 123 | + def is_results(section: list[str]) -> bool: |
| 124 | + return ( |
| 125 | + len(section) > 2 |
| 126 | + and section[1].startswith("\t1") |
| 127 | + and section[2].startswith("A\t") |
| 128 | + ) |
| 129 | + |
| 130 | + result_sections = [] |
| 131 | + for name, section in self.sections.items(): |
| 132 | + if name == "Layout": |
| 133 | + continue |
| 134 | + if is_results(section): |
| 135 | + result_sections.append(section[1:]) |
| 136 | + |
| 137 | + if result_sections: |
| 138 | + self._validate_result_sections(result_sections) |
| 139 | + return [ |
| 140 | + "Results", |
| 141 | + result_sections[0][0], |
| 142 | + *[ |
| 143 | + section[i + 1] |
| 144 | + for i in range(len(result_sections[0]) - 1) |
| 145 | + for section in result_sections |
| 146 | + ], |
| 147 | + ] |
| 148 | + |
| 149 | + return None |
| 150 | + |
| 151 | + def extract_data_context(self, file_path: str) -> Gen5DataContext: |
| 152 | + results_section = self.get_results_section() |
| 153 | + if not results_section: |
| 154 | + self.header_data.get_unread() |
| 155 | + raise AllotropeConversionError(NO_MEASUREMENTS_ERROR) |
| 156 | + |
| 157 | + kinetic_result = get_kinetic_measurements(self.time_section) |
| 158 | + kinetic_measurements, kinetic_elapsed_time, kinetic_errors = kinetic_result or ( |
| 159 | + {}, |
| 160 | + [], |
| 161 | + {}, |
| 162 | + ) |
| 163 | + |
| 164 | + return Gen5DataContext( |
| 165 | + header_data=HeaderData.create(self.header_data, file_path), |
| 166 | + read_data=ReadData.create(self.procedure_details), |
| 167 | + kinetic_data=KineticData.create(self.procedure_details), |
| 168 | + results_section=results_section, |
| 169 | + sample_identifiers=get_identifiers(self.layout_section), |
| 170 | + concentration_values=get_concentrations(self.layout_section), |
| 171 | + actual_temperature=get_temperature(self.actual_temperature_section), |
| 172 | + kinetic_measurements=kinetic_measurements, |
| 173 | + kinetic_elapsed_time=kinetic_elapsed_time, |
| 174 | + kinetic_errors=kinetic_errors, |
| 175 | + wavelength_section=self.wavelength_section, |
| 176 | + ) |
0 commit comments