Beispiel #1
0
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()
Beispiel #2
0
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)
Beispiel #3
0
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
Beispiel #4
0
def test_calc_sasa():
    from pyxmolpp2.pdb import PdbFile
    from pyxmolpp2.geometry import calc_sasa
    from timeit import default_timer as timer
    import numpy as np
    import glob

    for filename in sorted(glob.glob("tests_dataset/pdb/rcsb/*.pdb")):
        frame = PdbFile(filename).get_frame()
        ats = frame.asAtoms
        radii = np.array([1.0] * ats.size)
        coords = ats.toCoords

        t1 = timer()
        assert calc_sasa(coords, radii, 0.0, np.array([0],
                                                      dtype=np.intc)).size == 1
        t2 = timer()
        assert calc_sasa(coords, radii, 0.0,
                         np.array([0, 1, 2, 3, 4], dtype=np.intc)).size == 5
        t3 = timer()
        assert calc_sasa(coords, radii, 0.0).size == radii.size
        t4 = timer()
        assert calc_sasa(coords, radii, 0.0, n_samples=1).size == radii.size

        T1, T3 = t2 - t1, t4 - t3
        assert T3 > T1
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
Beispiel #6
0
def test_torsions():
    from pyxmolpp2.polymer import TorsionAngleFactory, OutOfRangeResidue
    from pyxmolpp2.pdb import PdbFile
    from pyxmolpp2.geometry import Degrees
    import glob
    filenames = sorted(glob.glob("tests_dataset/pdb/rcsb/*.pdb"))

    for filename in filenames:
        frame = PdbFile(filename).get_frame()
        for r in frame.asResidues:
            try:
                angles = [
                    TorsionAngleFactory.phi(r),
                    TorsionAngleFactory.psi(r),
                    TorsionAngleFactory.omega(r),
                    TorsionAngleFactory.chi1(r),
                    TorsionAngleFactory.chi2(r),
                    TorsionAngleFactory.chi3(r),
                    TorsionAngleFactory.chi4(r),
                    TorsionAngleFactory.chi5(r),
                ]
                for angle in angles:
                    if angle:
                        try:
                            value = angle.value()
                            angle.set(value + Degrees(30), Degrees(0))
                        except Exception:
                            pass
            except OutOfRangeResidue:
                pass
Beispiel #7
0
def test_read_frame():
    from pyxmolpp2.pdb import PdbFile
    import glob

    for filename in glob.glob("tests_dataset/pdb/rcsb/*.pdb"):

        frame = PdbFile(filename).get_frame()

        assert frame.asAtoms.size > 0
Beispiel #8
0
def test_read_frames():
    from pyxmolpp2.pdb import PdbFile
    import glob

    for filename in glob.glob("tests_dataset/pdb/rcsb/*.pdb"):

        frames = PdbFile(filename).get_frames()

        assert len(frames) > 0
        print(len(frames))
Beispiel #9
0
def test_rmsd():
    from pyxmolpp2.polymer import AtomName
    from pyxmolpp2.pdb import PdbFile
    from pyxmolpp2.geometry import calc_rmsd, calc_alignment, calc_geom_center
    import glob

    filenames = sorted(glob.glob("tests_dataset/pdb/rcsb/*.pdb"))

    for i in range(len(filenames) - 1):
        frame_1 = PdbFile(filenames[i]).get_frame()
        residues_1 = [
            a.rId for a in frame_1.asChains[0].asAtoms.filter(
                lambda a: a.name.str == "CA")
        ]

        for j in range(i + 1, len(filenames)):
            frame_2 = PdbFile(filenames[j]).get_frame()
            residues_2 = [
                a.rId for a in frame_2.asChains[0].asAtoms.filter(
                    lambda a: a.name.str == "CA")
            ]
            common = set(residues_1) & set(residues_2)
            ats1 = frame_1.asChains[0].asAtoms.filter(lambda a: (
                a.id == a.residue[AtomName("CA")].id) and a.rId in common)
            ats2 = frame_2.asChains[0].asAtoms.filter(lambda a: (
                a.id == a.residue[AtomName("CA")].id) and a.rId in common)

            assert ats1.size == len(common)
            assert ats2.size == len(common)
            assert ats1.size == ats2.size

            assert len(ats1) > 3
            assert len(ats2) > 3
            aligment = calc_alignment(ats1.toCoords, ats2.toCoords)
            not_aligned_rmsd = calc_rmsd(ats1.toCoords, ats2.toCoords)
            aligned_rmsd = calc_rmsd(ats1.toCoords, ats2.toCoords, aligment)
Beispiel #10
0
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)))
Beispiel #12
0
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
Beispiel #13
0
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
Beispiel #14
0
def test_read_empty_file():
    from pyxmolpp2.pdb import PdbFile
    import os

    assert PdbFile(os.devnull).get_frame().asChains.size == 0
Beispiel #15
0
def test_read_non_existent_file():
    from pyxmolpp2.pdb import PdbFile, PdbException

    with pytest.raises(PdbException):
        PdbFile("does_not_exists.pdb")
"""

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))
for frame in frames:
    print("\tMODEL #%d contains %d molecules, total %d atoms" %
          (frame.index, frame.asChains.size, frame.asAtoms.size))

# Write first model to PDB file using altered records
frames[0].to_pdb("out.pdb", altered_records)