#!/bin/csh

# " *--------------------------------------------------------------------*"
# " |                     TALOS-N Starting Script                        |"
# " |               For Version 4.12 Rev 2016.137.12.07                  |"
# " |             For more information, use: talosn -help                |"
# " *--------------------------------------------------------------------*"
# ""
# "NOTICE:"
# " . By default (or when $useLocInstFlag = 0), this script first checkes for "
# "   and uses the nmrPipe-associated TALOS-N installation."
# " . If an nmrPipe-associated TALOS-N installation is not found, the independent 
# "   TALOS-N installation, as defined by $LocTALOSN_DIR, will be then used."
# " . When set $useLocInstFlag = 1, only the independent TALOS-N installation"
# " . is used."

set envFlag = `printenv | fgrep TALOSN_DIR | wc | awk '{print $1}'`

###                                                                                ###
### set this to 1 to use only the TALOS-N with an nmrPipe-independent installation ###
###                                                                                ###
set useLocInstFlag = 0

###                                                                                ###
### make sure this definition is correct when use "nmrPipe-independent" TALOS-N package
###                                                                                ###
set LocTALOSN_DIR = /u/shenyang/TALOS-N

if($envFlag == 1) then
# use first the system defined TALOSN_DIR, otherwise, use the specified one
   if(!(-d $TALOSN_DIR) && -d $LocTALOSN_DIR) then
	setenv TALOSN_DIR $LocTALOSN_DIR
   else if(!(-d $TALOSN_DIR) && !(-d $LocTALOSN_DIR)) then
	set envFlag = 0
   endif
else if($envFlag == 0 && -d $LocTALOSN_DIR) then
   setenv TALOSN_DIR $LocTALOSN_DIR
   set envFlag = 1
endif

if($envFlag == 0) then
   echo "=============================================================="
   echo "Error:: TALOSN_DIR environment variable is not defined."
   echo ""
   echo "TALOS-N prediction must be performed with an option -talosnDir"
   echo "to specify the TALOS-N installation path. For example:"
   echo ""
   echo "   /disk1/talosn/talosn -in shift.tab -talosnDir /disk1/talosn"
   echo ""
   echo "=============================================================="

   exit 0
endif

#echo "TALOSN_DIR = $TALOSN_DIR"

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

if (!($?NMRBASE) || $useLocInstFlag == 1) then
   if ($useLocInstFlag == 0) then
      echo "Failed to find an installed nmrPipe system"
   endif

   echo "A stand-alone version TALOS-N installation is used."
   echo "   The following nmrPipe related features are disabled:"
   echo "   . automatic TALOS/BMRB format input indentication and conversion"
   echo "   . automatic identical database protein identification and exclusion"
   echo ""
   
   echo "Testing $TALOSN_DIR/bin/TALOSN"
   set tInfo = `$TALOSN_DIR/bin/TALOSN -help |& fgrep TALOSN_INFO | wc`
 
   if ($tInfo[1] != 0) then
      echo "Running $TALOSN_DIR/bin/TALOSN $*"
      echo ""
      $TALOSN_DIR/bin/TALOSN $* 
      exit 0
   else
      foreach i ($BINS)
         if(-e $TALOSN_DIR/bin/TALOSN.$i) then
            set tInfo = `$TALOSN_DIR/bin/TALOSN.$i -help |& fgrep TALOSN_INFO | wc`
            echo "Testing $TALOSN_DIR/bin/TALOSN.$i"

            if ($tInfo[1] != 0) then
               echo "Running $TALOSN_DIR/bin/TALOSN.$i $*"
	       echo ""
	       
	       $TALOSN_DIR/bin/TALOSN.$i $* 
	       exit
            endif
	 endif
      end
      
      echo "Error finding a working TALOS-N executable in $TALOSN_DIR/bin"
   endif

   exit 0
endif


set  BINVAL = unknown

if ($?NMRBINTYPE) then
   set  BINVAL = $NMRBINTYPE
endif

if($BINVAL == linux) then
   set  BINVAL = linux9
endif
#echo "NMRBINTYPE = ${BINVAL}"

# The nmrPipe based starting script
#
if (`flagLoc $argv -help`) then
   if (-e $TALOSN_DIR/bin/TALOSN.${BINVAL}) then
      $TALOSN_DIR/bin/TALOSN.${BINVAL} -help

      if ($status == 0) then
         exit 0
      endif
   endif

   if (-e $TALOSN_DIR/bin/TALOSN.static.${BINVAL}) then
      $TALOSN_DIR/bin/TALOSN.static.${BINVAL} -help

      if ($status == 0) then
         exit 0
      endif
   endif

   exit 1
