Skip to content

Commit

Permalink
Enable passing version arguments to more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Mar 13, 2024
1 parent 1b6450a commit 282c80f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
33 changes: 24 additions & 9 deletions src/pyobo/api/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@


@wrap_norm_prefix
def get_properties_df(prefix: str, *, force: bool = False) -> pd.DataFrame:
def get_properties_df(
prefix: str, *, force: bool = False, version: Optional[str] = None
) -> pd.DataFrame:
"""Extract properties.
:param prefix: the resource to load
:param force: should the resource be re-downloaded, re-parsed, and re-cached?
:returns: A dataframe with the properties
"""
version = get_version(prefix)
if version is None:
version = get_version(prefix)
path = prefix_cache_join(prefix, name="properties.tsv", version=version)

@cached_df(path=path, dtype=str, force=force)
Expand All @@ -59,6 +62,7 @@ def get_filtered_properties_mapping(
*,
use_tqdm: bool = False,
force: bool = False,
version: Optional[str] = None,
) -> Mapping[str, str]:
"""Extract a single property for each term as a dictionary.
Expand All @@ -68,7 +72,12 @@ def get_filtered_properties_mapping(
:param force: should the resource be re-downloaded, re-parsed, and re-cached?
:returns: A mapping from identifier to property value
"""
version = get_version(prefix)
df = get_properties_df(prefix=prefix, force=force, version=version)
df = df[df["property"] == prop]
return dict(df[[f"{prefix}_id", "value"]].values)

if version is None:
version = get_version(prefix)
path = prefix_cache_join(prefix, "properties", name=f"{prop}.tsv", version=version)
all_properties_path = prefix_cache_join(prefix, name="properties.tsv", version=version)

Expand All @@ -95,6 +104,7 @@ def get_filtered_properties_multimapping(
*,
use_tqdm: bool = False,
force: bool = False,
version: Optional[str] = None,
) -> Mapping[str, List[str]]:
"""Extract multiple properties for each term as a dictionary.
Expand All @@ -104,7 +114,8 @@ def get_filtered_properties_multimapping(
:param force: should the resource be re-downloaded, re-parsed, and re-cached?
:returns: A mapping from identifier to property values
"""
version = get_version(prefix)
if version is None:
version = get_version(prefix)
path = prefix_cache_join(prefix, "properties", name=f"{prop}.tsv", version=version)
all_properties_path = prefix_cache_join(prefix, name="properties.tsv", version=version)

Expand All @@ -124,7 +135,7 @@ def _mapping_getter() -> Mapping[str, List[str]]:
return _mapping_getter()


def get_property(prefix: str, identifier: str, prop: str) -> Optional[str]:
def get_property(prefix: str, identifier: str, prop: str, **kwargs) -> Optional[str]:
"""Extract a single property for the given entity.
:param prefix: the resource to load
Expand All @@ -136,11 +147,13 @@ def get_property(prefix: str, identifier: str, prop: str) -> Optional[str]:
>>> pyobo.get_property('chebi', '132964', 'http://purl.obolibrary.org/obo/chebi/smiles')
"C1(=CC=C(N=C1)OC2=CC=C(C=C2)O[C@@H](C(OCCCC)=O)C)C(F)(F)F"
"""
filtered_properties_mapping = get_filtered_properties_mapping(prefix=prefix, prop=prop)
filtered_properties_mapping = get_filtered_properties_mapping(
prefix=prefix, prop=prop, **kwargs
)
return filtered_properties_mapping.get(identifier)


def get_properties(prefix: str, identifier: str, prop: str) -> Optional[List[str]]:
def get_properties(prefix: str, identifier: str, prop: str, **kwargs) -> Optional[List[str]]:
"""Extract a set of properties for the given entity.
:param prefix: the resource to load
Expand All @@ -149,7 +162,7 @@ def get_properties(prefix: str, identifier: str, prop: str) -> Optional[List[str
:returns: Multiple values for the property. If only one is expected, use :func:`get_property`
"""
filtered_properties_multimapping = get_filtered_properties_multimapping(
prefix=prefix, prop=prop
prefix=prefix, prop=prop, **kwargs
)
return filtered_properties_multimapping.get(identifier)

Expand All @@ -161,6 +174,7 @@ def get_filtered_properties_df(
*,
use_tqdm: bool = False,
force: bool = False,
version: Optional[str] = None,
) -> pd.DataFrame:
"""Extract a single property for each term.
Expand All @@ -170,7 +184,8 @@ def get_filtered_properties_df(
:param force: should the resource be re-downloaded, re-parsed, and re-cached?
:returns: A dataframe from identifier to property value. Columns are [<prefix>_id, value].
"""
version = get_version(prefix)
if version is None:
version = get_version(prefix)
path = prefix_cache_join(prefix, "properties", name=f"{prop}.tsv", version=version)
all_properties_path = prefix_cache_join(prefix, name="properties.tsv", version=version)

Expand Down
6 changes: 4 additions & 2 deletions src/pyobo/sources/chebi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
]


def get_chebi_id_smiles_mapping() -> Mapping[str, str]:
def get_chebi_id_smiles_mapping(**kwargs) -> Mapping[str, str]:
"""Get a mapping from ChEBI identifiers to SMILES.
This is common enough that it gets its own function :)
"""
return get_filtered_properties_mapping("chebi", "http://purl.obolibrary.org/obo/chebi/smiles")
return get_filtered_properties_mapping(
"chebi", "http://purl.obolibrary.org/obo/chebi/smiles", **kwargs
)


def get_chebi_smiles_id_mapping() -> Mapping[str, str]:
Expand Down
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ commands = pyroma --min=10 .
description = Run the pyroma tool to check the package friendliness of the project.

[testenv:mypy]
deps = mypy
deps =
mypy
types-requests
types-tabulate
types-setuptools
skip_install = true
commands = mypy --install-types --non-interactive --ignore-missing-imports src/pyobo/
description = Run the mypy tool to check static typing on the project.
Expand Down Expand Up @@ -172,6 +176,7 @@ skip_install = true
deps =
wheel
build
setuptools
commands =
python -m build --sdist --wheel --no-isolation

Expand Down

0 comments on commit 282c80f

Please sign in to comment.