#! /bin/csh -f
# Do self-alignment of microtubule model
#  $Id: selfalign,v 391b6e79e7d1 2008/01/04 07:03:38 mast $
#  Log at end of file

set sec = "/"
set didsec = "0"
set order = "2"
set local = "7"
set transform = "0"
set imfile = ""

if ($#argv == 0) then
cat <<EOF
Usage: selfalign [options] root_name
           OR
       selfalign [options] input_model output_model
options:
  -s[ecs] list       sections to find alignment for (default is whole model)
  -l[ocal] #         number of sections over which to fit (default $local)
  -o[rder] #         order of polynomial fit (default $order)
  -t[rans] #         transform type (default $transform): 0 linear, 2 translation only,
                                     3 rotation/translation,
                                     4 rotation/translation/magnification
  -i[mage] filename  Name of image file that the model was built on 
                     (default root_name.ali)
EOF
exit 0
endif

set tmpdir = /usr/tmp
if ($?IMOD_DIR) then
    setenv PATH "$IMOD_DIR/bin:$PATH"
    if (-e "$IMOD_DIR/bin/settmpdir") source "$IMOD_DIR/bin/settmpdir"
endif

set opts = 1
while ($opts == 1)
  switch ($argv[1])
    case -s*:
      set sec = $argv[2]
      set didsec = "1"
      shift; shift
      breaksw
    case -o*:
      set order = $argv[2]
      shift; shift
      breaksw
    case -l*:
      set local = $argv[2]
      shift; shift
      breaksw
    case -t*:
      set transform = $argv[2]
      shift; shift
      breaksw
    case -i*:
      set imfile = $argv[2]
      shift; shift
      breaksw

    default:
      set opts = 0
      breaksw
  endsw
end

if ($#argv < 1) then
    echo 'selfalign error: Too few arguments'
    exit 1
endif

if ($#argv > 2) then
      echo "selfalign error: too many arguments or bad argument $argv[1]"
      exit 1
endif

if ($#argv == 1) then
    set rootname = $argv[1]
    set infile = $rootname.mod
    set selfmod = $rootname.selfmod
else
    set infile = $argv[1]
    set selfmod = $argv[2]
    set rootname = $infile:r
endif

if (! -e $infile) then
    echo "selfalign error: $infile does not exist"
    exit 1
endif

onintr clean

set selfxf = $rootname".selfxf"
set selfxg = $rootname".selfxg"
set tmpxf = "$tmpdir/tmpxf.$$"
set unitxf = "$tmpdir/unitxf.$$"
set testxf = "$tmpdir/testxf.$$"
set startxf = "$tmpdir/startxf.$$"
set endxf = "$tmpdir/endxf.$$"

if ($imfile == "") set imfile = $rootname".ali"

if (-e $imfile) then
    set xfin1 = $imfile
    set xfin2 = ""
    @ numsec =  `header -si $imfile | sed '/[[:cntrl:]]/s///g' | awk '{print $3}'`
else
    set modmaxes = `imodinfo -a $infile | sed -n -e '/[[:cntrl:]]/s///g' -e '/^max /p'`
    @ xmax = $modmaxes[2]
    @ ymax = $modmaxes[3]
    @ xcen = $xmax / 2
    @ ycen = $ymax / 2
    @ numsec = $modmaxes[4]
    set xfin1 = ""
    set xfin2 = "$xcen $ycen /"
    echo "$imfile does not exist; assuming dimensions in model header will work"
endif

xfmodel <<EOF
$xfin1
$xfin2
$infile
$transform
0

$tmpxf
$sec
0
EOF

# search for first and last non-unit transforms
@ numlines = `cat "$tmpxf" | wc -l`

head -n 1 "$tmpxf" > "$unitxf"
@ startnum = 2
set gotstart = "0"

while ($gotstart == "0" && $startnum < $numlines)
    head -n $startnum "$tmpxf" | tail -n 1 >! "$startxf"
    cmp -s "$startxf" "$unitxf"
    if ($status) then
	set gotstart = "1"
    else
        @ startnum++
    endif
end

@ endnum = 1
set gotend = "0"

while ($gotend == "0" && $endnum < $numlines)
    tail -n $endnum "$tmpxf" | head -n 1 >! "$endxf"
    cmp -s "$endxf" "$unitxf"
    if ($status) then
	set gotend = "1"
    else
        @ endnum++
    endif
end

@ startnum--
@ endnum--
@ headnum = $numlines - $endnum
@ tailnum = $headnum - $startnum

if (-e $selfxf) \mv $selfxf $selfxf~
\cp "$unitxf" $selfxf
while ($startnum > 1)
    cat "$startxf" >> $selfxf
    @ startnum--
end
head -n $headnum "$tmpxf" | tail -n $tailnum >> $selfxf
while ($endnum > 0)
    cat "$endxf" >> $selfxf
    @ endnum--
end

while ($numsec > $numlines)
    cat "$endxf" >>$selfxf
    @ numlines++
end

xftoxg <<EOF
$local
$order
$selfxf
$selfxg
EOF

if (-e $selfmod) \mv $selfmod $selfmod~
imodtrans $infile $selfmod -2 $selfxg

clean:
\rm -f "$tmpxf" "$unitxf" "$startxf" "$endxf"

#
#  $Log$
