Skip to content

Commit

Permalink
Merge pull request #89 from TheJacksonLaboratory/G3-325-api-genset-en…
Browse files Browse the repository at this point in the history
…dpoint-update

G3 325 api genset endpoint update
  • Loading branch information
francastell authored Aug 8, 2024
2 parents 86628bd + 06df4fe commit 3e93d98
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 298 deletions.
642 changes: 352 additions & 290 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.8.0a2"
version = "0.8.0a3"
description = "The Geneweaver API"
authors = [
"Alexander Berger <alexander.berger@jax.org>",
Expand All @@ -20,7 +20,7 @@ python = "^3.9"
geneweaver-core = "^0.10.0a3"
fastapi = {extras = ["all"], version = "^0.111.0"}
uvicorn = {extras = ["standard"], version = "^0.30.0"}
geneweaver-db = "0.5.0a15"
geneweaver-db = "0.5.0a16"
psycopg-pool = "^3.1.7"
requests = "^2.32.3"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
Expand Down
37 changes: 34 additions & 3 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Endpoints related to genesets."""

import json
import time
from datetime import date, datetime
from tempfile import TemporaryDirectory
from typing import Optional, Set

Expand All @@ -13,7 +13,7 @@
from geneweaver.api.services import geneset as genset_service
from geneweaver.api.services import publications as publication_service
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.score import GenesetScoreType
from geneweaver.core.schema.score import GenesetScoreType, ScoreType
from typing_extensions import Annotated

from . import message as api_message
Expand Down Expand Up @@ -69,6 +69,29 @@ def get_visible_genesets(
with_publication_info: Annotated[
bool, Query(description=api_message.ONLY_MY_GS)
] = True,
score_type: Optional[ScoreType] = None,
size_less_than: Annotated[
Optional[int],
Query(
format="int64",
minimum=0,
maxiumum=9223372036854775807,
description=api_message.GENESET_SIZE,
),
] = None,
size_greater_than: Annotated[
Optional[int],
Query(
format="int64",
minimum=0,
maxiumum=9223372036854775807,
description=api_message.GENESET_SIZE,
),
] = None,
created_after: Annotated[date, Query(description=api_message.CREATE_DATE)] = None,
created_before: Annotated[date, Query(description=api_message.CREATE_DATE)] = None,
updated_after: Annotated[date, Query(description=api_message.UPDATE_DATE)] = None,
updated_before: Annotated[date, Query(description=api_message.UPDATE_DATE)] = None,
limit: Annotated[
Optional[int],
Query(
Expand Down Expand Up @@ -104,6 +127,13 @@ def get_visible_genesets(
with_publication_info=with_publication_info,
ontology_term=ontology_term,
only_my_genesets=only_my_genesets,
score_type=score_type,
lte_count=size_less_than,
gte_count=size_greater_than,
created_after=created_after,
created_before=created_before,
updated_after=updated_after,
updated_before=updated_before,
limit=limit,
offset=offset,
)
Expand Down Expand Up @@ -190,7 +220,8 @@ def get_export_geneset_by_id_type(
gene_id_type: Optional[GeneIdentifier] = None,
) -> StreamingResponse:
"""Export geneset into JSON file. Search by ID and optional gene identifier type."""
timestr = time.strftime("%Y%m%d-%H%M%S")
current_datetime = datetime.now()
timestr = current_datetime.strftime("%Y%m%d-%H%M%S")

# Validate gene identifier type
if gene_id_type:
Expand Down
3 changes: 3 additions & 0 deletions src/geneweaver/api/controller/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
)
CHECK_DB_HEALTH = "Check DB health flag"
ONTOLOGY_ID = "Ontology term reference ID"
CREATE_DATE = "Create date limit (before or after). E.g. 2024-08-01"
UPDATE_DATE = "Update date limit (before or after). E.g. 2023-07-01"
GENESET_SIZE = "Geneset size (Genes count)"
26 changes: 24 additions & 2 deletions src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Service functions for dealing with genesets."""

from datetime import date
from typing import Iterable, Optional, Set

from fastapi.logger import logger
from geneweaver.api.controller import message
from geneweaver.api.core.exceptions import UnauthorizedException
from geneweaver.api.schemas.auth import AppRoles, User
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.score import GenesetScoreType
from geneweaver.core.schema.score import GenesetScoreType, ScoreType
from geneweaver.db import gene as db_gene
from geneweaver.db import geneset as db_geneset
from geneweaver.db import geneset_value as db_geneset_value
Expand Down Expand Up @@ -90,9 +91,16 @@ def get_visible_genesets(
gene_id_type: Optional[GeneIdentifier] = None,
search_text: Optional[str] = None,
ontology_term: Optional[str] = None,
with_publication_info: bool = True,
score_type: Optional[ScoreType] = None,
lte_count: Optional[int] = None,
gte_count: Optional[int] = None,
created_after: Optional[date] = None,
created_before: Optional[date] = None,
updated_after: Optional[date] = None,
updated_before: Optional[date] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
with_publication_info: bool = True,
) -> dict:
"""Get genesets from the database.
Expand All @@ -110,6 +118,13 @@ def get_visible_genesets(
:param search_text: Return genesets that match this search text (using PostgreSQL
full-text search).
:param ontology_term: Show only results associated with this ontology term.
:param score_type: Show only results with given score type.
:param lte_count: less than or equal geneset count.
:param gte_count: greater than or equal geneset count.
:param updated_before: Show only results updated before this date.
:param updated_after: Show only results updated after this date.
:param created_before: Show only results created before this date.
:param created_after: Show only results updated before this date.
:param limit: Limit the number of results.
:param offset: Offset the results.
:param with_publication_info: Include publication info in the return.
Expand All @@ -134,6 +149,13 @@ def get_visible_genesets(
search_text=search_text,
with_publication_info=with_publication_info,
ontology_term=ontology_term,
score_type=score_type,
lte_count=lte_count,
gte_count=gte_count,
created_after=created_after,
created_before=created_before,
updated_after=updated_after,
updated_before=updated_before,
limit=limit,
offset=offset,
)
Expand Down
69 changes: 69 additions & 0 deletions tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from unittest.mock import patch

import pytest
from geneweaver.api.controller import message

from tests.data import test_geneset_data, test_ontology_data, test_publication_data
Expand Down Expand Up @@ -398,3 +399,71 @@ def test_delete_geneset_ontology_terms_errors(mock_delete_genenset_onto_terms, c
}
response = client.delete("/api/genesets/1234/ontologies/D001921")
assert response.status_code == 404


@pytest.mark.parametrize("score_type", ["1", "binary"])
@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_score_type(mock_get_visible_genesets, score_type, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")

response = client.get("/api/genesets?score_type=" + score_type)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_create_date(mock_get_visible_genesets, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")

response = client.get(
"/api/genesets?created_after=2023-08-01&created_before=2024-07-01"
)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_update_date(mock_get_visible_genesets, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")

response = client.get(
"/api/genesets?updated_after=2023-08-01&updated_before=2024-07-01"
)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")


@pytest.mark.parametrize("score_type", ["2342", "test"])
def test_invalid_score_type(score_type, client):
"""Test general get geneset data no parameters -- default limit."""
response = client.get("/api/genesets?score_type=" + score_type)
assert response.status_code == 422


@pytest.mark.parametrize("created_before", ["20-23-20", "08-01-2023", "80/01/2022"])
@pytest.mark.parametrize("created_after", ["20-23-20", "08-01-2023", "80/01/2022"])
def test_invalid_create_date_params(created_before, created_after, client):
"""Test general get geneset data no parameters -- default limit."""
response = client.get(
"/api/genesets?created_before="
+ created_before
+ ",created_after="
+ created_after
)
assert response.status_code == 422


@pytest.mark.parametrize("updated_before", ["20-23-20", "08-01-2023", "80/01/2022"])
@pytest.mark.parametrize("updated_after", ["20-23-20", "08-01-2023", "80/01/2022"])
def test_invalid_update_date_params(updated_before, updated_after, client):
"""Test general get geneset data no parameters -- default limit."""
response = client.get(
"/api/genesets?created_before="
+ updated_before
+ ",created_after="
+ updated_after
)
assert response.status_code == 422
10 changes: 9 additions & 1 deletion tests/services/test_genset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for geneset Service."""

import datetime
from unittest.mock import patch

import pytest
Expand All @@ -8,7 +9,7 @@
from geneweaver.api.schemas.auth import AppRoles, User
from geneweaver.api.services import geneset
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.score import GenesetScoreType
from geneweaver.core.schema.score import GenesetScoreType, ScoreType

from tests.data import test_geneset_data

Expand Down Expand Up @@ -258,7 +259,14 @@ def test_visible_geneset_all_expected_parameters(mock_db_geneset):
abbreviation="test",
publication_id=123,
pubmed_id="p123",
score_type=ScoreType("p-value"),
lte_count=50,
gte_count=5,
gene_id_type=GeneIdentifier(5),
created_before=datetime.datetime(2024, 7, 31),
created_after=datetime.datetime(2008, 7, 31),
updated_before=datetime.datetime(2024, 7, 31),
updated_after=datetime.datetime(2023, 7, 31),
limit=10,
offset=0,
with_publication_info=True,
Expand Down

0 comments on commit 3e93d98

Please sign in to comment.