DL4DistancePrediction4/Scripts/PredictPairRelationRemote.sh
#!/bin/sh

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

#DeepModelFile=$DL4DistancePredHome/params/ModelFile4PairwisePred.txt
#ModelName=EC47C37C19CL99S35V2020MidModels
DeepModelFile=""
ModelName=""
GPU=-1
RemoteAccount="raptorx7.uchicago.edu"

alignmentType=0

function Usage {
        echo $0 "[ -T alignmentType | -f DeepModelFile | -m ModelName | -d ResultDir | -r remoteAccount | -g gpu ] proteinName inputFolder"
	echo "	This script predicts distance and orientation from MSAs and optionally templates using a GPU on a remote machine"
	echo "	proteinName: the protein name"
	echo "	inputFolder: a folder generated by BuildFeatures.sh, e.g., 1pazA_OUT/, that shall contain all needed features including proteinName_contact, proteinName_thread and proteinName.seq"
	echo " "
	echo "	-T: indicate which query-template alignments to be used: 0 for none (default), 1 for alignments generated by HHpred and 2 for alignments generated by RaptorX threading"
        echo "		When this option is 0, predict distance/orientation from only sequence info such as MSAs"
        echo "		When not 0, predict distance/orientation matrices from both MSAs and sequence-template alignments"
	echo "		The query-template alignments shall be available at inputFolder/HHP and inputFolder/DeepThreader, respectively"
        echo "	-f: a file containing some deep learning model files, default empty"
	echo "		Note that all the file paths shall be valid on the remote machine in order to work"
	echo "		when this option 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 in order to work"
	echo "		when this option is not used, the default model name on the remote machine will be used depending on alignment type"
	echo ""
        echo "	-d: the folder for result saving, default current work directory"
        echo "	-r: a remote machine or account with GPUs, e.g., jinbo@$RemoteAccount or $RemoteAccount"
        echo "		Please make sure that you can ssh and scp to this remote account without password"
        echo "	-g: -1 (default), 0-3. If -1, automatically select a GPU"
}

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

inputFolder=$2
if [ ! -d $inputFolder ]; then
	echo "ERROR: the folder for input features does not exist: $inputFolder"
	exit 1
fi

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

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

RemoteWorkDir="tmpWorkDir4RemoteDistancePrediction-${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 -r $inputFolder $RemoteAccount:$RemoteWorkDir/
if [ $? -ne 0 ]; then
	echo "ERROR: failed to scp $inputFolder to $RemoteWorkDir at $RemoteAccount!"
	exit 1
fi

RemoteResultDir=$RemoteWorkDir/`basename $ResultDir`

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

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

if [ $alignmentType -ne 0 ]; then
	options=$options" -T $alignmentType "
fi

bname=`basename $inputFolder`
ssh -o StrictHostKeyChecking=no $RemoteAccount "\$DL4DistancePredHome/Scripts/PredictPairRelationLocal.sh $options ${target} $RemoteWorkDir/$bname"
if [ $? -ne 0 ]; then
	echo "ERROR: failed to run PredictPairRelationLocal.sh $options ${target} $RemoteWorkDir/$bname at $RemoteAccount!"
	exit 1
fi

scp -r $RemoteAccount:$RemoteResultDir/*.* $ResultDir/
if [ $? -ne 0 ]; then
	echo "ERROR: failed to scp prediction results from the remote account $RemoteAccount to $ResultDir "
	exit 1
fi

ssh -o StrictHostKeyChecking=no $RemoteAccount "rm -rf $RemoteWorkDir"
