Esempio n. 1
0
 def getNNAtoms(self, molStr, cAtoms, hight):
     atoms = copy.deepcopy(cAtoms)
     # create an RDKit mol
     mol = Chem.MolFromMolBlock(molStr, True, False)
     if not mol:
         print "Could not create mol for compound "
         return []
     adj = GetAdjacencyMatrix(mol)
     visitedAtoms = []
     for n in range(hight):
         for atom in copy.deepcopy(atoms):
             if atom not in visitedAtoms:
                 lNN = findNeighbors(atom, adj)
                 visitedAtoms.append(atom)
                 for lnn in lNN:
                     if lnn not in atoms:
                         atoms.append(lnn)
     atoms.sort()
     return atoms
Esempio n. 2
0
 def getNNAtoms(self, molStr, cAtoms, hight):
     atoms = copy.deepcopy(cAtoms)
     # create an RDKit mol
     mol = Chem.MolFromMolBlock(molStr,True,False)
     if not mol:
         print "Could not create mol for compound "
         return []
     adj = GetAdjacencyMatrix(mol)
     visitedAtoms = []
     for n in range(hight):
       for atom in copy.deepcopy(atoms):
          if atom not in visitedAtoms:
             lNN = findNeighbors(atom,adj)
             visitedAtoms.append(atom)
             for lnn in lNN:
                 if lnn not in atoms:
                     atoms.append(lnn)
     atoms.sort()
     return atoms
Esempio n. 3
0
    def createSignImg(self,
                      smi,
                      signature,
                      atomColor,
                      imgPath,
                      endHeight=None):
        colors = []
        print "Creating signature image..."
        if not signature or not atomColor or not smi:
            print "Missing inputs:", str([smi, signature, atomColor])
            return "", "", [], []
        if hasattr(self.model, "specialType") and self.model.specialType == 1:
            # Create an Orange ExampleTable with a smiles attribute
            smilesAttr = orange.EnumVariable("SMILEStoPred", values=[smi])
            myDomain = orange.Domain([smilesAttr], 0)
            smilesData = dataUtilities.DataTable(myDomain, [[smi]])
            preCalcData = None
            startHeight = 0
            dataSign, cmpdSignDict, cmpdSignList, sdfStr = getSignatures.getSignatures(
                smilesData,
                startHeight,
                endHeight,
                preCalcData,
                returnAtomID=True)
            cmpdSignList = cmpdSignList[0]
            CLabDesc = []
            # create a mol file
            tmpFile = miscUtilities.generateUniqueFile(desc="NN", ext="mol")
            file = open(tmpFile, "w")
            molStr = ""
            for line in sdfStr[0]:
                if "$$$$" in line:
                    break
                molStr += line
                file.write(line)
            file.close()
        else:
            CLabDesc, cmpdSignList, tmpFile, molStr = self.getClabDescSignList(
                smi, getMolFile=True)
        if not cmpdSignList or not tmpFile:
            print "Couldn't get the cmpd list or the mol file"
            return "", "", [], []
        # create an RDKit mol
        mol = Chem.MolFromMolFile(tmpFile, True, False)
        if not mol:
            mol = Chem.MolFromMolFile(tmpFile, False, False)
        if not mol:
            print "Could not create mol for: ", smi
            return "", "", [], []
        adj = GetAdjacencyMatrix(mol)
        # find the NN
        hights = []
        for i in miscUtilities.Range(0, len(cmpdSignList), mol.GetNumAtoms()):
            hList = cmpdSignList[i:i + mol.GetNumAtoms()]
            if len(hList):
                hights.append(cmpdSignList[i:i + mol.GetNumAtoms()])

        atoms = []
        hight = None
        for idx, h in enumerate(hights):
            if signature in h:
                for i, a in enumerate(h):
                    if a == signature:
                        atoms.append(i)
                hight = idx
                break
        if len(atoms) == 0:
            print "ERROR: Could not find the atom for ", signature
            return "signatureNOTfound", "", [], []
        #print "IniAtoms: ",atoms
        visitedAtoms = []
        for n in range(hight):
            for atom in copy.deepcopy(atoms):
                if atom not in visitedAtoms:
                    lNN = findNeighbors(atom, adj)
                    visitedAtoms.append(atom)
                    for lnn in lNN:
                        if lnn not in atoms:
                            atoms.append(lnn)
        atoms.sort()
        os.system("rm " + tmpFile)
        #Specify the atom colors
        colors = [atomColor] * len(atoms)

        if not imgPath:
            return "", molStr, atoms, colors
        try:
            #Draw the image
            MolDrawing.elemDict = defaultdict(lambda: (0, 0, 0))
            Draw.MolToImageFile(mol,
                                imgPath,
                                size=(300, 300),
                                kekulize=True,
                                wedgeBonds=True,
                                highlightAtoms=atoms)
            #Color the Highlighted atoms with the choosen atomColor.
            # Only using one color
            if atomColor == 'r':
                rgb = (255, 0, 0)
            elif atomColor == 'g':
                rgb = (0, 255, 0)
            else:
                rgb = (0, 0, 255)  #Blue

            img = Image.open(imgPath)
            img = img.convert("RGBA")
            pixdata = img.getdata()
            newData = list()
            for item in pixdata:
                if item[0] == 255 and item[1] == 0 and item[2] == 0:
                    newData.append(rgb + (255, ))
                else:
                    newData.append(item)
            img.putdata(newData)
            img.save(imgPath)

            if os.path.isfile(imgPath):
                return imgPath, molStr, atoms, colors
            else:
                return "", molStr, atoms, colors
        except:
            return "", molStr, atoms, colors
