#!/bin/csh

# Change this to wherever you have installed the hhsuite library files
setenv HHLIB /usr/local/lib/hhsuite/hh

# Edit to more sensible locations e.g. /usr/local
set metabindir = ./bin
set metadatadir = ./data

set sequence = $1
set target = $1:t:r

echo "Running PSIPRED+SOLVPRED..."

# Make sure you edit the runpsipredandsolv script with your local install information
runpsipredandsolv $1

# You will need to edit this line for your own hhblits installation directories etc.
# You may want to add extra parameters e.g. to enable multithreading.
hhblits -i $1 -d /data/seq/hhblits/uniprot20_2013_03 -oa3m $target.a3m -n 3 -maxfilt 500000 -diff inf -id 99 -cov 60 > $target.hhblog

egrep -v "^>" $target.a3m | sed 's/[a-z]//g' > $target.aln

# We assume here that psicov, freecontact and ccmpred are in the default shell PATH.
# If this is not the case you will need to manually add the appropriate path information.
# You may also want to add extra parameters to these commands e.g. the -a option to freecontact
# or the -t option to ccmpred to enable multithreading. PSICOV enables multithreading by default.
if (`cat $target.aln | wc -l` >= 5) then
    echo "Running PSICOV..."
    psicov -o -d 0.03 $target.aln > $target.psicov
    echo "Running freecontact..."
    freecontact < $target.aln > $target.evfold
    echo "Running CCMpred..."
    ccmpred $target.aln $target.ccmpred
else
    \rm -f $target.psicov $target.evfold $target.ccmpred
    touch $target.psicov $target.evfold $target.ccmpred
endif

$metabindir/alnstats $target.aln $target.colstats $target.pairstats

echo "Running MetaPSICOV Stage 1..."

$metabindir/metapsicov $target.colstats $target.pairstats $target.psicov $target.evfold $target.ccmpred $target.ss2 $target.solv $metadatadir/weights_6A.dat $metadatadir/weights_65A.dat $metadatadir/weights_7A.dat $metadatadir/weights_75A.dat $metadatadir/weights_8A.dat $metadatadir/weights_85A.dat $metadatadir/weights_9A.dat $metadatadir/weights_10A.dat $metadatadir/weights_811A.dat $metadatadir/weights_1012A.dat | sort -n -r -k 5 > $target.metapsicov.stage1

echo "Running MetaPSICOV Stage 2..."

$metabindir/metapsicovp2 $target.colstats $target.metapsicov.stage1 $target.ss2 $target.solv $metadatadir/weights_pass2.dat | sort -n -r -k 5 > $target.metapsicov.stage2

echo "Running MetaPSICOV-HB..."

$metabindir/metapsicovhb $target.colstats $target.metapsicov.stage1 $target.ss2 $target.solv $metadatadir/weights_hbpass2.dat | sort -n -r -k 5 > $target.metapsicov.hb

echo "Output files: " $target.metapsicov.stage1 $target.metapsicov.stage2 $target.metapsicov.hb
