|
| 1 | +from click.testing import CliRunner |
| 2 | +from contextlib import contextmanager |
| 3 | +import pytest |
| 4 | +from importlib import resources |
| 5 | +from openpathsampling.tests.test_helpers import data_filename |
| 6 | +import openpathsampling as paths |
| 7 | + |
| 8 | +from paths_cli.commands.load_trajectory import * |
| 9 | + |
| 10 | + |
| 11 | +@contextmanager |
| 12 | +def run_load_trajectory(args): |
| 13 | + runner = CliRunner() |
| 14 | + with runner.isolated_filesystem(): |
| 15 | + storage = paths.Storage("setup.nc", 'w') |
| 16 | + storage.close() |
| 17 | + results = runner.invoke( |
| 18 | + load_trajectory, |
| 19 | + args |
| 20 | + ) |
| 21 | + assert results.exit_code == 0 |
| 22 | + st = paths.Storage("setup.nc", mode='r') |
| 23 | + assert len(st.trajectories) == 1 |
| 24 | + yield st |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("with_top", [True, False]) |
| 28 | +@pytest.mark.parametrize("with_tag", [True, False]) |
| 29 | +def test_load_trajectory_pdb(with_top, with_tag): |
| 30 | + # test that we can load a PDB file with or without topology; also tests |
| 31 | + # that the taging works correctly |
| 32 | + pytest.importorskip("openmm") |
| 33 | + pytest.importorskip("mdtraj") |
| 34 | + pdb_path = data_filename("ala_small_traj.pdb") |
| 35 | + out_file = "setup.nc" |
| 36 | + args = [ |
| 37 | + pdb_path, |
| 38 | + '--append-file', out_file, |
| 39 | + ] |
| 40 | + if with_top: |
| 41 | + args.extend(['--top', pdb_path]) |
| 42 | + |
| 43 | + if with_tag: |
| 44 | + args.extend(['--tag', 'init_snap']) |
| 45 | + |
| 46 | + with run_load_trajectory(args) as st: |
| 47 | + traj = st.trajectories[0] |
| 48 | + assert len(traj) == 10 |
| 49 | + if with_tag: |
| 50 | + tagged = st.tags['init_snap'] |
| 51 | + assert tagged == traj |
| 52 | + |
| 53 | +def test_load_trajectory_trr(): |
| 54 | + pytest.importorskip("openmm") |
| 55 | + pytest.importorskip("mdtraj") |
| 56 | + trr = data_filename("gromacs_engine/project_trr/0000000.trr") |
| 57 | + gro = data_filename("gromacs_engine/conf.gro") |
| 58 | + out_file = "setup.nc" |
| 59 | + args = [ |
| 60 | + trr, |
| 61 | + '--append-file', out_file, |
| 62 | + '--top', gro, |
| 63 | + ] |
| 64 | + with run_load_trajectory(args) as st: |
| 65 | + traj = st.trajectories[0] |
| 66 | + assert len(traj) == 4 |
| 67 | + |
| 68 | +def test_load_trajectory_bad_topology(): |
| 69 | + pytest.importorskip("openmm") |
| 70 | + pytest.importorskip("mdtraj") |
| 71 | + trr = data_filename("gromacs_engine/project_trr/0000000.trr") |
| 72 | + pdb = data_filename("tip4p_water.pdb") |
| 73 | + out_file = "setup.nc" |
| 74 | + args = [ |
| 75 | + trr, |
| 76 | + '--append-file', out_file, |
| 77 | + '--top', pdb, |
| 78 | + ] |
| 79 | + runner = CliRunner() |
| 80 | + with runner.isolated_filesystem(): |
| 81 | + storage = paths.Storage("setup.nc", 'w') |
| 82 | + storage.close() |
| 83 | + result = runner.invoke( |
| 84 | + load_trajectory, |
| 85 | + args |
| 86 | + ) |
| 87 | + assert result.exit_code == 1 |
| 88 | + assert "topology" in str(result.exception) |
| 89 | + assert "same atoms" in str(result.exception) |
0 commit comments