Example #1
0
def MolToQPixmap(mol, size=(300,300), kekulize=True,  wedgeBonds=True,
                 fitImage=False, options=None, **kwargs):
    """ Generates a drawing of a molecule on a Qt QPixmap
    """
    if not mol:
        raise ValueError('Null molecule provided')
    from rdkit.Chem.Draw.qtCanvas import Canvas
    canvas = Canvas(size)
    if options is None:
        options = DrawingOptions()
    options.bgColor = None
    if fitImage:
        options.dotsPerAngstrom = int(min(size) / 10)
    options.wedgeDashedBonds=wedgeBonds
    if kekulize:
        from rdkit import Chem
        mol = Chem.Mol(mol.ToBinary())
        Chem.Kekulize(mol)
    if not mol.GetNumConformers():
        from rdkit.Chem import AllChem
        AllChem.Compute2DCoords(mol)
    drawer = MolDrawing(canvas=canvas, drawingOptions=options)
    drawer.AddMol(mol, **kwargs)
    canvas.flush()
    return canvas.pixmap
Example #2
0
def MolToMPL(mol,size=(300,300),kekulize=True, wedgeBonds=True,
             imageType=None, fitImage=False, options=None, **kwargs):
  """ Generates a drawing of a molecule on a matplotlib canvas
  """
  if not mol:
    raise ValueError('Null molecule provided')
  from rdkit.Chem.Draw.mplCanvas import Canvas
  canvas = Canvas(size)
  if options is None:
    options = DrawingOptions()
    options.bgColor=None
  if fitImage:
      drawingOptions.dotsPerAngstrom = int(min(size) / 10)
  options.wedgeDashedBonds=wedgeBonds
  drawer = MolDrawing(canvas=canvas, drawingOptions=options)
  omol=mol
  if kekulize:
    from rdkit import Chem
    mol = Chem.Mol(mol.ToBinary())
    Chem.Kekulize(mol)
    
  if not mol.GetNumConformers():
    from rdkit.Chem import AllChem
    AllChem.Compute2DCoords(mol)
  
  drawer.AddMol(mol,**kwargs)
  omol._atomPs=drawer.atomPs[mol]
  for k,v in iteritems(omol._atomPs):
    omol._atomPs[k]=canvas.rescalePt(v)
  canvas._figure.set_size_inches(float(size[0])/100,float(size[1])/100)
  return canvas._figure
Example #3
0
def draw_mol_to_svg(mol, filename):
    """
    Draw a single molecule to an SVG file with transparent BG.

    """
    # change BG to transperent
    # (https://sourceforge.net/p/rdkit/mailman/message/31637105/)
    o = DrawingOptions()
    o.bgColor = None
    # Use copy of molecule to avoid changing instance of mol.
    new_mol = rdkit.MolFromMolBlock(rdkit.MolToMolBlock(mol))
    rdkit.Compute2DCoords(new_mol)
    Draw.MolToFile(new_mol,
                   filename,
                   fitImage=True,
                   imageType='svg',
                   options=o)
def draw_smiles_to_svg(smiles, filename):
    """
    Draw a single molecule to an SVG file with transparent BG.

    """
    mol = Chem.MolFromSmiles(smiles)
    # change BG to transperent
    # (https://sourceforge.net/p/rdkit/mailman/message/31637105/)
    o = DrawingOptions()
    o.bgColor = None
    Chem.Compute2DCoords(mol)
    Draw.MolToFile(
        mol,
        filename,
        fitImage=True,
        imageType='svg',
        options=o
    )
