Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update synonyms handling #138

Merged
merged 7 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install tox
run: |
sudo apt-get install graphviz
pip install tox
- name: Check manifest
run: tox -e manifest
- name: Check code quality with flake8
Expand All @@ -38,7 +40,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install tox
run: |
sudo apt-get install graphviz
pip install tox
- name: Check RST conformity with doc8
run: tox -e doc8
- name: Check README.rst
Expand Down Expand Up @@ -66,7 +70,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install tox
run: |
sudo apt-get install graphviz
pip install tox
- name: Test with pytest
run:
tox -e py
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ install_requires =
drugbank_downloader
chembl_downloader
umls_downloader
typing_extensions

# Random options
zip_safe = false
Expand Down
14 changes: 8 additions & 6 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Obo,
Reference,
Synonym,
SynonymSpecificities,
SynonymSpecificity,
SynonymTypeDef,
Term,
TypeDef,
Expand Down Expand Up @@ -415,14 +417,14 @@ def _extract_synonym(
logger.warning("[%s:%s] invalid synonym: %s", prefix, identifier, s)
return None

specificity = None
for skos in "RELATED", "EXACT", "BROAD", "NARROW":
if rest.startswith(skos):
specificity = skos
rest = rest[len(skos) :].strip()
specificity: Optional[SynonymSpecificity] = None
for _specificity in SynonymSpecificities:
if rest.startswith(_specificity):
specificity = _specificity
rest = rest[len(_specificity) :].strip()
break

stype = None
stype: Optional[SynonymTypeDef] = None
if specificity is not None: # go fishing for a synonym type definition
for std in synonym_typedefs:
if rest.startswith(std):
Expand Down
38 changes: 29 additions & 9 deletions src/pyobo/ssg/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,35 @@ <h5 class="card-header">
{% endfor %}
{% endif %}
</dl>

{% if obo.synonym_typedefs %}
<h3>Synonym Definitions</h3>
<ul>
{% for typedef in obo.synonym_typedefs %}
<li>{{ typedef }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>

{% if obo.synonym_typedefs %}
<div class="card" style="margin-top: 1em;">
<h5 class="card-header">
Synonym Type Definitions
</h5>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Specificity</th>
</tr>
</thead>
<tbody>
{% for typedef in obo.synonym_typedefs %}
<tr>
<td>{{ typedef.id }}</td>
<td>{{ typedef.name }}</td>
<td>{{ typedef.specificity }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}

{% if manifest %}
<div class="card" style="margin-top: 1em;">
<h5 class="card-header">
Expand All @@ -57,6 +75,7 @@ <h5 class="card-header">
<th>Type</th>
<th>Local Identifier</th>
<th>Name</th>
<th>Definition</th>
</tr>
</thead>
<tbody>
Expand All @@ -71,6 +90,7 @@ <h5 class="card-header">
</td>
<td align="right"><a href="{{ term.identifier }}">{{ term.identifier }}</a></td>
<td>{{ term.name }}</td>
<td>{{ term.definition }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
15 changes: 4 additions & 11 deletions src/pyobo/ssg/term.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ <h5 class="card-header">

{{ list_references(term.alt_ids, "Alt. IDs") }}
{% if term.synonyms %}
{% for synonym in term.synonyms %}
<dt>
<span class="badge badge-primary">Synonym</span>
{{ synonym.specificity.title() }}{% if synonym.type %}({{ synonym.type.name }}){% endif %}
</dt>
<dd>
{% if term.synonyms|length == 1 %}
{{ term.synonyms[0].name }} (<span class="muted">{{ term.synonyms[0].specificity }}</span>)
{% else %}
<ul>
{% for synonym in term.synonyms %}
<li>{{ synonym.name }} (<span class="muted">{{ synonym.specificity }}</span>)</li>
{% endfor %}
</ul>
{% endif %}
</dd>
<dd>{{ synonym.name }}</dd>
{% endfor %}
{% endif %}
{{ list_references(term.parents, "Parents") }}
{{ list_references(children, "Children") }}
Expand Down
2 changes: 2 additions & 0 deletions src/pyobo/struct/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .struct import ( # noqa: F401
Obo,
Synonym,
SynonymSpecificities,
SynonymSpecificity,
SynonymTypeDef,
Term,
make_ad_hoc_ontology,
Expand Down
19 changes: 16 additions & 3 deletions src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from more_click import force_option, verbose_option
from networkx.utils import open_file
from tqdm import tqdm
from typing_extensions import Literal

from .reference import Reference, Referenced
from .typedef import (
Expand Down Expand Up @@ -65,13 +66,18 @@
__all__ = [
"Synonym",
"SynonymTypeDef",
"SynonymSpecificity",
"SynonymSpecificities",
"Term",
"Obo",
"make_ad_hoc_ontology",
]

logger = logging.getLogger(__name__)

SynonymSpecificity = Literal["EXACT", "NARROW", "BROAD", "RELATED"]
SynonymSpecificities: Sequence[SynonymSpecificity] = ("EXACT", "NARROW", "BROAD", "RELATED")


@dataclass
class Synonym:
Expand All @@ -81,7 +87,7 @@ class Synonym:
name: str

#: The specificity of the synonym
specificity: str = "EXACT"
specificity: SynonymSpecificity = "EXACT"

#: The type of synonym. Must be defined in OBO document!
type: Optional["SynonymTypeDef"] = None
Expand Down Expand Up @@ -110,13 +116,19 @@ class SynonymTypeDef:

id: str
name: str
specificity: Optional[SynonymSpecificity] = None

def to_obo(self) -> str:
"""Serialize to OBO."""
return f'synonymtypedef: {self.id} "{self.name}"'
if self.specificity:
return f'synonymtypedef: {self.id} "{self.name}" {self.specificity}'
else:
return f'synonymtypedef: {self.id} "{self.name}"'

@classmethod
def from_text(cls, text) -> "SynonymTypeDef":
def from_text(
cls, text: str, specificity: Optional[SynonymSpecificity] = None
) -> "SynonymTypeDef":
"""Get a type definition from text that's normalized."""
return cls(
id=text.lower()
Expand All @@ -126,6 +138,7 @@ def from_text(cls, text) -> "SynonymTypeDef":
.replace(")", "")
.replace("(", ""),
name=text.replace('"', ""),
specificity=specificity,
)


Expand Down