#!/bin/sh
# Modified by Brian Fristensky (frist@cc.umanitoba.ca) to
# automatically delete input file once readseq has translated it.

# pima-gde.sh - Interface pima with GDE.
#
# Copyright (C) 1994 Baylor College of Medicine
#
# All rights reserved.
#
# Contact Brent Wiese, brent@bcm.tmc.edu
#

if [ $# -lt 3 ]; then
    echo "pima-gde: bad arguments."
    exit 1
fi

input=$1
name=$2
# 0 for maximal, 1 for single, 2 for both
type=$3
shift; shift; shift

#
# Can not use readseq to convert to genbank format since readseq will strip
# numbers (digits) and numbers are part of the patterns.  This pretty much
# leaves the flat file format.  Since the '%', '#', '@', and '"' characters
# are part of patterns and might cause difficulty at the beginning of a line,
# just put a space (which is stripped) at the beginning of every sequence line.
# Please let me (brent@bcm.tmc.edu) know if you experience any difficulty
# with this conversion method, thanks.
fixup1='s/^[^>]/ &/'
fixup2='s/^>/%/'

readseq -a -f8 -o=$input.fa $input
rm -f $input
nice pima "$@" $name $input.fa >$name.out 2>&1
error=$?
(dtpad $name.out; rm -f $name.out) &
if test $error -ne 0 -o ! -f $name-ML.pima; then
    exit 1
fi

case $type in
0)
   sed -e "$fixup1" -e "$fixup2" $name-ML.pima > $name-ML
   (gde $name-ML; rm -f $name-ML) &
   ;;
1)
   sed -e "$fixup1" -e "$fixup2" $name-SB.pima > $name-SB
   (gde $name-SB; rm -f $name-SB) &
   ;;
2)
   sed -e "$fixup1" -e "$fixup2" $name-ML.pima > $name-ML
   sed -e "$fixup1" -e "$fixup2" $name-SB.pima > $name-SB
   (gde $name-ML; rm -f $name-ML) &
   (gde $name-SB; rm -f $name-SB) &
   ;;
esac

rm -f $input.fa \
    $name-ML.cluster $name-ML.pattern $name-ML.pima \
    $name-SB.cluster $name-SB.pattern $name-SB.pima