Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1083: Indigo Service: Indigo options support #1089

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions api/http/indigo_service/indigo_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def similarities(
beta = request.data.attributes.beta
metric = request.data.attributes.metric

service.set_indigo_options(request.data.attributes.options)

compound, *_ = service.extract_compounds(source(request))
target_pairs = targets(request)
target_compounds = service.extract_compounds(target_pairs)
Expand All @@ -123,6 +125,8 @@ def similarities(
response_model_exclude_unset=True,
)
def exact_match(request: jsonapi.MatchRequest) -> jsonapi.MatchResponse:
service.set_indigo_options(request.data.attributes.options)

compound, *_ = service.extract_compounds(source(request))
target_pairs = targets(request)
target_compounds = service.extract_compounds(target_pairs)
Expand Down Expand Up @@ -152,6 +156,7 @@ def exact_match(request: jsonapi.MatchRequest) -> jsonapi.MatchResponse:
def convert(
request: jsonapi.CompoundConvertRequest,
) -> jsonapi.CompoundResponse:
service.set_indigo_options(request.data.attributes.options)
compound, *_ = service.extract_compounds(
compounds(request), request.data.attributes.compound.modifiers
)
Expand All @@ -166,6 +171,7 @@ def convert(
response_model_exclude_unset=True,
)
def validate(request: jsonapi.ValidationRequest) -> jsonapi.ValidationResponse:
service.set_indigo_options(request.data.attributes.options)
compound, *_ = service.extract_compounds(compounds(request))
validations = request.data.attributes.validations
results = {}
Expand All @@ -182,6 +188,7 @@ def validate(request: jsonapi.ValidationRequest) -> jsonapi.ValidationResponse:
def descriptors(
request: jsonapi.DescriptorRequest,
) -> jsonapi.DescriptorResponse:
service.set_indigo_options(request.data.attributes.options)
compound, *_ = service.extract_compounds(compounds(request))
properties = request.data.attributes.descriptors
results = {}
Expand All @@ -198,6 +205,7 @@ def descriptors(
def common_bits(
request: jsonapi.CommonBitsRequest,
) -> jsonapi.CommonBitsResponse:
service.set_indigo_options(request.data.attributes.options)
compound, *_ = service.extract_compounds(source(request))
target_compounds = service.extract_compounds(targets(request))
source_fp = compound.fingerprint("sim")
Expand All @@ -212,20 +220,23 @@ def common_bits(
def render(
request: jsonapi.RenderRequest,
) -> jsonapi.RenderResponse:
compound, *_ = service.extract_compounds(compounds(request))
output_format = request.data.attributes.outputFormat
indigo_renderer = IndigoRenderer(indigo())

output_format = request.data.attributes.outputFormat
options = request.data.attributes.options
if options:
output_format_option = options.get("render-output-format")
if output_format_option and output_format_option not in output_format:
raise HTTPException(
status_code=400, detail="Choose only one output format"
)

indigo().setOption(
"render-output-format", jsonapi.rendering_formats.get(output_format)
)
options = request.data.attributes.options
if options:
for option, value in options.items():
if option == "render-output-format":
raise HTTPException(
status_code=400, detail="Choose only one output format"
)
indigo().setOption(option, value)
service.set_indigo_options(options)

compound, *_ = service.extract_compounds(compounds(request))
raw_image = indigo_renderer.renderToBuffer(compound)
return jsonapi.make_render_response(raw_image, output_format)

Expand Down
1 change: 0 additions & 1 deletion api/http/indigo_service/indigo_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def indigo(thread_local: bool = False) -> Indigo:
if __cur_thread.get() != ident:
__indigo.set(Indigo())
__cur_thread.set(ident)

return __indigo.get()


Expand Down
11 changes: 9 additions & 2 deletions api/http/indigo_service/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import base64
import functools
from enum import Enum
from typing import Dict, Generic, List, Optional, TypeVar, Union
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union

from fastapi import HTTPException
from pydantic import BaseModel, conlist, validator
Expand Down Expand Up @@ -147,6 +147,7 @@ class CompoundObjectWithModifiers(CompoundObject):
class CompoundModel(GenericModel, Generic[OutputFormatT]):
compound: CompoundObjectWithModifiers
outputFormat: OutputFormatT
options: Optional[Dict[str, Any]] = None


class CompoundArrayModel(GenericModel, Generic[OutputFormatT]):
Expand All @@ -164,6 +165,7 @@ class CompoundPair(GenericModel, Generic[OutputFormatT]):
compounds: conlist(CompoundObject, min_items=2, max_items=2) # type: ignore # pylint: disable=line-too-long
# fmt: on
outputFormat: Optional[OutputFormatT]
options: Optional[Dict[str, Any]] = None


CompoundRequest = Request[CompoundModelType, CompoundModel[CompoundFormat]]
Expand Down Expand Up @@ -265,6 +267,7 @@ class MatchModel(BaseModel):
targets: List[CompoundObject]
outputFormat: MatchOutputFormat
flag: Optional[str] = "ALL"
options: Optional[Dict[str, Any]] = None


MapAtomResponse = Response[MapAtomModelType, MapModel[MapAtomModel]]
Expand Down Expand Up @@ -328,6 +331,7 @@ class CommonBitsModelType(BaseModel):
class CommonBitsModel(BaseModel):
source: CompoundObject
targets: List[CompoundObject]
options: Optional[Dict[str, Any]] = None


class CommonBitsCountModel(BaseModel):
Expand Down Expand Up @@ -397,6 +401,7 @@ class SimilaritiesModel(BaseModel):
metric: SimilarityMetric
alpha: Optional[float] = 0.5
beta: Optional[float] = 0.5
options: Optional[Dict[str, Any]] = None

@validator("alpha", "beta")
def tversky_factor(
Expand Down Expand Up @@ -455,6 +460,7 @@ class ValidationResultsModelType(BaseModel):
class ValidationModel(BaseModel):
compound: CompoundObject
validations: List[Validations]
options: Optional[Dict[str, Any]] = None


class ValidationResultsModel(BaseModel):
Expand Down Expand Up @@ -527,6 +533,7 @@ class DescriptorModelType(BaseModel):
class DescriptorModel(BaseModel):
compound: CompoundObject
descriptors: List[Descriptors]
options: Optional[Dict[str, Any]] = None


class DescriptorResultModelType(BaseModel):
Expand Down Expand Up @@ -598,7 +605,7 @@ def make_descriptor_response(
class RenderModel(BaseModel):
compound: CompoundObject
outputFormat: str
options: Optional[Dict[str, Union[int, float, bool, str]]] = None
options: Optional[Dict[str, Any]] = None


class RenderModelType(BaseModel):
Expand Down
10 changes: 9 additions & 1 deletion api/http/indigo_service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# limitations under the License.
#

from typing import List, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

from indigo import IndigoObject
from indigo.inchi import IndigoInchi
Expand All @@ -25,6 +25,14 @@
from indigo_service.indigo_tools import indigo


def set_indigo_options(
options: Optional[Dict[str, Union[str, int, float, bool]]]
) -> None:
if options:
for option, value in options.items():
indigo().setOption(option, value)


def extract_compounds(
pairs: List[Tuple[str, jsonapi.CompoundFormat]],
modifiers: Optional[List[jsonapi.CompoundModifiers]] = None,
Expand Down
Loading