Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Jan 8, 2024
1 parent c07ffd2 commit 94acd5e
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ $ curl 'https://normalize.cancervariants.org/gene/normalize?q=BRAF' | python -m
Or utilize the [Python API](https://gene-normalizer.readthedocs.io/en/latest/api/query_api.html) for fast access:

```python
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.query import QueryHandler
>>> q = QueryHandler(create_db())
>>> q = QueryHandler(get_db())
>>> result = q.normalize("KRAS")
>>> result.normalized_id
'hgnc:6407'
Expand Down
4 changes: 2 additions & 2 deletions docs/scripts/generate_normalize_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import gravis as gv

from gene.database import create_db
from gene.database import get_db
from gene.etl.base import APP_ROOT
from gene.query import QueryHandler
from gene.schemas import UnmergedNormalizationService
Expand Down Expand Up @@ -80,7 +80,7 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:

def gen_norm_figure() -> None:
"""Generate normalized graph figure for docs."""
q = QueryHandler(create_db())
q = QueryHandler(get_db())

otx2p1 = "OTX2P1"
otx2p2 = "OTX2P2"
Expand Down
4 changes: 2 additions & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ The Gene Normalizer can also be installed locally as a Python package for fast a
.. code-block:: pycon
>>> from gene.query import QueryHandler
>>> from gene.database import create_db
>>> q = QueryHandler(create_db())
>>> from gene.database import get_db
>>> q = QueryHandler(get_db())
>>> result = q.normalize("BRAF")
>>> result.normalized_id
'hgnc:1097'
Expand Down
6 changes: 3 additions & 3 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ Each search mode can be accessed directly within Python using the :ref:`query AP

.. code-block:: pycon
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.query import QueryHandler
>>> q = QueryHandler(create_db())
>>> q = QueryHandler(get_db())
>>> normalized_response = q.normalize('HER2')
>>> normalized_response
>>> normalized_response.match_type
<MatchType.ALIAS: 60>
>>> normalized_response.gene.label
'ERBB2'
Critically, the ``QueryHandler`` class must receive a database interface instance as its first argument. The most straightforward way to construct a database instance, as demonstrated above, is with the :py:meth:`create_db() <gene.database.database.create_db>` method. This method tries to build a database connection based on a number of conditions, which are resolved in the following order:
Critically, the ``QueryHandler`` class must receive a database interface instance as its first argument. The most straightforward way to construct a database instance, as demonstrated above, is with the :py:meth:`get_db() <gene.database.database.get_db>` method. This method tries to build a database connection based on a number of conditions, which are resolved in the following order:

1) if environment variable ``GENE_NORM_ENV`` is set to a value, or if the ``aws_instance`` method argument is True, try to create a cloud DynamoDB connection
2) if the ``db_url`` method argument is given a non-None value, try to create a DB connection to that address (if it looks like a PostgreSQL URL, create a PostgreSQL connection, but otherwise try DynamoDB)
Expand Down
14 changes: 7 additions & 7 deletions src/gene/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import click

