#!/bin/csh

# Copyright (C) 2005 Andrew E Firth, University of Otago, Dunedin, 
# New Zealand, aef(at)sanger.otago.ac.nz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

#Redraws sixframe plot.

limit stacksize unlimited

@ argc = `echo $argv | awk '{print NF}'`
if ($argc != 7) then
  echo "Usage: redosixframe newdir base1 base2 grids2 count newurl threshold<br>"
  exit 1
endif

set newdir = $1
cd $newdir
source mlogd.param
source sixframe.params

if (! $download) then
  setenv PATH ${PATH}:/home/aef/bin
else
  setenv PATH ${PATH}:${HOME}/bin
endif

set base1 = $2
set base2 = $3
set grids = $4
set count = $5
set newurl = $6
set threshold = $7

if (1 == $grids) then
  set tick = ""
else
  set tick = "#"
endif

#Apply threshold and scale scores.
set threshold = `echo $threshold | awk '{if (0.+$1<0.01) {print "0.01"} else {print $1}}' | awk '{if (0.+$1>0.99) {print "0.99"} else {print $1}}'`
set threshold2 = `echo $threshold $maxsumdiv | awk '{print $1*$2}'`
foreach frame (p.0 p.1 p.2 m.0 m.1 m.2)
  awk '{if (0.+$5>=0.+"'"$threshold2"'"&&0.+$5>0.) print $1,$2*"'"$maxsumdiv"'"/$5,$3,$4,$5}' mle.$frame | awk '{if (0.+$3>0.+$4) {printf "%s %s %s %s %s\n%s %s %s %s %s\n",$1,$2,$3,"'"$length2"'",$5,$1,$2,"1",$4,$5} else {print $0}}'  > mle2.$frame
end

cat mle2.[pm].[012] | awk '{printf "%14.6f\n",$2}' > temp1
echo 0 >> temp1
set y2min = `minmax temp1 | awk '{print $1}'`
set y2max = `minmax temp1 | awk '{print $2}'`
rm -f temp1

@ w = `wc -l mle2.p.0 | awk '{print $1}'`
if ($w == 0) then
  set jjj1 = "#"
else
  set jjj1 = ""
endif
@ w = `wc -l mle2.p.1 | awk '{print $1}'`
if ($w == 0) then
  set jjj2 = "#"
else
  set jjj2 = ""
endif
@ w = `wc -l mle2.p.2 | awk '{print $1}'`
if ($w == 0) then
  set jjj3 = "#"
else
  set jjj3 = ""
endif
@ w = `wc -l mle2.m.0 | awk '{print $1}'`
if ($w == 0) then
  set jjj4 = "#"
else
  set jjj4 = ""
endif
@ w = `wc -l mle2.m.1 | awk '{print $1}'`
if ($w == 0) then
  set jjj5 = "#"
else
  set jjj5 = ""
endif
@ w = `wc -l mle2.m.2 | awk '{print $1}'`
if ($w == 0) then
  set jjj6 = "#"
else
  set jjj6 = ""
endif

#Process sixframe.R and plot.
set date = `date +%y-%m-%d`

echo -n "Doing plots...<br>"

cat ../../SCRIPTS/sixframe.R | \
  sed 's/sixframe/sixframe.'$count'/g' | \
  sed 's/nnn/'$nseqs'/g' | sed 's/ddd/'$date'/g' | \
  sed 's/bbb1/'$base1'/g' | sed 's/bbb2/'$base2'/g' | \
  sed 's/www/'$ncodons'/g' | sed 's/lll/'$step'/g' | \
  sed 's/ttt/'$lambdasum'/g' | sed 's/#kkk0/'$tick'/g' | \
  sed 's/orf1x1/'$orf1x1'/g' | sed 's/orf1x2/'$orf1x2'/g' | \
  sed 's/#kkk4/'$kkk4'/g' | sed 's/#kkk5/'$kkk5'/g' | \
  sed 's/#jjj1/'$jjj1'/g' | sed 's/#jjj2/'$jjj2'/g' | \
  sed 's/#jjj3/'$jjj3'/g' | sed 's/#jjj4/'$jjj4'/g' | \
  sed 's/#jjj5/'$jjj5'/g' | sed 's/#jjj6/'$jjj6'/g' | \
  sed 's/#ppp1/'$ppp1'/g' | sed 's/#ppp2/'$ppp2'/g' | \
  sed 's/#ppp3/'$ppp3'/g' | sed 's/#ppp4/'$ppp4'/g' | \
  sed 's/#ppp5/'$ppp5'/g' | sed 's/#ppp6/'$ppp6'/g' | \
  sed 's/yyy2max/'$y2max'/g' | sed 's/yyy2min/'$y2min'/g' | \
  sed 's/sss1/'$sss1'/g' | sed 's/sss2/'$sss2'/g' | sed 's/@/"/g' | \
  sed 's/%sss%/'$refseq'/g' | sed 's/%ggg%/'$title'/g' > plot3.R
R --no-save --slave < plot3.R >>& errorlog.txt
epstopdf sixframe.$count.eps 

echo "...Done<br><br>"

chmod a+rw ./*
#Write rest of webpage.
set id9 = `echo $newurl | sed 's/.*\///'`
echo "<hr>Six-frame plot (download "
echo "<a href=$newurl/sixframe.$count.png>png</a>, "
echo "<a href=$newurl/sixframe.$count.eps>eps</a>, "
echo "<a href=$newurl/sixframe.$count.pdf>pdf</a>).<br>"
if (! $download) then
  echo "<a href=redo_sixframe.pl?id=$id9&count=$count>Redraw</a> "
  echo "this plot again. (Don't just use the back button in your browser, "
  echo "or the plot may not refresh.)<br>"
  echo "Return to <a href=mlogd.pl>MLOGD server</a>.<br>"
endif
echo "<br><img src="'"'"$newurl/sixframe.$count.png"'"'" "
echo "alt="'"'"six-frame plot (png)"'"'"><hr>"

chmod a+rw ./*


