#!/bin/csh
# " *--------------------------------------------------------------------*"
# " |     TALOS-N Sequence-base Secondary Structure Prediction Script    |"
# " |               For Version 4.12 Rev 2016.137.12.07                  |"        
# " |            For more information, use: talosn_ss -help              |"
# " *--------------------------------------------------------------------*"

set helpFlag  = 0
set fastaFlag = 0

set checkFile = unknown

if ($#argv != 1) then
   set helpFlag = 1
else if ($#argv == 1) then
   if ($argv[1] == "-help" || $argv[1] == "+help" || $argv[1] == "--help") then
      set helpFlag = 1
   endif
endif

if ($helpFlag == 1) then
   echo "   =================================================================="
   echo "      TALOS-N Sequence-based Secondary Structure Prediction Script   "
   echo "   =================================================================="
   echo ""
   echo "Usage:"
   echo "       talosn_ss [fasta_sequence_file.fasta]"
   echo "        # to run the prediction using a fasta sequence (psi-blast required)"
   echo "        # (will remove the fasta_sequence_file.check file if there is one)"
   echo ""
   echo "  .OR. talosn_ss [blast_check_file.check]"
   echo "        # ro run the prediction using a psi-blast generated check file"
   exit 0
endif

if (!(-e $argv[1])) then
   echo "[talosn_ss] ERROR: input file $1 is not found"
   exit 0
endif

######################################################################
### Make Sure the TALOS-N Installation Definition Below is Correct ###
######################################################################
setenv TALOSN_DIR /net/penton2/shenyang/projects/TALOS++/distribution/

set a    = (`echo $argv[1] | sed -e 's/\./ /g'`)
set suff = $a[$#a]

set b    = (`echo $argv[1] | sed -e 's/\// /g' | sed -e 's/\.fasta//g' | sed -e 's/\.check//g'`)
set root = $b[$#b]

if ($suff == "fasta") then
   set fastaFlag = 1
   if (-e $root.check) then
      echo "[talosn_ss] WARNING: blast checkpoint file found, deleted!"
      /bin/rm -f $root.check
   endif
   set checkFile = $root.check
else if ($suff == "check") then
   set fastaFlag = 0
   set checkFile = $argv[1]
endif

if ($fastaFlag == 1) then
# if (!(-e $root.check)) then
   ###################################################################
   ### Make Sure the Psi-Blast Definitions Listed Below is Correct ###
   ###################################################################
   set BLAST     = "/net/penton1/shenyang/blast/ncbi/bin/blastpgp"
   set NR_DBNAME = "/net/penton1/shenyang/blast/blastdb/nr"

   if (!(-e $BLAST)) then
      echo "[talosn_ss] ERROR: psi-blast program is not found @ $BLAST"
      exit 0
   endif
   if (!(-e {$NR_DBNAME}.pal)) then
      echo "[talosn_ss] ERROR: nr database ${NR_DBNAME} is not found"
      exit 0
   endif

   echo "[talosn_ss] Blasting ..."
   $BLAST -t T -i $1 -F F -j2 -o $root.blast -v10000 -b10000 -K1000 -h0.0009 -e0.0009 -C $root.check -Q $root.pssm -d $NR_DBNAME
endif

set BINS = "linux9_x64 linux9 linux mac winxp static.linux9_x64 static.linux9 static.linux static.winxp"

if (-e $checkFile) then
   echo "[talosn_ss] TALOS-N sequence-based secondary structure prediction ..."

   foreach i ($BINS)
      if (-e $TALOSN_DIR/bin/TALOSN.$i) then
         set tInfo = `$TALOSN_DIR/bin/TALOSN.$i -help |& fgrep TALOSN_INFO | wc`
	 echo "[talosn_ss] Testing $TALOSN_DIR/bin/TALOSN.$i"

         if ($tInfo[1] != 0) then
            echo "[talosn_ss] Running $TALOSN_DIR/bin/TALOSN.$i -seqSS -blast_check $checkFile -inTitle $argv[1]"
            echo ""

            $TALOSN_DIR/bin/TALOSN.$i -seqSS -blast_check $checkFile -inTitle $argv[1]
            exit
         endif
      endif
   end

   echo "[talosn_ss] Error: no working TALOS-N executable in $TALOSN_DIR/bin"
endif

exit 1

