; Computes Initial Reconstruction
;
; SOURCE: spider/docs/techs/recon/newprogs/recon.spi 
;
; PURPOSE: Computes initial reconstruction using multiple defocus groups.
;          For each defocus group in the reconstruction:
;             Divides particle data into two sets, odd vs even-numbered particles.
;             Computes odd and even sample 3D reconstructions.
;             Computes Fourier Shell Correlation for each group reconstruction.
;             Computes CTF correction file for each group reconstruction.
;             Apply CTF correction to the defocus group odd/even sample volumes
;          For overall reconstruction:
;          Applies  CTF correction to the defocus group odd/even sample volumes.
;          Combines defocus group odd/even sample volumes.
;          Computes resolution of the combined reconstruction at 0.5 threshold.
;
; I/O PARAMETERS AND FILES ARE SET HERE:
;
; --------------------- Parameters -----------------------------------

[rad]        = -1        ; Radius of restored object (Use: -1 for 95% winsize / 2)
[bplambda]   = 0.2e-5    ; Back projection weighting lambda 
[climit]     = 0.0       ; Back projection correction limit
[iters]      = 60        ; Back projection iteration limit
[snr]        = 3         ; SNR for CTF correction using 'TF CTS'

[do_bp]      = 1         ; Skip group back projections    (if = zero)
[do_combine] = 1         ; Skip computing combined volume (if = zero)

; -------------------- Input files -----------------------------------

[params]     = '../params'                             ; Reconstruction parameter file

[sel_grp]    = 'sel_group_cclim'                       ; Defocus group selection file
[sel_part]   = 'sel_particles_{***[grp]}'              ; Particle selection files (one / defocus group)

[ali]        = '../Alignment/dala01_{***[grp]}@******' ; Aligned particle images (one stack / defocus group)
[angles]     = '../Alignment/align_01_{***[grp]}'      ; Alignment parameter doc files (one / defocus group)

; -------------------------- Output files ---------------------------

[selodd]     = 'df{***[grp]}/sel_odd'         ; Selection files for odd-numbered particles (one / defocus group)
[seleven]    = 'df{***[grp]}/sel_even'        ; Selection files for even-numbered particles (one / defocus group)

[volodd]     = 'vol01_odd'                    ; Volume created from "odd" particles
[voleven]    = 'vol01_even'                   ; Volume created from "even" particles

[grpvolodd]  = 'df{***[grp]}/[volodd]'        ; Volumes for even-numbered particles (one per group)
[grpvoleven] = 'df{***[grp]}/[voleven]'       ; Volumes for odd-numbered particles (one per group)
[grpfsc]     = 'df{***[grp]}/fscdoc'          ; FSC doc files (one per group)
[ctf]        = 'df***/ctffile'                ; CTF correction files (one per group)

[vol]        = 'vol01'                        ; CTF-corrected combined volume  
[combfsc]    = 'combires'                     ; FSC doc file with FSC curve for combined volume
[res_file]   = 'resolution'                   ; Resolution doc file for combined volume

[tmpfsc]     = 'jnktmpfsc'                    ; Temp. FSC file (deleted)

; -------------- END BATCH HEADER -------------------------------------------


MD
TR OFF                            ; Decrease results file output
MD
VB OFF                            ; Decrease results file output
MD 
SET MP 
0                                 ; 0 = use all available processors  

; Get parameters from reconstruction parameter file -----------------------------
UD 7,[sp_sph_abb]                 ; Spherical Abberation
[params]
UD 8,[sp_sourcesiz]               ; Source size
[params]
UD 9,[sp_def_spr]                 ; Defocus spread
[params]
UD 12,[sp_acr]                    ; ACR
[params]
UD 13,[sp_geh]                    ; Gaussian envelope
[params]
UD 14,[sp_lambda]                 ; Lambda
[params]
UD 15,[sp_maxspfreq]              ; Max. spatial frequency
[params]
UD 17,[sp_winsiz]                 ; Window size
[params]

IF ([rad] .EQ. -1) THEN           ; Check reconstruction radius
   [rad] = INT( (0.95*[sp_winsiz])/2.0 )
ENDIF

; Make header for  resolution doc file
SD /    GROUP,  NORM'D FREQ,  RESOLUTION (ANG.),  (CUTOFF=50%) 
[res_file]

