-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathadd_mesh_xrefs_to_uberon_obo.py
More file actions
61 lines (49 loc) · 1.89 KB
/
add_mesh_xrefs_to_uberon_obo.py
File metadata and controls
61 lines (49 loc) · 1.89 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
"""This script adds newly inferred cross-references for UBERON.
These are added directly to the version controlled UBERON OBO file.
"""
from biomappings import load_positive_mappings
EDITABLE_OBO_PATH = "/Users/ben/src/uberon/src/ontology/uberon-edit.obo"
def add_xref(lines: list[str], node: str, xref_curie: str) -> list[str]:
"""Add xref to OBO file lines in the appropriate place."""
look_for_xref = False
start_xref_idx: int | None = None
def_idx = None
xref_entries = []
for idx, line in enumerate(lines):
if line == f"id: {node}\n":
look_for_xref = True
if look_for_xref and line.startswith("def"):
def_idx = idx
if look_for_xref:
if line.startswith("xref"):
if not start_xref_idx:
start_xref_idx = idx
xref_entries.append(line[6:].strip())
if start_xref_idx and not line.startswith("xref"):
break
if look_for_xref and not line.strip():
start_xref_idx = def_idx
xref_entries.append(xref_curie)
xref_entries = sorted(xref_entries)
xr_idx = xref_entries.index(xref_curie)
if start_xref_idx is None:
raise ValueError
lines.insert(start_xref_idx + xr_idx, f"xref: {xref_curie}\n")
return lines
def main() -> None:
"""Add curated cross-references to the UBERON OBO file."""
mappings = load_positive_mappings()
uberon_mappings = [m for m in mappings if m.subject.prefix == "uberon"]
with open(EDITABLE_OBO_PATH) as fh:
lines = fh.readlines()
for mapping in uberon_mappings:
lines = add_xref(
lines,
# TODO update to OBO-preferred prefix for subject curie
mapping.subject.curie,
mapping.object.curie,
)
with open(EDITABLE_OBO_PATH, "w") as fh:
fh.writelines(lines)
if __name__ == "__main__":
main()