IMP logo
IMP Reference Guide  2.12.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,
20  representation=None,
21  images=None,
22  pixel_size=None,
23  image_resolution=None,
24  projection_number=None,
25  micrographs_number=None,
26  resolution=None,
27  n_components=1,
28  hier=None):
29  """Constructor.
30  @param representation DEPRECATED, pass 'hier' instead
31  @param images 2D class average filenames in PGM text format
32  @param pixel_size Pixel size in angstroms
33  @param image_resolution Estimated resolution of the images
34  in angstroms
35  @param projection_number Number of projections of the model
36  to generate and fit to images. The lower the number, the
37  faster the evaluation, but the lower the accuracy
38  @param micrographs_number Number of micrograph particles that
39  were used to generate the class averages, if known
40  @param resolution Which level of
41  [model representation](@ref pmi_resolution) to use in the fit
42  @param n_components Number of the largest components to be
43  considered for the EM image
44  @param hier The root hierarchy for applying the restraint
45  """
46 
47  import IMP.em2d
48 
49  # check input
50  if images is None:
51  raise Exception("Must pass images")
52  if pixel_size is None:
53  raise Exception("Must pass pixel size")
54  if image_resolution is None:
55  raise Exception("must pass image resolution")
56 
57  self.datasets = []
58  for image in images:
59  l = ihm.location.InputFileLocation(image,
60  details="Electron microscopy class average")
61  d = ihm.dataset.EM2DClassDataset(l)
62  self.datasets.append(d)
63 
64  if representation:
65  for p, state in representation._protocol_output:
66  for i in range(len(self.datasets)):
67  p.add_em2d_restraint(state, self, i, resolution, pixel_size,
68  image_resolution, projection_number,
69  micrographs_number)
70 
71  # PMI1/2 selection
72  if representation is None and hier is not None:
73  self.m = hier.get_model()
74  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
75  elif hier is None and representation is not None:
76  self.m = representation.prot.get_model()
77  particles = IMP.pmi.tools.select(
78  representation,
79  resolution=resolution)
80  else:
81  raise Exception("EM2D: must pass hier or representation")
82  self.weight = 1.0
83  self.rs = IMP.RestraintSet(self.m, 'em2d')
84  self.label = "None"
85 
86  # read PGM FORMAT images
87  # format conversion recommendataion - first run "e2proc2d.py $FILE ${NEW_FILE}.pgm"
88  # then, run "convert ${NEW_FILE}.pgm -compress none ${NEW_FILE2}.pgm"
89  if (n_components >= 2) : # Number of the largest components to be considered for the EM image
91  particles, images, pixel_size, image_resolution, projection_number, True, n_components)
92  else :
94  particles, images, pixel_size, image_resolution, projection_number, True)
95  self._em2d_restraint = em2d
96  self._num_images = len(images)
97  self.rs.add_restraint(em2d)
98 
99  def set_label(self, label):
100  self.label = label
101 
102  def add_to_model(self):
103  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
104 
105  def get_restraint(self):
106  return self.rs
107 
108  def set_weight(self,weight):
109  self.weight = weight
110  self.rs.set_weight(self.weight)
111 
112  def get_output(self):
113  output = {}
114  score = self.weight*self.rs.unprotected_evaluate(None)
115  output["_TotalScore"] = str(score)
116  output["ElectronMicroscopy2D_" + self.label] = str(score)
117  # For each image, get the transformation that places the
118  # model on the image, and its cross correlation coefficient
119  for i in range(self._num_images):
120  prefix = 'ElectronMicroscopy2D_%s_Image%d' % (self.label, i+1)
121  ccc = self._em2d_restraint.get_cross_correlation_coefficient(i)
122  output[prefix + '_CCC'] = str(ccc)
123  tran = self._em2d_restraint.get_transformation(i)
124  r = tran.get_rotation().get_quaternion()
125  t = tran.get_translation()
126  for j in range(4):
127  output[prefix + '_Rotation%d' % j] = str(r[j])
128  for j in range(3):
129  output[prefix + '_Translation%d' % j] = str(t[j])
130  return output
131 
133  """FFT based image alignment, developed by Javier Velazquez-Muriel"""
134  def __init__(
135  self,
136  representation=None,
137  images=None,
138  pixel_size=None,
139  image_resolution=None,
140  projection_number=None,
141  resolution=None,
142  hier=None):
143  """Constructor.
144  @param representation DEPRECATED, pass 'hier' instead
145  @param images SPIDER FORMAT images (format conversion should be done through EM2EM)
146  @param pixel_size sampling rate of the available EM images (angstroms)
147  @param image_resolution resolution at which you want to generate the projections of the model
148  In principle you want "perfect" projections, so use the highest resolution
149  @param projection_number Number of projections of the model (coarse registration) to
150  estimate the registration parameters
151  @param resolution Which level of
152  [model representation](@ref pmi_resolution) to use in the fit
153  @param n_components Number of the largest components to be
154  considered for the EM image
155  @param hier The root hierarchy for applying the restraint
156  """
157 
158  import IMP.em2d
159 
160  # check input
161  if images is None:
162  raise Exception("EM2D_FFT: must pass images")
163  if pixel_size is None:
164  raise Exception("EM2D_FFT: must pass pixel size")
165  if image_resolution is None:
166  raise Exception("EM2D_FFT: must pass image resolution")
167  if projection_number is None:
168  raise Exception("EM2D_FFT: must pass projection_number")
169 
170  # PMI1/2 selection
171  if representation is None and hier is not None:
172  self.m = hier.get_model()
173  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
174  elif hier is None and representation is not None:
175  self.m = representation.prot.get_model()
176  particles = IMP.pmi.tools.select(
177  representation,
178  resolution=resolution)
179  else:
180  raise Exception("EM2D: must pass hier or representation")
181 
182  self.weight=1.0
183  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
184  self.label = "None"
185 
186  # read
188  imgs = IMP.em2d.read_images(images, srw)
189  rows = imgs[0].get_header().get_number_of_rows()
190  cols = imgs[0].get_header().get_number_of_columns()
191 
192  params = IMP.em2d.Em2DRestraintParameters(pixel_size, image_resolution, projection_number)
193 
194  # This method (recommended) uses preprocessing of the images and projections to speed-up the registration
195  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
196  params.optimization_steps = 50
197  params.simplex_initial_length = 0.1
198  params.simplex_minimum_size = 0.02
199 
200  # use true if you want to save the projections from the model that best match the EM images
201  params.save_match_images = False
202 
203  ######################
204  # set up the em2D restraint
205  ######################
206  score_function = IMP.em2d.EM2DScore()
207  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
208  em2d_restraint.setup(score_function, params)
209  em2d_restraint.set_images(imgs)
210  em2d_restraint.set_fast_mode(5)
211  em2d_restraint.set_name("em2d_restraint")
212 
213  print ("len(particles) = ", len(particles))
214  container = IMP.container.ListSingletonContainer(self.m, particles)
215  em2d_restraint.set_particles(container)
216 
217  self.rs.add_restraint(em2d_restraint)
218 
219  def set_label(self, label):
220  self.label = label
221 
222  def add_to_model(self):
223  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
224 
225  def get_restraint(self):
226  return self.rs
227 
228  def set_weight(self,weight):
229  self.weight=weight
230  self.rs.set_weight(self.weight)
231 
232  def get_output(self):
233  output = {}
234  score = self.weight*self.rs.unprotected_evaluate(None)
235  output["_TotalScore"] = str(score)
236  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
237  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:89
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.