def build_system_from_molecules(self, molecules, patchers=None, leap_header_cmds=None): if patchers is None: patchers = [] if leap_header_cmds is None: leap_header_cmds = [] if isinstance(leap_header_cmds, string_types): leap_header_cmds = [leap_header_cmds] with util.in_temp_dir(): leap_cmds = [] mol_ids = [] leap_cmds.extend(self._generate_leap_header()) leap_cmds.extend(leap_header_cmds) for index, mol in enumerate(molecules): mol_id = 'mol_{}'.format(index) mol_ids.append(mol_id) mol.prepare_for_tleap(mol_id) leap_cmds.extend(mol.generate_tleap_input(mol_id)) leap_cmds.extend(self._generate_leap_footer(mol_ids)) with open('tleap.in', 'w') as tleap_file: tleap_string = '\n'.join(leap_cmds) tleap_file.write(tleap_string) subprocess.check_call('tleap -f tleap.in > tleap.out', shell=True) return load_amber_system('system.top', 'system.mdcrd', patchers)
def setUp(self): with in_temp_dir(): with open("pdb.pdb", "w") as outfile: outfile.write(pdb_string) p = subsystem.SubSystemFromPdbFile("pdb.pdb") b = builder.SystemBuilder(explicit_solvent=True) self.system = b.build_system([p])
def build_system_from_molecules(self, molecules, patchers=None, leap_header_cmds=None): if patchers is None: patchers = [] if leap_header_cmds is None: leap_header_cmds = [] if isinstance(leap_header_cmds, str): leap_header_cmds = [leap_header_cmds] with util.in_temp_dir(): leap_cmds = [] mol_ids = [] leap_cmds.extend(self._generate_leap_header()) leap_cmds.extend(leap_header_cmds) for index, mol in enumerate(molecules): mol_id = f"mol_{index}" mol_ids.append(mol_id) mol.prepare_for_tleap(mol_id) leap_cmds.extend(mol.generate_tleap_input(mol_id)) if self._explicit_solvent: leap_cmds.extend(self._generate_solvent(mol_ids)) leap_cmds.extend(self._generate_leap_footer([f"solute"])) else: leap_cmds.extend(self._generate_leap_footer(mol_ids)) with open("tleap.in", "w") as tleap_file: tleap_string = "\n".join(leap_cmds) tleap_file.write(tleap_string) subprocess.check_call("tleap -f tleap.in > tleap.out", shell=True) return load_amber_system("system.top", "system.mdcrd", patchers)
def patch(self, top_string, crd_string): INTOP = 'in.top' INRST = 'in.rst' OUTTOP = 'out.top' OUTRST = 'out.rst' with util.in_temp_dir(): with open(INTOP, 'wt') as outfile: outfile.write(top_string) with open(INRST, 'wt') as outfile: outfile.write(crd_string) topol = pmd.load_file(INTOP) crd = pmd.load_file(INRST) topol.coordinates = crd.coordinates self._add_particles(topol) topol.write_parm(OUTTOP) topol.write_rst7(OUTRST) with open(OUTTOP, 'rt') as infile: top_string = infile.read() with open(OUTRST, 'rt') as infile: crd_string = infile.read() return top_string, crd_string
def patch(self, top_string, crd_string): INTOP = "in.top" INRST = "in.rst" OUTTOP = "out.top" OUTRST = "out.rst" with util.in_temp_dir(): with open(INTOP, "wt") as outfile: outfile.write(top_string) with open(INRST, "wt") as outfile: outfile.write(crd_string) topol = pmd.load_file(INTOP) crd = pmd.load_file(INRST) topol.coordinates = crd.coordinates self._add_particles(topol) topol.write_parm(OUTTOP) topol.write_rst7(OUTRST) with open(OUTTOP, "rt") as infile: top_string = infile.read() with open(OUTRST, "rt") as infile: crd_string = infile.read() return top_string, crd_string
def build_system_from_molecules( self, molecules, patchers=None, leap_header_cmds=None ): if patchers is None: patchers = [] if leap_header_cmds is None: leap_header_cmds = [] if isinstance(leap_header_cmds, str): leap_header_cmds = [leap_header_cmds] with util.in_temp_dir(): leap_cmds = [] mol_ids = [] leap_cmds.extend(self._generate_leap_header()) leap_cmds.extend(leap_header_cmds) for index, mol in enumerate(molecules): mol_id = f"mol_{index}" mol_ids.append(mol_id) mol.prepare_for_tleap(mol_id) leap_cmds.extend(mol.generate_tleap_input(mol_id)) if self._explicit_solvent: leap_cmds.extend(self._generate_solvent(mol_ids)) leap_cmds.extend(self._generate_leap_footer([f"solute"])) else: leap_cmds.extend(self._generate_leap_footer(mol_ids)) with open("tleap.in", "w") as tleap_file: tleap_string = "\n".join(leap_cmds) tleap_file.write(tleap_string) subprocess.check_call("tleap -f tleap.in > tleap.out", shell=True) return load_amber_system("system.top", "system.mdcrd", patchers)
def test_init_mode_w_creates_results(self): "calling initialize should create the results.h5 file" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') self.assertTrue(os.path.exists('Data/Blocks/block_000000.nc'), 'results_000000.nc not created')
def test_load_datastore(self): "should call load the datastore" with in_temp_dir(): os.mkdir("Logs") launch.launch("Reference", self.log_handler) self.MockDataStore.load_data_store.assert_called_once_with()
def test_should_init_store(self): "should initialize the store" with in_temp_dir(): os.mkdir("Logs") launch.launch("Reference", self.log_handler) self.mock_store.initialize.assert_called_once_with(mode="a")
def test_should_init_comm(self): "should initialize the communicator" with in_temp_dir(): os.mkdir("Logs") launch.launch("Reference", self.log_handler) self.mock_comm.initialize.assert_called_once_with()
def test_init_mode_w_creates_directories(self): "calling initialize should create the Data and Data/Backup directories" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') self.assertTrue(os.path.exists('Data'), 'Data directory does not created') self.assertTrue(os.path.exists('Data/Backup'), 'Backup directory not created')
def test_should_run(self): "should run remd runner with correct parameters" with in_temp_dir(): os.mkdir("Logs") launch.launch("Reference", self.log_handler) self.mock_remd_leader.run.assert_called_once_with( self.mock_comm, self.mock_runner, self.mock_store)
def test_init_mode_w_raises_when_dirs_exist(self): "calling initialize should raise RuntimeError when Data and Data/Backup directories exist" with in_temp_dir(): os.mkdir('Data') os.mkdir('Data/Backup') pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) with self.assertRaises(RuntimeError): store.initialize(mode='w')
def test_init_mode_w_creates_results(self): "calling initialize should create the results.h5 file" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') self.assertTrue(os.path.exists('Data/Blocks/block_000000.nc'), 'results_000000.nc not created')
def test_init_mode_w_raises_when_dirs_exist(self): "calling initialize should raise RuntimeError when Data directories exist" with in_temp_dir(): os.mkdir("Data") os.mkdir("Data/Backup") # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) with self.assertRaises(RuntimeError): store.initialize(mode="w")
def test_save_and_load_run_options(self): "should be able to save and load run options" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') fake_run_options = object() store.save_run_options(fake_run_options) store.load_run_options() self.assertTrue(os.path.exists('Data/run_options.dat'))
def test_init_mode_w_creates_directories(self): "calling initialize should create the Data and Data/Backup directories" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") self.assertTrue(os.path.exists("Data"), "Data directory does not created") self.assertTrue(os.path.exists("Data/Backup"), "Backup directory not created")
def test_save_and_load_run_options(self): "should be able to save and load run options" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") fake_run_options = system.RunOptions() store.save_run_options(fake_run_options) store.load_run_options() self.assertTrue(os.path.exists("Data/run_options.dat"))
def test_save_and_load_system(self): "should be able to save and load a System" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') fake_system = object() store.save_system(fake_system) store.load_system() self.assertTrue(os.path.exists('Data/system.dat'))
def test_save_and_load_system(self): "should be able to save and load a System" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.state_template, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") fake_system = object() store.save_system(fake_system) store.load_system() self.assertTrue(os.path.exists("Data/system.dat"))
def test_save_and_load_data_store(self): "should be able to save and then reload the DataStore" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') store.save_data_store() store2 = vault.DataStore.load_data_store() self.assertEqual(store.n_atoms, store2.n_atoms) self.assertEqual(store.n_replicas, store2.n_replicas) self.assertIsNone(store2._cdf_data_set) self.assertTrue(os.path.exists('Data/data_store.dat'))
def test_save_and_load_data_store(self): "should be able to save and then reload the DataStore" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") store.save_data_store() store2 = vault.DataStore.load_data_store() self.assertEqual(store.n_atoms, store2.n_atoms) self.assertEqual(store.n_replicas, store2.n_replicas) self.assertIsNone(store2._cdf_data_set) self.assertTrue(os.path.exists("Data/data_store.dat"))
def test_save_and_load_remd_runner(self): "should be able to save and reload an remd runner" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') l = ladder.NearestNeighborLadder(n_trials=100) policy = adaptor.AdaptationPolicy(1.0, 50, 100) a = adaptor.EqualAcceptanceAdaptor(n_replicas=self.N_REPLICAS, adaptation_policy=policy) runner = master_runner.MasterReplicaExchangeRunner(self.N_REPLICAS, max_steps=100, ladder=l, adaptor=a) store.save_remd_runner(runner) runner2 = store.load_remd_runner() self.assertEqual(runner.n_replicas, runner2.n_replicas) self.assertTrue(os.path.exists('Data/remd_runner.dat'))
def test_save_and_load_communicator(self): "should be able to save and reload the communicator" with in_temp_dir(): pdb_writer = object() # dummy pdb writer; can't use a mock because they can't be pickled store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode='w') c = comm.MPICommunicator(self.N_ATOMS, self.N_REPLICAS) # set _mpi_comm to something # this should not be saved c._mpi_comm = 'foo' store.save_communicator(c) c2 = store.load_communicator() self.assertEqual(c.n_atoms, c2.n_atoms) self.assertEqual(c.n_replicas, c2.n_replicas) self.assertIsNone(c2._mpi_comm, '_mpi_comm should not be saved') self.assertTrue(os.path.exists('Data/communicator.dat'))
def test_save_and_load_communicator(self): "should be able to save and reload the communicator" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") c = comm.MPICommunicator(self.N_ATOMS, self.N_REPLICAS) # set _mpi_comm to something # this should not be saved c._mpi_comm = "foo" store.save_communicator(c) c2 = store.load_communicator() self.assertEqual(c.n_atoms, c2.n_atoms) self.assertEqual(c.n_replicas, c2.n_replicas) self.assertTrue(os.path.exists("Data/communicator.dat"))
def test_save_and_load_remd_runner(self): "should be able to save and reload an remd runner" with in_temp_dir(): # dummy pdb writer; can't use a mock because they can't be pickled pdb_writer = object() store = vault.DataStore(self.N_ATOMS, self.N_REPLICAS, pdb_writer) store.initialize(mode="w") l = ladder.NearestNeighborLadder(n_trials=100) policy = adaptor.AdaptationPolicy(1.0, 50, 100) a = adaptor.EqualAcceptanceAdaptor(n_replicas=self.N_REPLICAS, adaptation_policy=policy) runner = master_runner.MasterReplicaExchangeRunner(self.N_REPLICAS, max_steps=100, ladder=l, adaptor=a) store.save_remd_runner(runner) runner2 = store.load_remd_runner() self.assertEqual(runner.n_replicas, runner2.n_replicas) self.assertTrue(os.path.exists("Data/remd_runner.dat"))
def build_system_from_molecules(self, molecules): with util.in_temp_dir(): leap_cmds = [] mol_ids = [] leap_cmds.extend(self._generate_leap_header()) for index, mol in enumerate(molecules): mol_id = 'mol_{}'.format(index) mol_ids.append(mol_id) mol.prepare_for_tleap(mol_id) leap_cmds.extend(mol.generate_tleap_input(mol_id)) leap_cmds.extend(self._generate_leap_footer(mol_ids)) with open('tleap.in', 'w') as tleap_file: tleap_string = '\n'.join(leap_cmds) tleap_file.write(tleap_string) print tleap_string subprocess.check_call('tleap -f tleap.in > tleap.out', shell=True) with open('system.top') as top_file: top = top_file.read() with open('system.mdcrd') as crd_file: crd = crd_file.read() return System(top, crd)
def build_system( self, subsystems: List[subsystem._SubSystem], patchers: Optional[List[patchers.PatcherBase]] = None, leap_header_cmds: Optional[List[str]] = None, ) -> interfaces.ISystem: """ Build the system from SubSystems Args: subsystems: component subsystems that make up the system patchers: an optional list of patchers to modify system leap_header_cmds: an optional list of leap commands Returns: the MELD system """ if not subsystems: raise ValueError("len(subsystems) must be > 0") if patchers is None: patchers = [] if leap_header_cmds is None: leap_header_cmds = [] if isinstance(leap_header_cmds, str): leap_header_cmds = [leap_header_cmds] with util.in_temp_dir(): mol_ids = [] chains = [] current_res_index = 0 leap_cmds = [] leap_cmds.extend(self._generate_leap_header()) leap_cmds.extend(leap_header_cmds) for index, sub in enumerate(subsystems): # First we'll update the indexing for this subsystem for chain in sub._info.chains: residues_with_offset = { k: v + current_res_index for k, v in chain.residues.items() } chains.append(indexing._ChainInfo(residues_with_offset)) current_res_index += sub._info.n_residues # now add the leap commands for this subsystem mol_id = f"mol_{index}" mol_ids.append(mol_id) sub.prepare_for_tleap(mol_id) leap_cmds.extend(sub.generate_tleap_input(mol_id)) if self._explicit_solvent: leap_cmds.extend(self._generate_solvent(mol_ids)) leap_cmds.extend(self._generate_leap_footer([f"solute"])) else: leap_cmds.extend(self._generate_leap_footer(mol_ids)) with open("tleap.in", "w") as tleap_file: tleap_string = "\n".join(leap_cmds) tleap_file.write(tleap_string) try: subprocess.check_call("tleap -f tleap.in > tleap.out", shell=True) except subprocess.CalledProcessError: print("Call to tleap failed.") print() print() print() print("=========") print("tleap.in") print("=========") print(open("tleap.in").read()) print() print() print() print("=========") print("tleap.out") print("=========") print(open("tleap.out").read()) print() print() print() print("========") print("leap.log") print("========") print(open("leap.log").read()) print() print() print() raise return meld_system._load_amber_system("system.top", "system.mdcrd", chains, patchers)
def patch(self, top_string, crd_string): INTOP = "in.top" INRST = "in.rst" OUTTOP = "out.top" OUTRST = "out.rst" with util.in_temp_dir(): with open(INTOP, "wt") as outfile: outfile.write(top_string) with open(INRST, "wt") as outfile: outfile.write(crd_string) base = pmd.load_file(INTOP) crd = pmd.load_file(INRST) base.coordinates = crd.coordinates # create a new structure to add our dummy atoms to parm = pmd.Structure() # add in atom type for our dummy particles atype = pmd.AtomType("SDUM", 0, mass=12.0, charge=0.0) atype.set_lj_params(0.0, 0.0) for i in range(self.n_tensors): a1 = pmd.Atom( name="S1", atomic_number=atype.atomic_number, type=str(atype), charge=atype.charge, mass=atype.mass, solvent_radius=1.0, screen=0.5, ) a1.atom_type = atype a2 = pmd.Atom( name="S2", atomic_number=atype.atomic_number, type=str(atype), charge=atype.charge, mass=atype.mass, solvent_radius=1.0, screen=0.5, ) a2.atom_type = atype parm.add_atom(a1, resname="SDM", resnum=i) parm.add_atom(a2, resname="SDM", resnum=i) # we add noise here because we'll get NaN if the particles ever # end up exactly on top of each other parm.positions = np.zeros((2 * self.n_tensors, 3)) # combine the old system with the new dummy atoms comb = base + parm last_index = comb.residues[-1].idx self.resids = list(range(last_index - self.n_tensors + 2, last_index + 2)) comb.write_parm(OUTTOP) comb.write_rst7(OUTRST) with open(OUTTOP, "rt") as infile: top_string = infile.read() with open(OUTRST, "rt") as infile: crd_string = infile.read() return top_string, crd_string