#PBS -N test_server
#PBS -q nowy
#PBS -l nodes=4:ppn=4

#-----------------------------------------------------------------------------
setenv UNRES_BIN /users2/czarek/UNRES/run/ADAM/unres-mult-symetr_KCC_ifort_MPICH-NEWCORR-SAXS-NMRAMB-Bfac.exe
#-----------------------------------------------------------------------------
setenv DD /users2/czarek/UNRES/PARAM
setenv BONDPAR $DD/bond_AM1_ext_dum.parm
setenv THETPAR $DD/theta_opt.parm.OPT_TRP1_FSD_Villin_E0L_QHK_N9L_LX7_BDD_I18
setenv THETPARPDB $DD/thetaml_ext.5parm
setenv ROTPARPDB $DD/scgauss_ext.parm
setenv ROTPAR $DD/rotamers_AM1_aura_ext.10022007.parm
setenv TORPAR $DD/torsion_abinitio.parm-2d-all-DL-03-02-2cos
setenv TORDPAR $DD/pot_tord_G631_DIL_ext.parm
setenv ELEPAR $DD/electr_631Gdp_ext.parm
setenv SIDEPAR $DD/scinter_GB_ext_lip.parm
setenv FOURIER $DD/fourier_opt.parm.OPT_TRP1_FSD_Villin_E0L_QHK_N9L_LX7_BDD_I18
setenv SCCORPAR $DD/sccor_am1_pawel_ext.dat
setenv SCPPAR $DD/scp_ext.parm
setenv PATTERN $DD/patterns.cart
setenv LIPTRANPAR $DD/Lip_tran_initial_ext.parm
#-----------------------------------------------------------------------------
cd $PBS_O_WORKDIR

setenv MPIRUN "/users2/local/mpich2-1.4.1p1_intel/bin/mpirun "
set NPROCS=`cat $PBS_NODEFILE | wc -l`

setenv FGPROCS 2
setenv POT GB
setenv PREFIX file
setenv OUT1FILE YES

#UNRES MREMD
$MPIRUN -machinefile $PBS_NODEFILE -np $NPROCS $UNRES_BIN

#WHAM
setenv WHAM_BIN /users2/czarek/UNRES/run/ADAM/wham_ifort_KCC_MPICH-okeanos_NEWCORR-SAXS-NMRAMB-Bfac.exe
setenv CONTFUNC GB
setenv SIDEP $DD/contact_ext.3.parm
setenv SCRATCHDIR .
setenv PREFIX file_wham

$MPIRUN -machinefile $PBS_NODEFILE -np 2 $WHAM_BIN

#test images
setenv line `awk '{}END{print int(NR*0.2)}' file_GB000.stat`

if ( `grep -c pdbref file.inp` ) then
 tail -q -n +$line file_GB*.stat |  awk '{if ( NF == 14 ) print}' >remd_all.stat
 cat file_GB*.stat |  awk '{if ( NF == 14 ) print}' > remd_all0.stat
else
 tail -q -n +$line file_GB*.stat |  awk '{if ( NF == 10 ) print}' >remd_all.stat
 cat file_GB*.stat |awk '{if ( NF == 10 ) print}' > remd_all0.stat
endif
../files/matplotlib_hist.py $temperatures
rm remd_all.stat remd_all0.stat

#CLUSTER WHAM
setenv INPUT file_cluster
setenv INTIN file_wham
setenv OUTPUT file_cluster
setenv PDB CART
setenv COORD CX
setenv PRINTCOOR PRINT_PDB
setenv CLUSTER_WHAM_BIN /users2/czarek/UNRES/run/ADAM/unres_clustMD-mult_ifort_MPICH_NEWCORR-SAXS-MRAMB-bfac.exe

$MPIRUN -machinefile $PBS_NODEFILE -np 2 $CLUSTER_WHAM_BIN

