3 Align the Stacks at Background

The capturing Linux box is a relatively powerful computer. Except capturing job, there is no other things running. Therefore, I thought it would be very efficient if we can align the stacks at the background as soon as the stack file is mature. If the alignment is good, tomography users can simply get the aligned output. You can always get the raw stack files in case you want to realign or do whatever you like to do with them.

Currently, I tried to use IMOD aligning programs. Other program might also work well here. Following Greg's idea to copy files over to storage computer, I run a cron jon every 20 seconds to find the captured Falcon frames and run a bash shell script on it. Here is an example how I run it.

Example 1. find-n-align.sh, to be run every 30 seconds from system cron.


#!/bin/sh

# First avoid running the script if it is already running

lockfile=".align_running"

if [ -d /tmp/$lockfile ]; then
  echo Lockfile exited, exit...
    exit
fi

mkdir /tmp/$lockfile
if [ $? -ne 0 ]; then
   echo "Failed to make temp directory"
   exit
fi

source=/mnt/FreeNAS/Falcon_Frames
## assume destincation is mounted. 
#destination=/mnt/FreeNAS/Falcon_Frames

cd -- "$source" &&
find . -type f -name "*.mrcs" -mmin +10 | \
    while read file; do sh /root/com/mk-pcm-align-single.sh $file; done

if [ -d /tmp/$lockfile ]; then
  rmdir /tmp/$lockfile 
fi
        

Here is the content of mk-pcm-align-single.sh. I first check if the file has been processed already. If not, I genarated a IMOD python command script (.pcm) and run it.

Example 2. mk-pcm-align-single.sh


#!/bin/sh
# sh script to generate IMOD command file *.pcm
usage(){
  echo
  echo    Usage: sh $0 "stack.mrc"
  echo  
}

if [ $# == 0 ]; then usage; exit 0; fi

# if processed, i.e. have .mrcs_aligned.mrc, don't do
ext_processed="mrcs_aligned.mrc"

file="$1"
dirname=$(dirname "$file")
filename=$(basename "$file")
extension="${filename##*.}"
basename="${filename%.*}"

if [ -f $dirname/$basename.$ext_processed ]; then 
	echo $dirname/$basename.mrcs is already processed, exit ...
	exit 1
else
	mrcf=$dirname/$basename.mrcs
fi 

#grep Path ${mdoc} | cut -f3 -d'\' | sed s'/mrc/mrc",/' | sed s'/^/\>"/' > tmp
#echo ${file}
echo ${mrcf}

pcmf="${mrcf}.pcm"
if [ -f ${pcmf} ]; then rm -f ${pcmf}; fi

iter="1"
filter="6"
corrbin="6"
radius2="0.30"
sigma2="0.02"
scaling="5000"
countsPerElec="19.44"
shrink="1"

echo '>files = [' >> ${pcmf}
echo \>\"${mrcf}\", >> ${pcmf}
echo \>']' >> ${pcmf}
cat << EOF >> ${pcmf}
>iter = ${iter}
>filter = ${filter}
>corrbin = ${corrbin}
>radius2 = ${radius2}
>sigma2 = ${sigma2}
>scaling = ${scaling}
>countsPerElec = ${countsPerElec}
>shrink = ${shrink}
>avgstr = ''
>for mrcf in files:
>  avgfile = mrcf + '.avg'
>  xffile = mrcf + '.xf'
>  xgfile = mrcf + '.xg'
>  tmpfile = mrcf + '.tmp'
>  truncfile = mrcf + '.trnc'
>  avgstr += ' ' + avgfile
$  tiltxcorr -Stan
InputFile %mrcf
OutputFile %xffile
RotationAngle 0
FirstTiltAngle 0
TiltIncrement 0
FilterRadius2 %radius2
FilterSigma1 0.01
FilterSigma2 %sigma2
ShiftLimitsXandY 30,30
CumulativeCorrelation
BinningToApply %corrbin
Iterate %iter
ReverseOrder
#AntialiasFilter %filter

$  xftoxg -n 0 %xffile %xgfile
#$  newstack -bin %shrink -mul %scaling,0 -anti 6 -mode 1 -xf %xgfile %mrcf tmp.ali
$  newstack -bin %shrink -anti 6 -mode 1 -xf %xgfile %mrcf %tmpfile
$  avgstack
%tmpfile
%avgfile
/
#$  newstack -mode 1 -mul %scaling,0 %avgstr ${mrcf}_aligned.st
$  newstack -mode 1 %avgstr ${mrcf}_aligned.mrc
EOF


if [ -f ${pcmf} ]; then 
  echo  ${pcmf} has been created...
  echo  % submfg ${pcmf}
  #subm ${pcmf}
  /usr/local/IMOD/bin/submfg ${pcmf}
fi

# clean up
rm -f *.avg *.avg~
rm -f *.tmp *.tmp~
rm -f *.xf *.xf~ *.xg *.xg~