def test_AtomName(): from pyxmolpp2.polymer import AtomName with pytest.raises(RuntimeError): a = AtomName("ATOME") assert AtomName("ATOM").str == "ATOM"
def test_range_exceptions(): from pyxmolpp2.polymer import OutOfRangeAtomSelection,\ OutOfRangeChainSelection,\ OutOfRangeResidueSelection, \ OutOfRangeChain, \ OutOfRangeFrame, \ OutOfRangeResidue, \ AtomName frame = make_polyglycine([("A",1),("B",4),("C",3)]) # with pytest.raises(OutOfRangeFrame): # x = frame[3] # with pytest.raises(OutOfRangeChain): # x = frame.asChains[0][3] with pytest.raises(OutOfRangeResidue): x = frame.asResidues[0][AtomName("ca")] assert frame.asResidues[0][AtomName("CA")].name.str == "CA" natoms = frame.asAtoms.size nresidues = frame.asResidues.size nchains = frame.asChains.size frame.asAtoms[0] frame.asAtoms[-natoms] frame.asResidues[0] frame.asResidues[-nresidues] frame.asChains[0] frame.asChains[-nchains] with pytest.raises(OutOfRangeAtomSelection): x = frame.asAtoms[natoms] with pytest.raises(OutOfRangeAtomSelection): x = frame.asAtoms[-natoms-1] with pytest.raises(OutOfRangeResidueSelection): x = frame.asResidues[nresidues] with pytest.raises(OutOfRangeResidueSelection): x = frame.asResidues[-nresidues-1] with pytest.raises(OutOfRangeChainSelection): x = frame.asChains[nchains] with pytest.raises(OutOfRangeChainSelection): x = frame.asChains[-nchains-1]
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 make_polyglycine( chain_lengths, no_reserve=True): from pyxmolpp2.polymer import Frame from pyxmolpp2.polymer import ChainName from pyxmolpp2.polymer import AtomName from pyxmolpp2.polymer import ResidueName, ResidueId from pyxmolpp2.geometry import XYZ aid=1 rid=1 frame = Frame(0) for chainId, N in chain_lengths: if no_reserve: c = frame.emplace(ChainName(chainId)) else: c = frame.emplace(ChainName(chainId),N) for i in range(N): if no_reserve: r = c.emplace(ResidueName("GLY"),ResidueId(rid)) else: r = c.emplace(ResidueName("GLY"),ResidueId(rid),7) rid+=1 for aname in ["N","H","CA","HA2","HA3","C","O"]: r.emplace(AtomName(aname),aid,XYZ(1,2,3)) aid+=1 return frame
def test_lookup_after_rename(): from pyxmolpp2.polymer import ChainName, ResidueId, AtomName, \ OutOfRangeResidue, OutOfRangeFrame, OutOfRangeChain frame = make_polyglycine([("A",2)]) frame[ChainName("A")] # does not throw frame[ChainName("A")][ResidueId(2)] # does not throw frame[ChainName("A")][ResidueId(2)][AtomName("CA")] # does not throw frame[ChainName("A")][ResidueId(2)][AtomName("CA")].name = AtomName("CX") frame[ChainName("A")][ResidueId(2)].id = ResidueId(99) frame[ChainName("A")].name = ChainName("X") frame[ChainName("X")] # does not throw frame[ChainName("X")][ResidueId(99)] # does not throw frame[ChainName("X")][ResidueId(99)][AtomName("CX")] # does not throw
def test_lookup_by_name(): from pyxmolpp2.polymer import ChainName, ResidueId, AtomName, \ OutOfRangeResidue, OutOfRangeFrame, OutOfRangeChain frame = make_polyglycine([("A",2)]) frame[ChainName("A")] # does not throw frame[ChainName("A")][ResidueId(2)] # does not throw frame[ChainName("A")][ResidueId(2)][AtomName("CA")] # does not throw frame[ChainName("A")][ResidueId(2)][AtomName("N")] # does not throw with pytest.raises(OutOfRangeFrame): frame[ChainName("B")] # does throw with pytest.raises(OutOfRangeChain): frame[ChainName("A")][ResidueId(3)] # does throw with pytest.raises(OutOfRangeResidue): frame[ChainName("A")][ResidueId(2)][AtomName("CX")] # does not throw
def test_Atom_setters(): from pyxmolpp2.polymer import AtomName from pyxmolpp2.geometry import XYZ frame = make_polyglycine([("A", 1)]) a = frame.asAtoms[0] a.name = AtomName("X") assert a.name == AtomName("X") a.aName = AtomName("Y") assert a.aName == AtomName("Y") a.id = 5 assert a.id == 5 a.aId = 7 assert a.aId == 7 a.r = XYZ(9,9,9) assert (a.r - XYZ(9,9,9)).len() == 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)
def test_Frame(): from pyxmolpp2.polymer import Frame from pyxmolpp2.polymer import ChainName from pyxmolpp2.polymer import AtomName from pyxmolpp2.polymer import ResidueName, ResidueId from pyxmolpp2.geometry import XYZ f = Frame(5) c = f.emplace(ChainName("A"), 1) r = c.emplace(ResidueName("LYS"), ResidueId(1)) a = r.emplace(AtomName("CA"), 1, XYZ(1,2,3)) assert a.residue == r assert a.chain == c assert a.frame == f assert r.asAtoms[0] == a assert c.asAtoms[0] == a assert f.asAtoms[0] == a assert r.asAtoms.asResidues[0] == r assert c.asAtoms.asResidues[0] == r assert f.asAtoms.asResidues[0] == r assert r.asAtoms.asResidues.asChains[0] == c assert c.asAtoms.asResidues.asChains[0] == c assert f.asAtoms.asResidues.asChains[0] == c assert r.chain == c assert r.frame == f assert c.frame == f assert c.asResidues[0] == r assert f.asResidues[0] == r assert f.asChains[0] == c
def test_AtomName_compare(): from pyxmolpp2.polymer import AtomName frame = make_polyglycine([("A", 1)]) a = frame.asAtoms[0] assert a.name == AtomName(a.name.str)
print(chain.asAtoms.asResidues) # selects non-empty residues print(frame.asResidues.asChains) # selects chains with at least 1 residue print(frame.asChains.asResidues.asAtoms.asResidues.asChains ) # select chains with at least 1 non-empty residue ############################################################################## # # Filter # ^^^^^^ # ``filter`` method return new selection with elements that match predicate: # from pyxmolpp2.polymer import AtomName, ResidueName chain.asAtoms.filter(lambda a: a.r.x < 0) # select atoms with negative x coord chain.asAtoms.filter(lambda a: a.name == AtomName("CA")) # select CA atoms chain.asResidues.filter( lambda r: r.name == ResidueName("LYS")) # select LYS residues ############################################################################## # # ``pyxmolpp2`` defines predicate-generators which return predicate when compared to something: # from pyxmolpp2.polymer import aName, rName, aId, rId, cName, cIndex frame.asAtoms.filter(aName == "CA") # select CA atoms frame.asResidues.filter(rName == "LYS") # select LYS residues frame.asChains.filter(cName == "A") # select chain(s) A ############################################################################## # |ChainPredicate| and |ResiduePredicate| can be naturally applied to |AtomSelection|,
############################################################################## # Construction # ^^^^^^^^^^^^ # Torsion angle constructor allow two forms: # 1. Read-only torsion angle # 2. Read-write torsion angle from pyxmolpp2.polymer import TorsionAngle, AtomName, Atom r1 = frame.asResidues[1] r2 = frame.asResidues[2] # Let's create a read-only phi of residue 2 phi_2_ro = TorsionAngle( r1[AtomName("C")], r2[AtomName("N")], r2[AtomName("CA")], r2[AtomName("C")], ) # Check against factory angle: assert phi_2_ro.value().degrees == TorsionAngleFactory.phi(r2).value().degrees ############################################################################## # Attempt to set TorsionAngle will lead to ``RuntimeError``: try: phi_2_ro.set(Degrees(-130)) except RuntimeError as e: print(e)