コード例 #1
0
def run(in_parms):
    """
  Run a GROMACS simulations using the PDBREMIX parms dictionary.
  """
    parms = copy.deepcopy(in_parms)
    basename = parms['output_basename']

    # Copies across topology and related *.itp files, with appropriate
    # filename renaming in #includes
    top = basename + '.top'
    in_top = parms['topology']
    shutil.copy(in_top, top)
    in_name = os.path.basename(in_top).replace('.top', '')
    in_dir = os.path.dirname(in_top)
    file_tag = "%s/%s_*itp" % (in_dir, in_name)
    new_files = [top]
    for f in glob.glob(file_tag):
        new_f = os.path.basename(f)
        new_f = new_f.replace(in_name, basename)
        shutil.copy(f, new_f)
        new_files.append(new_f)
    for f in new_files:
        replace_include_file(f, in_name + "_", basename + "_")

    # Copy over input coordinates/velocities
    in_gro = basename + '.in.gro'
    shutil.copy(parms['input_crds'], in_gro)

    # Generates a postiional-restraint topology file
    if parms['restraint_pdb']:
        # 1kcal*mol*A**-2 = 4.184 kJ*mol*(0.1 nm)**-2
        kcalmolang2_to_kJmolnm2 = 400.184
        open(basename + '_posre.itp', 'w').write(
            make_restraint_itp(
                parms['restraint_pdb'],
                parms['restraint_force'] * kcalmolang2_to_kJmolnm2))

    # Generate .mdp file based on parms
    in_mdp = basename + '.grompp.mdp'
    open(in_mdp, 'w').write(make_mdp(parms))

    # Now run .grompp to generate this .tpr file
    tpr = basename + '.tpr'
    # .mdp to save complete set of parameters
    mdp = basename + '.mdrun.mdp'
    data.binary(
        'grompp',
        '-f %s -po %s -c %s -p %s -o %s' \
            % (in_mdp, mdp, in_gro, top, tpr),
        basename + '.grompp')
    util.check_files(tpr)

    # Run simulation with the .tpr file
    data.binary('mdrun', '-v -deffnm %s' % (basename), basename + '.mdrun')
    top, crds, vels = get_restart_files(basename)
    util.check_output(top)
    util.check_output(crds)

    # Cleanup
    delete_backup_files(basename)
コード例 #2
0
def run_pymol_script(pml, width=500, height=500):
  is_quit = 'quit' in util.words_in_file(pml)
  if is_quit:
    pymol_batch = data.binary("pymol_batch")
    cmd = pymol_batch + ' -c '
  else:
    pymol = data.binary("pymol")
    cmd = pymol + " -q " # no splash screen
  cmd += " -W %d -H %d " % (width, height)
  cmd += pml
  util.run_with_output(cmd)
コード例 #3
0
def check_dcd_byte_order(dcd):
  """
  Uses flipdcd to DCD trajectories have matching OS endianess.
  """
  if sys.byteorder in 'big':
    option = '-B'
  elif sys.byteorder in 'little':
    option = '-L'
  else:
    raise Exception("Couldn't figure out system byte order %s" % sys.byteorder)
  data.binary('flipdcd', '%s %s' % (option, dcd), dcd+'.flipdcd')
コード例 #4
0
ファイル: gromacs.py プロジェクト: autodataming/pdbremix
def neutralize_system_with_salt(
    in_top, in_gro, basename, force_field):
  """
  Takes a .top file and adds counterions to neutralize the overall
  charge of system, and saves to `basename.gro`.
  """
  # Calculate overall charege in the .top file
  qtot = sum([q for mass, q, chain in read_top(in_top)])
  counter_ion_charge = -int(round(qtot))
  if counter_ion_charge == 0:
    shutil.copy(in_gro, basename + '.gro')
    return

  # Create a .tpr paramater file for genion to find low-energy sites
  in_mdp = basename + '.salt.grompp.mdp'
  open(in_mdp, 'w').write(ions_mdp + force_field_mdp)
  top = basename + '.top'
  if in_top != top:
    shutil.copy(in_top, top)
  tpr = basename + '.salt.tpr'
  out_mdp = basename + '.mdp'
  data.binary(
      'grompp',
      '-f %s -po %s -c %s -p %s -o %s' \
            % (in_mdp, out_mdp, in_gro, top, tpr),
      basename + '.salt.grompp')
  util.check_files(tpr)

  # Use genion to generate a gro of system with counterions 
  gro = basename + '.gro'
  # Genion requires user input "SOL" to choose solvent for replacement
  input_fname = basename + '.salt.genion.in'
  open(input_fname, 'w').write('SOL')
  # Different versions of Gromacs use different counterions
  charge_str = ""
  if 'GROMACS4.5' in force_field:
    charge_str = " -pname NA -nname CL "
  elif 'GROMACS4.0' in force_field:
    charge_str = " -pname NA+ -nname CL- "
  else:
    raise ValueError, "Cannot recognize force_field " + force_field
  if counter_ion_charge > 0:
    charge_str += " -np %d " % counter_ion_charge
  else:
    charge_str += " -nn %d " % abs(counter_ion_charge)
  log = basename + '.salt.genion.log'
  data.binary(
      'genion', 
      '-g %s -s %s -o %s -p %s -neutral %s' % \
          (log, tpr, gro, top, charge_str),
      basename + '.salt.genion',
      input_fname)
  util.check_files(gro)
