#!/bin/csh -f

# Provide names of the pdb files
set protein1 = 1UDI_A.pdb
set protein2 = 1UDI_B.pdb

# Provide number of steps in milions
set nstep = 0.5

# Provide number of clusters
set nclust = 10

# Provide temperature of clustering
set temper = 300

# Provide UNRES instalation pathway
set UNRES = /users2/vetinari/unres_hom_m/unres/



################################################################################
################################################################################
################################################################################
sed -e "s=UUU=$UNRES=" start_unres_example.mat > start_unres.mat
sed -e "s=UUU=$UNRES=" start_wham_example.mat > start_wham.mat
sed -e "s=UUU#=$UNRES=" start_cluster_example.mat > start_cluster.mat


# Generation of initial orientations
echo "Generation of initial orientations"
echo "TER" > TER
gfortran -O3 generator_v13b.f -o generator_v13b
./generator_v13b $protein1 $protein2 0 > log
grep -v "TER" $protein1 > prot1
grep -v "TER" $protein2 > prot2
cat prot1 "TER" prot2 > reference.pdb
rm TER prot1 prot2


################################################################################
# Setting up and running MREMD
echo "Setting up and running MREMD"
set nstep2 = `echo $nstep \* 1000000 | bc | awk -F "." '{print $1}'`
set box = `grep -A1 "Boxsize (suggested)" log | tail -1 | cut -c 3-8`
sed "s/boxxx/$box/g" unres_example.inp > unres_dock.inp
sed "s/NNN/$nstep2/g" unres_dock.inp > unres_dock.in
mv unres_dock.in unres_dock.inp

#qsub start_unres.mat



################################################################################
# Setting up and running WHAM analysis
echo "Setting up and running WHAM analysis"
#set resnum = `grep -A 1 "PDB data will be read from file" dimer.out_GB000 | tail -1 | awk '{print $2}'`
#set resnum = `echo $resnum + 5 | bc`

#grep -A $resnum "PDB data will be read from file" dimer.out_GB000 | grep -B 9999 "nsup=" | egrep -v "PDB|Nres:|nsup=" | awk '{print $3}' > seq_raw

egrep "CA|TER" reference.pdb > temp.pdb
set res1 = `grep -B 9999 "TER" temp.pdb | grep -v TER | grep -c CA`
set res2 = `grep -A 9999 "TER" temp.pdb | grep -v TER | grep -c CA`
set start1 = `head -1 temp.pdb | awk '{print $4}'`
set end2 = `tail -1 temp.pdb | awk '{print $4}'`
set start2 = `grep -A 1 TER temp.pdb | tail -1 | awk '{print $4}'`
set end1 = `grep -B 1 TER temp.pdb | head -1 | awk '{print $4}'`

echo First protein has $res1 residues, while second molecule has $res2 residues.
echo $start1 $end1 $start2 $end2

rm seq_raw

if ($start1 != GLY) then
echo D > seq_raw
endif

head -$res1 temp.pdb | awk '{print $4}' >> seq_raw

if ($end1 != GLY) then
echo D >> seq_raw
endif

if ($start2 != GLY) then
echo D >> seq_raw
endif

tail -$res2 temp.pdb | awk '{print $4}' >> seq_raw

if ($end2 != GLY) then
echo D >> seq_raw
endif


set resnum = `grep -c "." seq_raw`

echo System contains $resnum residues including dummy atoms.

set i = 1
set j = 1
set seqq = ""
rm seq
while ($i <= $resnum)

set aa = `head -$i seq_raw | tail -1`
if ($aa == D) then
set seqq = ${seqq}X
else if ($aa == ALA) then
set seqq = ${seqq}A
else if ($aa == ARG) then
set seqq = ${seqq}R
else if ($aa == ASN) then
set seqq = ${seqq}N
else if ($aa == ASP) then
set seqq = ${seqq}D
else if ($aa == CYS) then
set seqq = ${seqq}C
else if ($aa == GLU) then
set seqq = ${seqq}E
else if ($aa == GLN) then
set seqq = ${seqq}Q
else if ($aa == GLY) then
set seqq = ${seqq}G
else if ($aa == HIS) then
set seqq = ${seqq}H
else if ($aa == ILE) then
set seqq = ${seqq}I
else if ($aa == LEU) then
set seqq = ${seqq}L
else if ($aa == LYS) then
set seqq = ${seqq}K
else if ($aa == MET) then
set seqq = ${seqq}M
else if ($aa == PHE) then
set seqq = ${seqq}F
else if ($aa == PRO) then
set seqq = ${seqq}P
else if ($aa == SER) then
set seqq = ${seqq}S
else if ($aa == THR) then
set seqq = ${seqq}T
else if ($aa == TRP) then
set seqq = ${seqq}W
else if ($aa == TYR) then
set seqq = ${seqq}Y
else if ($aa == VAL) then
set seqq = ${seqq}V
endif

if ($j == 80) then
echo $seqq >> seq
set seqq = ""
set j = 0
endif

set i = `echo $i + 1 | bc`
set j = `echo $j + 1 | bc`
end
echo $seqq >> seq
rm seq_raw

set range1 = `echo $nstep2 / 2500 | bc | awk -F "." '{print $1}'`
set range1 = `echo $range1 \* 24 | bc`
set div = `echo 12000 \* 10 / 24 | bc | awk -F "." '{print $1}'`
set range0 = `echo $range1 - $div | bc | awk -F "." '{print $1}'`
if ($range0 < 0 ) then
set range0 = 10
endif

#echo $nstep2 $div
echo WHAM will analyze range of snapshots from $range0 to $range1.

sed "s/xxxxx/$box/g" wham_example.inp > wham.inp
sed "s/XXX/$resnum/g" wham.inp > wham
head -4 wham > wham.in
grep -B 1 -A 999 "HOMOL_DIST" wham_example.inp > wham
cat wham.in seq wham > wham.inp
sed "s/BBB/$range0/g" wham.inp > wham
sed "s/CCC/$range1/g" wham > wham.inp
rm wham wham.in
#qsub start_wham.mat


################################################################################
# Setting up and running cluster analysis
echo "Setting up and running cluster analysis"

sed "s/XXX/$resnum/g" cluster_example.inp > clust
head -8 clust > clust.in
grep -B 1 -A 999 "HOMOL_DIST" cluster_example.inp > clus
cat clust.in seq clus > cluster.inp
sed "s/nclust=ZZZ/nclust=$nclust/g" cluster.inp > clut
sed "s/temper=AAA/temper=$temper/g" clut > cluster.inp
rm clust clust.in clus
#qsub start_cluster.mat