IF ([do_bp].LE.0) GOTO LB98       ; Can skip group back-projections

[grploop]    = 1                  ; Set flag for looping over groups

; ----------------------  Extract PubSub group number (When given) ------------------
IF ([grp] .GT. 0) THEN            ; Group # sent on invokation line
   UD FIND [key],[grp],[p],[def]  ; Find defocus value for given group   
   [sel_grp]                      ; Group selection file       (input)
   1,[grp]                        ; Search col. & value

   UD FIND E                      ; End doc file use   
   [sel_grp]                      ; Group selection file       (ends)
   [grploop]    = 0               ; Set flag for NOT looping over groups
ELSEIF ([grp] .LT. 0) THEN        ; Negative group # sent on invokation line
    GOTO LB98                     ; Skip computing group back-projections 
ENDIF

VM
echo  ' 'Commencing back projection, please wait.     
VM
echo  ' '    
MY FL

DO   ; -----------------------------  Loop over all defocus groups -----------------------

   IF ([grploop] .GT. 0) THEN        ; Loop over defocus groups 
      UD NEXT [key],[grp],[p],[def]  ; Get group from group sel. file
      [sel_grp]                      ; Group selection file       (input)
      IF ([key] .LE. 0) EXIT         ; End of groups in doc file
   ENDIF

   VM                                ; Create output dir if needed
   mkdir -p  df{***[grp]}          

   ; Create particle selection doc files for odd and even particle sets -----------------

   DE                             ; Delete any existing output file
   [seleven]
   DE                             ; Delete any existing output file
   [selodd]

   DOC SPLIT                      ; Alternately split doc. file into two 
   [sel_part]                     ; Particle selection file              (input)
   [selodd]                       ; particle selection file              (output)
   [seleven]                      ; Particle selection file              (output)

   MY FL

   ; Compute the odd sampled 3D reconstruction. -----------------------------------
   BP RP  [niter]                 ; Gets number of BP iterations
   [ali]                          ; Template for input image files       (input)
   [selodd]                       ; Selection doc file for odd images    (input)
   [rad]                          ; Radius of restored object
   [angles]                       ; Angles doc file                      (input)
   *                              ; Symmetries doc file (* = none)       (input)
   [grpvolodd]                    ; Reconstructed 3D volume              (output)
   [bplambda],[climit]            ; Correction lambda, limit
   [iters],0                      ; Iteration limit, mode
   (.5,.5)                        ; Minimum, maximum
   (.5)                           ; Smoothing constant

   ; Compute the even sampled 3D reconstruction. ----------------------------------
   BP RP  [niter]                 ; Gets number of BP iterations
   [ali]                          ; Template for input image files       (input)
   [seleven]                      ; Selection doc file for even images   (input)
   [rad]                          ; Radius of restored object
   [angles]                       ; Angles doc file                      (input)
   *                              ; Symmetries doc file (* = none)       (input)
   [grpvoleven]                   ; Reconstructed 3D volume              (output)
   [bplambda],[climit]            ; Correction lambda, limit
   ([iters],0)                    ; Iteration limit, mode
   (.5,.5)                        ; Minimum, maximum
   (.5)                           ; Smoothing constant

   MY FL

   ; Compute  Fourier Shell Correlation for group reconstruction --------------------------------
   DE                             ; Remove any existing FSC file
   [grpfsc]                       ; Doc file

   RF 3 [unused],[spfq]           ; Fourier Shell Correlation
   [grpvolodd]                    ; First input volume              (input)
   [grpvoleven]                   ; Second input volume             (input)
   (0.5)                          ; Ring width
   (0.2,2)                        ; Lower, upper scale factors
   C                              ; C = missing cone
   (90)                           ; Max. tilt angle
   (3)                            ; Factor for noise comparison
   [grpfsc]                       ; FSC doc file                    (output)

   DOC REN                        ; Renumber doc keys (1...)
   [grpfsc]                       ; FSC doc file                    (input)
   [grpfsc]                       ; FSC doc file                    (output)

   ; Add footer in FSC doc file
   SD /     NORM-FREQ         DPH         FSC        FSCCRIT        VOXELS
   [grpfsc]                       ; FSC doc file                    (output)
   SD E
   [grpfsc]

   [resol] = 0.5/([sp_maxspfreq]*[spfq]) ; Resolution = pixel size / spatial freq.
   VM
   echo  ' 'Group: {****[grp]}'  'Defocus: {%f7.0%[def]}'  'Particles: {******[p]}'  'Resolution: {%f7.2%[resol]}    
   MY FL

   ; Save resolution in doc file ----------------------------------------------------
   SD [grp], [grp],[spfq],[resol]
   [res_file]

   MY FL

   TF C3                         ; Create CTF correction file ---------------------
   [ctf][grp]                    ; Output file                  (output)
   [sp_sph_abb]                  ; Cs
   [def],[sp_lambda]             ; Defocus, lambda
   [sp_winsiz]                   ; Dimension of volume
   [sp_maxspfreq]                ; Max. spatial freq.
   [sp_sourcesiz],[sp_def_spr]   ; Source size, defocus spread
   (0,0)                         ; Astigmatism, azimuth
   [sp_acr],[sp_geh]             ; Amplitude contrast ratio, Gaussian halfwidth
   (-1)                          ; Sign

   IF ([grploop] .LE. 0) GOTO LB99 ; For handling single defocus group only

