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

Drafting IUPAC Name Property filtering with v0.5 #30

Draft
wants to merge 2 commits into
base: v0.5
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions kif_lib/compiler/sparql/mapping/pubchem.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def set_normalize_casrn(self, normalize_casrn: bool | None) -> None:

_re_canonical_smiles: Final[re.Pattern] = re.compile(
r'^[A-Za-z0-9+\-\*=#$:()\.>/\\\[\]%]+$')

_re_iupac_name: Final[re.Pattern] = re.compile(
r'^[A-Za-z0-9+\-\*=#$:()\.>/\\\[\]%]+$')

_re_cas_registry_number: Final[re.Pattern] = re.compile(
r'^\d+-\d+-\d+$')
Expand Down Expand Up @@ -125,6 +128,10 @@ def set_normalize_casrn(self, normalize_casrn: bool | None) -> None:
#: Checks whether argument is a canonical SMILES value.
CheckCanonicalSMILES: Final[M.EntryCallbackArgProcessorAlias] =\
functools.partial(M.CheckLiteral, match=_re_canonical_smiles)

#: Checks whether argument is a IUPAC Name value.
CheckIUPACName: Final[M.EntryCallbackArgProcessorAlias] =\
functools.partial(M.CheckLiteral, match=_re_iupac_name)

#: Checks whether argument is a CAS Registry Number.
CheckCAS_RegistryNumber: Final[M.EntryCallbackArgProcessorAlias] =\
Expand Down Expand Up @@ -217,6 +224,17 @@ def wd_label_compound(self, c: C, x: V_URI, y: VLiteral):
(attr, RDF.type, CHEMINF.IUPAC_Name_generated_by_LexiChem),
(attr, SIO.has_value, y))

@M.register(
[wd.IUPAC_Name(Item(x), Text(y, 'en'))],
{x: CheckCompound(),
y: CheckIUPACName(set_language='en')})
def wd_IUPAC_Name(self, c: C, x: V_URI, y: VLiteral):
attr = c.bnode()
c.q.triples()(
(x, SIO.has_attribute, attr),
(attr, RDF.type, CHEMINF.IUPAC_Name_generated_by_LexiChem),
(attr, SIO.has_value, y))

@M.register(
[wd.canonical_SMILES(Item(x), String(y))],
{x: CheckCompound(),
Expand Down
33 changes: 33 additions & 0 deletions kif_lib/store/mapping/pubchem.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ def check_canonical_SMILES(cls, v: Value) -> str:
Spec.Skip: `v` is not a canonical SMILES.
"""
return cls.check_string(v).content

@classmethod
def check_IUPAC_Name(cls, v: Value) -> str:
"""Checks whether `v` is a IUPAC Name.

Returns:
The string value of `v`.

Raises:
Spec.Skip: `v` is not a IUPAC Name.
"""
return cls.check_text(v).content

@classmethod
def check_chemical_formula(cls, v: Value) -> str:
Expand Down Expand Up @@ -572,6 +584,27 @@ def wd_COMPOUND_description(
(chebi_class, IAO.definition, v))


@PubChemMapping.register(
property=wd.IUPAC_Name,
datatype=TextDatatype(),
subject_prefix=PubChemMapping.COMPOUND)
def wd_IUPAC_Name(
spec: Spec,
q: Builder,
s: TTrm,
p: TTrm,
v: TTrm
) -> None:
if isinstance(v, Value):
###
# IMPORTANT: IUPAC values in PubChem are tagged with @en.
###
v = Text(spec.check_IUPAC_Name(cast(Value, v)), 'en')
with q.sp(s, SIO.has_attribute) as sp:
sp.pairs(
(RDF.type, CHEMINF.IUPAC_Name_generated_by_LexiChem),
(SIO.has_value, v))

@PubChemMapping.register(
property=wd.canonical_SMILES,
datatype=StringDatatype(),
Expand Down
2 changes: 2 additions & 0 deletions kif_lib/vocabulary/wd/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
auxiliary_verb = P(5401)
based_on_heuristic = P(887)
canonical_SMILES = P(233)
IUPAC_Name = P(2561)
capital = P(36)
CAS_Registry_Number = P(231)
ChEBI_ID = P(683)
Expand Down Expand Up @@ -190,6 +191,7 @@
'author_name_string',
'auxiliary_verb',
'based_on_heuristic',
'IUPAC_Name',
'canonical_SMILES',
'capital',
'CAS_Registry_Number',
Expand Down