api/utils.py
def draw_mol(
    smiles,
    height=200,
    width=200,
    img_type=None,
    highlightAtoms=[],
    atomcolors=[],
    highlightBonds=[],
    bondcolors={},
    mol=None,
):
    """
    Draw a molecule from a smiles
    :param smiles: the SMILES to render
    :param height: the height in px
    :param width: the width in px
    :return: an SVG as a string of the inage
    """
    if mol is None:
        mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return "None Mol"
    AllChem.Compute2DCoords(mol)
    Chem.Kekulize(mol)
    if not height:
        height = 200
    if not width:
        width = 200
    if img_type == "png":
        img = Draw.MolToImage(
            mol,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        img = img.convert("RGBA")
        datas = img.getdata()
        newData = []
        for item in datas:
            if item[0] == 255 and item[1] == 255 and item[2] == 255:
                newData.append((255, 255, 255, 0))
            else:
                newData.append(item)
        img.putdata(newData)
        response = HttpResponse(content_type="image/png")
        img.save(response, "PNG")
        return response
    else:
        drawer = rdMolDraw2D.MolDraw2DSVG(height, width)
        drawopt = drawer.drawOptions()
        drawopt.clearBackground = False
        drawer.DrawMolecule(
            mol,
            highlightAtoms=highlightAtoms,
            highlightAtomColors=atomcolors,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        drawer.DrawMolecule(mol)
        drawer.FinishDrawing()
        return drawer.GetDrawingText().replace("svg:", "")

    Draw a molecule from a smiles
    :param smiles: the SMILES to render
    :param height: the height in px
    :param width: the width in px
    :return: an SVG as a string of the inage
    
Draw a molecule from a smiles
Draw a molecule from a smiles
def draw_mol(
    smiles,
    height=200,
    width=200,
    img_type=None,
    highlightAtoms=[],
    atomcolors=[],
    highlightBonds=[],
    bondcolors={},
    mol=None,
):
    if mol is None:
        mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return "None Mol"
    AllChem.Compute2DCoords(mol)
    Chem.Kekulize(mol)
    if not height:
        height = 200
    if not width:
        width = 200
    if img_type == "png":
        img = Draw.MolToImage(
            mol,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        img = img.convert("RGBA")
        datas = img.getdata()
        newData = []
        for item in datas:
            if item[0] == 255 and item[1] == 255 and item[2] == 255:
                newData.append((255, 255, 255, 0))
            else:
                newData.append(item)
        img.putdata(newData)
        response = HttpResponse(content_type="image/png")
        img.save(response, "PNG")
        return response
    else:
        drawer = rdMolDraw2D.MolDraw2DSVG(height, width)
        drawopt = drawer.drawOptions()
        drawopt.clearBackground = False
        drawer.DrawMolecule(
            mol,
            highlightAtoms=highlightAtoms,
            highlightAtomColors=atomcolors,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        drawer.DrawMolecule(mol)
        drawer.FinishDrawing()
        return drawer.GetDrawingText().replace("svg:", "")
def draw_mol ( smiles , height = 200 , width = 200 , img_type = None , highlightAtoms = [ ] , atomcolors = [ ] , highlightBonds = [ ] , bondcolors = { } , mol = None , ) : if mol is None : mol = Chem . MolFromSmiles ( smiles ) if mol is None : return "None Mol" AllChem . Compute2DCoords ( mol ) Chem . Kekulize ( mol ) if not height : height = 200 if not width : width = 200 if img_type == "png" : img = Draw . MolToImage ( mol , highlightBonds = highlightBonds , highlightBondColors = bondcolors , ) img = img . convert ( "RGBA" ) datas = img . getdata ( ) newData = [ ] for item in datas : if item [ 0 ] == 255 and item [ 1 ] == 255 and item [ 2 ] == 255 : newData . append ( ( 255 , 255 , 255 , 0 ) ) else : newData . append ( item ) img . putdata ( newData ) response = HttpResponse ( content_type = "image/png" ) img . save ( response , "PNG" ) return response else : drawer = rdMolDraw2D . MolDraw2DSVG ( height , width ) drawopt = drawer . drawOptions ( ) drawopt . clearBackground = False drawer . DrawMolecule ( mol , highlightAtoms = highlightAtoms , highlightAtomColors = atomcolors , highlightBonds = highlightBonds , highlightBondColors = bondcolors , ) drawer . DrawMolecule ( mol ) drawer . FinishDrawing ( ) return drawer . GetDrawingText ( ) . replace ( "svg:" , "" )
Draw a molecule from a smiles
Draw a molecule from a smiles
"""
    Draw a molecule from a smiles
    :param smiles: the SMILES to render
    :param height: the height in px
    :param width: the width in px
    :return: an SVG as a string of the inage
    """
an SVG as a string of the inage
an SVG as a string of the inage
smiles
the SMILES to render
the SMILES to render
height
the height in px
the height in px
width
the width in px
the width in px
img_type
highlightAtoms
atomcolors
highlightBonds
bondcolors
mol