Example #5
0
def MolToMPL(mol,
             size=(300, 300),
             kekulize=True,
             wedgeBonds=True,
             imageType=None,
             fitImage=False,
             options=None,
             **kwargs):
    """ Generates a drawing of a molecule on a matplotlib canvas
  """
    if not mol:
        raise ValueError('Null molecule provided')
    from rdkit.Chem.Draw.mplCanvas import Canvas
    canvas = Canvas(size)
    if options is None:
        options = DrawingOptions()
        options.bgColor = None
    if fitImage:
        options.dotsPerAngstrom = int(min(size) / 10)
    options.wedgeDashedBonds = wedgeBonds
    drawer = MolDrawing(canvas=canvas, drawingOptions=options)
    omol = mol
    if kekulize:
        from rdkit import Chem
        mol = Chem.Mol(mol.ToBinary())
        Chem.Kekulize(mol)

    if not mol.GetNumConformers():
        from rdkit.Chem import AllChem
        AllChem.Compute2DCoords(mol)

    drawer.AddMol(mol, **kwargs)
    omol._atomPs = drawer.atomPs[mol]
    for k, v in omol._atomPs.items():
        omol._atomPs[k] = canvas.rescalePt(v)
    canvas._figure.set_size_inches(float(size[0]) / 100, float(size[1]) / 100)
    return canvas._figure
Example #6
0
    def generate_fragemnts(self):

        # Clear class level list
        del self.molecule[:]

        # Get text input from LineEdit widget
        self.smile = str(self.NTPS_smiles_entry.text())

        # Append cStructure mol to class level list
        self.molecule.append(
            cStructure('precursor', self.smile, 'precursor.png'))

        # SMARTS string for bond disconnection
        self.patt = Chem.MolFromSmarts(
            '[!$([NH]!@C(=O))&!D1&!$(*#*)]-&!@[!$([NH]!@C(=O))&!D1&!$(*#*)]')

        # Init parent mol object
        self.mol = Chem.MolFromSmiles(self.smile)

        # find the rotatable bonds
        self.bonds = self.mol.GetSubstructMatches(self.patt)

        # create an editable molecule, break the bonds, and add dummies:
        self.all_smis = [self.smile]

        # disconnect rotatable bonds
        for a, b in self.bonds:
            self.em = Chem.EditableMol(self.mol)
            self.nAts = self.mol.GetNumAtoms()
            self.em.RemoveBond(a, b)
            self.em.AddAtom(Chem.Atom(0))
            self.em.AddBond(a, self.nAts, Chem.BondType.SINGLE)
            self.em.AddAtom(Chem.Atom(0))
            self.em.AddBond(b, self.nAts + 1, Chem.BondType.SINGLE)
            self.nAts += 2
            self.p = self.em.GetMol()
            Chem.SanitizeMol(self.p)
            self.smis = [
                Chem.MolToSmiles(x, True)
                for x in Chem.GetMolFrags(self.p, asMols=True)
            ]
            for self.smi in self.smis:
                self.all_smis.append(self.smi)

        # draw molecules and save png images for display in structure viewer widget
        # --> there's probably a better way to do this...
        self.draw = 0
        for i, self.smi in enumerate(self.all_smis):
            if i == 0:
                struct_type = 'precursor'
                img_path = 'precursor.png'

            else:
                struct_type, img_path = 'fragment', 'fragment_%s.png' % self.draw

            self.molecule.append(cStructure(struct_type, self.smi, img_path))
            self.template = Chem.MolFromSmiles(self.smi)
            drawOptions = DrawingOptions()
            drawOptions.bgColor = (0, 0, 0)
            Draw.MolToFile(self.template, img_path, options=drawOptions)

            # append to class level list
            self.structures.append(img_path)

            # append to GUI fragments listbox - file name only
            self.NTPS_frag_list.addItem(
                QtGui.QListWidgetItem(
                    img_path.split('/')[-1].strip().split('.')[0]))

            self.draw += 1

        self.autocomplete_mod_properties()

        # add image of first structure to graphicsview
        scene = QtGui.QGraphicsScene()
        scene.addPixmap(QtGui.QPixmap(self.structures[0]))
        self.NTPS_structure_view.setScene(scene)
        self.NTPS_structure_view.show()

        return