Skip to content

Commit 9ebd8fc

Browse files
authored
Simplify typing (#213)
1 parent 60616c1 commit 9ebd8fc

8 files changed

Lines changed: 43 additions & 32 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["uv_build>=0.6.6,<0.7"]
2+
requires = ["uv_build>=0.6.6,<1.0"]
33
build-backend = "uv_build"
44

55
[project]
@@ -60,8 +60,8 @@ dependencies = [
6060
"click",
6161
"pyyaml",
6262
"tqdm",
63-
"pystow>=0.2.7",
64-
"bioregistry>=0.10.43",
63+
"pystow>=0.7.11",
64+
"bioregistry>=0.12.42",
6565
]
6666

6767
[project.optional-dependencies]
@@ -91,10 +91,10 @@ web = [
9191
"flask-wtf",
9292
]
9393
gilda = [
94-
"pyobo[gilda]>=0.12.0",
94+
"pyobo[gilda]>=0.12.8",
9595
]
9696
gilda-slim = [
97-
"pyobo[gilda-slim]>=0.12.0",
97+
"pyobo[gilda-slim]>=0.12.8",
9898
]
9999
ndex = [
100100
"ndex2",

src/biomappings/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def curate(
6060
resolver_base: str | None,
6161
) -> None:
6262
"""Run a target curation web app."""
63-
from curies import NamableReference
63+
from curies import Reference
6464

6565
from .resources import _load_table
6666
from .wsgi import get_app
6767

68-
target_references: list[NamableReference] = []
68+
target_references: list[Reference] = []
6969
for mapping in _load_table(path, standardize=True):
7070
target_references.append(mapping.subject)
7171
target_references.append(mapping.object)

src/biomappings/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _graph_from_mappings(
8787
mapping.subject,
8888
mapping.object,
8989
relation=mapping.predicate.curie,
90-
provenance=mapping.author.name if mapping.author else None,
90+
provenance=mapping.author.curie if mapping.author else None,
9191
type=mapping.mapping_justification.curie,
9292
strata=strata,
9393
)

src/biomappings/resources/__init__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656

5757
logger = logging.getLogger(__name__)
5858

59+
COLUMNS = [
60+
"subject_id",
61+
"subject_label",
62+
"predicate_id",
63+
"object_id",
64+
"object_label",
65+
"mapping_justification",
66+
"author_id",
67+
"mapping_tool",
68+
"predicate_modifier",
69+
]
70+
5971

6072
class _CuratedTuple(NamedTuple):
6173
"""A tuple for writing manual curations to SSSOM TSV."""
@@ -283,8 +295,8 @@ def append_true_mapping_tuples(mappings: Iterable[SemanticMapping]) -> None:
283295

284296

285297
def write_true_mappings(mappings: Iterable[SemanticMapping], *, path: Path | None = None) -> None:
286-
"""Write mappigns to the true mappings file."""
287-
_write_helper(mappings, path=path or POSITIVES_SSSOM_PATH, mode="w", t="curated")
298+
"""Write mappings to the true mappings file."""
299+
_write_helper(mappings, path or POSITIVES_SSSOM_PATH, mode="w", t="curated")
288300

289301

290302
def lint_true_mappings(*, path: Path | None = None, standardize: bool) -> None:
@@ -316,7 +328,7 @@ def append_false_mappings(
316328
"""Append new lines to the false mappings table."""
317329
if path is None:
318330
path = NEGATIVES_SSSOM_PATH
319-
_write_helper(mappings=mappings, path=path, mode="a", t="curated")
331+
_write_helper(mappings, path=path, mode="a", t="curated")
320332
if sort:
321333
lint_false_mappings(path=path, standardize=standardize)
322334

@@ -412,7 +424,7 @@ def append_predictions(
412424

413425
if path is None:
414426
path = PREDICTIONS_SSSOM_PATH
415-
_write_helper(mappings, path, mode="a", t="predicted")
427+
_write_helper(mappings, path=path, mode="a", t="predicted")
416428
if sort:
417429
lint_predictions(path=path, standardize=standardize)
418430

@@ -441,8 +453,7 @@ def lint_predictions(
441453
additional_curated_mappings or [],
442454
),
443455
)
444-
mappings = _remove_redundant(mappings)
445-
mappings = sorted(mappings)
456+
mappings = _clean_mappings(mappings)
446457
write_predictions(mappings, path=path)
447458

448459

@@ -454,6 +465,11 @@ def remove_mappings(
454465
return (mapping for mapping in mappings if get_canonical_tuple(mapping) not in skip_tuples)
455466

456467

468+
def _clean_mappings(mappings: Iterable[SemanticMapping]):
469+
m = sorted(mappings)
470+
return _remove_redundant(m)
471+
472+
457473
def _remove_redundant(mappings: Iterable[SemanticMapping]) -> Iterable[SemanticMapping]:
458474
dd = defaultdict(list)
459475
for mapping in mappings:

src/biomappings/resources/export_sssom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def get_sssom_df(*, use_tqdm: bool = False) -> SSSOMReturnTuple:
7070
prefixes: set[str] = {"semapv"}
7171

7272
# NEW WAY: load all DFs, concat them, reorder columns
73-
a = pd.read_csv(POSITIVES_SSSOM_PATH, sep="\t")
74-
b = pd.read_csv(NEGATIVES_SSSOM_PATH, sep="\t")
75-
c = pd.read_csv(PREDICTIONS_SSSOM_PATH, sep="\t")
73+
a = pd.read_csv(POSITIVES_SSSOM_PATH, sep="\t", comment="#")
74+
b = pd.read_csv(NEGATIVES_SSSOM_PATH, sep="\t", comment="#")
75+
c = pd.read_csv(PREDICTIONS_SSSOM_PATH, sep="\t", comment="#")
7676
df = pd.concat([a, b, c])
7777
df = df[columns]
7878

src/biomappings/testing.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
import bioregistry
1414
from bioregistry import NormalizedNamableReference
15-
from curies import NamableReference
15+
from curies import Reference
1616

1717
from biomappings.resources import (
1818
CURATORS_PATH,
1919
SemanticMapping,
20-
_CuratedTuple,
21-
_PredictedTuple,
2220
load_mappings,
2321
load_predictions,
2422
)
@@ -38,7 +36,6 @@
3836

3937
SEMAPV_ID_TO_NAME = get_semapv_id_to_name()
4038

41-
TPL = TypeVar("TPL", _CuratedTuple, _PredictedTuple)
4239
X = TypeVar("X")
4340
Y = TypeVar("Y")
4441

@@ -98,7 +95,7 @@ def test_valid_curies(self) -> None:
9895
if mapping.author is not None:
9996
self.assert_valid(label, line, mapping.author)
10097

101-
def assert_valid(self, label: str, line: int, reference: NamableReference) -> None:
98+
def assert_valid(self, label: str, line: int, reference: Reference) -> None:
10299
"""Assert a reference is valid and normalized to the Bioregistry."""
103100
norm_prefix = bioregistry.normalize_prefix(reference.prefix)
104101
self.assertIsNotNone(
@@ -167,9 +164,7 @@ def test_cross_redundancy(self) -> None:
167164

168165
def assert_no_internal_redundancies(self, mappings: list[SemanticMapping]) -> None:
169166
"""Assert that the list of mappings doesn't have any redundancies."""
170-
counter: defaultdict[tuple[NamableReference, NamableReference], list[int]] = defaultdict(
171-
list
172-
)
167+
counter: defaultdict[tuple[Reference, Reference], list[int]] = defaultdict(list)
173168
for line_number, mapping in enumerate(mappings, start=1):
174169
counter[mapping.subject, mapping.object].append(line_number)
175170
redundant = _extract_redundant(counter)

src/biomappings/wsgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pydantic
1616
import werkzeug
1717
from bioregistry import NormalizedNamableReference
18-
from curies import NamableReference
18+
from curies import NamableReference, Reference
1919
from flask import current_app
2020
from flask_wtf import FlaskForm
2121
from pydantic import BaseModel
@@ -104,7 +104,7 @@ def url_for_state(endpoint, state: State, **kwargs: Any) -> str:
104104

105105
def get_app(
106106
*,
107-
target_references: Iterable[NamableReference] | None = None,
107+
target_references: Iterable[Reference] | None = None,
108108
predictions_path: Path | None = None,
109109
positives_path: Path | None = None,
110110
negatives_path: Path | None = None,
@@ -153,7 +153,7 @@ class Controller:
153153
def __init__(
154154
self,
155155
*,
156-
target_references: Iterable[NamableReference] | None = None,
156+
target_references: Iterable[Reference] | None = None,
157157
predictions_path: Path | None = None,
158158
positives_path: Path | None = None,
159159
negatives_path: Path | None = None,

tests/test_wsgi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from bioregistry import NormalizedNamableReference as Reference
88

99
from biomappings import SemanticMapping
10-
from biomappings.resources import _write_helper, write_predictions
10+
from biomappings.resources import COLUMNS, write_predictions
1111
from biomappings.wsgi import Controller, State, get_app
1212

1313
TEST_USER = Reference(prefix="orcid", identifier="0000-0000-0000-0000", name="Max Mustermann")
@@ -69,9 +69,9 @@ def setUp(self) -> None:
6969
unsure_path = directory.joinpath("unsure.tsv")
7070

7171
write_predictions(predictions, path=predictions_path)
72-
_write_helper([], path=positives_path, mode="w", t="curated")
73-
_write_helper([], path=negatives_path, mode="w", t="curated")
74-
_write_helper([], path=unsure_path, mode="w", t="curated")
72+
for path in [positives_path, negatives_path, unsure_path]:
73+
with path.open("w") as file:
74+
print(*COLUMNS, sep="\t", file=file)
7575

7676
self.controller = Controller(
7777
predictions_path=predictions_path,

0 commit comments

Comments
 (0)