diff --git a/api/api.py b/api/api.py index 28d29e3..2ef7f37 100644 --- a/api/api.py +++ b/api/api.py @@ -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() @@ -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), diff --git a/api/models.py b/api/models.py index 1d6e1f8..74bc666 100644 --- a/api/models.py +++ b/api/models.py @@ -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): @@ -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 @@ -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, @@ -76,6 +82,9 @@ class Item(BaseModel): class ReferenceObject(BaseModel): doi: str + title: str + date: str + journal: str class ReferenceResult(BaseModel): diff --git a/api/queries.py b/api/queries.py index 16a873d..19a5c23 100644 --- a/api/queries.py +++ b/api/queries.py @@ -6,6 +6,7 @@ from api.models import ( # ReferenceOption, Item, ReferenceItem, + ReferenceOption, StructureItem, StructureOption, TaxonItem, @@ -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: @@ -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 @@ -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 diff --git a/doc/api/schemas/_index.yaml b/doc/api/schemas/_index.yaml index 0368e22..015a20b 100644 --- a/doc/api/schemas/_index.yaml +++ b/doc/api/schemas/_index.yaml @@ -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: diff --git a/doc/api/schemas/referenceItem.yaml b/doc/api/schemas/referenceItem.yaml index 025579f..86ed260 100644 --- a/doc/api/schemas/referenceItem.yaml +++ b/doc/api/schemas/referenceItem.yaml @@ -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" diff --git a/doc/api/schemas/referenceObject.yaml b/doc/api/schemas/referenceObject.yaml index 18aab26..d62dc47 100644 --- a/doc/api/schemas/referenceObject.yaml +++ b/doc/api/schemas/referenceObject.yaml @@ -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" diff --git a/doc/api/schemas/referenceOption.yaml b/doc/api/schemas/referenceOption.yaml new file mode 100644 index 0000000..2c0288a --- /dev/null +++ b/doc/api/schemas/referenceOption.yaml @@ -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 diff --git a/model/data_model.py b/model/data_model.py index 69efe1c..d6ca4f9 100644 --- a/model/data_model.py +++ b/model/data_model.py @@ -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: @@ -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]: diff --git a/storage/models/references.py b/storage/models/references.py index 6f73523..9413a13 100644 --- a/storage/models/references.py +++ b/storage/models/references.py @@ -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})" diff --git a/tests/test_references.py b/tests/test_references.py index 64e82cf..c087498 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -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)" + )