Skip to content

Commit

Permalink
Release 10.0.3 (#292)
Browse files Browse the repository at this point in the history
## What's new

* feat: allow passing size param to RDKit endpoints
(#290)
  • Loading branch information
puehringer authored May 2, 2024
2 parents 9b632f1 + 5599f4a commit fd3d9b7
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "visyn_core",
"description": "Core repository for datavisyn applications.",
"version": "10.0.2",
"version": "10.0.3",
"author": {
"name": "datavisyn GmbH",
"email": "[email protected]",
Expand Down
18 changes: 10 additions & 8 deletions visyn_core/rdkit/img_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@


@app.get("/", response_class=SvgResponse)
def draw_smiles(structure: SmilesMolecule, substructure: SmilesMolecule | None = None, align: SmilesMolecule | None = None):
return draw(structure.mol, aligned(structure.mol, align and align.mol) or substructure and substructure.mol)
def draw_smiles(
structure: SmilesMolecule, substructure: SmilesMolecule | None = None, align: SmilesMolecule | None = None, size: int = 300
):
return draw(structure.mol, size=size, substructure=aligned(structure.mol, align and align.mol) or substructure and substructure.mol)


@app.post("/")
def multiple_images(structures: set[SmilesMolecule]):
return {m: draw(m.mol) for m in structures}
def multiple_images(structures: set[SmilesMolecule], size: int = 300):
return {m: draw(m.mol, size=size) for m in structures}


@app.get("/murcko/", response_class=SvgResponse)
def draw_murcko(structure: SmilesMolecule):
def draw_murcko(structure: SmilesMolecule, size: int = 300):
"""https://www.rdkit.org/docs/GettingStartedInPython.html#murcko-decomposition"""
murcko = MurckoScaffold.GetScaffoldForMol(structure.mol)
return draw(murcko)
return draw(murcko, size=size)


@app.get("/similarity/", response_class=SvgResponse)
Expand All @@ -44,12 +46,12 @@ def draw_molecule_similarity(structure: SmilesMolecule, reference: SmilesMolecul


@app.post("/mcs/", response_class=SvgResponse)
def draw_maximum_common_substructure_molecule(structures: list[SmilesMolecule]):
def draw_maximum_common_substructure_molecule(structures: list[SmilesMolecule], size: int = 300):
unique = [m.mol for m in set(structures)]
mcs = maximum_common_substructure_query_mol(unique)
if not mcs or not isinstance(mcs, Mol):
return Response("null", status_code=HTTP_204_NO_CONTENT)
return draw(mcs)
return draw(mcs, size=size)


@app.post("/substructures/")
Expand Down
38 changes: 14 additions & 24 deletions visyn_core/rdkit/util/draw.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
from collections.abc import Callable

from rdkit.Chem import Mol # type: ignore
from rdkit.Chem.Draw import SimilarityMaps, rdMolDraw2D # type: ignore
from rdkit.Chem.Draw.rdMolDraw2D import MolDraw2DSVG # type: ignore
from rdkit.Chem.Draw.SimilarityMaps import GetSimilarityMapForFingerprint # type: ignore


def _draw_wrapper(draw_inner: Callable[[MolDraw2DSVG, ...], None]) -> Callable[..., str]: # type: ignore
"""Function wrapper for drawing
Can annotate any function that takes a drawer as first arg, ignores its return type
Passes a drawer into annotated function
Passes on args and kwargs
Returns a svg as string
"""

def inner(*args, **kwargs):
drawer = rdMolDraw2D.MolDraw2DSVG(300, 300)
_options = drawer.drawOptions()
_options.clearBackground = False

draw_inner(drawer, *args, **kwargs)
def get_drawer(size: int):
drawer = rdMolDraw2D.MolDraw2DSVG(size, size)
_options = drawer.drawOptions()
_options.clearBackground = False
return drawer

drawer.FinishDrawing()
return drawer.GetDrawingText().replace("<?xml version='1.0' encoding='iso-8859-1'?>\n", "")

return inner
def finish_drawing(drawer: MolDraw2DSVG) -> str:
drawer.FinishDrawing()
return drawer.GetDrawingText().replace("<?xml version='1.0' encoding='iso-8859-1'?>\n", "")


@_draw_wrapper
def draw(drawer: MolDraw2DSVG, structure, substructure=None):
def draw(structure, *, size: int, substructure: str | None = None):
drawer = get_drawer(size)
highlight_atoms = structure.GetSubstructMatch(substructure) if substructure else None
drawer.DrawMolecule(structure, highlightAtoms=highlight_atoms, highlightBonds=None, highlightAtomColors=None, highlightBondColors=None)
return finish_drawing(drawer)


def _similarity(m, i):
"""https://github.com/rdkit/rdkit/blob/master/rdkit/Chem/Draw/SimilarityMaps.py"""
return SimilarityMaps.GetMorganFingerprint(m, i, radius=2, fpType="bv")


@_draw_wrapper
def draw_similarity(drawer: MolDraw2DSVG, ref: Mol, probe=Mol, *_): # ignore args after probe
def draw_similarity(ref: Mol, probe=Mol, *_): # ignore args after probe
drawer = get_drawer(300)
GetSimilarityMapForFingerprint(ref, probe, fpFunction=_similarity, draw2d=drawer)
return finish_drawing(drawer)
56 changes: 56 additions & 0 deletions visyn_core/tests/rdkit/align_trimmed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion visyn_core/tests/rdkit/substructure.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions visyn_core/tests/rdkit/substructure_trimmed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 129 additions & 0 deletions visyn_core/tests/rdkit/valid_CC(=O)OC1=CC=CC=C1C(=O)O.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fd3d9b7

Please sign in to comment.