-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_convert_html_to_csv.py
More file actions
304 lines (248 loc) · 9.96 KB
/
03_convert_html_to_csv.py
File metadata and controls
304 lines (248 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
#!/usr/bin/env python3
"""Convert EUCAST MIC HTML pages into normalized CSV outputs."""
from __future__ import annotations
import csv
import re
from pathlib import Path
from typing import Dict, Iterable, List, Sequence, Tuple
from bs4 import BeautifulSoup # type: ignore[import]
from rpy2.robjects import StrVector, r # type: ignore[import]
from rpy2.robjects.packages import importr # type: ignore[import]
SCRIPT_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = SCRIPT_DIR.parent
SPECIES_CSV = SCRIPT_DIR / "species_eucast_with_amr.csv"
HTML_DIR = SCRIPT_DIR / "eucast_mic_html"
ANTIBIOTICS_REFERENCE = PROJECT_ROOT / "antibiotics.csv"
OUTPUT_COMBOS = SCRIPT_DIR / "mic_combinations.csv"
OUTPUT_VALUES = SCRIPT_DIR / "mic_values.csv"
SUMMARY_HEADERS = {
"Distributions": "distribution_count",
"Observations": "observation_count",
"(T)ECOFF": "ecoff_value",
"Confidence interval": "confidence_interval",
}
INVALID_ECOFFS = {"ID", "IE"}
SEPARATOR_PATTERN = re.compile(r"[‐‑‒–—-]")
PAREN_PATTERN = re.compile(r"\([^)]*\)")
AMR = importr("AMR")
def normalize_text(text: str) -> str:
return " ".join(text.split())
def load_species_map(path: Path) -> Dict[str, Dict[str, str]]:
with path.open("r", encoding="utf-8", newline="") as handle:
reader = csv.DictReader(handle)
mapping: Dict[str, Dict[str, str]] = {}
for row in reader:
mapping[row["species_id"]] = {
"species_name": row["species_name"],
"amr_mo": row["amr_mo"],
"phenotype": row.get("phenotype", ""),
}
return mapping
def normalize_variants(value: str) -> List[str]:
"""Return canonical matching keys (plain + variant without parentheses)."""
def _normalize(text: str) -> str:
text = SEPARATOR_PATTERN.sub("/", text.lower())
text = re.sub(r"\s*/\s*", "/", text)
text = re.sub(r"\s+", " ", text)
return text.strip()
cleaned = value.strip()
if not cleaned:
return []
variants = {_normalize(cleaned)}
stripped = PAREN_PATTERN.sub(" ", cleaned)
stripped = re.sub(r"\s+", " ", stripped).strip()
if stripped and stripped != cleaned:
variants.add(_normalize(stripped))
return list(variants)
def load_antibiotic_reference(path: Path) -> Dict[str, str]:
lookup: Dict[str, str] = {}
with path.open("r", encoding="utf-8", newline="") as handle:
reader = csv.DictReader(handle)
for row in reader:
amr_code = row.get("amr_code", "").strip()
if not amr_code:
continue
for column in (
"full_name_en",
"full_name_de",
"short_name_en",
"short_name_de",
):
value = row.get(column, "")
if value:
for variant in normalize_variants(value):
lookup.setdefault(variant, amr_code)
for column in ("synonyms_en", "synonyms_de"):
value = row.get(column, "")
if value:
for synonym in value.split(";"):
synonym = synonym.strip()
if synonym:
for variant in normalize_variants(synonym):
lookup.setdefault(variant, amr_code)
return lookup
def resolve_antibiotic_code(
name: str, reference: Dict[str, str]
) -> Tuple[str, str]:
for variant in normalize_variants(name):
if variant in reference:
return reference[variant], "reference"
mos = AMR.as_ab(StrVector([name]))
code = r["as.character"](mos)[0]
if code not in ("NA", "NULL", "UNKNOWN"):
return code, "AMR::as_ab"
raise ValueError(
f"Unable to resolve AMR code for antibiotic '{name}'. "
"Add it to antibiotics.csv or adjust the resolver."
)
def classify_ecoff(value: str) -> Tuple[str, str]:
value = value.strip()
if not value:
return "", "missing"
if value in INVALID_ECOFFS:
return "", "invalid"
if value == "-":
return "", "less_than_three"
if value.startswith("((") and value.endswith("))"):
return value[2:-2].strip(), "forced_ecoff"
if value.startswith("(") and value.endswith(")"):
return value[1:-1].strip(), "tentative_ecoff"
return value, "value"
def parse_confidence_interval(raw: str) -> Tuple[str, str]:
raw = raw.strip()
if not raw:
return "", ""
match = re.match(r"^\s*([0-9]+(?:\.[0-9]+)?)\s*-\s*([0-9]+(?:\.[0-9]+)?)\s*$", raw)
if not match:
return "", ""
return match.group(1), match.group(2)
def to_int(text: str) -> int:
text = text.strip()
return int(text) if text.isdigit() else 0
def find_html_path(species_id: str) -> Path | None:
matches = sorted(HTML_DIR.glob(f"{species_id}_*.html"))
if matches:
return matches[0]
return None
def extract_headers(table: BeautifulSoup) -> Tuple[List[str], List[Tuple[int, str]]]:
header_cells = table.thead.find_all("th") if table.thead else []
headers = [normalize_text(cell.get_text()) for cell in header_cells]
dilution_columns: List[Tuple[int, str]] = []
for idx, label in enumerate(headers):
if idx == 0 or label in SUMMARY_HEADERS or not label:
continue
dilution_columns.append((idx, label))
return headers, dilution_columns
def parse_table_rows(
table: BeautifulSoup,
headers: List[str],
dilution_columns: List[Tuple[int, str]],
) -> Iterable[List[str]]:
for tbody in table.find_all("tbody"):
for tr in tbody.find_all("tr"):
cells = tr.find_all("td")
if len(cells) != len(headers):
continue
yield [normalize_text(cell.get_text()) for cell in cells]
def main() -> None:
species_map = load_species_map(SPECIES_CSV)
antibiotic_lookup = load_antibiotic_reference(ANTIBIOTICS_REFERENCE)
combo_rows: List[Dict[str, str]] = []
value_rows: List[Dict[str, str]] = []
combo_id = 1
for species_id in sorted(species_map.keys(), key=lambda x: int(x)):
metadata = species_map[species_id]
html_path = find_html_path(species_id)
if html_path is None:
print(f"[WARN] Missing HTML for species {species_id} ({metadata['species_name']})")
continue
soup = BeautifulSoup(html_path.read_text(encoding="utf-8"), "html.parser")
table = soup.find("table", id="search-results-table")
if not table:
print(f"[WARN] No MIC table in {html_path}")
continue
headers, dilution_columns = extract_headers(table)
try:
distribution_idx = headers.index("Distributions")
observation_idx = headers.index("Observations")
ecoff_idx = headers.index("(T)ECOFF")
confidence_idx = headers.index("Confidence interval")
except ValueError as exc:
print(f"[WARN] Missing expected column in {html_path}: {exc}")
continue
for row in parse_table_rows(table, headers, dilution_columns):
antibiotic_name = row[0]
if not antibiotic_name:
continue
ecoff_text = row[ecoff_idx]
if ecoff_text in INVALID_ECOFFS:
continue
ecoff_value, ecoff_annotation = classify_ecoff(ecoff_text)
antibiotic_code, source = resolve_antibiotic_code(antibiotic_name, antibiotic_lookup)
if source == "AMR::as_ab":
print(f"[INFO] Antibiotic '{antibiotic_name}' resolved via AMR::as_ab → {antibiotic_code}")
distribution_count = to_int(row[distribution_idx])
observation_count = to_int(row[observation_idx])
confidence_lower, confidence_upper = parse_confidence_interval(row[confidence_idx])
combo_rows.append(
{
"combo_id": str(combo_id),
"species_amr_mo": metadata["amr_mo"],
"species_name": metadata["species_name"],
"phenotype": metadata.get("phenotype", ""),
"antibiotic_name": antibiotic_name,
"antibiotic_amr_code": antibiotic_code,
"distribution_count": str(distribution_count),
"observation_count": str(observation_count),
"ecoff_value": ecoff_value,
"ecoff_annotation": ecoff_annotation,
"confidence_lower": confidence_lower,
"confidence_upper": confidence_upper,
}
)
for idx, label in dilution_columns:
count = to_int(row[idx])
value_rows.append(
{
"combo_id": str(combo_id),
"dilution_mg_l": label,
"count": str(count),
}
)
combo_id += 1
write_csv(
OUTPUT_COMBOS,
combo_rows,
[
"combo_id",
"species_amr_mo",
"species_name",
"phenotype",
"antibiotic_name",
"antibiotic_amr_code",
"distribution_count",
"observation_count",
"ecoff_value",
"ecoff_annotation",
"confidence_lower",
"confidence_upper",
],
)
write_csv(
OUTPUT_VALUES,
value_rows,
["combo_id", "dilution_mg_l", "count"],
)
print(
f"Wrote {len(combo_rows)} combinations to {OUTPUT_COMBOS} "
f"and {len(value_rows)} MIC rows to {OUTPUT_VALUES}"
)
def write_csv(path: Path, rows: Sequence[Dict[str, str]], fieldnames: List[str]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
if __name__ == "__main__":
main()