AMBER:对单个复合物进行分子动力学模拟的python包(cpu并行版本)

最近在试着把md的标准过程写成python包,闲话少说上脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# =============================================================================
# 对于单个复合物进行md流程
# =============================================================================
# -*- coding: utf-8 -*-

"""
Created on Sat Mar 28 20:52:49 2020

@author: recherHE
@Email:[email protected]
"""

import os, sys



def write_file(output_file, outline):
	buffer = open(output_file, 'w')
	buffer.write(outline)
	buffer.close()


def protein_prepare(protein_file):
	'''
   this script is used for protein preparation.
   '''
	protein_name = os.path.basename(protein_file).split('.')[0]
	cmdline = 'pdb4amber -i %s.pdb -o %s_pre.pdb -y' % (protein_name, protein_name)
	os.system(cmdline)
    

def ligand_prepare(ligand_file):
    '''
    check ligand structrue and calculate charge
    '''
    ligand_name = os.path.basename(ligand_file).split('.')[0]
    cmdline = 'reduce %s.pdb> %s_h.pdb &&' %(ligand_name,ligand_name)
    cmdline += 'antechamber -i %s_h.pdb -fi pdb -o %s.mol2 -fo mol2 -c bcc &&' % (ligand_name,ligand_name)
    cmdline += 'parmchk2 -i %s.mol2 -f mol2 -o %s.frcmod' % (ligand_name, ligand_name)
    
    os.system(cmdline)

def tleap(res_name,lig_name):
    '''
    produce compounds promtop and inpcrd file for md
    '''
    
    outline = 'source oldff/leaprc.ff03\n'
    outline += 'source leaprc.gaff\n'
    outline += 'loadamberparams %s.frcmod\n' % (lig_name)
    outline += 'res=loadpdb %s_pre.pdb\n' % (res_name)
    outline += 'lig=loadmol2 %s.mol2\n' % (lig_name)
    outline += 'comp-now=combine{res lig}\n'
    outline += 'saveamberparm res %s.prmtop %s.inpcrd\n' % (res_name,res_name)
    outline += 'saveamberparm lig %s.prmtop %s.inpcrd\n' % (lig_name,lig_name)
    outline += 'saveamberparm comp-now %s_complex-now.prmtop %s_complex-now.inpcrd\n' % (lig_name,lig_name)
    outline += 'comp=combine{res lig}\n'
    outline += 'addions comp Cl- 0\n'
    outline += 'addions comp Na+ 0\n'
    outline += 'charge comp\n'
    outline += 'set default PBradii mbondi2\n'
    outline += 'solvatebox comp TIP3PBOX 12.0\n'
    outline += 'saveamberparm comp %s_complex.prmtop %s_complex.inpcrd\n' % (lig_name,lig_name)
    outline += 'savepdb comp %s_complex.pdb\n'% (lig_name)
    outline += 'quit'
    write_file('tleap.in', outline)
    
    
    cmdline = 'tleap -f tleap.in'
    os.system(cmdline)
        
    


def get_res_num(lig_name):
	'''
    get total residue number from leap file
    '''
	
	filelines = open('%s_complex.pdb'% (lig_name),'r').readlines()
	i = 0
	while filelines[i].strip() != 'TER':
		i = i + 1
	res_num = filelines[i + 1].split()[4]
	return res_num

