def write_map(self, filename='converted.map'): if(self.map is None): hex=False self.zga = math.zernike_grid( self.np_on_grid, self.nmax, hex ) self.zga.load_coefs( self.nlm_array.nlm(), self.nlm_array.coefs() ) self.map = flex.abs( self.zga.f() ) xplor_map_type( self.map, self.np_on_grid, self.rmax, file_name=filename )
def __init__(self, data, xplor_file, rmax, qmax=0.15, nmax=20, np_on_grid=30, prefix='prefix', splat_range=1, n_trial=5, fraction=0.9, pdb_nlm=None, nbr_dist=2): self.raw_data = data self.rmax = rmax / fraction self.fraction = fraction self.qmax = qmax self.nmax = nmax self.nbr_dist = nbr_dist self.pdb_nlm = pdb_nlm self.np_on_grid = np_on_grid self.zga = math.zernike_grid(self.np_on_grid, self.nmax, False) self.n = self.np_on_grid * 2 + 1 self.n2 = self.n**2 self.n3 = self.n2 * self.n self.prefix = prefix + '_' self.splat_range = splat_range self.start_model = None neighbors = [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)] self.neighbors = flex.int() for n in neighbors: self.neighbors.append(self.convert_indx_3_1(n)) self.build_nbr_list(self.nbr_dist) print "neighbor distance: ", self.nbr_dist self.build_sphere_list() #### Labels for different regions #### self.solvent_label = 0 self.molecule_label = 1 self.surface_label = 2 self.nlm_array = math.nlm_array(nmax) self.nlm = self.nlm_array.nlm() self.counter = 0 self.n_accept = 0 self.bandwidth = min(smath.pi / rmax / 2.0, 0.01) # self.data = reduce_raw_data(self.raw_data,self.qmax, self.bandwidth,level=0.00001 ) self.data = self.raw_data self.data.i = self.data.i / self.data.i[0] self.data.s = self.data.i self.scale_2_expt = self.data.i[0] self.build_starting_model() self.mark_mod_core_region0(splat_range) #self.mark_mod_core_region(splat_range) self.n_trial = n_trial self.finals = [] for ii in range(n_trial): self.refine(ii) self.finals.append(self.best_nlm_coefs.deep_copy()) self.pair_align()
def tst_zernike_grid(skip_iteration_probability=0.95): #THIS TEST TAKES A BIT OF TIME M=20 N=4 ddd = (M*2+1) zga = math.zernike_grid(M,N,False) zgb = math.zernike_grid(M,N,False) xyz = zga.xyz() coefs = zga.coefs() nlm = zga.nlm() import random rng = random.Random(x=None) for ii in range(nlm.size()): for jj in range(ii+1,nlm.size()): if (rng.random() < skip_iteration_probability): continue coefsa = coefs*0.0 coefsb = coefs*0.0 coefsa[ii]=1.0+1.0j coefsb[jj]=1.0+1.0j zga.load_coefs( nlm, coefsa ) zgb.load_coefs( nlm, coefsb ) fa = zga.f() fb = zgb.f() prodsum = flex.sum(fa*flex.conj(fb) )/xyz.size() prodsuma= flex.sum(fa*flex.conj(fa) )/xyz.size() prodsumb= flex.sum(fb*flex.conj(fb) )/xyz.size() t1 = abs(prodsum) t2 = abs(prodsuma) t3 = abs(prodsumb) t1 = 100.0*(t1/t2) t2 = 100.0*(abs(t2-t3)/t3) # unfortunately, this numerical integration scheme is not optimal. For certain # combinations of nlm, we see significant non-orthogonality that reduces when # we increase the number of points. A similar behavior is seen in the radial # part of the Zernike polynome. If we compile withiout the radial function, similar # behavior is seen using only the spherical harmonics functions. # For this reason, the liberal limts set below are ok assert t1<2.0 assert t2<5.0
def write_bead( self, filename='converted.pdb'): self.n_bds = int(self.rmax/(3.6))+1 hex = True self.zga_hex = math.zernike_grid( self.n_bds, self.nmax, hex) self.zga_hex.load_coefs( self.nlm_array.nlm(), self.nlm_array.coefs() ) self.map_hex = flex.abs( self.zga_hex.f() ) max_value = flex.max( self.map_hex ) threshold = max_value*0.3 select = flex.bool( self.map_hex.as_1d() >= threshold ) xyz = self.zga_hex.xyz().select( select ) xyz = (xyz+(1.0,1.0,1.0)) * self.rmax res_id = 1 out = open( filename, 'w') for x,y,z in xyz: print>>out, "ATOM %5d CA ASP %4d %8.3f%8.3f%8.3f 1.00 1.00"%(res_id, res_id, x, y, z) res_id = res_id + 1 out.close()
def build_map(nmax, rmax, coefs, codes, model_indx, pdb_models, prefix, clusters=None, fract=0.9, type='.ccp4'): global stdfile global outfilelog rmax_over_fraction = rmax / fract np_on_grid = 30 zga = math.zernike_grid(np_on_grid, nmax, False) ref_nlm_array = math.nlm_array(nmax) mov_nlm_array = math.nlm_array(nmax) nlm = ref_nlm_array.nlm() nlm_total = ref_nlm_array.coefs().size() top_cc = flex.double() top_ids = [] ntop = model_indx.size() rank = 0 ave_c = flex.complex_double(nlm_total, 0) aligned_coefs = [] map_files = [] levels = [] scale_model = False if ((pdb_models is not None) and (pdb_models[0].rmax > rmax)): scale_model = True external_rmax = pdb_models[0].rmax grid = build_3d_grid(np_on_grid, rmax_over_fraction) with open(stdfile, "a") as log: print >> log, "Rank PDB_code cc (to the given model or the first model):" print "Rank PDB_code cc (to the given model or the first model):" if (pdb_models is not None): ref_nlm_array.load_coefs(nlm, pdb_models[0].nlm_coef) fraction = 0 else: c = coefs[model_indx[0]][0:nlm_total] ave_c = c.deep_copy() ref_nlm_array.load_coefs(nlm, c) rank = 1 if prefix != None: filename = prefix + "m" + str(rank) + "_" + codes[model_indx[0]] #filename = os.path.join(prefix,"m"+str(rank)+"_"+codes[ model_indx[0] ]) else: filename = "m" + str(rank) + "_" + codes[model_indx[0]] map_files.append(filename + type) fraction, map = write_map(zga, nlm, c, np_on_grid, rmax_over_fraction, filename, type=type) #level = flex.max(map)/3.0 print "map in search pdb.py: \n" print map level = map.standard_deviation_of_the_sample() print "level in search pdb: ", level levels.append(level) top_cc.append(1.0) # refering to itself if (scale_model): c = get_moments_for_scaled_model(map, np_on_grid, grid, nmax, rmax, external_rmax) with open(stdfile, "a") as log: print >> log, rank, codes[model_indx[0]] print rank, codes[model_indx[0]] aligned_coefs.append(c.deep_copy()) # save the aligned nlm coefs mean_frac = fraction mean_sqr_frac = fraction * fraction for ii in model_indx[rank:]: rank = rank + 1 c = coefs[ii][0:nlm_total] mov_nlm_array.load_coefs(nlm, c) align_obj = fft_align.align(ref_nlm_array, mov_nlm_array, nmax=nmax, refine=True) new_c = align_obj.moving_nlm.coefs() if prefix != None: #filename = os.path.join(prefix,"m"+str(rank)+"_"+codes[ii]) filename = prefix + "m" + str(rank) + "_" + codes[ii] print "**********************************" print "outfilelog: ", outfilelog with open(outfilelog, "a") as f: print >> f, filename + ".ccp4" else: filename = "m" + str(rank) + "_" + codes[ii] with open(outfilelog, "a") as f: print >> f, filename + ".ccp4" map_files.append(filename + type) fraction, map = write_map(zga, nlm, new_c, np_on_grid, rmax_over_fraction, filename, type=type) if (scale_model): c = get_moments_for_scaled_model(map, np_on_grid, grid, nmax, rmax, external_rmax) mov_nlm_array.load_coefs(nlm, c) align_obj = fft_align.align(ref_nlm_array, mov_nlm_array, nmax=nmax, refine=True) new_c = align_obj.moving_nlm.coefs() fraction, map = write_map(zga, nlm, new_c, np_on_grid, rmax_over_fraction, filename, type=type) level = flex.max(map) / 3.0 levels.append(level) cc = align_obj.get_cc() with open(stdfile, "a") as log: print >> log, "%2d %5s %5.3f" % (rank, codes[ii], cc) print "%2d %5s %5.3f" % (rank, codes[ii], cc) top_cc.append(cc) top_ids.append(codes[ii]) ave_c = ave_c + new_c aligned_coefs.append(new_c.deep_copy()) # save the aligned nlm coefs mean_frac = mean_frac + fraction mean_sqr_frac = mean_sqr_frac + fraction * fraction sphere_volume = rmax_over_fraction**3.0 * 8.0 # cube with d=2.0*r mean_frac = mean_frac / ntop sigma_frac = smath.sqrt(mean_sqr_frac / ntop - mean_frac * mean_frac) with open(stdfile, "a") as log: print >> log, "Volume is ", mean_frac * sphere_volume, "+/-", sigma_frac * sphere_volume, "(A^3)" print "Volume is ", mean_frac * sphere_volume, "+/-", sigma_frac * sphere_volume, "(A^3)" #### Write average map #### ave_maps = [] ave_levels = [] ave_cc = [] cluster_ids = [1] * len(model_indx) # initialize cluster_ids # filename = "ave_map" # map_files.append( filename+type ) # fraction, map=write_map( zga, nlm, ave_c/ntop, np_on_grid, rmax_over_fraction, filename, type=type ) # levels.append( flex.max(map)/3.0 ) # if( len(clusters.nodes) == 1): return top_cc, top_ids, map_files, levels, [1]*len(map_files) cluster_id = 1 with open(stdfile, "a") as log: print >> log, "cc. between Cluster average and PDB model" print "cc. between Cluster average and PDB model" for node in clusters.nodes: ave_c = ave_c * 0 coefs_list = [] for ii in node.leaf_eles: ave_c = ave_c + aligned_coefs[ii] cluster_ids[ii] = cluster_id coefs_list.append(aligned_coefs[ii]) ave_c = ave_c / len(node.leaf_eles) level_n = model_consistency.good2n(nmax, coefs_list, ave_c, threshold=0.90, outfile=stdfile) with open(stdfile, "a") as log: print >> log, "consistency level to order n: %d" % level_n print "consistency level to order n: %d" % level_n mov_nlm_array.load_coefs(nlm, ave_c) align_obj = fft_align.align(ref_nlm_array, mov_nlm_array, nmax=nmax, refine=True) cc = align_obj.get_cc() ave_cc.append(cc) with open(stdfile, "a") as log: print >> log, "cluster # ", cluster_id, "cc=", cc print "cluster # ", cluster_id, "cc=", cc if prefix == None: filename = "ave_" + str(cluster_id) with open(outfilelog, "a") as f: print >> f, filename else: filename = prefix + "ave_" + str(cluster_id) with open(outfilelog, "a") as f: print >> f, filename + ".ccp4" ave_maps.append(filename + type) fraction, map = write_map(zga, nlm, ave_c, np_on_grid, rmax_over_fraction, filename, type=type) ave_levels.append(flex.max(map) / 3.0) with open(stdfile, "a") as log: print >> log, "Averaged Model #%d Volume is %f (A^3)" % ( cluster_id, fraction * sphere_volume) print "Averaged Model #%d Volume is %f (A^3)" % (cluster_id, fraction * sphere_volume) cluster_id = cluster_id + 1 # with open(stdfile,"a") as log: # log.write("__END__") return top_cc, top_ids, map_files, levels, cluster_ids, ave_maps, ave_levels, ave_cc
def zernike_moments(pdbfile, nmax=20, np=50, fix_dx=False, np_on_grid=20, shift=False, buildmap=False, coef_out=True, calc_intensity=True, external_rmax=-1): base = pdbfile.split('.')[0] splat_range = 0 fraction = 0.9 default_dx = 0.7 uniform = True pdbi = pdb.hierarchy.input(file_name=pdbfile) # pdbi = iotbx.pdb.hierarchy.input(pdb_string='''ATOM 1 N ASP A 37 10.710 14.456 9.568 1.00 15.78 N''') if (len(pdbi.hierarchy.models()) == 0): return None, None, None atoms = pdbi.hierarchy.models()[0].atoms() # predefine some arrays we will need atom_types = flex.std_string() radius = flex.double() b_values = flex.double() occs = flex.double() # xyz = flex.vec3_double() xyz = get_xyz_using_split(pdbfile) # keep track of the atom types we have encountered # for atom in atoms: # print"---==================atom=================---",atom.xyz # if(not atom.hetero): # xyz.append( atom.xyz ) # b_values.append( atom.b ) # occs.append( atom.occ ) #print time.time() if (xyz.size() == 0): return None, None, None density = flex.double(xyz.size(), 1.0) voxel_obj = math.sphere_voxel(np, splat_range, uniform, fix_dx, external_rmax, default_dx, fraction, xyz, density) np = voxel_obj.np() # print "*********************voxel_obj.rmax(): ",voxel_obj.rmax() rmax = voxel_obj.rmax() / fraction # print "*********************fraction: ",fraction # print "*********************rmax=voxel_obj.rmax()/fraction: ",rmax #print base, "RMAX: ", voxel_obj.rmax() #print time.time() grid_obj = math.sphere_grid(np, nmax) pdb_out = False grid_obj.clean_space(voxel_obj, pdb_out) #print time.time(), "END GRID CONST" grid_obj.construct_space_sum() #print time.time(), "SS CALC" mom_obj = math.zernike_moments(grid_obj, nmax) #print time.time(), "END MOM CALC" moments = mom_obj.moments() if (coef_out): nn = mom_obj.fnn() easy_pickle.dump(base + '.nlm.pickle', moments.coefs()) easy_pickle.dump(base + '.nn.pickle', nn.coefs()) # ####### The following will be optional ### if (shift): shift = [rmax, rmax, rmax] centered_xyz = voxel_obj.xyz() + shift out_pdb_name = base + '_centered.pdb' for a, xyz in zip(atoms, centered_xyz): a.set_xyz(new_xyz=xyz) pdbi.hierarchy.write_pdb_file(file_name=out_pdb_name, open_append=False) original_map = voxel_obj.map() xplor_map_type(original_map, np, rmax, file_name=base + '_pdb.xplor') ######## Calculate Intnesity Profile ############ if (calc_intensity): q_array = flex.double(range(51)) / 100.0 z_model = zm.zernike_model(moments, q_array, rmax, nmax) nn = mom_obj.fnn() intensity = z_model.calc_intensity(nn) #iq_file = open(base+"_"+str(nmax)+"_"+str(int(rmax*fraction))+".zi", 'w') iq_file = open(base + "_" + str(nmax) + ".zi", 'w') for qq, ii in zip(q_array, intensity): print >> iq_file, qq, ii iq_file.close() ####### END of Intensity Calculation ########### if (buildmap): print("grid setting up...") zga = math.zernike_grid(np_on_grid, nmax, False) print("grid setting up...Done") zga.load_coefs(moments.nlm(), moments.coefs()) print("start reconstruction") map = flex.abs(zga.f()) print("finished reconstruction") xplor_map_type(map, np_on_grid, rmax, file_name=base + '.xplor') ccp4_map_type(map, np_on_grid, rmax, file_name=base + '.ccp4') return mom_obj, voxel_obj, pdbi
def zernike_moments(mol2_file, nmax=20, np=50, uniform=True, fix_dx=False, np_on_grid=20, shift=False, buildmap=False, coef_out=True, calc_intensity=True, external_rmax=-1): base = mol2_file.split('.')[0] splat_range = 0 fraction = 0.9 default_dx = 0.5 models = mol2.read_Mol2_file( mol2_file ) if(len( models )== 0): return None,None,None ## for testing purpose, here only the first model in the mol2 file is converted ## this_molecule = models[0] ## models should be a list of molecules, same ligand at different conformations ## all_atoms = this_molecule.atom_list if(len( all_atoms )== 0): return None,None,None # predefine some arrays we will need xyz = flex.vec3_double() charges = flex.double() # keep track of the atom types we have encountered for atom in all_atoms: xyz.append( ( atom.X, atom.Y, atom.Z ) ) charges.append( atom.Q ) if(xyz.size() == 0): return None,None,None if (uniform): density=flex.double(xyz.size(),1.0) else: # use charges density=charges voxel_obj = math.sphere_voxel(np,splat_range,uniform,fix_dx,external_rmax, default_dx, fraction,xyz,density) np = voxel_obj.np() rmax=voxel_obj.rmax()/fraction #print base, "RMAX: ", voxel_obj.rmax() #print time.time() grid_obj = math.sphere_grid(np, nmax) pdb_out = False grid_obj.clean_space(voxel_obj, pdb_out) #print time.time(), "END GRID CONST" grid_obj.construct_space_sum() #print time.time(), "SS CALC" mom_obj = math.zernike_moments(grid_obj, nmax) #print time.time(), "END MOM CALC" moments = mom_obj.moments() if(coef_out): nn = mom_obj.fnn() easy_pickle.dump(base+'.nlm.pickle', moments.coefs() ) easy_pickle.dump(base+'.nn.pickle', nn.coefs() ) # ####### The following will be optional ### if(shift): shift = [rmax, rmax, rmax] centered_xyz = voxel_obj.xyz() + shift out_mol2_filename =base+'_centered.mol2' for a,xyz in zip( all_atoms, centered_xyz): a.X = xyz[0] a.Y = xyz[1] a.Z = xyz[2] mol2.write_mol2( this_molecule, out_mol2_filename ) original_map = voxel_obj.map() xplor_map_type( original_map, np, rmax, file_name=base+'_pdb.xplor') ######## Calculate Intnesity Profile ############ if(calc_intensity): q_array = flex.double( range(51) )/100.0 z_model = zm.zernike_model( moments, q_array, rmax, nmax) nn = mom_obj.fnn() intensity = z_model.calc_intensity(nn) #iq_file = open(base+"_"+str(nmax)+"_"+str(int(rmax*fraction))+".zi", 'w') iq_file = open(base+"_"+str(nmax)+".zi", 'w') for qq, ii in zip(q_array, intensity): print>>iq_file, qq,ii iq_file.close() ####### END of Intensity Calculation ########### if(buildmap): print "grid setting up..." zga = math.zernike_grid(np_on_grid, nmax, False) print "grid setting up...Done" zga.load_coefs(moments.nlm(), moments.coefs() ) print "start reconstruction" map=flex.abs(zga.f() ) print "finished reconstruction" xplor_map_type( map, np_on_grid, rmax, file_name=base+'.xplor') ccp4_map_type( map, np_on_grid, rmax, file_name=base+'.ccp4' ) return mom_obj, voxel_obj, models
def build_map(nmax, shapes, coefs, codes, pdb_models): np_on_grid = 30 zga = math.zernike_grid(np_on_grid, nmax, False) ref_nlm_array = math.nlm_array(nmax) mov_nlm_array = math.nlm_array(nmax) nlm = ref_nlm_array.nlm() nlm_total = ref_nlm_array.coefs().size() top_cc = flex.double() ntop = shapes.ntop rank = 0 ave_c = flex.complex_double(nlm_total, 0) if (pdb_models is not None): ref_nlm_array.load_coefs(nlm, pdb_models[0].nlm_coef) fraction = 0 else: c = coefs[shapes.best_indices[0]][0:nlm_total] ave_c = c.deep_copy() ref_nlm_array.load_coefs(nlm, c) rank = 1 filename = "m" + str(rank) + "_" + shapes.best_codes[0] + ".xplor" fraction = write_xplor(zga, nlm, c, np_on_grid, shapes.best_rmax[0], filename) print rank, shapes.best_codes[0], fraction ref_mean, ref_s = get_mean_sigma(ref_nlm_array) mean_frac = fraction mean_sqr_frac = fraction * fraction for ii, code in zip(shapes.best_indices[rank:], shapes.best_codes[rank:]): coef = coefs[ii][0:nlm_total] mov_nlm_array.load_coefs(nlm, coef) mov_mean, mov_s = get_mean_sigma(mov_nlm_array) align_obj = fft_align.align(ref_nlm_array, mov_nlm_array, nmax=nmax, refine=True) new_c = align_obj.moving_nlm.coefs() cc = align_obj.get_cc() top_cc.append(cc) ave_c = ave_c + new_c filename = "m" + str(rank + 1) + "_" + code + ".xplor" fraction = write_xplor(zga, nlm, new_c, np_on_grid, shapes.best_rmax[rank], filename) rank = rank + 1 print rank, code, fraction mean_frac = mean_frac + fraction mean_sqr_frac = mean_sqr_frac + fraction * fraction #sphere_volume = 4.0/3.0*smath.pi*rmax*rmax*rmax rmax = shapes.best_rmax[0] sphere_volume = rmax * rmax * rmax * 8.0 mean_frac = mean_frac / ntop sigma_frac = smath.sqrt(mean_sqr_frac / ntop - mean_frac * mean_frac) print "Volume is ", mean_frac * sphere_volume, "+/-", sigma_frac * sphere_volume #### Write average map #### filename = "ave_map.xplor" write_xplor(zga, nlm, ave_c / ntop, np_on_grid, rmax, filename) return top_cc