def _evaluate(f, models): if f[0] is SIMPLE: ks = knownSelection(f[1], models) if ks is not None: if ks == 1: sel = selection.copyCurrent() elif isinstance(ks, basestring): sel = selection.OSLSelection(ks, models) else: sel = ks else: sel = selection.OSLSelection(f[1], models) elif f[0] is ZONE: sel = zone(_evaluate(f[1], chimera.openModels.list()), f[2], f[3], f[4], models) elif f[0] is UNION: left = _evaluate(f[1], models) right = _evaluate(f[2], models) sel = selection.ItemizedSelection() sel.merge(selection.REPLACE, left) sel.merge(selection.EXTEND, right) elif f[0] is INTERSECT: left = _evaluate(f[1], models) right = _evaluate(f[2], left.graphs()) sel = selection.ItemizedSelection() sel.merge(selection.REPLACE, left) sel.merge(selection.INTERSECT, right) elif f[0] is COMPLEMENT: sel = selection.ItemizedSelection() sel.add(models) operand = _evaluate(f[1], models) sel.merge(selection.REMOVE, operand) else: raise ValueError, 'Unknown specifier function: %s' % str(f[0]) return sel
def min_distance(spec1, spec2): s1 = selection.OSLSelection(spec1) # Select the protein s2 = selection.OSLSelection(spec2) # Select the drug dist = [] for a1 in s1.atoms(): for a2 in s2.atoms(): d = a1.xformCoord().distance(a2.xformCoord( )) # Get the distance from atom 1 in protein to atom 2 in drug dist.append((d, a1, a2)) dmin, a1, a2 = min(dist) return dmin, a1, a2
def _processSpec(spec, defModel): if spec[0] not in "#:@": spec = ":" + spec sel = selection.OSLSelection(spec) atoms = sel.atoms() if len(atoms) == 1: return atoms[0] if defModel and spec[0] != "#": spec = "#" + defModel + spec sel = selection.OSLSelection(spec) atoms = sel.atoms() if len(atoms) == 1: return atoms[0] return None
def analysisAtoms(movie, useSel, ignoreBulk, ignoreHyds): mol = movie.model.Molecule() if useSel: selAtoms = selection.currentAtoms() if selAtoms: # reduce to just ours sel1 = selection.ItemizedSelection() sel1.add(selAtoms) sel2 = selection.ItemizedSelection() sel2.add(mol.atoms) sel1.merge(selection.INTERSECT, sel2) atoms = sel1.atoms() if not atoms: raise UserError("No selected atoms in" " trajectory!") else: atoms = mol.atoms else: atoms = mol.atoms if ignoreBulk: bulkSel = selection.OSLSelection("@/surfaceCategory=" "solvent or surfaceCategory=ions") atomSel = selection.ItemizedSelection() atomSel.add(atoms) atomSel.merge(selection.REMOVE, bulkSel) atoms = atomSel.atoms() if not atoms: raise UserError("No atoms remaining after ignoring" " solvent/ions") if ignoreHyds: atoms = [a for a in atoms if a.element.number != 1] if not atoms: raise UserError("No atoms remaining after ignoring" " hydrogens") return atoms
def selectionFromText(self, enumText, models=None): if enumText[0] in OSL_START_CHAR and '\n' not in enumText: sel = selection.ItemizedSelection() sel.merge(selection.REPLACE, selection.OSLSelection(enumText)) sel.addImplied(vertices=0) return sel else: return CodeItemizedSelection(enumText, models=models)
def _getAtoms(self, m): if not self.subSelection: atoms = m.atoms else: osl = '%s%s' % (m.oslIdent(), self.subSelection) s = selection.OSLSelection(osl) wanted = s.vertices(asDict=True) atoms = [a for a in m.atoms if a in wanted] return atoms
def oslLookup(osl, bondAsAtomPair=0): global _oslItemMap if _oslItemMap.has_key(osl): return _oslItemMap[osl] if isinstance(osl, tuple): #pseudobond pbOsl = osl category, osl = pbOsl a1, a2 = oslLookup(osl, bondAsAtomPair=1) pb = a1.associations(category, a2)[0] _oslItemMap[pbOsl] = pb return pb if osl.count('@') == 2: #bond if osl.count('#') == 2: mol2 = osl.rindex('#') osl1 = osl[:mol2] osl2 = osl[mol2:] elif osl.count(':') == 2: res2 = osl.rindex(':') osl1 = osl[:res2] res1 = osl.index(':') osl2 = osl[:res1] + osl[res2:] else: at2 = osl.rindex('@') osl1 = osl[:at2] at1 = osl.index('@') osl2 = osl[:at1] + osl[at2:] a1 = oslLookup(osl1) a2 = oslLookup(osl2) if bondAsAtomPair: return a1, a2 bond = a1.bondsMap[a2] _oslItemMap[osl] = bond return bond if ':' in osl: resIndex = osl.index(':') molOSL = osl[:resIndex] remainder = osl[resIndex:] else: molOSL = osl remainder = "" try: mappedOSL = _oslMap[molOSL] + remainder except KeyError: raise ValueError, "OSL for non-existent model" sel = selection.OSLSelection(mappedOSL) if '@' in mappedOSL: items = sel.atoms() elif ':' in mappedOSL: items = sel.residues() else: items = sel.molecules() if len(items) > 1: raise LookupError, "non-unique OSL" else: item = items[0] _oslItemMap[osl] = item return item
def __init__(self, refModels, altModels, subSelection, *args, **kw): # # Save the subselection (part of model to match) # as well as the model lists (ordered by subid) # self.subSelection = subSelection l = [(m.subid, m) for m in refModels] l.sort() self.refModels = [t[1] for t in l] l = [(m.subid, m) for m in altModels] l.sort() self.altModels = [t[1] for t in l] # # Grab the atomic coordinates that will be used # to compute RMSDs and check for errors # self.atomDict = {} counts = [] for ml in self.refModels, self.altModels: for m in ml: osl = '%s%s' % (m.oslIdent(), self.subSelection) s = selection.OSLSelection(osl) atoms = s.atoms() self.atomDict[m] = match._coordArray(atoms) counts.append(len(atoms)) if min(counts) != max(counts): raise chimera.UserError("Unequal number of atoms " "found in models") if counts[0] == 0: raise chimera.UserError("No atoms match atom specifier") # # Add handler to monitor change in display or active # status changes # self.trigger = chimera.triggers.addHandler('check for changes', self._statusCheck, None) # # Initialize dialog # ModelessDialog.__init__(self, *args, **kw) # # Register with extension manager # chimera.extension.manager.registerInstance(self)
def getLastResidue(after_res): ## get a reference to the residue specified by the ## after_res argument, and make sure it is the last ## one in the chain from chimera import selection sel_obj = selection.OSLSelection(after_res) sel_res = sel_obj.residues() num_res_spec = len(sel_res) if num_res_spec == 0: raise AddAAError, "Couldn't find valid residue based on atom spec \"%s\"" % after_res if num_res_spec > 1: raise AddAAError, "Too many residues specified: " \ "%s specifies %d residues" \ % (after_res, num_res_spec) ## last_res represents the last residue of the chain ## (i.e. the one we're adding on to!) last_res = sel_res[0] if not isLastRes(last_res): raise AddAAError, "Can only add to last residue!" return last_res
from chimera import selection from chimera import runCommand as rc from collections import OrderedDict import csv sel1 = selection.OSLSelection('#0') sel2 = selection.OSLSelection('#1') protein_residues_length = len( sel1.residues()) # Get the number of residues in protein drug_residues_length = len( sel2.residues()) # Get the number of drugs loaded in def min_distance(spec1, spec2): s1 = selection.OSLSelection(spec1) # Select the protein s2 = selection.OSLSelection(spec2) # Select the drug dist = [] for a1 in s1.atoms(): for a2 in s2.atoms(): d = a1.xformCoord().distance(a2.xformCoord( )) # Get the distance from atom 1 in protein to atom 2 in drug dist.append((d, a1, a2)) dmin, a1, a2 = min(dist) return dmin, a1, a2 for j in range(1, drug_residues_length + 1): for i in range(1, protein_residues_length + 1): d, a1, a2 = min_distance('#0:' + str(i), '#1.' + str(j)) print('Minimum distance: ' + str(d) + ' between ' +
import chimera from chimera import selection from chimera import runCommand as rc from collections import OrderedDict import csv sel1 = selection.OSLSelection('#0') sel2 = selection.OSLSelection('#1') protein_residues_length = len( sel1.residues()) # Get the number of residues in protein drug_residues_length = len( sel2.residues()) # Get the number of drugs loaded in models = chimera.openModels.list() def min_distance(spec1, spec2): s1 = selection.OSLSelection(spec1) # Select the protein s2 = selection.OSLSelection(spec2) # Select the drug dist = [] for a1 in s1.atoms(): for a2 in s2.atoms(): d = a1.xformCoord().distance(a2.xformCoord( )) # Get the distance from atom 1 in protein to atom 2 in drug dist.append((d, a1, a2)) dmin, a1, a2 = min(dist) return dmin, a1, a2 distance_dict = OrderedDict() drug_name = []
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