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

if [ $# -lt 1 ]; then
        echo $0 modelFolder [savefile]
	echo "modelFolder: a folder for 3D models generated by RosettaCM"
	echo "savefile: the file for result saving, default is the basename of modelFolder followed by .score"
        exit 1 
fi

if [[ -z "$DistanceFoldingHome" ]]; then
	echo "ERROR: please set environmental variable DistanceFoldingHome, e.g., $HOME/RaptorX-3DModeling/Folding"
	exit 1
fi

modelFolder=$1
if [ ! -d $modelFolder ]; then
	echo "ERROR: $modelFolder is not a valid folder!"
	exit 1
fi

scorefile=`basename $modelFolder`.score
if [ $# -ge 2 ]; then
	scorefile=$2
fi

cp /dev/null $scorefile
for i in $modelFolder/*.pdb
do
	if [ ! -f $i ]; then
		continue
	fi
	## col 16 is the atom pair constraint score and col 23 is the total score
        scores=` grep ^pose $i | cut -f16,23 -d' ' `
	numResidues=` grep CtermProteinFull $i | cut -f1 -d' ' | cut -f2 -d':' | cut -f2 -d'_'`
	#echo $scores $weights $numResidues

	## claculate per-residue score
        avg=` echo $scores $numResidues | awk '{a=$1/$3; b=$2/$3} END {printf "%.2f %.2f ", a, b}' `
        echo $i $avg >> $scorefile
        #echo $i $sum $scores >> $scorefile
done

normalize=$DistanceFoldingHome/Helpers/Normalize.py

if [ -s $scorefile ]; then
	processID=$$	
	sort -k2,2 -g $scorefile > $scorefile.$processID
	python $normalize $scorefile.$processID $scorefile
	rm -f $scorefile.$processID
	echo "All model scores are saved to $scorefile"
fi

