#!/bin/bash
# Set variables for running RxDock
export CMDOCK_ROOT=/var/www/dockit/cmdock
export PATH=/var/www/dockit/cmdock/bin:/var/www/dockit/cmdock/bin/legacy:$PATH
export LD_LIBRARY_PATH=$CMDOCK_ROOT/lib:$LD_LIBRARY_PATH
export PERL5LIB=$CMDOCK_ROOT/lib:$PERL5LIB

#export RBT_ROOT=/var/www/html/dockit/CmDock 
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$RBT_ROOT/lib 
#export PATH=$PATH:$RBT_ROOT/bin 

# Create temporary directory 
mkdir data/$2
cd data/$2

echo $3
echo $3 >> tmp.smi

# Move SMILES to temporary directory
# mv ../../tmp.smi .

# Convert SMILES to ionized form of 3D structures
python3 /var/www/dockit/gy/run_gypsum_dl.py --source tmp.smi --min_ph 6.0 --max_ph 8.0 --pka_precision 1 --max_variants_per_compound 1 --use_durrant_lab_filters

# Copy target files to temporary directory
cp /var/www/dockit/t/$1/TARGET* .

# Run rxdock
cmdock -i gypsum_dl_success.sdf -o gypsum_out2.sd -r TARGET_$1.prm -p dock.prm -n 15

# Collect and prepare results for analysis
sdsort -n -f'SCORE.INTER' gypsum_out2.sd > temp2.sdf
sdreport -c temp2.sdf > temp2.csv

# Calculate desktiptors with Openbabel
obabel temp2.sdf -osdf -O temp.sdf --add "MW HBA1 HBA2 HBD logP  MR formula cansmi rotors bonds abonds TPSA InChi InChiKey atoms"

# Fill SQL database with data
#python3 ../../FILL_DB_DOCKIT.py $1 $2 'crtomir'

# Delete all target files
rm TARGET*

