def test_hrex_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin powers = [2., 2., 4.] n_replicas = len(powers) oscillators = [testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas)] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations":5} rex = hamiltonian_exchange.HamiltonianExchange.create(state, systems, positions, nc_filename, parameters=parameters) rex.run() rex.extend(5) rex = resume(nc_filename) rex.run() eq(rex.__class__.__name__, "HamiltonianExchange")
def test_shape(): xyz, time, boxlength, boxangles = NetCDFTrajectoryFile(get_fn('mdcrd.nc')).read() yield lambda: eq(xyz.shape, (101, 223, 3)) yield lambda: eq(time.shape, (101,)) yield lambda: eq(boxlength, None) yield lambda: eq(boxangles, None)
def test_spartaplus(): t = md.load(get_fn('2EQQ.pdb')) result = md.chemical_shifts_spartaplus(t) print(result) eq(result.shape[1], 20) # 2EQQ is NMR structure with 20 frames eq(float(result.ix[(1, "HA")][0]), 4.378, decimal=4)
def test_repex_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas parameters = {"number_of_iterations":5} rex = replica_exchange.ReplicaExchange.create(states, coordinates, nc_filename, parameters=parameters) rex.run() rex.extend(5) rex = resume(nc_filename) rex.run() eq(rex.__class__.__name__, "ReplicaExchange")
def test_unique_pairs(): n = 10 a = np.arange(n) b = np.arange(n, n+n) eq(md.Topology._unique_pairs(a, a).sort(), md.Topology._unique_pairs_equal(a).sort()) eq(md.Topology._unique_pairs(a, b).sort(), md.Topology._unique_pairs_mutually_exclusive(a, b).sort())
def test_harmonic_oscillators(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations":1000} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() u_permuted = replica_exchange.database.ncfile.variables["energies"][:] s = replica_exchange.database.ncfile.variables["states"][:] u = permute_energies(u_permuted, s) u0 = np.array([[ho.reduced_potential_expectation(s0, s1) for s1 in states] for s0 in states]) l0 = np.log(u0) # Compare on log scale because uncertainties are proportional to values l1 = np.log(u.mean(0)) eq(l0, l1, decimal=1)
def test_harmonic_oscillators_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_i = [T_min, T_min * 10., T_min * 100.] n_replicas = len(T_i) ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions states = [ ThermodynamicState(system=system, temperature=T_i[i]) for i in range(n_replicas) ] coordinates = [positions] * n_replicas mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations":200} replica_exchange = ReplicaExchange.create(states, coordinates, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename, mpicomm=mpicomm) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_load_psf(): top = psf.load_psf(get_fn('ala_ala_ala.psf')) ref_top = md.load(get_fn('ala_ala_ala.pdb')).topology eq(top, ref_top) # Test the XPLOR psf format parsing top2 = psf.load_psf(get_fn('ala_ala_ala.xpsf')) eq(top2, ref_top)
def _test_against_vmd(pdb): # this is probably not cross-platform compatible. I assume that the exact # path to this CHARMM topology that is included with VMD depends on # the install mechanism, especially for bundled mac or windows installers VMD_ROOT = os.path.join(os.path.dirname(os.path.realpath(VMD)), '..') top_paths = [os.path.join(r, f) for (r, _, fs) in os.walk(VMD_ROOT) for f in fs if 'top_all27_prot_lipid_na.inp' in f] assert len(top_paths) >= 0 top = os.path.abspath(top_paths[0]).replace(" ", "\\ ") TEMPLATE = ''' package require psfgen topology %(top)s pdbalias residue HIS HSE pdbalias atom ILE CD1 CD segment U {pdb %(pdb)s} coordpdb %(pdb)s U guesscoord writepdb out.pdb writepsf out.psf exit ''' % {'top': top, 'pdb' : pdb} with enter_temp_directory(): with open('script.tcl', 'w') as f: f.write(TEMPLATE) os.system(' '.join([VMD, '-e', 'script.tcl', '-dispdev', 'none'])) out_pdb = md.load('out.pdb') out_psf = md.load_psf('out.psf') # make sure the two topologies are equal eq(out_pdb.top, out_psf)
def test_parallel_tempering_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_max = 10.0 * unit.kelvin n_temps = 3 ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions coordinates = [positions] * n_temps mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations":200} replica_exchange = ParallelTempering.create(system, coordinates, nc_filename, T_min=T_min, T_max=T_max, n_temps=n_temps, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_bool(): sp = parse_selection("protein or water") eq(sp.source, "(atom.residue.is_protein or atom.residue.is_water)") sp = parse_selection("protein or water or all") eq(sp.source, "(atom.residue.is_protein or atom.residue.is_water or True)")
def test_hbonds_against_dssp(): t = md.load(get_fn('2EQQ.pdb'))[0] pdb = os.path.join(tmpdir, 'f.pdb') dssp = os.path.join(tmpdir, 'f.pdb.dssp') t.save(pdb) cmd = ['mkdssp', '-i', pdb, '-o', dssp] subprocess.check_output(' '.join(cmd), shell=True) energy = scipy.sparse.lil_matrix((t.n_residues, t.n_residues)) # read the dssp N-H-->O column from the output file with open(dssp) as f: # skip the lines until the description of each residue's hbonds while not f.readline().startswith(' # RESIDUE AA STRUCTURE'): continue for i, line in enumerate(f): line = line.rstrip() offset0, e0 = map(float, line[39:50].split(',')) offset1, e1 = map(float, line[61:72].split(',')) if e0 <= -0.5: energy[int(i+offset0), i] = e0 if e1 <= -0.5: energy[int(i+offset1), i] = e1 dssp = energy.todense() ours = md.geometry.hbond.kabsch_sander(t)[0].todense() # There is tricky issues with the rounding right at the -0.5 cutoff, # so lets just check for equality with DSSP at -0.6 or less eq((dssp < -0.6), (ours < -0.6)) eq(dssp[dssp < -0.6], ours[ours < -0.6], decimal=1)
def test_parallel_tempering(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_max = 10.0 * unit.kelvin n_temps = 3 ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions coordinates = [positions] * n_temps mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations":1000} replica_exchange = ParallelTempering.create(system, coordinates, nc_filename, T_min=T_min, T_max=T_max, n_temps=n_temps, mpicomm=mpicomm, parameters=parameters) eq(replica_exchange.n_replicas, n_temps) replica_exchange.run() u_permuted = replica_exchange.database.ncfile.variables["energies"][:] s = replica_exchange.database.ncfile.variables["states"][:] u = permute_energies(u_permuted, s) states = replica_exchange.thermodynamic_states u0 = np.array([[ho.reduced_potential_expectation(s0, s1) for s1 in states] for s0 in states]) l0 = np.log(u0) # Compare on log scale because uncertainties are proportional to values l1 = np.log(u.mean(0)) eq(l0, l1, decimal=1)
def test_fitghmm(): with tempdir(): RawPositionsFeaturizer().save('featurizer.pickl') shell('hmsm fit-ghmm --featurizer featurizer.pickl --n-init 10 ' ' --n-states 4 --dir %s --ext h5 --top %s' % ( DATADIR, os.path.join(DATADIR, 'Trajectory0.h5'))) shell('hmsm inspect -i hmms.jsonlines --details') shell('hmsm sample-ghmm --no-match-vars -i hmms.jsonlines --lag-time 1 --n-state 4 ' '--featurizer featurizer.pickl --dir %s --ext h5 --top %s' % ( DATADIR, os.path.join(DATADIR, 'Trajectory0.h5'))) shell('hmsm means-ghmm -i hmms.jsonlines --lag-time 1 --n-state 4 ' '--featurizer featurizer.pickl --dir %s --ext h5 --top %s' % ( DATADIR, os.path.join(DATADIR, 'Trajectory0.h5'))) shell('hmsm structures means.csv --ext pdb --prefix means --top %s' % os.path.join(DATADIR, 'Trajectory0.h5')) samples_csv = pd.read_csv('samples.csv', skiprows=1) means_csv = pd.read_csv('means.csv', skiprows=1) model = next(iterobjects('hmms.jsonlines')) means_pdb = md.load(glob('means-*.pdb')) means = np.array(sorted(model['means'], key=lambda e: e[0])) print('true\n', HMM.means_) print('learned\n', means) eq(HMM.means_, means, decimal=0) means_pdb_xyz = np.array(sorted(means_pdb.xyz.reshape(4, 3), key=lambda e: e[0])) eq(means_pdb_xyz, np.array(sorted(model['means'], key=lambda e:e[0])), decimal=0)
def test_baker_hubbard_2(): t = md.load(get_fn('1vii_sustiva_water.pdb')) triplets = md.baker_hubbard(t) N = 1000 rows = triplets[:, 0] * N*N + triplets[:, 1] * N + triplets[:, 2] # ensure that there aren't any repeat rows eq(len(np.unique(rows)), len(rows))
def test_3(): # test using a callable metric. should get same results model1 = LandmarkAgglomerative(n_clusters=10, n_landmarks=20, metric='euclidean') model2 = LandmarkAgglomerative(n_clusters=10, n_landmarks=20, metric=lambda target, ref, i: np.sqrt(np.sum((target-ref[i])**2, axis=1))) data = np.random.RandomState(0).randn(100, 2) eq(model1.fit_predict([data])[0], model2.fit_predict([data])[0])
def test_select_atom_indices(): top = md.load(get_fn("native.pdb")).topology yield lambda: eq(top.select_atom_indices("alpha"), np.array([8])) yield lambda: eq(top.select_atom_indices("minimal"), np.array([4, 5, 6, 8, 10, 14, 15, 16, 18])) assert_raises(ValueError, lambda: top.select_atom_indices("sdfsdfsdf"))
def test_center_of_mass(): top = md.Topology() chain = top.add_chain() resi = top.add_residue("NA", chain) top.add_atom('H1', md.element.hydrogen, resi) top.add_atom('H2', md.element.hydrogen, resi) top.add_atom('O', md.element.oxygen, resi) xyz = np.array([ [ # Frame 1 - Right triangle [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0] ], [ # Frame 2 [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.5, 0.5, 0.0] ] ]) traj = md.Trajectory(xyz, top) com_test = mdtraj.geometry.distance.compute_center_of_mass(traj) com_actual = (1 / 18.015324) * np.array([ [1.007947, 1.007947, 0.0], [1.007947 + 0.5 * 15.99943, 1.007947 + 0.5 * 15.99943, 0.0], ]) eq(com_actual, com_test, decimal=4)
def test_write_1(): xyz = np.array(np.random.randn(500, 10, 3), dtype=np.float32) with BINPOSTrajectoryFile(temp, 'w') as f: f.write(xyz) xyz2 = BINPOSTrajectoryFile(temp).read() eq(xyz, xyz2)
def test_slice(): t = md.load(fn) yield lambda: eq((t[0:5] + t[5:10]).xyz, t[0:10].xyz) yield lambda: eq((t[0:5] + t[5:10]).time, t[0:10].time) yield lambda: eq((t[0:5] + t[5:10]).unitcell_vectors, t[0:10].unitcell_vectors) yield lambda: eq((t[0:5] + t[5:10]).unitcell_lengths, t[0:10].unitcell_lengths) yield lambda: eq((t[0:5] + t[5:10]).unitcell_angles, t[0:10].unitcell_angles)
def f(): t.save(e) t2 = md.load(e, top=get_fn('ala_dipeptide.pdb')) # change decimal to 3 since the precision is different in different trajectory # format eq(t.xyz, t2.xyz, decimal=3, err_msg=e)
def test_atom_indices_1(): atom_indices = np.arange(10) top = md.load(get_fn("native.pdb")) t0 = md.load(get_fn("frame0.mdcrd"), top=top) t1 = md.load(get_fn("frame0.mdcrd"), top=top, atom_indices=atom_indices) eq(t0.xyz[:, atom_indices], t1.xyz)
def test_double_set_thermodynamic_states(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_max = 10.0 * unit.kelvin n_temps = 3 ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions coordinates = [positions] * n_temps mpicomm = dummympi.DummyMPIComm() parameters = {"number_of_iterations": 3} replica_exchange = ParallelTempering.create( system, coordinates, nc_filename, T_min=T_min, T_max=T_max, n_temps=n_temps, mpicomm=mpicomm, parameters=parameters, ) eq(replica_exchange.n_replicas, n_temps) replica_exchange.run() states = replica_exchange.thermodynamic_states replica_exchange.database.thermodynamic_states = states
def test_hrex_save_and_load(mpicomm): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin K0 = 100.0 # Units are automatically added by the testsystem K = [K0, K0 * 10., K0 * 1.] powers = [2., 2., 4.] n_replicas = len(K) oscillators = [testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas)] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations":200} replica_exchange = hamiltonian_exchange.HamiltonianExchange.create(state, systems, positions, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() replica_exchange.extend(100) replica_exchange = resume(nc_filename, mpicomm=mpicomm) eq(replica_exchange.iteration, 200) replica_exchange.run()
def test_parse_ligand_filename(): molecule_name = "sustiva" input_filename = utils.get_data_filename("chemicals/sustiva/sustiva.mol2") name, ext = utils.parse_ligand_filename(input_filename) eq(name, "sustiva") eq(ext, ".mol2")
def test_inertia(): assert eq(order.compute_inertia_tensor(TRAJ1), order._compute_inertia_tensor_slow(TRAJ1)) assert eq(order.compute_inertia_tensor(TRAJ2), order._compute_inertia_tensor_slow(TRAJ2)) assert eq(order.compute_inertia_tensor(TRAJ3), order._compute_inertia_tensor_slow(TRAJ3))
def test_power_oscillators(mpicomm): nc_filename = tempfile.mkdtemp() + "/out.nc" temperature = 1 * unit.kelvin K0 = 100.0 # Units are automatically added by the testsystem K = [K0, K0 * 10., K0 * 1.] powers = [2., 2., 4.] n_replicas = len(K) oscillators = [testsystems.PowerOscillator(b=powers[i]) for i in range(n_replicas)] systems = [ho.system for ho in oscillators] positions = [ho.positions for ho in oscillators] state = ThermodynamicState(system=systems[0], temperature=temperature) parameters = {"number_of_iterations":2000} replica_exchange = hamiltonian_exchange.HamiltonianExchange.create(state, systems, positions, nc_filename, mpicomm=mpicomm, parameters=parameters) replica_exchange.run() u_permuted = replica_exchange.database.ncfile.variables["energies"][:] s = replica_exchange.database.ncfile.variables["states"][:] u = permute_energies(u_permuted, s) beta = (state.temperature * kB) ** -1. u0 = np.array([[testsystems.PowerOscillator.reduced_potential(beta, ho2.K, ho2.b, ho.K, ho.b) for ho in oscillators] for ho2 in oscillators]) l = np.log(u.mean(0)) l0 = np.log(u0) eq(l0, l, decimal=1)
def test_write_coordinates_reshape(): coordinates = np.random.randn(10,3) with LH5TrajectoryFile(temp, 'w') as f: f.write(coordinates) with LH5TrajectoryFile(temp) as f: eq(f.read(), coordinates.reshape(1,10,3), decimal=3)
def test_parallel_tempering_save_and_load(): nc_filename = tempfile.mkdtemp() + "/out.nc" T_min = 1.0 * unit.kelvin T_max = 10.0 * unit.kelvin n_temps = 3 ho = testsystems.HarmonicOscillator() system = ho.system positions = ho.positions coordinates = [positions] * n_temps parameters = {"number_of_iterations":5} rex = parallel_tempering.ParallelTempering.create(system, coordinates, nc_filename, T_min=T_min, T_max=T_max, n_temps=n_temps, parameters=parameters) rex.run() rex.extend(5) rex = resume(nc_filename) rex.run() eq(rex.__class__.__name__, "ParallelTempering")
def test_read_1(): with MDCRDTrajectoryFile(get_fn("frame0.mdcrd"), n_atoms=22) as f: xyz, _ = f.read() with MDCRDTrajectoryFile(get_fn("frame0.mdcrd"), n_atoms=22) as f: xyz3, _ = f.read(stride=3) eq(xyz[::3], xyz3)
def test_3p(): a = compute_distances(ptraj, pairs, periodic=True, opt=True) b = compute_displacements(ptraj, pairs, periodic=True, opt=True) print(a, b) eq(a, np.sqrt(np.sum(np.square(b), axis=2)))
def test_tica_shape(): model = tICA(n_components=3).fit([random.randn(100, 10)]) eq(model.eigenvalues_.shape, (3, )) eq(model.eigenvectors_.shape, (10, 3)) eq(model.components_.shape, (3, 10))
def test_load_mdcrd_with_psf(): traj = md.load(get_fn('ala_ala_ala.mdcrd'), top=get_fn('ala_ala_ala.psf')) ref_traj = md.load(get_fn('ala_ala_ala.pdb')) eq(traj.topology, ref_traj.topology) eq(traj.xyz, ref_traj.xyz)
def test_multichain_psf(): top = psf.load_psf(get_fn('3pqr_memb.psf')) # Check that each segment was turned into a chain eq(top.n_chains, 11) chain_lengths = [5162, 185, 97, 28, 24, 24, 45, 35742, 72822, 75, 73] for i, n in enumerate(chain_lengths): eq(top.chain(i).n_atoms, n) # There are some non-sequentially numbered residues across chain # boundaries... make sure resSeq is properly taken from the PSF eq(top.chain(0).residue(0).resSeq, 1) eq(top.chain(0).residue(-1).resSeq, 326) eq(top.chain(1).residue(0).resSeq, 340) eq(top.chain(1).residue(-1).resSeq, 350) eq(top.chain(2).residue(0).resSeq, 1) eq(top.chain(2).residue(-1).resSeq, 4) eq(top.chain(3).residue(0).resSeq, 1) eq(top.chain(3).residue(-1).resSeq, 1) eq(top.chain(4).residue(0).resSeq, 1) eq(top.chain(4).residue(-1).resSeq, 1) eq(top.chain(5).residue(0).resSeq, 1) eq(top.chain(5).residue(-1).resSeq, 1) eq(top.chain(6).residue(0).resSeq, 1) eq(top.chain(6).residue(-1).resSeq, 2) eq(top.chain(7).residue(0).resSeq, 1) eq(top.chain(7).residue(-1).resSeq, 276) eq(top.chain(8).residue(0).resSeq, 1) eq(top.chain(8).residue(-1).resSeq, 24274) eq(top.chain(9).residue(0).resSeq, 1) eq(top.chain(9).residue(-1).resSeq, 75) eq(top.chain(10).residue(0).resSeq, 1) eq(top.chain(10).residue(-1).resSeq, 73)
def test_load_freesolv_gaffmol2_vs_sybylmol2_vs_obabelpdb(get_fn, tmpdir): tar_filename = "freesolve_v0.3.tar.bz2" tar = tarfile.open(get_fn(tar_filename), mode="r:bz2") os.chdir(str(tmpdir)) tar.extractall() tar.close() with open("./v0.3/database.pickle", 'rb') as f: database = pickle.load(f) for key in database: gaff_filename = "./v0.3/mol2files_gaff/%s.mol2" % key pdb_filename = "./v0.3/mol2files_gaff/%s.pdb" % key sybyl_filename = "./v0.3/mol2files_sybyl/%s.mol2" % key cmd = "obabel -imol2 %s -opdb > %s 2>/dev/null" % (sybyl_filename, pdb_filename) assert os.system(cmd) == 0 t_pdb = md.load(pdb_filename) t_gaff = md.load(gaff_filename) t_sybyl = md.load(sybyl_filename) eq(t_pdb.n_atoms, t_gaff.n_atoms) eq(t_pdb.n_atoms, t_sybyl.n_atoms) eq(t_pdb.n_frames, t_gaff.n_frames) eq(t_pdb.n_frames, t_gaff.n_frames) eq(t_pdb.xyz, t_gaff.xyz, decimal=4) eq(t_pdb.xyz, t_sybyl.xyz, decimal=4) top_pdb, bonds_pdb = t_pdb.top.to_dataframe() top_gaff, bonds_gaff = t_gaff.top.to_dataframe() top_sybyl, bonds_sybyl = t_sybyl.top.to_dataframe() eq(top_sybyl.name.values, top_pdb.name.values) # eq(top_gaff.name.values, top_sybyl.name.values) # THEY CAN HAVE DIFFERENT NAMES, so this isn't TRUE! def make_bonds_comparable(bond_array): """Create a bond connectivity matrix from a numpy array of atom pairs. Avoids having to compare the order in which bonds are listed.""" n_bonds = len(bond_array) data = np.ones(n_bonds) i = bond_array[:, 0] j = bond_array[:, 1] matrix = scipy.sparse.coo_matrix((data, (i, j)), shape=(t_pdb.n_atoms, t_pdb.n_atoms)).toarray() return matrix + matrix.T # Symmetrize to account for (a ~ b) versus (b ~ a) bond_matrix_pdb = make_bonds_comparable(bonds_pdb) bond_matrix_gaff = make_bonds_comparable(bonds_gaff) bond_matrix_sybyl = make_bonds_comparable(bonds_sybyl) eq(bond_matrix_pdb, bond_matrix_gaff) eq(bond_matrix_pdb, bond_matrix_sybyl)
def test_mol2_status_bits(get_fn): trj = md.load_mol2(get_fn('status-bits.mol2')) eq(trj.topology.n_atoms, 18) eq(trj.topology.n_bonds, 18)
def test_mol2_dataframe(get_fn): top, bonds = mol2.mol2_to_dataframes(get_fn("imatinib.mol2")) eq(top.name[2], "N1") eq(top.atype[2], "n3") eq(top.resName[2], "LIG") eq(float(top.charge[2]), -0.732600)
def test_0p(): a = compute_distances(ptraj, pairs, periodic=True, opt=True) b = compute_distances(ptraj, pairs, periodic=True, opt=False) print(a, b) eq(a, b, decimal=3)
def test_1(): a = compute_displacements(ptraj, pairs, periodic=False, opt=True) b = compute_displacements(ptraj, pairs, periodic=False, opt=False) eq(a, b)
def test_1p(): a = compute_displacements(ptraj, pairs, periodic=True, opt=True) b = compute_displacements(ptraj, pairs, periodic=True, opt=False) eq(a, b, decimal=3)
def test_generator(): pairs2 = itertools.combinations(range(N_ATOMS), 2) a = compute_distances(ptraj, pairs) b = compute_distances(ptraj, pairs2) eq(a, b)
def test_2(): a = compute_distances(ptraj, pairs, periodic=False, opt=False) b = compute_displacements(ptraj, pairs, periodic=False, opt=False) eq(a, np.sqrt(np.sum(np.square(b), axis=2)))
def test_transform(): T = alignment.compute_transformation(xyz2, xyz1) xyz2_prime = T.transform(xyz2) eq(xyz1, xyz2_prime)
def test_0(): a = compute_distances(ptraj, pairs, periodic=False, opt=True) b = compute_distances(ptraj, pairs, periodic=False, opt=False) eq(a, b)
def test_rmsd_zero(): rmsd_kabsch = alignment.rmsd_kabsch(xyz1, xyz2) rmsd_qcp = alignment.rmsd_qcp(xyz1, xyz2) eq(float(rmsd_kabsch), 0.0, decimal=5) eq(float(rmsd_qcp), 0.0, decimal=5)
def test_5(): # simple wrap around along the z axis. xyz = np.array([[[0.0, 0.0, 0.0], [0.0, 0.0, 2.2]]]) box = np.eye(3, 3).reshape(1, 3, 3) result = _displacement_mic(xyz, np.array([[0, 1]]), box, True) eq(result, np.array([[[0, 0, 0.2]]]))
def test_get_velocities_and_forces(): """Write data with velocities and forces, and read it back""" # NOTE: this is a test of a hidden API xyz = np.array(np.random.randn(500, 50, 3), dtype=np.float32) vel = np.array(np.random.randn(500, 50, 3), dtype=np.float32) forces = np.array(np.random.randn(500, 50, 3), dtype=np.float32) box = np.array(np.random.randn(500, 3, 3), dtype=np.float32) time = np.array(np.random.randn(500), dtype=np.float32) step = np.array(np.arange(500), dtype=np.int32) lambd = np.array(np.random.randn(500), dtype=np.float32) with TRRTrajectoryFile(temp, 'w') as f: f._write(xyz=xyz, time=time, step=step, box=box, lambd=lambd, vel=vel, forces=forces) with TRRTrajectoryFile(temp) as f: xyz2, time2, step2, box2, lambd2, vel2, forces2 = f._read( n_frames=500, atom_indices=None, get_velocities=True, get_forces=True ) eq(xyz, xyz2) eq(vel, vel2) eq(forces, forces2) eq(time, time2) eq(step, step2) eq(lambd, lambd2)
def test_rmsd_nonzero(): rmsd_kabsch = alignment.rmsd_kabsch(xyz1, xyz3) rmsd_qcp = alignment.rmsd_qcp(xyz1, xyz3) eq(rmsd_kabsch, rmsd_qcp, decimal=5)
def test_contact_0(get_fn): pdb = md.load(get_fn('bpti.pdb')) contacts = np.loadtxt(get_fn('contacts.dat')).astype(int) ca, ca_pairs = md.compute_contacts(pdb, contacts, scheme='ca') closest, closest_pairs = md.compute_contacts(pdb, contacts, scheme='closest') closest_heavy, closest_heavy_pairs = md.compute_contacts( pdb, contacts, scheme='closest-heavy') sidechain, sidechain_pairs = md.compute_contacts(pdb, contacts, scheme='sidechain') sidechain_heavy, sidechain_heavy_pairs = md.compute_contacts( pdb, contacts, scheme='sidechain-heavy') ref_ca = np.loadtxt(get_fn('cc_ca.dat')) ref_closest = np.loadtxt(get_fn('cc_closest.dat')) ref_closest_heavy = np.loadtxt(get_fn('cc_closest-heavy.dat')) ref_sidechain = np.loadtxt(get_fn('cc_sidechain.dat')) ref_sidechain_heavy = np.loadtxt(get_fn('cc_sidechain-heavy.dat')) eq(ref_ca, ca.flatten()) eq(ref_closest, closest.flatten()) eq(ref_closest_heavy, closest_heavy.flatten()) eq(ref_sidechain, sidechain.flatten()) eq(ref_sidechain_heavy, sidechain_heavy.flatten()) eq(contacts, ca_pairs) eq(contacts, closest_pairs) eq(contacts, closest_heavy_pairs) eq(contacts, sidechain_pairs) eq(contacts, sidechain_heavy_pairs)
def test_get_velocities_forces_atom_indices_2(): # NOTE: this is a test of a hidden API xyz = np.array(np.random.randn(500, 50, 3), dtype=np.float32) vel = np.array(np.random.randn(500, 50, 3), dtype=np.float32) forces = np.array(np.random.randn(500, 50, 3), dtype=np.float32) box = np.array(np.random.randn(500, 3, 3), dtype=np.float32) time = np.array(np.random.randn(500), dtype=np.float32) step = np.array(np.arange(500), dtype=np.int32) lambd = np.array(np.random.randn(500), dtype=np.float32) with TRRTrajectoryFile(temp, 'w') as f: f._write(xyz=xyz, time=time, step=step, box=box, lambd=lambd, vel=vel, forces=forces) with TRRTrajectoryFile(temp) as f: xyz2, time2, step2, box2, lambd2, vel2, forces2 = f._read( n_frames=500, atom_indices=slice(None, None, 2), get_velocities=True, get_forces=True ) eq(xyz[:, ::2], xyz2) eq(vel[:, ::2], vel2) eq(forces[:, ::2], forces2) eq(time, time2) eq(step, step2) eq(lambd, lambd2) pass
def test_4ZUO(get_fn): t = load(get_fn('4ZUO.pdb')) eq(t.n_frames, 1) eq(t.n_atoms, 6200) # this is a random line from the file #ATOM 1609 O GLY A 201 -25.423 13.774 -25.234 1.00 8.92 O atom = list(t.top.atoms)[1525] eq(atom.element.symbol, 'O') eq(atom.residue.name, 'GLY') eq(atom.index, 1525) eq(t.xyz[0, 1525], np.array([-25.423, 13.774, -25.234]) / 10) # converting to NM # this is atom 1577 in the PDB #CONECT 1577 5518 #ATOM 1577 O HIS A 197 -18.521 9.724 -32.805 1.00 8.81 O #HETATM 5518 K K A 402 -19.609 10.871 -35.067 1.00 9.11 K atom = list(t.top.atoms)[1493] eq(atom.name, 'O') eq(atom.residue.name, 'HIS') eq([(a1.index, a2.index) for a1, a2 in t.top.bonds if a1.index == 1493 or a2.index == 1493], [(1492, 1493), (1493, 5129)])
def test_seek(get_fn): reference = TRRTrajectoryFile(get_fn('frame0.trr')).read()[0] with TRRTrajectoryFile(get_fn('frame0.trr')) as f: eq(len(f), len(reference)) eq(len(f.offsets), len(reference)) eq(f.tell(), 0) eq(f.read(1)[0][0], reference[0]) eq(f.tell(), 1) xyz = f.read(1)[0][0] eq(xyz, reference[1]) eq(f.tell(), 2) f.seek(0) eq(f.tell(), 0) xyz = f.read(1)[0][0] eq(f.tell(), 1) eq(xyz, reference[0]) f.seek(5) eq(f.read(1)[0][0], reference[5]) eq(f.tell(), 6) f.seek(-5, 1) eq(f.tell(), 1) eq(f.read(1)[0][0], reference[1])
def test_pdbstructure_3(): loc = pdbstructure.Atom.Location(' ', [1,2,3], 1.0, 20.0, "XXX") expected = [1, 2, 3] for i, c in enumerate(loc): eq(expected[i], c)
def check(): out1 = md.load(os.path.join(output_dir, fn2), **load_kwargs_check1) out2 = md.load(os.path.join(output_dir, 'subset.' + fn2), **load_kwargs_check2) out3 = md.load(os.path.join(output_dir, 'stride.' + fn2), **load_kwargs_check1) eq(out1.xyz, TRAJ.xyz) eq(out2.xyz, TRAJ.xyz[:, atom_indices]) eq(out3.xyz, TRAJ.xyz[::3]) if ext1 not in ['.binpos', '.lh5'] and ext2 not in ['.binpos', '.lh5']: # binpos doesn't save unitcell information eq(out1.unitcell_vectors, TRAJ.unitcell_vectors, decimal=2) eq(out2.unitcell_vectors, TRAJ.unitcell_vectors, decimal=2) eq(out3.unitcell_vectors, TRAJ.unitcell_vectors[::3], decimal=2) if all(e in ['.xtc', '.trr', '.nc', '.h5'] for e in [ext1, ext2]): # these formats contain time information eq(out1.time, TRAJ.time) eq(out2.time, TRAJ.time) eq(out3.time, TRAJ.time[::3]) if ext2 in ['.pdb', '.h5', '.lh5']: # these formats contain a topology in the file that was # read from disk eq(out1.topology, TRAJ.topology) eq(out2.topology, TRAJ.topology.subset(atom_indices)) eq(out3.topology, TRAJ.topology)
def test_2EQQ_0(get_fn): # this is an nmr structure with 20 models t = load(get_fn('2EQQ.pdb')) assert eq(t.n_frames, 20) assert eq(t.n_atoms, 423) assert eq(len(list(t.top.residues)), 28)
def test_1vii_url_and_gz(get_fn): t1 = load_pdb('http://www.rcsb.org/pdb/files/1vii.pdb.gz') t2 = load_pdb('http://www.rcsb.org/pdb/files/1vii.pdb') t3 = load_pdb(get_fn('1vii.pdb.gz')) t4 = load_pdb(get_fn('1vii.pdb')) eq(t1.n_frames, 1) eq(t1.n_frames, t2.n_frames) eq(t1.n_frames, t3.n_frames) eq(t1.n_frames, t4.n_frames) eq(t1.n_atoms, t2.n_atoms) eq(t1.n_atoms, t3.n_atoms) eq(t1.n_atoms, t4.n_atoms)
def test_reporter_subset(): tempdir = os.path.join(dir, 'test_2') os.makedirs(tempdir) pdb = PDBFile(get_fn('native2.pdb')) pdb.topology.setUnitCellDimensions([2, 2, 2]) forcefield = ForceField('amber99sbildn.xml', 'amber99_obc.xml') system = forcefield.createSystem(pdb.topology, nonbondedMethod=CutoffPeriodic, nonbondedCutoff=1 * nanometers, constraints=HBonds, rigidWater=True) integrator = LangevinIntegrator(300 * kelvin, 1.0 / picoseconds, 2.0 * femtoseconds) integrator.setConstraintTolerance(0.00001) platform = Platform.getPlatformByName('Reference') simulation = Simulation(pdb.topology, system, integrator, platform) simulation.context.setPositions(pdb.positions) simulation.context.setVelocitiesToTemperature(300 * kelvin) hdf5file = os.path.join(tempdir, 'traj.h5') ncfile = os.path.join(tempdir, 'traj.nc') dcdfile = os.path.join(tempdir, 'traj.dcd') atomSubset = [0, 1, 2, 4, 5] reporter = HDF5Reporter(hdf5file, 2, coordinates=True, time=True, cell=True, potentialEnergy=True, kineticEnergy=True, temperature=True, velocities=True, atomSubset=atomSubset) reporter2 = NetCDFReporter(ncfile, 2, coordinates=True, time=True, cell=True, atomSubset=atomSubset) reporter3 = DCDReporter(dcdfile, 2, atomSubset=atomSubset) simulation.reporters.append(reporter) simulation.reporters.append(reporter2) simulation.reporters.append(reporter3) simulation.step(100) reporter.close() reporter2.close() t = md.load(get_fn('native.pdb')) t.restrict_atoms(atomSubset) with HDF5TrajectoryFile(hdf5file) as f: got = f.read() eq(got.temperature.shape, (50, )) eq(got.potentialEnergy.shape, (50, )) eq(got.kineticEnergy.shape, (50, )) eq(got.coordinates.shape, (50, len(atomSubset), 3)) eq(got.velocities.shape, (50, len(atomSubset), 3)) eq(got.cell_lengths, 2 * np.ones((50, 3))) eq(got.cell_angles, 90 * np.ones((50, 3))) eq(got.time, 0.002 * 2 * (1 + np.arange(50))) assert f.topology == md.load(get_fn('native.pdb'), atom_indices=atomSubset).topology with NetCDFTrajectoryFile(ncfile) as f: xyz, time, cell_lengths, cell_angles = f.read() eq(cell_lengths, 20 * np.ones((50, 3))) eq(cell_angles, 90 * np.ones((50, 3))) eq(time, 0.002 * 2 * (1 + np.arange(50))) eq(xyz.shape, (50, len(atomSubset), 3)) hdf5_traj = md.load(hdf5file) dcd_traj = md.load(dcdfile, top=hdf5_traj) netcdf_traj = md.load(ncfile, top=hdf5_traj) # we don't have to convert units here, because md.load already handles # that eq(hdf5_traj.xyz, netcdf_traj.xyz) eq(hdf5_traj.unitcell_vectors, netcdf_traj.unitcell_vectors) eq(hdf5_traj.time, netcdf_traj.time) eq(dcd_traj.xyz, hdf5_traj.xyz) eq(dcd_traj.unitcell_vectors, hdf5_traj.unitcell_vectors)
def test_pdbstructure_2(): atom = pdbstructure.Atom("ATOM 2209 CB TYR A 299 6.167 22.607 20.046 1.00 8.12 C") expected = np.array([6.167, 22.607, 20.046]) for i, c in enumerate(atom.iter_coordinates()): eq(expected[i], c)
def test_load_with_frame(get_fn): t1 = md.load(get_fn('frame0.xtc'), top=get_fn('frame0.pdb'), frame=3) t2 = md.load(get_fn('frame0.xtc'), top=get_fn('frame0.pdb')) t2 = t2.slice([3]) eq(t1.xyz, t2.xyz) eq(t1.time, t2.time)
def test_reporter(): tempdir = os.path.join(dir, 'test1') os.makedirs(tempdir) pdb = PDBFile(get_fn('native.pdb')) forcefield = ForceField('amber99sbildn.xml', 'amber99_obc.xml') # NO PERIODIC BOUNDARY CONDITIONS system = forcefield.createSystem(pdb.topology, nonbondedMethod=CutoffNonPeriodic, nonbondedCutoff=1.0 * nanometers, constraints=HBonds, rigidWater=True) integrator = LangevinIntegrator(300 * kelvin, 1.0 / picoseconds, 2.0 * femtoseconds) integrator.setConstraintTolerance(0.00001) platform = Platform.getPlatformByName('Reference') simulation = Simulation(pdb.topology, system, integrator, platform) simulation.context.setPositions(pdb.positions) simulation.context.setVelocitiesToTemperature(300 * kelvin) hdf5file = os.path.join(tempdir, 'traj.h5') ncfile = os.path.join(tempdir, 'traj.nc') dcdfile = os.path.join(tempdir, 'traj.dcd') reporter = HDF5Reporter(hdf5file, 2, coordinates=True, time=True, cell=True, potentialEnergy=True, kineticEnergy=True, temperature=True, velocities=True) reporter2 = NetCDFReporter(ncfile, 2, coordinates=True, time=True, cell=True) reporter3 = DCDReporter(dcdfile, 2) simulation.reporters.append(reporter) simulation.reporters.append(reporter2) simulation.reporters.append(reporter3) simulation.step(100) reporter.close() reporter2.close() with HDF5TrajectoryFile(hdf5file) as f: got = f.read() eq(got.temperature.shape, (50, )) eq(got.potentialEnergy.shape, (50, )) eq(got.kineticEnergy.shape, (50, )) eq(got.coordinates.shape, (50, 22, 3)) eq(got.velocities.shape, (50, 22, 3)) eq(got.cell_lengths, None) eq(got.cell_angles, None) eq(got.time, 0.002 * 2 * (1 + np.arange(50))) assert f.topology == md.load(get_fn('native.pdb')).top with NetCDFTrajectoryFile(ncfile) as f: xyz, time, cell_lengths, cell_angles = f.read() eq(cell_lengths, None) eq(cell_angles, None) eq(time, 0.002 * 2 * (1 + np.arange(50))) hdf5_traj = md.load(hdf5file) dcd_traj = md.load(dcdfile, top=get_fn('native.pdb')) netcdf_traj = md.load(ncfile, top=get_fn('native.pdb')) # we don't have to convert units here, because md.load already # handles that assert hdf5_traj.unitcell_vectors is None eq(hdf5_traj.xyz, netcdf_traj.xyz) eq(hdf5_traj.unitcell_vectors, netcdf_traj.unitcell_vectors) eq(hdf5_traj.time, netcdf_traj.time) eq(dcd_traj.xyz, hdf5_traj.xyz)