|
| 1 | +from abc import ABC |
| 2 | +from typing import Any, Type, overload |
| 3 | + |
| 4 | +from pydantic import BaseModel |
| 5 | + |
| 6 | +from deepsearch.chemistry.models import ChemistryCompound, ChemistryDocument |
| 7 | +from deepsearch.chemistry.resources import KnowledgeDbResource |
| 8 | +from deepsearch.cps.client import api |
| 9 | +from deepsearch.cps.client.queries.query import Query |
| 10 | + |
| 11 | + |
| 12 | +class ChemistryQuery(BaseModel, ABC): |
| 13 | + _result_type: Type |
| 14 | + |
| 15 | + |
| 16 | +class CompoundsQuery(ChemistryQuery): |
| 17 | + _result_type = ChemistryCompound |
| 18 | + |
| 19 | + |
| 20 | +class DocumentsQuery(ChemistryQuery): |
| 21 | + _result_type = ChemistryDocument |
| 22 | + |
| 23 | + |
| 24 | +class CompoundsByIds(CompoundsQuery): |
| 25 | + """Query compounds that have any of the given identifiers.""" |
| 26 | + |
| 27 | + persistent_ids: list[str] = [] |
| 28 | + |
| 29 | + |
| 30 | +class CompoundsBySmiles(CompoundsQuery): |
| 31 | + """Query compounds that (exactly) match the given SMILES code.""" |
| 32 | + |
| 33 | + structure: str |
| 34 | + |
| 35 | + |
| 36 | +class CompoundsBySmarts(CompoundsQuery): |
| 37 | + """Query compounds that (exactly) match the given SMARTS code.""" |
| 38 | + |
| 39 | + structure: str |
| 40 | + |
| 41 | + |
| 42 | +class CompoundsBySimilarity(CompoundsQuery): |
| 43 | + """Query compounds that are similar to the given SMILES code.""" |
| 44 | + |
| 45 | + structure: str |
| 46 | + threshold: float = 0.9 |
| 47 | + |
| 48 | + |
| 49 | +class CompoundsBySubstructure(CompoundsQuery): |
| 50 | + """Query compounds that contain a substructure with the given SMILES code.""" |
| 51 | + |
| 52 | + structure: str |
| 53 | + |
| 54 | + |
| 55 | +class CompoundsIn(CompoundsQuery): |
| 56 | + """Query compounds that occur in the given documents.""" |
| 57 | + |
| 58 | + documents: DocumentsQuery |
| 59 | + |
| 60 | + |
| 61 | +class DocumentsByIds(DocumentsQuery): |
| 62 | + """Query documents that have any of the given identifiers.""" |
| 63 | + |
| 64 | + publication_ids: list[str] = [] |
| 65 | + application_ids: list[str] = [] |
| 66 | + persistent_ids: list[str] = [] |
| 67 | + |
| 68 | + |
| 69 | +class DocumentsHaving(DocumentsQuery): |
| 70 | + """Query documents that contain compounds matching the given query.""" |
| 71 | + |
| 72 | + compounds: CompoundsQuery |
| 73 | + |
| 74 | + |
| 75 | +@overload |
| 76 | +def query_chemistry( |
| 77 | + api: api.CpsApi, query: CompoundsQuery, offset: int = 0, limit: int = 10 |
| 78 | +) -> list[ChemistryCompound]: ... |
| 79 | + |
| 80 | + |
| 81 | +@overload |
| 82 | +def query_chemistry( |
| 83 | + api: api.CpsApi, query: DocumentsQuery, offset: int = 0, limit: int = 10 |
| 84 | +) -> list[ChemistryDocument]: ... |
| 85 | + |
| 86 | + |
| 87 | +def query_chemistry( |
| 88 | + api: api.CpsApi, query: ChemistryQuery, offset: int = 0, limit: int = 10 |
| 89 | +) -> list[Any]: |
| 90 | + """Perform a chemistry query on the knowledge base.""" |
| 91 | + |
| 92 | + # Resolve knowledge lookup functions and arguments. |
| 93 | + function_names = { |
| 94 | + CompoundsByIds: "compounds", |
| 95 | + CompoundsBySmiles: "compounds_by_smiles", |
| 96 | + CompoundsBySmarts: "compounds_by_smarts", |
| 97 | + CompoundsBySimilarity: "compounds_by_similarity", |
| 98 | + CompoundsBySubstructure: "compounds_by_substructure", |
| 99 | + CompoundsIn: "compounds_in_documents", |
| 100 | + DocumentsByIds: "documents", |
| 101 | + DocumentsHaving: "documents_having_compounds", |
| 102 | + } |
| 103 | + |
| 104 | + query_parts: list[ChemistryQuery] = [query] |
| 105 | + |
| 106 | + if type(query) is CompoundsIn: |
| 107 | + query_parts.append(query.documents) |
| 108 | + elif type(query) is DocumentsHaving: |
| 109 | + query_parts.append(query.compounds) |
| 110 | + |
| 111 | + function_parts = [function_names[type(q)] for q in query_parts] |
| 112 | + arguments = query_parts[-1].model_dump() |
| 113 | + |
| 114 | + # Compose query task. |
| 115 | + query_tasks = Query() |
| 116 | + |
| 117 | + lookup = query_tasks.add( |
| 118 | + "KnowledgeLookup", |
| 119 | + task_id="lookup", |
| 120 | + parameters={ |
| 121 | + "schema": "patcid", |
| 122 | + "function": function_parts, |
| 123 | + "arguments": arguments, |
| 124 | + "offset": offset, |
| 125 | + "limit": limit, |
| 126 | + }, |
| 127 | + coordinates=KnowledgeDbResource(), |
| 128 | + ) |
| 129 | + lookup.output("result").output_as("result") |
| 130 | + |
| 131 | + # Run task. |
| 132 | + response = api.queries.run(query_tasks) |
| 133 | + |
| 134 | + return [ |
| 135 | + query_parts[0]._result_type.model_validate(item) |
| 136 | + for item in response.outputs["result"] |
| 137 | + ] |
0 commit comments