|
| 1 | +"""Extract classes and relations from ChEBI OBO ontology files.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | + |
| 10 | +def _parse_obo_stanzas(filepath: str | Path) -> list[dict[str, list[str]]]: |
| 11 | + """Parse an OBO file and return a list of stanza dicts.""" |
| 12 | + stanzas: list[dict[str, list[str]]] = [] |
| 13 | + current_stanza: dict[str, list[str]] | None = None |
| 14 | + |
| 15 | + with open(filepath, encoding="utf-8") as f: |
| 16 | + for line in f: |
| 17 | + line = line.strip() |
| 18 | + if not line or line.startswith("!"): |
| 19 | + continue |
| 20 | + if line.startswith("["): |
| 21 | + if current_stanza is not None: |
| 22 | + stanzas.append(current_stanza) |
| 23 | + stanza_type = line.strip("[]") |
| 24 | + current_stanza = {"_type": [stanza_type]} |
| 25 | + elif current_stanza is not None and ":" in line: |
| 26 | + key, _, value = line.partition(":") |
| 27 | + current_stanza.setdefault(key.strip(), []).append(value.strip()) |
| 28 | + |
| 29 | + if current_stanza is not None: |
| 30 | + stanzas.append(current_stanza) |
| 31 | + |
| 32 | + return stanzas |
| 33 | + |
| 34 | + |
| 35 | +def extract_classes(filepath: str | Path) -> pd.DataFrame: |
| 36 | + """Extract ontology classes (terms) from a ChEBI OBO file. |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + filepath : str or Path |
| 41 | + Path to the ChEBI OBO file. |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + pd.DataFrame |
| 46 | + DataFrame with columns: id, name, definition, is_obsolete. |
| 47 | + """ |
| 48 | + stanzas = _parse_obo_stanzas(filepath) |
| 49 | + rows = [] |
| 50 | + for stanza in stanzas: |
| 51 | + if stanza.get("_type", [None])[0] != "Term": |
| 52 | + continue |
| 53 | + row = { |
| 54 | + "id": stanza.get("id", [None])[0], |
| 55 | + "name": stanza.get("name", [None])[0], |
| 56 | + "definition": stanza.get("def", [None])[0], |
| 57 | + "is_obsolete": stanza.get("is_obsolete", ["false"])[0] == "true", |
| 58 | + } |
| 59 | + rows.append(row) |
| 60 | + return pd.DataFrame(rows, columns=["id", "name", "definition", "is_obsolete"]) |
| 61 | + |
| 62 | + |
| 63 | +def extract_relations(filepath: str | Path) -> pd.DataFrame: |
| 64 | + """Extract class relations from a ChEBI OBO file. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + filepath : str or Path |
| 69 | + Path to the ChEBI OBO file. |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + pd.DataFrame |
| 74 | + DataFrame with columns: source_id, target_id, relation_type. |
| 75 | + """ |
| 76 | + stanzas = _parse_obo_stanzas(filepath) |
| 77 | + rows = [] |
| 78 | + |
| 79 | + for stanza in stanzas: |
| 80 | + if stanza.get("_type", [None])[0] != "Term": |
| 81 | + continue |
| 82 | + source_id = stanza.get("id", [None])[0] |
| 83 | + if source_id is None: |
| 84 | + continue |
| 85 | + |
| 86 | + for is_a_val in stanza.get("is_a", []): |
| 87 | + target_id = is_a_val.split("!")[0].strip() |
| 88 | + rows.append({"source_id": source_id, "target_id": target_id, "relation_type": "is_a"}) |
| 89 | + |
| 90 | + for rel_val in stanza.get("relationship", []): |
| 91 | + parts = rel_val.split() |
| 92 | + if len(parts) >= 2: |
| 93 | + rel_type = parts[0] |
| 94 | + target_id = parts[1].split("!")[0].strip() |
| 95 | + rows.append( |
| 96 | + { |
| 97 | + "source_id": source_id, |
| 98 | + "target_id": target_id, |
| 99 | + "relation_type": rel_type, |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + return pd.DataFrame(rows, columns=["source_id", "target_id", "relation_type"]) |
0 commit comments