Skip to content

Commit

Permalink
WIP #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Feb 29, 2024
1 parent dd6acf1 commit 724b9bb
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 42 deletions.
7 changes: 5 additions & 2 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ async def search_triplets(
return TripletResult(
triplets=triplets,
references={
wid: ReferenceObject(doi=value)
# TODO
wid: ReferenceObject(doi=value, title="None", date="None", journal="None")
for wid, value in dm.get_dict_of_rid_to_reference_doi(
[reference_id for reference_id, _, _ in triplets]
).items()
Expand Down Expand Up @@ -168,7 +169,9 @@ async def search_references(
return ReferenceResult(
ids=dict_items.keys(),
objects={
rid: ReferenceObject(doi=value) for rid, value in dict_items.items()
# TODO
rid: ReferenceObject(doi=value, title="None", date="None", journal="None")
for rid, value in dict_items.items()
},
description="References matching the query",
count=len(dict_items),
Expand Down
21 changes: 15 additions & 6 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from pydantic import BaseModel

# class ReferenceOption(BaseModel):
# TODO
class ReferenceOption(BaseModel):
date_min: Optional[str] = None
date_max: Optional[str] = None
journal: Optional[str] = None


class StructureOption(BaseModel):
Expand All @@ -18,7 +20,8 @@ class TaxonOption(BaseModel):
class ReferenceItem(BaseModel):
wid: Optional[int] = None
doi: Optional[str] = None
# option: ReferenceOption = ReferenceOption()
title: Optional[str] = None
option: ReferenceOption = ReferenceOption()
# limit: Optional[int] = None


Expand Down Expand Up @@ -49,9 +52,12 @@ class Item(BaseModel):
"reference": {
"wid": 44488598,
"doi": "10.1080/1057563021000040466",
# "option": {
# "TODO": True,
# },
"title": "Iridoids from Seeds of Gentiana Lutea",
"option": {
"date_min": "1999-12-31",
"date_max": "2024-01-01",
"journal": "Natural Product Research",
},
},
"structure": {
"wid": 27151406,
Expand All @@ -76,6 +82,9 @@ class Item(BaseModel):

class ReferenceObject(BaseModel):
doi: str
title: str
date: str
journal: str


class ReferenceResult(BaseModel):
Expand Down
37 changes: 26 additions & 11 deletions api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from api.models import ( # ReferenceOption,
Item,
ReferenceItem,
ReferenceOption,
StructureItem,
StructureOption,
TaxonItem,
Expand All @@ -20,21 +21,35 @@

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

wid = item.reference.wid
doi = item.reference.doi
title = item.reference.title
date_min = item.reference.option.date_min
date_max = item.reference.option.date_max
journal = item.reference.option.journal

if doi and wid:
if len([param for param in [wid, doi, title] if param is not None]) >= 2:
raise HTTPException(
status_code=500,
detail=f"You cannot provide both 'doi' and 'wid'",
detail=f"Only one of ['wid', 'doi', 'title'] should be provided",
)
elif wid is not None or doi is not None:
# This needs to be explained in the API doc
elif wid is not None or doi is not None or title is not None:
if wid:
return dm.get_reference_with_id(wid)
return dm.get_references_with_doi(doi)

return None
elif doi:
return dm.get_references_with_doi(doi)
# else:
# return dm.get_references_with_title(title)
# if date_min:
# TODO
# if date_max:
# TODO
# if journal:
# TODO
else:
return references


def structures_from_structure_in_item(dm: DataModel, item: Item) -> set[int] | None:
Expand All @@ -46,10 +61,10 @@ def structures_from_structure_in_item(dm: DataModel, item: Item) -> set[int] | N
sub = item.structure.option.substructure_search
sim = item.structure.option.similarity_level

if molecule and wid:
if len([param for param in [wid, molecule] if param is not None]) >= 2:
raise HTTPException(
status_code=500,
detail=f"You cannot provide both 'molecule' and 'wid'",
detail=f"Only one of ['wid', 'molecule'] should be provided",
)
elif molecule is not None or wid is not None:
# This needs to be explained in the API doc
Expand Down Expand Up @@ -88,10 +103,10 @@ def taxa_from_taxon_in_item(dm: DataModel, item: Item) -> set[int] | None:
name = item.taxon.name
children = item.taxon.option.taxon_children

if name and wid:
if len([param for param in [wid, name] if param is not None]) >= 2:
raise HTTPException(
status_code=500,
detail=f"You cannot provide both 'name' and 'wid'",
detail=f"Only one of ['wid', 'name'] should be provided",
)
if wid is not None or name is not None:
# This needs to be explained in the API doc
Expand Down
4 changes: 2 additions & 2 deletions doc/api/schemas/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ referenceItem:
$ref: "./referenceItem.yaml"
referenceObject:
$ref: "./referenceObject.yaml"
# referenceOption:
# $ref: "./referenceOption.yaml"
referenceOption:
$ref: "./referenceOption.yaml"
referenceResult:
$ref: "./referenceResult.yaml"
structureId:
Expand Down
10 changes: 8 additions & 2 deletions doc/api/schemas/referenceItem.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ properties:
default: ~
example: "10.1080/1057563021000040466"
type: string
title:
description: |
The title of the reference.
default: ~
example: "Iridoids from Seeds of Gentiana Lutea"
type: string
# external_id:
# $ref: "./referenceId.yaml"
# option:
# $ref: "./referenceOption.yaml"
option:
$ref: "./referenceOption.yaml"
# filter:
# $ref: "./referenceFilter.yaml"
20 changes: 15 additions & 5 deletions doc/api/schemas/referenceObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ properties:
DOI
example: "10.1080/1057563021000040466"
type: string
title:
description: |
The title of the reference.
example: "Iridoids from Seeds of Gentiana Lutea"
type: string
date:
description: |
The date of the reference.
example: "2003-08"
type: string
journal:
description: |
The journal of the reference.
example: "Natural Product Research"
type: string
# external_ids:
# $ref: "./referenceId.yaml"
# title:
# description: |
# Title
# type: string
# example: "Iridoids from Seeds of Gentiana Lutea"
18 changes: 18 additions & 0 deletions doc/api/schemas/referenceOption.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
title: TaxonOption
type: object
properties:
date_min:
description: |
The oldest allowed date of the reference.
example: "1999-12-31"
type: string
date_max:
description: |
The most recent allowed date of the reference.
example: "2024-01-01"
type: string
journal:
description: |
The journal of the reference.
example: "Natural Product Research"
type: string
14 changes: 11 additions & 3 deletions model/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@ def get_reference_with_id(self, rid: int) -> set[int]:

def get_dict_of_rid_to_reference_doi(self, rid: Iterable[int]) -> dict[int, str]:
with self.storage.session() as session:
result = session.query(References.id, References.doi).filter(
References.id.in_(rid)
)
result = session.query(
References.id,
References.doi,
References.title,
References.date,
References.journal,
).filter(References.id.in_(rid))
return {row[0]: row[1] for row in result}

def get_reference_doi_from_rid(self, rid: int) -> str | None:
Expand All @@ -222,6 +226,10 @@ def get_references_with_doi(self, doi: str) -> set[int]:
)
return {row[0] for row in result}

# TODO ref from title
# TODO ref from date
# TODO ref from journal

### Mixonomy
# Todo, we probably want to still return that as a set
def get_structures_of_taxon(self, tid: int, recursive: bool = True) -> set[int]:
Expand Down
16 changes: 7 additions & 9 deletions storage/models/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ class References(Base):

id: Mapped[int] = mapped_column(primary_key=True)
doi: Mapped[str]
# TODO uncomment when ready
# title: Mapped[str]
# date: Mapped[str]
# journal: Mapped[int]
title: Mapped[str]
date: Mapped[str]
journal: Mapped[int]

__table_args__ = (
Index("reference_doi", "doi"),
Index("reference_id", "id"),
# Index("reference_title", "title"),
# Index("reference_date", "date"),
# Index("reference_journal", "journal"),
Index("reference_title", "title"),
Index("reference_date", "date"),
Index("reference_journal", "journal"),
)

def __repr__(self):
return f"References(id={self.id}, doi={self.doi})"
# return f"References(id={self.id}, doi={self.doi}, title={self.title}, date={self.date}, journal={self.journal})"
return f"References(id={self.id}, doi={self.doi}, title={self.title}, date={self.date}, journal={self.journal})"
13 changes: 11 additions & 2 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@


def test_references():
ref = References(id=1, doi="42.1/1")
assert ref.__repr__() == "References(id=1, doi=42.1/1)"
ref = References(
id=1,
doi="42.1/1",
title="Iridoids from Seeds of Gentiana Lutea",
date="2003-08",
journal="Natural Product Research",
)
assert (
ref.__repr__()
== "References(id=1, doi=42.1/1, title=Iridoids from Seeds of Gentiana Lutea, date=2003-08, journal=Natural Product Research)"
)

0 comments on commit 724b9bb

Please sign in to comment.