Skip to content

Commit

Permalink
Update docs and code
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jan 31, 2024
1 parent 87fc576 commit 5ec8618
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
45 changes: 45 additions & 0 deletions lexica/cell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Cell and Cell Line Lexicon

This directory contains a coherent, merged lexical index for the following resources:

1. Medical Subject Headings (MeSH) (children of `mesh:D002477`)
2. Experimental Factor Ontology (EFO) (children of `efo:0000324`)
3. Cellosaurus
4. Cancer Cell Line Encyclopedia (CCLE)
5. DepMap
6. BRENDA Tissue Ontology (BTO)
7. Cell Ontology (CL)
8. Cell Line Ontology (CLO)


## Using in Python

Use in Python like in the following:

```python
import biolexica
import gilda

URL = "https://github.com/biopragmatics/biolexica/raw/main/lexica/cell/terms.tsv.gz"

grounder: gilda.Grounder = biolexica.load_grounder(URL)
scored_matches = grounder.ground("HeLA")
```

## Running an API

If you've cloned the repository, you can run:

```shell
python web.py
```

If you have `biolexica[web]` installed, you can do:

```python
from biolexica.web import run_app

URL = "https://github.com/biopragmatics/biolexica/raw/main/lexica/cell/terms.tsv.gz"

run_app(URL)
```
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ docs =
sphinx-click
sphinx_automodapi
autodoc_pydantic
web =
uvicorn
fastapi


[options.entry_points]
console_scripts =
Expand Down
17 changes: 17 additions & 0 deletions src/biolexica/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""API for assembling biomedial lexica."""

import logging
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, List, Literal, Optional, Union
from urllib.request import urlretrieve

import bioregistry
import biosynonyms
Expand All @@ -21,13 +23,16 @@
"Input",
"assemble_terms",
"iter_terms_by_prefix",
"load_grounder",
]

logger = logging.getLogger(__name__)

HERE = Path(__file__).parent.resolve()
Processor = Literal["pyobo", "bioontologies", "biosynonyms", "gilda"]

GrounderHint = Union[gilda.Grounder, str, Path]


class Input(BaseModel):
"""An input towards lexicon assembly."""
Expand All @@ -43,6 +48,18 @@ class TermsInput(BaseModel):
terms: List[gilda.Term]


def load_grounder(grounder: GrounderHint) -> gilda.Grounder:
"""Load a gilda grounder, potentially from a remote location."""
if isinstance(grounder, str) and grounder.startswith("http"):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory).joinpath("terms.tsv.gz")
urlretrieve(grounder, path) # noqa:S310
return gilda.Grounder(path)
if isinstance(grounder, (str, Path)):
return gilda.Grounder(grounder)
return grounder


def assemble_grounder(
inputs: List[Union[Input, TermsInput]],
mappings: Optional[List["semra.Mapping"]] = None,
Expand Down
14 changes: 6 additions & 8 deletions src/biolexica/web.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""A FastAPI wrapper for Gilda."""

import pathlib
from typing import List, Union
from typing import List

import fastapi
import gilda
from curies import Reference
from fastapi import FastAPI, Request
from pydantic import BaseModel

from biolexica.api import GrounderHint, load_grounder

__all__ = [
"run_app",
"get_app",
Expand All @@ -25,20 +26,17 @@ class Match(BaseModel):
score: float


def run_app(grounder: Union[gilda.Grounder, str, pathlib.Path]):
def run_app(grounder: GrounderHint):
"""Costruct a FastAPI app from a Gilda grounder and run with :mod:`uvicorn`."""
import uvicorn

if isinstance(grounder, (str, pathlib.Path)):
grounder = gilda.Grounder(grounder)

uvicorn.run(get_app(grounder))


def get_app(grounder: gilda.Grounder) -> FastAPI:
def get_app(grounder: GrounderHint) -> FastAPI:
"""Construct a FastAPI app from a Gilda grounder."""
app = FastAPI(title="Biolexica Grounder")
app.state = grounder
app.state = load_grounder(grounder)
app.include_router(api_router, prefix="/api")
return app

Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ commands = pyroma --min=10 .
description = Run the pyroma tool to check the package friendliness of the project.

[testenv:mypy]
deps = mypy
deps =
mypy
pydantic
skip_install = true
commands = mypy --install-types --non-interactive --ignore-missing-imports src/
description = Run the mypy tool to check static typing on the project.
Expand Down

0 comments on commit 5ec8618

Please sign in to comment.