Skip to content

Commit

Permalink
Use None for default specificity (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Jan 5, 2025
1 parent 57001c5 commit 16e27f8
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/pyobo/obographs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _get_class_node(term: Term) -> Node:
synonyms = [
Synonym.from_parsed(
name=synonym.name,
predicate=OIO_TO_REFERENCE[OBO_SYNONYM_TO_OIO[synonym.specificity]],
predicate=OIO_TO_REFERENCE[OBO_SYNONYM_TO_OIO[synonym.specificity or "EXACT"]],
synonym_type=_rewire(synonym.type) if synonym.type else None,
references=[_rewire(x) for x in synonym.provenance],
)
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def _extract_synonym(

return Synonym(
name=name,
specificity=specificity or "EXACT",
specificity=specificity,
type=synonym_typedef.reference if synonym_typedef else DEFAULT_SYNONYM_TYPE.reference,
provenance=provenance,
annotations=annotations,
Expand Down
6 changes: 1 addition & 5 deletions src/pyobo/sources/conso.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ def iter_terms() -> Iterable[Term]:
synonyms_df["reference"] = synonyms_df["reference"].map(
lambda s: [Reference.from_curie_or_uri(s)] if pd.notna(s) and s != "?" else [],
)
synonyms_df["specificity"] = synonyms_df["specificity"].map(
lambda s: "EXACT" if pd.isna(s) or s == "?" else s
)

synonyms = multidict(
(
identifier,
Synonym(
name=synonym,
provenance=provenance,
specificity=specificity,
specificity=None if pd.isna(specificity) or specificity == "?" else specificity,
),
)
for identifier, synonym, provenance, specificity in synonyms_df.values
Expand Down
8 changes: 4 additions & 4 deletions src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Synonym:
name: str

#: The specificity of the synonym
specificity: SynonymSpecificity = "EXACT"
specificity: SynonymSpecificity | None = None

#: The type of synonym. Must be defined in OBO document!
type: Reference = field(
Expand All @@ -129,7 +129,7 @@ def __lt__(self, other: Synonym) -> bool:
return self._sort_key() < other._sort_key()

def _sort_key(self) -> tuple[str, str, Reference]:
return self.name, self.specificity, self.type
return self.name, self.specificity or "EXACT", self.type

def to_obo(
self,
Expand All @@ -149,7 +149,7 @@ def _fp(
_synonym_typedef_warn(ontology_prefix, self.type, synonym_typedefs)
# TODO inherit specificity from typedef?
# TODO validation of specificity against typedef
x = f'"{self._escape(self.name)}" {self.specificity}'
x = f'"{self._escape(self.name)}" {self.specificity or "EXACT"}'
if self.type and self.type.pair != DEFAULT_SYNONYM_TYPE.pair:
x = f"{x} {reference_escape(self.type, ontology_prefix=ontology_prefix)}"
return f"{x} [{comma_separate_references(self.provenance)}]"
Expand Down Expand Up @@ -367,7 +367,7 @@ def append_synonym(
synonym = Synonym(
synonym,
type=type,
specificity=specificity or "EXACT",
specificity=specificity,
provenance=provenance or [],
)
self.synonyms.append(synonym)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ def test_extract_synonym(self):
(
Synonym(
name="LTEC I",
specificity="EXACT",
specificity=None,
provenance=[Reference(prefix="orphanet", identifier="93938")],
),
'"LTEC I" [Orphanet:93938]',
),
(
Synonym(name="LTEC I", specificity="EXACT"),
Synonym(name="LTEC I", specificity=None),
'"LTEC I" []',
),
(
Expand All @@ -156,11 +156,11 @@ def test_extract_synonym(self):
'"HAdV-A" BROAD omo:0003012 []',
),
(
Synonym(name="HAdV-A", specificity="EXACT", type=acronym.reference),
Synonym(name="HAdV-A", specificity=None, type=acronym.reference),
'"HAdV-A" OMO:0003012 []',
),
(
Synonym(name="HAdV-A", specificity="EXACT", type=acronym.reference),
Synonym(name="HAdV-A", specificity=None, type=acronym.reference),
'"HAdV-A" omo:0003012 []',
),
]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def test_synonym_minimal(self) -> None:
self.assertEqual(1, len(term.synonyms))
synonym = term.synonyms[0]
self.assertEqual("LTEC I", synonym.name)
self.assertEqual("EXACT", synonym.specificity)
self.assertIsNone(synonym.specificity)
self.assertEqual(DEFAULT_SYNONYM_TYPE.reference, synonym.type)
self.assertEqual([], synonym.provenance)

Expand Down Expand Up @@ -658,7 +658,7 @@ def test_synonym_with_type(self) -> None:
self.assertEqual(1, len(term.synonyms))
synonym = term.synonyms[0]
self.assertEqual("LTEC I", synonym.name)
self.assertEqual("EXACT", synonym.specificity)
self.assertIsNone(synonym.specificity)
self.assertEqual(Reference(prefix="omo", identifier="1234567"), synonym.type)
self.assertEqual([], synonym.provenance)

Expand Down

0 comments on commit 16e27f8

Please sign in to comment.