IMP logo
IMP Reference Guide  2.13.0
The Integrative Modeling Platform
restraints/em2d.py
1 """@namespace IMP.pmi.restraints.em2d
2 Restraints for handling electron microscopy images.
3 """
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.core
8 import IMP.algebra
9 import IMP.atom
10 import IMP.pmi.tools
11 import ihm.location
12 import ihm.dataset
13 
14 class ElectronMicroscopy2D(object):
15  """Fit particles against a set of class averages by principal components.
16  Compares how well the principal components of the segmented class
17  average fit to the principal components of the particles.
18  """
19  def __init__(self, hier, images, pixel_size, image_resolution,
20  projection_number, resolution, micrographs_number=None,
21  n_components=1):
22  """Constructor.
23  @param hier The root hierarchy for applying the restraint
24  @param images 2D class average filenames in PGM text format
25  @param pixel_size Pixel size in angstroms
26  @param image_resolution Estimated resolution of the images
27  in angstroms
28  @param projection_number Number of projections of the model
29  to generate and fit to images. The lower the number, the
30  faster the evaluation, but the lower the accuracy
31  @param resolution Which level of
32  [model representation](@ref pmi_resolution) to use in the fit
33  @param micrographs_number Number of micrograph particles that
34  were used to generate the class averages, if known
35  @param n_components Number of the largest components to be
36  considered for the EM image
37  """
38 
39  import IMP.em2d
40 
41  self.datasets = []
42  for image in images:
43  l = ihm.location.InputFileLocation(image,
44  details="Electron microscopy class average")
45  d = ihm.dataset.EM2DClassDataset(l)
46  self.datasets.append(d)
47 
48  for p, state in IMP.pmi.tools._all_protocol_outputs(hier):
49  for i in range(len(self.datasets)):
50  p.add_em2d_restraint(state, self, i, resolution, pixel_size,
51  image_resolution, projection_number,
52  micrographs_number)
53 
54  # PMI2 selection
55  self.m = hier.get_model()
56  particles = IMP.atom.Selection(
57  hier, resolution=resolution).get_selected_particles()
58  self.weight = 1.0
59  self.rs = IMP.RestraintSet(self.m, 'em2d')
60  self.label = "None"
61 
62  # read PGM FORMAT images
63  # format conversion recommendation - first run "e2proc2d.py $FILE ${NEW_FILE}.pgm"
64  # then, run "convert ${NEW_FILE}.pgm -compress none ${NEW_FILE2}.pgm"
65  if (n_components >= 2) : # Number of the largest components to be considered for the EM image
67  particles, images, pixel_size, image_resolution, projection_number, True, n_components)
68  else :
70  particles, images, pixel_size, image_resolution, projection_number, True)
71  self._em2d_restraint = em2d
72  self._num_images = len(images)
73  self.rs.add_restraint(em2d)
74 
75  def set_label(self, label):
76  self.label = label
77 
78  def add_to_model(self):
79  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs, add_to_rmf=True)
80 
81  def get_restraint(self):
82  return self.rs
83 
84  def set_weight(self,weight):
85  self.weight = weight
86  self.rs.set_weight(self.weight)
87 
88  def get_output(self):
89  output = {}
90  score = self.weight*self.rs.unprotected_evaluate(None)
91  output["_TotalScore"] = str(score)
92  output["ElectronMicroscopy2D_" + self.label] = str(score)
93  # For each image, get the transformation that places the
94  # model on the image, and its cross correlation coefficient
95  for i in range(self._num_images):
96  prefix = 'ElectronMicroscopy2D_%s_Image%d' % (self.label, i+1)
97  ccc = self._em2d_restraint.get_cross_correlation_coefficient(i)
98  output[prefix + '_CCC'] = str(ccc)
99  tran = self._em2d_restraint.get_transformation(i)
100  r = tran.get_rotation().get_quaternion()
101  t = tran.get_translation()
102  for j in range(4):
103  output[prefix + '_Rotation%d' % j] = str(r[j])
104  for j in range(3):
105  output[prefix + '_Translation%d' % j] = str(t[j])
106  return output
107 
109  """FFT based image alignment, developed by Javier Velazquez-Muriel"""
110  def __init__(self, hier, images=None, pixel_size=None,
111  image_resolution=None, projection_number=None,
112  resolution=None):
113  """Constructor.
114  @param hier The root hierarchy for applying the restraint
115  @param images SPIDER FORMAT images (format conversion should be done through EM2EM)
116  @param pixel_size sampling rate of the available EM images (angstroms)
117  @param image_resolution resolution at which you want to generate the projections of the model
118  In principle you want "perfect" projections, so use the highest resolution
119  @param projection_number Number of projections of the model (coarse registration) to
120  estimate the registration parameters
121  @param resolution Which level of
122  [model representation](@ref pmi_resolution) to use in the fit
123  @param n_components Number of the largest components to be
124  considered for the EM image
125  """
126 
127  import IMP.em2d
128 
129  # check input
130  if images is None:
131  raise Exception("EM2D_FFT: must pass images")
132  if pixel_size is None:
133  raise Exception("EM2D_FFT: must pass pixel size")
134  if image_resolution is None:
135  raise Exception("EM2D_FFT: must pass image resolution")
136  if projection_number is None:
137  raise Exception("EM2D_FFT: must pass projection_number")
138 
139  # PMI2 selection
140  self.m = hier.get_model()
141  particles = IMP.atom.Selection(
142  hier, resolution=resolution).get_selected_particles()
143 
144  self.weight=1.0
145  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
146  self.label = "None"
147 
148  # read
150  imgs = IMP.em2d.read_images(images, srw)
151  rows = imgs[0].get_header().get_number_of_rows()
152  cols = imgs[0].get_header().get_number_of_columns()
153 
154  params = IMP.em2d.Em2DRestraintParameters(pixel_size, image_resolution, projection_number)
155 
156  # This method (recommended) uses preprocessing of the images and projections to speed-up the registration
157  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
158  params.optimization_steps = 50
159  params.simplex_initial_length = 0.1
160  params.simplex_minimum_size = 0.02
161 
162  # use true if you want to save the projections from the model that best match the EM images
163  params.save_match_images = False
164 
165  ######################
166  # set up the em2D restraint
167  ######################
168  score_function = IMP.em2d.EM2DScore()
169  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
170  em2d_restraint.setup(score_function, params)
171  em2d_restraint.set_images(imgs)
172  em2d_restraint.set_fast_mode(5)
173  em2d_restraint.set_name("em2d_restraint")
174 
175  print ("len(particles) = ", len(particles))
176  container = IMP.container.ListSingletonContainer(self.m, particles)
177  em2d_restraint.set_particles(container)
178 
179  self.rs.add_restraint(em2d_restraint)
180 
181  def set_label(self, label):
182  self.label = label
183 
184  def add_to_model(self):
185  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
186 
187  def get_restraint(self):
188  return self.rs
189 
190  def set_weight(self,weight):
191  self.weight=weight
192  self.rs.set_weight(self.weight)
193 
194  def get_output(self):
195  output = {}
196  score = self.weight*self.rs.unprotected_evaluate(None)
197  output["_TotalScore"] = str(score)
198  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
199  return output
Restraints using electron microscopy 2D images (class averages).
Fit particles against a set of class averages by principal components.
Miscellaneous utilities.
Definition: tools.py:1
Object used to hold a set of restraints.
Definition: RestraintSet.h:36
Fast scoring of Particles against electron microscopy class averages.
Store a list of ParticleIndexes.
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: tools.py:84
Images read_images(const Strings &names, const ImageReaderWriter *rw)
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...
Parameters used by Em2DRestraint and ProjectionFinder.
The general base class for IMP exceptions.
Definition: exception.h:49
Functionality for loading, creating, manipulating and scoring atomic structures.
Select hierarchy particles identified by the biological name.
Definition: Selection.h:66
FFT based image alignment, developed by Javier Velazquez-Muriel.