Skip to content

Commit

Permalink
feat: CDK and RDKit depict functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohulan committed Mar 9, 2023
1 parent 35c9004 commit 92c269a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
24 changes: 19 additions & 5 deletions app/modules/cdkmodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,38 @@


def getCDKSDG(smiles: str):
"""This function takes the user input SMILES and Canonicalize it
using the CDK Canonicalisation algorthim.
"""This function takes the user input SMILES and Creates a
Structure Diagram Layout using the CDK.
Args:
smiles (string): SMILES string given by the user.
Returns:
mol object (string): CDK Structure Diagram Layout mol block.
mol object : mol object with CDK SDG.
"""
cdk_base = "org.openscience.cdk"
SCOB = JClass(cdk_base + ".silent.SilentChemObjectBuilder")
StringW = JClass("java.io.StringWriter")()
SmilesParser = JClass(cdk_base + ".smiles.SmilesParser")(SCOB.getInstance())
molecule = SmilesParser.parseSmiles(smiles)
StructureDiagramGenerator = JClass(cdk_base + ".layout.StructureDiagramGenerator")()
StructureDiagramGenerator.generateCoordinates(molecule)
molecule_ = StructureDiagramGenerator.getMolecule()

return molecule_


def getCDKSDGMol(smiles: str):
"""This function takes the user input SMILES and returns a mol
block as a string with Structure Diagram Layout.
Args:
smiles (string): SMILES string given by the user.
Returns:
mol object (string): CDK Structure Diagram Layout mol block.
"""
cdk_base = "org.openscience.cdk"
StringW = JClass("java.io.StringWriter")()

moleculeSDG = getCDKSDG(smiles)
SDFW = JClass(cdk_base + ".io.SDFWriter")(StringW)
SDFW.write(molecule_)
SDFW.write(moleculeSDG)
SDFW.close()
mol_str = StringW.toString()
return mol_str
64 changes: 64 additions & 0 deletions app/modules/depict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
import app.modules.cdkmodules as cdkmodules
from IPython.display import SVG


def getCDKDepiction(smiles: str, size=512.0):
"""This function takes the user input SMILES and Depicts it
using the CDK Depiction Generator.
Args:
smiles (string): SMILES string given by the user.
Returns:
imag (PIL): CDK Structure Depiction as a pillow image.
image (png): CDK Structure Depiction as a PNG image.
"""
cdk_base = "org.openscience.cdk"
StandardGenerator = cdkmodules.JClass(
cdk_base + ".renderer.generators.standard.StandardGenerator"
)
DepictionGenerator = cdkmodules.JClass(cdk_base + ".depict.DepictionGenerator")()
Color = cdkmodules.JClass("java.awt.Color")
UniColor = cdkmodules.JClass(cdk_base + ".renderer.color.UniColor")

DepictionGenerator.withSize(size, size).withAtomValues().withParam(
StandardGenerator.StrokeRatio.class_, 1.5
).withAnnotationColor(Color.BLACK).withParam(
StandardGenerator.AtomColor.class_, UniColor(Color.BLACK)
).withBackgroundColor(
Color.WHITE
).withZoom(
2.0
)

moleculeSDG = cdkmodules.getCDKSDG(smiles)
mol_image = DepictionGenerator.depict(moleculeSDG).toSvgStr("px")

return mol_image.getBytes()


def getRDKitDepiction(smiles, molSize=(512, 512), kekulize=True):
"""This function takes the user input SMILES and Canonicalize it
using the RDKit.
Args:
smiles (string): SMILES string given by the user.
Returns:
imag (PIL): CDK Structure Depiction as a pillow image.
image (png): CDK Structure Depiction as a PNG image.
"""
mol = Chem.MolFromSmiles(smiles)
mc = Chem.Mol(mol.ToBinary())
if kekulize:
try:
Chem.Kekulize(mc)
except:
mc = Chem.Mol(mol.ToBinary())
if not mc.GetNumConformers():
rdDepictor.Compute2DCoords(mc)
drawer = rdMolDraw2D.MolDraw2DSVG(molSize[0], molSize[1])
drawer.DrawMolecule(mc)
drawer.FinishDrawing()
svg = drawer.GetDrawingText()
return SVG(svg.replace("svg:", ""))
4 changes: 2 additions & 2 deletions app/routers/chem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from app.modules.npscorer import getnp_score
from app.modules.descriptor_calculator import GetBasicDescriptors
from app.modules.classyfire import classify, result
from app.modules.cdkmodules import getCDKSDG
from app.modules.cdkmodules import getCDKSDGMol

router = APIRouter(
prefix="/chem",
Expand Down Expand Up @@ -141,7 +141,7 @@ async def classyfire_result(id: Optional[str]):
@router.get("/{smiles}/cdk2d")
async def cdk2d_coordinates(smiles: Optional[str]):
if smiles:
return getCDKSDG(smiles)
return getCDKSDGMol(smiles)


# @app.get("/molecules/", response_model=List[schemas.Molecule])
Expand Down

0 comments on commit 92c269a

Please sign in to comment.