#!/bin/tcsh

# This is a simple script which will carry out all of the basic steps
# required to make a PSIPRED prediction. Note that it assumes that the
# following programs are available in the appropriate directories:
# seq2mtx - PSIPRED V3 program
# psipred - PSIPRED V3 program
# psipass2 - PSIPRED V3 program

# usage: runpsipred_single faFile or -l falistfile -outpath outpath
set usage = "runpsipred_single faFile | -l falistFile -outpath outpath"

if ($#argv < 1) then
    echo $usage
    exit
endif

set faFile = 
set faListFile = 
set outpath = 
set isQuiet=false

set isNonOptionArg=false
while ("$1" != "" )
    if ( $isNonOptionArg == true ) then 
    set faFile = $1
    set isNonOptionArg="false"
    else if ( "$1" == "--" ) then
        set isNonOptionArg=true
    else 
        switch($1)
        case -h:
        case --help:
            echo $usage
            exit
            breaksw
        case -o:
        case -outpath:
            set outpath = $2
            shift
            breaksw
        case -l:
        case --list:
            set faListFile=$2
            shift
            breaksw
        case -q:
        case -quiet:
            set isQuiet=true
            breaksw
        case -*:
            echo "Error! Wrong argument: $1"
            exit
            breaksw
        default:
            set faFile=$1
            breaksw
        endsw
    endif
    shift
end               

if ("$faListFile" != "") then 
    if ("$outpath" == "") then
        echo "outpath must be set when faListFile is set"
        exit 1;
    endif
endif

set psipreddir=`dirname $0` # added 2010-10-22 by Nanjiang
# Where the PSIPRED V3 programs have been installed
set execdir = $psipreddir/bin
# Where the PSIPRED V3 data files have been installed
set datadir = $psipreddir/data

if ( "$faFile" != "" ) then 
# NOTE: Script modified to be more cluster friendly (DTJ April 2008)
    set basename = $faFile:r
    set rootname = $basename:t
    set outname = $basename.chk
# Generate a "unique" temporary filename root
    set hostid = `hostid`
    set tmproot = psitmp$$$hostid
endif

if ($isQuiet == false) then
    echo "Generating mtx file from sequence" $1 "..."
endif

if ("$faFile" != "") then 
    $execdir/seq2mtx $faFile > $tmproot.mtx
else if ("$faListFile" != "") then 
    \mkdir -p $outpath
    $execdir/seq2mtx -l $faListFile -outpath $outpath 
endif


if ($status != 0) then
    echo "FATAL: Error whilst running makemat - script terminated!"
    exit 1
endif

if ( $isQuiet == false) then
    echo "Predicting secondary structure based on single sequence ..."
    echo Pass1 ...
endif

set mtxFileList = /tmp/mtxfilelist$$.txt
\find $outpath -name "*.mtx" > $mtxFileList

if ("$faFile" != "") then
    $execdir/psipred $tmproot.mtx $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3 > $rootname.ss
else if ("$faListFile" != "") then
    $execdir/psipred -l $mtxFileList -outpath $outpath $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3
endif

if ($status != 0) then
    echo "FATAL: Error whilst running psipred - script terminated!"
    exit 1
endif

if ( $isQuiet == false) then 
    echo Pass2 ...
endif 

set ssFileList = /tmp/ssfilelist$$.txt
\find $outpath -name "*.ss" > $ssFileList

if ($faFile != "") then
    $execdir/psipass2 $datadir/weights_p2.dat 1 1.0 1.0 $rootname.ss2 $rootname.ss > $rootname.horiz
else if ($faListFile != "") then
    $execdir/psipass2 -l $ssFileList -outpath $outpath $datadir/weights_p2.dat 1 1.0 1.0
endif


if ($status != 0) then
    echo "FATAL: Error whilst running psipass2 - script terminated!"
    exit 1
endif

# Remove temporary files

if ( $isQuiet == false) then 
    echo Cleaning up ...
    if ("$faFile" != "") then 
        echo "Final output files:" $rootname.ss2 $rootname.horiz
    else if ($faListFile != "") then
        echo "Final results output to $outpath" 
    endif
    echo "Finished."
endif 

if ($faFile != "") then 
    \rm -f $tmproot.* error.log
else if ($faListFile != "") then 
    \rm -f $mtxFileList $ssFileList
endif
