def unselResidues(): from chimera.selection import currentResidues selResidues = currentResidues(asDict=True) unsel = [] for m in chimera.openModels.list(modelTypes=[chimera.Molecule]): unsel.extend(filter(lambda r: r not in selResidues, m.residues)) return unsel
def standardize_terminal_protein_residues(receptor_pdb, molID="#0"): """ To prevent errors like 'ValueError: Cannot determine GAFF type for :11.A@HD14 (etc.)' raised by initiateAddions(), originating from termini capped by Shrodinger's Maestro or incomplete or missing protein residues, mutate the terminal residues to their original aa type (applies to every protein chain). """ print("Standardizing protein's terminal residues.") rc("sel %s & protein" % molID) chaindID_resids = defaultdict(list) for r in currentResidues(): chaindID_resids[r.id.chainId].append((r.id.position, r.type)) for chainID in chaindID_resids.keys(): chaindID_resids[chainID].sort(key=itemgetter(0)) # NOTE: the side-chain mutations (swapaa) were not necessary in the proteins tested so far. # rc("swapaa %s %s:%i.%s" % # (chaindID_resids[chainID][0][1], molID, chaindID_resids[chainID][0][0], str(chainID))) # N-term rc("del element.H & %s:%i.%s" % (molID, chaindID_resids[chainID][0][0], str(chainID))) # rc("swapaa %s %s:%i.%s" % # (chaindID_resids[chainID][-1][1], molID, chaindID_resids[chainID][-1][0], str(chainID))) # C-term rc("del element.H & %s:%i.%s" % (molID, chaindID_resids[chainID][-1][0], str(chainID))) # Only if you write and load the PDB then Chimera will reset the valence of the N-terminal amide and # thus will not again the H1,H2,H3 which cause the error. rc("write format pdb #0 " + receptor_pdb.replace(".pdb", "_tmp.pdb")) rc("del #0") rc("open %s %s" % (molID, receptor_pdb.replace(".pdb", "_tmp.pdb"))) os.remove(receptor_pdb.replace(".pdb", "_tmp.pdb"))
def NDBColors(self): from chimera import selection if selection.currentEmpty(): import Midas residues = Midas._selectedResidues('#') else: residues = selection.currentResidues() NA.NDBColors(residues)
def apply(self, restrict=False): if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: self.setResidue(r) else: for m in chimera.openModels.list(modelTypes=[chimera.Molecule]): for r in m.residues: self.setResidue(r)
def apply(self, restrict=False): if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: self.setResidue(r) else: for m in chimera.openModels.list( modelTypes=[chimera.Molecule]): for r in m.residues: self.setResidue(r)
def Apply(self, restrict): if self.currentRRC: rrc = self.currentRRC else: rrc = RRC(None, self.guide, self.plane, self.planeNormal, self.isNucleic, self.mainchain) if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: rrc.setResidue(r) else: for m in chimera.openModels.list(modelTypes=[chimera.Molecule]): for r in m.residues: rrc.setResidue(r)
def _getMolecules(self, assignsel, usesel, restrict): if assignsel or usesel: sel = selection.currentMolecules() if usesel: for r in selection.currentResidues(): restrict[r] = True for a in selection.currentAtoms(): restrict[a] = True for m in sel: restrict[m] = True if assignsel: molecules = sel else: molecules = [] if not molecules: molecules = chimera.openModels.list( modelTypes=[chimera.Molecule]) return molecules
def Apply(self, restrict): if self.currentXS: xs = self.currentXS else: xs = XS(None, self.points, self.smoothacross, self.smoothalong, self.closed, self.divisions) if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: xs.setResidue(r) else: for m in chimera.openModels.list(modelTypes=[chimera.Molecule]): for r in m.residues: xs.setResidue(r)
def showChimeraSelection(self): selRegion = self.getRegion(SEL_REGION_NAME, create=1, fill=self.seqCanvas.mav.prefs[SEL_REGION_INTERIOR], outline=self.seqCanvas.mav.prefs[SEL_REGION_BORDER]) selRegion.clear() resDict = {} for res in currentResidues(): resDict[res] = 1 blocks = [] for aseq in self.seqCanvas.seqs: try: matchMaps = aseq.matchMaps except AttributeError: continue for matchMap in matchMaps.values(): start = None end = None for i in range(len(aseq.ungapped())): if i in matchMap \ and matchMap[i] in resDict: if start is not None: end = i else: end = start = i else: if start is not None: blocks.append([aseq, aseq, aseq. \ ungapped2gapped(start ), aseq. \ ungapped2gapped(end)]) start = end = None if start is not None: blocks.append([aseq, aseq, aseq.ungapped2gapped(start), aseq.ungapped2gapped(end)]) if blocks and self._firstChimeraShow: self._firstChimeraShow = False self.seqCanvas.mav.status( "Chimera selection region displayed.\n" "Preferences..Regions controls this display.\n") selRegion.addBlocks(blocks) self.raiseRegion(selRegion)
def Apply(self, restrict): if self.currentXS: xs = self.currentXS else: xs = XS(None, self.points, self.smoothacross, self.smoothalong, self.closed, self.divisions) if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: xs.setResidue(r) else: for m in chimera.openModels.list( modelTypes=[chimera.Molecule]): for r in m.residues: xs.setResidue(r)
def Apply(self, restrict): if self.currentRRC: rrc = self.currentRRC else: rrc = RRC(None, self.guide, self.plane, self.planeNormal, self.isNucleic, self.mainchain) if restrict: from chimera import selection rList = selection.currentResidues() else: rList = [] if rList: for r in rList: rrc.setResidue(r) else: for m in chimera.openModels.list( modelTypes=[chimera.Molecule]): for r in m.residues: rrc.setResidue(r)
def selResidues(noneReturnsAll=True, implied=False, create=False): residues = selection.currentResidues() extendSelection(residues, 'residues', noneReturnsAll, implied, create) return residues
def nw(s1, s2, scoreMatch=10, scoreMismatch=-3, scoreGap=0, scoreGapOpen=-40, gapChar=".", returnSeqs=False, scoreMatrix=None, similarityMatrix=None, frequencyMatrix=None, endsAreGaps=False, ssMatrix=None, ssFraction=0.9, gapOpenHelix=None, gapOpenStrand=None, gapOpenOther=None, debug=False): # if 'scoreMatrix', 'similarityMatrix', or 'frequencyMatrix' is # provided, then 'scoreMatch' and 'scoreMismatch' are ignored and # the matrix is used to evaluate matching between the sequences. # 'scoreMatrix' should be a two-dimensional array of size # len(s1) x len(s2). 'similarityMatrix' should be a dictionary # keyed with two-tuples of residue types. 'frequencyMatrix' should # be a list of length s2 of dictionaries, keyed by residue type. # # if 'ssFraction' is not None/False, then 'ssMatrix' should be a 3x3 # matrix keyed with 2-tuples of secondary structure types ('H': helix, # 'S': strand, 'O': other). The score will be a mixture of the # ss/similarity matrix scores weighted by the ssFraction # [ssFraction * ss score + (1 - ssFraction) * similarity score] # # if 'gapOpenHelix/Strand/Other' is not None and 'ssFraction' is not # None/False, then scoreGapOpen is ignored when an intra-helix/ # intra-strand/other gap is opened and the appropriate penalty # is applied instead # # if 'returnSeqs' is True, then instead of returning a match list # (a list of two-tuples) as the second value, a two-tuple of gapped # Sequences will be returned. In both cases, the first return value # is the match score. m = [] bt = [] for i1 in range(len(s1) + 1): m.append((len(s2) + 1) * [ 0 ]) bt.append((len(s2) + 1) * [None]) bt[i1][0] = 1 if endsAreGaps and i1 > 0: m[i1][0] = scoreGapOpen + i1 * scoreGap for i2 in range(len(s2) + 1): bt[0][i2] = 2 if endsAreGaps and i2 > 0: m[0][i2] = scoreGapOpen * i2 * scoreGap if similarityMatrix is not None: evaluate = lambda i1, i2: similarityMatrix[(s1[i1], s2[i2])] elif scoreMatrix is not None: evaluate = lambda i1, i2: scoreMatrix[i1][i2] elif frequencyMatrix is not None: evaluate = lambda i1, i2: frequencyMatrix[i2][s1[i1]] else: def evaluate(i1, i2): if s1[i1] == s2[i2]: return scoreMatch return scoreMismatch doingSS = ssFraction is not None and ssFraction is not False \ and ssMatrix is not None if doingSS: prevEval = evaluate simFraction = 1.0 - ssFraction def ssEval(i1, i2): if hasattr(s1, 'ssFreqs'): freqs1 = s1.ssFreqs[i1] else: freqs1 = {s1.ssType(i1): 1.0} if hasattr(s2, 'ssFreqs'): freqs2 = s2.ssFreqs[i2] else: freqs2 = {s2.ssType(i2): 1.0} val = 0.0 for ss1, freq1 in freqs1.items(): if ss1 == None: continue for ss2, freq2 in freqs2.items(): if ss2 == None: continue val += freq1 * freq2 * ssMatrix[(ss1, ss2)] return val evaluate = lambda i1, i2: ssFraction * ssEval(i1, i2) + \ simFraction * prevEval(i1, i2) # precompute appropriate gap-open penalties gapOpen1 = [scoreGapOpen] * (len(s1)+1) gapOpen2 = [scoreGapOpen] * (len(s2)+1) if endsAreGaps: if gapOpenOther is not None: gapOpen1[0] = gapOpen2[0] = gapOpenOther else: gapOpen1[0] = gapOpen2[0] = 0 if doingSS and gapOpenOther != None: for seq, gapOpens in [(s1, gapOpen1), (s2, gapOpen2)]: if hasattr(seq, 'gapFreqs'): for i, gapFreq in enumerate(seq.gapFreqs): gapOpens[i+1] = \ gapFreq['H'] * gapOpenHelix + \ gapFreq['S'] * gapOpenStrand + \ gapFreq['O'] * gapOpenOther else: ssTypes = [seq.ssType(i) for i in range(len(seq))] for i, ss in enumerate(ssTypes[:-1]): nextSS = ssTypes[i+1] if ss == nextSS and ss == 'H': gapOpens[i+1] = gapOpenHelix elif ss == nextSS and ss == 'S': gapOpens[i+1] = gapOpenStrand else: gapOpens[i+1] = gapOpenOther colGapStarts = [0] * len(s2) # don't care about column zero for i1 in range(len(s1)): rowGapPos = 0 for i2 in range(len(s2)): best = m[i1][i2] + evaluate(i1, i2) btType = 0 if i2 + 1 < len(s2) or endsAreGaps: colGapPos = colGapStarts[i2] skipSize = i1 + 1 - colGapPos if hasattr(s1, "occupancy"): totOcc = 0.0 for i in range(colGapPos, i1+1): totOcc += s1.occupancy[i] colSkipVal = totOcc * scoreGap else: colSkipVal = skipSize * scoreGap baseColGapVal = m[colGapPos][i2+1] + colSkipVal skip = baseColGapVal + gapOpen2[i2+1] else: skipSize = 1 colSkipVal = 0 skip = m[i1][i2+1] if skip > best: best = skip btType = skipSize if i1 + 1 < len(s1) or endsAreGaps: skipSize = i2 + 1 - rowGapPos if hasattr(s2, "occupancy"): totOcc = 0.0 for i in range(rowGapPos, i2+1): totOcc += s2.occupancy[i] rowSkipVal = totOcc * scoreGap else: rowSkipVal = skipSize * scoreGap baseRowGapVal = m[i1+1][rowGapPos] + rowSkipVal skip = baseRowGapVal + gapOpen1[i1+1] else: skipSize = 1 rowSkipVal = 0 skip = m[i1+1][i2] if skip > best: best = skip btType = 0 - skipSize m[i1+1][i2+1] = best bt[i1+1][i2+1] = btType if btType >= 0: # not gapping the row if best > baseRowGapVal: rowGapPos = i2 + 1 if btType <= 0: # not gapping the column if best > baseColGapVal: colGapStarts[i2] = i1 + 1 if debug: from chimera.selection import currentResidues cr = currentResidues(asDict=True) if cr: for fileName, matrix in [("scores", m), ("trace", bt)]: out = open("/home/socr/a/pett/rm/" + fileName, "w") print>>out, " ", for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print>>out, "%5d" % i2, print>>out print>>out, " ", for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print>>out, "%5s" % s2[i2], print>>out for i1, r1 in enumerate(s1.residues): if r1 not in cr: continue print>>out, "%3d" % i1, s1[i1], for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print>>out, "%5g" % ( matrix[i1+1][i2+1]), print>>out out.close() i1 = len(s1) i2 = len(s2) matchList = [] while i1 > 0 and i2 > 0: btType = bt[i1][i2] if btType == 0: matchList.append((i1-1, i2-1)) i1 = i1 - 1 i2 = i2 - 1 elif btType > 0: i1 = i1 - btType else: i2 = i2 + btType if returnSeqs: return m[len(s1)][len(s2)], matches2gappedSeqs(matchList, s1, s2, gapChar=gapChar) return m[len(s1)][len(s2)], matchList
def makeAllMeshes(proID, jobID, peptideLen=40, pocketDist=5.0): global curResID global curProID global meshDir global pocketMap global resCount global curJobID #Insure input is a 4 character protein ID proID = proID[0:4] #Create a new directory for the protein meshDir = proID + 'Mesh' curProID = proID curJobID = jobID # os.mkdir(meshDir) # os.system("chmod 777 " + meshDir) try: #Fetch protein from the PDB runCommand('open ' + proID) #Initial prep for pocket search runCommand('delete solvent') runCommand('delete :HOH') #Open protein model model = openModels.list(modelTypes=[Molecule])[0] RES_LIST = model.residues resCount = len(RES_LIST) #Pocket finding procedure chains = [] counts = [] index = 0 print 'Counting Chain Lengths................' for res in RES_LIST: curChain = str(res.id.chainId) if len(chains) == 0: chains.append(curChain) counts.append(1) if curChain != chains[index]: index = index + 1 chains.append(curChain) counts.append(1) else: counts[index] = counts[index] + 1 print 'Finding Peptide Ligand Pockets................' for i, chain in enumerate(chains): if counts[i] < peptideLen: #Less than X amino acids = peptide runCommand('~select') runCommand('select ligand & :.' + chain) #Check if actually ligand runCommand('select selected zr<' + str(pocketDist)) #Select pocket runCommand('~select :/isHet zr<' + str(pocketDist)) #De-select ligand runCommand('~select ligand') residues = selection.currentResidues() #Store pocket in memory for res in residues: if res in pocketMap: pocketMap[res].append('|' + chain) else: pocketMap[res] = [chain] print 'Finding non-Peptide Ligand Pockets................' for res in RES_LIST: if res.isHet: #Check if "heterogenous residue" which includes ligands curResID = str(res.id) runCommand('~select') runCommand('select :' + curResID + ' & ligand') #Check if actually ligand runCommand('select selected zr<' + str(pocketDist)) #Select pocket runCommand('~select :' + curResID) residues = selection.currentResidues() #Store pocket in memory for pocketRes in residues: if pocketRes in pocketMap: pocketMap[pocketRes].append('|' + res.type + '-' + curResID) else: pocketMap[pocketRes] = [res.type + '-' + curResID] print 'Dock Prep................' prep(openModels.list(modelTypes=[Molecule])) runCommand('delete ligand') print '\nRunning Delphi................' runCommand('~select') runCommand('write format pdb 0 ' + proID + curJobID + '.pdb') #Write delphi-ready protein with hydrogens #Create delphi parameter file f = open('./param_' + curJobID, 'w') f.write('in(pdb,file="' + proID + curJobID + '.pdb")\n') #Input/Output files f.write('in(siz,file="amber.siz")\n') f.write('in(crg,file="amber.crg")\n') f.write('out(phi,file="' + proID + curJobID + '.phi")\n') f.write('indi=2\n') #Interior dielectric constant f.write('exdi=80.0\n') #Exterior dielectric constant (water) f.write('prbrad=1.4\n') f.write('salt=0.15\n') #Salt concentration and radius f.write('ionrad=2.0\n') # f.write('nonit=20\n') #Non-linear iterations f.close() os.system("./DELPHI_2004_LINUX_v2/delphi_static param_" + curJobID) #Run delphi # print '\nExporting Mesh................' runCommand('select #0') runCommand('surface') #runCommand('export format X3D ./' + proID + '.x3d') # Prints raw X3D file. print '\nOpening Delphi Data................' runCommand('open ' + proID + curJobID + '.phi') runCommand('scolor #0 volume #1 cmap -1,red:0,white:1,blue;') print '\nRemoving Temp Files................' os.remove("./" + proID + curJobID + '.pdb') os.remove("./" + proID + curJobID + '.phi') os.remove('./param_' + curJobID) print '\nDONE: ' + proID except: f = open("./" + 'ErrorLog' + curJobID + '.txt', 'w') f.write(proID + ' error: ' + str(sys.exc_info()[0]) + str(sys.exc_info()[1]) + '\n') f.close() raise return
def makeAllMeshes(proID, jobID, peptideLen=40, pocketDist=5.0): global curResID global curProID global meshDir global pocketMap global resCount global curJobID #Insure input is a 4 character protein ID proID = proID[0:4] #Create a new directory for the protein meshDir = proID + 'Mesh' curProID = proID curJobID = jobID # os.mkdir(meshDir) # os.system("chmod 777 " + meshDir) try: #Fetch protein from the PDB runCommand('open '+ proID) #Initial prep for pocket search runCommand('delete solvent') runCommand('delete :HOH') #Open protein model model = openModels.list(modelTypes=[Molecule])[0] RES_LIST = model.residues resCount = len(RES_LIST) #Pocket finding procedure chains = [] counts = [] index = 0 print 'Counting Chain Lengths................' for res in RES_LIST: curChain = str(res.id.chainId) if len(chains) == 0: chains.append(curChain) counts.append(1) if curChain != chains[index]: index = index + 1 chains.append(curChain) counts.append(1) else: counts[index] = counts[index] + 1 print 'Finding Peptide Ligand Pockets................' for i, chain in enumerate(chains): if counts[i] < peptideLen: #Less than X amino acids = peptide runCommand('~select') runCommand('select ligand & :.' + chain) #Check if actually ligand runCommand('select selected zr<' + str(pocketDist)) #Select pocket runCommand('~select :/isHet zr<' + str(pocketDist)) #De-select ligand runCommand('~select ligand') residues = selection.currentResidues() #Store pocket in memory for res in residues: if res in pocketMap: pocketMap[res].append('|' + chain) else: pocketMap[res] = [chain] print 'Finding non-Peptide Ligand Pockets................' for res in RES_LIST: if res.isHet: #Check if "heterogenous residue" which includes ligands curResID = str(res.id) runCommand('~select') runCommand('select :' + curResID + ' & ligand') #Check if actually ligand runCommand('select selected zr<' + str(pocketDist)) #Select pocket runCommand('~select :' + curResID) residues = selection.currentResidues() #Store pocket in memory for pocketRes in residues: if pocketRes in pocketMap: pocketMap[pocketRes].append('|' + res.type + '-' + curResID) else: pocketMap[pocketRes] = [res.type + '-' + curResID] print 'Dock Prep................' prep(openModels.list(modelTypes=[Molecule])) runCommand('delete ligand') print '\nRunning Delphi................' runCommand('~select') runCommand('write format pdb 0 '+ proID + curJobID + '.pdb') #Write delphi-ready protein with hydrogens #Create delphi parameter file f = open('./param_' + curJobID, 'w') f.write('in(pdb,file="'+ proID + curJobID + '.pdb")\n') #Input/Output files f.write('in(siz,file="amber.siz")\n') f.write('in(crg,file="amber.crg")\n') f.write('out(phi,file="'+ proID + curJobID + '.phi")\n') f.write('indi=2\n') #Interior dielectric constant f.write('exdi=80.0\n') #Exterior dielectric constant (water) f.write('prbrad=1.4\n') f.write('salt=0.15\n') #Salt concentration and radius f.write('ionrad=2.0\n') # f.write('nonit=20\n') #Non-linear iterations f.close() os.system("./DELPHI_2004_LINUX_v2/delphi_static param_" + curJobID) #Run delphi # print '\nExporting Mesh................' runCommand('select #0') runCommand('surface') #runCommand('export format X3D ./' + proID + '.x3d') # Prints raw X3D file. print '\nOpening Delphi Data................' runCommand('open '+ proID + curJobID + '.phi') runCommand('scolor #0 volume #1 cmap -1,red:0,white:1,blue;') print '\nRemoving Temp Files................' os.remove("./" + proID + curJobID + '.pdb') os.remove("./" + proID + curJobID + '.phi') os.remove('./param_' + curJobID) print '\nDONE: ' + proID except: f = open("./" + 'ErrorLog' + curJobID + '.txt', 'w') f.write(proID + ' error: ' + str(sys.exc_info()[0]) + str(sys.exc_info()[1]) + '\n') f.close() raise
def _nextSel(): sel = selection.ItemizedSelection() sel.add(selection.currentBarrenGraphs()) if selLevel == SELRES: items = [] addResidues = {} for r in selection.currentResidues(): addResidues[r] = 1 selPseudoBonds = filter( lambda e, PB=chimera.PseudoBond: isinstance(e, PB), selection.currentEdges()) # for currently selected chain-trace pseudobonds # act as if they select their residues for ct in filter( lambda e: isinstance(e.pseudoBondGroup, chimera.ChainTrace), selPseudoBonds): addResidues[ct.atoms[0].residue] = 1 addResidues[ct.atoms[1].residue] = 1 for r in addResidues.keys(): items.extend(r.atoms) sel.add(items) sel.addImplied(vertices=0) sel.add(selPseudoBonds) # add chain trace pseudobonds that should be selected sel.add(selChainTrace(sel)) elif selLevel == SELCHAIN: chainIDs = {} for a in selection.currentAtoms(): chainID = a.residue.id.chainId try: chainIDs[a.molecule][chainID] = 1 except KeyError: chainIDs[a.molecule] = {chainID: 1} items = [] for m, ids in chainIDs.items(): for a in m.atoms: if not ids.has_key(a.residue.id.chainId): continue items.append(a) items.extend(a.bonds) pbgs = {} for pb in filter(lambda b, PB=chimera.PseudoBond: isinstance(b, PB), selection.currentEdges()): pbgs[pb.pseudoBondGroup] = 1 for pbg in pbgs.keys(): if isinstance(pbg, chimera.ChainTrace): continue items.extend(pbg.pseudoBonds) sel.add(items) sel.add(selChainTrace(sel)) elif selLevel == SELSUBMODEL: items = [] for m in selection.currentMolecules(): items.extend(m.atoms) items.extend(m.bonds) items.extend( filter(lambda e, PB=chimera.PseudoBond: isinstance(e, PB), selection.currentEdges())) sel.add(items) sel.add(selChainTrace(sel)) elif selLevel == SELMODEL: molDict = {} for m in selection.currentMolecules(): osl = m.oslIdent() if '.' in osl: molDict[osl[:osl.index('.')]] = 1 else: molDict[m] = 1 items = [] for mosl in molDict.keys(): if isinstance(mosl, basestring): for m in selection.OSLSelection(mosl).molecules(): items.extend(m.atoms) items.extend(m.bonds) else: items.extend(mosl.atoms) items.extend(mosl.bonds) items.extend( filter(lambda e, PB=chimera.PseudoBond: isinstance(e, PB), selection.currentEdges())) sel.add(items) sel.add(selChainTrace(sel)) elif selLevel == SELALL: global _topValid _topValid = True items = [] if selection.currentMolecules(): for m in chimera.openModels.list(): if hasattr(m, 'atoms'): items.extend(m.atoms) items.extend(m.bonds) for e in selection.currentEdges(): if isinstance(e, chimera.PseudoBond): pbsSelected = True break else: pbsSelected = False if pbsSelected: mgr = chimera.PseudoBondMgr.mgr() for grp in mgr.pseudoBondGroups: items.extend(grp.pseudoBonds) sel.add(items) sel.add(selChainTrace(sel)) global _selAllHandler if _selAllHandler is not None: _selAllHandler = \ chimera.openModels.addAddHandler( _addModelHandler, None) else: raise ValueError, "Bad selection level" # Handle selected surface pieces from Surface import selected_surface_pieces plist = selected_surface_pieces() mlist = set([p.model for p in plist]) if selLevel == SELALL and len(mlist) > 0: from _surface import SurfaceModel mlist = chimera.openModels.list(modelTypes=[SurfaceModel]) sel.add(mlist) return sel
args = cmdlineparse() import chimera from chimera import runCommand as rc from Addions import initiateAddions from DockPrep import prep from AddCharge import estimateFormalCharge from chimera.selection import currentResidues if args.COMPLEX: rc("open %s" % args.COMPLEX) # load the protein-ligand complex if args.STRIP_IONS: rc("delete ions") rc("split #0 ligands") rc("sel #0.2") # select the ligand ligres = currentResidues()[0] ligres.type = 'LIG' # change the resname of the ligand to 'LIG' rc("combine #0.1 modelId 1" ) # create a new molecule containing just the receptor rc("combine #0.2 modelId 2" ) # create a new molecule containing just the ligand # Now that we calculated the charges of the protein and the ligand, we just need the complex rc("combine #1,2 modelId 3" ) # create a new molecule containing the protein-ligand complex rc("del #0-2") pdb = args.COMPLEX.replace(".pdb", "_prep.pdb") elif args.RECEPTOR and args.LIGAND: rc("open %s" % args.RECEPTOR) # load the receptor rc("open %s" % args.LIGAND) # load the ligand if args.STRIP_IONS: rc("delete ions")
def chimeraFeatureExtraction( pdb_file, radii=[5, 8, 10, 12], metal_binding_sites=[], disopred_disorder_map=[], disopred_binding_map=[], sppider_binding_map=[], logfile="log.txt", attempts_limit=5, ): """ Arguments: metal_binding_sites: List of metal binding sites, each in dictionary keys: 'File Name', 'Site Number', 'Metal ID', 'Residues'. Global Features: """ # Open the pdb file, generate a surface, then perform DockPrep # (Add hydrogens, etc). # Try, in case surface generation fails. attempts = 0 while attempts < attempts_limit: try: rc("open " + pdb_file) generate_surface() break except Exception as e: attempts += 1 if attempts >= attempts_limit: raise Exception( "Surface computation failed {} times".format(attempts)) rc("~select") add_hydrogens_prep() # Initalize metal binding features for this pdb file metals = [] for metal_binding_site in metal_binding_sites: metal_type = metal_binding_site["Metal ID"] site_number = metal_binding_site["Site Number"] # Convert residues from: # C208,C211,etc ; to: # #:208@|#:211@ residues = metal_binding_site["Residues"] residues_selection = re.sub(r",", "@|#:", residues) residues_selection = re.sub(r"[a-zA-Z]", "", residues_selection) residues_selection = "#:{}@".format(residues_selection) # Select all residues associated with this metal binding site rc("select {}".format(residues_selection)) # Deselect backbone (only side chains should be selected) rc("~select @n,ca,c,o") # Clear the reply log, since we'll need it in a minute save_and_clear_reply_log(logfile) # Define a centroid around the currently selected residues rc(("define centroid massWeighting false radius {} raiseTool false " "number 1 selection").format(ionicRadiusDict[metal_type])) # Get the x,y,z coordinates of the metal from the reply log r, coords = read_reply_log() coords = re.sub(r"centroid name, ID, center: centroid: c1 \( *", "", coords) coords = re.sub(r"\)", "", coords) coords = re.sub(r",", "", coords) coords = re.sub(r" +", " ", coords) coords = re.sub(r"\n", "", coords) coords = re.sub(r"\r", "", coords) coords = ",".join(coords.split()) save_and_clear_reply_log(logfile) metals.append(MetalAtom(metal_type, residues, coords, site_number)) # Depths depths = get_depths() # Get RPKT atoms and residues rc("~select") rc("select :arg@cd|:lys@ce|:mly@ce|:kcx@ce|:pro@cd|:thr@cb") print("getting all {} rpkt residues".format( len(selection.currentResidues()))) rpkt_residues = [ ExtendedResidue( residue, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, ) for residue in selection.currentResidues() ] rpkt_atoms = [ ExtendedAtom( atom, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, ) for atom in selection.currentAtoms() ] print("done") # Get atoms and residues near RPKT # Efficiency technique: include already-calculated RPKT atoms, delete those from selection rc("~select") rc("select protein") rc("select :arg@cd z<12|:lys@ce z<12|:mly@ce z<12|:kcx@ce z<12|:pro@cd z<12|:thr@cb z<12" ) residues_near_rpkt = list() for residue in selection.currentResidues(): residues_near_rpkt.append( ExtendedResidue( residue, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, )) atoms_near_rpkt = list() for atom in selection.currentAtoms(): atoms_near_rpkt.append( ExtendedAtom( atom, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, )) # Get all atoms and residues # Efficiency technique: include already-calculated atoms near RPKT, delete those from selection rc("~select") rc("select protein") rc("~select :arg@cd z<12|:lys@ce z<12|:mly@ce z<12|:kcx@ce z<12|:pro@cd z<12|:thr@cb z<12" ) rc("~select :arg@cd|:lys@ce|:mly@ce|:kcx@ce|:pro@cd|:thr@cb") print("getting all {} additional residues".format( len(selection.currentResidues()))) all_residues = list(residues_near_rpkt) for residue in selection.currentResidues(): all_residues.append( ExtendedResidue( residue, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, )) # No need to get all atoms within 12A. print("done") print("getting global attributes") global_attributes = compute_global_attributes_residues(all_residues) global_attributes.pop("circularVariance") save_and_clear_reply_log(logfile) print("getting bubble attributes") atom_radius_features = {} for radius in radii: radius_plus_bit = radius + 1e-20 print("Analyzing at radius: {}\n".format(radius)) # Get atoms and residues near RPKT atoms rc("~select") rc(("select :arg@cd z<{radius}|:lys@ce z<{radius}|" ":mly@ce z<{radius}| :kcx@ce z<{radius}|:pro@cd z<{radius}|" ":thr@cb z<{radius}").format(radius=radius_plus_bit)) rc("~select :arg@cd|:lys@ce|:mly@ce|:kcx@ce|:pro@cd|:thr@cb") print("getting all {} non-RPKT residues near rpkt".format( len(selection.currentResidues()))) # Get all residues in the selection (near RPKT, building on previous RPKT list) residues_near_rpkt = list(rpkt_residues) for residue in selection.currentResidues(): residues_near_rpkt.append( ExtendedResidue( residue, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, )) atoms_near_rpkt = list(rpkt_atoms) for atom in selection.currentAtoms(): atoms_near_rpkt.append( ExtendedAtom( atom, depths, metals, disopred_disorder_map, disopred_binding_map, sppider_binding_map, )) print("done") # Get bubble attributes for atom in rpkt_atoms: if atom.name in atom_radius_features: atom_radius_features[ atom.name][radius] = compute_bubble_attributes_residues( atom, all_residues, atoms_near_rpkt, radius) else: # This must be the first radius the loop has seen. # atom_radius_features[atom.name] should point to a dict # with radii for keys, bubble attributes for values atom_radius_features[atom.name] = { radius: compute_bubble_attributes_residues(atom, all_residues, atoms_near_rpkt, radius) } print("getting atom-level attributes") atom_features = {} for atom in rpkt_atoms: eResidue = ExtendedResidue(atom.residue, depths, metals) extended_atoms = [] for a in eResidue.atoms: extended_atoms.append(ExtendedAtom(a, depths, metals)) atom_features[atom.name] = compute_bubble_attributes_residues( atom, residues_near_rpkt, extended_atoms, 0) # Format each feature with _res to match labels of original pipeline data for key in atom_features[atom.name]: if "res" not in key: atom_features[atom.name]["{}_res".format(key)] = atom_features[ atom.name].pop(key) # Add global circular variance atom_features[atom.name].update( compute_global_circular_variance(atom, atoms_near_rpkt)) atom_features[atom.name].update({ "AA": eResidue.residue_1_letter, "Position": eResidue.number }) return (global_attributes, atom_features, atom_radius_features)
def Apply(self): from chimera import selection if not self.restrict.get() or selection.currentEmpty(): molecules = chimera.openModels.list( modelTypes=[chimera.Molecule]) residues = [] for mol in molecules: residues.extend(mol.residues) else: residues = selection.currentResidues() molecules = tuple(set(r.molecule for r in residues)) residues = [r for r in residues if r.ribbonResidueClass.isNucleic()] backbone = self.showBackbone.get() display = backbone != 'atoms & bonds' for r in residues: r.ribbonDisplay = display side = self.showSide.get() if side == 'ladder': distSlop = 0.0 angleSlop = 0.0 relax = self.relaxParams.relaxConstraints if relax: distSlop = self.relaxParams.relaxDist angleSlop = self.relaxParams.relaxAngle NA.set_ladder(molecules, residues, rungRadius=self.rungRadius.get(), showStubs=self.showStubs.get(), skipNonBaseHBonds=self.skipNonBase.get(), useExisting=self.useExisting.get(), distSlop=distSlop, angleSlop=angleSlop) return if side.endswith('slab'): if self.currentStyle is None: info = self._getInfo() NA.addStyle(None, info) showGly = self.anchor.get() != NA.SUGAR if showGly and side.startswith('tube'): showGly = self.showGlycosidic.get() NA.set_slab(side, molecules, residues, style=self.currentStyle, thickness=self.thickness.get(), orient=self.showOrientation.get(), shape=self.shape.get(), showGly=showGly, hide=self.hideBases.get()) if side.startswith('fill'): for r in residues: r.fillDisplay = True else: for r in residues: r.fillDisplay = False if side.endswith('fill'): if self.showOrientation.get(): NA.set_orient(molecules, residues) else: NA.set_normal(molecules, residues) elif side.startswith('atoms'): NA.set_normal(molecules, residues) return
# NOTE: the default option addHFunc=AddH.hbondAddHydrogens raised an Error in Carbonic # Unhydrase with the Zn+2 ion. However, is works better for some proteins, where AddH.simpleAddHydrogens # leads to net_charge prediction of the order of 120... if args.COMPLEX: rc("open %s" % args.COMPLEX) # load the protein-ligand complex if args.KEEP_PROTEIN_HYDROGENS: rc("delete element.H") if args.REC_NET_CHARGE == None: standardize_terminal_protein_residues(args.RECEPTOR, "#0") # TODO: UNTESTED if args.STRIP_IONS: rc("delete ions") rc("split #0 ligands") rc("sel #0.2") # select the ligand ligres = currentResidues()[0] ligres.type = 'LIG' # change the resname of the ligand to 'LIG' rc("combine #0.1 modelId 1" ) # create a new molecule containing just the receptor rc("combine #0.2 modelId 2" ) # create a new molecule containing just the ligand rc("del #0") if args.REC_NET_CHARGE != None: rec_charge = args.REC_NET_CHARGE else: # We will estimate the receptor's net charge. For this we need to DockPrep the receptor (is fast). models = chimera.openModels.list(modelTypes=[chimera.Molecule]) # For a full list of DockPrep options, look into file Chimera-alpha_py2.7/share/DockPrep/__init__.py prep([models[0]], nogui=True, method=args.CHARGE_METHOD,
import chimera from Addions import initiateAddions from DockPrep import prep from AddCharge import estimateFormalCharge models = chimera.openModels.list(modelTypes=[chimera.Molecule]) print( "Preparing receptor for docking and calculating ligand AM1 charges (may be slow)." ) prep(models, nogui=True, method='am1') # Select the residues to be protonated if args.RADIUS > 0: rc("sel #1 z<%f & ~ #1" % args.RADIUS) elif args.RADIUS == 0: rc("sel #0") residues = currentResidues() # get the residue of the pocket residue_states = {} protonatable_resids = [] protonatable_resnames = [] for r in residues: if r.type in ["GLU", "GLH"]: states = resname_states_dict["GLU"] protonatable_resids.append(str(r.id)) protonatable_resnames.append(r.type) elif r.type in ["ASP", "ASH"]: states = resname_states_dict["ASP"] protonatable_resids.append(str(r.id)) protonatable_resnames.append(r.type) elif r.type in ["HIS", "HIE", "HID", "HIP"]: states = ["HIE", "HID", "HIP"] protonatable_resids.append(str(r.id))
def nw(s1, s2, scoreMatch=10, scoreMismatch=-3, scoreGap=0, scoreGapOpen=-40, gapChar=".", returnSeqs=False, scoreMatrix=None, similarityMatrix=None, frequencyMatrix=None, endsAreGaps=False, ssMatrix=None, ssFraction=0.9, gapOpenHelix=None, gapOpenStrand=None, gapOpenOther=None, debug=False): # if 'scoreMatrix', 'similarityMatrix', or 'frequencyMatrix' is # provided, then 'scoreMatch' and 'scoreMismatch' are ignored and # the matrix is used to evaluate matching between the sequences. # 'scoreMatrix' should be a two-dimensional array of size # len(s1) x len(s2). 'similarityMatrix' should be a dictionary # keyed with two-tuples of residue types. 'frequencyMatrix' should # be a list of length s2 of dictionaries, keyed by residue type. # # if 'ssFraction' is not None/False, then 'ssMatrix' should be a 3x3 # matrix keyed with 2-tuples of secondary structure types ('H': helix, # 'S': strand, 'O': other). The score will be a mixture of the # ss/similarity matrix scores weighted by the ssFraction # [ssFraction * ss score + (1 - ssFraction) * similarity score] # # if 'gapOpenHelix/Strand/Other' is not None and 'ssFraction' is not # None/False, then scoreGapOpen is ignored when an intra-helix/ # intra-strand/other gap is opened and the appropriate penalty # is applied instead # # if 'returnSeqs' is True, then instead of returning a match list # (a list of two-tuples) as the second value, a two-tuple of gapped # Sequences will be returned. In both cases, the first return value # is the match score. m = [] bt = [] for i1 in range(len(s1) + 1): m.append((len(s2) + 1) * [0]) bt.append((len(s2) + 1) * [None]) bt[i1][0] = 1 if endsAreGaps and i1 > 0: m[i1][0] = scoreGapOpen + i1 * scoreGap for i2 in range(len(s2) + 1): bt[0][i2] = 2 if endsAreGaps and i2 > 0: m[0][i2] = scoreGapOpen * i2 * scoreGap if similarityMatrix is not None: evaluate = lambda i1, i2: similarityMatrix[(s1[i1], s2[i2])] elif scoreMatrix is not None: evaluate = lambda i1, i2: scoreMatrix[i1][i2] elif frequencyMatrix is not None: evaluate = lambda i1, i2: frequencyMatrix[i2][s1[i1]] else: def evaluate(i1, i2): if s1[i1] == s2[i2]: return scoreMatch return scoreMismatch doingSS = ssFraction is not None and ssFraction is not False \ and ssMatrix is not None if doingSS: prevEval = evaluate simFraction = 1.0 - ssFraction def ssEval(i1, i2): if hasattr(s1, 'ssFreqs'): freqs1 = s1.ssFreqs[i1] else: freqs1 = {s1.ssType(i1): 1.0} if hasattr(s2, 'ssFreqs'): freqs2 = s2.ssFreqs[i2] else: freqs2 = {s2.ssType(i2): 1.0} val = 0.0 for ss1, freq1 in freqs1.items(): if ss1 == None: continue for ss2, freq2 in freqs2.items(): if ss2 == None: continue val += freq1 * freq2 * ssMatrix[(ss1, ss2)] return val evaluate = lambda i1, i2: ssFraction * ssEval(i1, i2) + \ simFraction * prevEval(i1, i2) # precompute appropriate gap-open penalties gapOpen1 = [scoreGapOpen] * (len(s1) + 1) gapOpen2 = [scoreGapOpen] * (len(s2) + 1) if endsAreGaps: if gapOpenOther is not None: gapOpen1[0] = gapOpen2[0] = gapOpenOther else: gapOpen1[0] = gapOpen2[0] = 0 if doingSS and gapOpenOther != None: for seq, gapOpens in [(s1, gapOpen1), (s2, gapOpen2)]: if hasattr(seq, 'gapFreqs'): for i, gapFreq in enumerate(seq.gapFreqs): gapOpens[i+1] = \ gapFreq['H'] * gapOpenHelix + \ gapFreq['S'] * gapOpenStrand + \ gapFreq['O'] * gapOpenOther else: ssTypes = [seq.ssType(i) for i in range(len(seq))] for i, ss in enumerate(ssTypes[:-1]): nextSS = ssTypes[i + 1] if ss == nextSS and ss == 'H': gapOpens[i + 1] = gapOpenHelix elif ss == nextSS and ss == 'S': gapOpens[i + 1] = gapOpenStrand else: gapOpens[i + 1] = gapOpenOther colGapStarts = [0] * len(s2) # don't care about column zero for i1 in range(len(s1)): rowGapPos = 0 for i2 in range(len(s2)): best = m[i1][i2] + evaluate(i1, i2) btType = 0 if i2 + 1 < len(s2) or endsAreGaps: colGapPos = colGapStarts[i2] skipSize = i1 + 1 - colGapPos if hasattr(s1, "occupancy"): totOcc = 0.0 for i in range(colGapPos, i1 + 1): totOcc += s1.occupancy[i] colSkipVal = totOcc * scoreGap else: colSkipVal = skipSize * scoreGap baseColGapVal = m[colGapPos][i2 + 1] + colSkipVal skip = baseColGapVal + gapOpen2[i2 + 1] else: skipSize = 1 colSkipVal = 0 skip = m[i1][i2 + 1] if skip > best: best = skip btType = skipSize if i1 + 1 < len(s1) or endsAreGaps: skipSize = i2 + 1 - rowGapPos if hasattr(s2, "occupancy"): totOcc = 0.0 for i in range(rowGapPos, i2 + 1): totOcc += s2.occupancy[i] rowSkipVal = totOcc * scoreGap else: rowSkipVal = skipSize * scoreGap baseRowGapVal = m[i1 + 1][rowGapPos] + rowSkipVal skip = baseRowGapVal + gapOpen1[i1 + 1] else: skipSize = 1 rowSkipVal = 0 skip = m[i1 + 1][i2] if skip > best: best = skip btType = 0 - skipSize m[i1 + 1][i2 + 1] = best bt[i1 + 1][i2 + 1] = btType if btType >= 0: # not gapping the row if best > baseRowGapVal: rowGapPos = i2 + 1 if btType <= 0: # not gapping the column if best > baseColGapVal: colGapStarts[i2] = i1 + 1 if debug: from chimera.selection import currentResidues cr = currentResidues(asDict=True) if cr: for fileName, matrix in [("scores", m), ("trace", bt)]: out = open("/home/socr/a/pett/rm/" + fileName, "w") print >> out, " ", for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print >> out, "%5d" % i2, print >> out print >> out, " ", for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print >> out, "%5s" % s2[i2], print >> out for i1, r1 in enumerate(s1.residues): if r1 not in cr: continue print >> out, "%3d" % i1, s1[i1], for i2, r2 in enumerate(s2.residues): if r2 not in cr: continue print >> out, "%5g" % (matrix[i1 + 1][i2 + 1]), print >> out out.close() i1 = len(s1) i2 = len(s2) matchList = [] while i1 > 0 and i2 > 0: btType = bt[i1][i2] if btType == 0: matchList.append((i1 - 1, i2 - 1)) i1 = i1 - 1 i2 = i2 - 1 elif btType > 0: i1 = i1 - btType else: i2 = i2 + btType if returnSeqs: return m[len(s1)][len(s2)], matches2gappedSeqs(matchList, s1, s2, gapChar=gapChar) return m[len(s1)][len(s2)], matchList
def lists(level="atom", mode="any", attribute=None): import chimera from chimera import replyobj, selection mode = findBestMatch(mode, ["any", "all"]) level = findBestMatch(level, ["atom", "residue", "chain", "molecule"]) if level == "atom": if attribute is None: attribute = "idatmType" _reportAtoms(selection.currentAtoms(), attribute) elif level == "residue": if mode == "any": residues = selection.currentResidues() else: rMap = {} for a in selection.currentAtoms(): l = rMap.setdefault(a.residue, []) l.append(a) residues = [] for r, aList in rMap.iteritems(): if len(r.atoms) == len(aList): residues.append(r) if attribute is None: attribute = "type" _reportResidues(residues, attribute) elif level == "chain": if mode == "any": chains = selection.currentChains() else: rcMap = {} cached = set([]) cMap = {} for r in selection.currentResidues(): if r.molecule not in cached: cached.add(r.molecule) for seq in r.molecule.sequences(): for res in seq.residues: rcMap[res] = seq try: seq = rcMap[r] except KeyError: pass else: l = cMap.setdefault(seq, []) l.append(r) chains = [] for seq, rList in cMap.iteritems(): if len(seq) == len(rList): chains.append(seq) if attribute is None: attribute = "chain" _reportChains(chains, attribute) elif level == "molecule": if mode == "any": molecules = selection.currentMolecules() else: mMap = {} for a in selection.currentAtoms(): l = mMap.setdefault(a.molecule, []) l.append(a) molecules = [] for m, aList in mMap.iteritems(): if len(m.atoms) == len(aList): molecules.append(m) if attribute is None: attribute = "name" _reportModels(molecules, attribute) else: raise chimera.UserError("\"%s\": unknown listselection level" % level)