ENDDO                            ; End of defocus group loop ----------------------

UD NEXT END                      ; Finished  with group selection file
[sel_grp]                        ; Group selection file         

VM                                      
echo ' '

LB98                             ; Entry point after skipping back projections
IF ([do_combine].LE.0) GOTO LB99  ; Can skip combination & resolution 

; Apply CTF correction to all defocus group volumes  ------------------------------
TF CTS                          ; CTF correction 
df***/[voleven]                 ; Template for image file       (input)
[sel_grp]                       ; Group selection file          (input)
[ctf]                           ; Template for ctf file         (input)
[snr]                           ; SNR
_1                              ; Temp. inline file             (output)

TF CTS
df***/[volodd]                  ; Template for image file       (input)
[sel_grp]                       ; Group selection file          (input)
[ctf]                           ; Template for ctf file         (input)
[snr]                           ; SNR
_2                              ; Temp. inline file             (output)

; Add  odd & even volumes to get combined volume   --------------------------------
AD                              ; Add  volumes
_1                              ; Volume                        (input)
_2                              ; Volume                        (input)
[vol]                           ; Volume                        (output) 
*                               ; Finished adding

DE                              ; Delete existing FSC file
[tmpfsc]

RF 3 [unused],[spfq]            ; Compute combined FSC resolution curve -----------
_1                              ; First sample volume           (input)
_2                              ; Second sample volume          (input)
0.5                             ; Ring width 
0.2, 2.0                        ; Lower, upper scale factors
C                               ; C = missing cone    
90.0                            ; Max. tilt angle
3                               ; Factor for noise comparison
[tmpfsc]                        ; FSC  doc file                (output)

; Compute reconstruction resolution -----------------------------------------------

[resol] = 0.5/([sp_maxspfreq]*[spfq])  ; Resolution = pixel size / spatial freq.

VM                                      
echo ' 'Reconstruction: [vol].$DATEXT'   'Resolution: {%f5.2%[resol]} Angstroms 
VM                                      
echo ' '

[key] = [grp] + 1
[grp] = 0
SD /    OVERALL,  NORM'D FREQ,  RESOLUTION (ANG.),  (CUTOFF=50%) 
[res_file]                      ; Resolution doc file        (output)

SD [key],[grp][spfq],[resol]    ; Put resolution in file
[res_file]                      ; Resolution doc file        (output)

; Reformat FSC curve file (removes first line?) -----------------------------------

DOC REN                         ; Renumber the doc file
[tmpfsc]                        ; Doc file                     (input)
[tmpfsc]                        ; Doc file                    (output)

UD N [nline]                    ; Number of lines in temp doc file
[tmpfsc]                        ; Doc file                    (input)

DE                              ; Delete existing FSC file 
[combfsc]                       ; Doc file                    (input)

SD /    NORM'D FREQ    DPR        FSC        FSCCRIT     VOXELS
[combfsc]                       ; Doc file                   (output)

DO  [line] = 1,[nline]
   UD [line], [v62],[v63],[v64],[v65],[v66]
   [tmpfsc]                     ; Doc file                   (input)
   SD [line], [v62],[v63],[v64],[v65],[v66]
   [combfsc]                    ; Doc file                   (output)
ENDDO

DE                              ; Delete temp. file
[tmpfsc]

LB99
EN 

;