#!/bin/bash
if [ -z "$OSTYPE" ]; then
      if which cygpath 1>/dev/null 2>/dev/null; then
              OSTYPE=cygwin
      fi
fi
mypath=$0
while [ -h "$mypath" ]; do  # Resolve symlinks
        ls=`ls -ld "$mypath"`
        link=`expr "$ls" : '.*-> \(.*\)$'`
        if expr "$link" : '\/' > /dev/null; then
                mypath="$link"
        else
                mypath=`dirname "$mypath"`/"$link"
        fi
done

if expr "$mypath" : '[^\/]' > /dev/null; then
	mypath="`pwd`/$mypath"
fi

case "$OSTYPE" in
cygwin*)
	dir="`dirname $mypath`"
	dir="`cygpath -w $dir`"
        JCHEMHOME="$dir\\..\\.."
        ;;
*)
        dir=`dirname $mypath`
        JCHEMHOME="$dir/../.."
        ;;
esac


echo
echo "Usage: prescreen <target file> <actives file> <nr. of test sets>"
echo "Target and actives can be chosen from the directory"
echo "'examples/molecules', e.g.:"
echo "prescreen nci500.smiles beta2_adrenoceptor_antagonists.smiles 5"
echo
echo 
echo "This example performs the following tasks given number times:"
echo "it generates the required test sets for parameter optimization"
echo "and evaluation of different parametrized metrics of two molecular descriptors"
echo "(pharmacophore fingerprints and chemical fingerprints). Then it performs" 
echo "the optimization of parameters and evaluation of the performance of the"
echo "generated parametrized metrics for each set. These metrics are used in" 
echo "screening a set of molecules against a set of known actives. Three"
echo "optimizations are performed with different goal functions: selectivity"
echo "effectiveness, asymmetric selectivity effectiveness and enrichment."
echo
echo 
echo "Results are written into the files i-<goal-function>-example.stat in"
echo "the working directory; where i stands for the index of test set."
echo
echo
for (( i = 1; i <= $3; i=i+1 ));
do
############################
#Generate random test files
############################
   $JCHEMHOME/examples/bin/randomms $1 opt-target hit-target -n 50 -v;
   $JCHEMHOME/examples/bin/randomms $2 hit-test opt-actives -p 35 -v;
   $JCHEMHOME/examples/bin/randomms opt-actives opt-test opt-query -p 50 -v;
#######################################
#Optimize for SelectivityEffectiveness
#######################################
   $JCHEMHOME/bin/optimizemetrics \
   opt-target opt-test opt-query -e 3 -H -v -f SelectivityEffectiveness \
     -k CF -c $JCHEMHOME/examples/config/cfp.xml -o opt-CF.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotoa Tanimoto -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
     -k PF -o opt-PF.xml -c $JCHEMHOME/examples/config/pharma-frag.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotos Tanimoto -s \
       -M Tanimotoa Tanimoto -a \
       -M Tanimotosa Tanimoto -s -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
       -M Euclideanw Euclidean -w \
       -M Euclideanwan Euclidean -w -a -n \
     -k PF -o opt-PF-z0.3.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.3 \
       -M Tanimotot0.3 Tanimoto \
       -M Tanimotos0.3 Tanimoto -s \
       -M Tanimotoa0.3 Tanimoto -a \
       -M Tanimotosa0.3 Tanimoto -s -a \
       -M Euclideant0.3 Euclidean \
       -M Euclideann0.3 Euclidean -n \
       -M Euclideana0.3 Euclidean -a \
       -M Euclideanw0.3 Euclidean -w \
       -M Euclideanwan0.3 Euclidean -w -a -n \
     -k PF -o opt-PF-z0.7.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.7 \
       -M Tanimotot0.7 Tanimoto \
       -M Tanimotos0.7 Tanimoto -s \
       -M Tanimotoa0.7 Tanimoto -a \
       -M Tanimotosa0.7 Tanimoto -s -a \
       -M Euclideant0.7 Euclidean \
       -M Euclideann0.7 Euclidean -n \
       -M Euclideana0.7 Euclidean -a \
       -M Euclideanw0.7 Euclidean -w \
       -M Euclideanwan0.7 Euclidean -w -a -n ;
#
   $JCHEMHOME/bin/hitstatistics \
   hit-target hit-test opt-query -o $i-SE-example.stat -e 3 -g -v -H -b \
     -k PF -c opt-PF.xml \
     -k PF -c opt-PF-z0.3.xml \
     -k PF -c opt-PF-z0.7.xml \
     -k CF -c opt-CF.xml;
