#!/bin/bash


FRAMES=20
CORES=5

TRAJ="50ns_noWAT_5000frames_wATP_hept_1.nc"
PRMTOP="1XCK_hept_MGATP_noWAT.prmtop"

PRMTOP_LIG="1XCK_chainA_MGATP_noWAT.prmtop"
INPCRD_LIG="1XCK_chainA_MGATP_noWAT.inpcrd"

PRMTOP_REC=""
INPCRD_REC=""

LIG_MASK=":1-524,3669-3670"
REC_MASK=":525-3668,3671-3682"

LIG_PTRAJ="_lig.ptraj"
REC_PTRAJ="_rec.ptraj"

LIG_MDCRD="_lig.mdcrd"
REC_MDCRD="_rec.mdcrd"

for j in `seq 1 7`;
do
	LIG_ENRGY="lig.results.chain_"${j}

	let prot_end=524*${j}
	let prot_start=${prot_end}-524+1
	let atp_end=${j}*2+3668
	let atp_start=${atp_end}-1

	LIG_MASK=":"${prot_start}"-"${prot_end}","${atp_start}"-"${atp_end}

	for i in `seq 1 ${CORES}`;
	do
		let frms=${FRAMES}/${CORES}
	        let end=${frms}*${i}
        	let start=${end}-${frms}+1

		rm ${LIG_PTRAJ}
        	echo 'trajin' ${TRAJ} ${start} ${end} 1 >> ${LIG_PTRAJ}
		echo "strip '* & ! "${LIG_MASK}"'" >> ${LIG_PTRAJ}
        	echo 'trajout' ${LIG_MDCRD}.${i} 'nobox' >> ${LIG_PTRAJ}
	        ptraj ${PRMTOP} ${LIG_PTRAJ} 
	done

	for i in `seq 1 ${CORES}`;
	do
		$AMBERHOME/bin/sander -O -i gb.in -o _lig.out.${i} -p ${PRMTOP_LIG} -c ${INPCRD_LIG} -y ${LIG_MDCRD}.${i} &
	done
	
	wait

	touch tmp
	for i in `seq 1 ${CORES}`;
	do
		cat _lig.out.${i} tmp > tmp2
		mv tmp2 tmp
	done
	mv tmp ${LIG_ENRGY}
done

