#!/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

cd $JCHEMHOME

echo
echo "This example demonstrates virtual screening of a compound library stored"
echo "in a relational database table."
echo "If an RDMS other than Derby is used, variable DB have to be"
echo "set in this script."
echo "========================================================================"


# Derby example (keep this line if not RDMS set up in chemaxon\.jchem is used, otherwise comment them out)
export DB="--driver org.apache.derby.jdbc.EmbeddedDriver --dburl jdbc:derby:$JCHEMHOME/examples/output/derbyDB;create=true"

# MySQL example
# export DB="--driver com.mysql.jdbc.Driver --dburl jdbc:mysql://localhost/cdexample -l dbuser -p dbpasswd"

# keep this line if setting in .chemaxon/.jchem are used
# export DB=

#Removing old descriptor table
bin/generatemd d nci1000 nci1000pfp $DB > /dev/null 2> /dev/null

#removing old structure table
bin/jcman d nci1000 $DB > /dev/null 2> /dev/null

echo
echo "Creates table nci1000 and uploads structures from file nci1000.smiles."

bin/jcman c nci1000 $DB 

bin/jcman a nci1000 examples/molecules/nci1000.smiles $DB

echo
echo "Creates a descriptor table nci1000pfp in the database, then generates "
echo "pharmacophore fingerprints and stores them in the nci1000pfp table."

bin/generatemd c -a nci1000 -k PF nci1000pfp -c examples/config/pharma-frag.xml $DB

echo 
echo "This pharmacophore fingerprint table will be screened for beta2 adrenoceptor "
echo "antagonists. To make this search efficient, dissimilarity metrics will "
echo "be optimized for beta2 antagonists. "

bin/optimizemetrics examples/molecules/opt-beta2-target.smiles examples/molecules/beta2-training.smiles examples/molecules/beta2-query.smiles \
    -k PF -c examples/config/pharma-frag.xml -H Median -o examples/output/opt-beta2.xml -l \
    -M Euclideant Euclidean \
    -M Euclideann Euclidean -n \
    -M Euclideanwn Euclidean -w -n \
    -M Euclideana Euclidean -a \
    -M Euclideanwa Euclidean -w -a \
    -M Euclideanan Euclidean -a -n \
    -M Euclideanwan Euclidean -w -a -n 

echo 
echo "Optimization completed. Optimized metrics will be validated now."

bin/hitstatistics examples/molecules/valid-beta2-target.smiles examples/molecules/beta2-spikes.smiles examples/molecules/beta2-query.smiles \
    -k PF -c examples/output/opt-beta2.xml \
    -M Euclideant Euclideann Euclideanwn Euclideana Euclideanwa Euclideanan Euclideanwan \
    -e 3 -H Median -b

echo
echo "Metrics performing the best are: weighted asymmetric Euclidean and weighted normalized asymmetric Euclidean" 
echo "these are added to the screening configuration stored in the database."

bin/generatemd a nci1000 nci1000pfp "weighted asymmetric Euclidean optimized for beta2"  PharmacophoreFingerprintEuclideanwa.xml $DB
bin/generatemd a nci1000 nci1000pfp "weighted asymmetric normalized Euclidean optimized for beta2"  PharmacophoreFingerprintEuclideanwan.xml $DB

echo
echo "Compound library will be screened now for beta2 analogs using the optimized"
echo "weighted asymmetric normalized Euclidean dissimilarity metric."

bin/screenmd examples/molecules/beta2-query.smiles -a nci1000 -k nci1000pfp -M Euclideanwan -o sdf examples/output/hits.sdf $DB

echo
echo "Screening is completed. Hits will be displayed in MarvinView."

bin/mview examples/output/hits.sdf 



