def replace_bfactors(self,infile,outfile,options,check_valid=False): """ Writes the .vol file 'infile' to 'outfile', but replaces the bfactor column with either packing density or volume. Also adds a short remark before the first atom. """ extralines_pd="""REMARK REMARK COLUMNS MODIFIED FROM ORIGINAL PDB FILE: REMARK PD - LOCAL PACKING DENSITY REMARK VDWVOL - VOLUME INSIDE VAN-DER-WAALS SPHERE REMARK SEVOL - VOLUME IN 1.4 ANGSTROM LAYER OUTSIDE VDW-SPHERE REMARK BUR - INDICATES BURIED ATOMS (1=BURIED, 0=SURFACE) REMARK REMARK AT# ELEM RES C RES# X Y Z PD PD VDWVOL SEVOL BUR """ extralines_vol="""REMARK REMARK COLUMNS MODIFIED FROM ORIGINAL PDB FILE: REMARK VOL - TOTAL ATOMIC VOLUME REMARK VDWVOL - VOLUME INSIDE VAN-DER-WAALS SPHERE REMARK SEVOL - VOLUME IN 1.4 ANGSTROM LAYER OUTSIDE VDW-SPHERE REMARK BUR - INDICATES BURIED ATOMS (1=BURIED, 0=SURFACE) REMARK REMARK AT# ELEM RES C RES# X Y Z VOL VOL VDWVOL SEVOL BUR """ extralines_z="""REMARK REMARK COLUMNS MODIFIED FROM ORIGINAL PDB FILE: REMARK Z - Z-SCORE REMARK VDWVOL - VOLUME INSIDE VAN-DER-WAALS SPHERE REMARK SEVOL - VOLUME IN 1.4 ANGSTROM LAYER OUTSIDE VDW-SPHERE REMARK BUR - INDICATES BURIED ATOMS (1=BURIED, 0=SURFACE) REMARK REMARK AT# ELEM RES C RES# X Y Z Z Z VDWVOL SEVOL BUR """ f_atom = 0 out = "" mode = options['bfactor'] if mode == 'zscore': z = ZScore(AveragePacking(options)) for l in open(infile).readlines(): if re.search('(^ATOM)|(^HETATM)',l): if not f_atom: f_atom = 1 if mode == 'packing_density': out += extralines_pd elif mode == 'volume': out += extralines_vol elif mode == 'zscore': out += extralines_z # extract volume data atom = self.parse_atom(l,options) if check_valid and not atom['valid']: continue if mode == 'packing_density': value = atom['packing_density'] elif mode == 'zscore': value = z.get_z_score(atom,options) if not value: value = -1 else: value = atom['total_volume'] newline = l[:55] + "%5.2f %5.2f"%(value,value) + l[67:] out += newline else: out += l open(outfile,'w').writelines(out+'\n')
def write_pdb_file(self,filename,options): """ Writes a pdb file containing all valid atoms. The bfactor option may be set to 'packdens' - substitutes bfactor with packing density. 'volume' - same with total atomic volume 'minuspackdens' - same with 1-packing density 'zscore' - z-score (normalized difference to reference value) """ bfactor = options['bfactor'] out = [] # z-score calculation only needed when the b-factor is to # be replaced with the z-score # actually, the expensive part is not the ZScore initialization # but the AveragePacking initialization if bfactor == 'zscore': zsc = ZScore(AveragePacking(options)) for i in range(len(self.atoms)): if not self.valid[i]:continue atom = self.get_atom(i) pl = atom['pdb_line'] # now get the value to be inserted as bfactor # # For some programs they need to be normalized # # Hallo Fiete! # # unter 'packdens' kannst Du dich austoben. # Habe ein Beispiel reingeschrieben, # in Python sollte etwas Mathe nicht so schwer sein. # Aber frag mich trotzdem falls Du im Code versumpfst. # 11.7.06 KR # if bfactor == 'packdens': value = atom['packing_density'] # normalize values from 0.4 to 0.9 to bfactors between 0.0 and 30. # 'packdens' vmin = 0.4 vmax = 0.9 bmin = 0.0 bmax = 30.0 value = ((value-vmin)/vmax) * (bmax-bmin) + bmin # if bfactor == 'minuspackdens': value = 1-atom['packing_density'] # normalize values from 0.4 to 0.9 to bfactors between 30.0 and 0.0 # 'minuspackdens' vmin = 0.4 vmax = 0.9 bmin = 0.0 bmax = 30.0 value = ((value-vmin)/vmax) * (bmax-bmin) + bmin # elif bfactor == 'volume': value = atom['total_volume'] # nomalize # (insert code on demand) elif bfactor == 'zscore': value = zsc.get_z_score(atom,options) if not value: value=100.0 # normalize # (insert code on demand) pl = pl[:60]+"%6.2f"%(value)+pl[66:] out.append(pl+'\n') open(filename,'w').writelines(out)