Skip to content

Commit

Permalink
Merge pull request #18 from Steinbeck-Lab/dev-kohulan
Browse files Browse the repository at this point in the history
feat: add rotation to CDKDepict and RDKit
  • Loading branch information
CS76 authored Mar 10, 2023
2 parents 6ccf663 + 9e3c29e commit 0767d0c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/modules/depict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from jpype import JClass


def getCDKDepiction(smiles: str, molSize=(512, 512)):
def getCDKDepiction(smiles: str, molSize=(512, 512), rotate=0):
"""This function takes the user input SMILES and Depicts it
using the CDK Depiction Generator.
Args:
Expand All @@ -22,6 +22,8 @@ def getCDKDepiction(smiles: str, molSize=(512, 512)):
DepictionGenerator = JClass(cdk_base + ".depict.DepictionGenerator")()
Color = JClass("java.awt.Color")
UniColor = JClass(cdk_base + ".renderer.color.UniColor")

# Generate depiction with settings
DepictionGenerator.withSize(molSize[0], molSize[1]).withAtomValues().withParam(
StandardGenerator.StrokeRatio.class_, 1.0
).withAnnotationColor(Color.BLACK).withParam(
Expand All @@ -33,6 +35,13 @@ def getCDKDepiction(smiles: str, molSize=(512, 512)):
)
getString = JClass("java.lang.String")
moleculeSDG = getCDKSDG(smiles)

# Rotate molecule
point = JClass(cdk_base + ".geometry.GeometryTools").get2DCenter(moleculeSDG)
JClass(cdk_base + ".geometry.GeometryTools").rotate(
moleculeSDG, point, (rotate * JClass("java.lang.Math").PI / 180.0)
)

mol_image = DepictionGenerator.depict(moleculeSDG)
mol_imagex = mol_image.toSvgStr(getString("px")).getBytes()

Expand All @@ -49,7 +58,7 @@ def getCDKDepiction(smiles: str, molSize=(512, 512)):
return rescaled_mol


def getRDKitDepiction(smiles, molSize=(512, 512), kekulize=True):
def getRDKitDepiction(smiles, molSize=(512, 512), rotate=0, kekulize=True):
"""This function takes the user input SMILES and Canonicalize it
using the RDKit.
Args:
Expand All @@ -68,6 +77,7 @@ def getRDKitDepiction(smiles, molSize=(512, 512), kekulize=True):
if not mc.GetNumConformers():
rdDepictor.Compute2DCoords(mc)
drawer = rdMolDraw2D.MolDraw2DSVG(molSize[0], molSize[1])
drawer.drawOptions().rotate = rotate
drawer.DrawMolecule(mc)
drawer.FinishDrawing()
svg = drawer.GetDrawingText()
Expand Down

0 comments on commit 0767d0c

Please sign in to comment.