def parse(self): """ This function read a file and create the corresponding data hierarchy. """ self.readFile() #molList = [] molList = ProteinSet() if self.allLines is None: return elif len(self.allLines)!=0: self.getKeysAndLinesIndices() else: print "The file %s is empty"%self.filename return molList if not self.keysAndLinesIndices.has_key("@<TRIPOS>ATOM"): print "The file %s doesn't have Atom records, molecules can't be built"%self.filename return molList if self.keysAndLinesIndices.has_key('@<TRIPOS>SUBSTRUCTURE'): self.parse_MOL2_Substructure(self.allLines [self.keysAndLinesIndices ['@<TRIPOS>SUBSTRUCTURE'][0]: self.keysAndLinesIndices ['@<TRIPOS>SUBSTRUCTURE'][1]]) molList.append(self.mol) else: atmlines = map(string.split, self.allLines [self.keysAndLinesIndices ['@<TRIPOS>ATOM'][0]: self.keysAndLinesIndices ['@<TRIPOS>ATOM'][1]]) self.build4LevelsTree({},atmlines) ## self.build2LevelsTree(map(string.split, self.allLines ## [self.keysAndLinesIndices ## ['@<TRIPOS>ATOM'][0]: ## self.keysAndLinesIndices ## ['@<TRIPOS>ATOM'][1]])) molList.append(self.mol) if self.keysAndLinesIndices.has_key('@<TRIPOS>BOND'): self.parse_MOL2_Bonds(self.allLines [self.keysAndLinesIndices ['@<TRIPOS>BOND'][0]: self.keysAndLinesIndices['@<TRIPOS>BOND'] [1]]) if self.keysAndLinesIndices.has_key('@<TRIPOS>SET'): self.parse_MOL2_Sets(self.keysAndLinesIndices['@<TRIPOS>SET']) return molList
def addModelToMolecules(self, listOfMol): length = len(listOfMol) for i in xrange(length): listOfMol[i].model = ProteinSet() for j in xrange(length): if listOfMol[i] != listOfMol[j]: listOfMol[i].model.append(listOfMol[j])
def guiCallback(self): self.selection = self.vf.getSelection().copy() if len(self.selection) == 0 : return ##FIX THIS WHEN MolKit gets straightened out: self.level = self.nameDict[self.vf.selectionLevel] if isinstance(self.selection, MoleculeSet): self.selection = ProteinSet(self.selection) itemName = self.selection[0].__class__.__name__ self.updateChooser() # Update the level if the form exists already. if self.cmdForms.has_key('default'): descr = self.cmdForms['default'].descr levelwid = descr.entryByName['level']['widget'] oldlevel =levelwid.getcurselection() #if oldlevel != self.nameDict[self.vf.ICmdCaller.level.value]: # levelwid.invoke(self.nameDict[self.vf.ICmdCaller.level.value]) if oldlevel != self.level: levelwid.invoke(self.level) val = self.showForm('default', modal=0, blocking=1) if self.cmdForms.has_key('chooser'): self.cmdForms['chooser'].withdraw() if not val: return val['textcolor'] = self.textColor lLabelsGeom = self.getRelatedLabelsGeom() lLabelsState = lLabelsGeom.getSubClassState() lLabelsState.pop('labels') # appart from the labels themselves we want everything val['font'] = lLabelsState if not val['location']: val['location'] = 'center' else: val['location'] = val['location'][0] if not val['format'] or val['format'] == ('None',): val['format'] = None else: val['format'] = val['format'][0] if val['display']=='label': val['only']= 0 val['negate'] = 0 del val['display'] elif val['display']=='label only': val['only']= 1 val['negate'] = 0 del val['display'] elif val['display']== 'unlabel': val['negate'] = 1 val['only'] = 0 del val['display'] val['redraw'] = 1 if val.has_key('level'): del val['level'] if self.lastGeomOwnGuiShown is not None: self.lastGeomOwnGuiShown.hideOwnGui() #apply(self.doitWrapper, (self.vf.getSelection(),), val) apply(self.doitWrapper, (self.selection,), val)
def test_select_intersect_mols_empty(self): """ test selecting with stringRepr of empty intersect mols """ stringSel = CompoundStringSelector() diff_mols = self.mols1 & self.mols2 selString = diff_mols.stringRepr selected, msg = stringSel.select(self.mols, selString, returnMsg=True) self.assertEquals(selected, diff_mols) self.assertEquals(selString, "stringSel/&/protease") self.assertEquals(selected, ProteinSet())
def test_merge(): # merge mol2 into mol1. from MolKit import Read mol1 = Read("./Data/protease.pdb")[0] mol2 = Read("./Data/indinavir.pdb")[0] from MolKit.protein import ProteinSet pset = ProteinSet([mol1, mol2]) chains = pset.chains[:] mol1.merge(mol2) mol1.chains.sort() chains.sort() assert mol1.chains == chains
def setroot(self, atom): """ setroot to 'C11' or 'hsg1:A:ILE2:CA' """ if type(atom) == bytes: if find(atom, ':') > -1: #have to use full_name list #check that it is an atom mols = ProteinSet([self]) nodes = mols.NodesFromName(atom) if not nodes: return 'invalid root name' if nodes.__class__ != AtomSet: return 'invalid root name: not atom level' else: nodes = self.allAtoms.get(lambda x, n=atom: x.name == n) if not nodes: return 'invalid root name' if len(nodes) > 1: return 'root name must be unique' atom = nodes[0] #elif type(atom)!=types.InstanceType: elif isInstance(atom) is False: return atom, ' invalid root atom' elif atom.__class__ != Atom: return atom, ' can only select an Atom instance as root' #fix this rnum0 stuff #in case toggling back and forth if hasattr(self, 'autoRoot') and hasattr(self.autoRoot, 'rnum0'): delattr(self.autoRoot, 'rnum0') #if there is an old root, remove rnum0 if hasattr(self, 'ROOT') and hasattr(self.ROOT, 'rnum0'): delattr(self.ROOT, 'rnum0') self.ROOT = atom self.ROOT.rnum0 = 0 return self.ROOT
def parse(self): """ This function read a file and create the corresponding data hierarchy. """ self.readFile() #molList = [] molList = ProteinSet() if self.allLines is None: return elif len(self.allLines) != 0: self.getKeysAndLinesIndices() else: print("The file %s is empty" % self.filename) return molList if "@<TRIPOS>ATOM" not in self.keysAndLinesIndices: print( "The file %s doesn't have Atom records, molecules can't be built" % self.filename) return molList if '@<TRIPOS>SUBSTRUCTURE' in self.keysAndLinesIndices: self.parse_MOL2_Substructure(self.allLines[ self.keysAndLinesIndices['@<TRIPOS>SUBSTRUCTURE'][0]:self. keysAndLinesIndices['@<TRIPOS>SUBSTRUCTURE'][1]]) molList.append(self.mol) else: atmlines = list( map( string.split, self. allLines[self.keysAndLinesIndices['@<TRIPOS>ATOM'][0]:self. keysAndLinesIndices['@<TRIPOS>ATOM'][1]])) self.build4LevelsTree({}, atmlines) ## self.build2LevelsTree(map(string.split, self.allLines ## [self.keysAndLinesIndices ## ['@<TRIPOS>ATOM'][0]: ## self.keysAndLinesIndices ## ['@<TRIPOS>ATOM'][1]])) molList.append(self.mol) if '@<TRIPOS>BOND' in self.keysAndLinesIndices: self.parse_MOL2_Bonds( self.allLines[self.keysAndLinesIndices['@<TRIPOS>BOND'][0]:self .keysAndLinesIndices['@<TRIPOS>BOND'][1]]) if '@<TRIPOS>SET' in self.keysAndLinesIndices: self.parse_MOL2_Sets(self.keysAndLinesIndices['@<TRIPOS>SET']) return molList
all_res += res if verbose: print(" added ", res.name, " to ", all_res) else: print("WARNING: no residue named " + n) else: # '1JFF_protein:A:THR179_GLU183,1JFF_protein:B:TRP21_ARG64_LEU275' chainStart = res_names_by_chain.index(':') + 1 molName = res_names_by_chain[:chainStart - 1] #if verbose: print "molName = ", molName, " chainStart=", chainStart n = res_names_by_chain[chainStart:].replace("_", ",") #if verbose: print "after comma replaced '_', n is ", n, '\n' selStr = molName + ":" + n #if verbose: print "now selStr", selStr #eg: 'hsg1:A:ARG8,ILE82' result, msg = CompoundStringSelector().select( ProteinSet([rec]), selStr) if verbose: print("prepare_flexreceptor4 line 157: result=", result) if result.__class__ != ResidueSet: print(residues_to_move, " is not a ResidueSet instead it is a ", result.__class__) #if verbose: print selStr, " selection =", res, " msg=", msg, '\n' resS = ResidueSet() if result.__class__ == AtomSet: resS = result.parent.uniq() else: resS = result if len(resS): all_res += resS else:
for residue in res: all_res += res if verbose: print " added ", res.name, " to ", all_res else: print "WARNING: no residue named " + n else: # '1JFF_protein:A:THR179_GLU183,1JFF_protein:B:TRP21_ARG64_LEU275' chainStart = res_names_by_chain.index(':') + 1 molName = res_names_by_chain[:chainStart-1] if verbose: print "molName = ", molName, " chainStart=", chainStart n = res_names_by_chain[chainStart:].replace("_", ",") if verbose: print "after comma replaced '_', n is ", n, '\n' selStr = molName + ":" + n if verbose: print "now selStr", selStr #eg: 'hsg1:A:ARG8,ILE82' result, msg = CompoundStringSelector().select(ProteinSet([rec]), selStr) if verbose: print "prepare_flexreceptor4 line 157: result=", result if result.__class__!= ResidueSet: print residues_to_move, " is not a ResidueSet instead it is a ", result.__class__ #if verbose: print selStr, " selection =", res, " msg=", msg, '\n' resS = ResidueSet() if result.__class__ == AtomSet: resS = result.parent.uniq() else: resS = result if len(resS): all_res += resS else: print "no residue found using string ", selStr #if verbose: print "built all_res=", all_res.full_name() #check for duplicates
def go(self): msgStr = None if self.moleculeSet: self.molSet = self.getMolecules(self.moleculeSet, self.selList[0]) else: self.molSet = None return [] # return [], msgStr # SPLIT here if mol.children==Atoms # possibly: self.Mols4levels=filter(lambda x: x.childrenSetClass == ChainSet, self.molSet) # then process the others separately....????????? # eg self.Mols2levels=filter(lambda x: x.childrenSetClass == AtomSet, self.molSet) # self.Mols2levels would get fed to self.getAtoms and two results ???merged??? if not self.molSet: self.chainSet = None msgStr = str(self.selList[0]) + " selected no molecules" return [] # return [], msgStr noChainMols = MoleculeSet( [x for x in self.molSet if Chain not in x.levels]) haveChainMols = MoleculeSet( [x for x in self.molSet if Chain in x.levels]) # build the residues belonging to molecules w/ no Chains (!WEIRD!) ncrs = ResidueSet() for item in noChainMols: if Residue in item.levels: itemRes = item.allAtoms.parent.uniq() ncrs = ncrs + itemRes if ncrs: noChainResSet = self.getResidues(ncrs, self.selList[2]) if not noChainResSet: noChainResSet = ResidueSet() else: noChainResSet = ResidueSet() if len(haveChainMols): self.chainSet = self.getChains(haveChainMols.findType(Chain), self.selList[1]) if self.chainSet: haveChainResSet = self.getResidues(self.chainSet.findType(Residue), self.selList[2]) # also test noChainMols for residues if haveChainResSet: self.resSet = haveChainResSet + noChainResSet else: self.resSet = noChainResSet else: self.resSet = noChainResSet ##don't return unless selList[2]!==[''] if self.selList[1] != ['']: msgStr = str(self.selList[1]) + " selected no chains" return [] # return [], msgStr # now: if self.selList for Chain and Residue level was empty, get the Atoms from noChains if self.selList[1] == [''] and self.selList[2] == ['']: tla = AtomSet() for item in noChainMols: if Residue not in item.levels: tla = tla + item.allAtoms twoLevelAtoms = tla else: twoLevelAtoms = AtomSet() if self.resSet: resAts = self.resSet.findType(Atom) if twoLevelAtoms: resAts = resAts + twoLevelAtoms self.atomSet = self.getAtoms(resAts, self.selList[3]) else: if self.selList[2] != ['']: msgStr = str(self.selList[2]) + " selected no residues" return [] # return [], msgStr else: self.atomSet = self.getAtoms(twoLevelAtoms, self.selList[3]) selNodes = self.atomSet # find correct levelType to return # need to split atomSet into two parts: if self.atomSet: haveChainAtoms = AtomSet( [x for x in self.atomSet if x.top != x.parent]) haveNoChainAtoms = self.atomSet - haveChainAtoms if self.selList[3] == ['']: # change atoms to residues if haveChainAtoms: selNodes = haveChainAtoms.parent.uniq() else: selNodes = ResidueSet() if self.selList[2] == ['']: # change residues to chains if len(selNodes): selNodes = selNodes.parent.uniq() if self.selList[1] == ['']: # change chains to molecules if haveNoChainAtoms: noChainTops = haveNoChainAtoms.top.uniq() else: noChainTops = ProteinSet() if selNodes: selTops = selNodes.top.uniq() else: selTops = ProteinSet() selNodes = selTops + noChainTops if self.selList[0] == ['']: # change molecules to molecules(?)in the case of no strs if selNodes.__class__ != MoleculeSet: selNodes = MoleculeSet(selNodes.top.uniq()) else: msgStr = str(self.selList[3]) + " selected no atoms" for item in ['moleculeSet', 'molSet', 'chainSet', 'resSet', 'atomSet']: if hasattr(self, item): delattr(self, item) # exec('del self.'+item) return selNodes
def select(self, nodes, selectionString, sets=None, caseSensitive=True, escapeCharacters=False): # print "in select with selectionString=", selectionString overallresults = None overallmsg = "" overall_class = None # eg: stringSel:A:PRO1:N;stringSel:B:PRO1:CA; if len(selectionString) and selectionString[-1] == ';': selectionString = selectionString[:-1] # eg: stringSel:A:PRO1:N;stringSel:B:PRO1:CA ## mol1;mol2;mol3::: allSelectionStrings = selectionString.split(';') # print "allSelectionStrings=", allSelectionStrings all_tops = nodes.top.uniq() final_str_repr = "" # keep track of string to assign at end for selString in allSelectionStrings: # print "processing selString=", selString lambda_index = selString.find('lambda ') if lambda_index == -1: setsStrings = selString.split(':') else: # split repair possible splits of lambda expressions # ":::lambda x:x.top.name=='ind'" # lambda_index = 3 setsStrings = selString[:lambda_index].split(':') # setsStrings = ['','','',''] # replace the last one with join of split on lambda ':' lambda_exp_list = selString[lambda_index:].split(':') lambda_exp = lambda_exp_list[0] + ':' + lambda_exp_list[1] setsStrings[-1] = lambda_exp if len(lambda_exp_list) > 2: # there may be trailing stuff setsStrings.extend(lambda_exp_list[2:]) # setsStrings = selString.split(':') # print "setsStrings=", setsStrings len_setsStrings = len(setsStrings) results = all_tops msg = '' for i in range(len_setsStrings): # print "setsStrings[",i,"]=", setsStrings[i] if i == 0 and len_setsStrings == 1 and setsStrings[i] == '': # special case of empty selection string final_str_repr = '' for m in all_tops: final_str_repr += m.name + ',' final_str_repr = final_str_repr[:-1] elif setsStrings[i] != '': # print "in elif loop" these_sets = None if sets is not None: these_sets = sets.get(stype=results.__class__) results, msgList = results.get( setsStrings[i], sets=these_sets, caseSensitive=caseSensitive, escapeCharacters=escapeCharacters, returnMsg=True) # print "results=", results, "msgList=", msgList for m in msgList: setsStrings[i].replace(m, '') msg = msg + str(m) if len(results) == 0: # print "no results with ", setsStrings[i] overallresults = None break # check whether the rest of the setsStrings are empty, if so final_str_repr += setsStrings[i] + ':' results.stringRepr = final_str_repr if i < len(setsStrings) - 1: results = results.children final_str_repr = final_str_repr[:-1] results.stringRepr = final_str_repr if len(results): if overallresults is None: overallresults = results overall_class = results.__class__ overallmsg = msg else: # levels are the same if overall_class == results.__class__: overallresults = overallresults + results # does this happen automatically? overallmsg = overallmsg + msg else: print("ERROR") print("overall_class->", overall_class) print("results.__class__->", results.__class__) print("results=", results) raise RuntimeError if selString != allSelectionStrings[-1]: final_str_repr += ';' final_str_repr.replace("/+/", ';') else: overallresults.stringRepr = final_str_repr if overallresults is None: overallmsg = selectionString if len(setsStrings) == 1: overallresults = ProteinSet() elif len(setsStrings) == 2: overallresults = ChainSet() elif len(setsStrings) == 3: overallresults = ResidueSet() else: overallresults = AtomSet() return overallresults, overallmsg
l = Read(ligand_filename)[0] l.buildBondsByDistance() if verbose: print 'read ', ligand_filename r = Read(receptor_filename)[0] r.buildBondsByDistance() if verbose: print 'read ', receptor_filename all_res = ResidueSet() res_names = residues_to_move.split('_') for n in res_names: if n.find(':') == -1: res = r.chains.residues.get(lambda x: x.name == n) all_res += res if verbose: print "get: adding ", res.name, " to ", all_res else: res, msg = CompoundStringSelector().select(ProteinSet([r]), n) all_res += res if verbose: print "css: adding ", res.name, " to ", all_res if verbose: print "all_res=", all_res.full_name( ), 'all_res.__class__=', all_res.__class__ #?check for duplicates d = {} for res in all_res: d[res] = 1 all_res = d.keys() all_res = ResidueSet(all_res) #inactivate specified bonds #disallowed_Pairs "CA_CB:CB_CG:C_CA" all_bnds = BondSet()