Skip to content

Commit

Permalink
tox
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Dec 28, 2023
1 parent 52a1916 commit bfb33da
Show file tree
Hide file tree
Showing 28 changed files with 261 additions and 207 deletions.
62 changes: 43 additions & 19 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from fastapi import Depends, FastAPI
from fastapi_versioning import VersionedFastAPI, version

from api.models import (Item, ReferenceInfo, ReferenceResult,
StructureInfo, StructureResult, TaxonInfo, TaxonResult, TripletResult)
from api.queries import (combine_and_filter_outputs, get_matching_references_from_reference_in_item,
from api.models import (Item, ReferenceInfo, ReferenceResult, StructureInfo,
StructureResult, TaxonInfo, TaxonResult, TripletResult)
from api.queries import (combine_and_filter_outputs,
get_matching_references_from_reference_in_item,
get_matching_references_from_structure_in_item,
get_matching_references_from_taxon_in_item,
get_matching_structures_from_reference_in_item,
Expand Down Expand Up @@ -55,14 +56,16 @@ async def search_triplets(item: Item, dm: DataModel = Depends(get_dm)) -> Triple
selected_structures = get_matching_structures_from_structure_in_item(dm, item)
selected_taxa = get_matching_taxa_from_taxon_in_item(dm, item)

triplets_set = dm.get_triples_for(reference_ids=selected_references,
structure_ids=selected_structures,
taxon_ids=selected_taxa)
triplets_set = dm.get_triples_for(
reference_ids=selected_references,
structure_ids=selected_structures,
taxon_ids=selected_taxa,
)

if item.limit == 0:
triplets = list(triplets_set)
else:
triplets = list(triplets_set)[:item.limit]
triplets = list(triplets_set)[: item.limit]

return TripletResult(
triplets=triplets,
Expand Down Expand Up @@ -91,7 +94,9 @@ async def search_triplets(item: Item, dm: DataModel = Depends(get_dm)) -> Triple

@app.post("/structures")
@version(1, 0)
async def search_structures(item: Item, dm: DataModel = Depends(get_dm)) -> StructureResult:
async def search_structures(
item: Item, dm: DataModel = Depends(get_dm)
) -> StructureResult:
matching_structures_by_structure = get_matching_structures_from_structure_in_item(
dm, item
)
Expand All @@ -100,15 +105,22 @@ async def search_structures(item: Item, dm: DataModel = Depends(get_dm)) -> Stru
dm, item
)

ids = combine_and_filter_outputs([matching_structures_by_reference,
matching_structures_by_taxon,
matching_structures_by_structure], limit=item.limit)
ids = combine_and_filter_outputs(
[
matching_structures_by_reference,
matching_structures_by_taxon,
matching_structures_by_structure,
],
limit=item.limit,
)

dict_items = dm.get_dict_of_sid_to_smiles(ids)

return StructureResult(
ids=dict_items.keys(),
structures={sid: StructureInfo(smiles=value) for sid, value in dict_items.items()},
structures={
sid: StructureInfo(smiles=value) for sid, value in dict_items.items()
},
description="Structures matching the query",
count=len(dict_items),
)
Expand All @@ -121,9 +133,14 @@ async def search_taxa(item: Item, dm: DataModel = Depends(get_dm)) -> TaxonResul
matching_taxa_by_structure = get_matching_taxa_from_structure_in_item(dm, item)
matching_taxa_by_reference = get_matching_taxa_from_reference_in_item(dm, item)

ids = combine_and_filter_outputs([matching_taxa_by_reference,
matching_taxa_by_structure,
matching_taxa_by_taxon], limit=item.limit)
ids = combine_and_filter_outputs(
[
matching_taxa_by_reference,
matching_taxa_by_structure,
matching_taxa_by_taxon,
],
limit=item.limit,
)

dict_items = dm.get_dict_of_tid_to_taxon_name(ids)

Expand All @@ -137,7 +154,9 @@ async def search_taxa(item: Item, dm: DataModel = Depends(get_dm)) -> TaxonResul

@app.post("/references")
@version(1, 0)
async def search_references(item: Item, dm: DataModel = Depends(get_dm)) -> ReferenceResult:
async def search_references(
item: Item, dm: DataModel = Depends(get_dm)
) -> ReferenceResult:
matching_references_by_reference = get_matching_references_from_reference_in_item(
dm, item
)
Expand All @@ -146,9 +165,14 @@ async def search_references(item: Item, dm: DataModel = Depends(get_dm)) -> Refe
)
matching_references_by_taxon = get_matching_references_from_taxon_in_item(dm, item)

ids = combine_and_filter_outputs([matching_references_by_reference,
matching_references_by_structure,
matching_references_by_taxon], limit=item.limit)
ids = combine_and_filter_outputs(
[
matching_references_by_reference,
matching_references_by_structure,
matching_references_by_taxon,
],
limit=item.limit,
)

dict_items = dm.get_dict_of_rid_to_reference_doi(ids)

Expand Down
37 changes: 28 additions & 9 deletions api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
from api.models import Item
from model import DataModel

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def get_matching_references_from_reference_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_references_from_reference_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
"""Returns the WID of matching references."""
references = None
if item.reference_doi and item.reference_wid:
Expand All @@ -31,7 +35,9 @@ def get_matching_references_from_reference_in_item(dm: DataModel, item: Item) ->
return references


def get_matching_structures_from_structure_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_structures_from_structure_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
"""Returns the WID of matching structures."""
structures = None

Expand Down Expand Up @@ -99,7 +105,10 @@ def get_matching_taxa_from_taxon_in_item(dm: DataModel, item: Item) -> set[int]

return taxa

def get_matching_references_from_structure_in_item(dm: DataModel, item: Item) -> set[int] | None:

def get_matching_references_from_structure_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
structures = get_matching_structures_from_structure_in_item(dm, item)

if structures is None:
Expand All @@ -108,7 +117,9 @@ def get_matching_references_from_structure_in_item(dm: DataModel, item: Item) ->
return dm.get_references_of_structures(structures)


def get_matching_references_from_taxon_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_references_from_taxon_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
taxa = get_matching_taxa_from_taxon_in_item(dm, item)

if taxa is None:
Expand All @@ -117,7 +128,9 @@ def get_matching_references_from_taxon_in_item(dm: DataModel, item: Item) -> set
return dm.get_references_of_taxa(taxa)


def get_matching_structures_from_reference_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_structures_from_reference_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
references = get_matching_references_from_reference_in_item(dm, item)

if references is None:
Expand All @@ -126,7 +139,9 @@ def get_matching_structures_from_reference_in_item(dm: DataModel, item: Item) ->
return dm.get_structures_of_references(references)


def get_matching_structures_from_taxon_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_structures_from_taxon_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
taxa = get_matching_taxa_from_taxon_in_item(dm, item)

if taxa is None:
Expand All @@ -142,7 +157,9 @@ def get_matching_structures_from_taxon_in_item(dm: DataModel, item: Item) -> set
return out


def get_matching_taxa_from_structure_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_taxa_from_structure_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
structures = get_matching_structures_from_structure_in_item(dm, item)

if structures is None:
Expand All @@ -151,7 +168,9 @@ def get_matching_taxa_from_structure_in_item(dm: DataModel, item: Item) -> set[i
return dm.get_taxa_of_structures(structures)


def get_matching_taxa_from_reference_in_item(dm: DataModel, item: Item) -> set[int] | None:
def get_matching_taxa_from_reference_in_item(
dm: DataModel, item: Item
) -> set[int] | None:
references = get_matching_references_from_reference_in_item(dm, item)

if references is None:
Expand Down
16 changes: 8 additions & 8 deletions doc/api/paths/references/post.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
operationId: search_references
security: []
security: [ ]
summary: |
Search references using parameters
description: |
Expand All @@ -23,10 +23,10 @@ post:
'404':
description: No reference matching the given search could be found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: No reference matching the given search could be found
application/json:
schema:
type: object
properties:
message:
type: string
example: No reference matching the given search could be found
30 changes: 15 additions & 15 deletions doc/api/paths/structures/post.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
operationId: search_structures
security: []
security: [ ]
summary: |
Search structures using parameters
description: |
Expand All @@ -23,20 +23,20 @@ post:
'404':
description: No structure matching the given query search be found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: No structure matching the given search could be found
application/json:
schema:
type: object
properties:
message:
type: string
example: No structure matching the given search could be found
'500':
description: The structure given is invalid
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: The structure given is invalid
application/json:
schema:
type: object
properties:
message:
type: string
example: The structure given is invalid
16 changes: 8 additions & 8 deletions doc/api/paths/taxa/post.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
operationId: search_taxa
security: []
security: [ ]
summary: |
Search taxa using parameters
description: |
Expand All @@ -23,10 +23,10 @@ post:
'404':
description: No taxon matching the given search could be found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: No taxon matching the given search could be found
application/json:
schema:
type: object
properties:
message:
type: string
example: No taxon matching the given search could be found
16 changes: 8 additions & 8 deletions doc/api/paths/triplets/post.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
operationId: search_triplets
security: []
security: [ ]
summary: |
Search triplets using parameters
description: |
Expand All @@ -23,10 +23,10 @@ post:
'404':
description: No triplet matching the given search could be found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: No triplet matching the given search could be found
application/json:
schema:
type: object
properties:
message:
type: string
example: No triplet matching the given search could be found
2 changes: 1 addition & 1 deletion doc/api/schemas/item.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: Item
type: object
required: []
required: [ ]
properties:
#' References.
# TODO
Expand Down
2 changes: 1 addition & 1 deletion doc/api/schemas/references.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ title: References
description: |
References
type: object
required: []
required: [ ]
properties:
doi:
title: DOI
Expand Down
4 changes: 2 additions & 2 deletions doc/api/schemas/structureResult.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ properties:
description: |
IDs
type: array
example: [27151406]
example: [ 27151406 ]
structures:
title: Structures
description: |
Structures
type: object
properties:
ids:
ids:
type: integer
example: 27151406
structures:
Expand Down
2 changes: 1 addition & 1 deletion doc/api/schemas/taxa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ title: Taxa
description: |
Taxa
type: object
required: []
required: [ ]
properties:
name:
title: Name
Expand Down
4 changes: 2 additions & 2 deletions doc/api/schemas/taxonResult.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ properties:
description: |
IDs
type: array
example: [158572]
example: [ 158572 ]
taxa:
title: Taxa
description: |
Taxa
type: object
properties:
ids:
ids:
type: integer
example: 158572
taxa:
Expand Down
Loading

0 comments on commit bfb33da

Please sign in to comment.