Esempio n. 4
0
    def createSignImg(self,smi,signature,atomColor,imgPath, endHeight = None):
        colors = []
        print "Creating signature image..."
        if not signature or not atomColor or not smi:
            print "Missing inputs:",str([smi,signature,atomColor])
            return "","",[], []
        if hasattr(self.model, "specialType") and self.model.specialType == 1:
            # Create an Orange ExampleTable with a smiles attribute
            smilesAttr = orange.EnumVariable("SMILEStoPred", values = [smi])
            myDomain = orange.Domain([smilesAttr], 0)
            smilesData = dataUtilities.DataTable(myDomain, [[smi]])
            preCalcData = None
            startHeight = 0
            dataSign,cmpdSignDict, cmpdSignList, sdfStr  = getSignatures.getSignatures(smilesData, startHeight, endHeight, preCalcData, returnAtomID=True)
            cmpdSignList = cmpdSignList[0]
            CLabDesc = []
            # create a mol file
            tmpFile = miscUtilities.generateUniqueFile(desc="NN", ext = "mol")
            file= open(tmpFile,"w")
            molStr=""
            for line in sdfStr[0]:
                if "$$$$" in line:
                    break
                molStr += line
                file.write(line)
            file.close()
        else: 
            CLabDesc,cmpdSignList, tmpFile, molStr  =  self.getClabDescSignList(smi, getMolFile=True)
        if not cmpdSignList or not tmpFile:
            print "Couldn't get the cmpd list or the mol file"
            return "","",[], []
        # create an RDKit mol
        mol = Chem.MolFromMolFile(tmpFile,True,False)
        if not mol:
            mol = Chem.MolFromMolFile(tmpFile,False,False)
        if not mol:
            print "Could not create mol for: ",smi
            return "","",[], []
        adj = GetAdjacencyMatrix(mol)
        # find the NN
        hights = []
        for i in miscUtilities.Range(0,len(cmpdSignList),mol.GetNumAtoms()):
            hList = cmpdSignList[i:i+mol.GetNumAtoms()]
            if len(hList):
                hights.append(cmpdSignList[i:i+mol.GetNumAtoms()])
       
        atoms = []
        hight = None
        for idx,h in enumerate(hights):
            if signature in h:
                for i,a in enumerate(h):
                    if a == signature:
                        atoms.append(i)
                hight = idx
                break
        if len(atoms) == 0:
            print "ERROR: Could not find the atom for ",signature
            return "signatureNOTfound","",[],[]
        #print "IniAtoms: ",atoms
        visitedAtoms = []
        for n in range(hight):
          for atom in copy.deepcopy(atoms):
             if atom not in visitedAtoms:    
                lNN = findNeighbors(atom,adj)
                visitedAtoms.append(atom)
                for lnn in lNN:
                    if lnn not in atoms: 
                        atoms.append(lnn)
        atoms.sort()
        os.system("rm " + tmpFile)
        #Specify the atom colors
        colors=[atomColor]*len(atoms)

        if not imgPath:
            return "",molStr,atoms,colors 
        try:
                #Draw the image
                MolDrawing.elemDict=defaultdict(lambda : (0,0,0))
                Draw.MolToImageFile(mol,imgPath,size=(300, 300), kekulize=True, wedgeBonds=True, highlightAtoms=atoms)
                #Color the Highlighted atoms with the choosen atomColor.
                # Only using one color
                if atomColor == 'r':
                    rgb = (255,0,0)
                elif atomColor == 'g':
                    rgb = (0,255,0)
                else:
                    rgb = (0,0,255)    #Blue
                    
                img = Image.open(imgPath)
                img = img.convert("RGBA")
                pixdata = img.getdata()
                newData = list()
                for item in pixdata:
                  if item[0] == 255 and item[1] == 0 and item[2] == 0:
                    newData.append(rgb + (255,) )
                  else:
                    newData.append(item)
                img.putdata(newData)
                img.save(imgPath)

                if os.path.isfile(imgPath):
                    return imgPath,molStr,atoms,colors
                else:
                    return "",molStr,atoms,colors
        except:
                return "",molStr,atoms,colors