BuildFeatures/GenDistFeatures4OneProtein.sh
#!/bin/sh

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

if [[ -z "${DistFeatureHome}" ]]; then
        echo "ERROR: Please set environmental variable DistFeatureHome to the installation folder of BuildFeatures "
        exit 1
fi

gpu=-1
numAllowedJobs=2
#GPUMachineFile=$DistFeatureHome/params/GPUMachines.txt
GPUMachineFile=$ModelingHome/params/GPUMachines.txt
GPUmode=2

function Usage
{
	echo $0 "[ -g gpu | -n numJobs | -r machineMode | -h MachineFile ] targetName ContactDir"
	echo "	This script generates distance features for one protein, which may have multiple MSAs generated by different methods and settings"
	echo "	targetName: the protein name"
	echo "	ContactDir: a folder for all MSA files generated by BuildMSAs.sh, e.g., 1pazA_OUT/1pazA_contact/"
	echo "	     It shall be writable and contain subfolders targetName_uce3, targetName_ure3, targetName_uce3_meta ..."
	echo "	     Each subfolder shall contain an a3m file"
	echo "	The results will be saved to new subfolders in ContactDir named after feat_targetName_uce0, feat_targetName_uce3... "
	echo " "
	echo "	-g: -1(default), 0-3. If -1, select one GPU automatically"
	echo "	-n: the number of MSA files to be run simultaneously, default $numAllowedJobs"
	echo "	     Note that these jobs may compete for the same GPU, setting gpu=-1 will allow use of more than 1 GPUs if available"
	echo "	-r: specifiy what kind of CPUs and GPUs to use, default $GPUmode"
        echo "	     1: use local GPUs if available"
        echo "	     2: use local GPUs and CPUs"
        echo "	     3: use local GPUs and GPUs of machines defined by the -h option"
        echo "	     4: use local GPUs/CPUs and GPUs of machines defined by the -h option"
        echo "	-h: a file specifying remote machines with GPUs, default $GPUMachineFile"
	echo "		Remote GPUs will not be used if this file does not exist or is empty"
}

while getopts ":g:n:r:h:" opt; do
        case ${opt} in
        g )
        	gpu=$OPTARG
        	;;
	n )
		numAllowedJobs=$OPTARG
		;;
	h )
                GPUMachineFile=$OPTARG
                ;;
        r )
                GPUmode=$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

targetName=$1

ContactDir=$2
if [ ! -d $ContactDir ]; then
	echo "ERROR: invalid folder for distance feature generation: $ContactDir "
	exit 1
fi

if [ ! -w $ContactDir ]; then
	echo "ERROR: non-writable folder: $ContactDir "
	exit 1
fi

cmd=`readlink -f $0 `
cmdDir=`dirname $cmd`
program=$cmdDir/GenDistFeaturesFromMSA.sh
if [ ! -x $program ]; then
	echo "ERROR: non-executable program $program"
	exit 1
fi

keywords=`basename $program`
myself=`basename $0 `

for method in user uce3 uce5 ure3 ure5 uce3_meta uce5_meta ure3_meta ure5_meta
do
	if [ ! -d "$ContactDir/${targetName}_${method}" ]; then
		continue
	fi

	if [ ! -f "$ContactDir/${targetName}_${method}/${targetName}.a3m" ]; then
		continue
	fi

	while true
	do
                numRunningJobs=`ps -x | grep ${keywords} | grep $targetName | grep -v ${myself} | wc -l`
                if [ $numRunningJobs -lt $numAllowedJobs ]; then
                	$program -o $ContactDir/feat_${targetName}_${method} -g $gpu -r $GPUmode -h $GPUMachineFile $ContactDir/${targetName}_${method}/$targetName.a3m &
			sleep 2
                        break
                else
                        a=`expr $RANDOM % 4 `
                        sleep $a
                fi
	done
done

wait