def minimization(mpi_num,lig_name):
	'''
	perform minimization
	'''
	res_total_num = get_res_num(lig_name)
	
	outline1 = '''01_min_restrain.in: minimization with Cartesian restraints
 &cntrl
  imin = 1, maxcyc = 5000, ncyc = 2500,
  cut = 10.0, ntb = 1
  ntc = 2, ntf = 2,
  ntpr = 100,
  ntr = 1, restraintmask = ':1-%s&!@H=', restraint_wt = 5.0
 /
'''%res_total_num
	write_file('01_min_solv.in', outline1)
	
	outline2 = '''02_min_restrain.in: minimization with Cartesian restraints
 &cntrl
  imin = 1, maxcyc = 5000, ncyc = 2500,
  cut = 10.0, ntb = 1
  ntc = 2, ntf = 2,
  ntpr = 100,
  ntr = 1, restraintmask = ':1-%s@CA | :%s', restraint_wt = 4.0
 /
'''%(int(res_total_num)-1, res_total_num)	
	write_file('02_min_sidechain.in', outline2)
	
	outline3 = '''03_min_restrain.in: minimization with Cartesian restraints
 &cntrl
  imin = 1, maxcyc = 5000, ncyc = 2500,
  cut = 10.0, ntb = 1
  ntc = 2, ntf = 2,
  ntpr = 100,
 /
'''	
	write_file('03_min_all.in', outline3)	
	
	# cmdline = 'cd %s &&' % name
	cmdline = 'mpirun -np %s pmemd.MPI -O-i 01_min_solv.in -o 01_min_solv.out -p %s_complex.prmtop -c %s_complex.inpcrd -r 01_min_solv.rst -ref %s_complex.inpcrd -inf 01_min_solv.mdinfo -x 01_min_solv.nc &&' % (
            mpi_num,lig_name,lig_name,lig_name)
	cmdline += 'mpirun -np %s pmemd.MPI -O -i 02_min_sidechain.in -o 02_min_sidechain.out -p %s_complex.prmtop -c 01_min_solv.rst -r 02_min_sidechain.rst -ref 01_min_solv.rst -inf 02_min_sidechain.mdinfo -x 02_min_sidechain.nc &&'% (
            mpi_num,lig_name)
	cmdline += 'mpirun -np %s pmemd.MPI -O -i 03_min_all.in -o 03_min_all.out  -p %s_complex.prmtop -c 02_min_sidechain.rst -r 03_min_all.rst -ref 02_min_sidechain.rst -inf 03_min_all.mdinfo -x 03_min_all.nc &&'% (
            mpi_num,lig_name)
	cmdline += 'ambpdb -p %s_complex.prmtop -c 03_min_all.rst > %s_complex_after_min.pdb '% (
            lig_name,lig_name)
	os.system(cmdline)	

def heat(mpi_num,lig_name):
    res_total_num = get_res_num(lig_name)
    
    outline = '''04_heat_100ps.in: 100ps of heat equilibration
 &cntrl
  imin = 0, irest = 0, ntx = 1,
  nstlim = 50000, dt = 0.002,
  ntc = 2, ntf = 2,
  cut = 8.0, ntb = 1,
  ntpr = 2000, ntwx = 2000,
  ntt = 3, gamma_ln = 2.0,
  tempi = 0.0, temp0 = 300.0,
  ntr = 1, restraintmask = ':1-%s&!@H=', restraint_wt = 3.0,
  nmropt = 1,
  iwrap = 1, ioutfm = 1,
 /
 &wt TYPE='TEMP0', istep1=0, istep2=25000,
  value1=0.1, value2=300.0, /
 &wt TYPE='END' /
''' % (res_total_num)	
	
    write_file('04_heat_100ps.in', outline)	
    cmdline = 'mpirun -np %s pmemd.MPI -O -i 04_heat_100ps.in -o 04_heat_100ps.out -p %s_complex.prmtop -c 03_min_all.rst -r 04_heat_100ps.rst -ref 03_min_all.rst -inf 04_heat_100ps.mdinfo -x 04_heat_100ps.nc ' % (
            mpi_num,lig_name)
    os.system(cmdline)

