DL4PropertyPrediction/Scripts/PredictPropertyWrapper.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 installation folder of DL4PropertyPrediction"
        exit 1
fi

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

function Usage {
        echo $0 "[ -f DeepModelFile | -m ModelName | -d ResultDir | -g gpu | -h GPUMachineFile ] inputFeature_PKL"
        echo "	This script predicts properties from a feature file (in PKL format) using a local machine or a remote server with GPUs if the local machine does not have GPUs"
        echo "	inputFeature: the input feature file in PKL format generated by CollectPropertyFeatures.sh or GenPropertyFeaturesFromMultiHHMs.py"
	echo "  -f: a file containing a list of deep learning model files, default empty"
        echo "          Note that 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 "          Note that 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 "	-d: the folder for result saving, default current work directory"
        echo "	-g: -1 (default), 0-3. If set to -1, automatically select a GPU"
	echo "	-h: a file containing some remote computer accounts which have access to GPUs and to which you may ssh without password, default $GPUMachineFile"
	echo "		if GPUMachineFile does not exist, only local GPUs will be used if they are available"
}

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 1 ]; then
        Usage
        exit 1
fi

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

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

if [ ! -f $GPUMachineFile ]; then
	UseRemoteMachine=false
else
	UseRemoteMachine=true
	localMachine=`hostname `
	machines=`grep -w on $GPUMachineFile | cut -f1 -d' ' `
	for machine in $machines
	do
        	if [ $localMachine == "$machine" ]; then
                	UseRemoteMachine=false
                	break;
        	fi
	done
	if [[ -z "${machines}" ]]; then
                UseRemoteMachine=false
        fi

fi

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

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


if $UseRemoteMachine; then
        remoteAccount=`grep -w on $GPUMachineFile | shuf | head -1 |cut -f1 -d' '`
	echo "WARNING: there is no local GPU or local GPU does not have sufficient memory. Try to predict properties from $inputFeature at $remoteAccount..."
	$DL4PropertyPredHome/Scripts/PredictPropertyRemote.sh $options -d $ResultDir -r $remoteAccount -g $GPU $inputFeature
	if [ $? -ne 0 ]; then
		echo "ERROR: failed to run $DL4PropertyPredHome/Scripts/PredictPropertyRemote.sh $options -d $ResultDir -r $remoteAccount -g $GPU $inputFeature"
		exit 1
	fi
else
	$DL4PropertyPredHome/Scripts/PredictPropertyLocal.sh $options -d $ResultDir -g $GPU $inputFeature
	if [ $? -ne 0 ]; then
		echo "ERROR: failed to run $DL4PropertyPredHome/Scripts/PredictPropertyLocal.sh $options -d $ResultDir -g $GPU $inputFeature"
		exit 1
	fi
fi