from gene.database import create_db
from gene.database import get_db
from gene.database.database import DatabaseError
from gene.etl.update import update_all_sources, update_normalized, update_source
from gene.schemas import SourceName
Expand Down Expand Up @@ -79,7 +79,7 @@ def update(
click.echo(ctx.get_help())
ctx.exit(1)

db = create_db(db_url, aws_instance, silent)
db = get_db(db_url, aws_instance, silent)

processed_ids = None
if all:
Expand Down Expand Up @@ -129,7 +129,7 @@ def update_from_remote(data_url: Optional[str], db_url: str, silent: bool) -> No
click.get_current_context().exit()
if not data_url:
data_url = os.environ.get("GENE_NORM_REMOTE_DB_URL")
db = create_db(db_url, False, silent)
db = get_db(db_url, False, silent)
try:
db.load_from_remote(data_url)
except NotImplementedError:
Expand Down Expand Up @@ -165,8 +165,8 @@ def check_db(db_url: str, verbose: bool, silent: bool) -> None:
This command is equivalent to the combination of the database classes'
``check_schema_initialized()`` and ``check_tables_populated()`` methods:
>>> from gene.database import create_db
>>> db = create_db()
>>> from gene.database import get_db
>>> db = get_db()
>>> db.check_schema_initialized() and db.check_tables_populated()
True # DB passes checks
Expand All @@ -175,7 +175,7 @@ def check_db(db_url: str, verbose: bool, silent: bool) -> None:
:param verbose: if true, print result to console
:param silent: if True, suppress console output
""" # noqa: D301
db = create_db(db_url, False, silent)
db = get_db(db_url, False, silent)
if not db.check_schema_initialized():
if verbose:
click.echo("Health check failed: DB schema uninitialized.")
Expand Down Expand Up @@ -212,7 +212,7 @@ def dump_database(output_directory: Path, db_url: str, silent: bool) -> None:
if not output_directory:
output_directory = Path(".")

db = create_db(db_url, False, silent)
db = get_db(db_url, False, silent)
try:
db.export_db(output_directory)
except NotImplementedError:
Expand Down
12 changes: 10 additions & 2 deletions src/gene/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"""Provide database clients."""
from .database import (
AWS_ENV_VAR_NAME,
AbstractDatabase,
DatabaseError,
DatabaseInitializationError,
DatabaseReadError,
DatabaseWriteError,
create_db,
get_db,
)

__all__ = [
"AWS_ENV_VAR_NAME",
"DatabaseError",
"DatabaseInitializationError",
"DatabaseReadError",
"DatabaseWriteError",
"get_db",
]
16 changes: 8 additions & 8 deletions src/gene/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def get_all_records(self, record_type: RecordType) -> Generator[Dict, None, None
For example,
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.schemas import RecordType
>>> db = create_db()
>>> db = get_db()
>>> for record in db.get_all_records(RecordType.MERGER):
>>> pass # do something
Expand Down Expand Up @@ -274,7 +274,7 @@ def confirm_aws_db_use(env_name: AwsEnvName) -> None:
sys.exit()


def create_db(
def get_db(
db_url: Optional[str] = None, aws_instance: bool = False, silent: bool = False
) -> AbstractDatabase:
"""Database factory method. Checks environment variables and provided parameters
Expand All @@ -286,18 +286,18 @@ def create_db(
Some examples:
>>> from gene.database import create_db
>>> default_db = create_db() # by default, creates DynamoDB connection on port 8000
>>> from gene.database import get_db
>>> default_db = get_db() # by default, creates DynamoDB connection on port 8000
>>>
>>> postgres_url = "postgresql://postgres@localhost:5432/gene_normalizer"
>>> pg_db = create_db(postgres_url) # creates Postgres connection at port 5432
>>> pg_db = get_db(postgres_url) # creates Postgres connection at port 5432
>>>
>>> import os
>>> os.environ["GENE_NORM_DB_URL"] = "http://localhost:8001"
>>> local_db = create_db() # creates DynamoDB connection on port 8001
>>> local_db = get_db() # creates DynamoDB connection on port 8001
>>>
>>> os.environ["GENE_NORM_ENV"] = "Prod"
>>> prod_db = create_db() # creates connection to AWS cloud DynamoDB instance,
>>> prod_db = get_db() # creates connection to AWS cloud DynamoDB instance,
>>> # overruling `GENE_NORM_DB_URL` variable setting
Precedence is handled for connection settings like so:
Expand Down
4 changes: 2 additions & 2 deletions src/gene/database/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ def get_all_records(self, record_type: RecordType) -> Generator[Dict, None, None
For example,
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.schemas import RecordType
>>> db = create_db()
>>> db = get_db()
>>> for record in db.get_all_records(RecordType.MERGER):
>>> pass # do something
Expand Down
4 changes: 2 additions & 2 deletions src/gene/database/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ def get_all_records(self, record_type: RecordType) -> Generator[Dict, None, None
For example,
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.schemas import RecordType
>>> db = create_db()
>>> db = get_db()
>>> for record in db.get_all_records(RecordType.MERGER):
>>> pass # do something
Expand Down
8 changes: 4 additions & 4 deletions src/gene/etl/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def update_source(
For example, to completely refresh HGNC data:
>>> from gene.schemas import SourceName
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.etl.update import update_source
>>> db = create_db()
>>> db = get_db()
>>> processed_ids = update_source(SourceName.HGNC, db)
:param source: name of source to update
Expand Down Expand Up @@ -203,9 +203,9 @@ def update_all_and_normalize(
For example, to completely refresh all Gene Normalizer data:
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.etl.update import update_all_and_normalize
>>> db = create_db()
>>> db = get_db()
>>> update_all_and_normalize(db, False)
:param db: database instance
Expand Down
4 changes: 2 additions & 2 deletions src/gene/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import FastAPI, HTTPException, Query

from gene import __version__
from gene.database import create_db
from gene.database import get_db
from gene.query import QueryHandler
from gene.schemas import (
SOURCES,
Expand All @@ -15,7 +15,7 @@
UnmergedNormalizationService,
)

db = create_db()
db = get_db()
query_handler = QueryHandler(db)

description = """
Expand Down
22 changes: 11 additions & 11 deletions src/gene/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class QueryHandler:

def __init__(self, database: AbstractDatabase) -> None:
"""Initialize QueryHandler instance. Requires a created database object to
initialize. The most straightforward way to do this is via the ``create_db``
initialize. The most straightforward way to do this is via the ``get_db``
method in the ``gene.database`` module:
>>> from gene.query import QueryHandler
>>> from gene.database import create_db
>>> q = QueryHandler(create_db())
>>> from gene.database import get_db
>>> q = QueryHandler(get_db())
We'll generally call ``create_db`` without any arguments in code examples, for
the sake of brevity. See the `usage` page in the docs and the ``create_db`` API
We'll generally call ``get_db`` without any arguments in code examples, for
the sake of brevity. See the `usage` page in the docs and the ``get_db`` API
description for more details.
:param database: storage backend to search against
Expand Down Expand Up @@ -286,8 +286,8 @@ def search(
"""Return highest match for each source.
>>> from gene.query import QueryHandler
>>> from gene.database import create_db
>>> q = QueryHandler(create_db())
>>> from gene.database import get_db
>>> q = QueryHandler(get_db())
>>> result = q.search("BRAF")
>>> result.source_matches[0].records[0].concept_id
'ncbigene:673'
Expand Down Expand Up @@ -515,8 +515,8 @@ def normalize(self, query: str) -> NormalizeService:
Use to retrieve normalized gene concept records:
>>> from gene.query import QueryHandler
>>> from gene.database import create_db
>>> q = QueryHandler(create_db())
>>> from gene.database import get_db
>>> q = QueryHandler(get_db())
>>> result = q.normalize("BRAF")
>>> result.normalized_id
'hgnc:1097'
Expand Down Expand Up @@ -671,9 +671,9 @@ def normalize_unmerged(self, query: str) -> UnmergedNormalizationService:
provided query string.
>>> from gene.query import QueryHandler
>>> from gene.database import create_db
>>> from gene.database import get_db
>>> from gene.schemas import SourceName
>>> q = QueryHandler(create_db())
>>> q = QueryHandler(get_db())
>>> response = q.normalize_unmerged("BRAF")
>>> response.match_type
<MatchType.CONCEPT_ID: 100>
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import pytest

from gene.database import AbstractDatabase, create_db
from gene.database import AbstractDatabase, get_db


@pytest.fixture(scope="session")
def database() -> AbstractDatabase:
"""Create database instance."""
return create_db()
return get_db()


def pytest_addoption(parser):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_emit_warnings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Test the emit_warnings function."""
from gene.database import create_db
from gene.database import get_db
from gene.query import QueryHandler


Expand All @@ -10,7 +10,7 @@ def test_emit_warnings():
"non_breaking_space_characters": "Query contains non-breaking space characters"
}
]
db = create_db()
db = get_db()
query_handler = QueryHandler(db)

# Test emit no warnings
Expand Down

0 comments on commit 94acd5e

Please sign in to comment.