#!/bin/bash
#
# Program: TPP HPC Tools
# Author:  Joe Slagel
#
# Copyright (C) 2009-2012 by Joseph Slagel
# 
# This library is free software; you can redistribute it and/or             
# modify it under the terms of the GNU Lesser General Public                
# License as published by the Free Software Foundation; either              
# version 2.1 of the License, or (at your option) any later version.        
#                                                                           
# This library 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 Lesser General Public          
# License along with this library; if not, write to the Free Software       
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
# 
# Institute for Systems Biology
# 1441 North 34th St.
# Seattle, WA  98103  USA
# jslagel@systemsbiology.org
#
# $Id: $
#

set -e          # Tell bash to exit if any statement fails

# -- Local settings -----------------------------------------------------------
   
   # Default name for jobs in queue
   QNAME=${QNAME:-}

   # Default project to submit jobs to
   QPROJECT=${QPROJECT:-}

   # Default number of maximum processors (threads)
   MAX_PROCS=${MAX_PROCS:-8}

   # Default qsub flags
   QSUBFLAGS="-pe serial ${MAX_PROCS} ${QSUBFLAGS}"

   # Programs
   MYRIMATCH=${MYRIMATCH-$(which myrimatch || true)}
   INTERACTPARSER=${INTERACTPARSER-$(which InteractParser || true)}
   
# -----------------------------------------------------------------------------
   
#
# List of commands to run in the qsub script.  Commands are echo'ed out and
# are intended to be read in by the qsubmit() function.
#
function setcmds() {
   prog=myrimatch
   
   rm -f $odir$root.pep.xml                     # Remove previous results
   rm -f $odir$root.pepXML
   
   cmds=$(cat <<EOF
$MYRIMATCH -cfg $P_OPT -cpus $MAX_PROCS $file

# Now make the pep.xml compatible with TPP
$INTERACTPARSER $odir$root.pep.xml $odir$root.pepXML -L0 -R9999 \
   -a$idir
#rm -f $odir$root.pepXML
EOF
)
}


# -- Main ----------------------------------------------------------------------

source qgrid_functions                          # Load "q" grid functions

# Check for necessary programs
[ -x "${MYRIMATCH}" ] || die "Error no myrimatch in your path"

search_options myrimatch.params "*.mzML|*.mzXML" $*

P_OPT="$(rel2abs "$P_OPT")"

# Check to make myrimatch can write a database index file if one
# doesn't exist.  This is typically named with a ".index" extension
# and will automatically be created if it doesn't exist.
DB=$(perl -ne 'print $1 if /ProteinDatabase\s*=\s*(.*)/i;' $P_OPT)
DBDIR=$(dirname $DB)
if [ ! -f "$DB.index" -a ! -w "$DBDIR" ]; then
   die "Error myrimatch needs write permissions for database index file"
fi

submit_jobs

exit 0
