Skip to content

Commit

Permalink
Updating enum usage for consistency with geneweaver-core updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bergsalex committed Jan 31, 2024
1 parent f8022d2 commit 15af712
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 68 deletions.
61 changes: 13 additions & 48 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
from tempfile import TemporaryDirectory
from typing import Optional

from fastapi import APIRouter, Depends, HTTPException, Query, Security
from fastapi import APIRouter, Depends, HTTPException, Path, Security
from fastapi.responses import FileResponse
from geneweaver.api import dependencies as deps
from geneweaver.api.schemas.apimodels import gene_id_type_options
from geneweaver.api.schemas.auth import UserInternal
from geneweaver.api.services import geneset as genset_service
from geneweaver.core.enum import GeneIdentifier
from geneweaver.core.schema.geneset import GenesetUpload
from geneweaver.db import geneset as db_geneset
from geneweaver.db import geneset_value as db_geneset_value
from typing_extensions import Annotated

from . import message as api_message

Expand All @@ -33,20 +31,17 @@ def get_visible_genesets(

@router.get("/{geneset_id}")
def get_geneset(
geneset_id: int,
geneset_id: Annotated[
int, Path(format="int64", minimum=0, maxiumum=9223372036854775807)
],
user: UserInternal = Security(deps.full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
# using int type and adding doc options here as using GeneIdentifier
# won't produce the right docs for gene identifier options
gene_id_type: Optional[int] = Query(
None, description=f"Options: {gene_id_type_options}"
),
gene_id_type: Optional[GeneIdentifier] = None,
) -> dict:
"""Get a geneset by ID. Optional filter results by gene identifier type."""
if gene_id_type:
gene_identifier_type = get_gene_identifier_type(gene_id_type)
response = genset_service.get_geneset_w_gene_id_type(
cursor, geneset_id, user, gene_identifier_type
cursor, geneset_id, user, gene_id_type
)
else:
response = genset_service.get_geneset(cursor, geneset_id, user)
Expand All @@ -62,24 +57,21 @@ def get_geneset(

@router.get("/{geneset_id}/file", response_class=FileResponse)
def get_export_geneset_by_id_type(
geneset_id: int,
geneset_id: Annotated[
int, Path(format="int64", minimum=0, maxiumum=9223372036854775807)
],
user: UserInternal = Security(deps.full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
temp_dir: TemporaryDirectory = Depends(deps.get_temp_dir),
# using int type and adding doc options here as using
# GeneIdentifier won't produce the right docs for gene identifier options
gene_id_type: Optional[int] = Query(
None, description=f"Options: {gene_id_type_options}"
),
) -> dict:
gene_id_type: Optional[GeneIdentifier] = None,
) -> FileResponse:
"""Export geneset into JSON file. Search by ID and optional gene identifier type."""
timestr = time.strftime("%Y%m%d-%H%M%S")

# Validate gene identifier type
if gene_id_type:
gene_identifier_type = get_gene_identifier_type(gene_id_type)
response = genset_service.get_geneset_w_gene_id_type(
cursor, geneset_id, user, gene_identifier_type
cursor, geneset_id, user, gene_id_type
)
else:
response = genset_service.get_geneset(cursor, geneset_id, user)
Expand Down Expand Up @@ -107,30 +99,3 @@ def get_export_geneset_by_id_type(
media_type="application/octet-stream",
filename=geneset_filename,
)


def upload_geneset(
geneset: GenesetUpload,
user: UserInternal = Security(deps.full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> dict:
"""Upload a geneset."""
db_geneset_value.format_geneset_values_for_file_insert(geneset.gene_list)
return {"geneset_id": 0}


def get_gene_identifier_type(gene_id_type: int) -> GeneIdentifier:
"""Get a valid GeneIdentifier object. Raise HTTP exception if invalid value.
@param gene_id_type: gene identifier type
@return: GeneIdentifier obj or HTTP exception if invalid id value.
"""
try:
gene_identifier = GeneIdentifier(gene_id_type)
except ValueError as err:
raise HTTPException(
status_code=400,
detail=f"{api_message.GENE_IDENTIFIER_TYPE_VALUE_ERROR}, "
f"valid options= {gene_id_type_options}",
) from err
return gene_identifier
20 changes: 4 additions & 16 deletions src/geneweaver/api/schemas/apimodels.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
"""Models for API requests."""
from typing import Iterable, Optional

from fastapi import Query
from geneweaver.core.enum import GeneIdentifier, Species
from pydantic import BaseModel

gene_id_type_options = [f"{choice.name} ({choice.value})" for choice in GeneIdentifier]
species_type_options = [f"{choice.name} ({choice.value})" for choice in Species]


class GeneIdMappingResp(BaseModel):
"""Model for gene id mapping."""
Expand All @@ -19,15 +15,7 @@ class GeneIdMappingReq(BaseModel):
"""Model for gene id mapping request."""

source_ids: Iterable[str]
target_gene_id_type: GeneIdentifier = Query(
description=f"Options: {gene_id_type_options}"
)
source_gene_id_type: Optional[GeneIdentifier] = Query(
None, description=f"Options: {gene_id_type_options}"
)
target_species: Optional[Species] = Query(
None, description=f"Options: {species_type_options}"
)
source_species: Optional[Species] = Query(
None, description=f"Options: {species_type_options}"
)
target_gene_id_type: GeneIdentifier
source_gene_id_type: Optional[GeneIdentifier] = None
target_species: Optional[Species] = None
source_species: Optional[Species] = None
2 changes: 1 addition & 1 deletion src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_geneset_w_gene_id_type(
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["sp_id"] != genedb_sp_id:
if genedb_sp_id != 0 and geneset["species_id"] != genedb_sp_id:
mapping_across_species = True
gene_id_type = GeneIdentifier(7)

Expand Down
5 changes: 2 additions & 3 deletions tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest.mock import patch

import pytest
from geneweaver.api.controller import message

from tests.data import test_geneset_data

Expand Down Expand Up @@ -78,5 +77,5 @@ def test_invalid_gene_type_id(mock_service_get_geneset_w_gene_id_type, client):
mock_service_get_geneset_w_gene_id_type.return_value = geneset_w_gene_id_type_resp
response = client.get("/api/genesets/1234/file?gene_id_type=25")

assert message.GENE_IDENTIFIER_TYPE_VALUE_ERROR in response.json()["detail"]
assert response.status_code == 400
assert 'ctx' in response.json()["detail"][0]
assert response.status_code == 422

0 comments on commit 15af712

Please sign in to comment.