def test_stack_substack(self): # img = EMAN2db.EMData() # EMAN2db.db_read_image(img, "bdb:/home/adnan/DemoResults/06_SUBSTACK_ANO#isac_substack",0 ) # img1 = EMAN2db.db_read_images("bdb:/home/adnan/DemoResults/06_SUBSTACK_ANO#isac_substack") # print(img1[0].get_2dview()) e = EMAN2_cppwrap.EMData() e.read_image("bdb:/home/adnan/DemoResults/04_ISAC#stack_ali2d", 5) d = EMAN2_cppwrap.EMData() d.read_image("bdb:/home/adnan/DemoResults/04_ISAC#case4stack", 5) img = EMAN2db.EMData.read_images( "bdb:/home/adnan/DemoResults/04_ISAC#case4stack") self.assertTrue(numpy.array_equal(e.get_2dview(), d.get_2dview())) self.assertTrue(numpy.array_equal(img[5].get_2dview(), d.get_2dview()))
def max_3D_pixel_error(t1, t2, r=1.0): """ Compute maximum pixel error between two sets of orientation parameters assuming object has radius r, t1 is the projection transformation of the first projection and t2 of the second one, respectively: t = Transform({"type":"spider","phi":phi,"theta":theta,"psi":psi}) t.set_trans(Vec2f(-tx, -ty)) Note the function is symmetric in t1, t2. """ pass #IMPORTIMPORTIMPORT from EMAN2 import Vec2f pass #IMPORTIMPORTIMPORT import types dummy = EMAN2_cppwrap.Transform() if (type(dummy) != type(t1)): t = EMAN2_cppwrap.Transform({ "type": "spider", "phi": t1[0], "theta": t1[1], "psi": t1[2] }) t.set_trans(EMAN2_cppwrap.Vec2f(-t1[3], -t1[4])) else: t = t1 if (type(dummy) != type(t2)): u = EMAN2_cppwrap.Transform({ "type": "spider", "phi": t2[0], "theta": t2[1], "psi": t2[2] }) u.set_trans(EMAN2_cppwrap.Vec2f(-t2[3], -t2[4])) else: u = t2 return EMAN2_cppwrap.EMData().max_3D_pixel_error(t, u, r)
def import_particle_stack(particle_stack, output_dir): """ Import the particle stack. Arguments: particle_stack - Path to the particle stack Returns: Particle array """ particle_header = EMAN2_cppwrap.EMData() particle_header.read_image( particle_stack, 0, True ) dtype_list, name_list = create_stack_dtype(particle_header.get_attr_dict()) header_dict = {} for name in name_list: header_dict[name] = np.array(EMAN2_cppwrap.EMUtil.get_all_attributes(particle_stack, name)) stack_array, create_stack = create_stack_array(dtype_list, header_dict, output_dir) return stack_array, create_stack
def create_particle_stack(particle_stack, output_dir, particle_data): """ Create a mrcs particle stack based on the bdb/hdf input stack. Arguments: particle_stack - Particle stack name output_dir - Output directory particle_data - Particle_data array Returns: None """ print('|_Get particle ID and particle names') ptcl_ids = [int(entry.split('@')[0]) for entry in particle_data['_rlnImageName']] ptcl_names = [entry.split('@')[1] for entry in particle_data['_rlnImageName']] print('|_Write images') for particle_idx in range(particle_data.shape[0]): if particle_idx % 10000 == 0: print(particle_idx, ' of ', particle_data.shape[0]) emdata = EMAN2_cppwrap.EMData(particle_stack, particle_idx) output_name = os.path.join(output_dir, ptcl_names[particle_idx]) try: os.makedirs(os.path.dirname(output_name)) except OSError: pass emdata.write_image(output_name, -1)
def create_particle_stack(particle_stack, output_dir, particle_data): """ Create a mrcs particle stack based on the bdb/hdf input stack. Arguments: particle_stack - Particle stack name output_dir - Output directory particle_data - Particle_data array Returns: None """ sp_global_def.sxprint("|_Get particle ID and particle names") ptcl_names = [entry.split("@")[1] for entry in particle_data["_rlnImageName"]] sp_global_def.sxprint("|_Write images") for particle_idx in range(particle_data.shape[0]): if particle_idx % 10000 == 0: sp_global_def.sxprint(particle_idx, " of ", particle_data.shape[0]) emdata = EMAN2_cppwrap.EMData(particle_stack, particle_idx) output_name = os.path.join(output_dir, "Particles", os.path.basename(ptcl_names[particle_idx])) try: os.makedirs(os.path.dirname(output_name)) except OSError: pass emdata.write_image(output_name, -1)
def import_particle_stack(particle_stack, output_dir, project_dir): """ Import the particle stack. Arguments: particle_stack - Path to the particle stack Returns: Particle array """ particle_header = EMAN2_cppwrap.EMData() particle_header.read_image(particle_stack, 0, True) dtype_list, name_list = create_stack_dtype(particle_header.get_attr_dict()) new_header = sp_utilities.make_v_stack_header(particle_stack, project_dir) header_dict = {} for name in name_list: for entry in new_header: header_dict.setdefault(name, []).append(entry[name]) for key in header_dict: header_dict[key] = np.array(header_dict[key]) stack_array, create_stack = create_stack_array(dtype_list, header_dict, output_dir, project_dir) return stack_array, create_stack
def prepare_refrings( volft, kb, nz=-1, delta=2.0, ref_a="P", sym="c1", numr=None, MPI=False, phiEqpsi="Zero", kbx=None, kby=None, initial_theta=None, delta_theta=None, initial_phi=None, ): """ Generate quasi-evenly distributed reference projections converted to rings ref_a can be a list of angles, in which case it is used instead of being generated """ # mpi communicator can be sent by the MPI parameter if type(MPI) is bool: if MPI: mpi_comm = mpi.MPI_COMM_WORLD else: mpi_comm = MPI MPI = True mode = "F" if type(ref_a) is list: # if ref_a is list, it has to be a list of projection directions, use it ref_angles = ref_a else: # generate list of Eulerian angles for reference projections # phi, theta, psi if initial_theta and initial_phi: ref_angles = sp_utilities.even_angles( delta, theta1=initial_theta, phi1=initial_phi, symmetry=sym, method=ref_a, phiEqpsi=phiEqpsi, ) else: if initial_theta is None: if sym[:1] == "c" or sym[:1] == "d": ref_angles = sp_utilities.even_angles( delta, symmetry=sym, method=ref_a, phiEqpsi=phiEqpsi ) else: psp = sp_fundamentals.symclass(sym) ref_angles = psp.even_angles(delta) del psp else: if delta_theta is None: delta_theta = 1.0 ref_angles = sp_utilities.even_angles( delta, theta1=initial_theta, theta2=delta_theta, symmetry=sym, method=ref_a, phiEqpsi=phiEqpsi, ) wr_four = ringwe(numr, mode) cnx = old_div(nz, 2) + 1 cny = old_div(nz, 2) + 1 num_ref = len(ref_angles) if MPI: myid = mpi.mpi_comm_rank(mpi_comm) ncpu = mpi.mpi_comm_size(mpi_comm) else: ncpu = 1 myid = 0 if nz < 1: sp_global_def.ERROR( "Data size has to be given (nz)", "prepare_refrings", 1, myid ) ref_start, ref_end = sp_applications.MPI_start_end(num_ref, ncpu, myid) refrings = ( [] ) # list of (image objects) reference projections in Fourier representation sizex = numr[len(numr) - 2] + numr[len(numr) - 1] - 1 for i in range(num_ref): prjref = EMAN2_cppwrap.EMData() prjref.set_size(sizex, 1, 1) refrings.append(prjref) if kbx is None: for i in range(ref_start, ref_end): prjref = sp_projection.prgs( volft, kb, [ref_angles[i][0], ref_angles[i][1], ref_angles[i][2], 0.0, 0.0], ) cimage = EMAN2_cppwrap.Util.Polar2Dm( prjref, cnx, cny, numr, mode ) # currently set to quadratic.... EMAN2_cppwrap.Util.Normalize_ring(cimage, numr, 0) EMAN2_cppwrap.Util.Frngs(cimage, numr) EMAN2_cppwrap.Util.Applyws(cimage, numr, wr_four) refrings[i] = cimage else: for i in range(ref_start, ref_end): prjref = sp_projection.prgs( volft, kb, [ref_angles[i][0], ref_angles[i][1], ref_angles[i][2], 0.0, 0.0], kbx, kby, ) cimage = EMAN2_cppwrap.Util.Polar2Dm( prjref, cnx, cny, numr, mode ) # currently set to quadratic.... EMAN2_cppwrap.Util.Normalize_ring(cimage, numr, 0) EMAN2_cppwrap.Util.Frngs(cimage, numr) EMAN2_cppwrap.Util.Applyws(cimage, numr, wr_four) refrings[i] = cimage if MPI: sp_utilities.bcast_compacted_EMData_all_to_all(refrings, myid, comm=mpi_comm) for i in range(len(ref_angles)): n1, n2, n3 = sp_utilities.getfvec(ref_angles[i][0], ref_angles[i][1]) refrings[i].set_attr_dict( { "phi": ref_angles[i][0], "theta": ref_angles[i][1], "psi": ref_angles[i][2], "n1": n1, "n2": n2, "n3": n3, } ) return refrings
def recons3d_4nn_ctf_MPI( myid, prjlist, snr=1.0, sign=1, symmetry="c1", finfo=None, npad=2, xysize=-1, zsize=-1, mpi_comm=None, smearstep=0.0, ): """ recons3d_4nn_ctf - calculate CTF-corrected 3-D reconstruction from a set of projections using three Eulerian angles, two shifts, and CTF settings for each projeciton image Input stack: name of the stack file containing projection data, projections have to be squares list_proj: list of projections to be included in the reconstruction or image iterator snr: Signal-to-Noise Ratio of the data sign: sign of the CTF symmetry: point-group symmetry to be enforced, each projection will enter the reconstruction in all symmetry-related directions. """ if mpi_comm == None: mpi_comm = mpi.MPI_COMM_WORLD if type(prjlist) == list: prjlist = sp_utilities.iterImagesList(prjlist) if not prjlist.goToNext(): sp_global_def.ERROR("empty input list", "recons3d_4nn_ctf_MPI", 1) imgsize = prjlist.image().get_xsize() if prjlist.image().get_ysize() != imgsize: imgsize = max(imgsize, prjlist.image().get_ysize()) dopad = True else: dopad = False prjlist.goToPrev() fftvol = EMAN2_cppwrap.EMData() if smearstep > 0.0: # if myid == 0: print " Setting smear in prepare_recons_ctf" ns = 1 smear = [] for j in range(-ns, ns + 1): if j != 0: for i in range(-ns, ns + 1): for k in range(-ns, ns + 1): smear += [ i * smearstep, j * smearstep, k * smearstep, 1.0 ] # Deal with theta = 0.0 cases prj = [] for i in range(-ns, ns + 1): for k in range(-ns, ns + 1): prj.append(i + k) for i in range(-2 * ns, 2 * ns + 1, 1): smear += [i * smearstep, 0.0, 0.0, float(prj.count(i))] # if myid == 0: print " Smear ",smear fftvol.set_attr("smear", smear) weight = EMAN2_cppwrap.EMData() if xysize == -1 and zsize == -1: params = { "size": imgsize, "npad": npad, "snr": snr, "sign": sign, "symmetry": symmetry, "fftvol": fftvol, "weight": weight, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf", params) else: if xysize != -1 and zsize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = old_div(float(zsize), imgsize) elif xysize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = 1.0 else: rx = 1.0 ry = 1.0 rz = old_div(float(zsize), imgsize) # There is an error here with sizeprojection PAP 10/22/2014 params = { "size": sizeprojection, "npad": npad, "snr": snr, "sign": sign, "symmetry": symmetry, "fftvol": fftvol, "weight": weight, "xratio": rx, "yratio": ry, "zratio": rz, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params) r.setup() # if not (finfo is None): nimg = 0 while prjlist.goToNext(): prj = prjlist.image() if dopad: prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference") # if params: insert_slices(r, prj) if not (finfo is None): nimg += 1 finfo.write(" %4d inserted\n" % (nimg)) finfo.flush() del sp_utilities.pad if not (finfo is None): finfo.write("begin reduce\n") finfo.flush() sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm) sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm) if not (finfo is None): finfo.write("after reduce\n") finfo.flush() if myid == 0: dummy = r.finish(True) else: if xysize == -1 and zsize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize) else: if zsize == -1: fftvol = sp_utilities.model_blank(xysize, xysize, imgsize) elif xysize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize) else: fftvol = sp_utilities.model_blank(xysize, xysize, zsize) return fftvol
def recons3d_4nn( stack_name, list_proj=[], symmetry="c1", npad=4, snr=None, weighting=1, varsnr=False, xysize=-1, zsize=-1, ): """ Perform a 3-D reconstruction using Pawel's FFT Back Projection algorithm. Input: stack_name - name of the file with projection data. list_proj - list of projections to be used in the reconstruction symmetry - Point group of the target molecule (defaults to "C1") npad - Angles and shifts are passed in the file header as set_attr. Keywords are phi, theta, psi, sx, sy Return: 3D reconstructed volume image Usage: vol = recons3d_4nn(filepattern, list_proj, symmetry) """ if list_proj == []: if type(stack_name) == bytes: nima = EMAN2_cppwrap.EMUtil.get_image_count(stack_name) else: nima = len(stack_name) list_proj = list(range(nima)) # read first image to determine the size to use if type(stack_name) == bytes: proj = EMAN2_cppwrap.EMData() proj.read_image(stack_name, list_proj[0]) else: proj = stack_name[list_proj[0]].copy() size = proj.get_xsize() # sanity check -- image must be square if size != proj.get_ysize(): sp_global_def.ERROR("input data has to be square", "recons3d_4nn", 1) # reconstructor fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() params = { "npad": npad, "symmetry": symmetry, "weighting": weighting, "fftvol": fftvol, "weight": weight, } if xysize == -1 and zsize == -1: params["size"] = size if snr != None: params["snr"] = snr # params["varsnr"] = int(varsnr) r = EMAN2_cppwrap.Reconstructors.get("nn4", params) else: if xysize != -1 and zsize != -1: rx = old_div(float(xysize), size) ry = old_div(float(xysize), size) rz = old_div(float(zsize), size) elif xysize != -1: rx = old_div(float(xysize), size) ry = old_div(float(xysize), size) rz = 1.0 else: rx = 1.0 ry = 1.0 rz = old_div(float(zsize), size) if snr is None: params["sizeprojection"] = size params["xratio"] = rx params["yratio"] = ry params["zratio"] = rz else: params["sizeprojection"] = size params["snr"] = snr params["varsnr"] = int(varsnr) params["xratio"] = rx params["yratio"] = ry params["zratio"] = rz r = EMAN2_cppwrap.Reconstructors.get("nn4_rect", params) r.setup() if type(stack_name) == bytes: for i in range(len(list_proj)): proj.read_image(stack_name, list_proj[i]) # horatio active_refactoring Jy51i1EwmLD4tWZ9_00000_1 # active = proj.get_attr_default('active', 1) # if active == 1: # insert_slices(r, proj) insert_slices(r, proj) else: for i in list_proj: # horatio active_refactoring Jy51i1EwmLD4tWZ9_00000_1 # active = stack_name[i].get_attr_default('active', 1) # if active == 1: # insert_slices(r, stack_name[i]) insert_slices(r, stack_name[i]) dummy = r.finish(True) return fftvol
def recons3d_4nn_ctf( stack_name, list_proj=[], snr=1.0, sign=1, symmetry="c1", verbose=0, npad=2, xysize=-1, zsize=-1, ): """Perform a 3-D reconstruction using Pawel's FFT Back Projection algoritm. Input: stack_name - name of the stack file on a disk, each image has to have the following attributes set: psi, theta, phi, sx, sy, defocus, list_proj - list of images from stack_name to be included in the reconstruction symmetry -- Point group of the target molecule (defaults to "C1") Return: 3d reconstructed volume image Usage: anglelist = getAngles("myangles.txt") # not yet written vol = do_reconstruction(filepattern, start, end, anglelist, symmetry) """ # read first image to determine the size to use if list_proj == []: if type(stack_name) == bytes: nima = EMAN2_cppwrap.EMUtil.get_image_count(stack_name) else: nima = len(stack_name) list_proj = list(range(nima)) # read first image to determine the size to use if type(stack_name) == bytes: proj = EMAN2_cppwrap.EMData() proj.read_image(stack_name, list_proj[0]) else: proj = stack_name[list_proj[0]].copy() # convert angles to transform (rotation) objects # horatio active_refactoring Jy51i1EwmLD4tWZ9_00000_1 # active = proj.get_attr_default('active', 1) size = proj.get_xsize() if proj.get_ysize() != size: size = max(size, proj.get_ysize()) dopad = True else: dopad = False # reconstructor fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() params = { "npad": npad, "symmetry": symmetry, "snr": snr, "sign": sign, "fftvol": fftvol, "weight": weight, } if xysize == -1 and zsize == -1: params["size"] = size r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf", params) else: if xysize != -1 and zsize != -1: rx = old_div(float(xysize), size) ry = old_div(float(xysize), size) rz = old_div(float(zsize), size) elif xysize != -1: rx = old_div(float(xysize), size) ry = old_div(float(xysize), size) rz = 1.0 else: rx = 1.0 ry = 1.0 rz = old_div(float(zsize), size) params["sizeprojection"] = size params["xratio"] = rx params["yratio"] = ry params["zratio"] = rz r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params) r.setup() if type(stack_name) == bytes: for i in range(len(list_proj)): proj.read_image(stack_name, list_proj[i]) if dopad: proj = sp_utilities.pad(proj, size, size, 1, "circumference") insert_slices(r, proj) else: for i in range(len(list_proj)): insert_slices(r, stack_name[list_proj[i]]) dummy = r.finish(True) return fftvol
def recons3d_trl_struct_MPI( myid, main_node, prjlist, paramstructure, refang, rshifts_shrank, delta, upweighted=True, mpi_comm=None, CTF=True, target_size=-1, avgnorm=1.0, norm_per_particle=None, ): """ recons3d_4nn_ctf - calculate CTF-corrected 3-D reconstruction from a set of projections using three Eulerian angles, two shifts, and CTF settings for each projeciton image Input list_of_prjlist: list of lists of projections to be included in the reconstruction """ if mpi_comm == None: mpi_comm = mpi.MPI_COMM_WORLD refvol = sp_utilities.model_blank(target_size) refvol.set_attr("fudge", 1.0) if CTF: do_ctf = 1 else: do_ctf = 0 fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() params = { "size": target_size, "npad": 2, "snr": 1.0, "sign": 1, "symmetry": "c1", "refvol": refvol, "fftvol": fftvol, "weight": weight, "do_ctf": do_ctf, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctfw", params) r.setup() if prjlist: if norm_per_particle == None: norm_per_particle = len(prjlist) * [1.0] nnx = prjlist[0].get_xsize() nny = prjlist[0].get_ysize() nshifts = len(rshifts_shrank) for im in range(len(prjlist)): # parse projection structure, generate three lists: # [ipsi+iang], [ishift], [probability] # Number of orientations for a given image numbor = len(paramstructure[im][2]) ipsiandiang = [ old_div(paramstructure[im][2][i][0], 1000) for i in range(numbor) ] allshifts = [ paramstructure[im][2][i][0] % 1000 for i in range(numbor) ] probs = [paramstructure[im][2][i][1] for i in range(numbor)] # Find unique projection directions tdir = list(set(ipsiandiang)) bckgn = prjlist[im].get_attr("bckgnoise") ct = prjlist[im].get_attr("ctf") # For each unique projection direction: data = [None] * nshifts for ii in range(len(tdir)): # Find the number of times given projection direction appears on the list, it is the number of different shifts associated with it. lshifts = sp_utilities.findall(tdir[ii], ipsiandiang) toprab = 0.0 for ki in range(len(lshifts)): toprab += probs[lshifts[ki]] recdata = EMAN2_cppwrap.EMData(nny, nny, 1, False) recdata.set_attr("is_complex", 0) for ki in range(len(lshifts)): lpt = allshifts[lshifts[ki]] if data[lpt] == None: data[lpt] = sp_fundamentals.fshift( prjlist[im], rshifts_shrank[lpt][0], rshifts_shrank[lpt][1]) data[lpt].set_attr("is_complex", 0) EMAN2_cppwrap.Util.add_img( recdata, EMAN2_cppwrap.Util.mult_scalar( data[lpt], old_div(probs[lshifts[ki]], toprab)), ) recdata.set_attr_dict({ "padffted": 1, "is_fftpad": 1, "is_fftodd": 0, "is_complex_ri": 1, "is_complex": 1, }) if not upweighted: recdata = sp_filter.filt_table(recdata, bckgn) recdata.set_attr_dict({"bckgnoise": bckgn, "ctf": ct}) ipsi = tdir[ii] % 100000 iang = old_div(tdir[ii], 100000) r.insert_slice( recdata, EMAN2_cppwrap.Transform({ "type": "spider", "phi": refang[iang][0], "theta": refang[iang][1], "psi": refang[iang][2] + ipsi * delta, }), old_div(toprab * avgnorm, norm_per_particle[im]), ) # clean stuff del bckgn, recdata, tdir, ipsiandiang, allshifts, probs sp_utilities.reduce_EMData_to_root(fftvol, myid, main_node, comm=mpi_comm) sp_utilities.reduce_EMData_to_root(weight, myid, main_node, comm=mpi_comm) if myid == main_node: dummy = r.finish(True) mpi.mpi_barrier(mpi_comm) if myid == main_node: return fftvol, weight, refvol else: return None, None, None
def recons3d_4nnw_MPI( myid, prjlist, bckgdata, snr=1.0, sign=1, symmetry="c1", finfo=None, npad=2, xysize=-1, zsize=-1, mpi_comm=None, smearstep=0.0, fsc=None, ): """ recons3d_4nn_ctf - calculate CTF-corrected 3-D reconstruction from a set of projections using three Eulerian angles, two shifts, and CTF settings for each projeciton image Input stack: name of the stack file containing projection data, projections have to be squares prjlist: list of projections to be included in the reconstruction or image iterator bckgdata = [get_im("tsd.hdf"),read_text_file("data_stamp.txt")] snr: Signal-to-Noise Ratio of the data sign: sign of the CTF symmetry: point-group symmetry to be enforced, each projection will enter the reconstruction in all symmetry-related directions. """ pass # IMPORTIMPORTIMPORT from sp_utilities import reduce_EMData_to_root, pad pass # IMPORTIMPORTIMPORT from EMAN2 import Reconstructors pass # IMPORTIMPORTIMPORT from sp_utilities import iterImagesList, set_params_proj, model_blank pass # IMPORTIMPORTIMPORT from mpi import MPI_COMM_WORLD pass # IMPORTIMPORTIMPORT import types if mpi_comm == None: mpi_comm = mpi.MPI_COMM_WORLD if type(prjlist) == list: prjlist = sp_utilities.iterImagesList(prjlist) if not prjlist.goToNext(): sp_global_def.ERROR("empty input list", "recons3d_4nnw_MPI", 1) imgsize = prjlist.image().get_xsize() if prjlist.image().get_ysize() != imgsize: imgsize = max(imgsize, prjlist.image().get_ysize()) dopad = True else: dopad = False prjlist.goToPrev() # Do the FSC shtick. bnx = old_div(imgsize * npad, 2) + 1 if fsc: pass # IMPORTIMPORTIMPORT from math import sqrt pass # IMPORTIMPORTIMPORT from sp_utilities import reshape_1d t = [0.0] * len(fsc) for i in range(len(fsc)): t[i] = min(max(fsc[i], 0.0), 0.999) t = sp_utilities.reshape_1d(t, len(t), npad * len(t)) refvol = sp_utilities.model_blank(bnx, 1, 1, 0.0) for i in range(len(fsc)): refvol.set_value_at(i, t[i]) else: refvol = sp_utilities.model_blank(bnx, 1, 1, 1.0) refvol.set_attr("fudge", 1.0) fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() if smearstep > 0.0: # if myid == 0: print " Setting smear in prepare_recons_ctf" ns = 1 smear = [] for j in range(-ns, ns + 1): if j != 0: for i in range(-ns, ns + 1): for k in range(-ns, ns + 1): smear += [ i * smearstep, j * smearstep, k * smearstep, 1.0 ] # Deal with theta = 0.0 cases prj = [] for i in range(-ns, ns + 1): for k in range(-ns, ns + 1): prj.append(i + k) for i in range(-2 * ns, 2 * ns + 1, 1): smear += [i * smearstep, 0.0, 0.0, float(prj.count(i))] # if myid == 0: print " Smear ",smear fftvol.set_attr("smear", smear) if xysize == -1 and zsize == -1: params = { "size": imgsize, "npad": npad, "snr": snr, "sign": sign, "symmetry": symmetry, "refvol": refvol, "fftvol": fftvol, "weight": weight, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctfw", params) else: if xysize != -1 and zsize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = old_div(float(zsize), imgsize) elif xysize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = 1.0 else: rx = 1.0 ry = 1.0 rz = old_div(float(zsize), imgsize) # There is an error here with sizeprojection PAP 10/22/2014 params = { "size": sizeprojection, "npad": npad, "snr": snr, "sign": sign, "symmetry": symmetry, "fftvol": fftvol, "weight": weight, "xratio": rx, "yratio": ry, "zratio": rz, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params) r.setup() # from utilities import model_blank, get_im, read_text_file # bckgdata = [get_im("tsd.hdf"),read_text_file("data_stamp.txt")] nnx = bckgdata[0].get_xsize() nny = bckgdata[0].get_ysize() bckgnoise = [] for i in range(nny): prj = sp_utilities.model_blank(nnx) for k in range(nnx): prj[k] = bckgdata[0].get_value_at(k, i) bckgnoise.append(prj) datastamp = bckgdata[1] if not (finfo is None): nimg = 0 while prjlist.goToNext(): prj = prjlist.image() try: stmp = old_div(nnx, 0) stmp = prj.get_attr("ptcl_source_image") except: try: stmp = prj.get_attr("ctf") stmp = round(stmp.defocus, 4) except: sp_global_def.ERROR( "Either ptcl_source_image or ctf has to be present in the header.", "recons3d_4nnw_MPI", 1, myid, ) try: indx = datastamp.index(stmp) except: sp_global_def.ERROR("Problem with indexing ptcl_source_image.", "recons3d_4nnw_MPI", 1, myid) if dopad: prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference") prj.set_attr("bckgnoise", bckgnoise[indx]) insert_slices(r, prj) if not (finfo is None): nimg += 1 finfo.write(" %4d inserted\n" % (nimg)) finfo.flush() del sp_utilities.pad if not (finfo is None): finfo.write("begin reduce\n") finfo.flush() sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm) sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm) if not (finfo is None): finfo.write("after reduce\n") finfo.flush() if myid == 0: dummy = r.finish(True) else: pass # IMPORTIMPORTIMPORT from sp_utilities import model_blank if xysize == -1 and zsize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize) else: if zsize == -1: fftvol = sp_utilities.model_blank(xysize, xysize, imgsize) elif xysize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize) else: fftvol = sp_utilities.model_blank(xysize, xysize, zsize) return fftvol
def recons3d_4nn_MPI( myid, prjlist, symmetry="c1", finfo=None, snr=1.0, npad=2, xysize=-1, zsize=-1, mpi_comm=None, ): if mpi_comm == None: mpi_comm = mpi.MPI_COMM_WORLD if type(prjlist) == list: prjlist = sp_utilities.iterImagesList(prjlist) if not prjlist.goToNext(): sp_global_def.ERROR("empty input list", "recons3d_4nn_MPI", 1) imgsize = prjlist.image().get_xsize() if prjlist.image().get_ysize() != imgsize: imgsize = max(imgsize, prjlist.image().get_ysize()) dopad = True else: dopad = False prjlist.goToPrev() fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() if xysize == -1 and zsize == -1: params = { "size": imgsize, "npad": npad, "symmetry": symmetry, "fftvol": fftvol, "weight": weight, "snr": snr, } r = EMAN2_cppwrap.Reconstructors.get("nn4", params) else: if xysize != -1 and zsize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = old_div(float(zsize), imgsize) elif xysize != -1: rx = old_div(float(xysize), imgsize) ry = old_div(float(xysize), imgsize) rz = 1.0 else: rx = 1.0 ry = 1.0 rz = old_div(float(zsize), imgsize) params = { "sizeprojection": imgsize, "npad": npad, "symmetry": symmetry, "fftvol": fftvol, "weight": weight, "xratio": rx, "yratio": ry, "zratio": rz, } r = EMAN2_cppwrap.Reconstructors.get("nn4_rect", params) r.setup() if not (finfo is None): nimg = 0 while prjlist.goToNext(): prj = prjlist.image() if dopad: prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference") insert_slices(r, prj) if not (finfo is None): nimg += 1 finfo.write("Image %4d inserted.\n" % (nimg)) finfo.flush() if not (finfo is None): finfo.write("Begin reducing ...\n") finfo.flush() sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm) sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm) if myid == 0: dummy = r.finish(True) else: if xysize == -1 and zsize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize) else: if zsize == -1: fftvol = sp_utilities.model_blank(xysize, xysize, imgsize) elif xysize == -1: fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize) else: fftvol = sp_utilities.model_blank(xysize, xysize, zsize) return fftvol
def run(): progname = optparse.os.path.basename(sys.argv[0]) usage = """%prog [options] input.pdb output.hdf Converts a pdb file into an electron density map. 0,0,0 in PDB space will map to the center of the volume.""" parser = optparse.OptionParser(usage=usage, version=EMAN2_meta.EMANVERSION) parser.add_option("--apix", "-A", type="float", help="Angstrom/voxel", default=1.0) parser.add_option("--box", "-B", type="string", help="Box size in pixels, <xyz> or <x,y,z>") parser.add_option("--het", action="store_true", help="Include HET atoms in the map", default=False) parser.add_option( "--chains", type="string", help= "String list of chain identifiers to include, e.g. 'ABEFG'; default: include all chains", default="", ) parser.add_option( "--center", type="string", default="a", help= "center: c - coordinates; a (default) - center of gravity; <x,y,z> - vector (in Angstrom) to subtract from all coordinates; n - none", ) parser.add_option("--O", action="store_true", default=False, help="use O system of coordinates") parser.add_option("--quiet", action="store_true", default=False, help="Verbose is the default") parser.add_option( "--tr0", type="string", default="none", help="Filename of initial 3x4 transformation matrix", ) parser.add_option( "--set_apix_value", action="store_true", help="Set apix value in header of the ouput map", default=False, ) (options, args) = parser.parse_args() # if len(args) < 2: sp_global_def.ERROR("Input and output files required") return if sp_global_def.CACHE_DISABLE: sp_utilities.disable_bdb_cache() chains = options.chains if chains == "": chains = None try: infile = open(args[0], "r") except: sp_global_def.ERROR("Cannot open input file") return aavg = [0, 0, 0] # calculate atomic center asig = [0, 0, 0] # to compute radius of gyration natm = 0 atoms = [] # we store a list of atoms to process to avoid multiple passes nelec = 0 mass = 0 # read in initial-transformation file: if options.tr0 != "none": cols = sp_utilities.read_text_file(options.tr0, -1) txlist = [] for i in range(3): txlist.append(cols[0][i]) txlist.append(cols[1][i]) txlist.append(cols[2][i]) txlist.append(cols[3][i]) tr0 = EMAN2_cppwrap.Transform(txlist) # parse the pdb file and pull out relevant atoms for line in infile: if line[:4] == "ATOM" or (line[:6] == "HETATM" and options.het): if chains and (line[21] not in chains): continue try: a = line[12:14].strip() aseq = int(line[6:11].strip()) res = int(line[22:26].strip()) if options.O: x = float(line[38:46]) y = float(line[30:38]) z = -float(line[46:54]) else: x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) except: sp_global_def.sxprint( "PDB Parse error:\n%s\n'%s','%s','%s' '%s','%s','%s'\n" % ( line, line[12:14], line[6:11], line[22:26], line[30:38], line[38:46], line[46:54], )) sp_global_def.sxprint(a, aseq, res, x, y, z) try: nelec += atomdefs[a.upper()][0] mass += atomdefs[a.upper()][1] except: sp_global_def.sxprint(( "Unknown atom %s ignored at %d. You can modify the atomdefs info at the top of this file with the atomic number and atomic weight." % (a, aseq))) continue atoms.append([a, x, y, z]) natm += 1 if options.center == "a": aavg[0] += x * atomdefs[a.upper()][1] aavg[1] += y * atomdefs[a.upper()][1] aavg[2] += z * atomdefs[a.upper()][1] asig[0] += x**2 * atomdefs[a.upper()][1] asig[1] += y**2 * atomdefs[a.upper()][1] asig[2] += z**2 * atomdefs[a.upper()][1] else: aavg[0] += x aavg[1] += y aavg[2] += z asig[0] += x**2 asig[1] += y**2 asig[2] += z**2 infile.close() if options.center == "a": rad_gyr = math.sqrt( old_div((asig[0] + asig[1] + asig[2]), mass) - (old_div(aavg[0], mass))**2 - (old_div(aavg[1], mass))**2 - (old_div(aavg[2], mass))**2) else: rad_gyr = math.sqrt( old_div((asig[0] + asig[1] + asig[2]), natm) - (old_div(aavg[0], natm))**2 - (old_div(aavg[1], natm))**2 - (old_div(aavg[2], natm))**2) if not options.quiet: sp_global_def.sxprint( "%d atoms; total charge = %d e-; mol mass = %.2f kDa; radius of gyration = %.2f A" % (natm, nelec, old_div(mass, 1000.0), rad_gyr)) # center PDB according to option: if options.center == "a": if not options.quiet: sp_global_def.sxprint( "center of gravity at %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % ( old_div(aavg[0], mass), old_div(aavg[1], mass), old_div(aavg[2], mass), )) for i in range(len(atoms)): atoms[i][1] -= old_div(aavg[0], mass) atoms[i][2] -= old_div(aavg[1], mass) atoms[i][3] -= old_div(aavg[2], mass) if options.center == "c": if not options.quiet: sp_global_def.sxprint( "atomic center at %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % ( old_div(aavg[0], natm), old_div(aavg[1], natm), old_div(aavg[2], natm), )) for i in range(len(atoms)): atoms[i][1] -= old_div(aavg[0], natm) atoms[i][2] -= old_div(aavg[1], natm) atoms[i][3] -= old_div(aavg[2], natm) spl = options.center.split(",") if len(spl) == 3: # substract the given vector from all coordinates if not options.quiet: sp_global_def.sxprint( "vector to substract: %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % (float(spl[0]), float(spl[1]), float(spl[2]))) for i in range(len(atoms)): atoms[i][1] -= float(spl[0]) atoms[i][2] -= float(spl[1]) atoms[i][3] -= float(spl[2]) # apply given initial transformation (this used to be done before centering, # thereby loosing the translation. This is the right place to apply tr0): if options.tr0 != "none": if not options.quiet: sp_global_def.sxprint( "Applying initial transformation to PDB coordinates... ") for i in range(len(atoms)): atom_coords = EMAN2_cppwrap.Vec3f(atoms[i][1], atoms[i][2], atoms[i][3]) new_atom_coords = tr0 * atom_coords atoms[i][1] = new_atom_coords[0] atoms[i][2] = new_atom_coords[1] atoms[i][3] = new_atom_coords[2] if not options.quiet: sp_global_def.sxprint("done.\n") # bounding box: amin = [atoms[0][1], atoms[0][2], atoms[0][3]] amax = [atoms[0][1], atoms[0][2], atoms[0][3]] for i in range(1, len(atoms)): for k in range(3): amin[k] = min(atoms[i][k + 1], amin[k]) amax[k] = max(atoms[i][k + 1], amax[k]) if not options.quiet: sp_global_def.sxprint("Range of coordinates [A]: x: %7.2f - %7.2f" % (amin[0], amax[0])) sp_global_def.sxprint(" y: %7.2f - %7.2f" % (amin[1], amax[1])) sp_global_def.sxprint(" z: %7.2f - %7.2f" % (amin[2], amax[2])) # find the output box size, either user specified or from bounding box box = [0, 0, 0] try: spl = options.box.split(",") if len(spl) == 1: box[0] = box[1] = box[2] = int(spl[0]) else: box[0] = int(spl[0]) box[1] = int(spl[1]) box[2] = int(spl[2]) except: for i in range(3): box[i] = int( old_div(2 * max(math.fabs(amax[i]), math.fabs(amin[i])), options.apix)) # Increase the box size by 1/4. box[i] += old_div(box[i], 4) if not options.quiet: sp_global_def.sxprint("Bounding box [pixels]: x: %5d " % box[0]) sp_global_def.sxprint(" y: %5d " % box[1]) sp_global_def.sxprint(" z: %5d " % box[2]) # figure oversampled box size # bigb = max(box[0],box[1],box[2]) fcbig = 1 """Multiline Comment0""" if not options.quiet: sp_global_def.sxprint( "Box size: %d x %d x %d" % (box[0], box[1], box[2]), ", oversampling ", fcbig, ) # Calculate working dimensions pixelbig = old_div(options.apix, fcbig) bigbox = [] for i in range(3): bigbox.append(box[i] * fcbig) # initialize the final output volume outmap = EMAN2_cppwrap.EMData(bigbox[0], bigbox[1], bigbox[2], True) nc = [] for i in range(3): nc.append(old_div(bigbox[i], 2)) # fill in the atoms for i in range(len(atoms)): # print "Adding %d '%s'"%(i,atoms[i][0]) if not options.quiet and i % 1000 == 0: sp_global_def.sxprint("\r %d" % i, end=" ") sys.stdout.flush() try: elec = atomdefs[atoms[i][0].upper()][0] # outmap[int(atoms[i][1]/pixelbig+bigbox[0]//2),int(atoms[i][2]/pixelbig+bigbox[1]//2),int(atoms[i][3]/pixelbig+bigbox[2]//2)] += elec for k in range(2): pz = old_div(atoms[i][3], pixelbig) + nc[2] dz = pz - int(pz) uz = ((1 - k) + (2 * k - 1) * dz) * elec for l in range(2): py = old_div(atoms[i][2], pixelbig) + nc[1] dy = py - int(py) uy = ((1 - l) + (2 * l - 1) * dy) * uz for m in range(2): px = old_div(atoms[i][1], pixelbig) + nc[0] dx = px - int(px) outmap[int(px) + m, int(py) + l, int(pz) + k] += ((1 - m) + (2 * m - 1) * dx) * uy except: sp_global_def.sxprint("Skipping %d '%s'" % (i, atoms[i][0])) if not options.quiet: sp_global_def.sxprint("\r %d\nConversion complete." % len(atoms)) # ," Now shape atoms." """Multiline Comment1""" (filename_path, filextension) = optparse.os.path.splitext(args[1]) if filextension == ".hdf": if options.set_apix_value: outmap.set_attr("apix_x", options.apix) outmap.set_attr("apix_y", options.apix) outmap.set_attr("apix_z", options.apix) outmap.set_attr("pixel_size", options.apix) else: sp_global_def.sxprint("Pixel_size is not set in the header!") outmap.write_image(args[1], 0, EMAN2_cppwrap.EMUtil.ImageType.IMAGE_HDF) elif filextension == ".spi": outmap.write_image(args[1], 0, EMAN2_cppwrap.EMUtil.ImageType.IMAGE_SINGLE_SPIDER) else: sp_global_def.ERROR("Unknown image type") return
def runcheck(classavgstack, reconfile, outdir, inangles=None, selectdoc=None, prjmethod='trilinear', displayYN=False, projstack='proj.hdf', outangles='angles.txt', outstack='comp-proj-reproj.hdf', normstack='comp-proj-reproj-norm.hdf'): print("\n%s, Modified 2018-12-07\n" % __file__) # Check if inputs exist check(classavgstack) check(reconfile) # Create directory if it doesn't exist if not os.path.isdir(outdir): os.makedirs(outdir) # os.mkdir() can only operate one directory deep print("mkdir -p %s" % outdir) # Expand path for outputs projstack = os.path.join(outdir, projstack) outangles = os.path.join(outdir, outangles) outstack = os.path.join(outdir, outstack) normstack = os.path.join(outdir, normstack) # Get number of images nimg0 = EMAN2_cppwrap.EMUtil.get_image_count(classavgstack) recon = EMAN2_cppwrap.EMData(reconfile) nx = recon.get_xsize() # In case class averages include discarded images, apply selection file if selectdoc: goodavgs, extension = os.path.splitext(classavgstack) newclasses = goodavgs + "_kept" + extension # e2proc2d appends to existing files, so rename existing output if os.path.exists(newclasses): renamefile = newclasses + '.bak' os.rename(newclasses, renamefile) print("mv %s %s" % (newclasses, renamefile)) cmd7="e2proc2d.py %s %s --list=%s" % (classavgstack, newclasses, selectdoc) print(cmd7) os.system(cmd7) # Update class-averages classavgstack = newclasses # Import Euler angles if inangles: cmd6 = "sxheader.py %s --params=xform.projection --import=%s" % (classavgstack, inangles) print(cmd6) header(classavgstack, 'xform.projection', fimport=inangles) try: header(classavgstack, 'xform.projection', fexport=outangles) cmd1 = "sxheader.py %s --params=xform.projection --export=%s" % (classavgstack, outangles) print(cmd1) except RuntimeError: print("\nERROR!! No projection angles found in class-average stack header!\n") print('Usage:', USAGE) exit() #cmd2="sxproject3d.py %s %s --angles=%s" % (recon, projstack, outangles) #print(cmd2) #os.system(cmd2) # Here if you want to be fancy, there should be an option to chose the projection method, # the mechanism can be copied from sxproject3d.py PAP if prjmethod=='trilinear': method_num = 1 elif prjmethod=='gridding': method_num = -1 elif prjmethod=='nn': method_num = 0 else: print("\nERROR!! Valid projection methods are: trilinear (default), gridding, and nn (nearest neighbor).") print('Usage:', USAGE) exit() #project3d(recon, stack=projstack, listagls=outangles) recon = prep_vol(recon, npad = 2, interpolation_method = 1) result=[] # Here you need actual radius to compute proper ccc's, but if you do, you have to deal with translations, PAP mask = model_circle(nx//2-2,nx,nx) # Number of images may have changed nimg1 = EMAN2_cppwrap.EMUtil.get_image_count(classavgstack) outangles = read_text_row(outangles) for imgnum in range(nimg1): # get class average classimg = get_im(classavgstack, imgnum) # compute re-projection prjimg = prgl(recon, outangles[imgnum], 1, False) # calculate 1D power spectra rops_dst = rops_table(classimg*mask) rops_src = rops_table(prjimg) # Set power spectrum of reprojection to the data. # Since data has an envelope, it would make more sense to set data to reconstruction, # but to do it one would have to know the actual resolution of the data. # you can check sxprocess.py --adjpw to see how this is done properly PAP table = [0.0]*len(rops_dst) # initialize table for j in range( len(rops_dst) ): table[j] = sqrt( old_div(rops_dst[j],rops_src[j]) ) prjimg = fft(filt_table(prjimg, table)) # match FFT amplitdes of re-projection and class average cccoeff = ccc(prjimg, classimg, mask) #print(imgnum, cccoeff) classimg.set_attr_dict({'cross-corr':cccoeff}) prjimg.set_attr_dict({'cross-corr':cccoeff}) prjimg.write_image(outstack,2*imgnum) classimg.write_image(outstack, 2*imgnum+1) result.append(cccoeff) del outangles meanccc = old_div(sum(result),nimg1) print("Average CCC is %s" % meanccc) nimg2 = EMAN2_cppwrap.EMUtil.get_image_count(outstack) for imgnum in xrange(nimg2): if (imgnum % 2 ==0): prjimg = get_im(outstack,imgnum) meanccc1 = prjimg.get_attr_default('mean-cross-corr', -1.0) prjimg.set_attr_dict({'mean-cross-corr':meanccc}) write_header(outstack,prjimg,imgnum) if (imgnum % 100) == 0: print(imgnum) # e2proc2d appends to existing files, so delete existing output if os.path.exists(normstack): os.remove(normstack) print("rm %s" % normstack) # Why would you want to do it? If you do, it should have been done during ccc calculations, # otherwise what is see is not corresponding to actual data, thus misleading. PAP #cmd5="e2proc2d.py %s %s --process=normalize" % (outstack, normstack) #print(cmd5) #os.system(cmd5) # Optionally pop up e2display if displayYN: cmd8 = "e2display.py %s" % outstack print(cmd8) os.system(cmd8) print("Done!")