Skip to content

Commit

Permalink
Update obo.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Aug 23, 2023
1 parent c83995f commit 823e4b2
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/biomappings/contribute/obo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Contribute Biomappings back to ontologies encoded in the OBO flat file format.
Example ontologies using Functional OWL:
Example ontologies using the OBO flat file format:
- Uber Anatomy Ontology (UBERON)
- Mondo Disease Ontology (MONDO)
"""

from pathlib import Path
Expand Down Expand Up @@ -52,25 +53,28 @@ def update_obo(prefix: str, path: Union[str, Path], *, uppercase_prefix: bool =
def add_xref(lines, node, xref, xref_name, author_orcid: str):
"""Add xref to OBO file lines in the appropriate place."""
look_for_xref = False
#: The 0-indexed line number on which the first xref appears
start_xref_idx = None
#: The 0-indexed line number on which the definition appears in a given
def_idx = None
xref_entries = []
xref_values = set()
for idx, line in enumerate(lines):
if line == "id: %s\n" % node:
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
xxx: str = line.removeprefix("xref:").strip()
if "{" in xxx: # if there are qualifiers, strip them
xref_values.add(xxx[: xxx.find("{")].strip())
xref_line: str = line.removeprefix("xref:").strip()
# TODO handle [] xrefs
if "{" in xref_line: # if there are qualifiers, only keep everything up until them
xref_values.add(xref_line[: xref_line.find("{")].strip())
else:
xref_values.add(xxx)
xref_entries.append(xxx)
xref_values.add(xref_line)
xref_entries.append(xref_line)
if start_xref_idx and not line.startswith("xref"):
break
if look_for_xref and not line.strip():
Expand All @@ -82,7 +86,9 @@ def add_xref(lines, node, xref, xref_name, author_orcid: str):
xref_entries.append(xref)
xref_entries = sorted(xref_entries)
xr_idx = xref_entries.index(xref)
line = f'xref: {xref} {{dcterms:contributor="https://orcid.org/{author_orcid}"}} ! {xref_name}\n'
line = (
f'xref: {xref} {{dcterms:contributor="https://orcid.org/{author_orcid}"}} ! {xref_name}\n'
)
lines.insert(start_xref_idx + xr_idx, line)
return lines

Expand Down

0 comments on commit 823e4b2

Please sign in to comment.