DL4PropertyPrediction/Scripts/PredictProperty4Server.sh
#!/bin/bash

if [ -z "${ModelingHome}" ]; then
        echo "ERROR: Please set the environmental variable ModelingHome to the install folder of the RaptorX-3DModeling package"
        exit 1
fi
if [ -z "${DL4PropertyPredHome}" ]; then
        echo "ERROR: Please set the environmental variable DL4PropertyPredHome to the install folder of DL4PropertyPrediction"
        exit 1
fi

#DeepModelFile=$DL4PropertyPredHome/params/ModelFile4PropertyPred.txt
#ModelName=PhiPsiSet10820Models
DeepModelFile=""
ModelName=""
GPU=-1
GPUMachineFile=$ModelingHome/params/GPUMachines.txt
ResultDir=""

function Usage {
        echo $0 "[ -f DeepModelFile | -m ModelName | -d ResultDir | -g gpu | -h GPUMachineFile ] proteinName rootDir"
        echo "	This script predicts structure properties of one protein using a local or remote GPU"
	echo "	proteinName: the protein name"
        echo "	rootDir: a folder generated by BuildFeatures.sh or BuildMSAs.sh in BuidFeatures/, e.g., 1pazA_OUT/"
	echo "  -f: a file containing a list of deep learning model files, default empty"
        echo "          When a remote machine is used, all the file paths shall be valid on that machine"
        echo "          when this option is not used, the default DeepModelFile will be used"
        echo "  -m: a model name defined in DeepModelFile representing a set of deep learning models, default empty"
        echo "          When a remote machine is used, this name shall be valid on that machine"
        echo "          when this option is not used, the default model name will be used"
        echo "	-g: -1 (default) or 0-3. If -1, automatically select a GPU"
        echo "	-d: the folder for result saving, default rootDir/PropertyPred/. That is, rootDir shall be writable if you do not provide a folder for result saving"
	echo "	-h: a file containing remote machines with GPUs to which you may ssh without password, default $GPUMachineFile"
	echo "		When this file does not exist, only local GPUs will be used"
}

while getopts ":f:m:d:g:h:" opt; do
        case ${opt} in
                f )
                  DeepModelFile=$OPTARG
                  ;;
                m )
                  ModelName=$OPTARG
                  ;;
                d )
                  ResultDir=$OPTARG
                  ;;
                g )
                  GPU=$OPTARG
                  ;;
		h )
		  GPUMachineFile=$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 2 ]; then
        Usage
        exit 1
fi

target=$1

rootDir=$2
if [ ! -d $rootDir ]; then
	echo "ERROR: the root directory for $target does not exist: $rootDir"
	exit 1
fi

inputFeature=$rootDir/PropertyPred/${target}.propertyFeatures.pkl
if [ ! -f $inputFeature ]; then
	$DL4PropertyPredHome/Scripts/CollectPropertyFeatures.sh $target $rootDir
	if [ $? -ne 0 ]; then
		echo "ERROR: Failed to collect input feature for property prediction!"
		exit 1
	fi
fi

if [ ! -f $inputFeature ]; then
        echo "ERROR: invalid input feature file for property prediction: " $inputFeature
        exit 1
fi

if [ -z "${ResultDir}" ]; then
	if [ ! -w $rootDir ]; then
		echo "ERROR: folder not writable: $rootDir"
		exit 1
	fi
	ResultDir=$rootDir/PropertyPred/
fi

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

cmd=`readlink -f $0`
cmdDir=`dirname $cmd`

options=""
if [[ ! -z "${DeepModelFile}" ]]; then
        options=$options" -f $DeepModelFile "
fi

if [[ ! -z "${ModelName}" ]]; then
        options=$options" -m $ModelName "
fi

$cmdDir/PredictPropertyWrapper.sh $options -d $ResultDir -g $GPU -h $GPUMachineFile $inputFeature
if [ $? -ne 0 ]; then
	echo "ERROR: failed to run $cmdDir/PredictPropertyWrapper.sh $options -d $ResultDir -g $GPU $inputFeature"
	exit 1
fi