コード例 #5
0
def neutralize_system_with_salt(in_top, in_gro, basename, force_field):
    """
  Takes a .top file and adds counterions to neutralize the overall
  charge of system, and saves to `basename.gro`.
  """
    # Calculate overall charege in the .top file
    qtot = sum([q for mass, q, chain in read_top(in_top)])
    counter_ion_charge = -int(round(qtot))
    if counter_ion_charge == 0:
        shutil.copy(in_gro, basename + '.gro')
        return

    # Create a .tpr paramater file for genion to find low-energy sites
    in_mdp = basename + '.salt.grompp.mdp'
    open(in_mdp, 'w').write(ions_mdp + force_field_mdp)
    top = basename + '.top'
    if in_top != top:
        shutil.copy(in_top, top)
    tpr = basename + '.salt.tpr'
    out_mdp = basename + '.mdp'
    data.binary(
        'grompp',
        '-f %s -po %s -c %s -p %s -o %s' \
              % (in_mdp, out_mdp, in_gro, top, tpr),
        basename + '.salt.grompp')
    util.check_files(tpr)

    # Use genion to generate a gro of system with counterions
    gro = basename + '.gro'
    # Genion requires user input "SOL" to choose solvent for replacement
    input_fname = basename + '.salt.genion.in'
    open(input_fname, 'w').write('SOL')
    # Different versions of Gromacs use different counterions
    charge_str = ""
    if 'GROMACS4.5' in force_field:
        charge_str = " -pname NA -nname CL "
    elif 'GROMACS4.0' in force_field:
        charge_str = " -pname NA+ -nname CL- "
    else:
        raise ValueError, "Cannot recognize force_field " + force_field
    if counter_ion_charge > 0:
        charge_str += " -np %d " % counter_ion_charge
    else:
        charge_str += " -nn %d " % abs(counter_ion_charge)
    log = basename + '.salt.genion.log'
    data.binary(
        'genion',
        '-g %s -s %s -o %s -p %s -neutral %s' % \
            (log, tpr, gro, top, charge_str),
        basename + '.salt.genion',
        input_fname)
    util.check_files(gro)
コード例 #6
0
def pdb_to_top_and_crds(force_field, pdb, basename, solvent_buffer=10.0): 
  """
  Creates CHARMM .coor and .psf file for NAMD simulation.
  """
  solv_dir = basename + '.solvate'
  save_dir = os.getcwd()

  pdb = os.path.abspath(pdb)

  util.goto_dir(solv_dir)

  # Remove all but protein heavy atoms in a single clean conformation
  stripped_pdb = basename + '.clean.pdb' 
  pdbtext.clean_pdb(pdb, stripped_pdb)

  # Make input script for psfgen
  psfgen_psf = basename+'.psfgen.psf'
  psfgen_pdb = basename+'.psfgen.pdb'
  script = module_load_script 
  script += make_chain_loading_script(stripped_pdb, basename)
  script += make_disulfide_script(stripped_pdb)
  script += write_script 
  script = script % {
    # load the included CHARMM2 atom topologies
    'topology': os.path.join(data.data_dir, 'charmm22.topology'),
    'out_pdb': psfgen_pdb,
    'out_psf': psfgen_psf
  }

  psfgen_in = basename+".psfgen.in"
  open(psfgen_in, "w").write(script)

  data.binary('psfgen', psfgen_in, basename+'.psfgen')
  util.check_output(psfgen_psf)
  util.check_output(psfgen_pdb)

  solvate_psf(psfgen_psf, psfgen_pdb, basename, solvent_buffer)

  psf = basename+'.psf'
  coor = basename+'.coor'
  pdb = basename+'.pdb'
  os.rename(pdb, coor)
  convert_restart_to_pdb(basename, pdb)
  shutil.copy(psf, save_dir)
  shutil.copy(coor, save_dir)
  shutil.copy(pdb, save_dir)
  
  os.chdir(save_dir)

  return psf, coor
