def test_traj_exceptions(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.polymer import AtomName from pyxmolpp2.trjtool import DatFile from pyxmolpp2.trajectory import Trajectory, TrajectoryException records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) datfile1 = DatFile("tests_dataset/trjtool/GB1/run00001.dat") with pytest.raises(TrajectoryException): frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb").get_frame( records) frame.asAtoms[0].name = AtomName("XX") trj = Trajectory(frame, True) trj.push_trajectory_portion(datfile1) with pytest.raises(TrajectoryException): frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb").get_frame( records) frame.asAtoms[0].delete() trj = Trajectory(frame, True) trj.push_trajectory_portion(datfile1)
def test_traj_size(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.trjtool import DatFile from pyxmolpp2.trajectory import Trajectory records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb").get_frame( records) assert frame.asAtoms.size > 0 trj = Trajectory(frame, True) trj.push_trajectory_portion( DatFile("tests_dataset/trjtool/GB1/run00001.dat")) assert len(trj[0:10]) == 10 assert len(trj[0:10:10]) == 1 assert len(trj[0:10:100]) == 1 assert len(trj[0:100:10]) == 10 assert len(trj[0:100:-10]) == 0 assert len(trj[100:0:-1]) == 100 assert len(trj[0:100:-1]) == 0
def test_trajectory_integer_indexing(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.trjtool import DatFile from pyxmolpp2.trajectory import Trajectory records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb", records).get_frame() trj = Trajectory(frame, True) trj.push_trajectory_portion( DatFile("tests_dataset/trjtool/GB1/run00001.dat")) n = trj.size trj[-n] trj[n - 1] with pytest.raises(IndexError): trj[n] with pytest.raises(IndexError): trj[-n - 1] frame1 = trj[0] frame2 = trj[-n] assert frame1.index == frame2.index
def test_read_trjtool(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.trjtool import DatFile records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7,12]) frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb",records).get_frame() assert frame.asAtoms.size > 0 datfile = DatFile("tests_dataset/trjtool/GB1/run00001.dat") assert datfile.n_frames == 1000 assert datfile.match(frame.asAtoms) assert datfile.n_atoms_per_frame == frame.asAtoms.size datfile.set_coordinates(0, frame.asAtoms, list(range(frame.asAtoms.size)))
def test_read_trjtool(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.amber import NetCDFTrajectoryFile records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) frame = PdbFile("tests_dataset/amber/GB1_F30C_MTSL/box.pdb", records).get_frame() assert frame.asAtoms.size > 0 datfile = NetCDFTrajectoryFile( "tests_dataset/amber/GB1_F30C_MTSL/GB1_F30C_MTSL_10_frames.nc") assert datfile.n_frames == 10 assert datfile.match(frame.asAtoms) assert datfile.n_atoms_per_frame == frame.asAtoms.size datfile.set_coordinates(0, frame.asAtoms, list(range(frame.asAtoms.size)))
def test_traj_iteration(): from pyxmolpp2.pdb import PdbFile, AlteredPdbRecords, StandardPdbRecords, RecordName, FieldName from pyxmolpp2.trjtool import DatFile from pyxmolpp2.trajectory import Trajectory records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) frame = PdbFile("tests_dataset/trjtool/GB1/run00001.pdb").get_frame( records) assert frame.asAtoms.size > 0 datfile1 = DatFile("tests_dataset/trjtool/GB1/run00001.dat") datfile2 = DatFile("tests_dataset/trjtool/GB1/run00002.dat") trj = Trajectory(frame, True) trj.push_trajectory_portion(datfile1) trj.push_trajectory_portion(datfile2) assert trj.size == datfile1.n_frames + datfile2.n_frames n = 0 stride = 50 for f in trj[::stride]: r = f.asAtoms[0].r n += 1 assert trj.size // stride == n n = 0 stride = 1 ats = frame.asResidues[0].asAtoms trj.set_update_list(ats) for f in trj[::stride]: n += 1 assert trj.size // stride == n
def traj_from_dir(path: str, reference_pdb: Union[str, None] = None, stride: int = 1, first: int = 1, last: int = 1, pattern: str = "run%05d", subdir="5_run", filetype: str = "dat") \ -> Tuple[pyxmolpp2.trajectory.Trajectory, pyxmolpp2.polymer.Frame]: import os from tqdm import tqdm from pyxmolpp2.trajectory import Trajectory from pyxmolpp2.trjtool import DatFile from pyxmolpp2.amber import NetCDFTrajectoryFile from pyxmolpp2.pdb import PdbFile, \ AlteredPdbRecords, \ StandardPdbRecords, \ RecordName, \ FieldName if reference_pdb is None: for pdb in [ os.path.join(path, "5_run", "run00001.pdb"), os.path.join(path, "5_run", "step-1.pdb"), os.path.join(path, "ref", "ref.pdb") ]: if os.path.isfile(pdb): reference_pdb = pdb break if reference_pdb is None: raise RuntimeError("Reference pdb is not specified") if not os.path.isfile(reference_pdb): if not os.path.isfile(os.path.join(path, reference_pdb)): raise RuntimeError("Reference pdb is not found: `%s`" % reference_pdb) reference_pdb = os.path.join(path, reference_pdb) altered_records = AlteredPdbRecords(StandardPdbRecords.instance()) altered_records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) ref_frame = PdbFile(reference_pdb, altered_records).get_frame() coordinate_files = [ os.path.join(path, subdir, "%s.%s" % ((pattern % i), filetype)) for i in range(first, last + 1, stride) ] traj = Trajectory(ref_frame, check_portions_to_match_reference=True) if filetype == "dat": portion_type = DatFile # type: Callable[[str], pyxmolpp2.trajectory.TrajectoryPortion] elif filetype == "pdb": portion_type = lambda filename: PdbFile(filename, altered_records ) # noqa: E731 elif filetype == "nc": portion_type = NetCDFTrajectoryFile else: raise RuntimeError("Unknown trajectory coordinate file type `%s`" % filetype) for coordinate_file in tqdm(coordinate_files, leave=False, desc="checking input files"): if not os.access(coordinate_file, os.O_RDONLY): raise RuntimeError("Can't access file `%s`" % coordinate_file) traj.push_trajectory_portion(portion_type(coordinate_file)) return traj, ref_frame
def test_read_field_error(): from pyxmolpp2.pdb import PdbFile, StandardPdbRecords, AlteredPdbRecords, RecordName, FieldName, PdbException import glob records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("serial"), [4, 6]) #print(glob.glob("tests_dataset/pdb/rcsb/*.pdb")) for filename in glob.glob("tests_dataset/pdb/rcsb/*.pdb"): # print(filename) with pytest.raises(PdbException): PdbFile(filename, records).get_frame() records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("x"), [4, 4]) #print(glob.glob("tests_dataset/pdb/rcsb/*.pdb")) for filename in glob.glob("tests_dataset/pdb/rcsb/*.pdb"): # print(filename) with pytest.raises(PdbException): frame = PdbFile(filename, records).get_frame() # print(frame) records = AlteredPdbRecords(StandardPdbRecords.instance()) records.alter_record(RecordName("ATOM"), FieldName("resName"), [1, 85]) #print(glob.glob("tests_dataset/pdb/rcsb/*.pdb")) for filename in glob.glob("tests_dataset/pdb/rcsb/*.pdb"): # print(filename) with pytest.raises(PdbException): frame = PdbFile(filename, records).get_frame()
------------------- ``pyxmolpp2`` supports reading/writing non-standard PDB files. To read such file you need to specify a set of new records (usually based on :py:class:`~pyxmolpp2.pdb.StandardPdbRecords` records). Full set of available :py:class:`~pyxmolpp2.pdb.RecordName`, :py:class:`~pyxmolpp2.pdb.FiledName` and standard values you can find `here <https://github.com/sizmailov/pyxmolpp2/blob/master/src/xmol/pdb/BundledPDBRecordTypesBaseInit.cpp#L32>`_ """ import pyxmolpp2 from pyxmolpp2.pdb import AlteredPdbRecords, StandardPdbRecords, FieldName, RecordName, PdbFile import os # Create PDB records description based on standard records altered_records = AlteredPdbRecords( pyxmolpp2.pdb.StandardPdbRecords.instance()) # Expand ATOM.serial record to columns 7-12 altered_records.alter_record(RecordName("ATOM"), FieldName("serial"), [7, 12]) pdb_filename = os.path.join(os.environ["TEST_DATA_PATH"], "trjtool/GB1/run00001.pdb") # Define a PDB file with altered records fields layout pdb_file = PdbFile(pdb_filename, altered_records) # Read all frames (i.e. MODELS) from PDB frames = pdb_file.get_frames() # Print some info about frames print("PDB contains %d MODELS: " % len(frames))