#!/bin/sh -

# this script will run SMART (http://smart.embl-heidelberg.de/) for an
# sequence input file or on each file in a file of filenames.  it uses
# lynx to access send a query to the SMART web site and then writes
# the output to a file in the same way as run_fasta and run_blastp.
# see:
#   http://www.sanger.ac.uk/Software/Artemis/stable/manual/runmenu.html#RUNMENU-CONFIGURATION
# for more details.

# you will need the "GET" command from the perl LWP module to use this script

# to customise this script see the function called run_one_prog below


RCS_HEADER="$Header: //tmp/pathsoft/artemis/etc/run_smart,v 1.1 2004-06-09 10:03:12 tjc Exp $"

PROG=`echo $RCS_HEADER | sed 's/.*run_\(.*\),v.*/\1/'`


if [ $# = 3 -a x$1 = x-onefile -o $# = 4 -a x$1 = x-onefile ]
then
    shift
    ONEFILE=t
else
    if [ $# != 2 -a $# = != 1 ]
    then
        echo usage: $0 -onefile input_file output_file 1>&2
        echo    or: $0 file_of_filenames 1>&2
        exit 1
    fi
fi


# expand any ~ or environment variables
EXPANDED_PARAMETERS=`echo "echo $PARAMETERS" | /bin/csh -f`


### change this function to suit your site:

run_one_prog () {
    INPUT_FILE=$1
    OUTPUT_FILE=$2
    PARAMETERS=$3

    # remove the rubbish from the FASTA header and change the sequence
    # into URL form
    SEQUENCE=`perl -pne 'chomp; s/^>.*/>sequence/; s/ /\%20/g; $_ .= "%0D"' $INPUT_FILE`

    SEQUENCE=`echo $SEQUENCE | sed 's/%0D$//'`

    URL='http://smart.embl-heidelberg.de/smart/show_motifs.pl?INCLUDE_SIGNALP=INCLUDE_SIGNALP&DO_PROSPERO=DO_PROSPERO&DO_PFAM=DO_PFAM&SEQUENCE='"$SEQUENCE"

    ### change these lines:
    EXEC=lynx

    echo "about to start $EXEC with input from $INPUT_FILE and output to" 1>&2
    echo "$OUTPUT_FILE" 1>&2

    COMMAND="$EXEC -force_html -dump $URL"

    echo "command line: $COMMAND" 1>&2

    (echo "read from $URL"; echo) > $OUTPUT_FILE

    $COMMAND 2>&1 >> $OUTPUT_FILE |
      tee ${PROG}_errors.new 1>&2

    #### end of changes

    if [ -s ${PROG}_errors.new ]
    then
        ( echo ERROR running $PROG: ; echo;
          echo ===================================================
          cat ${PROG}_errors.new ) >> $OUTPUT_FILE
        cat ${PROG}_errors.new >> ${PROG}_errors
    fi
}

(echo "#!/bin/sh -"; echo "kill $$") > $PROG.kill

chmod a+x $PROG.kill

if [ x$ONEFILE = x ]
then
    for i in `cat $1`
    do
        run_one_prog $i $i.out $EXPANDED_PARAMETERS
    done

else
    run_one_prog $1 $2 $EXPANDED_PARAMETERS
fi

exit 0