コード例 #7
0
def solvate_psf(in_psf, in_pdb, basename, solvent_buffer=10.0):
  """
  Uses VMD to add explicit waters to a .psf topology file
  """
  parms = {
    'in_psf': in_psf,
    'in_pdb': in_pdb,
    'name': basename,
    'solvent_buffer': solvent_buffer,
  }
  tcl = basename + '.vmd.tcl'
  open(tcl, 'w').write(solvate_vmd_script % parms)
  data.binary('vmd', '-dispdev text -eofexit', basename+'.vmd', tcl)
  util.check_output(basename+'.vmd.pdb')
  util.check_output(basename+'.pdb')
コード例 #8
0
def run(in_parms):
  """
  Runs a NAMD simulation using the PDBREMIX in_parms dictionary.
  """
  parms = copy.deepcopy(in_parms)
  name = parms['output_basename']

  # load the included CHARMM2 energy parameters
  parms['parameter'] = os.path.join(data.data_dir, 'charmm22.parameter')
  parms['psf_type'] =  'paraTypeCharmm on'

  # copy over input xsc and topology files (same basename)
  xsc = parms['topology'].replace('.psf', '.xsc')
  if os.path.isfile(xsc):
    shutil.copy(xsc, name + '.in.xsc')
    parms['xsc'] = name + '.in.xsc'
  else:
    parms['xsc'] = ''
  shutil.copy(parms['topology'], name + '.psf')
  parms['topology'] = name + '.psf'

  # copy over coordinates
  shutil.copy(parms['input_crds'], name + '.in.coor')
  parms['input_crds'] = name + '.in.coor'

  # copy over velocities
  if 'input_vels' in parms and parms['input_vels']:
    shutil.copy(parms['input_vels'], name + '.in.vel')
    parms['input_vels'] = name + '.in.vel'
  else:
    parms['input_vels'] = ''

  # copy over restraint coordinates
  if 'restraint_pdb' in parms and parms['restraint_pdb']:
    shutil.copy(parms['restraint_pdb'], name + '.restraint.coor')
    parms['restraint_pdb'] = name + '.restraint.coor'
  else:
    parms['restraint_pdb'] = ''
    
  namd_in = name + ".namd2.in"
  open(namd_in, "w").write(make_namd_input_file(parms))
  
  data.binary('namd2', namd_in, name + '.namd2')

  top, crds, vels = get_restart_files(name)
  util.check_output(top)
  util.check_output(crds)
コード例 #9
0
def run(in_parms):
    """
  Run a AMBER simulations using the PDBREMIX in_parms dictionary.
  """
    parms = copy.deepcopy(in_parms)
    basename = parms['output_basename']

    # Copies across topology file
    input_top = parms['topology']
    util.check_files(input_top)
    new_top = basename + '.top'
    shutil.copy(input_top, new_top)

    # Copies over coordinate/velocity files
    input_crd = parms['input_crds']
    util.check_files(input_crd)
    if input_crd.endswith('.crd'):
        new_crd = basename + '.in.crd'
    else:
        new_crd = basename + '.in.rst'
    shutil.copy(input_crd, new_crd)

    # Decide on type of output coordinate/velocity file
    if 'n_step_minimization' in parms:
        rst = basename + ".crd"
    else:
        rst = basename + ".rst"

    # Construct the long list of arguments for sander
    trj = basename + ".trj"
    vel_trj = basename + ".vel.trj"
    ene = basename + ".ene"
    inf = basename + ".inf"
    sander_out = basename + ".sander.out"
    sander_in = basename + ".sander.in"
    args = "-O -i %s -o %s -p %s -c %s -r %s -x %s -v %s -e %s -inf %s" \
            % (sander_in, sander_out, new_top, new_crd, rst, trj, vel_trj, ene, inf)

    # Make the input script
    script = make_sander_input_file(parms)

    # If positional restraints
    if parms['restraint_pdb']:
        # Generate the AMBER .crd file that stores the constrained coordinates
        pdb = parms['restraint_pdb']
        soup = pdbatoms.Soup(pdb)
        ref_crd = basename + '.restraint.crd'
        write_soup_to_rst(soup, ref_crd)
        util.check_output(ref_crd)
        # Add the restraints .crd to the SANDER arguments
        args += " -ref %s" % ref_crd
        # Add the restraint forces and atom indices to the SANDER input file
        script += make_restraint_script(pdb, parms['restraint_force'])

    open(sander_in, "w").write(script)

    # Run the simulation
    data.binary('sander', args, basename)

    # Check if output is okay
    util.check_output(sander_out, ['FATAL'])
    top, crds, vels = get_restart_files(basename)
    util.check_output(top)
    util.check_output(crds)
