; Pick particles from micrographs
;
; PURPOSE: Uses fast local correlation algorithm to pick particles from 
;          input micrographs.  Requires a reference volume.  
;
; SOURCE:  lfc_pick.spi    
;
; CALLS:   pickparticle.spi   convert_p.spi

[part] = 1  ; The first particle number (= 1 or next particle number) 

; [decimate] = decimation factor for 'DC S' operation
;    0 = get value from param file
;    1 = full sized image
;    2 = 1/2 size
;    4 = 1/4 size
[decimate] = 0  ; Decimation factor (0 = get value from  param file)

; Peak separation distance (e.g., 75% of window size) may be changed such 
;    that [sep]*[windiam] ~ size of particle, where [windiam]= side of the window   
[sep] = 0.75    ; Separation distance (% of window size)
[sep] = 0.62    ; Separation distance (% of window size) al feb 2007

; ----------- Input files --------------
FR G
[params]../params                    ; Parameter file 

FR G
[sel_mic]../sel_micrograph           ; Micrograph file numbers

FR G
[micgr]../Micrographs/mic{****[mic]} ; Micrograph images

FR G
[refvol]../reference_volume          ; Reference volume

FR G
[noise]noise                         ; Noise file (should be in current dir)

; ----------- Output files --------------
FR G
[ser]win/winser_{****[mic]}@          ; Template for particle image stacks

FR G
[sel_doc]win/sel_particle_{****[mic]} ; Template for particle image stacks

FR G
[sndc]coords/sndc{****[mic]}          ; Template for doc file with coordinates

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

MD                      ; Use all available processors
SET MP
0 

MD                      ; Skip unnecessary output 
VB OFF
MD                      ; Skip unnecessary output 
TR OFF
MY FL
                                                               
FR G
[out]out{****[mic]}     ; Temporary spider file for converting from other formats

UD 5, [pixelsize]       ; Get pixelsize 
[params]

; Get window size & actual size from parameter file. If zero, compute them
UD 17, [windiam]        ; Get window diameter
[params]

; For ribosomes, actual size = 250 A, window = 368 A
IF ([windiam].LT.1)  [windiam]  = INT(368/[pixelsize])

; Compute peak separation distance ([sep] is x% of window size)
[peaksep] = [sep] * [windiam]
[peaksep]               ; Echo to results file

MY FL
; Loop over all micrographs ----------------------------------------------------
DO  
   UD NEXT [key],[mic]  ; Get micrograph number
   [sel_mic]
   IF ([key].LE. 0) EXIT

   @convert_p([mic],[decimate])
   [params]           ; Parameter file
   [micgr]            ; Input micrograph
   [out]              ; Template for output SPIDER file

   DE
   [sndc]             ; Delete any old output document files

   @pickparticle
   [out]              ; Input micrograph 
   [refvol]           ; Reference volume                 (input)
   [noise]            ; Noise image used for normalization of windowed particles
   [ser]              ; Template for windowed particles    (output)
   (1)                ; Starting particle number for windowed particles
   [sndc]             ; Document file with particle co-ordinate   (output)
   0                  ; Selection file is NOT used
   0                  ; PHI start value
   0                  ; PHI end value
   0                  ; PHI step size
   0                  ; THETA start value
   0                  ; THETA end value
   0                  ; THETA step size
   0                  ; PSI start value
   0                  ; PSI end value
   0                  ; PSI step size
   2                  ; Interpolation factor
   20000              ; No of peaks to be searched (arbitrary)
   [peaksep]          ; Peak separation distance
   1                  ; Use symmetric mask


   UD N  [n]          ; Get number of particles in this micrograph
   [sndc]

   [part]=[part]+[n]  ; Increment the particle counter

   DE                 ; Delete the temporary SPIDER file
   [out]

   VM
   echo ' 'Picked:  {*****[n]} images from micrograph: {*****[mic]}

   DOC CREATE         ; Make selection doc file
   [sel_doc]          ; Doc file                         (output)
   (1)                ; First register
   (1-[n])            ; Image numbers

ENDDO

VM
echo ' 'Total picked particles:  {*******[part]}
VM
echo ' '

EN D
;