Skip to content

Commit

Permalink
Merge pull request #70 from TheJacksonLaboratory/G3-251-geneset-value…
Browse files Browse the repository at this point in the history
…-w-gene-id-type

G3-251 geneset value enpoint with gene id type
  • Loading branch information
francastell authored May 29, 2024
2 parents dd75c65 + 17ff4da commit 88d4db2
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 102 deletions.
167 changes: 90 additions & 77 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.6.0a3"
version = "0.6.0a4"
description = "The Geneweaver API"
authors = [
"Alexander Berger <[email protected]>",
Expand Down
5 changes: 4 additions & 1 deletion src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ def get_geneset_values(
],
user: UserInternal = Security(deps.full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
gene_id_type: Optional[GeneIdentifier] = None,
) -> GeneValueReturn:
"""Get geneset gene values by geneset ID."""
response = genset_service.get_geneset_gene_values(cursor, geneset_id, user)
response = genset_service.get_geneset_gene_values(
cursor=cursor, geneset_id=geneset_id, user=user, gene_id_type=gene_id_type
)

if "error" in response:
if response.get("message") == api_message.ACCESS_FORBIDDEN:
Expand Down
73 changes: 53 additions & 20 deletions src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,39 @@ def get_geneset(cursor: Cursor, geneset_id: int, user: User) -> dict:
raise err


def get_geneset_gene_values(cursor: Cursor, geneset_id: int, user: User) -> dict:
def get_geneset_gene_values(
cursor: Cursor, geneset_id: int, user: User, gene_id_type: GeneIdentifier = None
) -> dict:
"""Get a gene values for a given geneset ID.
@param cursor: DB cursor
@param geneset_id: geneset identifier
@param user: GW user
@param gene_id_type: gene identifier type object
@return: dictionary response (geneset and genset values).
"""
try:
if user is None or user.id is None:
return {"error": True, "message": message.ACCESS_FORBIDDEN}

geneset_values = db_geneset_value.by_geneset_id(cursor, geneset_id)
# If gene id type is given, check gene species homology to
# construct proper gene species mapping
if gene_id_type is not None:
results = db_geneset.get(
cursor,
gs_id=geneset_id,
with_publication_info=False,
)
if len(results) <= 0:
return {"data": None}

geneset = results[0]
geneset_values = get_gsv_w_gene_homology_update(
cursor=cursor, geneset=geneset, gene_id_type=gene_id_type
)
else:
geneset_values = db_geneset_value.by_geneset_id(cursor, geneset_id)

if geneset_values is None or len(geneset_values) <= 0:
return {"data": None}

Expand Down Expand Up @@ -247,26 +267,10 @@ def get_geneset_w_gene_id_type(
with_publication_info=False,
)
geneset = results[0]

mapping_across_species = False
original_gene_id_type = gene_id_type
genedb_sp_id = db_gene.gene_database_by_id(cursor, gene_id_type)[0]["sp_id"]

if genedb_sp_id != 0 and geneset["species_id"] != genedb_sp_id:
mapping_across_species = True
gene_id_type = GeneIdentifier(7)

geneset_values = db_geneset_value.by_geneset_id(
cursor, geneset_id, gene_id_type
geneset_values = get_gsv_w_gene_homology_update(
cursor=cursor, geneset=geneset, gene_id_type=gene_id_type
)

if mapping_across_species:
geneset_values = map_geneset_homology(
cursor, geneset_values, original_gene_id_type
)

gene_id_type = original_gene_id_type

return {
"gene_identifier_type": gene_id_type.name,
"geneset": geneset,
Expand All @@ -278,6 +282,35 @@ def get_geneset_w_gene_id_type(
raise err


def get_gsv_w_gene_homology_update(
cursor: Cursor, geneset: dict, gene_id_type: GeneIdentifier
) -> Iterable[dict]:
"""Check gene homology mapping and update it.
@param cursor: DB cursor
@param gene_id_type: geneset identifier
@return: geneset value
"""
mapping_across_species = False
original_gene_id_type = gene_id_type
genedb_sp_id = db_gene.gene_database_by_id(cursor, gene_id_type)[0]["sp_id"]

if genedb_sp_id != 0 and geneset["species_id"] != genedb_sp_id:
mapping_across_species = True
gene_id_type = GeneIdentifier(7)

geneset_values = db_geneset_value.by_geneset_id(
cursor, geneset.get("id"), gene_id_type
)

if mapping_across_species:
geneset_values = map_geneset_homology(
cursor, geneset_values, original_gene_id_type
)

return geneset_values


def map_geneset_homology(
cursor: Cursor, geneset_value: Iterable[dict], gene_id_type: GeneIdentifier
) -> Iterable[dict]:
Expand Down
10 changes: 10 additions & 0 deletions tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ def test_get_geneset_gene_values_errors(mock_get_geneset_gene_values, client):
assert response.status_code == 404


@patch("geneweaver.api.services.geneset.get_geneset_gene_values")
def test_get_geneset_gene_values_w_gs_id_type(mock_get_geneset_gene_values, client):
"""Test get geneset gene values data response."""
mock_get_geneset_gene_values.return_value = geneset_genes_values_resp

response = client.get("/api/genesets/1234/values?gene_id_type=2")
assert response.status_code == 200
assert response.json() == geneset_genes_values_resp


@patch("geneweaver.api.services.geneset.update_geneset_threshold")
def test_set_geneset_endpoint_response(mock_update_geneset_threshold, client):
"""Test set geneset threshold endpoint."""
Expand Down
58 changes: 55 additions & 3 deletions tests/services/test_genset.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def test_geneset_gene_value_response(mock_db_geneset_value):
"geneset_values"
)

response = geneset.get_geneset_gene_values(None, user=mock_user, geneset_id=1234)
response = geneset.get_geneset_gene_values(
None, user=mock_user, geneset_id=1234, gene_id_type=None
)
assert response == geneset_genes_values_resp


Expand All @@ -312,13 +314,17 @@ def test_get_geneset_gene_values_db_errors(mock_db_geneset_value):
mock_db_geneset_value.by_geneset_id.side_effect = Exception("ERROR")

with pytest.raises(expected_exception=Exception):
geneset.get_geneset_gene_values(None, user=mock_user, geneset_id=1234)
geneset.get_geneset_gene_values(
None, user=mock_user, geneset_id=1234, gene_id_type=None
)


@patch("geneweaver.api.services.geneset.db_geneset_value")
def test_get_geneset_gene_values_invalid_user(mock_db_geneset_value):
"""Test invalid user."""
response = geneset.get_geneset_gene_values(None, user=None, geneset_id=1234)
response = geneset.get_geneset_gene_values(
None, user=None, geneset_id=1234, gene_id_type=None
)
assert response.get("error") is True
assert response.get("message") == message.ACCESS_FORBIDDEN

Expand Down Expand Up @@ -369,3 +375,49 @@ def test_geneset_thershold_update_errors(mock_db_threshold, mock_db_geneset):
geneset_id=1234,
geneset_score=geneset_threshold,
)


@patch("geneweaver.api.services.geneset.db_geneset")
@patch("geneweaver.api.services.geneset.db_gene")
@patch("geneweaver.api.services.geneset.db_geneset_value")
def test_geneset_gene_value_w_gene_id_type(
mock_db_geneset_value, mock_db_gene, mock_db_geneset
):
"""Test geneset gene value data response."""
mock_db_geneset.get.return_value = [geneset_by_id_resp.get("geneset")]
mock_db_gene.gene_database_by_id.return_value = [{"sp_id": 0}]
mock_db_geneset_value.by_geneset_id.return_value = geneset_by_id_resp.get(
"geneset_values"
)

response = geneset.get_geneset_gene_values(
None, user=mock_user, geneset_id=1234, gene_id_type=GeneIdentifier(2)
)
assert response == geneset_genes_values_resp


@patch("geneweaver.api.services.geneset.db_geneset")
@patch("geneweaver.api.services.geneset.db_gene")
@patch("geneweaver.api.services.geneset.db_geneset_value")
def test_geneset_gene_value_w_gene_id_type_none_resp(
mock_db_geneset_value, mock_db_gene, mock_db_geneset
):
"""Test geneset gene value data empty response."""
mock_db_geneset.get.return_value = [geneset_by_id_resp.get("geneset")]
mock_db_gene.gene_database_by_id.return_value = [{"sp_id": 0}]
mock_db_geneset_value.by_geneset_id.return_value = None
response = geneset.get_geneset_gene_values(
None, user=mock_user, geneset_id=1234, gene_id_type=GeneIdentifier(2)
)
assert response == {"data": None}


@patch("geneweaver.api.services.geneset.db_geneset")
def test_geneset_gene_value_w_gene_id_type_none_resp2(mock_db_geneset):
"""Test geneset gene value data empty response."""
mock_db_geneset.get.return_value = []

response = geneset.get_geneset_gene_values(
None, user=mock_user, geneset_id=1234, gene_id_type=GeneIdentifier(2)
)
assert response == {"data": None}

0 comments on commit 88d4db2

Please sign in to comment.