#!/bin/bash
#
# Code by  venkentom@gmail.com
#
# Licenced by the GPL General Public License
# Have a look at:
# http://www.gnu.org/copyleft/gpl.html
# For more information about the GPL License
#
# I am not responsible for what this code does to your hw/sw/mind
# Use at your own risk
# 

#Put gromacs commands after this line

DIR=`pwd`

#Config
NPROCS=2

# !! *** same procedure as in TM31_sol, now with polarized ff *** !!

# used TM31 created in moe, then sed chain name in pymol using 
#		alter (all),chain='A'
#		sort 
# Problem with protonated Aspartate. Is not supported, but is basically the same as a cysteine residue.
# Therefore mutated aa form of TM31C to TM31C_D14C
# then make martini topology on labtop: 
# ./martinize.py -f TM31C_D14C_E19C.pdb -o system_vaccum.top -x TM31C_D14C_E19C_pol_CG.pdb -dssp /Applications/DSSP_MAC.EXE -p backbone -ff martini22p
# then modified both TM31C_D14C_E19C_pol_CG.pdb to TM31CproDproE_pol_CG.pdb and Protein_A.itp by changing Cys into Asp0 and Glu0 settings

editconf -f TM31CproDproE_pol_CG.pdb -o editconf.pdb -bt cubic -d 1 -c

#change martini.itp to #include "martini_v2.2.itp" in system_vaccum.top
grompp -f minimization_vaccum.mdp -p system_vaccum.top -c editconf.pdb -o minimization_vaccum.tpr -maxwarn 20
mdrun -deffnm minimization_vaccum -v

#after minimization, rotate to align with z-axis membrane
editconf -f minimization_vaccum.gro -princ -o minimization_vaccum_rotate.gro -rotate 0 80 0

#and put into membrane (allow large box to accomodate 31 residue peptide)
./insane.py -f minimization_vaccum_rotate.gro -o system.gro -pbc optimal -box 6.9,6.9,11 -l POPC:7 -l CHOL:3 -u POPC:7 -u CHOL:3 -center -sol PW

#ensure following ratio in system.gro!
#POPC            53
#CHOL            23
#POPC            53
#CHOL            23

#due to ASP0 and GLU0, system is not neutral, so let's neutralize:
grompp -f genion.mdp -c system.gro -p system.top -o system_neutral.tpr
genion -s system_neutral.tpr -o system_neutral.gro -p system.top -pname NA- -nname CL- -neutral -conc 0.000001 << EOL
PW
EOL

# index file necessary to use as input with grompp, POPC_CHOL needs to be specified as one temperature coupling group, mdp files were modified accordingly
# also correct ASP0 and GLU0, need to be part of Protein
# finally, PW and CL- need to be combined as well
make_ndx -f system_neutral.gro -o index.ndx <<EOL
del 17-23
r1-31
del 1
name 18 Protein
14 | 15
16 | 17
q
EOL

grompp -f minimization.mdp -c system_neutral.gro -p system.top -o minimization.tpr
mdrun -deffnm minimization -v -nt 4

grompp -f equilibration.mdp -c minimization.gro -p system.top -o equilibration.tpr -n index.ndx -maxwarn 1
mdrun -deffnm equilibration -v -nt 4

grompp -f dynamic.mdp -c equilibration.gro -p system.top -o dynamic.tpr  -t equilibration.trr -n  index.ndx
mdrun -deffnm dynamic -v -nt 4


rm \#*