コード例 #10
0
def run_tleap(force_field, pdb, name, solvent_buffer=0.0, excess_charge=0):
    """
  Generates AMBER topology and coordinate files from PDB.

  Depending on whether excess_charge is non-zero, will also generate
  counterions. If solvent_buffer is non-zero, will generate explicit
  waters, otherwise, no waters generated. No waters is used for
  implicit solvent simulations.
  """

    util.check_output(pdb)

    # Remove all but protein heavy atoms in a single clean conformation
    tleap_pdb = name + '.clean.pdb'
    pdbtext.clean_pdb(pdb, tleap_pdb)

    # The restart files to be generated
    top = name + '.top'
    crd = name + '.crd'

    # Dictionary to substitute into tleap scripts
    params = {
        'top': top,
        'crd': crd,
        'pdb': tleap_pdb,
        'data_dir': data.data_dir,
        'solvent_buffer': solvent_buffer,
    }

    # use best force-field for the 2 versions of AMBER author has tested
    if 'AMBER11' in force_field:
        params['amber_ff'] = "leaprc.ff99SB"
    elif 'AMBER8' in force_field:
        params['amber_ff'] = "leaprc.ff96"
    else:
        raise Exception("Don't know which version of AMBER(8|11) to use.")

    # make the tleap input script
    script = force_field_script
    # check for a few non-standard residue that have been included
    residues = [r.type for r in pdbatoms.Soup(tleap_pdb).residues()]
    if 'PHD' in residues:
        leaprc = open("%s/phd.leaprc" % data.data_dir).read()
        script += leaprc
    if 'ZNB' in residues:
        leaprc = open("%s/znb.leaprc" % data.data_dir).read()
        script += leaprc
    script += "pdb = loadpdb %(pdb)s\n"
    script += disulfide_script_and_rename_cysteines(tleap_pdb, tleap_pdb)
    if 'GBSA' not in force_field:
        # Add explicit waters as not GBSA implicit solvent
        if excess_charge != 0:
            # Add script to add counterions, must specify + or -
            if excess_charge > 0:
                script += "addions pdb Cl- 0\n"
            else:
                script += "addions pdb Na+ 0\n"
        solvent_buffer = 10
        params['solvent_buffer'] = solvent_buffer
        script += explicit_water_box_script
    script += save_and_quit_script
    script = script % params

    # Now write script to input file
    tleap_in = name + ".tleap.in"
    open(tleap_in, "w").write(script)

    # Now run tleap with tleap_in
    data.binary('tleap', "-f " + tleap_in, name + '.tleap')

    # Check output is okay
    if os.path.isfile('leap.log'):
        os.rename('leap.log', name + '.tleap.log')
    util.check_output(name + '.tleap.log', ['FATAL'])
    util.check_output(top)
    util.check_output(crd)

    return top, crd
