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 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 get_energy(top, crd): "Returns potential energy of top and crd by running sander" top = os.path.abspath(top) crd = os.path.abspath(crd) util.goto_dir('energy-temp') parms = minimization_parms.copy() parms['topology'] = top parms['input_crds'] = crd parms['output_name'] = 'energy' parms['n_step_minimization'] = 0 parms['n_step_steepest_descent'] = 0 run(parms) lines = open('energy.sander.out').readlines() energies = {} is_results = False for i, line in enumerate(lines): if not is_results: if '4' in line and 'RESULTS' in line: is_results = True else: if 'NSTEP' in line and 'ENERGY' in line: util.goto_dir('..') os.system('rm -rf energy-temp') return float(lines[i+1].split()[1]) raise "Can't find energies"
def pulse(in_parms, n_step_per_pulse, reset_vel_func): config = in_parms['output_name'] + ".config" if util.is_same_dict_in_file(in_parms, config): print "simulation already run." return name = in_parms['output_name'] shutil.copy(in_parms['topology'], name + '.psf') if in_parms['constraint_pdb']: shutil.copy(in_parms['constraint_pdb'], name + '.constraint') n_pulse = in_parms['n_step_dynamics'] // n_step_per_pulse n_step_list = [n_step_per_pulse for i in range(n_pulse)] n_excess_step = in_parms['n_step_dynamics'] % n_step_per_pulse if n_excess_step > 0: n_pulse += 1 n_step_list.append(n_excess_step) pulse_in_coor = util.insert_path(in_parms['input_crds'], '..') pulse_in_vel = util.insert_path(in_parms['input_vels'], '..') stopwatch = util.Timer() pulses = ["pulse%d" % i for i in range(n_pulse)] for pulse, n_step in zip(pulses, n_step_list): util.goto_dir(pulse) pulse_parms = copy.deepcopy(in_parms) pulse_parms['topology'] = util.insert_path(in_parms['topology'], '..') pulse_parms['n_step_dynamics'] = n_step if 'constraint_pdb' in pulse_parms and pulse_parms['constraint_pdb']: pulse_parms['constraint_pdb'] = util.insert_path(in_parms['constraint_pdb'], '..') pulse_parms['input_crds'] = pulse_in_coor pulse_parms['input_vels'] = pulse_in_vel soup = SoupFromRestartFiles(pulse_parms['topology'], pulse_parms['input_crds'], pulse_parms['input_vels']) reset_vel_func(soup) pulse_parms['input_vels'] = name + ".in.vel" write_soup_to_vel(soup, pulse_parms['input_vels']) run(pulse_parms) pulse_in_coor = '../%s/%s.coor' % (pulse, name) pulse_in_vel = '../%s/%s.vel' % (pulse, name) os.chdir('..') stopwatch.stop() open(name + '.time', 'w').write(stopwatch.str()) merge_simulations(name, pulses) for pulse in pulses: os.system('rm -rf %s' % pulse) util.write_dict(in_parms, config)
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
def systemRefresh(): """ Pull the system spices from github and create a new system.tar for the clients all to download. """ log('UPDATING SYSTEM SPICE') goto_dir(makepath('spicerackclient')) command('git', 'pull') command('tar', '-cvf', makepath('system.tar)', 'system')) goto_dir(makepath('')) currentversion = dget('systemversion', 0) currentversion = 1 + int(currentversion) put('systemversion', currentversion) filewrite('systemversion.txt', str(currentversion)) command('tar', '--append', '--file=system.tar', 'systemversion.txt') log('UPDATED SYSTEM SPICE TO VERSION: %s' % currentversion) return 'success'
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 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
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
def spiceMerge(spices, username): """ Manages the merger of a spice into the development repository. @param spices: list of (spicename, dirname) """ log('[spicemanager] In func:spiceMerge') goto_dir(makepath('spicerackdev')) command('git', 'pull') goto_dir(makepath('')) spice_to_metadata = {} for dirname in spices: data = jsonload(makepath('spicerackdev', dirname, 'metadata.json')) spicename = data['name'] if 'spicename' not in data: data['spicename'] = spicename if 'width' not in data: data['width'] = 0 data['height'] = 0 data['dependencies'] = [] Statsd.increment('spices.spicerackdev.%s' % spicename.replace(' ', '-')) mid = get('spice-id:%s' % spicename.replace(' ', '-')) if mid is None: mid = str(uuid.uuid4()) put('spice-id:%s' % spicename.replace(' ', '-'), mid) log('[spicemanager] Processing %s %s' % (spicename, mid)) version = 1 + int(dget('version:%s' % mid, 0)) put('version:%s' % mid, version) log('[spicemanager] Version: %s' % version) spice_to_metadata[mid] = { 'spicename' : data['spicename'], 'gituser' : username, 'author' : data['author'], 'tagline' : data['description'], 'description' : data['description'], 'version' : version, 'hotkey' : False, 'main' : data['main'], 'width' : data['width'], 'height' : data['height'], 'dependencies' : data['dependencies'] } put('metadata:%s' % mid, spice_to_metadata[mid]) jsondump(makepath('spicerackdev', dirname, 'metadata.json'), spice_to_metadata[mid]) # Tar the spice directory into /spices/{mid}.tar.gz command('tar', '-cvf', makepath('spices', '%s.tar.gz' % mid), makepath('spicerackdev', dirname)) # Copy the icon file into /icons iconpath = makepath('spicerackdev', dirname, 'icon.png') command('cp', '-f', iconpath, makepath('icons', '%s.png' % mid)) iconpath = makepath(dirname, 'icon.png') if not os.path.exists(iconpath): command('tar', '-cvf', makepath('spices', '%s.tar.gz' % mid), makepath('spicerackdev', dirname)) else: iconpath = iconpath.strip('icon.png') command('tar', '-cvf', makepath('spices', '%s.tar.gz' % mid), makepath('spicerackdev', dirname), '--directory=%s' % iconpath, 'icon.png') goto_dir(makepath('spicerackdev')) command('git', 'stash') goto_dir(makepath('')) heroku_metadata = { 'spicename' : data['spicename'], 'gituser' : username, 'author' : data['author'], 'tagline' : data['description'], 'description' : data['description'], 'version' : version, 'hotkey' : False, 'main' : data['main'] } log('HEROKU: %s %s', type(heroku_metadata), heroku_metadata) for key, val in heroku_metadata.iteritems(): log('KEY %s VAL %s %s', key, type(val), val) resp, content = send_post({str(mid) : heroku_metadata}, '/spices/refresh') log('[spicemanager] Heroku response: %s' % str(resp)) return 'success'