def get_pdbs_with_ftp(*pdbs): """ Fetches PDB files using FTP from a list of pdb-codes/text-files. """ pdbs = expand_pdbs(*pdbs) entries = ['pdb%s.ent.gz' % pdb for pdb in pdbs] mget_scripts = ["get %s" % entry for entry in entries] substitutions = { 'host': 'ftp.wwpdb.org', 'mget_str': '\n'.join(mget_scripts), } ftp_script = template % substitutions open('ftp.sh', 'w').write(ftp_script) util.run_with_output('chmod +x ftp.sh') util.run_with_output('./ftp.sh') for pdb, entry in zip(pdbs, entries): fname = pdb + '.pdb' if os.path.isfile(entry): out_f = open(fname, 'w') for line in gzip.open(entry): out_f.write(line) util.clean_fname(entry) print fname else: print "Failed: %s" % fname util.clean_fname('ftp.sh')
def calculate_energy(top, crd): """ Returns potential energy of top and crd by running sander and parsing the sander output. """ top = os.path.abspath(top) crd = os.path.abspath(crd) util.goto_dir('energy-temp') parms = minimization_parms.copy() parms.extend({ 'topology': top, 'input_crds': crd, 'output_basename': 'energy', 'n_step_minimization': 0, 'n_step_steepest_descent': 0}) run(parms) blocks = read_minimization_sander_out('energy.sander.out') util.goto_dir('..') util.clean_fname('energy-temp') return blocks[0]['ENERGY']
def soup_to_bfactor_png( soup, png, bfactors, lower_bfactor=None, upper_bfactor=None, highlight_res=None, is_putty=False): temp_pdb = util.temp_fname('.pdb') soup.load_residue_bfactors(bfactors) max_bfactor = rescale_positive_bfactors_pdb(soup, temp_pdb, lower_bfactor, upper_bfactor) temp2_pdb = strip_solvent_pdb(temp_pdb) script = bgcolor_script('white') script += bfactor_script( temp2_pdb, lower_bfactor, upper_bfactor, max_bfactor, is_putty) if highlight_res is not None: script += highlight_res_script(highlight_res) script += hide_backbone_sticks_script() if 'frame_pymol_script' in soup.__dict__: script += soup.frame_pymol_script script += "clip far, -20\n" script += "save %s\n" % png script += "quit" width, height = 480, 480 if 'width' in soup.__dict__: width = soup.width if 'height' in soup.__dict__: height = soup.height run_pymol_script(script, width, height) util.clean_fname(temp_pdb, temp2_pdb)
def calculate_energy(top, crd): """ Returns potential energy of top and crd by running sander and parsing the sander output. """ top = os.path.abspath(top) crd = os.path.abspath(crd) util.goto_dir('energy-temp') parms = minimization_parms.copy() parms.extend({ 'topology': top, 'input_crds': crd, 'output_basename': 'energy', 'n_step_minimization': 0, 'n_step_steepest_descent': 0 }) run(parms) blocks = read_minimization_sander_out('energy.sander.out') util.goto_dir('..') util.clean_fname('energy-temp') return blocks[0]['ENERGY']
def make_pdbs_png(png, pdbs, bgcolor="white", center_res=None, top_res=None, highlight_res=None, is_sticks=True, is_putty=False, width=480, height=480): if 'transparent' in bgcolor: script = 'set opaque_background, off\n' else: script = make_bgcolor_script(bgcolor) temp_fnames = [] if center_res and top_res: transform = get_pdb_transform(pdbs[0], center_res, top_res) for i in range(len(pdbs)): soup = pdbatoms.Soup(pdbs[i]) soup.transform(transform) new_pdb = util.fname_variant(pdbs[i]) soup.write_pdb(new_pdb) temp_fnames.append(new_pdb) pdbs[i] = new_pdb del soup script += make_load_pdbs_script(pdbs) script += make_separate_chain_colors_script(pdbs) if is_putty: script += make_putty_script(get_scale_max(max_bfactor, upper_bfactor)) else: script += cartoon_script if not is_sticks: script += "hide stick\n" else: script += "show stick\n" script += make_ligands_as_sticks_script(pdbs) if highlight_res: script += make_highlight_res_script(highlight_res) script += hide_backbone_sticks_script # script += "clip far, 5\n" script += "save %s\n" % png script += "quit" pml = util.fname_variant('temp.pml') open(pml, 'w').write(script) run_pymol_script(pml, width, height) temp_fnames.append(pml) util.clean_fname(*temp_fnames)
def make_pdbs_png( png, pdbs, bgcolor="white", center_res=None, top_res=None, highlight_res=None, is_sticks=True, is_putty=False, width=480, height=480): if 'transparent' in bgcolor: script = 'set opaque_background, off\n' else: script = make_bgcolor_script(bgcolor) temp_fnames = [] if center_res and top_res: transform = get_pdb_transform(pdbs[0], center_res, top_res) for i in range(len(pdbs)): soup = pdbatoms.Soup(pdbs[i]) soup.transform(transform) new_pdb = util.fname_variant(pdbs[i]) soup.write_pdb(new_pdb) temp_fnames.append(new_pdb) pdbs[i] = new_pdb del soup script += make_load_pdbs_script(pdbs) script += make_separate_chain_colors_script(pdbs) if is_putty: script += make_putty_script(get_scale_max( max_bfactor, upper_bfactor)) else: script += cartoon_script if not is_sticks: script += "hide stick\n" else: script += "show stick\n" script += make_ligands_as_sticks_script(pdbs) if highlight_res: script += make_highlight_res_script(highlight_res) script += hide_backbone_sticks_script # script += "clip far, 5\n" script += "save %s\n" % png script += "quit" pml = util.fname_variant('temp.pml') open(pml, 'w').write(script) run_pymol_script(pml, width, height) temp_fnames.append(pml) util.clean_fname(*temp_fnames)
def pdb_to_bfactor_png(bfactor_pdb, png, lower_bfactor=None, upper_bfactor=None, highlight_res=None, is_putty=False, is_sticks=True, center_res=None, top_res=None, height=480, width=480): """ Generates a bfactor-colored .png using the standard white-to-red color scheme with a useful set of options. highlight_res, center_res & top_res follow the 'A:10' style of residue naming. """ pdb, max_bfactor = rescale_positive_bfactors_pdb(bfactor_pdb, lower_bfactor, upper_bfactor) temp_fnames = [pdb] if center_res or top_res: pdb = get_pdb_transform(pdb, center_res, top_res) temp_fnames.append(pdb) script = "" script += bfactor_script(pdb, lower_bfactor, upper_bfactor, max_bfactor, is_putty) if highlight_res is not None: pymol_res_id = get_pymol_id_from_res_tag(highlight_res) script += make_highlight_res_script(pymol_res_id) script += hide_backbone_sticks_script script += "clip far, -20\n" script += "save %s\n" % png script += "quit" pml = util.fname_variant('temp.pml') open(pml, 'w').write(script) run_pymol_script(pml, width, height) temp_fnames.append(pml) util.clean_fname(*temp_fnames)
def soup_to_bfactor_png( soup, png, bfactors, lower_bfactor=None, upper_bfactor=None, highlight_res=None, is_putty=False, is_sticks=True, center_res=None, top_res=None, height=480, width=480): """ Wrapper around pdb_to_bfactor that loads in external values for the residue bfactors with a reusable soup object. """ pdb = util.temp_fname('.pdb') temp_fnames = [pdb] soup.load_residue_bfactors(bfactors) soup.write_pdb(pdb) pdb_to_bfactor_png( pdb, png, lower_bfactor, upper_bfactor, highlight_res, is_putty, is_sticks, center_res, top_res, height, width) util.clean_fname(*temp_fnames)
def pdb_to_bfactor_png( bfactor_pdb, png, lower_bfactor=None, upper_bfactor=None, highlight_res=None, is_putty=False, is_sticks=True, center_res=None, top_res=None, height=480, width=480): """ Generates a bfactor-colored .png using the standard white-to-red color scheme with a useful set of options. highlight_res, center_res & top_res follow the 'A:10' style of residue naming. """ pdb, max_bfactor = rescale_positive_bfactors_pdb( bfactor_pdb, lower_bfactor, upper_bfactor) temp_fnames = [pdb] if center_res or top_res: pdb = get_pdb_transform(pdb, center_res, top_res) temp_fnames.append(pdb) script = "" script += bfactor_script( pdb, lower_bfactor, upper_bfactor, max_bfactor, is_putty) if highlight_res is not None: pymol_res_id = get_pymol_id_from_res_tag(highlight_res) script += make_highlight_res_script(pymol_res_id) script += hide_backbone_sticks_script script += "clip far, -20\n" script += "save %s\n" % png script += "quit" pml = util.fname_variant('temp.pml') open(pml, 'w').write(script) run_pymol_script(pml, width, height) temp_fnames.append(pml) util.clean_fname(*temp_fnames)
def soup_to_bfactor_png(soup, png, bfactors, lower_bfactor=None, upper_bfactor=None, highlight_res=None, is_putty=False, is_sticks=True, center_res=None, top_res=None, height=480, width=480): """ Wrapper around pdb_to_bfactor that loads in external values for the residue bfactors with a reusable soup object. """ pdb = util.temp_fname('.pdb') temp_fnames = [pdb] soup.load_residue_bfactors(bfactors) soup.write_pdb(pdb) pdb_to_bfactor_png(pdb, png, lower_bfactor, upper_bfactor, highlight_res, is_putty, is_sticks, center_res, top_res, height, width) util.clean_fname(*temp_fnames)
def pulse( force_field, in_basename, basename, n_step, pulse_fn, n_step_per_pulse=100, restraint_pdb="", restraint_force=None): """ Runs a pulse simulation that uses the restart-file modification strategy to manage a steered-molecular dynamics simulation. The pulsed approacha pplies external forces in pulses, which is practically carried out be running short constant-energy simulations and directly modifying the restart velocities between each simulation. Pulse simulations has certain advantages: for instance, the system can respond to the forces between pulses, and the incredibly flexibility in applying forces. The disadvantage is the costly setup which is hopefully, mitigated by this library. Reference: Bosco K. Ho and David A. Agard (2010) "An improved strategy for generating forces in steered molecular dynamics: the mechanical unfolding of titin, e2lip3 and ubiquitin" PLoS ONE 5(9):e13068. """ # Grab the simulation prameters for a constant energy # simulation. Constant energy is preferred as we want to ensure # energy changes come only from our velocity modification. top, crds, vels = get_restart_files(in_basename) # use dummy top and crds, which will be overriden overall_config_parms = fetch_simulation_parameters( force_field, top, crds, restraint_pdb, 'constant_energy', basename, restraint_force) overall_config_parms.update({ 'input_md_name': in_basename, 'input_vels': vels, 'n_step_dynamics': n_step, 'n_step_per_snapshot': n_step_per_pulse // 2, 'n_step_per_pulse': n_step_per_pulse }) # Check if the simulation has already run as the config # file is not written until the very end config = basename + ".config" if util.is_same_dict_in_file(overall_config_parms, config): print "Skipping: pulsing simulation already run." return # The overall_config_parms will be written out at the end. # We make a copy for internal use pulse_parms = copy.deepcopy(overall_config_parms) # Calculate steps for each pulse, esp for last step n_pulse = pulse_parms['n_step_dynamics'] / n_step_per_pulse n_step_list = [n_step_per_pulse for i in range(n_pulse)] n_excess_step = pulse_parms['n_step_dynamics'] % n_step_per_pulse if n_excess_step > 0: n_pulse += 1 n_step_list.append(n_excess_step) # Prepare restart files for first step pulse_parms['topology'] = os.path.abspath(pulse_parms['topology']) in_basename = pulse_parms['input_md_name'] pulse_parms['input_md_name'] = os.path.abspath(in_basename) # Now loop through pulses timer = util.Timer() save_dir = os.getcwd() pulses = ["pulse%d" % i for i in range(n_pulse)] for pulse, n_step in zip(pulses, n_step_list): print "Pulse: %s/%d" % (pulse, n_pulse) os.chdir(save_dir) util.goto_dir(pulse) pulse_parms['n_step_dynamics'] = n_step soup = soup_from_restart_files(pulse_parms['input_md_name']) # Apply forces by modifying the velocities directly pulse_fn(soup) crds, vels = write_soup_to_crds_and_vels( force_field, soup, basename + '.pulse.in') pulse_parms['input_crds'] = crds pulse_parms['input_vels'] = vels run_simulation_with_parameters(pulse_parms) # Setup new restart files based on just-finished pulse pulse_parms['input_md_name'] = os.path.abspath(basename) os.chdir(save_dir) merge_simulations(force_field, basename, pulses) # cleanup pulses after merging util.clean_fname(*pulses) # everything worked, no exceptions thrown open(basename+'.time', 'w').write(timer.str()+'\n') util.write_dict(config, overall_config_parms)
def delete_backup_files(tag): util.clean_fname(*util.re_glob('*', '^#' + tag))