IMP logo
IMP Reference Guide  2.8.0
The Integrative Modeling Platform
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 IMP.pmi.metadata
12 
13 class ElectronMicroscopy2D(object):
14  """Fit particles against a set of class averages by principal components.
15  Compares how well the principal components of the segmented class
16  average fit to the principal components of the particles.
17  """
18  def __init__(self,
19  representation=None,
20  images=None,
21  pixel_size=None,
22  image_resolution=None,
23  projection_number=None,
24  resolution=None,
25  n_components=1,
26  hier=None):
27  """Constructor.
28  @param representation DEPRECATED, pass 'hier' instead
29  @param images 2D class average filenames in PGM text format
30  @param pixel_size Pixel size in angstroms
31  @param image_resolution Estimated resolution of the images
32  in angstroms
33  @param projection_number Number of projections of the model
34  to generate and fit to images. The lower the number, the
35  faster the evaluation, but the lower the accuracy
36  @param resolution Which level of
37  [model representation](@ref pmi_resolution) to use in the fit
38  @param n_components Number of the largest components to be
39  considered for the EM image
40  @param hier The root hierarchy for applying the restraint
41  """
42 
43  import IMP.em2d
44 
45  # check input
46  if images is None:
47  raise Exception("Must pass images")
48  if pixel_size is None:
49  raise Exception("Must pass pixel size")
50  if image_resolution is None:
51  raise Exception("must pass image resolution")
52 
53  self.datasets = []
54  for image in images:
55  if representation:
56  d = representation.get_file_dataset(image)
57  if d:
58  self.datasets.append(d)
59  continue
61  details="Electron microscopy class average")
63  self.datasets.append(d)
64 
65  if representation:
66  for p, state in representation._protocol_output:
67  for i in range(len(self.datasets)):
68  p.add_em2d_restraint(state, self, i, resolution, pixel_size,
69  image_resolution, projection_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  self.m.update()
114  output = {}
115  score = self.weight*self.rs.unprotected_evaluate(None)
116  output["_TotalScore"] = str(score)
117  output["ElectronMicroscopy2D_" + self.label] = str(score)
118  # For each image, get the transformation that places the
119  # model on the image, and its cross correlation coefficient
120  for i in range(self._num_images):
121  prefix = 'ElectronMicroscopy2D_%s_Image%d' % (self.label, i+1)
122  ccc = self._em2d_restraint.get_cross_correlation_coefficient(i)
123  output[prefix + '_CCC'] = str(ccc)
124  tran = self._em2d_restraint.get_transformation(i)
125  r = tran.get_rotation().get_quaternion()
126  t = tran.get_translation()
127  for j in range(4):
128  output[prefix + '_Rotation%d' % j] = str(r[j])
129  for j in range(3):
130  output[prefix + '_Translation%d' % j] = str(t[j])
131  return output
132 
134  """FFT based image alignment, developed by Javier Velazquez-Muriel"""
135  def __init__(
136  self,
137  representation=None,
138  images=None,
139  pixel_size=None,
140  image_resolution=None,
141  projection_number=None,
142  resolution=None,
143  hier=None):
144  """Constructor.
145  @param representation DEPRECATED, pass 'hier' instead
146  @param images SPIDER FORMAT images (format conversion should be done through EM2EM)
147  @param pixel_size sampling rate of the available EM images (angstroms)
148  @param image_resolution resolution at which you want to generate the projections of the model
149  In principle you want "perfect" projections, so use the highest resolution
150  @param projection_number Number of projections of the model (coarse registration) to
151  estimate the registration parameters
152  @param resolution Which level of
153  [model representation](@ref pmi_resolution) to use in the fit
154  @param n_components Number of the largest components to be
155  considered for the EM image
156  @param hier The root hierarchy for applying the restraint
157  """
158 
159  import IMP.em2d
160 
161  # check input
162  if images is None:
163  raise Exception("EM2D_FFT: must pass images")
164  if pixel_size is None:
165  raise Exception("EM2D_FFT: must pass pixel size")
166  if image_resolution is None:
167  raise Exception("EM2D_FFT: must pass image resolution")
168  if projection_number is None:
169  raise Exception("EM2D_FFT: must pass projection_number")
170 
171  # PMI1/2 selection
172  if representation is None and hier is not None:
173  self.m = hier.get_model()
174  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
175  elif hier is None and representation is not None:
176  self.m = representation.prot.get_model()
177  particles = IMP.pmi.tools.select(
178  representation,
179  resolution=resolution)
180  else:
181  raise Exception("EM2D: must pass hier or representation")
182 
183  self.weight=1.0
184  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
185  self.label = "None"
186 
187  # read
189  imgs = IMP.em2d.read_images(images, srw)
190  rows = imgs[0].get_header().get_number_of_rows()
191  cols = imgs[0].get_header().get_number_of_columns()
192 
193  params = IMP.em2d.Em2DRestraintParameters(pixel_size, image_resolution, projection_number)
194 
195  # This method (recommended) uses preprocessing of the images and projections to speed-up the registration
196  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
197  params.optimization_steps = 50
198  params.simplex_initial_length = 0.1
199  params.simplex_minimum_size = 0.02
200 
201  # use true if you want to save the projections from the model that best match the EM images
202  params.save_match_images = False
203 
204  ######################
205  # set up the em2D restraint
206  ######################
207  score_function = IMP.em2d.EM2DScore()
208  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
209  em2d_restraint.setup(score_function, params)
210  em2d_restraint.set_images(imgs)
211  em2d_restraint.set_fast_mode(5)
212  em2d_restraint.set_name("em2d_restraint")
213 
214  print ("len(particles) = ", len(particles))
215  container = IMP.container.ListSingletonContainer(self.m, particles)
216  em2d_restraint.set_particles(container)
217 
218  self.rs.add_restraint(em2d_restraint)
219 
220  def set_label(self, label):
221  self.label = label
222 
223  def add_to_model(self):
224  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
225 
226  def get_restraint(self):
227  return self.rs
228 
229  def set_weight(self,weight):
230  self.weight=weight
231  self.rs.set_weight(self.weight)
232 
233  def get_output(self):
234  self.m.update()
235  output = {}
236  score = self.weight*self.rs.unprotected_evaluate(None)
237  output["_TotalScore"] = str(score)
238  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
239  return output
Restraints using electron microscopy 2D images (class averages).
Classes for attaching metadata to PMI objects.
Definition: metadata.py:1
Fit particles against a set of class averages by principal components.
Definition: em2d.py:13
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:38
Images read_images(const Strings &names, const ImageReaderWriter *rw)
An individual file or directory.
Definition: metadata.py:167
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
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: tools.py:700
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.
Definition: em2d.py:133