def filterBlastHit( self ): """ Extract sequence identity and overlap length from one single bl2seq hit @return: blast results, e.g. {'aln_id':1.0, 'aln_len':120} @rtype: dict """ ## check that the outfut file is there and seems valid if not os.path.exists( self.f_out ): raise Blast2SeqError,\ 'Hmmersearch result file %s does not exist.'%self.f_out if T.fileLength( self.f_out ) < 10: raise Blast2SeqError,\ 'Hmmersearch result file %s seems incomplete.'%self.f_out out = open( self.f_out, 'r' ) hitStr = out.read() out.close() # get rid of line breaks hitStr = hitStr.replace( '\n', '#' ) try: _id = self.ex_identity.match(hitStr) # Blast Identity if (_id == None): return {'res_id':0, 'aln_id':0, 'aln_len':0} res_identical, res_number = int(_id.group(1)), int(_id.group(2)) id = 1.0 * res_identical/res_number return {'res_id':res_identical, 'aln_id':id, 'aln_len':res_number} except: T.errWriteln("Error 1 in filterBlastHit:") T.errWriteln("Hit Parsed: " + hitStr) return {}
def add(self, str): """ Add String str and line break to file. @param str: string to add to pml file @type str: str """ try: self.fgenerate.write(str + '\n') except (IOError): T.errWriteln( "PymolInput.add(): Error adding string to pymol script file.") T.errWriteln(T.lastError())
def add(self, str): """ Add String str and line break to file. @param str: string to add to pml file @type str: str """ try: self.fgenerate.write(str + '\n') except (IOError): T.errWriteln( "PymolInput.add(): Error adding string to pymol script file.") T.errWriteln( T.lastError() )
def go(self, dict): """ Calculate rmsd values for trajectory and plot them. @param dict: dictionary with path to pdb files as keys @type dict: dict @return: dictionary mapping input:output names @rtype: dict """ d = {} print "working on :", for pdbIn, out in dict.items(): print T.stripFilename( pdbIn ) ## set file name for pickled Structure object dir = os.path.dirname( pdbIn ) if self.params['out'] <> None: dir = self.params['out'] out = dir + '/' + T.stripFilename( pdbIn) + '.model' try: m = PDBModel( pdbIn, skipRes=self.params['skipRes'] ) if self.params['amber']: self.renameAmberRes( m ) self.renameToAmberAtoms( m ) if self.params['sort']: m = m.sort() m.saveAs( out ) except: if self.params.has_key('report') and self.params['report']: f = open( out[:-6] + '.error', 'w' ) f.write( T.lastError() ) f.close() else: T.errWriteln("Error" + T.lastError() ) out = '' d[pdbIn] = out print "Done." return d
def go(self, dict): """ Calculate rmsd values for trajectory and plot them. @param dict: dictionary with path to pdb files as keys @type dict: dict @return: dictionary mapping input:output names @rtype: dict """ d = {} print "working on :", for pdbIn, out in dict.items(): print T.stripFilename(pdbIn) ## set file name for pickled Structure object dir = os.path.dirname(pdbIn) if self.params['out'] <> None: dir = self.params['out'] out = dir + '/' + T.stripFilename(pdbIn) + '.model' try: m = PDBModel(pdbIn, skipRes=self.params['skipRes']) if self.params['amber']: self.renameAmberRes(m) self.renameToAmberAtoms(m) if self.params['sort']: m = m.sort() m.saveAs(out) except: if self.params.has_key('report') and self.params['report']: f = open(out[:-6] + '.error', 'w') f.write(T.lastError()) f.close() else: T.errWriteln("Error" + T.lastError()) out = '' d[pdbIn] = out print "Done." return d
def _completeResidues(self, chain): """ Look for missing or unknown atom names, add missing atoms, report unknown atoms. @param chain: Scientific.IO.PDB.PeptideChain object @type chain: object @return: Scientific.IO.PDB.PeptideChain object @rtype: chain object """ chain.deleteHydrogens() ## delete all hydrogens i = 0 self.log.add("Checking atoms of chain " + chain.segment_id) for res in chain: try: if i < len(chain) - 1: # normal residue alowed = self.aminoAcidDict[res.name] else: # c-terminal residue alowed = self._res2Terminal(self.aminoAcidDict[res.name]) name_list = [] for atom in res.atom_list: # check for unknown atom names # store for missing atom check name_list = name_list + [atom.name] if not (atom.name in alowed): self.log.add('\tunknown atom: ' + atom.name + ' : '+\ res.name+ str(res.number)) for name in alowed: # check for missing atoms if not (name in name_list): # add missing atom with 0 xyz self._addMissing(res, name) self.log.add('\tadded missing atom -> '+ name+ ' : '+\ res.name+ str(res.number)) except: s = "\ncompleteResidues(): Error while checking atoms.\n" s = s + "residue " + str(i) + " :" + str(res) + "\n" s = s + T.lastError() T.errWriteln( "Error while completing residues, check log for details.") self.log.add(s) i = i + 1 return chain
def _completeResidues(self, chain): """ Look for missing or unknown atom names, add missing atoms, report unknown atoms. @param chain: Scientific.IO.PDB.PeptideChain object @type chain: object @return: Scientific.IO.PDB.PeptideChain object @rtype: chain object """ chain.deleteHydrogens() ## delete all hydrogens i = 0 self.log.add("Checking atoms of chain "+chain.segment_id) for res in chain: try: if i < len(chain)-1: # normal residue alowed = self.aminoAcidDict[res.name] else: # c-terminal residue alowed = self._res2Terminal(self.aminoAcidDict[res.name]) name_list = [] for atom in res.atom_list: # check for unknown atom names # store for missing atom check name_list = name_list + [atom.name] if not (atom.name in alowed): self.log.add('\tunknown atom: ' + atom.name + ' : '+\ res.name+ str(res.number)) for name in alowed: # check for missing atoms if not (name in name_list): # add missing atom with 0 xyz self._addMissing(res, name) self.log.add('\tadded missing atom -> '+ name+ ' : '+\ res.name+ str(res.number)) except: s = "\ncompleteResidues(): Error while checking atoms.\n" s = s + "residue " + str(i)+ " :"+ str(res) + "\n" s = s + T.lastError() T.errWriteln( "Error while completing residues, check log for details.") self.log.add(s) i = i+1 return chain
def removeTER(self, fname): """ Remove TER record from PDB. @param fname: name of existing file. @type fname: string """ try: #command = 'egrep -v "^TER " ' + fname + '> temp.pdb' path = os.path.dirname(fname) command = 'egrep -v "^TER " %s > %s/temp.pdb' % (fname, path) commands.getstatusoutput(command) os.rename('%s/temp.pdb' % path, fname) except (OSError): T.errWriteln("Error removing 'TER' statement from %s: ") T.errWriteln(T.lastError())
def removeTER(self, fname): """ Remove TER record from PDB. @param fname: name of existing file. @type fname: string """ try: #command = 'egrep -v "^TER " ' + fname + '> temp.pdb' path = os.path.dirname( fname ) command = 'egrep -v "^TER " %s > %s/temp.pdb'%( fname, path ) commands.getstatusoutput(command) os.rename('%s/temp.pdb'%path, fname) except (OSError): T.errWriteln("Error removing 'TER' statement from %s: ") T.errWriteln( T.lastError() )
def setResValues( self, model, values, key='temperature_factor', lastOnly=0 ): """ Add numeric value per residue to all atoms of all Structures or the last Structure in a model. The values will be written to either the B- (temperature_factor) or Q-factor 'occupancy' column in the temporary pdb-file. These values can then be used to display properties in PyMol via commands like 'color_b' and 'color_q'. See also L{setAtomValues}. @param model: model name @type model: str @param values: list of numbers, len( values ) == number of residues @type values: [float] @param key: key for Atom.properties dictionary (default: temperature_factor) @type key: occupancy|temperature_factor @param lastOnly: 0 - add to all in model OR 1 - add only to last Structure (default: 0) @type lastOnly: 1|0 """ if lastOnly: self.dic[ model ][-1].addResProperty( values, key ) else: for m in self.dic[ model ]: try: m.addResProperty( values, key ) except: T.errWriteln( "Warning: error while adding properties.") T.errWriteln( "Key: "+str( key )+" values: "+str( values ) ) T.errWriteln( T.lastError() )
def filterBlastHit(self): """ Extract sequence identity and overlap length from one single bl2seq hit @return: blast results, e.g. {'aln_id':1.0, 'aln_len':120} @rtype: dict """ ## check that the outfut file is there and seems valid if not os.path.exists(self.f_out): raise Blast2SeqError,\ 'Hmmersearch result file %s does not exist.'%self.f_out if T.fileLength(self.f_out) < 10: raise Blast2SeqError,\ 'Hmmersearch result file %s seems incomplete.'%self.f_out out = open(self.f_out, 'r') hitStr = out.read() out.close() # get rid of line breaks hitStr = hitStr.replace('\n', '#') try: _id = self.ex_identity.match(hitStr) # Blast Identity if (_id == None): return {'res_id': 0, 'aln_id': 0, 'aln_len': 0} res_identical, res_number = int(_id.group(1)), int(_id.group(2)) id = 1.0 * res_identical / res_number return { 'res_id': res_identical, 'aln_id': id, 'aln_len': res_number } except: T.errWriteln("Error 1 in filterBlastHit:") T.errWriteln("Hit Parsed: " + hitStr) return {}
def writeChain(self, chain): """ Write single chain as PDB. File name will be segid + '_seg.pdb'. @param chain: Scientific.IO.PDB.PeptideChain object @type chain: chain object """ if (chain <> None): try: fname = self.path + '/' + chain.segment_id + "_seg.PDB" file = self._startPDB(chain, fname) # include comments chain.writeToFile(file) # create PDB file.close() # make sure file is complete! self.removeTER(file.file.file.name) #remove TER record from PDB return 1 # write a chain except (IOError): T.errWriteln("Error writing chain to file %s:" % fname) T.errWriteln( T.lastError() ) return 0 # false, no more chains to write
def setResValues(self, model, values, key='temperature_factor', lastOnly=0): """ Add numeric value per residue to all atoms of all Structures or the last Structure in a model. The values will be written to either the B- (temperature_factor) or Q-factor 'occupancy' column in the temporary pdb-file. These values can then be used to display properties in PyMol via commands like 'color_b' and 'color_q'. See also L{setAtomValues}. @param model: model name @type model: str @param values: list of numbers, len( values ) == number of residues @type values: [float] @param key: key for Atom.properties dictionary (default: temperature_factor) @type key: occupancy|temperature_factor @param lastOnly: 0 - add to all in model OR 1 - add only to last Structure (default: 0) @type lastOnly: 1|0 """ if lastOnly: self.dic[model][-1].addResProperty(values, key) else: for m in self.dic[model]: try: m.addResProperty(values, key) except: T.errWriteln("Warning: error while adding properties.") T.errWriteln("Key: " + str(key) + " values: " + str(values)) T.errWriteln(T.lastError())
def getFluct_local( self, mask=None, border_res=1, left_atoms=['C'], right_atoms=['N'], verbose=1 ): """ Get mean displacement of each atom from it's average position after fitting of each residue to the reference backbone coordinates of itself and selected atoms of neighboring residues to the right and left. @param mask: N_atoms x 1 array of 0||1, atoms for which fluctuation should be calculated @type mask: array @param border_res: number of neighboring residues to use for fitting @type border_res: int @param left_atoms: atoms (names) to use from these neighbore residues @type left_atoms: [str] @param right_atoms: atoms (names) to use from these neighbore residues @type right_atoms: [str] @return: Numpy array ( N_unmasked x 1 ) of float @rtype: array """ if mask is None: mask = N.ones( len( self.frames[0] ), N.int32 ) if verbose: T.errWrite( "rmsd fitting per residue..." ) residues = N.nonzero( self.ref.atom2resMask( mask ) ) ## backbone atoms used for fit fit_atoms_right = N.nonzero( self.ref.mask( right_atoms ) ) fit_atoms_left = N.nonzero( self.ref.mask( left_atoms ) ) ## chain index of each residue rchainMap = N.take( self.ref.chainMap(), self.ref.resIndex() ) result = [] for res in residues: i_res, i_border = self.__resWindow(res, border_res, rchainMap, fit_atoms_left, fit_atoms_right) try: if not len( i_res ): raise PDBError, 'empty residue' t_res = self.takeAtoms( i_res + i_border ) i_center = range( len( i_res ) ) mask_BB = t_res.ref.maskBB() * t_res.ref.maskHeavy() ## fit with border atoms .. t_res.fit( ref=t_res.ref, mask=mask_BB, verbose=0 ) ## .. but calculate only with center residue atoms frames = N.take( t_res.frames, i_center, 1 ) avg = N.average( frames ) rmsd = N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2) )) result.extend( rmsd ) if verbose: T.errWrite('#') except ZeroDivisionError: result.extend( N.zeros( len(i_res), N.Float32 ) ) T.errWrite('?' + str( res )) if verbose: T.errWriteln( "done" ) return result
def getFluct_local(self, mask=None, border_res=1, left_atoms=['C'], right_atoms=['N'], verbose=1): """ Get mean displacement of each atom from it's average position after fitting of each residue to the reference backbone coordinates of itself and selected atoms of neighboring residues to the right and left. @param mask: N_atoms x 1 array of 0||1, atoms for which fluctuation should be calculated @type mask: array @param border_res: number of neighboring residues to use for fitting @type border_res: int @param left_atoms: atoms (names) to use from these neighbore residues @type left_atoms: [str] @param right_atoms: atoms (names) to use from these neighbore residues @type right_atoms: [str] @return: Numpy array ( N_unmasked x 1 ) of float @rtype: array """ if mask is None: mask = N.ones(len(self.frames[0]), N.int32) if verbose: T.errWrite("rmsd fitting per residue...") residues = N.nonzero(self.ref.atom2resMask(mask)) ## backbone atoms used for fit fit_atoms_right = N.nonzero(self.ref.mask(right_atoms)) fit_atoms_left = N.nonzero(self.ref.mask(left_atoms)) ## chain index of each residue rchainMap = N.take(self.ref.chainMap(), self.ref.resIndex()) result = [] for res in residues: i_res, i_border = self.__resWindow(res, border_res, rchainMap, fit_atoms_left, fit_atoms_right) try: if not len(i_res): raise PDBError, 'empty residue' t_res = self.takeAtoms(i_res + i_border) i_center = range(len(i_res)) mask_BB = t_res.ref.maskBB() * t_res.ref.maskHeavy() ## fit with border atoms .. t_res.fit(ref=t_res.ref, mask=mask_BB, verbose=0) ## .. but calculate only with center residue atoms frames = N.take(t_res.frames, i_center, 1) avg = N.average(frames) rmsd = N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2))) result.extend(rmsd) if verbose: T.errWrite('#') except ZeroDivisionError: result.extend(N.zeros(len(i_res), N.Float32)) T.errWrite('?' + str(res)) if verbose: T.errWriteln("done") return result