def density(mpi_num,lig_name):
	res_total_num = get_res_num(lig_name)
	
	outline = '''05_density_100ps.in: 100 ps of density equilibration
 &cntrl
  imin = 0, irest = 1, ntx = 5,
  nstlim = 50000, dt = 0.002,
  ntc = 2, ntf = 2,
  cut = 8.0, ntb = 2, ntp = 1, taup = 1.0,
  ntpr = 2000, ntwx = 2000,
  ntt = 3, gamma_ln = 2.0,
  temp0 = 300.0,
  ntr = 1, restraintmask = ':1-%s&!@H=', restraint_wt = 2.0,
  iwrap = 1, ioutfm=1,
 /
'''%res_total_num	
	write_file('05_density_100ps.in', outline)		
	cmdline = 'mpirun -np %s pmemd.MPI -O -i 05_density_100ps.in -o 05_density_100ps.out -p %s_complex.prmtop -c 04_heat_100ps.rst -r 05_density_100ps.rst -ref 04_heat_100ps.rst -inf 05_density_100ps.mdinfo -x 05_density_100ps.nc ' % (
            mpi_num,lig_name)
	os.system(cmdline)
    
def equilibrium(mpi_num,lig_name):
	
	outline = '''06_equil_100ps.in: 100ps of constant pressure equilibration at 300K
 &cntrl
  imin = 0, irest = 1, ntx = 5,
  nstlim = 50000, dt = 0.002,
  ntc = 2, ntf = 2,
  cut = 8.0, ntb = 2, ntp = 1, taup = 2.0,
  ntpr = 2000, ntwx = 2000,
  ntt = 3, gamma_ln = 2.0,
  temp0 = 300.0,
  iwrap = 1, ioutfm=1,
 /
'''
	write_file('06_equil_100ps.in', outline)	
	cmdline = 'mpirun -np %s pmemd.MPI -O -i 06_equil_100ps.in -o 06_equil_100ps.out -p %s_complex.prmtop -c 05_density_100ps.rst -r 06_equil_100ps.rst -ref 05_density_100ps.rst -inf 06_equil_100ps.mdinfo -x 06_equil_100ps.nc ' % (
            mpi_num,lig_name)
	os.system(cmdline)
    
def prod(lig_name):
    outline = '''07_prod.in: 20ns of md production
 &cntrl
  imin = 0, irest = 1, ntx = 5,
  nstlim = 10000000, dt = 0.002,
  ntc = 2, ntf = 2,
  cut = 8.0, ntb = 2, ntp = 1, taup = 2.0,
  ntpr = 5000, ntwx = 5000, ntwr=10000000,
  ntt = 3, gamma_ln = 2.0, ig = -1,
  temp0 = 300.0,
  iwrap = 1, ioutfm = 1,
 /
'''
    write_file('07_prod.in', outline)	
    cmdline = 'cd %s &&' % lig_name
    cmdline += 'mpirun -np %s pmemd.MPI -O -i 07_prod.in -o 07_prod.out -p %s_complex.prmtop -c 06_equil_100ps.rst -r 07_prod.rst -ref 06_equil_100ps.rst -inf 07_prod.mdinfo -x prod.nc ' % (
            mpi_num,lig_name)
    os.system(cmdline)
    
def main(protein_file,ligand_file,mpi_num):
    
    #protein_prepare(protein_file)
    #ligand_prepare(ligand_file)
    
    res_name = os.path.basename(protein_file).split('.')[0]
    lig_name = os.path.basename(ligand_file).split('.')[0]
    #tleap(res_name,lig_name)
    
    minimization(mpi_num,lig_name)
    heat(mpi_num,lig_name)
    density(mpi_num,lig_name)
    equilibrium(mpi_num,lig_name)
#    prod(lig_name)
    
if '__main__' == __name__:
    protein_file = sys.argv[1]
    ligand_file = sys.argv[2]
    mpi_num = sys.argv[3]
    main(protein_file, ligand_file,mpi_num)
三个变量分别为:
1.protein_file:受体的pdb文件路径
2.ligand_file: 配体小分子的文件路径
3. mpi_num:并行使用核数

你可能感兴趣的:(分子动力学)