Skip to content

Commit

Permalink
Merge pull request #93 from sizmailov/refactor/rework_tests
Browse files Browse the repository at this point in the history
Fix alignment shortcuts
  • Loading branch information
sizmailov authored Feb 19, 2019
2 parents 0e33fc3 + d753a1f commit 4f1a542
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
25 changes: 0 additions & 25 deletions pytests/xmol/polymer/test_Atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,28 +500,3 @@ def test_bad_selection_construction_from_list():
AtomSelection([ a for a in frame.asChains ])
with pytest.raises(Exception):
ChainSelection([ a for a in frame.asResidues ])



def test_shorthands():
# from pyxmolpp2.polymer import AtomSelection, ChainSelection

frame = make_polyglycine([("A", 20)])

asel = frame.asAtoms

asel.geom_center()
asel.mass_center([1.0]*asel.size)
asel.inertia_tensor([1.0]*asel.size)
asel.geom_inertia_tensor()
asel.rmsd(asel)
asel.rmsd(asel.toCoords)
asel.alignment(asel)
asel.alignment(asel.toCoords)
asel.align_to(asel)
asel.align_to(asel.toCoords)





68 changes: 68 additions & 0 deletions pytests/xmol/polymer/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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_shorthands():
import numpy as np

from pyxmolpp2.geometry import Rotation3d, Translation3d, XYZ, Degrees, calc_alignment

frame = make_polyglycine([("A", 20)])
for a in frame.asAtoms:
a.r = XYZ(*np.random.random(3))
frame2 = frame.copy()

asel = frame.asAtoms
asel2 = frame2.asAtoms

asel.geom_center()
asel.mass_center([1.0] * asel.size)
asel.inertia_tensor([1.0] * asel.size)
asel.geom_inertia_tensor()

T = Translation3d(XYZ(1, 0, 0))
asel2.transform(T)

assert np.isclose(asel.rmsd(asel2), 1.0)
assert np.isclose(asel.rmsd(asel2.toCoords), 1.0)
assert np.isclose(asel.rmsd(asel2.toCoords.transform(T.inverted())), 0.0)

T = Translation3d(XYZ(1, 0, 0)) * Rotation3d(XYZ(1, 1, 1), Degrees(45))
asel.align_to(asel2)
assert np.isclose(asel.rmsd(asel2), 0)

asel2.transform(T)
asel.align_to(asel2.toCoords)
assert np.isclose(asel.rmsd(asel2), 0)

T = Translation3d(XYZ(1, 0, 0)) * Rotation3d(XYZ(1, 1, 1), Degrees(45))
asel2.transform(T)

assert np.allclose(T.matrix3d(), calc_alignment(asel2.toCoords, asel.toCoords).matrix3d())
assert np.allclose(T.matrix3d(), asel.alignment_to(asel2).matrix3d())
assert np.allclose(T.matrix3d(), asel.alignment_to(asel2.toCoords).matrix3d())
16 changes: 9 additions & 7 deletions pyxmolpp/polymer/Atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ Order is preserved across manipulations with :py:class:`ChainSelection`
.def(
"rmsd",
[](AtomSelection& sel, AtomSelection& ref) {
return xmol::geometry::calc_rmsd(sel.toCoords(),ref.toCoords());
return xmol::geometry::calc_rmsd(sel.toCoords(), ref.toCoords());
},
py::arg("ref"),
"Returns rmsd between two selections")
Expand All @@ -631,26 +631,27 @@ Order is preserved across manipulations with :py:class:`ChainSelection`
"Returns rmsd between selection and reference coordinates")

.def(
"alignment",
"alignment_to",
[](AtomSelection& sel, std::vector<XYZ> ref) {
return xmol::geometry::calc_alignment(sel.toCoords(), ref);
return xmol::geometry::calc_alignment(ref, sel.toCoords());
},
py::arg("ref"),
"Equivalent to :code:`calc_alignment(ref.toCoords(), self.toCoords())`")


.def(
"alignment",
"alignment_to",
[](AtomSelection& sel, AtomSelection& ref) {
return xmol::geometry::calc_alignment(sel.toCoords(), ref.toCoords());
return xmol::geometry::calc_alignment(ref.toCoords(), sel.toCoords());
},
py::arg("ref"),
"Equivalent to :code:`calc_alignment(ref.toCoords(), self.toCoords())`")

.def(
"align_to",
[](AtomSelection& sel, std::vector<XYZ> ref) -> AtomSelection& {
xmol::geometry::calc_alignment(sel.toCoords(), ref);
auto al = xmol::geometry::calc_alignment(ref, sel.toCoords());
sel.for_each([&](Atom& a) { a.set_r(al.transform(a.r())); });
return sel;
},
py::arg("ref"),
Expand All @@ -660,7 +661,8 @@ Order is preserved across manipulations with :py:class:`ChainSelection`
.def(
"align_to",
[](AtomSelection& sel, AtomSelection& ref) -> AtomSelection& {
xmol::geometry::calc_alignment(sel.toCoords(), ref.toCoords());
auto al = xmol::geometry::calc_alignment(ref.toCoords(), sel.toCoords());
sel.for_each([&](Atom& a) { a.set_r(al.transform(a.r())); });
return sel;
},
py::arg("ref"),
Expand Down

0 comments on commit 4f1a542

Please sign in to comment.