##################################################
#Optimize for Asymmetric SelectivityEffectiveness
##################################################
   $JCHEMHOME/bin/optimizemetrics \
   opt-target opt-test opt-query -e 3 -H -v -f SelectivityEffectiveness 0.3 \
     -k CF -c $JCHEMHOME/examples/config/cfp.xml -o opt-CF.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotoa Tanimoto -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
     -k PF -o opt-PF.xml -c $JCHEMHOME/examples/config/pharma-frag.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotos Tanimoto -s \
       -M Tanimotoa Tanimoto -a \
       -M Tanimotosa Tanimoto -s -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
       -M Euclideanw Euclidean -w \
       -M Euclideanwan Euclidean -w -a -n \
     -k PF -o opt-PF-z0.3.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.3 \
       -M Tanimotot0.3 Tanimoto \
       -M Tanimotos0.3 Tanimoto -s \
       -M Tanimotoa0.3 Tanimoto -a \
       -M Tanimotosa0.3 Tanimoto -s -a \
       -M Euclideant0.3 Euclidean \
       -M Euclideann0.3 Euclidean -n \
       -M Euclideana0.3 Euclidean -a \
       -M Euclideanw0.3 Euclidean -w \
       -M Euclideanwan0.3 Euclidean -w -a -n \
     -k PF -o opt-PF-z0.7.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.7 \
       -M Tanimotot0.7 Tanimoto \
       -M Tanimotos0.7 Tanimoto -s \
       -M Tanimotoa0.7 Tanimoto -a \
       -M Tanimotosa0.7 Tanimoto -s -a \
       -M Euclideant0.7 Euclidean \
       -M Euclideann0.7 Euclidean -n \
       -M Euclideana0.7 Euclidean -a \
       -M Euclideanw0.7 Euclidean -w \
       -M Euclideanwan0.7 Euclidean -w -a -n ;
#
   $JCHEMHOME/bin/hitstatistics \
   hit-target hit-test opt-query -o $i-SE-asymmetric-example.stat -e 3 -g -v -H -b -l 0.3 \
     -k PF -c opt-PF.xml \
     -k PF -c opt-PF-z0.3.xml \
     -k PF -c opt-PF-z0.7.xml \
     -k CF -c opt-CF.xml;
##########################
# Optimize for Enrichment
##########################
   $JCHEMHOME/bin/optimizemetrics \
   opt-target opt-test opt-query -e 3 -H -v -f Enrichment \
     -k CF -c $JCHEMHOME/examples/config/cfp.xml -o opt-CF.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotoa Tanimoto -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
     -k PF -o opt-PF.xml -c $JCHEMHOME/examples/config/pharma-frag.xml \
       -M Tanimotot Tanimoto \
       -M Tanimotos Tanimoto -s \
       -M Tanimotoa Tanimoto -a \
       -M Tanimotosa Tanimoto -s -a \
       -M Euclideant Euclidean \
       -M Euclideann Euclidean -n \
       -M Euclideana Euclidean -a \
       -M Euclideanw Euclidean -w \
       -M Euclideanwan Euclidean -w -a -n \
     -k PF -o opt-PF-z0.3.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.3 \
       -M Tanimotot0.3 Tanimoto \
       -M Tanimotos0.3 Tanimoto -s \
       -M Tanimotoa0.3 Tanimoto -a \
       -M Tanimotosa0.3 Tanimoto -s -a \
       -M Euclideant0.3 Euclidean \
       -M Euclideann0.3 Euclidean -n \
       -M Euclideana0.3 Euclidean -a \
       -M Euclideanw0.3 Euclidean -w \
       -M Euclideanwan0.3 Euclidean -w -a -n \
     -k PF -o opt-PF-z0.7.xml -c $JCHEMHOME/examples/config/pharma-frag.xml -z 0.7 \
       -M Tanimotot0.7 Tanimoto \
       -M Tanimotos0.7 Tanimoto -s \
       -M Tanimotoa0.7 Tanimoto -a \
       -M Tanimotosa0.7 Tanimoto -s -a \
       -M Euclideant0.7 Euclidean \
       -M Euclideann0.7 Euclidean -n \
       -M Euclideana0.7 Euclidean -a \
       -M Euclideanw0.7 Euclidean -w \
       -M Euclideanwan0.7 Euclidean -w -a -n;
#
   $JCHEMHOME/bin/hitstatistics \
   hit-target hit-test opt-query -o $i-E-example.stat -e 3 -g -v -H -b -f \
     -k PF -c opt-PF.xml \
     -k PF -c opt-PF-z0.3.xml \
     -k PF -c opt-PF-z0.7.xml \
     -k CF -c opt-CF.xml;
################################################
done

echo 
echo "Example is complete. Results are written into the files" 
echo "i-<goal-function>-example.stat"
echo "in the working directory; where i stands for the index of test set."
echo