if `awk '{cap=toupper($0); if (cap!=$0) {print 1;exit}}' file.seq` then
#no allatom conversion/refinementkx for proteins with D-aminoacids
 ln -s file_wham_T*K_ave.pdb ave
 sed -n '/ENERGY/,/ENDMDL/p' ave >tmp.pdb
 setenv numstruc `grep ENERGY tmp.pdb|wc -l`
 setenv allline `cat tmp.pdb|wc -l`
 setenv onestruc `echo $allline "/" $numstruc| bc -l|sed 's/\.0*//'`
 foreach i (2 4 6 8 10)
   setenv headval `echo $i "*" $onestruc|bc -l`
   setenv halfi `echo $i/2|bc`
   head -n $headval tmp.pdb | tail -n $onestruc >MODEL${halfi}.pdb
 end
 rm ave tmp.pdb
else
# proteins with L-aminoacids only
ln -s file_wham_T*K_ave.pdb ave
../files/cluster2allatom.sh ave
rm ave

#amber min refinement
foreach i (`seq 1 5`)
../files/refine.sh MODEL$i.pdb $ssbond
end
endif

/users2/local/pymol_1.6/pymol -c ../files/model.pml

if ( `grep -c pdbref file.inp` ) then
 awk '{printf "%s%s%s\n",substr($0,0,21)," ",substr($0,23)}' plik.pdb > tmp.pdb
 if ( -f "plik1ter.pdb" ) then
  grep -v TER plik1ter.pdb | awk '{printf "%s%s%s\n",substr($0,0,21)," ",substr($0,23)}' > tmp.pdb
  /users2/local/mmtsb/perl/convpdb.pl -setchain A -renumber 1 -out generic_noh plik.pdb |grep ATOM > plik1chain.pdb
  /users2/local/mmtsb/perl/convpdb.pl -setchain B -renumber 1 -out generic_noh plik2.pdb |grep ATOM >> plik1chain.pdb  
  foreach i (`seq 1 5`)
    sed /TER/q MODEL$i.pdb_ > tmp1.pdb
    /users2/local/mmtsb/perl/convpdb.pl -setchain A -renumber 1 -out generic_noh tmp1.pdb |grep ATOM > MODEL${i}chain.pdb    
    sed '0,/TER/d' MODEL$i.pdb_ > tmp2.pdb
    /users2/local/mmtsb/perl/convpdb.pl -setchain B -renumber 1 -out generic_noh tmp2.pdb |grep ATOM >> MODEL${i}chain.pdb    
    rm tmp1.pdb tmp2.pdb MODEL$i.pdb_
    ../files/DockQ.py MODEL${i}chain.pdb plik1chain.pdb > dockq_$i.out
  end
 endif
 /users2/local/mmtsb/perl/convpdb.pl -renumber 1 -out generic tmp.pdb > plik1.pdb
 rm tmp.pdb

 /users2/local/bin/tmscore MODEL1.pdb plik1.pdb > tmscore1.out
 /users2/local/bin/tmscore MODEL2.pdb plik1.pdb > tmscore2.out
 /users2/local/bin/tmscore MODEL3.pdb plik1.pdb > tmscore3.out
 /users2/local/bin/tmscore MODEL4.pdb plik1.pdb > tmscore4.out
 /users2/local/bin/tmscore MODEL5.pdb plik1.pdb > tmscore5.out
 if (-e saxs.data) then
  awk -f ../files/saxs_dist.awk plik1.pdb > plik_saxs.data
 endif
# rm plik1.pdb
endif

if (-e saxs.data) then
awk -f ../files/saxs_dist.awk MODEL1.pdb > MODEL1_saxs.data
awk -f ../files/saxs_dist.awk MODEL2.pdb > MODEL2_saxs.data
awk -f ../files/saxs_dist.awk MODEL3.pdb > MODEL3_saxs.data
awk -f ../files/saxs_dist.awk MODEL4.pdb > MODEL4_saxs.data
awk -f ../files/saxs_dist.awk MODEL5.pdb > MODEL5_saxs.data
../files/plot_saxs.py
endif

#END
touch finished