endif

set csName     = (`getArgD $argv -in  None`)
set testName   = (`getArgD $argv -in  None`)
set pdbName    = (`getArgD $argv -ref None`)
set bmrbName   = None

if ($?NMRSERVER_MODE) then
   set serverFlag = $NMRSERVER_MODE
else
   set serverFlag = 0
endif

if ($serverFlag == 0) then
   set testBinFlag  = 1
   set failSafeFlag = 0
   set autoFlag     = 1
else
   set testBinFlag  = 0
   set failSafeFlag = 1
   set autoFlag     = 1
endif

set starArgs  = ""
set protArgs  = ""
set tExclList = ""

set cInfo    = (0 0 0)
set sInfo    = (0 0 0)
set pInfo    = (0 0 0)

if (`flagLoc $argv -noauto`) then
   set autoFlag = 0
endif

if ($csName == None) then
   echo "TALOS-N Error: No Chemical Shift Input Specified."

   if ($failSafeFlag != 0) then
      echo "TALOS-N Error: No Chemical Shift Input Specified." > fail.safe
   endif

   exit 1
endif

if (($csName != None) && !(-e $csName)) then
   echo "TALOS-N Error Finding Chemical Shift Input."

   if ($failSafeFlag != 0) then
      echo "TALOS-N Error Finding Chemical Shift Input." > fail.safe
   endif

   exit 1
endif

if (($pdbName != None) && !(-e $pdbName)) then
   echo "TALOS-N Error Finding PDB Input."

   if ($failSafeFlag != 0) then
      echo "TALOS-N Error Finding PDB Input." > fail.safe
   endif

   exit 1
endif


set cNMRFileFlag = `which checkNMRFile.tcl |& sed -e 's/\// /g' | awk '{print $NF}'`
if ($cNMRFileFlag != checkNMRFile.tcl) then
   set autoFlag = 0
   echo "Failed to Find checkNMRFile.tcl Script"
endif

if ($autoFlag == 0) then
   echo "TALOS-N Using Input without Auto Format Determination ..."
else
   set bmrbName   = (`getArgD $argv -bmrb bmrb.tab`)

   if (($pdbName != None) && (-e $pdbName)) then
      echo "TALOS-N Checking PDB Input ..."
      set pInfo = (`checkNMRFile.tcl -pdb -in $pdbName -verb` 0)

      if ($pInfo[1] == 0) then
         echo "TALOS-N Error. PDB Input does not seem to be in PDB Format."

         if ($failSafeFlag != 0) then
            echo "TALOS-N Error. PDB Input does not seem to be in PDB Format." > fail.safe
         endif

         exit 1
      endif
   endif

   if (($csName != None) && (-e $csName)) then
      echo "TALOS-N Probing for Input Table Format ..."
      set cInfo = (`checkNMRFile.tcl -tab  -in $csName -noverb` 0)
      set sInfo = (`checkNMRFile.tcl -star -in $csName -noverb` 0)
   endif
 
   if ($cInfo[1] == 0 && $sInfo[1] == 0) then
      echo "Chemical Shift Input does not seem to be NMRPipe-Format Table."
      echo "Chemical Shift Input does not seem to be BMRB NMR-Star Format."
      echo "TALOS-N Error. Chemical Shift Input is not correctly formatted."

      if ($failSafeFlag != 0) then
         echo "TALOS-N Error. Chemical Shift Input is not correctly formatted." > fail.safe
      endif

      exit 1
   endif

   if ($cInfo[1] != 0) then
      echo "Chemical Shift Input seems to be NMRPipe-Format Table."
   else if ($sInfo[1] != 0) then
      echo "Chemical Shift Input seems to be BMRB NMR-Star Format."
      echo "TALOS-N Testing BMRB NMR-Star Format ..."
      set sInfo = (`checkNMRFile.tcl -bmrb -in $csName -verb` 0)

      if ($sInfo[1] == 0) then
         echo "TALOS-N Error. Chemical Shift Input is not correctly formatted."

         if ($failSafeFlag != 0) then
            echo "TALOS-N Error. Chemical Shift Input is not correctly formatted." > fail.safe
         endif

         exit 1
      endif
      
      
      set star2csFlag = `which star2cs.tcl |& sed -e 's/\// /g' | awk '{print $NF}'`

      if ($star2csFlag == star2cs.tcl) then
         echo "Converting Input from NMR-Star Format to NMRPipe-Format Table ..."
         star2cs.tcl -in $csName -title $csName -out $bmrbName
         set starArgs = "-star $bmrbName"
         set testName = $bmrbName
      else
         echo "Failed to Find Conversion Script star2cs.tcl"
      endif

      if (!(-s $bmrbName)) then
         echo "Error Converting Input from BMRB NMR-Star Format."

         if ($failSafeFlag != 0) then
            echo "TALOS-N Error Converting Input from BMRB NMR-Star Format." > fail.safe
         endif

         exit 1
      endif
   endif

   echo "TALOS-N Checking NMRPipe-Format Chemical Shifts ..."
   set cInfo = (`checkNMRFile.tcl -cs -in $testName -maxBadSeq 3 -verb` 0)

   if ($cInfo[1] == 0) then
      echo "Error in NMRPipe-Format Chemical Shift Input."

      if ($failSafeFlag != 0) then
         echo "TALOS-N Error in NMRPipe-Format Chemical Shift Input." > fail.safe
      endif

      exit 1
   endif