コード例 #11
0
def pdb_to_top_and_crds(force_field, pdb, basename, solvent_buffer=10):
    """
  Converts a PDB file into GROMACS topology and coordinate files,
  and fully converted PDB file. These constitute the restart files
  of a GROMACS simulation.
  """
    util.check_files(pdb)
    full_pdb = os.path.abspath(pdb)
    save_dir = os.getcwd()

    # All intermediate files placed into a subdirectory
    util.goto_dir(basename + '.solvate')

    # Remove all but protein heavy atoms in a single clean conformation
    pdb = basename + '.clean.pdb'
    pdbtext.clean_pdb(full_pdb, pdb)

    # Generate protein topology in pdb2gmx_gro using pdb2gmx
    pdb2gmx_gro = basename + '.pdb2gmx.gro'
    top = basename + '.top'
    itp = basename + '_posre.itp'
    # Choose force field based on GROMACS version
    if 'GROMACS4.5' in force_field:
        ff = 'amber99'
    elif 'GROMACS4.0' in force_field:
        ff = 'G43a1'
    else:
        raise ValueError, "Couldn't work out pdb2gmx for " + force_field
    args = '-ignh -ff %s -water spc -missing -f %s -o %s -p %s -i %s -chainsep id_or_ter -merge all' \
            % (ff, pdb, pdb2gmx_gro, top, itp)
    data.binary('pdb2gmx', args, basename + '.pdb2gmx')
    util.check_files(pdb2gmx_gro)

    # Now add a box with editconf
    box_gro = basename + '.box.gro'
    solvent_buffer_in_nm = solvent_buffer / 10.0
    data.binary(
        'editconf',
        '-f %s -o %s -c -d %f -bt cubic' \
            % (pdb2gmx_gro, box_gro, solvent_buffer_in_nm),
        basename+'.box')
    util.check_files(box_gro)

    # Given box dimensions, can now populate with explict waters
    solvated_gro = basename + '.solvated.gro'
    data.binary(
        'genbox',
        '-cp %s -cs spc216.gro -o %s -p %s' \
            % (box_gro, solvated_gro, top),
         '%s.solvated' % basename)
    util.check_files(solvated_gro)

    # Neutralize with counterions using genion to place ions
    # based on energy parameters processed by grompp
    gro = basename + '.gro'
    neutralize_system_with_salt(top, solvated_gro, basename, force_field)
    util.check_files(gro)

    # Make a reference PDB file from restart files for viewing and restraints
    convert_restart_to_pdb(basename, basename + '.pdb')

    # Copy finished restart files back into original directory
    fnames = util.re_glob(
        '*',
        os.path.basename(basename) + r'[^\.]*\.(pdb|itp|gro|mdp|top)$')
    for fname in fnames:
        shutil.copy(fname, save_dir)

    # Cleanup
    delete_backup_files(basename)
    os.chdir(save_dir)

    return top, gro
コード例 #12
0
ファイル: amber.py プロジェクト: autodataming/pdbremix
def run(in_parms):
  """
  Run a AMBER simulations using the PDBREMIX in_parms dictionary.
  """
  parms = copy.deepcopy(in_parms)
  basename = parms['output_basename']

  # Copies across topology file
  input_top = parms['topology'] 
  util.check_files(input_top)
  new_top = basename + '.top'
  shutil.copy(input_top, new_top)

  # Copies over coordinate/velocity files
  input_crd = parms['input_crds']
  util.check_files(input_crd)
  if input_crd.endswith('.crd'): 
    new_crd = basename + '.in.crd'
  else:
    new_crd = basename + '.in.rst'
  shutil.copy(input_crd, new_crd)
  
  # Decide on type of output coordinate/velocity file
  if 'n_step_minimization' in parms:
    rst = basename + ".crd"
  else:
    rst = basename + ".rst"

  # Construct the long list of arguments for sander
  trj = basename + ".trj"
  vel_trj = basename + ".vel.trj"
  ene = basename + ".ene"
  inf = basename + ".inf"
  sander_out = basename + ".sander.out"
  sander_in = basename + ".sander.in"
  args = "-O -i %s -o %s -p %s -c %s -r %s -x %s -v %s -e %s -inf %s" \
          % (sander_in, sander_out, new_top, new_crd, rst, trj, vel_trj, ene, inf)

  # Make the input script
  script = make_sander_input_file(parms)

  # If positional restraints
  if parms['restraint_pdb']:
    # Generate the AMBER .crd file that stores the constrained coordinates
    pdb = parms['restraint_pdb']
    soup = pdbatoms.Soup(pdb)
    ref_crd = basename + '.restraint.crd'
    write_soup_to_rst(soup, ref_crd)
    util.check_output(ref_crd)
    # Add the restraints .crd to the SANDER arguments
    args += " -ref %s" % ref_crd
    # Add the restraint forces and atom indices to the SANDER input file
    script += make_restraint_script(pdb, parms['restraint_force'])

  open(sander_in, "w").write(script)

  # Run the simulation
  data.binary('sander', args, basename)

  # Check if output is okay
  util.check_output(sander_out, ['FATAL'])
  top, crds, vels = get_restart_files(basename)
  util.check_output(top)
  util.check_output(crds)
