;
; ; PURPOSE: Restacks particles listed in a set of particle selection files ; into specified number of new stacks. Can apply CTF correction ; filter to files while restacking. ; ; SOURCE: spider/docs/techs/recon1/Procs/restack-n-ctf.spi ; ; USAGE: spider spi/dat @restack-n-ctf ; ------------------- Parameters ------------------- [newnumgrps] = 12 ; Number of new groups (e.g., number of nodes) [want-stats-only-yn] = 0 ; Want statistics only and no stacks? (1 == no stacks) [apply-ctf-yn] = 1 ; Apply ctf correction? (0 == no) ; (Used to restack images that have not been corrected) [want-align-yn] = 0 ; Want to copy alignment files also? (0 == no) ; (Used to duplicate an existing alignment file set) [want-global-yn] = 1 ; Want global lookup table? (0 == no) ; ------------------ Input files ----------------------- [old_sel_grp] = '../sel_micrograph' ; Group (or micrograph) selection file (one) ![old_sel_part] = '../Particles/win/sel_part_****' ; Particle selection files (one/micrograph) [old_sel_part] = '../Particles/win/sel_good_****' ; Particle selection files (one/micrograph) [old_stack] = '../Particles/win/data_bymic_****' ; Existing particle stacks (one/micrograph) [ctf_cor] = '../Power_Spectra/power/flipctf_****' ; CTF correction file (one/micrograph) (OPTIONAL) ![old_align] = 'input/align_01_***' ; Alignment parameter files (one/group) (OPTIONAL) [old_align] = '*' ; No Alignment parameter files (OPTIONAL) ; ----------------- Output files ----------------------- [win_dir] = '../win_0' ; New windowed particles directory (one) [new_sel_grp] = '[win_dir]/sel_group' ; New group selection file (one) [new_sel_part] = '[win_dir]/sel_part_***' ; New particle selection files (one/group) [new_stack] = '[win_dir]/data_***' ; New particle stacks (one/group) [part2global] = '[win_dir]/part2glonum_***' ; Global particle lookup table (one/group) (OPTIONAL) [global2grp_lut] = '[win_dir]/global2group' ; Combined global lookup table (one) (OPTIONAL) [new_align] = '[win_dir]/align_01_***' ; Alignment parameter doc files (one/group) (OPTIONAL) ; -------------- END BATCH HEADER ------------------------------- MD ; Skip unnecessary output VB OFF MD ; Skip unnecessary output TR OFF SYS ; Create output directory if needed mkdir -p [win_dir] UD N [old_num_grps] ; Find number of old groups (usually micrographs) [old_sel_grp] ; Old group (micrograph) selection file (input) [ntot] = 0 ; Initialize overall cumulative total DO ; Loop over all old groups (micrographs) --------------- UD NEXT [key], [ingrp] ; Get old group (micrograph) number [old_sel_grp] ; Old group (micrograph) selection file (input) IF ( [key] <= 0 ) EXIT ; Accumulate total number of particles UD N [numparts] [old_sel_part][ingrp] ; Old particle selection file (input) SYS echo " Group: {%I4%[ingrp]} has: {%I6%[numparts]} particles." ; Count number of selected particles [ntot] = [ntot] + [numparts] ENDDO ; End loop over micrographs ----------------------------- UD NEXT E ; Finished with doc file [old_sel_grp] ; Finished with doc file (input) [newnpg] = [ntot]/[newnumgrps] ; Number of particles in new groups [newnpg1] = INT([newnpg]) ; Number of particles in new groups IF ([newnpg] > [newnpg1])[newnpg]=INT([newnpg]+1) ; Number of particles in new groups SYS echo ; echo " Restacking: {%I6%[ntot]} particles" SYS echo " From: {%I4%[old_num_grps]} groups into: {%I3%[newnumgrps]} groups, with {%I5%[newnpg]} particles each." ; echo IF ( [want-stats-only-yn] == 1 ) THEN EN ENDIF DE ; Remove any new group selection file [new_sel_grp] ; New group selection file (removed) SD / GROUP NUM_PARTS [new_sel_grp] ; Group selection doc file (output) [one] = 1 DE A [new_sel_part][one] DE A [new_stack][one] DE A [part2global][one] IF ( [want-global-yn] == 1 ) THEN DE [global2grp_lut] SD / APSHSLICE MIC_SLICE APSHGROUP MIC_NUM [global2grp_lut] ENDIF ; Compute number of particles for each new group [newnpg] = [ntot]/[newnumgrps] ; Number of particles in new groups [newnpg1] = INT([newnpg]) ; Number of particles in new groups IF ([newnpg] > [newnpg1])[newnpg]=INT([newnpg]+1) ; Number of particles in new groups [outgrp] = 0 ; Initialize output group number [outpart] = [newnpg] + 1 ; To force new output group [needold] = 1 ; To force new input group [part_counter] = 0 ; Initialize overall counter DO ; Loop over all old particles ---------------- IF ( [needold] >= 1 ) THEN ; Start another input group (micrograph) IF ( [want-align-yn] > 0) THEN UD ICE ; Close doc file [old_align][ingrp] ; Old alignment doc file (closed) ENDIF UD NEXT [key], [ingrp] ; Get next group (micrograph) number [old_sel_grp] ; Group (micrograph) selection file (input) IF ([key] <= 0) EXIT ; Finished with all old groups [needold] = 0 ; Flag to use old group ENDIF [calcgrpnum] = int([part_counter]*[newnumgrps]/[ntot])+1 ; The group number we *should* be on ; IF ( [outpart] > [newnpg] ) THEN ; (OUTPART is incremented after this step, so OUTPART+1 would get included) IF ( [calcgrpnum] > [outgrp] ) THEN [part_counter] ; DIAGNOSTIC outpart newgroup ; Must start another output group IF ( [outgrp] > 0) THEN ; Not first output group, give details on last output group SYS echo " Filled group: {%I3%[outgrp]} with: {%I6%[outpart]} particles." SD E ; Close doc file [new_sel_part][outgrp] ; Finished with this particle selection file (closed) SD E ; Close doc file [new_align][outgrp] ; Finished with this alignment file (closed) SD E ; Close doc file [part2global][outgrp] ; Global number doc file (closed) SD [outgrp],[outgrp],[outpart] ; Place group # in group selection file [new_sel_grp] ; Group selection file (output) ENDIF [outgrp] = [outgrp] + 1 ; New output group # IF ( [outgrp] <= [newnumgrps] ) THEN ; Delete pre-existing files and label new doc files DE ; Remove existing particle selection file [new_sel_part][outgrp] ; Particle selection file (removed) SD / # ORIG_MICRO MIC_WIN_# GLOBAL_# GRP [new_sel_part][outgrp] ; Particle selection file (output) DE ; Remove existing output stack [new_stack][outgrp] ; Particle stack file (removed) IF ( [want-global-yn] > 0) THEN DE ; Remove existing global lookup file [part2global][outgrp] ; Existing global number lookup file (removed) SD / KEY: WIN-NUM REG: GLOBAL-NUM MICROGRAPH; Label for new global number doc file [part2global][outgrp] ; New global number doc file (output) ENDIF IF ( [want-align-yn] > 0) THEN DE ; Remove existing alignment file [new_align][outgrp] ; Existing global alignment file (removed) SD / KEY PSI, THE, PHI, REF#, EXP#, CUM.{ROT, SX, SY}, NPROJ, DIFF, CCROT, ROT, SX, SY, MIR-CC [new_align][outgrp] ; New global number doc file (output) ENDIF ENDIF [outpart] = 0 ; Reset output particle # to zero ENDIF ; For starting new group UD NEXT [key], [inpart],[d2],[mic],[glonum] ; Get next input particle number [old_sel_part][ingrp] ; Old particle selection file (input) IF ( [key] <= 0) THEN [needold] = 1 ; Flag for need to start next input group UD NEXT E ; (Goes into infinite loop without this) [old_sel_part][ingrp] CYCLE ; Finished with this input group ENDIF ; Have old and new particle numbers now [outpart] = [outpart] + 1 ; New output particle # [part_counter] = [part_counter] + 1 IF ( [apply-ctf-yn] > 0) THEN ; Apply CTF correction to image and restack TF COR [old_stack][ingrp]@******[inpart] ; Existing particle stack (input) [ctf_cor][mic] ; CTF correction file (input) [new_stack][outgrp]@******[outpart] ; CTF corrected particle stack (output) ELSE ; No CTF correction, just restacking CP [old_stack][ingrp]@******[inpart] ; Existing particle stack (input) [new_stack][outgrp]@******[outpart] ; New particle stack (output) ENDIF ; FI H [mic],[glonum] ; Query image header values ; [old_stack][ingrp]@******[inpart] ; Existing particle stack (input) ; MIC,NUM ; ; FI H [mic] ; Query image header values ; [old_stack][ingrp]@******[inpart] ; Existing particle stack (input) ; MIC SD [outpart], [outpart],[mic],[inpart],[glonum],[outgrp] ; Save new particle data [new_sel_part][outgrp] ; Particle selection file (output) IF ( [want-global-yn] > 0) THEN SD [outpart],[glonum],[mic] ; Save lookup table [part2global][outgrp] ; New global number doc file (output) SD [glonum], [outpart],[inpart],[outgrp],[mic] [global2grp_lut] ENDIF IF ( [want-align-yn] > 0) THEN ; Get image dimension UD IC [inpart], [r1],[r2],[r3],[r4],[r5],[r6],[r7],[r8],[r9],[r10],[r11],[r12],[r13],[r14],[r15] [old_align][ingrp] ; Old alignment doc file (input) SD [outpart],[r1],[r2],[r3],[r4],[outpart],[r6],[r7],[r8],[r9],[r10],[r11],[r12],[r13],[r14],[r15] [new_align][outgrp] ; New alignment doc file (output) ENDIF ENDDO ; End loop over all old particles ---------------- IF ( [outpart] > 0) THEN SYS echo " Filled group: {%I3%[outgrp]} with: {%I6%[outpart]} particles." SD E ; Close doc file [new_sel_part][outgrp] ; Output particle selection file (closed) ENDIF UD ICE ; Close doc file [old_align][ingrp] ; Old alignment doc file (closed) ; UD NEXT E ; Close doc file ; [new_sel_grp] ; Group selection doc file (closed) UD NEXT E ; Close doc file [old_sel_grp] ; Group selection doc file (closed) [dummy] = -[newnumgrps] SD / PARTSBEFORE PARTS_AFTER [new_sel_grp] SD [dummy], [ntot],[part_counter] [new_sel_grp] SD E [new_sel_grp] IF ( [want-global-yn] == 1 ) THEN SD E [global2grp_lut] ENDIF SYS echo EN ;; Modified 2015-11-18 ; 2015-11-18 (agl) -- Use 'TF COR' not 'FD'!! ; 2014-08-13 (trs) -- Added combined global lookup ; 2013-11-29 (trs) -- Bug fix: went into infinite (?) loop if in-core files weren't closed ; 2013-11-28 (trs) -- Bug fix: now gets micrograph# and global particle number from OLD_SEL_PART instead of stack ; 2013-10-16 (agl) -- Modernized ; 2012-07-19 (trs) -- Creates output directory if needed ; 2012-05-01 (trs) -- Optionally writes global particle number to header position 42 ; 2012-04-05 (trs) -- Doesn't use output from pnums.spi anymore ; 2012-04-05 (trs) -- Adapted from listallparticles.spi ; 2010-09-02 (trs & ay) -- Trap for >999 micrographs for 'SD [dummy]' line ; 2009-10-16 (trs) -- Removed option for unstacked images ; 2008-10-03 (trs) -- Added output file part2global ; 2008-10-03 (trs) -- Assigns global particle number ; 2007-12-17 (trs) -- Optional parameter for maximum number of particles per micrograph ; 2007-10-10 (trs) -- Gets number of micrographs from sel_micrograph ;