endif


set sTabFlag = `which selectTab.tcl |& sed -e 's/\// /g' | awk '{print $NF}'`

if ($sTabFlag == selectTab.tcl) then
   if (`flagLoc $argv -autoproton`) then
      echo "Testing Input Proton Count ..."

      set hInfo = (`selectTab.tcl -in $testName -show RESID -nohdr -notext -cond "strmatch( ATOMNAME, 'H*' )" | wc` 0 0 0)

      if ($hInfo[1] == 0) then
         echo "Found 0 Protons in Input, Using TALOS-N No-Proton Mode."
         set protArgs = "-noproton"
      else
         echo "Found $hInfo[1] Protons, Using TALOS-N Standard Mode."
      endif
   endif
else
   echo "Failed to Find selectTab.tcl Script, Input Proton Count Step Skipped"
endif


set tExclFlag = `which talosExclList.tcl |& sed -e 's/\// /g' | awk '{print $NF}'`
set tExclList = ""

if ($tExclFlag == talosExclList.tcl) then
   set tExclList = (`talosExclList.tcl $* $starArgs`)

   if ($#tExclList > 1) then
      echo "Auto-Excluding Identical Sequence: $tExclList[2-]"
   endif
else
   echo "Failed to Find talosExclList.tcl Script, Auto-Excluding Identical Sequence Step Skipped"
endif


if ($testBinFlag == 0) then
    $TALOSN_DIR/bin/TALOSN.${BINVAL} $* $tExclList $starArgs $protArgs
    exit 0
endif

foreach i (${BINVAL} static.${BINVAL})
   if(-e $TALOSN_DIR/bin/TALOSN.$i) then
         set tInfo = `$TALOSN_DIR/bin/TALOSN.$i -help |& fgrep TALOSN_INFO | wc`
         echo "Testing $TALOSN_DIR/bin/TALOSN.$i"
	 
	 if ($tInfo[1] != 0) then
	    echo "Running $TALOSN_DIR/bin/TALOSN.$i $* $tExclList $starArgs $protArgs"
	    echo ""
	    
	    $TALOSN_DIR/bin/TALOSN.$i $* $tExclList $starArgs $protArgs
	    exit
	 endif
   endif
end

# search all binary files if the $BINVAL one fails
foreach i ($BINS)
   if($i != ${BINVAL} && -e $TALOSN_DIR/bin/TALOSN.$i) then
         set tInfo = `$TALOSN_DIR/bin/TALOSN.$i -help |& fgrep TALOSN_INFO | wc`
         echo "Testing $TALOSN_DIR/bin/TALOSN.$i"
	 
	 if ($tInfo[1] != 0) then
	    echo "Running $TALOSN_DIR/bin/TALOSN.$i $* $tExclList $starArgs $protArgs"
	    echo ""
	    
	    $TALOSN_DIR/bin/TALOSN.$i $* $tExclList $starArgs $protArgs
	    exit
	 endif
   endif
end


echo "Error finding a working TALOS-N executable in $TALOSN_DIR/bin"
exit 1
