Folding/Scripts4Rosetta/FoldNRelaxOneTarget.sh
#!/bin/sh

savefolder=`pwd`
numModels=120
alpha=1.61

# the maximum number of jobs that can be run simultaneously
numAllowedJobs=`nproc --all`

runningMode=0
UsePerturbation=false

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

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

function Usage 
{
	echo $0 "[-d savefolder | -n numModels | -c numCPUs | -r runningMode | -a alpha | -p ] seqFile predictedPairInfo [predictedPropertyInfo]"
	echo "	This script folds a protein (and optionally relaxes the initial models) using predicted distance/orientation/angles"
	echo "	seqFile: the primary sequence file in FASTA format"
	echo "	predictedPairInfo: a Rosetta constraint file generated by GenRosettaPotential.sh or a PKL file for predicted distance/orientation info"
	echo "	predictedPropertyInfo: could be empty, a string 'cst' or a PKL file for predicted Phi/Psi angles"
	echo "	     when empty or 'cst', predictedPairInfo shall be a Rosetta constraint file instead of a PKL file"
	echo "	     Otherwise, both predictedPairInfo and predictedPropertyInfo shall be PKL files"
	echo "	-n: the number of models to be generated, default $numModels"
	echo "	-c: the number of CPUs to be used, default $numAllowedJobs"
	echo "	-d: the folder for result saving, default current work directory"
	echo "	-r: 0 (default) or 1; if 0, fold only, otherwise fold+relax"
	echo "	-p: use perturbation in the folding stage, default No"
	echo "	-a: alpha value for DFIRE potential, default 1.61. If > 20, then a random alpha will be used"
}

while getopts ":n:c:d:r:a:p" opt; do
        case ${opt} in
                n )
                  numModels=$OPTARG
                  ;;
                c )
                  numAllowedJobs=$OPTARG
                  ;;
                d )
                  savefolder=$OPTARG
                  ;;
		r )
		  runningMode=$OPTARG
		  ;;
		a )
		  alpha=$OPTARG
		  ;;
		p )
		  UsePerturbation=true
		  ;;
                \? )
                  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 [ $runningMode -ne 0 -a $runningMode -ne 1 ]; then
	echo "ERROR: running mode can only be 0 or 1 "
	exit 1
fi

if [ $# -ne 3 -a $# -ne 2 ]; then
	Usage
	exit 1
fi

inFile=$1
if [ ! -f $inFile ]; then
	echo "ERROR: invalid input file for folding $inFile"
	exit 1
fi
target=`basename $inFile`
target=`echo $target | cut -f1 -d'.' `

pairMatrixFile=$2
if [ ! -f $pairMatrixFile ]; then
	echo "ERROR: invalid file for Rosetta constraints or predicted distance/orientation info: $pairMatrixFile"
	exit 1
fi

if [ $# -eq 3 ]; then
	propertyFile=$3
else
	propertyFile="cst"
fi

if [ "$propertyFile" != "cst" -a ! -f $propertyFile ]; then
	echo "ERROR: invalid file for predicted property: $propertyFile"
	exit 1
fi

#program=${DistanceFoldingHome}/Scripts4Rosetta/GenPotentialNFoldRelax.sh
program=${cmdDir}/GenPotentialNFoldRelax.sh
if [ ! -x $program ]; then
	echo "ERROR: invalid exectuable program $program"
	exit 1
fi
keywords=`basename $program`

i=0
while [ $i -lt $numModels ];
do
        while true
        do
                numRunningJobs=`ps -x | grep ${keywords} | wc -l  `
                if [ $numRunningJobs -lt $numAllowedJobs ]; then
			if $UsePerturbation; then
                        	$program -d $savefolder -r $runningMode -a $alpha -p $inFile $pairMatrixFile $propertyFile &
			else
                        	$program -d $savefolder -r $runningMode -a $alpha $inFile $pairMatrixFile $propertyFile &
			fi
			sleep 2
                        break
		else
                	a=`expr $RANDOM % 4 `
                	sleep $a
                fi
        done
        i=`expr $i + 1`
done

wait