コード例 #13
0
ファイル: amber.py プロジェクト: autodataming/pdbremix
def run_tleap(
    force_field, pdb, name, solvent_buffer=0.0, excess_charge=0): 
  """
  Generates AMBER topology and coordinate files from PDB.

  Depending on whether excess_charge is non-zero, will also generate
  counterions. If solvent_buffer is non-zero, will generate explicit
  waters, otherwise, no waters generated. No waters is used for
  implicit solvent simulations.
  """

  util.check_output(pdb)

  # Remove all but protein heavy atoms in a single clean conformation
  tleap_pdb = name + '.clean.pdb'
  pdbtext.clean_pdb(pdb, tleap_pdb)

  # The restart files to be generated
  top = name + '.top'
  crd = name + '.crd'

  # Dictionary to substitute into tleap scripts
  params = { 
    'top': top, 
    'crd': crd, 
    'pdb': tleap_pdb,
    'data_dir':data.data_dir,
    'solvent_buffer': solvent_buffer,
  }

  # use best force-field for the 2 versions of AMBER author has tested
  if 'AMBER11' in force_field:
    params['amber_ff'] = "leaprc.ff99SB"
  elif 'AMBER14' in force_field:
    params['amber_ff'] = "leaprc.ff14SB"
  elif 'AMBER8' in force_field:
    params['amber_ff'] = "leaprc.ff96"
  else:
    raise Exception("Don't know which version of AMBER(8|11|14) to use.")

  # make the tleap input script
  script = force_field_script
  # check for a few non-standard residue that have been included 
  residues = [r.type for r in pdbatoms.Soup(tleap_pdb).residues()]
  if 'PHD' in residues:
    leaprc = open("%s/phd.leaprc" % data.data_dir).read()
    script += leaprc
  if 'ZNB' in residues:
    leaprc = open("%s/znb.leaprc" % data.data_dir).read()
    script += leaprc
  script += "pdb = loadpdb %(pdb)s\n"
  script += disulfide_script_and_rename_cysteines(tleap_pdb, tleap_pdb)
  if 'GBSA' not in force_field:
    # Add explicit waters as not GBSA implicit solvent
    if excess_charge != 0:
      # Add script to add counterions, must specify + or -
      if excess_charge > 0:
        script += "addions pdb Cl- 0\n"
      else:
        script += "addions pdb Na+ 0\n"
    solvent_buffer = 10
    params['solvent_buffer'] = solvent_buffer
    script += explicit_water_box_script
  script += save_and_quit_script
  script = script % params

  # Now write script to input file
  tleap_in = name + ".tleap.in"
  open(tleap_in, "w").write(script)

  # Now run tleap with tleap_in
  data.binary('tleap', "-f "+tleap_in, name+'.tleap')

  # Check output is okay
  if os.path.isfile('leap.log'):
    os.rename('leap.log', name + '.tleap.log')
  util.check_output(name+'.tleap.log', ['FATAL'])
  util.check_output(top)
  util.check_output(crd)

  return top, crd
