IMP  2.4.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.domino as domino
7 import IMP.core as core
8 import IMP.display as display
9 import IMP.atom as atom
10 import IMP.algebra as alg
11 import IMP.em2d as em2d
12 
13 import IMP.EMageFit.imp_general.io as io
14 import IMP.EMageFit.imp_general.representation as representation
15 
16 import sys
17 import logging
18 
19 
20 log = logging.getLogger("alignments")
21 
22 
23 def get_reference_frames_from_chain_alignment(reference_rbs, reference_index,
24  rbs_to_align, index_to_align):
25  """
26  Align the rigid bodies rbs_to_align to the the reference frames of
27  reference_rbs. The rb with index_to_align is aligned to the reference rb
28  with reference_index. The function returns the reference frames to
29  apply to the rbs_to_align.
30  """
31  ref_coords = \
32  [m.get_coordinates()
33  for m in reference_rbs[reference_index].get_members()]
34  coords = [m.get_coordinates()
35  for m in rbs_to_align[index_to_align].get_members()]
36  if(len(coords) != len(ref_coords)):
37  raise ValueError(
38  "Mismatch in the number of members. Reference %d Aligned %d " % (
39  len(ref_coords), len(coords)))
40  T = alg.get_transformation_aligning_first_to_second(coords, ref_coords)
41  new_refs = []
42  for rb in rbs_to_align:
43 # log.debug("aligning ... %s",rb)
44  t = rb.get_reference_frame().get_transformation_to()
45  new_t = alg.compose(T, t)
46  new_refs.append(alg.ReferenceFrame3D(new_t))
47  return new_refs
48 
49 
50 def align_centroids_using_pca(ref_frames, ref_frames_reference):
51  """
52  Align the centroids of 2 sets of rigid bodyes using PCA using their
53  reference frames. returns the best rmsd and the ref_frames to get it.
54  """
55  if(len(ref_frames) != len(ref_frames_reference)):
56  raise ValueError("The number of reference frames must be the same")
57  Ts1 = [r.get_transformation_to() for r in ref_frames]
58  vs1 = [T.get_translation() for T in Ts1]
59  Ts2 = [r.get_transformation_to() for r in ref_frames_reference]
60  vs2 = [T.get_translation() for T in Ts2]
61 
62  # align with PCA
63  pc1 = alg.get_principal_components(vs1)
64  pc2 = alg.get_principal_components(vs2)
65  pcTs = alg.get_alignments_from_first_to_second(pc1, pc2)
66  best_refs = []
67  best_rmsd = 1e5
68  for j, pcT in enumerate(pcTs):
69  new_Ts1 = [alg.compose(pcT, T) for T in Ts1]
70  new_vs1 = [T.get_translation() for T in new_Ts1]
71  r = atom.get_rmsd(new_vs1, vs2)
72  if(r < best_rmsd):
73  best_rmsd = r
74  best_refs = [alg.ReferenceFrame3D(T) for T in new_Ts1]
75  return best_rmsd, best_refs
76 
77 
78 def get_reference_frames_aligning_rbs(rbs, reference_rbs):
79  """ rbs = rigid bodies """
80  refs = [rb.get_reference_frame() for rb in rbs]
81  ref_refs = [rb.get_reference_frame() for rb in reference_rbs]
82  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 the reference frames of reference_rbs. ...
Definition: alignments.py:23
Restraints using electron microscopy 2D images (class averages).
Utility functions to handle representation.
Utility functions to handle IO.
Definition: io.py:1
def get_reference_frames_aligning_rbs
rbs = rigid bodies
Definition: alignments.py:78
Basic functionality that is expected to be used by a wide variety of IMP users.
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 bodyes using PCA using their reference frames.
Definition: alignments.py:50
Output IMP model data in various file formats.
Functionality for loading, creating, manipulating and scoring atomic structures.
Divide-and-conquer inferential optimization in discrete space.