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