コード例 #14
0
ファイル: gromacs.py プロジェクト: autodataming/pdbremix
def run(in_parms):
  """
  Run a GROMACS simulations using the PDBREMIX parms dictionary.
  """
  parms = copy.deepcopy(in_parms)
  basename = parms['output_basename']

  # Copies across topology and related *.itp files, with appropriate
  # filename renaming in #includes
  top = basename + '.top'
  in_top = parms['topology']
  shutil.copy(in_top, top)
  in_name = os.path.basename(in_top).replace('.top', '')
  in_dir = os.path.dirname(in_top)
  file_tag = "%s/%s_*itp" % (in_dir, in_name)
  new_files = [top]
  for f in glob.glob(file_tag):
    new_f = os.path.basename(f)
    new_f = new_f.replace(in_name, basename)
    shutil.copy(f, new_f)
    new_files.append(new_f)
  for f in new_files:
    replace_include_file(f, in_name + "_", basename + "_")

  # Copy over input coordinates/velocities
  in_gro = basename + '.in.gro'
  shutil.copy(parms['input_crds'], in_gro)

  # Generates a postiional-restraint topology file
  if parms['restraint_pdb']:
    # 1kcal*mol*A**-2 = 4.184 kJ*mol*(0.1 nm)**-2 
    kcalmolang2_to_kJmolnm2 = 400.184
    open(basename + '_posre.itp', 'w').write(
        make_restraint_itp(
            parms['restraint_pdb'], 
            parms['restraint_force'] * kcalmolang2_to_kJmolnm2))

  # Generate .mdp file based on parms
  in_mdp = basename + '.grompp.mdp'
  open(in_mdp, 'w').write(make_mdp(parms))

  # Now run .grompp to generate this .tpr file 
  tpr = basename + '.tpr'
  # .mdp to save complete set of parameters
  mdp = basename + '.mdrun.mdp'
  data.binary(
      'grompp',
      '-f %s -po %s -c %s -p %s -o %s' \
          % (in_mdp, mdp, in_gro, top, tpr),
      basename + '.grompp')
  util.check_files(tpr)

  # Run simulation with the .tpr file
  data.binary(
      'mdrun',
      '-v -deffnm %s' % (basename),
      basename + '.mdrun')
  top, crds, vels = get_restart_files(basename)
  util.check_output(top)
  util.check_output(crds)
  
  # Cleanup
  delete_backup_files(basename)
コード例 #15
0
ファイル: gromacs.py プロジェクト: autodataming/pdbremix
def pdb_to_top_and_crds(force_field, pdb, basename, solvent_buffer=10):
  """
  Converts a PDB file into GROMACS topology and coordinate files,
  and fully converted PDB file. These constitute the restart files
  of a GROMACS simulation.
  """
  util.check_files(pdb)
  full_pdb = os.path.abspath(pdb)
  save_dir = os.getcwd()

  # All intermediate files placed into a subdirectory
  util.goto_dir(basename + '.solvate')

  # Remove all but protein heavy atoms in a single clean conformation
  pdb = basename + '.clean.pdb'
  pdbtext.clean_pdb(full_pdb, pdb)

  # Generate protein topology in pdb2gmx_gro using pdb2gmx
  pdb2gmx_gro = basename + '.pdb2gmx.gro'
  top = basename + '.top'
  itp = basename + '_posre.itp'
  # Choose force field based on GROMACS version
  if 'GROMACS4.5' in force_field:
    ff = 'amber99' 
  elif 'GROMACS4.0' in force_field:
    ff = 'G43a1' 
  else:
    raise ValueError, "Couldn't work out pdb2gmx for " + force_field
  args = '-ignh -ff %s -water spc -missing -f %s -o %s -p %s -i %s -chainsep id_or_ter -merge all' \
          % (ff, pdb, pdb2gmx_gro, top, itp)
  data.binary('pdb2gmx', args, basename+'.pdb2gmx')
  util.check_files(pdb2gmx_gro)

  # Now add a box with editconf
  box_gro = basename + '.box.gro'
  solvent_buffer_in_nm = solvent_buffer/10.0 
  data.binary(
      'editconf', 
      '-f %s -o %s -c -d %f -bt cubic' \
          % (pdb2gmx_gro, box_gro, solvent_buffer_in_nm),
      basename+'.box')
  util.check_files(box_gro)

  # Given box dimensions, can now populate with explict waters
  solvated_gro = basename + '.solvated.gro'
  data.binary(
      'genbox',
      '-cp %s -cs spc216.gro -o %s -p %s' \
          % (box_gro, solvated_gro, top),
       '%s.solvated' % basename)
  util.check_files(solvated_gro)

  # Neutralize with counterions using genion to place ions 
  # based on energy parameters processed by grompp 
  gro = basename + '.gro'
  neutralize_system_with_salt(top, solvated_gro, basename, force_field)
  util.check_files(gro)

  # Make a reference PDB file from restart files for viewing and restraints
  convert_restart_to_pdb(basename, basename+'.pdb')

  # Copy finished restart files back into original directory
  fnames = util.re_glob(
      '*', os.path.basename(basename) + r'[^\.]*\.(pdb|itp|gro|mdp|top)$')
  for fname in fnames:
    shutil.copy(fname, save_dir)

  # Cleanup
  delete_backup_files(basename)
  os.chdir(save_dir)

  return top, gro