DL4PropertyPrediction/Scripts/PredictPropertyRemote.sh
#!/bin/sh

#if [[ -z "${DL4PropertyPredHome}" ]]; then
#        echo "ERROR: please set environmental variable DL4PropertyPredHome to the install folder of DL4PropertyPrediction"
#        exit 1
#fi

#DeepModelFile=$DL4PropertyPredHome/params/ModelFile4PropertyPred.txt
#ModelName=PhiPsiSet10820Models
DeepModelFile=""
ModelName=""
GPU=-1
RemoteAccount="raptorx7.uchicago.edu"
ResultDir=`pwd`

function Usage {
        echo $0 "[ -f DeepModelFile | -m ModelName | -d ResultDir | -r remoteAccount | -g gpu ] inputFeature_PKL"
        echo "	This script predicts structure properties from a feature file in PKL format using a remote machine with GPUs"
        echo "	inputFeature: the input feature file in PKL format generated by CollectPropertyFeatures.sh"
	echo " "
        echo "	-f: a file containing a list of deep learning model files, default empty"
	echo "          Note that all the file paths shall be valid on the remote machine. Otherwise it may not work"
        echo "          when this file is not used, the default DeepModelFile on the remote machine will be used"
        echo "	-m: a model name defined in DeepModelFile representing a set of deep learning models, default empty"
	echo "          Note that this name shall be valid on the remote machine. Otherwise it may not work"
        echo "          when this option is not used, the default model name on the remote machine will be used"
        echo "	-d: the folder for result saving, default current work directory"
	echo "	-r: a remote computer or account with GPUs, e.g., RaptorX@$RemoteAccount or $RemoteAccount"
	echo "		Please make sure that you can ssh and scp to this account without password"
        echo "	-g: -1 (default), 0-3. If -1, automatically select a GPU"
}

while getopts ":f:m:d:r:g:" opt; do
        case ${opt} in
                f )
                  DeepModelFile=$OPTARG
                  ;;
                m )
                  ModelName=$OPTARG
                  ;;
                d )
                  ResultDir=$OPTARG
                  ;;
		r )
		  RemoteAccount=$OPTARG
		  ;;
                g )
                  GPU=$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

pid=$$
localMachine=`hostname | cut -f1 -d'.' `

target=`basename $inputFeature .propertyFeatures.pkl`
RemoteWorkDir="tmpWorkDir4RemotePropertyPrediction-${target}-$localMachine-$pid"

ssh -o StrictHostKeyChecking=no $RemoteAccount "mkdir -p $RemoteWorkDir"
if [ $? -ne 0 ]; then
        echo "ERROR: failed to create $RemoteWorkDir at $RemoteAccount"
        exit 1
fi

scp $inputFeature $RemoteAccount:$RemoteWorkDir/
bname=`basename $inputFeature`

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

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

ssh -o StrictHostKeyChecking=no $RemoteAccount "\$DL4PropertyPredHome/Scripts/PredictPropertyLocal.sh $options -d $RemoteWorkDir -g $GPU $RemoteWorkDir/$bname "
if [ $? -ne 0 ]; then
        echo "ERROR: failed to run PredictPropertyLocal.sh $options -d $RemoteWorkDir -g $GPU $RemoteWorkDir/$bname at $RemoteAccount"
        exit 1
fi

if [ ! -d $ResultDir ]; then
	mkdir -p $ResultDir
fi
scp $RemoteAccount:$RemoteWorkDir/${target}.predictedProperties.pkl $ResultDir/
if [ $? -ne 0 ]; then
	echo "ERROR: failed to scp $RemoteAccount:$RemoteWorkDir/${target}.predictedProperties.pkl $ResultDir/"
	exit 1
fi

ssh -o StrictHostKeyChecking=no $RemoteAccount "rm -rf $RemoteWorkDir"
if [ $? -ne 0 ]; then
        echo "ERROR: failed to delete $RemoteWorkDir at $RemoteAccount"
        exit 1
fi
