Utils/BuildHHM.sh
#!/bin/bash

numIterations=2
Evalue=0.001
neffmax=6.5

numCPUs=4
ResDir=`pwd`

if [ -z "${HHDIR}" ]; then
	echo "ERROR: please set HHDIR to the install folder of hhblits, e.g., /mnt/data/RaptorXCommon/HHblits/hhsuite-3.2.0-SSE2-Linux"
	exit 1
fi
HHbin=$HHDIR/bin

if [ -z "${HHDB}" ]; then
	echo "ERROR: please set HHDB to the seq database to be searched by hhblits, e.g., /mnt/data/RaptorXCommon/HHblits/DB/uniref30_2020_02/uniref30_2020_02"
	exit 1
fi

DB=$HHDB

function Usage
{
        echo $0 "[-i nIterations | -d DB | -e evalue | -m maxneff | -n numCPUs | -s savefolder ] seqFile"
	echo "	This script builds an MSA and a profile HMM for input protein sequence file, which then can be used for homology search, protein threading and protein structure property prediction"
	echo "		The MSA generated by this script may not be the best for contact/distance/orientation prediction"
	echo "	seqFile: an amino acid sequence file in FASTA format, ending with .seq or .fasta"
	echo "	-d: the HMM database to be searched by hhblits, default $DB"
	echo "	-i: the number of iterations searched by hhblits, default $numIterations"
        echo "	-n: the number of CPUs to be used by hhblits, default $numCPUs"
	echo "	-e: the E-value used by hhblits, default $Evalue"
	echo "	-m: the maximum neff value used by hhblits, default $neffmax"
	echo "	-s: the folder for result saving, default current work directroy"
}

while getopts ":i:d:n:e:m:s:" opt; do
        case ${opt} in
                i )
                  numIterations=$OPTARG
                  ;;
		d )
		  DB=$OPTARG
		  ;;
                n )
                  numCPUs=$OPTARG
                  ;;
		e )
		  Evalue=$OPTARG
		  ;;
		m )
		  neffmax=$OPTARG
		  ;;
                s )
                  ResDir=$OPTARG
                  ;;
                \? )
                  echo "Invalid Option: -$OPTARG" 1>&2
                  exit 1
                  ;;
                : )
                  echo "Invalid Option: -$OPTARG requires an argument" 1>&2
                  exit 1
                  ;;
        esac
done
shift $((OPTIND -1))

if [ $# -ne 1 ]; then
        Usage
        exit 1
fi

DBIndexFile=${DB}_hhm.ffindex
if [ ! -f $DBIndexFile ]; then
        echo "ERROR: invalid or damaged seq database to be searched by hhblits: $DB"
        exit 1
fi

seqFile=$1
if [ ! -f $seqFile ]; then
	echo "ERROR: invalid sequence file $seqFile"
	exit 1
fi

if [[ $seqFile == *.fasta ]]; then
        seqName=`basename $seqFile .fasta `
else
        seqName=`basename $seqFile .seq `
fi

if [ ! -d $ResDir ]; then
	mkdir -p $ResDir
fi

if [ ! -f $seqName.seq ]; then
        cp $seqFile $ResDir/$seqName.seq
fi

a3mfile=$ResDir/$seqName.a3m
hhmfile=$ResDir/$seqName.hhm

$HHbin/hhblits -i $ResDir/$seqName.seq -d $DB -oa3m $a3mfile -n $numIterations -neffmax $neffmax -e $Evalue -cpu $numCPUs
if [ $? -ne 0 ]; then
	echo "ERROR: failed to generate an MSA for $seqName by hhblits"
	exit 1
fi

$HHbin/hhmake -i $a3mfile -o $hhmfile
if [ $? -ne 0 ]; then
	echo "ERROR: failed to run $HHbin/hhmake -i $a3mfile -o $hhmfile"
	exit 1
fi
