IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
alignments.py
1 """@namespace IMP.EMageFit.imp_general.alignments
2  Utility functions to handle alignments.
3 """
4 
5 import IMP
6 import IMP.atom
7 import IMP.algebra
8 import logging
9 
10 log = logging.getLogger("alignments")
11 
12 
13 def get_reference_frames_from_chain_alignment(reference_rbs, reference_index,
14  rbs_to_align, index_to_align):
15  """
16  Align the rigid bodies rbs_to_align to the reference frames of
17  reference_rbs. The rb with index_to_align is aligned to the reference
18  rb with reference_index. The function returns the reference frames to
19  apply to the rbs_to_align.
20  """
21  ref_coords = \
22  [m.get_coordinates()
23  for m in reference_rbs[reference_index].get_members()]
24  coords = [m.get_coordinates()
25  for m in rbs_to_align[index_to_align].get_members()]
26  if len(coords) != len(ref_coords):
27  raise ValueError(
28  "Mismatch in the number of members. Reference %d Aligned %d " % (
29  len(ref_coords), len(coords)))
31  ref_coords)
32  new_refs = []
33  for rb in rbs_to_align:
34  t = rb.get_reference_frame().get_transformation_to()
35  new_t = IMP.algebra.compose(T, t)
36  new_refs.append(IMP.algebra.ReferenceFrame3D(new_t))
37  return new_refs
38 
39 
40 def align_centroids_using_pca(ref_frames, ref_frames_reference):
41  """
42  Align the centroids of 2 sets of rigid bodies using PCA using their
43  reference frames. returns the best rmsd and the ref_frames to get it.
44  """
45  if len(ref_frames) != len(ref_frames_reference):
46  raise ValueError("The number of reference frames must be the same")
47  Ts1 = [r.get_transformation_to() for r in ref_frames]
48  vs1 = [T.get_translation() for T in Ts1]
49  Ts2 = [r.get_transformation_to() for r in ref_frames_reference]
50  vs2 = [T.get_translation() for T in Ts2]
51 
52  # align with PCA
56  best_refs = []
57  best_rmsd = 1e5
58  for j, pcT in enumerate(pcTs):
59  new_Ts1 = [IMP.algebra.compose(pcT, T) for T in Ts1]
60  new_vs1 = [T.get_translation() for T in new_Ts1]
61  r = IMP.atom.get_rmsd(new_vs1, vs2)
62  if r < best_rmsd:
63  best_rmsd = r
64  best_refs = [IMP.algebra.ReferenceFrame3D(T) for T in new_Ts1]
65  return best_rmsd, best_refs
66 
67 
68 def get_reference_frames_aligning_rbs(rbs, reference_rbs):
69  """ rbs = rigid bodies """
70  refs = [rb.get_reference_frame() for rb in rbs]
71  ref_refs = [rb.get_reference_frame() for rb in reference_rbs]
72  best_rmsd, best_refs = align_centroids_using_pca(refs, ref_refs)
def get_reference_frames_from_chain_alignment
Align the rigid bodies rbs_to_align to the reference frames of reference_rbs.
Definition: alignments.py:13
A reference frame in 3D.
double get_rmsd(const Selection &s0, const Selection &s1)
def get_reference_frames_aligning_rbs
rbs = rigid bodies
Definition: alignments.py:68
Transformation3Ds get_alignments_from_first_to_second(const PrincipalComponentAnalysisD< 3 > &pca1, const PrincipalComponentAnalysisD< 3 > &pca2)
Get all alignments of the first principal component system to the second one.
PrincipalComponentAnalysisD< D > get_principal_components(const Vector< VectorD< D > > &ps)
Perform principal components analysis on a set of vectors.
Transformation3D compose(const Transformation3D &a, const Transformation3D &b)
Compose two transformations.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
def align_centroids_using_pca
Align the centroids of 2 sets of rigid bodies using PCA using their reference frames.
Definition: alignments.py:40
Transformation3D get_transformation_aligning_first_to_second(Vector3Ds a, Vector3Ds b)
Functionality for loading, creating, manipulating and scoring atomic structures.