def align_diff_textfile(textfile1, textfile2): ''' This function determines the relative angle, shifts and mirrorness between the two textfile of alignment parameters ''' from utilities import read_text_row ali1 = read_text_row(textfile1, "", "") ali2 = read_text_row(textfile2, "", "") nima = len(ali1) nima2 = len(ali2) if nima2 != nima: print "Error: Number of images don't agree!" return 0.0, 0.0, 0.0, 0 else: del nima2 # Read the alignment parameters and determine the relative mirrorness ali_params1 = [] ali_params2 = [] for i in xrange(nima): ali_params1.extend(ali1[i][0:4]) ali_params2.extend(ali2[i][0:4]) return align_diff_params(ali_params1, ali_params2)
def measure_for_outlier_criterion(criterion_name, masterdir, rviper_iter, list_of_viper_run_indices): # main_iterations = [NAME_OF_MAIN_DIR + "%03d" % i for i in range(1, rviper_iter + 1)] main_iterations = [NAME_OF_MAIN_DIR + "%03d" % i for i in range(rviper_iter, rviper_iter + 1)] mainoutputdir = masterdir + DIR_DELIM + main_iterations[0] + DIR_DELIM p = [] for i1 in list_of_viper_run_indices: p.append(read_text_row(mainoutputdir + NAME_OF_RUN_DIR + "%03d" % (i1) + DIR_DELIM + "params.txt")) subset, avg_diff_per_image, outp = find_common_subset(p, 0) avg_diff_per_image.sort() x1 = len(avg_diff_per_image) y1 = avg_diff_per_image[-1] if y1 <= ANGLE_ERROR_THRESHOLD: return TRIPLET_WITH_ANGLE_ERROR_LESS_THAN_THRESHOLD_HAS_BEEN_FOUND if criterion_name == "80th percentile": return avg_diff_per_image[int(x1*PERCENT_THRESHOLD_X)]/y1 elif criterion_name == "fastest increase in the last quartile": for k in range(5,6): avg_diff_per_image_diff = [x - avg_diff_per_image[i - k] for i, x in enumerate(avg_diff_per_image)][k:] avg_diff_per_image_diff_max = max(avg_diff_per_image_diff) avg_diff_per_image_diff_max_normalized = max(avg_diff_per_image_diff)/y1 if avg_diff_per_image_diff.index(avg_diff_per_image_diff_max) >= int(x1*0.75): return avg_diff_per_image_diff_max_normalized return 0.0 else: print "Error, no criterion name is specified!" mpi_finalize() sys.exit()
def ave_ali_err_textfile(textfile1, textfile2, r=25): ''' This function determines the relative angle, shifts and mirrorness between the two sets of alignment parameters. It also calculates the mirror consistent rate and average pixel error between two sets of parameters. ''' from utilities import combine_params2 from math import sqrt, sin, pi from utilities import read_text_row ali1 = read_text_row(textfile1, "", "") ali2 = read_text_row(textfile2, "", "") nima = len(ali1) nima2 = len(ali2) if nima2 != nima: print "Error: Number of images don't agree!" return 0.0, 0.0, 0.0, 0, 0.0, 0.0 else: del nima2 # Read the alignment parameters ali_params1 = [] ali_params2 = [] for i in xrange(nima): ali_params1.extend(ali1[i][0:4]) ali_params2.extend(ali2[i][0:4]) # Determine relative angle, shift and mirror alphai, sxi, syi, mirror = align_diff_params(ali_params1, ali_params2) # Determine the average pixel error nima = len(ali_params1)/4 mirror_same = 0 err = 0.0 for i in xrange(nima): alpha1, sx1, sy1, mirror1 = ali_params1[i*4:i*4+4] alpha2, sx2, sy2, mirror2 = ali_params2[i*4:i*4+4] if abs(mirror1-mirror2) == mirror: mirror_same += 1 alpha12, sx12, sy12, mirror12 = combine_params2(alpha1, sx1, sy1, int(mirror1), alphai, sxi, syi, 0) err += max_2D_pixel_error([alpha12, sx12, sy12], [alpha2, sx2, sy2], r) return alphai, sxi, syi, mirror, float(mirror_same)/nima, err/mirror_same
def defocus_get_Eudis(fnam_roo, volt=300, Pixel_size=1, Cs=2, wgh=.1, f_start=0, f_stop=-1, docf="a" ,skip="#", round_off=1, nr1=3, nr2=6): """ 1. Estimating envelope function and baseline noise using constrained simplex method so as to extract CTF imprints from 1D power spectrum 2. Based one extracted ctf imprints, perform exhaustive defocus searching to get defocus which matches the extracted CTF imprints 3. It returns Euclidean distance for defocus selection """ from math import sqrt, atan from utilities import read_text_row, generate_ctf roo = [] res = [] if docf == "a": TMP_roo = read_text_row(fnam_roo, "a", skip) for i in xrange(len(TMP_roo)): # remove first record roo.append(TMP_roo[i][1]) else: skip = ";" TMP_roo = read_text_row(fnam_roo, "s", skip) for i in xrange(len(TMP_roo)): # remove first record roo.append(TMP_roo[i][2]) Res_roo = [] Res_TE = [] if f_start == 0 : i_start=0 else: i_start=int(Pixel_size*2.*len(roo)/f_start) if f_stop <= i_start : i_stop=len(roo) else: i_stop=int(Pixel_size*2.*len(roo)/f_stop) TE = defocus_env_baseline_fit(roo, i_start, i_stop, int(nr1), 4) Pn1 = defocus_env_baseline_fit(roo, i_start, i_stop, int(nr2), 3) Res_roo = [] Res_TE = [] for i in xrange(len(roo)): Res_roo.append( roo[i] - Pn1[i] ) Res_TE.append( TE[i] - Pn1[i] ) # defocus=defocus_guess(Res_roo, Res_TE, volt, Cs, Pixel_size, wgh, i_start, i_stop, 2, round_off) nx = int(len(roo)*2) ctf = ctf_2(nx, generate_ctf([defocus,Cs,voltage,Pixel_size, 0.0, wgh])) for i in xrange(len(Res_TE)): ctf[i]=ctf[i]*Res_TE[i] dis = defocus_L2_euc(ctf, Res_roo, i_start, i_stop) return [defocus, dis]
def defocus_get(fnam_roo, volt=300, Pixel_size=1, Cs=2, wgh=.1, f_start=0, f_stop=-1, docf="a", skip="#", round_off=1, nr1=3, nr2=6): """ 1.Estimating envelope function and baseline noise using constrained simplex method so as to extract CTF imprints from 1D power spectrum 2. Based one extracted ctf imprints, perform exhaustive defocus searching to get defocus which matches the extracted CTF imprints """ from math import sqrt, atan from utilities import read_text_row roo = [] res = [] if(docf == "a"): TMP_roo = read_text_row(fnam_roo, "a", skip) for i in xrange(len(TMP_roo)): roo.append(TMP_roo[i][1]) else: TMP_roo=read_text_row(fnam_roo,"s",";") for i in xrange(len(TMP_roo)): roo.append(TMP_roo[i][2]) Res_roo = [] Res_TE = [] if f_start == 0 : i_start=0 else: i_start=int(Pixel_size*2.*len(roo)/f_start) if f_stop <= i_start : i_stop=len(roo) else: i_stop=int(Pixel_size*2.*len(roo)/f_stop) TE = defocus_env_baseline_fit(roo, i_start, i_stop, int(nr1), 4) Pn1 = defocus_env_baseline_fit(roo, i_start, i_stop, int(nr2), 3) Res_roo = [] Res_TE = [] for i in xrange(len(roo)): Res_roo.append(roo[i] - Pn1[i]) Res_TE.append( TE[i] - Pn1[i]) # defocus=defocus_guess(Res_roo, Res_TE, volt, Cs, Pixel_size, wgh, i_start, i_stop, 2, round_off) del roo del TE del Pn1 del Res_TE del Res_roo return defocus
def plot_errors_between_any_number_of_projections(masterdir, rviper_iter, list_of_projection_indices, error_value): import matplotlib.pyplot as plt # # for debugging purposes # if "counter" not in plot_errors_between_any_number_of_projections.__dict__: # plot_errors_between_any_number_of_projections.counter = 0 # plot_errors_between_any_number_of_projections.counter += 1 # main_iterations = [NAME_OF_MAIN_DIR + "%03d" % i for i in range(1, rviper_iter + 1)] main_iterations = [NAME_OF_MAIN_DIR + "%03d" % i for i in range(rviper_iter, rviper_iter + 1)] mainoutputdir = masterdir + DIR_DELIM + main_iterations[0] + DIR_DELIM p=[] for i1 in list_of_projection_indices: p.append(read_text_row(mainoutputdir + NAME_OF_RUN_DIR + "%03d"%(i1) + DIR_DELIM + "params.txt")) ti1, ti3, out = find_common_subset(p,0) u = [] for i in xrange(len(ti3)): u.append([ti3[i],i]) u.sort() # EMAN2.display([range(len(u)),[u[i][0] for i in xrange(len(u))]]) plt.plot(range(len(u)),[u[i][0] for i in xrange(len(u))]) # import json; f = open("error_curve%03d.json"%plot_errors_between_any_number_of_projections.counter, 'w') # json.dump([u[i][0] for i in xrange(len(u))],f); f.close() plt.ylabel('Error') plt.xlabel('Image index') plt.title("Sorted errors between projections") import StringIO which_projections = StringIO.StringIO() which_projections.write("_" + "%.6f"%error_value) for p_i in list_of_projection_indices: which_projections.write("_" + "%03d"%p_i) for p_i in list_of_projection_indices: which_projections.write("___" + "%03d"%get_already_processed_viper_runs.r_permutation[p_i]) plt.savefig(mainoutputdir + '/sorted_errors_between_projections' + which_projections.getvalue() + '.png') which_projections.close() plt.close()
def main(): import sys import os import math import random import pyemtbx.options import time from random import random, seed, randint from optparse import OptionParser progname = os.path.basename(sys.argv[0]) usage = progname + """ [options] <inputfile> <outputfile> Forms chains of 2D images based on their similarities. Functionality: 4. Order a 2-D stack of image based on pair-wise similarity (computed as a cross-correlation coefficent). Options 1-3 require image stack to be aligned. The program will apply orientation parameters if present in headers. The ways to use the program: 4.1 Use option initial to specify which image will be used as an initial seed to form the chain. sxprocess.py input_stack.hdf output_stack.hdf --initial=23 --radius=25 4.2 If options initial is omitted, the program will determine which image best serves as initial seed to form the chain sxprocess.py input_stack.hdf output_stack.hdf --radius=25 4.3 Use option circular to form a circular chain. sxprocess.py input_stack.hdf output_stack.hdf --circular--radius=25 4.4 New circular code based on pairwise alignments sxprocess.py aclf.hdf chain.hdf circle.hdf --align --radius=25 --xr=2 --pairwiseccc=lcc.txt 4.5 Circular ordering based on pairwise alignments sxprocess.py vols.hdf chain.hdf mask.hdf --dd --radius=25 """ parser = OptionParser(usage,version=SPARXVERSION) parser.add_option("--dd", action="store_true", help="Circular ordering without adjustment of orientations", default=False) parser.add_option("--circular", action="store_true", help="Select circular ordering (first image has to be similar to the last)", default=False) parser.add_option("--align", action="store_true", help="Compute all pairwise alignments and for the table of their similarities find the best chain", default=False) parser.add_option("--initial", type="int", default=-1, help="Specifies which image will be used as an initial seed to form the chain. (default = 0, means the first image)") parser.add_option("--radius", type="int", default=-1, help="Radius of a circular mask for similarity based ordering") # import params for 2D alignment parser.add_option("--ou", type="int", default=-1, help="outer radius for 2D alignment < nx/2-1 (set to the radius of the particle)") parser.add_option("--xr", type="int", default=0, help="range for translation search in x direction, search is +/xr (0)") parser.add_option("--yr", type="int", default=0, help="range for translation search in y direction, search is +/yr (0)") #parser.add_option("--nomirror", action="store_true", default=False, help="Disable checking mirror orientations of images (default False)") parser.add_option("--pairwiseccc", type="string", default= None, help="Input/output pairwise ccc file") (options, args) = parser.parse_args() global_def.BATCH = True if options.dd: nargs = len(args) if nargs != 3: print "must provide name of input and two output files!" return stack = args[0] new_stack = args[1] from utilities import model_circle from statistics import ccc from statistics import mono lend = EMUtil.get_image_count(stack) lccc = [None]*(lend*(lend-1)/2) for i in xrange(lend-1): v1 = get_im( stack, i ) if( i == 0 and nargs == 2): nx = v1.get_xsize() ny = v1.get_ysize() nz = v1.get_ysize() if options.ou < 1 : radius = nx//2-2 else: radius = options.ou mask = model_circle(radius, nx, ny, nz) else: mask = get_im(args[2]) for j in xrange(i+1, lend): lccc[mono(i,j)] = [ccc(v1, get_im( stack, j ), mask), 0] order = tsp(lccc) if(len(order) != lend): print " problem with data length" from sys import exit exit() print "Total sum of cccs :",TotalDistance(order, lccc) print "ordering :",order for i in xrange(lend): get_im(stack, order[i]).write_image( new_stack, i ) elif options.align: nargs = len(args) if nargs != 3: print "must provide name of input and two output files!" return from utilities import get_params2D, model_circle from fundamentals import rot_shift2D from statistics import ccc from time import time from alignment import align2d, align2d_scf from multi_shc import mult_transform stack = args[0] new_stack = args[1] d = EMData.read_images(stack) """ # will align anyway try: ttt = d[0].get_attr('xform.params2d') for i in xrange(len(d)): alpha, sx, sy, mirror, scale = get_params2D(d[i]) d[i] = rot_shift2D(d[i], alpha, sx, sy, mirror) except: pass """ nx = d[0].get_xsize() ny = d[0].get_ysize() if options.ou < 1 : radius = nx//2-2 else: radius = options.ou mask = model_circle(radius, nx, ny) if(options.xr < 0): xrng = 0 else: xrng = options.xr if(options.yr < 0): yrng = xrng else: yrng = options.yr initial = max(options.initial, 0) from statistics import mono lend = len(d) lccc = [None]*(lend*(lend-1)/2) from utilities import read_text_row if options.pairwiseccc == None or not os.path.exists(options.pairwiseccc) : st = time() for i in xrange(lend-1): for j in xrange(i+1, lend): # j>i meaning mono entry (i,j) or (j,i) indicates T i->j (from smaller index to larger) #alpha, sx, sy, mir, peak = align2d(d[i],d[j], xrng, yrng, step=options.ts, first_ring=options.ir, last_ring=radius, mode = "F") alpha, sx, sy, mir, peak = align2d_scf(d[i],d[j], xrng, yrng, ou=radius) lccc[mono(i,j)] = [ccc(d[j], rot_shift2D(d[i], alpha, sx, sy, mir, 1.0), mask), alpha, sx, sy, mir] #print " %4d %10.1f"%(i,time()-st) if(not os.path.exists(options.pairwiseccc)): from utilities import write_text_row write_text_row([[initial,0,0,0,0]]+lccc,options.pairwiseccc) elif(os.path.exists(options.pairwiseccc)): lccc = read_text_row(options.pairwiseccc) initial = int(lccc[0][0] + 0.1) del lccc[0] for i in xrange(len(lccc)): T = Transform({"type":"2D","alpha":lccc[i][1],"tx":lccc[i][2],"ty":lccc[i][3],"mirror":int(lccc[i][4]+0.1)}) lccc[i] = [lccc[i][0],T] tdummy = Transform({"type":"2D"}) maxsum = -1.023 for m in xrange(0,lend):#initial, initial+1): indc = range( lend ) lsnake = [[m, tdummy, 0.0]] del indc[m] lsum = 0.0 while len(indc) > 1: maxcit = -111. for i in xrange(len(indc)): cuc = lccc[mono(indc[i], lsnake[-1][0])][0] if cuc > maxcit: maxcit = cuc qi = indc[i] # Here we need transformation from the current to the previous, # meaning indc[i] -> lsnake[-1][0] T = lccc[mono(indc[i], lsnake[-1][0])][1] # If direction is from larger to smaller index, the transformation has to be inverted if( indc[i] > lsnake[-1][0] ): T = T.inverse() lsnake.append([qi,T, maxcit]) lsum += maxcit del indc[indc.index(qi)] T = lccc[mono(indc[-1], lsnake[-1][0])][1] if( indc[-1] > lsnake[-1][0]): T = T.inverse() lsnake.append([indc[-1], T, lccc[mono(indc[-1], lsnake[-1][0])][0]]) print " initial image and lsum ",m,lsum #print lsnake if(lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in xrange(lend)] print " Initial image selected : ",init,maxsum," ",TotalDistance([snake[m][0] for m in xrange(lend)], lccc) #for q in snake: print q from copy import deepcopy trans=deepcopy([snake[i][1] for i in xrange(len(snake))]) print [snake[i][0] for i in xrange(len(snake))] """ for m in xrange(lend): prms = trans[m].get_params("2D") print " %3d %7.1f %7.1f %7.1f %2d %6.2f"%(snake[m][0], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"], snake[m][2]) """ for k in xrange(lend-2,0,-1): T = snake[k][1] for i in xrange(k+1, lend): trans[i] = T*trans[i] # To add - apply all transformations and do the overall centering. for m in xrange(lend): prms = trans[m].get_params("2D") #print " %3d %7.1f %7.1f %7.1f %2d %6.2f"%(snake[m][0], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"], snake[m][2]) #rot_shift2D(d[snake[m][0]], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"]).write_image(new_stack, m) rot_shift2D(d[snake[m][0]], prms["alpha"], 0.0,0.0, prms["mirror"]).write_image(new_stack, m) order = tsp(lccc) if(len(order) != lend): print " problem with data length" from sys import exit exit() print TotalDistance(order, lccc) print order ibeg = order.index(init) order = [order[(i+ibeg)%lend] for i in xrange(lend)] print TotalDistance(order, lccc) print order snake = [tdummy] for i in xrange(1,lend): # Here we need transformation from the current to the previous, # meaning order[i] -> order[i-1]] T = lccc[mono(order[i], order[i-1])][1] # If direction is from larger to smaller index, the transformation has to be inverted if( order[i] > order[i-1] ): T = T.inverse() snake.append(T) assert(len(snake) == lend) from copy import deepcopy trans = deepcopy(snake) for k in xrange(lend-2,0,-1): T = snake[k] for i in xrange(k+1, lend): trans[i] = T*trans[i] # Try to smooth the angles - complicated, I am afraid one would have to use angles forward and backwards # and find their average?? # In addition, one would have to recenter them """ trms = [] for m in xrange(lend): prms = trans[m].get_params("2D") trms.append([prms["alpha"], prms["mirror"]]) for i in xrange(3): for m in xrange(lend): mb = (m-1)%lend me = (m+1)%lend # angles order mb,m,me # calculate predicted angles mb->m """ for m in xrange(lend): prms = trans[m].get_params("2D") #rot_shift2D(d[order[m]], prms["alpha"], prms["tx"], prms["ty"], prms["mirror"]).write_image("metro.hdf", m) rot_shift2D(d[order[m]], prms["alpha"], 0.0,0.0, prms["mirror"]).write_image(args[2], m) """ # This was an effort to get number of loops, inconclusive, to say the least from numpy import outer, zeros, float32, sqrt lend = len(d) cor = zeros(lend,float32) cor = outer(cor, cor) for i in xrange(lend): cor[i][i] = 1.0 for i in xrange(lend-1): for j in xrange(i+1, lend): cor[i,j] = lccc[mono(i,j)][0] cor[j,i] = cor[i,j] lmbd, eigvec = pca(cor) from utilities import write_text_file nvec=20 print [lmbd[j] for j in xrange(nvec)] print " G" mm = [-1]*lend for i in xrange(lend): # row mi = -1.0e23 for j in xrange(nvec): qt = eigvec[j][i] if(abs(qt)>mi): mi = abs(qt) mm[i] = j for j in xrange(nvec): qt = eigvec[j][i] print round(qt,3), # eigenvector print mm[i] print for j in xrange(nvec): qt = [] for i in xrange(lend): if(mm[i] == j): qt.append(i) if(len(qt)>0): write_text_file(qt,"loop%02d.txt"%j) """ """ print [lmbd[j] for j in xrange(nvec)] print " B" mm = [-1]*lend for i in xrange(lend): # row mi = -1.0e23 for j in xrange(nvec): qt = eigvec[j][i]/sqrt(lmbd[j]) if(abs(qt)>mi): mi = abs(qt) mm[i] = j for j in xrange(nvec): qt = eigvec[j][i]/sqrt(lmbd[j]) print round(qt,3), # eigenvector print mm[i] print """ """ lend=3 cor = zeros(lend,float32) cor = outer(cor, cor) cor[0][0] =136.77 cor[0][1] = 79.15 cor[0][2] = 37.13 cor[1][0] = 79.15 cor[2][0] = 37.13 cor[1][1] = 50.04 cor[1][2] = 21.65 cor[2][1] = 21.65 cor[2][2] = 13.26 lmbd, eigvec = pca(cor) print lmbd print eigvec for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i], # eigenvector print print " B" for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i]/sqrt(lmbd[j]), # eigenvector print print " G" for i in xrange(lend): # row for j in xrange(lend): print eigvec[j][i]*sqrt(lmbd[j]), # eigenvector print """ else: nargs = len(args) if nargs != 2: print "must provide name of input and output file!" return from utilities import get_params2D, model_circle from fundamentals import rot_shift2D from statistics import ccc from time import time from alignment import align2d from multi_shc import mult_transform stack = args[0] new_stack = args[1] d = EMData.read_images(stack) try: ttt = d[0].get_attr('xform.params2d') for i in xrange(len(d)): alpha, sx, sy, mirror, scale = get_params2D(d[i]) d[i] = rot_shift2D(d[i], alpha, sx, sy, mirror) except: pass nx = d[0].get_xsize() ny = d[0].get_ysize() if options.radius < 1 : radius = nx//2-2 else: radius = options.radius mask = model_circle(radius, nx, ny) init = options.initial if init > -1 : print " initial image: %d" % init temp = d[init].copy() temp.write_image(new_stack, 0) del d[init] k = 1 lsum = 0.0 while len(d) > 1: maxcit = -111. for i in xrange(len(d)): cuc = ccc(d[i], temp, mask) if cuc > maxcit: maxcit = cuc qi = i # print k, maxcit lsum += maxcit temp = d[qi].copy() del d[qi] temp.write_image(new_stack, k) k += 1 print lsum d[0].write_image(new_stack, k) else: if options.circular : print "Using options.circular" # figure the "best circular" starting image maxsum = -1.023 for m in xrange(len(d)): indc = range(len(d) ) lsnake = [-1]*(len(d)+1) lsnake[0] = m lsnake[-1] = m del indc[m] temp = d[m].copy() lsum = 0.0 direction = +1 k = 1 while len(indc) > 1: maxcit = -111. for i in xrange(len(indc)): cuc = ccc(d[indc[i]], temp, mask) if cuc > maxcit: maxcit = cuc qi = indc[i] lsnake[k] = qi lsum += maxcit del indc[indc.index(qi)] direction = -direction for i in xrange( 1,len(d) ): if( direction > 0 ): if(lsnake[i] == -1): temp = d[lsnake[i-1]].copy() #print " forw ",lsnake[i-1] k = i break else: if(lsnake[len(d) - i] == -1): temp = d[lsnake[len(d) - i +1]].copy() #print " back ",lsnake[len(d) - i +1] k = len(d) - i break lsnake[lsnake.index(-1)] = indc[-1] #print " initial image and lsum ",m,lsum #print lsnake if(lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in xrange(len(d))] print " Initial image selected : ",init,maxsum print lsnake for m in xrange(len(d)): d[snake[m]].write_image(new_stack, m) else: # figure the "best" starting image maxsum = -1.023 for m in xrange(len(d)): indc = range(len(d) ) lsnake = [m] del indc[m] temp = d[m].copy() lsum = 0.0 while len(indc) > 1: maxcit = -111. for i in xrange(len(indc)): cuc = ccc(d[indc[i]], temp, mask) if cuc > maxcit: maxcit = cuc qi = indc[i] lsnake.append(qi) lsum += maxcit temp = d[qi].copy() del indc[indc.index(qi)] lsnake.append(indc[-1]) #print " initial image and lsum ",m,lsum #print lsnake if(lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in xrange(len(d))] print " Initial image selected : ",init,maxsum print lsnake for m in xrange(len(d)): d[snake[m]].write_image(new_stack, m)
def main(): import sys import os import math import random import pyemtbx.options import time from random import random, seed, randint from optparse import OptionParser progname = os.path.basename(sys.argv[0]) usage = progname + """ [options] <inputfile> <outputfile> Forms chains of 2D images based on their similarities. Functionality: 4. Order a 2-D stack of image based on pair-wise similarity (computed as a cross-correlation coefficent). Options 1-3 require image stack to be aligned. The program will apply orientation parameters if present in headers. The ways to use the program: 4.1 Use option initial to specify which image will be used as an initial seed to form the chain. sxprocess.py input_stack.hdf output_stack.hdf --initial=23 --radius=25 4.2 If options initial is omitted, the program will determine which image best serves as initial seed to form the chain sxprocess.py input_stack.hdf output_stack.hdf --radius=25 4.3 Use option circular to form a circular chain. sxprocess.py input_stack.hdf output_stack.hdf --circular--radius=25 4.4 New circular code based on pairwise alignments sxprocess.py aclf.hdf chain.hdf circle.hdf --align --radius=25 --xr=2 --pairwiseccc=lcc.txt 4.5 Circular ordering based on pairwise alignments sxprocess.py vols.hdf chain.hdf mask.hdf --dd --radius=25 """ parser = OptionParser(usage,version=SPARXVERSION) parser.add_option("--dd", action="store_true", help="Circular ordering without adjustment of orientations", default=False) parser.add_option("--circular", action="store_true", help="Select circular ordering (first image has to be similar to the last)", default=False) parser.add_option("--align", action="store_true", help="Compute all pairwise alignments and for the table of their similarities find the best chain", default=False) parser.add_option("--initial", type="int", default=-1, help="Specifies which image will be used as an initial seed to form the chain. (default = 0, means the first image)") parser.add_option("--radius", type="int", default=-1, help="Radius of a circular mask for similarity based ordering") # import params for 2D alignment parser.add_option("--ou", type="int", default=-1, help="outer radius for 2D alignment < nx/2-1 (set to the radius of the particle)") parser.add_option("--xr", type="int", default=0, help="range for translation search in x direction, search is +/xr (0)") parser.add_option("--yr", type="int", default=0, help="range for translation search in y direction, search is +/yr (0)") #parser.add_option("--nomirror", action="store_true", default=False, help="Disable checking mirror orientations of images (default False)") parser.add_option("--pairwiseccc", type="string", default= None, help="Input/output pairwise ccc file") (options, args) = parser.parse_args() global_def.BATCH = True if options.dd: nargs = len(args) if nargs != 3: print "must provide name of input and two output files!" return stack = args[0] new_stack = args[1] from utilities import model_circle from statistics import ccc from statistics import mono lend = EMUtil.get_image_count(stack) lccc = [None]*(lend*(lend-1)/2) for i in xrange(lend-1): v1 = get_im( stack, i ) if( i == 0 and nargs == 2): nx = v1.get_xsize() ny = v1.get_ysize() nz = v1.get_ysize() if options.ou < 1 : radius = nx//2-2 else: radius = options.ou mask = model_circle(radius, nx, ny, nz) else: mask = get_im(args[2]) for j in xrange(i+1, lend): lccc[mono(i,j)] = [ccc(v1, get_im( stack, j ), mask), 0] order = tsp(lccc) if(len(order) != lend): print " problem with data length" from sys import exit exit() print "Total sum of cccs :",TotalDistance(order, lccc) print "ordering :",order for i in xrange(lend): get_im(stack, order[i]).write_image( new_stack, i ) elif options.align: nargs = len(args) if nargs != 3: print "must provide name of input and two output files!" return from utilities import get_params2D, model_circle from fundamentals import rot_shift2D from statistics import ccc from time import time from alignment import align2d, align2d_scf from multi_shc import mult_transform stack = args[0] new_stack = args[1] d = EMData.read_images(stack) """ # will align anyway try: ttt = d[0].get_attr('xform.params2d') for i in xrange(len(d)): alpha, sx, sy, mirror, scale = get_params2D(d[i]) d[i] = rot_shift2D(d[i], alpha, sx, sy, mirror) except: pass """ nx = d[0].get_xsize() ny = d[0].get_ysize() if options.ou < 1 : radius = nx//2-2 else: radius = options.ou mask = model_circle(radius, nx, ny) if(options.xr < 0): xrng = 0 else: xrng = options.xr if(options.yr < 0): yrng = xrng else: yrng = options.yr initial = max(options.initial, 0) from statistics import mono lend = len(d) lccc = [None]*(lend*(lend-1)/2) from utilities import read_text_row if options.pairwiseccc == None or not os.path.exists(options.pairwiseccc) : st = time() for i in xrange(lend-1): for j in xrange(i+1, lend): # j>i meaning mono entry (i,j) or (j,i) indicates T i->j (from smaller index to larger) #alpha, sx, sy, mir, peak = align2d(d[i],d[j], xrng, yrng, step=options.ts, first_ring=options.ir, last_ring=radius, mode = "F") alpha, sx, sy, mir, peak = align2d_scf(d[i],d[j], xrng, yrng, ou=radius) lccc[mono(i,j)] = [ccc(d[j], rot_shift2D(d[i], alpha, sx, sy, mir, 1.0), mask), alpha, sx, sy, mir] #print " %4d %10.1f"%(i,time()-st) if(not os.path.exists(options.pairwiseccc)): from utilities import write_text_row write_text_row([[initial,0,0,0,0]]+lccc,options.pairwiseccc) elif(os.path.exists(options.pairwiseccc)): lccc = read_text_row(options.pairwiseccc) initial = int(lccc[0][0] + 0.1) del lccc[0] for i in xrange(len(lccc)): T = Transform({"type":"2D","alpha":lccc[i][1],"tx":lccc[i][2],"ty":lccc[i][3],"mirror":int(lccc[i][4]+0.1)}) lccc[i] = [lccc[i][0],T] tdummy = Transform({"type":"2D"}) maxsum = -1.023 for m in xrange(0,lend):#initial, initial+1): indc = range( lend ) lsnake = [[m, tdummy, 0.0]] del indc[m] lsum = 0.0 while len(indc) > 1: maxcit = -111. for i in xrange(len(indc)): cuc = lccc[mono(indc[i], lsnake[-1][0])][0] if cuc > maxcit: maxcit = cuc qi = indc[i] # Here we need transformation from the current to the previous, # meaning indc[i] -> lsnake[-1][0] T = lccc[mono(indc[i], lsnake[-1][0])][1] # If direction is from larger to smaller index, the transformation has to be inverted if( indc[i] > lsnake[-1][0] ): T = T.inverse() lsnake.append([qi,T, maxcit]) lsum += maxcit del indc[indc.index(qi)] T = lccc[mono(indc[-1], lsnake[-1][0])][1] if( indc[-1] > lsnake[-1][0]): T = T.inverse() lsnake.append([indc[-1], T, lccc[mono(indc[-1], lsnake[-1][0])][0]]) print " initial image and lsum ",m,lsum #print lsnake if(lsum > maxsum): maxsum = lsum init = m snake = [lsnake[i] for i in xrange(lend)] print " Initial image selected : ",init,maxsum," ",TotalDistance([snake[m][0] for m in xrange(lend)], lccc) #for q in snake: print q from copy import deepcopy trans=deepcopy([snake[i][1] for i in xrange(len(snake))]) print [snake[i][0] for i in xrange(len(snake))]
def main(): import os import sys from optparse import OptionParser from global_def import SPARXVERSION import global_def arglist = [] for arg in sys.argv: arglist.append(arg) progname = os.path.basename(arglist[0]) usage = progname + " stack ref_vol outdir <maskfile> parameters listed below" parser = OptionParser(usage, version=SPARXVERSION) parser.add_option("--delta", type="string", default=" 10 6 4 3 2", help="angular step of reference projections") parser.add_option( "--maxit", type="int", default=30, help= "maximum number of iterations performed for each angular step (set to 30) " ) parser.add_option("--CTF", action="store_true", default=False, help="CTF correction") parser.add_option( "--slowIO", action="store_true", default=False, help="sequential reading data for each processor in MPI mode") parser.add_option("--snr", type="float", default=1.0, help="Signal-to-Noise Ratio of the data") #parser.add_option("--MPI", action="store_true", default=False, help="use MPI version") #parser.add_option("--fourvar", action="store_true", default=False, help="compute Fourier variance") parser.add_option("--apix", type="float", default=-1.0, help="pixel size [Angstroms]") parser.add_option("--dp", type="float", default=-1.0, help="rise of helical symmetry [Angstroms]") parser.add_option("--dphi", type="float", default=-1.0, help="azimuthal angle of helical symmetry [degrees]") parser.add_option( "--symdoc", type="string", default="", help="text file containing helical symmetry parameters dp and dphi") parser.add_option( "--psi_max", type="float", default=10.0, help= "maximum psi - how far rotation in plane can can deviate from 90 or 270 degrees" ) parser.add_option("--rmin", type="float", default=0.0, help="minimal radius for hsearch (Angstroms)") parser.add_option("--rmax", type="float", default=80.0, help="maximal radius for hsearch (Angstroms)") parser.add_option( "--fract", type="float", default=0.7, help="fraction of the volume used for helical search. Default 0.7.") parser.add_option( "--sym", type="string", default="c1", help="Point-group symmetry of the structure. Default c1.") parser.add_option( "--function", type="string", default="helical", help="name of the reference preparation function (Default: helical)") parser.add_option("--npad", type="int", default=2, help="padding size for 3D reconstruction (Default: 2)") parser.add_option("--debug", action="store_true", default=False, help="debug") parser.add_option( "--seg_ny", type="int", default=45, help= "Desired y dimension of segments. Only central part of segments nseg_ny pixels long will be used in calculations." ) parser.add_option( "--searchxshift", type="float", default=0.0, help= "search range for x-shift determination: +/- searchxshift (Angstroms)") parser.add_option( "--xwobble", type="float", default=0.0, help="wobble in x-directions (default = 0.0) (Angstroms)") parser.add_option( "--ywobble", type="float", default=0.0, help="wobble in y-directions (default = 0.0) (Angstroms)") parser.add_option( "--ystep", type="float", default=0.0, help="step is in y-directions (default = pixel size) (Angstroms)") parser.add_option( "--phiwobble", type="float", default=0.0, help="wobble of azimuthal angle (default = 0.0) (degrees)") parser.add_option( "--nopsisearch", action="store_true", default=False, help="Block searching for in-plane angle (default False)") (options, args) = parser.parse_args(arglist[1:]) if len(args) < 3 or len(args) > 4: print "usage: " + usage + "\n" print "Please run '" + progname + " -h' for detailed options" else: # Convert input arguments in the units/format as expected by ihrsr_MPI in applications. if options.apix < 0: print "Please enter pixel size" sys.exit() if len(options.symdoc) < 1: if options.dp < 0 or options.dphi < 0: print "Enter helical symmetry parameters either using --symdoc or --dp and --dphi" sys.exit() if options.dp < 0 or options.dphi < 0: # read helical symmetry parameters from symdoc from utilities import read_text_row hparams = read_text_row(options.symdoc) dp = hparams[0][0] dphi = hparams[0][1] else: dp = options.dp dphi = options.dphi rminp = int((float(options.rmin) / options.apix) + 0.5) rmaxp = int((float(options.rmax) / options.apix) + 0.5) from utilities import get_input_from_string, get_im searchxshiftp = int((options.searchxshift / options.apix) + 0.5) xwobblep = int((options.xwobble / options.apix) + 0.5) ywobble = options.ywobble / options.apix if (options.ystep <= 0.0): ystep = 1.0 else: ystep = options.ystep / options.apix if (dp / 2.0 < ywobble): ERROR('ywobble has to be smaller than dp/2.', 'sxhelicon') sys.exit() try: from mpi import mpi_init, mpi_finalize sys.argv = mpi_init(len(sys.argv), sys.argv) except: ERROR( 'This program has only MPI version. Please install MPI library.', 'sxhelicon') sys.exit() if global_def.CACHE_DISABLE: from utilities import disable_bdb_cache disable_bdb_cache() if len(args) < 4: mask = None else: mask = args[3] from applications import ehelix_MPI global_def.BATCH = True ehelix_MPI(args[0], args[1], args[2], options.seg_ny, options.delta, options.phiwobble, options.psi_max,\ searchxshiftp, xwobblep, ywobble, ystep, options.apix, dp, dphi, options.fract, rmaxp, rminp, not options.nopsisearch,\ mask, options.maxit, options.CTF, options.snr, options.sym, options.function, options.npad, options.debug, options.slowIO) global_def.BATCH = False from mpi import mpi_finalize mpi_finalize()
def found_outliers(list_of_projection_indices, outlier_percentile, rviper_iter, masterdir, bdb_stack_location, outlier_index_threshold_method, angle_threshold): # sxheader.py bdb:nj --consecutive --params=OID import numpy as np mainoutputdir = masterdir + DIR_DELIM + NAME_OF_MAIN_DIR + ("%03d" + DIR_DELIM) %(rviper_iter) # if this data analysis step was already performed in the past then return for check_run in list_of_projection_indices: if not (os.path.exists(mainoutputdir + DIR_DELIM + NAME_OF_RUN_DIR + "%03d"%(check_run) + DIR_DELIM + "rotated_reduced_params.txt")): break else: return print "identify_outliers" projs = [] for i1 in list_of_projection_indices: projs.append(read_text_row(mainoutputdir + NAME_OF_RUN_DIR + "%03d"%(i1) + DIR_DELIM + "params.txt")) # ti1, ti3, out = find_common_subset(projs, 1.0) subset, avg_diff_per_image, rotated_params = find_common_subset(projs, target_threshold = 0) # subset, avg_diff_per_image, rotated_params = find_common_subset(projs, target_threshold = 1.0) error_values_and_indices = [] for i in xrange(len(avg_diff_per_image)): error_values_and_indices.append([avg_diff_per_image[i], i]) del subset, avg_diff_per_image error_values_and_indices.sort() if outlier_index_threshold_method == "discontinuity_in_derivative": outlier_index_threshold = find_index_of_discontinuity_in_derivative([i[0] for i in error_values_and_indices], list_of_projection_indices, mainoutputdir, outlier_percentile) elif outlier_index_threshold_method == "percentile": outlier_index_threshold = outlier_percentile * (len(error_values_and_indices) - 1)/ 100.0 elif outlier_index_threshold_method == "angle_measure": error_values = [i[0] for i in error_values_and_indices] outlier_index_threshold = min(range(len(error_values)), key=lambda i: abs(error_values[i]-angle_threshold)) elif outlier_index_threshold_method == "use all images": outlier_index_threshold = len(error_values_and_indices) index_keep_images = [i[1] for i in error_values_and_indices[:outlier_index_threshold]] index_outliers = [i[1] for i in error_values_and_indices[outlier_index_threshold:]] # print "error_values_and_indices: %f"%error_values_and_indices print "index_outliers: ", index_outliers import copy reversed_sorted_index_outliers = copy.deepcopy(index_outliers) reversed_sorted_index_outliers.sort(reverse=True) for k in xrange(len(projs)): for l in reversed_sorted_index_outliers: del rotated_params[k][l] index_outliers.sort() index_keep_images.sort() write_text_file(index_outliers, mainoutputdir + "this_iteration_index_outliers.txt") write_text_file(index_keep_images, mainoutputdir + "this_iteration_index_keep_images.txt") #if len(index_outliers) < 3: #return False if len(index_outliers) > 0: cmd = "{} {} {} {}".format("e2bdb.py ", bdb_stack_location + "_%03d"%(rviper_iter - 1), "--makevstack=" + bdb_stack_location + "_outliers_%03d"%(rviper_iter), "--list=" + mainoutputdir + "this_iteration_index_outliers.txt") cmdexecute(cmd) cmd = "{} {} {} {}".format("e2bdb.py ", bdb_stack_location + "_%03d"%(rviper_iter - 1), "--makevstack=" + bdb_stack_location + "_%03d"%(rviper_iter), "--list=" + mainoutputdir + "this_iteration_index_keep_images.txt") cmdexecute(cmd) dat = EMData.read_images(bdb_stack_location + "_%03d"%(rviper_iter - 1)) write_text_file([dat[i].get_attr("original_image_index") for i in index_outliers],mainoutputdir + "index_outliers.txt") write_text_file([dat[i].get_attr("original_image_index") for i in index_keep_images],mainoutputdir + "index_keep_images.txt") print "index_outliers:: " + str(index_outliers) # write rotated param files for i1 in range(len(list_of_projection_indices)): write_text_row(rotated_params[i1], mainoutputdir + NAME_OF_RUN_DIR + "%03d"%(list_of_projection_indices[i1]) + DIR_DELIM + "rotated_reduced_params.txt") return True
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!")
def main(): import os import sys from optparse import OptionParser from global_def import SPARXVERSION import global_def arglist = [] for arg in sys.argv: arglist.append( arg ) progname = os.path.basename(arglist[0]) usage = progname + " stack ref_vol outdir <maskfile> parameters listed below" parser = OptionParser(usage,version=SPARXVERSION) parser.add_option("--delta", type="string", default= " 10 6 4 3 2", help="angular step of reference projections") parser.add_option("--maxit", type="int", default= 30, help="maximum number of iterations performed for each angular step (set to 30) ") parser.add_option("--CTF", action="store_true", default=False, help="CTF correction") parser.add_option("--snr", type="float", default= 1.0, help="Signal-to-Noise Ratio of the data") #parser.add_option("--MPI", action="store_true", default=False, help="use MPI version") #parser.add_option("--fourvar", action="store_true", default=False, help="compute Fourier variance") parser.add_option("--apix", type="float", default= -1.0, help="pixel size [Angstroms]") parser.add_option("--dp", type="float", default= -1.0, help="rise of helical symmetry [Angstroms]") parser.add_option("--dphi", type="float", default= -1.0, help="azimuthal angle of helical symmetry [degrees]") parser.add_option("--symdoc", type="string", default="", help="text file containing helical symmetry parameters dp and dphi") parser.add_option("--psi_max", type="float", default= 10.0, help="maximum psi - how far rotation in plane can can deviate from 90 or 270 degrees") parser.add_option("--rmin", type="float", default= 0.0, help="minimal radius for hsearch (Angstroms)") parser.add_option("--rmax", type="float", default= 80.0, help="maximal radius for hsearch (Angstroms)") parser.add_option("--fract", type="float", default= 0.7, help="fraction of the volume used for helical search. Default 0.7.") parser.add_option("--sym", type="string", default= "c1", help="Point-group symmetry of the structure. Default c1.") parser.add_option("--function", type="string", default="helical", help="name of the reference preparation function (Default: helical)") parser.add_option("--npad", type="int", default= 2, help="padding size for 3D reconstruction (Default: 2)") parser.add_option("--debug", action="store_true", default=False, help="debug") parser.add_option("--seg_ny", type="int", default= 45, help="Desired y dimension of segments. Only central part of segments nseg_ny pixels long will be used in calculations.") parser.add_option("--searchxshift", type="float", default= 0.0, help="search range for x-shift determination: +/- searchxshift (Angstroms)") parser.add_option("--xwobble", type="float", default=0.0, help="wobble in x-directions (default = 0.0) (Angstroms)") parser.add_option("--ywobble", type="float", default=0.0, help="wobble in y-directions (default = 0.0) (Angstroms)") parser.add_option("--ystep", type="float", default=0.0, help="step is in y-directions (default = pixel size) (Angstroms)") parser.add_option("--phiwobble", type="float", default=0.0, help="wobble of azimuthal angle (default = 0.0) (degrees)") parser.add_option("--nopsisearch", action="store_true", default=False, help="Block searching for in-plane angle (default False)") (options, args) = parser.parse_args(arglist[1:]) if len(args) < 3 or len(args) > 4: print "usage: " + usage + "\n" print "Please run '" + progname + " -h' for detailed options" else: # Convert input arguments in the units/format as expected by ihrsr_MPI in applications. if options.apix < 0: print "Please enter pixel size" sys.exit() if len(options.symdoc) < 1: if options.dp < 0 or options.dphi < 0: print "Enter helical symmetry parameters either using --symdoc or --dp and --dphi" sys.exit() if options.dp < 0 or options.dphi < 0: # read helical symmetry parameters from symdoc from utilities import read_text_row hparams=read_text_row(options.symdoc) dp = hparams[0][0] dphi = hparams[0][1] else: dp = options.dp dphi = options.dphi rminp = int((float(options.rmin)/options.apix) + 0.5) rmaxp = int((float(options.rmax)/options.apix) + 0.5) from utilities import get_input_from_string, get_im searchxshiftp = int( (options.searchxshift/options.apix) + 0.5) xwobblep = int( (options.xwobble/options.apix) + 0.5) ywobble = options.ywobble/options.apix if( options.ystep <= 0.0 ): ystep = 1.0 else: ystep = options.ystep/options.apix if( dp/2.0 < ywobble): ERROR('ywobble has to be smaller than dp/2.', 'sxhelicon') sys.exit() try: from mpi import mpi_init, mpi_finalize sys.argv = mpi_init(len(sys.argv), sys.argv) except: ERROR('This program has only MPI version. Please install MPI library.', 'sxhelicon') sys.exit() if global_def.CACHE_DISABLE: from utilities import disable_bdb_cache disable_bdb_cache() if len(args) < 4: mask = None else: mask = args[3] from applications import ehelix_MPI global_def.BATCH = True ehelix_MPI(args[0], args[1], args[2], options.seg_ny, options.delta, options.phiwobble, options.psi_max,\ searchxshiftp, xwobblep, ywobble, ystep, options.apix, dp, dphi, options.fract, rmaxp, rminp, not options.nopsisearch,\ mask, options.maxit, options.CTF, options.snr, options.sym, options.function, options.npad, options.debug) global_def.BATCH = False from mpi import mpi_finalize mpi_finalize()
def main(): import sys import os import math import random import pyemtbx.options import time from random import random, seed, randint from optparse import OptionParser progname = os.path.basename(sys.argv[0]) usage = progname + """ [options] <inputfile> <outputfile> Generic 2-D image processing programs. Functionality: 1. Phase flip a stack of images and write output to new file: sxprocess.py input_stack.hdf output_stack.hdf --phase_flip 2. Resample (decimate or interpolate up) images (2D or 3D) in a stack to change the pixel size. The window size will change accordingly. sxprocess input.hdf output.hdf --changesize --ratio=0.5 3. Compute average power spectrum of a stack of 2D images with optional padding (option wn) with zeroes or a 3-D volume. sxprocess.py input_stack.hdf powerspectrum.hdf --pw [--wn=1024] 4. Generate a stack of projections bdb:data and micrographs with prefix mic (i.e., mic0.hdf, mic1.hdf etc) from structure input_structure.hdf, with CTF applied to both projections and micrographs: sxprocess.py input_structure.hdf data mic --generate_projections format="bdb":apix=5.2:CTF=True:boxsize=64 5. Retrieve original image numbers in the selected ISAC group (here group 12 from generation 3): sxprocess.py bdb:test3 class_averages_generation_3.hdf list3_12.txt --isacgroup=12 --params=originalid 6. Retrieve original image numbers of images listed in ISAC output stack of averages: sxprocess.py select1.hdf ohk.txt 7. Adjust rotationally averaged power spectrum of an image to that of a reference image or a reference 1D power spectrum stored in an ASCII file. Optionally use a tangent low-pass filter. Also works for a stack of images, in which case the output is also a stack. sxprocess.py vol.hdf ref.hdf avol.hdf < 0.25 0.2> --adjpw sxprocess.py vol.hdf pw.txt avol.hdf < 0.25 0.2> --adjpw 8. Generate a 1D rotationally averaged power spectrum of an image. sxprocess.py vol.hdf --rotwp=rotpw.txt # Output will contain three columns: (1) rotationally averaged power spectrum (2) logarithm of the rotationally averaged power spectrum (3) integer line number (from zero to approximately to half the image size) 9. Apply 3D transformation (rotation and/or shift) to a set of orientation parameters associated with projection data. sxprocess.py --transfromparams=phi,theta,psi,tx,ty,tz input.txt output.txt The output file is then imported and 3D transformed volume computed: sxheader.py bdb:p --params=xform.projection --import=output.txt mpirun -np 2 sxrecons3d_n.py bdb:p tvol.hdf --MPI The reconstructed volume is in the position of the volume computed using the input.txt parameters and then transformed with rot_shift3D(vol, phi,theta,psi,tx,ty,tz) 10. Import ctf parameters from the output of sxcter into windowed particle headers. There are three possible input files formats: (1) all particles are in one stack, (2 aor 3) particles are in stacks, each stack corresponds to a single micrograph. In each case the particles should contain a name of the micrograph of origin stores using attribute name 'ptcl_source_image'. Normally this is done by e2boxer.py during windowing. Particles whose defocus or astigmatism error exceed set thresholds will be skipped, otherwise, virtual stacks with the original way preceded by G will be created. sxprocess.py --input=bdb:data --importctf=outdir/partres --defocuserror=10.0 --astigmatismerror=5.0 # Output will be a vritual stack bdb:Gdata sxprocess.py --input="bdb:directory/stacks*" --importctf=outdir/partres --defocuserror=10.0 --astigmatismerror=5.0 To concatenate output files: cd directory e2bdb.py . --makevstack=bdb:allparticles --filt=G IMPORTANT: Please do not move (or remove!) any input/intermediate EMAN2DB files as the information is linked between them. 11. Scale 3D shifts. The shifts in the input five columns text file with 3D orientation parameters will be DIVIDED by the scale factor sxprocess.py orientationparams.txt scaledparams.txt scale=0.5 12. Generate 3D mask from a given 3-D volume automatically or using threshold provided by user. 13. Postprocess 3-D or 2-D images: for 3-D volumes: calculate FSC with provided mask; weight summed volume with FSC; estimate B-factor from FSC weighted summed two volumes; apply negative B-factor to the weighted volume. for 2-D images: calculate B-factor and apply negative B-factor to 2-D images. 14. Winow stack file -reduce size of images without changing the pixel size. """ parser = OptionParser(usage,version=SPARXVERSION) parser.add_option("--order", action="store_true", help="Two arguments are required: name of input stack and desired name of output stack. The output stack is the input stack sorted by similarity in terms of cross-correlation coefficent.", default=False) parser.add_option("--order_lookup", action="store_true", help="Test/Debug.", default=False) parser.add_option("--order_metropolis", action="store_true", help="Test/Debug.", default=False) parser.add_option("--order_pca", action="store_true", help="Test/Debug.", default=False) parser.add_option("--initial", type="int", default=-1, help="Specifies which image will be used as an initial seed to form the chain. (default = 0, means the first image)") parser.add_option("--circular", action="store_true", help="Select circular ordering (fisr image has to be similar to the last", default=False) parser.add_option("--radius", type="int", default=-1, help="Radius of a circular mask for similarity based ordering") parser.add_option("--changesize", action="store_true", help="resample (decimate or interpolate up) images (2D or 3D) in a stack to change the pixel size.", default=False) parser.add_option("--ratio", type="float", default=1.0, help="The ratio of new to old image size (if <1 the pixel size will increase and image size decrease, if>1, the other way round") parser.add_option("--pw", action="store_true", help="compute average power spectrum of a stack of 2-D images with optional padding (option wn) with zeroes", default=False) parser.add_option("--wn", type="int", default=-1, help="Size of window to use (should be larger/equal than particle box size, default padding to max(nx,ny))") parser.add_option("--phase_flip", action="store_true", help="Phase flip the input stack", default=False) parser.add_option("--makedb", metavar="param1=value1:param2=value2", type="string", action="append", help="One argument is required: name of key with which the database will be created. Fill in database with parameters specified as follows: --makedb param1=value1:param2=value2, e.g. 'gauss_width'=1.0:'pixel_input'=5.2:'pixel_output'=5.2:'thr_low'=1.0") parser.add_option("--generate_projections", metavar="param1=value1:param2=value2", type="string", action="append", help="Three arguments are required: name of input structure from which to generate projections, desired name of output projection stack, and desired prefix for micrographs (e.g. if prefix is 'mic', then micrographs mic0.hdf, mic1.hdf etc will be generated). Optional arguments specifying format, apix, box size and whether to add CTF effects can be entered as follows after --generate_projections: format='bdb':apix=5.2:CTF=True:boxsize=100, or format='hdf', etc., where format is bdb or hdf, apix (pixel size) is a float, CTF is True or False, and boxsize denotes the dimension of the box (assumed to be a square). If an optional parameter is not specified, it will default as follows: format='bdb', apix=2.5, CTF=False, boxsize=64.") parser.add_option("--isacgroup", type="int", help="Retrieve original image numbers in the selected ISAC group. See ISAC documentation for details.", default=-1) parser.add_option("--isacselect", action="store_true", help="Retrieve original image numbers of images listed in ISAC output stack of averages. See ISAC documentation for details.", default=False) parser.add_option("--params", type="string", default=None, help="Name of header of parameter, which one depends on specific option") parser.add_option("--adjpw", action="store_true", help="Adjust rotationally averaged power spectrum of an image", default=False) parser.add_option("--rotpw", type="string", default=None, help="Name of the text file to contain rotationally averaged power spectrum of the input image.") parser.add_option("--transformparams", type="string", default=None, help="Transform 3D projection orientation parameters using six 3D parameters (phi, theta,psi,sx,sy,sz). Input: --transformparams=45.,66.,12.,-2,3,-5.5 desired six transformation of the reconstructed structure. Output: file with modified orientation parameters.") # import ctf estimates done using cter parser.add_option("--input", type="string", default= None, help="Input particles.") parser.add_option("--importctf", type="string", default= None, help="Name of the file containing CTF parameters produced by sxcter.") parser.add_option("--defocuserror", type="float", default=1000000.0, help="Exclude micrographs whose relative defocus error as estimated by sxcter is larger than defocuserror percent. The error is computed as (std dev defocus)/defocus*100%") parser.add_option("--astigmatismerror", type="float", default=360.0, help="Set to zero astigmatism for micrographs whose astigmatism angular error as estimated by sxcter is larger than astigmatismerror degrees.") # import ctf estimates done using cter parser.add_option("--scale", type="float", default=-1.0, help="Divide shifts in the input 3D orientation parameters text file by the scale factor.") # generate adaptive mask from an given 3-D volume parser.add_option("--adaptive_mask", action="store_true", help="create adavptive 3-D mask from a given volume", default=False) parser.add_option("--nsigma", type="float", default= 1., help="number of times of sigma of the input volume to obtain the the large density cluster") parser.add_option("--ndilation", type="int", default= 3, help="number of times of dilation applied to the largest cluster of density") parser.add_option("--kernel_size", type="int", default= 11, help="convolution kernel for smoothing the edge of the mask") parser.add_option("--gauss_standard_dev", type="int", default= 9, help="stanadard deviation value to generate Gaussian edge") parser.add_option("--threshold", type="float", default= 9999., help="threshold provided by user to binarize input volume") parser.add_option("--ne", type="int", default= 0, help="number of times to erode the binarized input image") parser.add_option("--nd", type="int", default= 0, help="number of times to dilate the binarized input image") parser.add_option("--postprocess", action="store_true", help="postprocess unfiltered odd, even 3-D volumes",default=False) parser.add_option("--fsc_weighted", action="store_true", help="postprocess unfiltered odd, even 3-D volumes") parser.add_option("--low_pass_filter", action="store_true", default=False, help="postprocess unfiltered odd, even 3-D volumes") parser.add_option("--ff", type="float", default=.25, help="low pass filter stop band frequency in absolute unit") parser.add_option("--aa", type="float", default=.1, help="low pass filter falloff" ) parser.add_option("--mask", type="string", help="input mask file", default=None) parser.add_option("--output", type="string", help="output file name", default="postprocessed.hdf") parser.add_option("--pixel_size", type="float", help="pixel size of the data", default=1.0) parser.add_option("--B_start", type="float", help="starting frequency in Angstrom for B-factor estimation", default=10.) parser.add_option("--FSC_cutoff", type="float", help="stop frequency in Angstrom for B-factor estimation", default=0.143) parser.add_option("--2d", action="store_true", help="postprocess isac 2-D averaged images",default=False) parser.add_option("--window_stack", action="store_true", help="window stack images using a smaller window size", default=False) parser.add_option("--box", type="int", default= 0, help="the new window size ") (options, args) = parser.parse_args() global_def.BATCH = True if options.phase_flip: nargs = len(args) if nargs != 2: print "must provide name of input and output file!" return from EMAN2 import Processor instack = args[0] outstack = args[1] nima = EMUtil.get_image_count(instack) from filter import filt_ctf for i in xrange(nima): img = EMData() img.read_image(instack, i) try: ctf = img.get_attr('ctf') except: print "no ctf information in input stack! Exiting..." return dopad = True sign = 1 binary = 1 # phase flip assert img.get_ysize() > 1 dict = ctf.to_dict() dz = dict["defocus"] cs = dict["cs"] voltage = dict["voltage"] pixel_size = dict["apix"] b_factor = dict["bfactor"] ampcont = dict["ampcont"] dza = dict["dfdiff"] azz = dict["dfang"] if dopad and not img.is_complex(): ip = 1 else: ip = 0 params = {"filter_type": Processor.fourier_filter_types.CTF_, "defocus" : dz, "Cs": cs, "voltage": voltage, "Pixel_size": pixel_size, "B_factor": b_factor, "amp_contrast": ampcont, "dopad": ip, "binary": binary, "sign": sign, "dza": dza, "azz":azz} tmp = Processor.EMFourierFilter(img, params) tmp.set_attr_dict({"ctf": ctf}) tmp.write_image(outstack, i) elif options.changesize: nargs = len(args) if nargs != 2: ERROR("must provide name of input and output file!", "change size", 1) return from utilities import get_im instack = args[0] outstack = args[1] sub_rate = float(options.ratio) nima = EMUtil.get_image_count(instack) from fundamentals import resample for i in xrange(nima): resample(get_im(instack, i), sub_rate).write_image(outstack, i) elif options.isacgroup>-1: nargs = len(args) if nargs != 3: ERROR("Three files needed on input!", "isacgroup", 1) return from utilities import get_im instack = args[0] m=get_im(args[1],int(options.isacgroup)).get_attr("members") l = [] for k in m: l.append(int(get_im(args[0],k).get_attr(options.params))) from utilities import write_text_file write_text_file(l, args[2]) elif options.isacselect: nargs = len(args) if nargs != 2: ERROR("Two files needed on input!", "isacgroup", 1) return from utilities import get_im nima = EMUtil.get_image_count(args[0]) m = [] for k in xrange(nima): m += get_im(args[0],k).get_attr("members") m.sort() from utilities import write_text_file write_text_file(m, args[1]) elif options.pw: nargs = len(args) if nargs < 2: ERROR("must provide name of input and output file!", "pw", 1) return from utilities import get_im, write_text_file from fundamentals import rops_table d = get_im(args[0]) ndim = d.get_ndim() if ndim ==3: pw = rops_table(d) write_text_file(pw, args[1]) else: nx = d.get_xsize() ny = d.get_ysize() if nargs ==3: mask = get_im(args[2]) wn = int(options.wn) if wn == -1: wn = max(nx, ny) else: if( (wn<nx) or (wn<ny) ): ERROR("window size cannot be smaller than the image size","pw",1) n = EMUtil.get_image_count(args[0]) from utilities import model_blank, model_circle, pad from EMAN2 import periodogram p = model_blank(wn,wn) for i in xrange(n): d = get_im(args[0], i) if nargs==3: d *=mask st = Util.infomask(d, None, True) d -= st[0] p += periodogram(pad(d, wn, wn, 1, 0.)) p /= n p.write_image(args[1]) elif options.adjpw: if len(args) < 3: ERROR("filt_by_rops input target output fl aa (the last two are optional parameters of a low-pass filter)","adjpw",1) return img_stack = args[0] from math import sqrt from fundamentals import rops_table, fft from utilities import read_text_file, get_im from filter import filt_tanl, filt_table if( args[1][-3:] == 'txt'): rops_dst = read_text_file( args[1] ) else: rops_dst = rops_table(get_im( args[1] )) out_stack = args[2] if(len(args) >4): fl = float(args[3]) aa = float(args[4]) else: fl = -1.0 aa = 0.0 nimage = EMUtil.get_image_count( img_stack ) for i in xrange(nimage): img = fft(get_im(img_stack, i) ) rops_src = rops_table(img) assert len(rops_dst) == len(rops_src) table = [0.0]*len(rops_dst) for j in xrange( len(rops_dst) ): table[j] = sqrt( rops_dst[j]/rops_src[j] ) if( fl > 0.0): img = filt_tanl(img, fl, aa) img = fft(filt_table(img, table)) img.write_image(out_stack, i) elif options.rotpw != None: if len(args) != 1: ERROR("Only one input permitted","rotpw",1) return from utilities import write_text_file, get_im from fundamentals import rops_table from math import log10 t = rops_table(get_im(args[0])) x = range(len(t)) r = [0.0]*len(x) for i in x: r[i] = log10(t[i]) write_text_file([t,r,x],options.rotpw) elif options.transformparams != None: if len(args) != 2: ERROR("Please provide names of input and output files with orientation parameters","transformparams",1) return from utilities import read_text_row, write_text_row transf = [0.0]*6 spl=options.transformparams.split(',') for i in xrange(len(spl)): transf[i] = float(spl[i]) write_text_row( rotate_shift_params(read_text_row(args[0]), transf) , args[1]) elif options.makedb != None: nargs = len(args) if nargs != 1: print "must provide exactly one argument denoting database key under which the input params will be stored" return dbkey = args[0] print "database key under which params will be stored: ", dbkey gbdb = js_open_dict("e2boxercache/gauss_box_DB.json") parmstr = 'dummy:'+options.makedb[0] (processorname, param_dict) = parsemodopt(parmstr) dbdict = {} for pkey in param_dict: if (pkey == 'invert_contrast') or (pkey == 'use_variance'): if param_dict[pkey] == 'True': dbdict[pkey] = True else: dbdict[pkey] = False else: dbdict[pkey] = param_dict[pkey] gbdb[dbkey] = dbdict elif options.generate_projections: nargs = len(args) if nargs != 3: ERROR("Must provide name of input structure(s) from which to generate projections, name of output projection stack, and prefix for output micrographs."\ "sxprocess - generate projections",1) return inpstr = args[0] outstk = args[1] micpref = args[2] parmstr = 'dummy:'+options.generate_projections[0] (processorname, param_dict) = parsemodopt(parmstr) parm_CTF = False parm_format = 'bdb' parm_apix = 2.5 if 'CTF' in param_dict: if param_dict['CTF'] == 'True': parm_CTF = True if 'format' in param_dict: parm_format = param_dict['format'] if 'apix' in param_dict: parm_apix = float(param_dict['apix']) boxsize = 64 if 'boxsize' in param_dict: boxsize = int(param_dict['boxsize']) print "pixel size: ", parm_apix, " format: ", parm_format, " add CTF: ", parm_CTF, " box size: ", boxsize scale_mult = 2500 sigma_add = 1.5 sigma_proj = 30.0 sigma2_proj = 17.5 sigma_gauss = 0.3 sigma_mic = 30.0 sigma2_mic = 17.5 sigma_gauss_mic = 0.3 if 'scale_mult' in param_dict: scale_mult = float(param_dict['scale_mult']) if 'sigma_add' in param_dict: sigma_add = float(param_dict['sigma_add']) if 'sigma_proj' in param_dict: sigma_proj = float(param_dict['sigma_proj']) if 'sigma2_proj' in param_dict: sigma2_proj = float(param_dict['sigma2_proj']) if 'sigma_gauss' in param_dict: sigma_gauss = float(param_dict['sigma_gauss']) if 'sigma_mic' in param_dict: sigma_mic = float(param_dict['sigma_mic']) if 'sigma2_mic' in param_dict: sigma2_mic = float(param_dict['sigma2_mic']) if 'sigma_gauss_mic' in param_dict: sigma_gauss_mic = float(param_dict['sigma_gauss_mic']) from filter import filt_gaussl, filt_ctf from utilities import drop_spider_doc, even_angles, model_gauss, delete_bdb, model_blank,pad,model_gauss_noise,set_params2D, set_params_proj from projection import prep_vol,prgs seed(14567) delta = 29 angles = even_angles(delta, 0.0, 89.9, 0.0, 359.9, "S") nangle = len(angles) modelvol = [] nvlms = EMUtil.get_image_count(inpstr) from utilities import get_im for k in xrange(nvlms): modelvol.append(get_im(inpstr,k)) nx = modelvol[0].get_xsize() if nx != boxsize: ERROR("Requested box dimension does not match dimension of the input model.", \ "sxprocess - generate projections",1) nvol = 10 volfts = [[] for k in xrange(nvlms)] for k in xrange(nvlms): for i in xrange(nvol): sigma = sigma_add + random() # 1.5-2.5 addon = model_gauss(sigma, boxsize, boxsize, boxsize, sigma, sigma, 38, 38, 40 ) scale = scale_mult * (0.5+random()) vf, kb = prep_vol(modelvol[k] + scale*addon) volfts[k].append(vf) del vf, modelvol if parm_format == "bdb": stack_data = "bdb:"+outstk delete_bdb(stack_data) else: stack_data = outstk + ".hdf" Cs = 2.0 pixel = parm_apix voltage = 120.0 ampcont = 10.0 ibd = 4096/2-boxsize iprj = 0 width = 240 xstart = 8 + boxsize/2 ystart = 8 + boxsize/2 rowlen = 17 from random import randint params = [] for idef in xrange(3, 8): irow = 0 icol = 0 mic = model_blank(4096, 4096) defocus = idef * 0.5#0.2 if parm_CTF: astampl=defocus*0.15 astangl=50.0 ctf = generate_ctf([defocus, Cs, voltage, pixel, ampcont, 0.0, astampl, astangl]) for i in xrange(nangle): for k in xrange(12): dphi = 8.0*(random()-0.5) dtht = 8.0*(random()-0.5) psi = 360.0*random() phi = angles[i][0]+dphi tht = angles[i][1]+dtht s2x = 4.0*(random()-0.5) s2y = 4.0*(random()-0.5) params.append([phi, tht, psi, s2x, s2y]) ivol = iprj % nvol #imgsrc = randint(0,nvlms-1) imgsrc = iprj % nvlms proj = prgs(volfts[imgsrc][ivol], kb, [phi, tht, psi, -s2x, -s2y]) x = xstart + irow * width y = ystart + icol * width mic += pad(proj, 4096, 4096, 1, 0.0, x-2048, y-2048, 0) proj = proj + model_gauss_noise( sigma_proj, nx, nx ) if parm_CTF: proj = filt_ctf(proj, ctf) proj.set_attr_dict({"ctf":ctf, "ctf_applied":0}) proj = proj + filt_gaussl(model_gauss_noise(sigma2_proj, nx, nx), sigma_gauss) proj.set_attr("origimgsrc",imgsrc) proj.set_attr("test_id", iprj) # flags describing the status of the image (1 = true, 0 = false) set_params2D(proj, [0.0, 0.0, 0.0, 0, 1.0]) set_params_proj(proj, [phi, tht, psi, s2x, s2y]) proj.write_image(stack_data, iprj) icol += 1 if icol == rowlen: icol = 0 irow += 1 iprj += 1 mic += model_gauss_noise(sigma_mic,4096,4096) if parm_CTF: #apply CTF mic = filt_ctf(mic, ctf) mic += filt_gaussl(model_gauss_noise(sigma2_mic, 4096, 4096), sigma_gauss_mic) mic.write_image(micpref + "%1d.hdf" % (idef-3), 0) drop_spider_doc("params.txt", params) elif options.importctf != None: print ' IMPORTCTF ' from utilities import read_text_row,write_text_row from random import randint import subprocess grpfile = 'groupid%04d'%randint(1000,9999) ctfpfile = 'ctfpfile%04d'%randint(1000,9999) cterr = [options.defocuserror/100.0, options.astigmatismerror] ctfs = read_text_row(options.importctf) for kk in xrange(len(ctfs)): root,name = os.path.split(ctfs[kk][-1]) ctfs[kk][-1] = name[:-4] if(options.input[:4] != 'bdb:'): ERROR('Sorry, only bdb files implemented','importctf',1) d = options.input[4:] #try: str = d.index('*') #except: str = -1 from string import split import glob uu = os.path.split(d) uu = os.path.join(uu[0],'EMAN2DB',uu[1]+'.bdb') flist = glob.glob(uu) for i in xrange(len(flist)): root,name = os.path.split(flist[i]) root = root[:-7] name = name[:-4] fil = 'bdb:'+os.path.join(root,name) sourcemic = EMUtil.get_all_attributes(fil,'ptcl_source_image') nn = len(sourcemic) gctfp = [] groupid = [] for kk in xrange(nn): junk,name2 = os.path.split(sourcemic[kk]) name2 = name2[:-4] ctfp = [-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0] for ll in xrange(len(ctfs)): if(name2 == ctfs[ll][-1]): # found correct if(ctfs[ll][8]/ctfs[ll][0] <= cterr[0]): # acceptable defocus error ctfp = ctfs[ll][:8] if(ctfs[ll][10] > cterr[1] ): # error of astigmatism exceed the threshold, set astigmatism to zero. ctfp[6] = 0.0 ctfp[7] = 0.0 gctfp.append(ctfp) groupid.append(kk) break if(len(groupid) > 0): write_text_row(groupid, grpfile) write_text_row(gctfp, ctfpfile) cmd = "{} {} {} {}".format('e2bdb.py',fil,'--makevstack=bdb:'+root+'G'+name,'--list='+grpfile) #print cmd subprocess.call(cmd, shell=True) cmd = "{} {} {} {}".format('sxheader.py','bdb:'+root+'G'+name,'--params=ctf','--import='+ctfpfile) #print cmd subprocess.call(cmd, shell=True) else: print ' >>> Group ',name,' skipped.' cmd = "{} {} {}".format("rm -f",grpfile,ctfpfile) subprocess.call(cmd, shell=True) elif options.scale > 0.0: from utilities import read_text_row,write_text_row scale = options.scale nargs = len(args) if nargs != 2: print "Please provide names of input and output file!" return p = read_text_row(args[0]) for i in xrange(len(p)): p[i][3] /= scale p[i][4] /= scale write_text_row(p, args[1]) elif options.adaptive_mask: from utilities import get_im from morphology import adaptive_mask, binarize, erosion, dilation nsigma = options.nsigma ndilation = options.ndilation kernel_size = options.kernel_size gauss_standard_dev = options.gauss_standard_dev nargs = len(args) if nargs ==0: print " Create 3D mask from a given volume, either automatically or from the user provided threshold." elif nargs > 2: print "Too many inputs are given, try again!" return else: inputvol = get_im(args[0]) input_path, input_file_name = os.path.split(args[0]) input_file_name_root,ext=os.path.splitext(input_file_name) if nargs == 2: mask_file_name = args[1] else: mask_file_name = "adaptive_mask_for_"+input_file_name_root+".hdf" # Only hdf file is output. if options.threshold !=9999.: mask3d = binarize(inputvol, options.threshold) for i in xrange(options.ne): mask3d = erosion(mask3d) for i in xrange(options.nd): mask3d = dilation(mask3d) else: mask3d = adaptive_mask(inputvol, nsigma, ndilation, kernel_size, gauss_standard_dev) mask3d.write_image(mask_file_name) elif options.postprocess: from utilities import get_im from fundamentals import rot_avg_table from morphology import compute_bfactor,power from statistics import fsc from filter import filt_table, filt_gaussinv from EMAN2 import periodogram e1 = get_im(args[0],0) if e1.get_zsize()==1: nimage = EMUtil.get_image_count(args[0]) if options.mask !=None: m = get_im(options.mask) else: m = None for i in xrange(nimage): e1 = get_im(args[0],i) if m: e1 *=m guinerline = rot_avg_table(power(periodogram(e1),.5)) freq_max = 1/(2.*pixel_size) freq_min = 1./options.B_start b,junk=compute_bfactor(guinerline, freq_min, freq_max, pixel_size) tmp = b/pixel_size**2 sigma_of_inverse=sqrt(2./tmp) e1 = filt_gaussinv(e1,sigma_of_inverse) if options.low_pass_filter: from filter import filt_tanl e1 =filt_tanl(e1,options.ff, options.aa) e1.write_image(options.output) else: nargs = len(args) e1 = get_im(args[0]) if nargs >1: e2 = get_im(args[1]) if options.mask !=None: m = get_im(options.mask) else: m =None pixel_size = options.pixel_size from math import sqrt if m !=None: e1 *=m if nargs >1 :e2 *=m if options.fsc_weighted: frc = fsc(e1,e2,1) ## FSC is done on masked two images #### FSC weighting sqrt((2.*fsc)/(1+fsc)); fil = len(frc[1])*[None] for i in xrange(len(fil)): if frc[1][i]>=options.FSC_cutoff: tmp = frc[1][i] else: tmp = 0.0 fil[i] = sqrt(2.*tmp/(1.+tmp)) if nargs>1: e1 +=e2 if options.fsc_weighted: e1=filt_table(e1,fil) guinerline = rot_avg_table(power(periodogram(e1),.5)) freq_max = 1/(2.*pixel_size) freq_min = 1./options.B_start b,junk = compute_bfactor(guinerline, freq_min, freq_max, pixel_size) tmp = b/pixel_size**2 sigma_of_inverse=sqrt(2./tmp) e1 = filt_gaussinv(e1,sigma_of_inverse) if options.low_pass_filter: from filter import filt_tanl e1 =filt_tanl(e1,options.ff, options.aa) e1.write_image(options.output) elif options.window_stack: nargs = len(args) if nargs ==0: print " Reduce image size of a stack" return else: output_stack_name = None inputstack = args[0] if nargs ==2:output_stack_name = args[1] input_path,input_file_name=os.path.split(inputstack) input_file_name_root,ext=os.path.splitext(input_file_name) if input_file_name_root[0:3]=="bdb":stack_is_bdb= True else: stack_is_bdb= False if output_stack_name is None: if stack_is_bdb: output_stack_name ="bdb:reduced_"+input_file_name_root[4:] else:output_stack_name = "reduced_"+input_file_name_root+".hdf" # Only hdf file is output. nimage = EMUtil.get_image_count(inputstack) from fundamentals import window2d for i in xrange(nimage): image = EMData() image.read_image(inputstack,i) w = window2d(image,options.box,options.box) w.write_image(output_stack_name,i) else: ERROR("Please provide option name","sxprocess.py",1)
def main(): import os import sys from optparse import OptionParser from global_def import SPARXVERSION import global_def arglist = [] for arg in sys.argv: arglist.append( arg ) progname = os.path.basename(arglist[0]) usage2 = progname + """ inputfile outputfile [options] Functionalities: 1. Helicise input volume and save the result to output volume: sxhelicon_utils.py input_vol.hdf output_vol.hdf --helicise --dp=27.6 --dphi=166.5 --fract=0.65 --rmax=70 --rmin=1 --apix=1.84 --sym=D1 2. Helicise pdb file and save the result to a new pdb file: sxhelicon_utils.py input.pdb output.pdb --helicisepdb --dp=27.6 --dphi=166.5 --nrepeats --apix=1.84 3. Generate two lists of image indices used to split segment stack into halves for helical fsc calculation. sxhelicon_utils.py bdb:big_stack --hfsc='flst' --filament_attr=filament 4. Map of filament distribution in the stack sxhelicon_utils.py bdb:big_stack --filinfo=info.txt The output file will contain four columns: 1 2 3 4 first image number last image number number of images in the filament name 5. Predict segments' orientation parameters based on distances between segments and known helical symmetry sxhelicon_utils.py bdb:big_stack --predict_helical=helical_params.txt --dp=27.6 --dphi=166.5 --apix=1.84 6. Generate disks from filament based reconstructions: sxheader.py stk.hdf --params=xform.projection --import=params.txt # horatio active_refactoring Jy51i1EwmLD4tWZ9_00000_1 # sxheader.py stk.hdf --params=active --one mpirun -np 2 sxhelicon_utils.py stk.hdf --gendisk='bdb:disk' --ref_nx=100 --ref_ny=100 --ref_nz=200 --apix=1.84 --dp=27.6 --dphi=166.715 --fract=0.67 --rmin=0 --rmax=64 --function="[.,nofunc,helical3c]" --sym="c1" --MPI 7. Stack disks based on helical symmetry parameters sxhelicon_utils.py disk_to_stack.hdf --stackdisk=stacked_disks.hdf --dphi=166.5 --dp=27.6 --ref_nx=160 --ref_ny=160 --ref_nz=225 --apix=1.84 8. Helical symmetry search: mpirun -np 3 sxhelicon_utils.py volf0010.hdf outsymsearch --symsearch --dp=27.6 --dphi=166.715 --apix=1.84 --fract=0.65 --rmin=0 --rmax=92.0 --datasym=datasym.txt --dp_step=0.92 --ndp=3 --dphi_step=1.0 --ndphi=10 --MPI """ parser = OptionParser(usage2,version=SPARXVERSION) #parser.add_option("--ir", type="float", default= -1, help="inner radius for rotational correlation > 0 (set to 1) (Angstroms)") parser.add_option("--ou", type="float", default= -1, help="outer radius for rotational 2D correlation < int(nx/2)-1 (set to the radius of the particle) (Angstroms)") parser.add_option("--rs", type="int", default= 1, help="step between rings in rotational correlation >0 (set to 1)" ) parser.add_option("--xr", type="string", default= "4 2 1 1 1", help="range for translation search in x direction, search is +/-xr (Angstroms) ") parser.add_option("--txs", type="string", default= "1 1 1 0.5 0.25", help="step size of the translation search in x directions, search is -xr, -xr+ts, 0, xr-ts, xr (Angstroms)") parser.add_option("--delta", type="string", default= "10 6 4 3 2", help="angular step of reference projections") parser.add_option("--an", type="string", default= "-1", help="angular neighborhood for local searches") parser.add_option("--maxit", type="int", default= 30, help="maximum number of iterations performed for each angular step (set to 30) ") parser.add_option("--CTF", action="store_true", default=False, help="CTF correction") parser.add_option("--snr", type="float", default= 1.0, help="Signal-to-Noise Ratio of the data") parser.add_option("--MPI", action="store_true", default=False, help="use MPI version") #parser.add_option("--fourvar", action="store_true", default=False, help="compute Fourier variance") parser.add_option("--apix", type="float", default= -1.0, help="pixel size in Angstroms") parser.add_option("--dp", type="float", default= -1.0, help="delta z - translation in Angstroms") parser.add_option("--dphi", type="float", default= -1.0, help="delta phi - rotation in degrees") parser.add_option("--rmin", type="float", default= 0.0, help="minimal radius for hsearch (Angstroms)") parser.add_option("--rmax", type="float", default= 80.0, help="maximal radius for hsearch (Angstroms)") parser.add_option("--fract", type="float", default= 0.7, help="fraction of the volume used for helical search") parser.add_option("--sym", type="string", default= "c1", help="symmetry of the structure") parser.add_option("--function", type="string", default="helical", help="name of the reference preparation function") parser.add_option("--npad", type="int", default= 2, help="padding size for 3D reconstruction") parser.add_option("--debug", action="store_true", default=False, help="debug") parser.add_option("--volalixshift", action="store_true", default=False, help="Use volalixshift refinement") parser.add_option("--searchxshift", type="float", default= 0.0, help="search range for x-shift determination: +/- searchxshift (Angstroms)") parser.add_option("--nearby", type="float", default= 6.0, help="neighborhood within which to search for peaks in 1D ccf for x-shift search (Angstroms)") # filinfo parser.add_option( "--filinfo", type="string", default="", help="Store in an output text file infomration about distribution of filaments in the stack." ) # diskali parser.add_option("--diskali", action="store_true", default=False, help="volume alignment") parser.add_option("--zstep", type="float", default= 1, help="Step size for translational search along z (Angstroms)") # helicise parser.add_option("--helicise", action="store_true", default=False, help="helicise input volume and save results to output volume") parser.add_option("--hfsc", type="string", default="", help="Generate two lists of image indices used to split segment stack into halves for helical fsc calculation. The lists will be stored in two text files named using file_prefix with '_even' and '_odd' suffixes, respectively." ) parser.add_option("--filament_attr", type="string", default="filament", help="attribute under which filament identification is stored" ) parser.add_option("--predict_helical", type="string", default="", help="Generate projection parameters consistent with helical symmetry") # helicise pdb parser.add_option("--helicisepdb", action="store_true", default=False, help="Helicise pdb file and save the result to a new pdb file") parser.add_option("--nrepeats", type="int", default= 50, help="Number of time the helical symmetry will be applied to the input file") # input options for generating disks parser.add_option("--gendisk", type="string", default="", help="Name of file under which generated disks will be saved to") parser.add_option("--ref_nx", type="int", default= -1, help="nx=ny volume size" ) parser.add_option("--ref_nz", type="int", default= -1, help="nz volume size - computed disks will be nx x ny x rise/apix" ) parser.add_option("--new_pixel_size", type="float", default= -1, help="desired pixel size of the output disks. The default is -1, in which case there is no resampling (unless --match_pixel_rise flag is True).") parser.add_option("--maxerror", type="float", default= 0.1, help="proportional to the maximum amount of error to tolerate between (dp/new_pixel_size) and int(dp/new_pixel_size ), where new_pixel_size is the pixel size calculated when the option --match_pixel_rise flag is True.") parser.add_option("--match_pixel_rise", action="store_true", default=False, help="calculate new pixel size such that the rise is approximately integer number of pixels given the new pixel size. This will be the pixel size of the output disks.") # get consistency parser.add_option("--consistency", type="string", default="", help="Name of parameters to get consistency statistics for") parser.add_option("--phithr", type="float", default= 2.0, help="phi threshold for consistency check") parser.add_option("--ythr", type="float", default= 2.0, help="y threshold (in Angstroms) for consistency check") parser.add_option("--segthr", type="int", default= 3, help="minimum number of segments/filament for consistency check") # stack disks parser.add_option("--stackdisk", type="string", default="", help="Name of file under which output volume will be saved to.") parser.add_option("--ref_ny", type="int", default=-1, help="ny of output volume size. Default is ref_nx" ) # symmetry search parser.add_option("--symsearch", action="store_true", default=False, help="Do helical symmetry search." ) parser.add_option("--ndp", type="int", default= 12, help="In symmetrization search, number of delta z steps equals to 2*ndp+1") parser.add_option("--ndphi", type="int", default= 12, help="In symmetrization search, number of dphi steps equals to 2*ndphi+1") parser.add_option("--dp_step", type="float", default= 0.1, help="delta z step for symmetrization [Angstroms] (default 0.1)") parser.add_option("--dphi_step", type="float", default= 0.1, help="dphi step for symmetrization [degrees] (default 0.1)") parser.add_option("--datasym", type="string", default="datasym.txt", help="symdoc") parser.add_option("--symdoc", type="string", default="", help="text file containing helical symmetry parameters dp and dphi") # filament statistics in the stack (options, args) = parser.parse_args(arglist[1:]) if len(args) < 1 or len(args) > 5: print "Various helical reconstruction related functionalities: " + usage2 print "Please run '" + progname + " -h' for detailed options" else: if len(options.hfsc) > 0: if len(args) != 1: print "Incorrect number of parameters" sys.exit() from applications import imgstat_hfsc imgstat_hfsc( args[0], options.hfsc, options.filament_attr) sys.exit() elif len(options.filinfo) > 0: if len(args) != 1: print "Incorrect number of parameters" sys.exit() from EMAN2 import EMUtil filams = EMUtil.get_all_attributes(args[0], "filament") ibeg = 0 filcur = filams[0] n = len(filams) inf = [] i = 1 while( i <= n): if(i < n): fis = filams[i] else: fis = "" if( fis != filcur ): iend = i-1 inf.append([ibeg,iend,iend-ibeg+1,filcur]) ibeg = i filcur = fis i += 1 from utilities import write_text_row write_text_row(inf, options.filinfo) sys.exit() if len(options.stackdisk) > 0: if len(args) != 1: print "Incorrect number of parameters" sys.exit() dpp = (float(options.dp)/options.apix) rise = int(dpp) if(abs(float(rise) - dpp)>1.0e-3): print " dpp has to be integer multiplicity of the pixel size" sys.exit() from utilities import get_im v = get_im(args[0]) from applications import stack_disks ref_ny = options.ref_ny if ref_ny < 0: ref_ny = options.ref_nx sv = stack_disks(v, options.ref_nx, ref_ny, options.ref_nz, options.dphi, rise) sv.write_image(options.stackdisk) sys.exit() if len(options.consistency) > 0: if len(args) != 1: print "Incorrect number of parameters" sys.exit() from development import consistency_params consistency_params(args[0], options.consistency, options.dphi, options.dp, options.apix,phithr=options.phithr, ythr=options.ythr, THR=options.segthr) sys.exit() rminp = int((float(options.rmin)/options.apix) + 0.5) rmaxp = int((float(options.rmax)/options.apix) + 0.5) from utilities import get_input_from_string, get_im xr = get_input_from_string(options.xr) txs = get_input_from_string(options.txs) irp = 1 if options.ou < 0: oup = -1 else: oup = int( (options.ou/options.apix) + 0.5) xrp = '' txsp = '' for i in xrange(len(xr)): xrp += " "+str(float(xr[i])/options.apix) for i in xrange(len(txs)): txsp += " "+str(float(txs[i])/options.apix) searchxshiftp = int( (options.searchxshift/options.apix) + 0.5) nearbyp = int( (options.nearby/options.apix) + 0.5) zstepp = int( (options.zstep/options.apix) + 0.5) if options.MPI: from mpi import mpi_init, mpi_finalize sys.argv = mpi_init(len(sys.argv), sys.argv) if len(options.predict_helical) > 0: if len(args) != 1: print "Incorrect number of parameters" sys.exit() if options.dp < 0: print "Helical symmetry paramter rise --dp should not be negative" sys.exit() from applications import predict_helical_params predict_helical_params(args[0], options.dp, options.dphi, options.apix, options.predict_helical) sys.exit() if options.helicise: if len(args) != 2: print "Incorrect number of parameters" sys.exit() if options.dp < 0: print "Helical symmetry paramter rise --dp should not be negative" sys.exit() from utilities import get_im, sym_vol vol = get_im(args[0]) vol = sym_vol(vol, options.sym) hvol = vol.helicise(options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp) hvol = sym_vol(hvol, options.sym) hvol.write_image(args[1]) sys.exit() if options.helicisepdb: if len(args) != 2: print "Incorrect number of parameters" sys.exit() if options.dp < 0: print "Helical symmetry paramter rise --dp should not be negative" sys.exit() from math import cos, sin, radians from copy import deepcopy import numpy from numpy import zeros,dot,float32 dp = options.dp dphi = options.dphi nperiod = options.nrepeats infile =open(args[0],"r") pall = infile.readlines() infile.close() p = [] pos = [] lkl = -1 for i in xrange( len(pall) ): if( (pall[i])[:4] == 'ATOM'): if( lkl == -1 ): lkl = i p.append( pall[i] ) pos.append(i) n = len(p) X = zeros( (3,len(p) ), dtype=float32 ) X_new = zeros( (3,len(p) ), dtype=float32 ) for i in xrange( len(p) ): element = deepcopy( p[i] ) X[0,i]=float(element[30:38]) X[1,i]=float(element[38:46]) X[2,i]=float(element[46:54]) pnew = [] for j in xrange(-nperiod, nperiod+1): for i in xrange( n ): pnew.append( deepcopy(p[i]) ) dphi = radians(dphi) m = zeros( (3,3 ), dtype=float32 ) t = zeros( (3,1 ), dtype=float32 ) m[2][2] = 1.0 t[0,0] = 0.0 t[1,0] = 0.0 for j in xrange(-nperiod, nperiod+1): if j != 0: rd = j*dphi m[0][0] = cos(rd) m[0][1] = sin(rd) m[1][0] = -m[0][1] m[1][1] = m[0][0] t[2,0] = j*dp X_new = dot(m, X) + t for i in xrange( n ): pnew[j*n+i] = pnew[j*n+i][:30] + "%8.3f"%( float(X_new[0,i]) )+"%8.3f"%( float(X_new[1,i]) )+"%8.3f"%( float(X_new[2,i]) ) + pnew[j*n+i][54:] outfile=open(args[1],"w") outfile.writelines(pall[0:lkl]) outfile.writelines(pnew) outfile.writelines("END\n") outfile.close() sys.exit() if options.volalixshift: if options.maxit > 1: print "Inner iteration for x-shift determinatin is restricted to 1" sys.exit() if len(args) < 4: mask = None else: mask = args[3] from applications import volalixshift_MPI global_def.BATCH = True volalixshift_MPI(args[0], args[1], args[2], searchxshiftp, options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp, mask, options.maxit, options.CTF, options.snr, options.sym, options.function, options.npad, options.debug, nearbyp) global_def.BATCH = False if options.diskali: #if options.maxit > 1: # print "Inner iteration for disk alignment is restricted to 1" # sys.exit() if len(args) < 4: mask = None else: mask = args[3] global_def.BATCH = True if(options.sym[:1] == "d" or options.sym[:1] == "D" ): from development import diskaliD_MPI diskaliD_MPI(args[0], args[1], args[2], mask, options.dp, options.dphi, options.apix, options.function, zstepp, options.fract, rmaxp, rminp, options.CTF, options.maxit, options.sym) else: from applications import diskali_MPI diskali_MPI(args[0], args[1], args[2], mask, options.dp, options.dphi, options.apix, options.function, zstepp, options.fract, rmaxp, rminp, options.CTF, options.maxit, options.sym) global_def.BATCH = False if options.symsearch: if len(options.symdoc) < 1: if options.dp < 0 or options.dphi < 0: print "Enter helical symmetry parameters either using --symdoc or --dp and --dphi" sys.exit() if options.dp < 0 or options.dphi < 0: # read helical symmetry parameters from symdoc from utilities import read_text_row hparams=read_text_row(options.symdoc) dp = hparams[0][0] dphi = hparams[0][1] else: dp = options.dp dphi = options.dphi from applications import symsearch_MPI if len(args) < 3: mask = None else: mask= args[2] global_def.BATCH = True symsearch_MPI(args[0], args[1], mask, dp, options.ndp, options.dp_step, dphi, options.ndphi, options.dphi_step, rminp, rmaxp, options.fract, options.sym, options.function, options.datasym, options.apix, options.debug) global_def.BATCH = False elif len(options.gendisk)> 0: from applications import gendisks_MPI global_def.BATCH = True if len(args) == 1: mask3d = None else: mask3d = args[1] if options.dp < 0: print "Helical symmetry paramter rise --dp must be explictly set!" sys.exit() gendisks_MPI(args[0], mask3d, options.ref_nx, options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp, options.CTF, options.function, options.sym, options.gendisk, options.maxerror, options.new_pixel_size, options.match_pixel_rise) global_def.BATCH = False if options.MPI: from mpi import mpi_finalize mpi_finalize()
def main(): import sys import os import math import random import pyemtbx.options import time from random import random, seed, randint from optparse import OptionParser progname = os.path.basename(sys.argv[0]) usage = progname + """ [options] <inputfile> <outputfile> Generic 2-D image processing programs. Functionality: 1. Phase flip a stack of images and write output to new file: sxprocess.py input_stack.hdf output_stack.hdf --phase_flip 2. Resample (decimate or interpolate up) images (2D or 3D) in a stack to change the pixel size. The window size will change accordingly. sxprocess input.hdf output.hdf --changesize --ratio=0.5 3. Compute average power spectrum of a stack of 2D images with optional padding (option wn) with zeroes. sxprocess.py input_stack.hdf powerspectrum.hdf --pw [--wn=1024] 4. Generate a stack of projections bdb:data and micrographs with prefix mic (i.e., mic0.hdf, mic1.hdf etc) from structure input_structure.hdf, with CTF applied to both projections and micrographs: sxprocess.py input_structure.hdf data mic --generate_projections format="bdb":apix=5.2:CTF=True:boxsize=64 5. Retrieve original image numbers in the selected ISAC group (here group 12 from generation 3): sxprocess.py bdb:test3 class_averages_generation_3.hdf list3_12.txt --isacgroup=12 --params=originalid 6. Retrieve original image numbers of images listed in ISAC output stack of averages: sxprocess.py select1.hdf ohk.txt 7. Adjust rotationally averaged power spectrum of an image to that of a reference image or a reference 1D power spectrum stored in an ASCII file. Optionally use a tangent low-pass filter. Also works for a stack of images, in which case the output is also a stack. sxprocess.py vol.hdf ref.hdf avol.hdf < 0.25 0.2> --adjpw sxprocess.py vol.hdf pw.txt avol.hdf < 0.25 0.2> --adjpw 8. Generate a 1D rotationally averaged power spectrum of an image. sxprocess.py vol.hdf --rotwp=rotpw.txt # Output will contain three columns: (1) rotationally averaged power spectrum (2) logarithm of the rotationally averaged power spectrum (3) integer line number (from zero to approximately to half the image size) 9. Apply 3D transformation (rotation and/or shift) to a set of orientation parameters associated with projection data. sxprocess.py --transfromparams=phi,theta,psi,tx,ty,tz input.txt output.txt The output file is then imported and 3D transformed volume computed: sxheader.py bdb:p --params=xform.projection --import=output.txt mpirun -np 2 sxrecons3d_n.py bdb:p tvol.hdf --MPI The reconstructed volume is in the position of the volume computed using the input.txt parameters and then transformed with rot_shift3D(vol, phi,theta,psi,tx,ty,tz) 10. Import ctf parameters from the output of sxcter into windowed particle headers. There are three possible input files formats: (1) all particles are in one stack, (2 aor 3) particles are in stacks, each stack corresponds to a single micrograph. In each case the particles should contain a name of the micrograph of origin stores using attribute name 'ptcl_source_image'. Normally this is done by e2boxer.py during windowing. Particles whose defocus or astigmatism error exceed set thresholds will be skipped, otherwise, virtual stacks with the original way preceded by G will be created. sxprocess.py --input=bdb:data --importctf=outdir/partres --defocuserror=10.0 --astigmatismerror=5.0 # Output will be a vritual stack bdb:Gdata sxprocess.py --input="bdb:directory/stacks*" --importctf=outdir/partres --defocuserror=10.0 --astigmatismerror=5.0 To concatenate output files: cd directory e2bdb.py . --makevstack=bdb:allparticles --filt=G IMPORTANT: Please do not move (or remove!) any input/intermediate EMAN2DB files as the information is linked between them. 11. Scale 3D shifts. The shifts in the input five columns text file with 3D orientation parameters will be DIVIDED by the scale factor sxprocess.py orientationparams.txt scaledparams.txt scale=0.5 12. Generate adaptive mask from a given 3-D volume. """ parser = OptionParser(usage, version=SPARXVERSION) parser.add_option( "--order", action="store_true", help= "Two arguments are required: name of input stack and desired name of output stack. The output stack is the input stack sorted by similarity in terms of cross-correlation coefficent.", default=False) parser.add_option("--order_lookup", action="store_true", help="Test/Debug.", default=False) parser.add_option("--order_metropolis", action="store_true", help="Test/Debug.", default=False) parser.add_option("--order_pca", action="store_true", help="Test/Debug.", default=False) parser.add_option( "--initial", type="int", default=-1, help= "Specifies which image will be used as an initial seed to form the chain. (default = 0, means the first image)" ) parser.add_option( "--circular", action="store_true", help= "Select circular ordering (fisr image has to be similar to the last", default=False) parser.add_option( "--radius", type="int", default=-1, help="Radius of a circular mask for similarity based ordering") parser.add_option( "--changesize", action="store_true", help= "resample (decimate or interpolate up) images (2D or 3D) in a stack to change the pixel size.", default=False) parser.add_option( "--ratio", type="float", default=1.0, help= "The ratio of new to old image size (if <1 the pixel size will increase and image size decrease, if>1, the other way round" ) parser.add_option( "--pw", action="store_true", help= "compute average power spectrum of a stack of 2-D images with optional padding (option wn) with zeroes", default=False) parser.add_option( "--wn", type="int", default=-1, help= "Size of window to use (should be larger/equal than particle box size, default padding to max(nx,ny))" ) parser.add_option("--phase_flip", action="store_true", help="Phase flip the input stack", default=False) parser.add_option( "--makedb", metavar="param1=value1:param2=value2", type="string", action="append", help= "One argument is required: name of key with which the database will be created. Fill in database with parameters specified as follows: --makedb param1=value1:param2=value2, e.g. 'gauss_width'=1.0:'pixel_input'=5.2:'pixel_output'=5.2:'thr_low'=1.0" ) parser.add_option( "--generate_projections", metavar="param1=value1:param2=value2", type="string", action="append", help= "Three arguments are required: name of input structure from which to generate projections, desired name of output projection stack, and desired prefix for micrographs (e.g. if prefix is 'mic', then micrographs mic0.hdf, mic1.hdf etc will be generated). Optional arguments specifying format, apix, box size and whether to add CTF effects can be entered as follows after --generate_projections: format='bdb':apix=5.2:CTF=True:boxsize=100, or format='hdf', etc., where format is bdb or hdf, apix (pixel size) is a float, CTF is True or False, and boxsize denotes the dimension of the box (assumed to be a square). If an optional parameter is not specified, it will default as follows: format='bdb', apix=2.5, CTF=False, boxsize=64." ) parser.add_option( "--isacgroup", type="int", help= "Retrieve original image numbers in the selected ISAC group. See ISAC documentation for details.", default=-1) parser.add_option( "--isacselect", action="store_true", help= "Retrieve original image numbers of images listed in ISAC output stack of averages. See ISAC documentation for details.", default=False) parser.add_option( "--params", type="string", default=None, help="Name of header of parameter, which one depends on specific option" ) parser.add_option( "--adjpw", action="store_true", help="Adjust rotationally averaged power spectrum of an image", default=False) parser.add_option( "--rotpw", type="string", default=None, help= "Name of the text file to contain rotationally averaged power spectrum of the input image." ) parser.add_option( "--transformparams", type="string", default=None, help= "Transform 3D projection orientation parameters using six 3D parameters (phi, theta,psi,sx,sy,sz). Input: --transformparams=45.,66.,12.,-2,3,-5.5 desired six transformation of the reconstructed structure. Output: file with modified orientation parameters." ) # import ctf estimates done using cter parser.add_option("--input", type="string", default=None, help="Input particles.") parser.add_option( "--importctf", type="string", default=None, help="Name of the file containing CTF parameters produced by sxcter.") parser.add_option( "--defocuserror", type="float", default=1000000.0, help= "Exclude micrographs whose relative defocus error as estimated by sxcter is larger than defocuserror percent. The error is computed as (std dev defocus)/defocus*100%" ) parser.add_option( "--astigmatismerror", type="float", default=360.0, help= "Set to zero astigmatism for micrographs whose astigmatism angular error as estimated by sxcter is larger than astigmatismerror degrees." ) # import ctf estimates done using cter parser.add_option( "--scale", type="float", default=-1.0, help= "Divide shifts in the input 3D orientation parameters text file by the scale factor." ) # generate adaptive mask from an given 3-Db volue parser.add_option("--adaptive_mask", action="store_true", help="create adavptive 3-D mask from a given volume", default=False) parser.add_option( "--nsigma", type="float", default=1., help= "number of times of sigma of the input volume to obtain the the large density cluster" ) parser.add_option( "--ndilation", type="int", default=3, help= "number of times of dilation applied to the largest cluster of density" ) parser.add_option( "--kernel_size", type="int", default=11, help="convolution kernel for smoothing the edge of the mask") parser.add_option( "--gauss_standard_dev", type="int", default=9, help="stanadard deviation value to generate Gaussian edge") (options, args) = parser.parse_args() global_def.BATCH = True if options.phase_flip: nargs = len(args) if nargs != 2: print "must provide name of input and output file!" return from EMAN2 import Processor instack = args[0] outstack = args[1] nima = EMUtil.get_image_count(instack) from filter import filt_ctf for i in xrange(nima): img = EMData() img.read_image(instack, i) try: ctf = img.get_attr('ctf') except: print "no ctf information in input stack! Exiting..." return dopad = True sign = 1 binary = 1 # phase flip assert img.get_ysize() > 1 dict = ctf.to_dict() dz = dict["defocus"] cs = dict["cs"] voltage = dict["voltage"] pixel_size = dict["apix"] b_factor = dict["bfactor"] ampcont = dict["ampcont"] dza = dict["dfdiff"] azz = dict["dfang"] if dopad and not img.is_complex(): ip = 1 else: ip = 0 params = { "filter_type": Processor.fourier_filter_types.CTF_, "defocus": dz, "Cs": cs, "voltage": voltage, "Pixel_size": pixel_size, "B_factor": b_factor, "amp_contrast": ampcont, "dopad": ip, "binary": binary, "sign": sign, "dza": dza, "azz": azz } tmp = Processor.EMFourierFilter(img, params) tmp.set_attr_dict({"ctf": ctf}) tmp.write_image(outstack, i) elif options.changesize: nargs = len(args) if nargs != 2: ERROR("must provide name of input and output file!", "change size", 1) return from utilities import get_im instack = args[0] outstack = args[1] sub_rate = float(options.ratio) nima = EMUtil.get_image_count(instack) from fundamentals import resample for i in xrange(nima): resample(get_im(instack, i), sub_rate).write_image(outstack, i) elif options.isacgroup > -1: nargs = len(args) if nargs != 3: ERROR("Three files needed on input!", "isacgroup", 1) return from utilities import get_im instack = args[0] m = get_im(args[1], int(options.isacgroup)).get_attr("members") l = [] for k in m: l.append(int(get_im(args[0], k).get_attr(options.params))) from utilities import write_text_file write_text_file(l, args[2]) elif options.isacselect: nargs = len(args) if nargs != 2: ERROR("Two files needed on input!", "isacgroup", 1) return from utilities import get_im nima = EMUtil.get_image_count(args[0]) m = [] for k in xrange(nima): m += get_im(args[0], k).get_attr("members") m.sort() from utilities import write_text_file write_text_file(m, args[1]) elif options.pw: nargs = len(args) if nargs < 2: ERROR("must provide name of input and output file!", "pw", 1) return from utilities import get_im d = get_im(args[0]) nx = d.get_xsize() ny = d.get_ysize() if nargs == 3: mask = get_im(args[2]) wn = int(options.wn) if wn == -1: wn = max(nx, ny) else: if ((wn < nx) or (wn < ny)): ERROR("window size cannot be smaller than the image size", "pw", 1) n = EMUtil.get_image_count(args[0]) from utilities import model_blank, model_circle, pad from EMAN2 import periodogram p = model_blank(wn, wn) for i in xrange(n): d = get_im(args[0], i) if nargs == 3: d *= mask st = Util.infomask(d, None, True) d -= st[0] p += periodogram(pad(d, wn, wn, 1, 0.)) p /= n p.write_image(args[1]) elif options.adjpw: if len(args) < 3: ERROR( "filt_by_rops input target output fl aa (the last two are optional parameters of a low-pass filter)", "adjpw", 1) return img_stack = args[0] from math import sqrt from fundamentals import rops_table, fft from utilities import read_text_file, get_im from filter import filt_tanl, filt_table if (args[1][-3:] == 'txt'): rops_dst = read_text_file(args[1]) else: rops_dst = rops_table(get_im(args[1])) out_stack = args[2] if (len(args) > 4): fl = float(args[3]) aa = float(args[4]) else: fl = -1.0 aa = 0.0 nimage = EMUtil.get_image_count(img_stack) for i in xrange(nimage): img = fft(get_im(img_stack, i)) rops_src = rops_table(img) assert len(rops_dst) == len(rops_src) table = [0.0] * len(rops_dst) for j in xrange(len(rops_dst)): table[j] = sqrt(rops_dst[j] / rops_src[j]) if (fl > 0.0): img = filt_tanl(img, fl, aa) img = fft(filt_table(img, table)) img.write_image(out_stack, i) elif options.rotpw != None: if len(args) != 1: ERROR("Only one input permitted", "rotpw", 1) return from utilities import write_text_file, get_im from fundamentals import rops_table from math import log10 t = rops_table(get_im(args[0])) x = range(len(t)) r = [0.0] * len(x) for i in x: r[i] = log10(t[i]) write_text_file([t, r, x], options.rotpw) elif options.transformparams != None: if len(args) != 2: ERROR( "Please provide names of input and output files with orientation parameters", "transformparams", 1) return from utilities import read_text_row, write_text_row transf = [0.0] * 6 spl = options.transformparams.split(',') for i in xrange(len(spl)): transf[i] = float(spl[i]) write_text_row(rotate_shift_params(read_text_row(args[0]), transf), args[1]) elif options.makedb != None: nargs = len(args) if nargs != 1: print "must provide exactly one argument denoting database key under which the input params will be stored" return dbkey = args[0] print "database key under which params will be stored: ", dbkey gbdb = js_open_dict("e2boxercache/gauss_box_DB.json") parmstr = 'dummy:' + options.makedb[0] (processorname, param_dict) = parsemodopt(parmstr) dbdict = {} for pkey in param_dict: if (pkey == 'invert_contrast') or (pkey == 'use_variance'): if param_dict[pkey] == 'True': dbdict[pkey] = True else: dbdict[pkey] = False else: dbdict[pkey] = param_dict[pkey] gbdb[dbkey] = dbdict elif options.generate_projections: nargs = len(args) if nargs != 3: ERROR("Must provide name of input structure(s) from which to generate projections, name of output projection stack, and prefix for output micrographs."\ "sxprocess - generate projections",1) return inpstr = args[0] outstk = args[1] micpref = args[2] parmstr = 'dummy:' + options.generate_projections[0] (processorname, param_dict) = parsemodopt(parmstr) parm_CTF = False parm_format = 'bdb' parm_apix = 2.5 if 'CTF' in param_dict: if param_dict['CTF'] == 'True': parm_CTF = True if 'format' in param_dict: parm_format = param_dict['format'] if 'apix' in param_dict: parm_apix = float(param_dict['apix']) boxsize = 64 if 'boxsize' in param_dict: boxsize = int(param_dict['boxsize']) print "pixel size: ", parm_apix, " format: ", parm_format, " add CTF: ", parm_CTF, " box size: ", boxsize scale_mult = 2500 sigma_add = 1.5 sigma_proj = 30.0 sigma2_proj = 17.5 sigma_gauss = 0.3 sigma_mic = 30.0 sigma2_mic = 17.5 sigma_gauss_mic = 0.3 if 'scale_mult' in param_dict: scale_mult = float(param_dict['scale_mult']) if 'sigma_add' in param_dict: sigma_add = float(param_dict['sigma_add']) if 'sigma_proj' in param_dict: sigma_proj = float(param_dict['sigma_proj']) if 'sigma2_proj' in param_dict: sigma2_proj = float(param_dict['sigma2_proj']) if 'sigma_gauss' in param_dict: sigma_gauss = float(param_dict['sigma_gauss']) if 'sigma_mic' in param_dict: sigma_mic = float(param_dict['sigma_mic']) if 'sigma2_mic' in param_dict: sigma2_mic = float(param_dict['sigma2_mic']) if 'sigma_gauss_mic' in param_dict: sigma_gauss_mic = float(param_dict['sigma_gauss_mic']) from filter import filt_gaussl, filt_ctf from utilities import drop_spider_doc, even_angles, model_gauss, delete_bdb, model_blank, pad, model_gauss_noise, set_params2D, set_params_proj from projection import prep_vol, prgs seed(14567) delta = 29 angles = even_angles(delta, 0.0, 89.9, 0.0, 359.9, "S") nangle = len(angles) modelvol = [] nvlms = EMUtil.get_image_count(inpstr) from utilities import get_im for k in xrange(nvlms): modelvol.append(get_im(inpstr, k)) nx = modelvol[0].get_xsize() if nx != boxsize: ERROR("Requested box dimension does not match dimension of the input model.", \ "sxprocess - generate projections",1) nvol = 10 volfts = [[] for k in xrange(nvlms)] for k in xrange(nvlms): for i in xrange(nvol): sigma = sigma_add + random() # 1.5-2.5 addon = model_gauss(sigma, boxsize, boxsize, boxsize, sigma, sigma, 38, 38, 40) scale = scale_mult * (0.5 + random()) vf, kb = prep_vol(modelvol[k] + scale * addon) volfts[k].append(vf) del vf, modelvol if parm_format == "bdb": stack_data = "bdb:" + outstk delete_bdb(stack_data) else: stack_data = outstk + ".hdf" Cs = 2.0 pixel = parm_apix voltage = 120.0 ampcont = 10.0 ibd = 4096 / 2 - boxsize iprj = 0 width = 240 xstart = 8 + boxsize / 2 ystart = 8 + boxsize / 2 rowlen = 17 from random import randint params = [] for idef in xrange(3, 8): irow = 0 icol = 0 mic = model_blank(4096, 4096) defocus = idef * 0.5 #0.2 if parm_CTF: astampl = defocus * 0.15 astangl = 50.0 ctf = generate_ctf([ defocus, Cs, voltage, pixel, ampcont, 0.0, astampl, astangl ]) for i in xrange(nangle): for k in xrange(12): dphi = 8.0 * (random() - 0.5) dtht = 8.0 * (random() - 0.5) psi = 360.0 * random() phi = angles[i][0] + dphi tht = angles[i][1] + dtht s2x = 4.0 * (random() - 0.5) s2y = 4.0 * (random() - 0.5) params.append([phi, tht, psi, s2x, s2y]) ivol = iprj % nvol #imgsrc = randint(0,nvlms-1) imgsrc = iprj % nvlms proj = prgs(volfts[imgsrc][ivol], kb, [phi, tht, psi, -s2x, -s2y]) x = xstart + irow * width y = ystart + icol * width mic += pad(proj, 4096, 4096, 1, 0.0, x - 2048, y - 2048, 0) proj = proj + model_gauss_noise(sigma_proj, nx, nx) if parm_CTF: proj = filt_ctf(proj, ctf) proj.set_attr_dict({"ctf": ctf, "ctf_applied": 0}) proj = proj + filt_gaussl( model_gauss_noise(sigma2_proj, nx, nx), sigma_gauss) proj.set_attr("origimgsrc", imgsrc) proj.set_attr("test_id", iprj) # flags describing the status of the image (1 = true, 0 = false) set_params2D(proj, [0.0, 0.0, 0.0, 0, 1.0]) set_params_proj(proj, [phi, tht, psi, s2x, s2y]) proj.write_image(stack_data, iprj) icol += 1 if icol == rowlen: icol = 0 irow += 1 iprj += 1 mic += model_gauss_noise(sigma_mic, 4096, 4096) if parm_CTF: #apply CTF mic = filt_ctf(mic, ctf) mic += filt_gaussl(model_gauss_noise(sigma2_mic, 4096, 4096), sigma_gauss_mic) mic.write_image(micpref + "%1d.hdf" % (idef - 3), 0) drop_spider_doc("params.txt", params) elif options.importctf != None: print ' IMPORTCTF ' from utilities import read_text_row, write_text_row from random import randint import subprocess grpfile = 'groupid%04d' % randint(1000, 9999) ctfpfile = 'ctfpfile%04d' % randint(1000, 9999) cterr = [options.defocuserror / 100.0, options.astigmatismerror] ctfs = read_text_row(options.importctf) for kk in xrange(len(ctfs)): root, name = os.path.split(ctfs[kk][-1]) ctfs[kk][-1] = name[:-4] if (options.input[:4] != 'bdb:'): ERROR('Sorry, only bdb files implemented', 'importctf', 1) d = options.input[4:] #try: str = d.index('*') #except: str = -1 from string import split import glob uu = os.path.split(d) uu = os.path.join(uu[0], 'EMAN2DB', uu[1] + '.bdb') flist = glob.glob(uu) for i in xrange(len(flist)): root, name = os.path.split(flist[i]) root = root[:-7] name = name[:-4] fil = 'bdb:' + os.path.join(root, name) sourcemic = EMUtil.get_all_attributes(fil, 'ptcl_source_image') nn = len(sourcemic) gctfp = [] groupid = [] for kk in xrange(nn): junk, name2 = os.path.split(sourcemic[kk]) name2 = name2[:-4] ctfp = [-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] for ll in xrange(len(ctfs)): if (name2 == ctfs[ll][-1]): # found correct if (ctfs[ll][8] / ctfs[ll][0] <= cterr[0]): # acceptable defocus error ctfp = ctfs[ll][:8] if (ctfs[ll][10] > cterr[1]): # error of astigmatism exceed the threshold, set astigmatism to zero. ctfp[6] = 0.0 ctfp[7] = 0.0 gctfp.append(ctfp) groupid.append(kk) break if (len(groupid) > 0): write_text_row(groupid, grpfile) write_text_row(gctfp, ctfpfile) cmd = "{} {} {} {}".format( 'e2bdb.py', fil, '--makevstack=bdb:' + root + 'G' + name, '--list=' + grpfile) #print cmd subprocess.call(cmd, shell=True) cmd = "{} {} {} {}".format('sxheader.py', 'bdb:' + root + 'G' + name, '--params=ctf', '--import=' + ctfpfile) #print cmd subprocess.call(cmd, shell=True) else: print ' >>> Group ', name, ' skipped.' cmd = "{} {} {}".format("rm -f", grpfile, ctfpfile) subprocess.call(cmd, shell=True) elif options.scale > 0.0: from utilities import read_text_row, write_text_row scale = options.scale nargs = len(args) if nargs != 2: print "Please provide names of input and output file!" return p = read_text_row(args[0]) for i in xrange(len(p)): p[i][3] /= scale p[i][4] /= scale write_text_row(p, args[1]) elif options.adaptive_mask: from utilities import get_im from morphology import adaptive_mask nsigma = options.nsigma ndilation = options.ndilation kernel_size = options.kernel_size gauss_standard_dev = options.gauss_standard_dev nargs = len(args) if nargs > 2: print "Too many inputs are given, try again!" return else: inputvol = get_im(args[0]) input_path, input_file_name = os.path.split(args[0]) input_file_name_root, ext = os.path.splitext(input_file_name) if nargs == 2: mask_file_name = args[1] else: mask_file_name = "adaptive_mask_for" + input_file_name_root + ".hdf" # Only hdf file is output. mask3d = adaptive_mask(inputvol, nsigma, ndilation, kernel_size, gauss_standard_dev) mask3d.write_image(mask_file_name) else: ERROR("Please provide option name", "sxprocess.py", 1)
def dovolume( ref_data ): from utilities import print_msg, read_text_row from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold # Prepare the reference in 3D alignment, this function corresponds to what do_volume does. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 fl = ref_data[2].cmp("dot",ref_data[2], {"negative":0, "mask":ref_data[0]} ) print_msg("do_volume user function Step = %5d GOAL = %10.3e\n"%(ref_ali2d_counter,fl)) stat = Util.infomask(ref_data[2], ref_data[0], False) vol = ref_data[2] - stat[0] Util.mul_scalar(vol, 1.0/stat[1]) vol = threshold(vol) #Util.mul_img(vol, ref_data[0]) try: aa = read_text_row("flaa.txt")[0] fl = aa[0] aa=aa[1] except: fl = 0.4 aa = 0.2 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) from utilities import read_text_file from fundamentals import rops_table, fftip, fft from filter import filt_table, filt_btwl fftip(vol) try: rt = read_text_file( "pwreference.txt" ) ro = rops_table(vol) # Here unless I am mistaken it is enough to take the beginning of the reference pw. for i in xrange(1,len(ro)): ro[i] = (rt[i]/ro[i])**0.5 vol = fft( filt_table( filt_tanl(vol, fl, aa), ro) ) msg = "Power spectrum adjusted\n" print_msg(msg) except: vol = fft( filt_tanl(vol, fl, aa) ) stat = Util.infomask(vol, ref_data[0], False) vol -= stat[0] Util.mul_scalar(vol, 1.0/stat[1]) vol = threshold(vol) vol = filt_btwl(vol, 0.38, 0.5) Util.mul_img(vol, ref_data[0]) if ref_data[1] == 1: cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n"%(cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) else: cs = [0.0]*3 return vol, cs
def main(): import os import sys from optparse import OptionParser from global_def import SPARXVERSION import global_def arglist = [] for arg in sys.argv: arglist.append(arg) progname = os.path.basename(arglist[0]) usage2 = progname + """ inputfile outputfile [options] Functionalities: 1. Helicise input volume and save the result to output volume: sxhelicon_utils.py input_vol.hdf output_vol.hdf --helicise --dp=27.6 --dphi=166.5 --fract=0.65 --rmax=70 --rmin=1 --apix=1.84 --sym=D1 2. Helicise pdb file and save the result to a new pdb file: sxhelicon_utils.py input.pdb output.pdb --helicisepdb --dp=27.6 --dphi=166.5 --nrepeats --apix=1.84 3. Generate two lists of image indices used to split segment stack into halves for helical fsc calculation. sxhelicon_utils.py bdb:big_stack --hfsc='flst' --filament_attr=filament 4. Map of filament distribution in the stack sxhelicon_utils.py bdb:big_stack --filinfo=info.txt The output file will contain four columns: 1 2 3 4 first image number last image number number of images in the filament name 5. Predict segments' orientation parameters based on distances between segments and known helical symmetry sxhelicon_utils.py bdb:big_stack --predict_helical=helical_params.txt --dp=27.6 --dphi=166.5 --apix=1.84 6. Generate disks from filament based reconstructions: sxheader.py stk.hdf --params=xform.projection --import=params.txt mpirun -np 2 sxhelicon_utils.py stk.hdf --gendisk='bdb:disk' --ref_nx=100 --ref_ny=100 --ref_nz=200 --apix=1.84 --dp=27.6 --dphi=166.715 --fract=0.67 --rmin=0 --rmax=64 --function="[.,nofunc,helical3c]" --sym="c1" --MPI 7. Stack disks based on helical symmetry parameters sxhelicon_utils.py disk_to_stack.hdf --stackdisk=stacked_disks.hdf --dphi=166.5 --dp=27.6 --ref_nx=160 --ref_ny=160 --ref_nz=225 --apix=1.84 8. Helical symmetry search: mpirun -np 3 sxhelicon_utils.py volf0010.hdf outsymsearch --symsearch --dp=27.6 --dphi=166.715 --apix=1.84 --fract=0.65 --rmin=0 --rmax=92.0 --datasym=datasym.txt --dp_step=0.92 --ndp=3 --dphi_step=1.0 --ndphi=10 --MPI """ parser = OptionParser(usage2, version=SPARXVERSION) #parser.add_option("--ir", type="float", default= -1, help="inner radius for rotational correlation > 0 (set to 1) (Angstroms)") parser.add_option( "--ou", type="float", default=-1, help= "outer radius for rotational 2D correlation < int(nx/2)-1 (set to the radius of the particle) (Angstroms)" ) parser.add_option( "--rs", type="int", default=1, help="step between rings in rotational correlation >0 (set to 1)") parser.add_option( "--xr", type="string", default="4 2 1 1 1", help= "range for translation search in x direction, search is +/-xr (Angstroms) " ) parser.add_option( "--txs", type="string", default="1 1 1 0.5 0.25", help= "step size of the translation search in x directions, search is -xr, -xr+ts, 0, xr-ts, xr (Angstroms)" ) parser.add_option("--delta", type="string", default="10 6 4 3 2", help="angular step of reference projections") parser.add_option("--an", type="string", default="-1", help="angular neighborhood for local searches") parser.add_option( "--maxit", type="int", default=30, help= "maximum number of iterations performed for each angular step (set to 30) " ) parser.add_option("--CTF", action="store_true", default=False, help="CTF correction") parser.add_option("--snr", type="float", default=1.0, help="Signal-to-Noise Ratio of the data") parser.add_option("--MPI", action="store_true", default=False, help="use MPI version") #parser.add_option("--fourvar", action="store_true", default=False, help="compute Fourier variance") parser.add_option("--apix", type="float", default=-1.0, help="pixel size in Angstroms") parser.add_option("--dp", type="float", default=-1.0, help="delta z - translation in Angstroms") parser.add_option("--dphi", type="float", default=-1.0, help="delta phi - rotation in degrees") parser.add_option("--rmin", type="float", default=0.0, help="minimal radius for hsearch (Angstroms)") parser.add_option("--rmax", type="float", default=80.0, help="maximal radius for hsearch (Angstroms)") parser.add_option("--fract", type="float", default=0.7, help="fraction of the volume used for helical search") parser.add_option("--sym", type="string", default="c1", help="symmetry of the structure") parser.add_option("--function", type="string", default="helical", help="name of the reference preparation function") parser.add_option("--npad", type="int", default=2, help="padding size for 3D reconstruction") parser.add_option("--debug", action="store_true", default=False, help="debug") parser.add_option("--volalixshift", action="store_true", default=False, help="Use volalixshift refinement") parser.add_option( "--searchxshift", type="float", default=0.0, help= "search range for x-shift determination: +/- searchxshift (Angstroms)") parser.add_option( "--nearby", type="float", default=6.0, help= "neighborhood within which to search for peaks in 1D ccf for x-shift search (Angstroms)" ) # filinfo parser.add_option( "--filinfo", type="string", default="", help= "Store in an output text file infomration about distribution of filaments in the stack." ) # diskali parser.add_option("--diskali", action="store_true", default=False, help="volume alignment") parser.add_option( "--zstep", type="float", default=1, help="Step size for translational search along z (Angstroms)") # helicise parser.add_option( "--helicise", action="store_true", default=False, help="helicise input volume and save results to output volume") parser.add_option( "--hfsc", type="string", default="", help= "Generate two lists of image indices used to split segment stack into halves for helical fsc calculation. The lists will be stored in two text files named using file_prefix with '_even' and '_odd' suffixes, respectively." ) parser.add_option( "--filament_attr", type="string", default="filament", help="attribute under which filament identification is stored") parser.add_option( "--predict_helical", type="string", default="", help="Generate projection parameters consistent with helical symmetry") # helicise pdb parser.add_option( "--helicisepdb", action="store_true", default=False, help="Helicise pdb file and save the result to a new pdb file") parser.add_option( "--nrepeats", type="int", default=50, help= "Number of time the helical symmetry will be applied to the input file" ) # input options for generating disks parser.add_option( "--gendisk", type="string", default="", help="Name of file under which generated disks will be saved to") parser.add_option("--ref_nx", type="int", default=-1, help="nx=ny volume size") parser.add_option( "--ref_nz", type="int", default=-1, help="nz volume size - computed disks will be nx x ny x rise/apix") parser.add_option( "--new_pixel_size", type="float", default=-1, help= "desired pixel size of the output disks. The default is -1, in which case there is no resampling (unless --match_pixel_rise flag is True)." ) parser.add_option( "--maxerror", type="float", default=0.1, help= "proportional to the maximum amount of error to tolerate between (dp/new_pixel_size) and int(dp/new_pixel_size ), where new_pixel_size is the pixel size calculated when the option --match_pixel_rise flag is True." ) parser.add_option( "--match_pixel_rise", action="store_true", default=False, help= "calculate new pixel size such that the rise is approximately integer number of pixels given the new pixel size. This will be the pixel size of the output disks." ) # get consistency parser.add_option( "--consistency", type="string", default="", help="Name of parameters to get consistency statistics for") parser.add_option("--phithr", type="float", default=2.0, help="phi threshold for consistency check") parser.add_option("--ythr", type="float", default=2.0, help="y threshold (in Angstroms) for consistency check") parser.add_option( "--segthr", type="int", default=3, help="minimum number of segments/filament for consistency check") # stack disks parser.add_option( "--stackdisk", type="string", default="", help="Name of file under which output volume will be saved to.") parser.add_option("--ref_ny", type="int", default=-1, help="ny of output volume size. Default is ref_nx") # symmetry search parser.add_option("--symsearch", action="store_true", default=False, help="Do helical symmetry search.") parser.add_option( "--ndp", type="int", default=12, help= "In symmetrization search, number of delta z steps equals to 2*ndp+1") parser.add_option( "--ndphi", type="int", default=12, help= "In symmetrization search, number of dphi steps equals to 2*ndphi+1") parser.add_option( "--dp_step", type="float", default=0.1, help="delta z step for symmetrization [Angstroms] (default 0.1)") parser.add_option( "--dphi_step", type="float", default=0.1, help="dphi step for symmetrization [degrees] (default 0.1)") parser.add_option("--datasym", type="string", default="datasym.txt", help="symdoc") parser.add_option( "--symdoc", type="string", default="", help="text file containing helical symmetry parameters dp and dphi") # filament statistics in the stack (options, args) = parser.parse_args(arglist[1:]) if len(args) < 1 or len(args) > 5: print("Various helical reconstruction related functionalities: " + usage2) print("Please run '" + progname + " -h' for detailed options") else: if len(options.hfsc) > 0: if len(args) != 1: print("Incorrect number of parameters") sys.exit() from applications import imgstat_hfsc imgstat_hfsc(args[0], options.hfsc, options.filament_attr) sys.exit() elif len(options.filinfo) > 0: if len(args) != 1: print("Incorrect number of parameters") sys.exit() from EMAN2 import EMUtil filams = EMUtil.get_all_attributes(args[0], "filament") ibeg = 0 filcur = filams[0] n = len(filams) inf = [] i = 1 while (i <= n): if (i < n): fis = filams[i] else: fis = "" if (fis != filcur): iend = i - 1 inf.append([ibeg, iend, iend - ibeg + 1, filcur]) ibeg = i filcur = fis i += 1 from utilities import write_text_row write_text_row(inf, options.filinfo) sys.exit() if len(options.stackdisk) > 0: if len(args) != 1: print("Incorrect number of parameters") sys.exit() dpp = (float(options.dp) / options.apix) rise = int(dpp) if (abs(float(rise) - dpp) > 1.0e-3): print(" dpp has to be integer multiplicity of the pixel size") sys.exit() from utilities import get_im v = get_im(args[0]) from applications import stack_disks ref_ny = options.ref_ny if ref_ny < 0: ref_ny = options.ref_nx sv = stack_disks(v, options.ref_nx, ref_ny, options.ref_nz, options.dphi, rise) sv.write_image(options.stackdisk) sys.exit() if len(options.consistency) > 0: if len(args) != 1: print("Incorrect number of parameters") sys.exit() from development import consistency_params consistency_params(args[0], options.consistency, options.dphi, options.dp, options.apix, phithr=options.phithr, ythr=options.ythr, THR=options.segthr) sys.exit() rminp = int((float(options.rmin) / options.apix) + 0.5) rmaxp = int((float(options.rmax) / options.apix) + 0.5) from utilities import get_input_from_string, get_im xr = get_input_from_string(options.xr) txs = get_input_from_string(options.txs) irp = 1 if options.ou < 0: oup = -1 else: oup = int((options.ou / options.apix) + 0.5) xrp = '' txsp = '' for i in xrange(len(xr)): xrp += " " + str(float(xr[i]) / options.apix) for i in xrange(len(txs)): txsp += " " + str(float(txs[i]) / options.apix) searchxshiftp = int((options.searchxshift / options.apix) + 0.5) nearbyp = int((options.nearby / options.apix) + 0.5) zstepp = int((options.zstep / options.apix) + 0.5) if options.MPI: from mpi import mpi_init, mpi_finalize sys.argv = mpi_init(len(sys.argv), sys.argv) if len(options.predict_helical) > 0: if len(args) != 1: print("Incorrect number of parameters") sys.exit() if options.dp < 0: print( "Helical symmetry paramter rise --dp should not be negative" ) sys.exit() from applications import predict_helical_params predict_helical_params(args[0], options.dp, options.dphi, options.apix, options.predict_helical) sys.exit() if options.helicise: if len(args) != 2: print("Incorrect number of parameters") sys.exit() if options.dp < 0: print( "Helical symmetry paramter rise --dp should not be negative" ) sys.exit() from utilities import get_im, sym_vol vol = get_im(args[0]) vol = sym_vol(vol, options.sym) hvol = vol.helicise(options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp) hvol = sym_vol(hvol, options.sym) hvol.write_image(args[1]) sys.exit() if options.helicisepdb: if len(args) != 2: print("Incorrect number of parameters") sys.exit() if options.dp < 0: print( "Helical symmetry paramter rise --dp should not be negative" ) sys.exit() from math import cos, sin, radians from copy import deepcopy import numpy from numpy import zeros, dot, float32 dp = options.dp dphi = options.dphi nperiod = options.nrepeats infile = open(args[0], "r") pall = infile.readlines() infile.close() p = [] pos = [] lkl = -1 for i in xrange(len(pall)): if ((pall[i])[:4] == 'ATOM'): if (lkl == -1): lkl = i p.append(pall[i]) pos.append(i) n = len(p) X = zeros((3, len(p)), dtype=float32) X_new = zeros((3, len(p)), dtype=float32) for i in xrange(len(p)): element = deepcopy(p[i]) X[0, i] = float(element[30:38]) X[1, i] = float(element[38:46]) X[2, i] = float(element[46:54]) pnew = [] for j in xrange(-nperiod, nperiod + 1): for i in xrange(n): pnew.append(deepcopy(p[i])) dphi = radians(dphi) m = zeros((3, 3), dtype=float32) t = zeros((3, 1), dtype=float32) m[2][2] = 1.0 t[0, 0] = 0.0 t[1, 0] = 0.0 for j in xrange(-nperiod, nperiod + 1): if j != 0: rd = j * dphi m[0][0] = cos(rd) m[0][1] = sin(rd) m[1][0] = -m[0][1] m[1][1] = m[0][0] t[2, 0] = j * dp X_new = dot(m, X) + t for i in xrange(n): pnew[j * n + i] = pnew[j * n + i][:30] + "%8.3f" % (float( X_new[0, i])) + "%8.3f" % (float( X_new[1, i])) + "%8.3f" % (float( X_new[2, i])) + pnew[j * n + i][54:] outfile = open(args[1], "w") outfile.writelines(pall[0:lkl]) outfile.writelines(pnew) outfile.writelines("END\n") outfile.close() sys.exit() if options.volalixshift: if options.maxit > 1: print( "Inner iteration for x-shift determinatin is restricted to 1" ) sys.exit() if len(args) < 4: mask = None else: mask = args[3] from applications import volalixshift_MPI global_def.BATCH = True volalixshift_MPI(args[0], args[1], args[2], searchxshiftp, options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp, mask, options.maxit, options.CTF, options.snr, options.sym, options.function, options.npad, options.debug, nearbyp) global_def.BATCH = False if options.diskali: #if options.maxit > 1: # print "Inner iteration for disk alignment is restricted to 1" # sys.exit() if len(args) < 4: mask = None else: mask = args[3] global_def.BATCH = True if (options.sym[:1] == "d" or options.sym[:1] == "D"): from development import diskaliD_MPI diskaliD_MPI(args[0], args[1], args[2], mask, options.dp, options.dphi, options.apix, options.function, zstepp, options.fract, rmaxp, rminp, options.CTF, options.maxit, options.sym) else: from applications import diskali_MPI diskali_MPI(args[0], args[1], args[2], mask, options.dp, options.dphi, options.apix, options.function, zstepp, options.fract, rmaxp, rminp, options.CTF, options.maxit, options.sym) global_def.BATCH = False if options.symsearch: if len(options.symdoc) < 1: if options.dp < 0 or options.dphi < 0: print( "Enter helical symmetry parameters either using --symdoc or --dp and --dphi" ) sys.exit() if options.dp < 0 or options.dphi < 0: # read helical symmetry parameters from symdoc from utilities import read_text_row hparams = read_text_row(options.symdoc) dp = hparams[0][0] dphi = hparams[0][1] else: dp = options.dp dphi = options.dphi from applications import symsearch_MPI if len(args) < 3: mask = None else: mask = args[2] global_def.BATCH = True symsearch_MPI(args[0], args[1], mask, dp, options.ndp, options.dp_step, dphi, options.ndphi, options.dphi_step, rminp, rmaxp, options.fract, options.sym, options.function, options.datasym, options.apix, options.debug) global_def.BATCH = False elif len(options.gendisk) > 0: from applications import gendisks_MPI global_def.BATCH = True if len(args) == 1: mask3d = None else: mask3d = args[1] if options.dp < 0: print( "Helical symmetry paramter rise --dp must be explictly set!" ) sys.exit() gendisks_MPI(args[0], mask3d, options.ref_nx, options.apix, options.dp, options.dphi, options.fract, rmaxp, rminp, options.CTF, options.function, options.sym, options.gendisk, options.maxerror, options.new_pixel_size, options.match_pixel_rise) global_def.BATCH = False if options.MPI: from mpi import mpi_finalize mpi_finalize()
def dovolume(ref_data): from utilities import print_msg, read_text_row from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold # Prepare the reference in 3D alignment, this function corresponds to what do_volume does. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 fl = ref_data[2].cmp("dot", ref_data[2], { "negative": 0, "mask": ref_data[0] }) print_msg("do_volume user function Step = %5d GOAL = %10.3e\n" % (ref_ali2d_counter, fl)) stat = Util.infomask(ref_data[2], ref_data[0], False) vol = ref_data[2] - stat[0] Util.mul_scalar(vol, 1.0 / stat[1]) vol = threshold(vol) #Util.mul_img(vol, ref_data[0]) try: aa = read_text_row("flaa.txt")[0] fl = aa[0] aa = aa[1] except: fl = 0.4 aa = 0.2 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) from utilities import read_text_file from fundamentals import rops_table, fftip, fft from filter import filt_table, filt_btwl fftip(vol) try: rt = read_text_file("pwreference.txt") ro = rops_table(vol) # Here unless I am mistaken it is enough to take the beginning of the reference pw. for i in xrange(1, len(ro)): ro[i] = (rt[i] / ro[i])**0.5 vol = fft(filt_table(filt_tanl(vol, fl, aa), ro)) msg = "Power spectrum adjusted\n" print_msg(msg) except: vol = fft(filt_tanl(vol, fl, aa)) stat = Util.infomask(vol, ref_data[0], False) vol -= stat[0] Util.mul_scalar(vol, 1.0 / stat[1]) vol = threshold(vol) vol = filt_btwl(vol, 0.38, 0.5) Util.mul_img(vol, ref_data[0]) if ref_data[1] == 1: cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n" % ( cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) else: cs = [0.0] * 3 return vol, cs