IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
/restraints/em2d.py
1 """@namespace IMP.pmi1.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.pmi1.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 pmi1_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.pmi1.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):
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  output = {}
119  score = self.weight*self.rs.unprotected_evaluate(None)
120  output["_TotalScore"] = str(score)
121  output["ElectronMicroscopy2D_" + self.label] = str(score)
122  # For each image, get the transformation that places the
123  # model on the image, and its cross correlation coefficient
124  for i in range(self._num_images):
125  prefix = 'ElectronMicroscopy2D_%s_Image%d' % (self.label, i+1)
126  ccc = self._em2d_restraint.get_cross_correlation_coefficient(i)
127  output[prefix + '_CCC'] = str(ccc)
128  tran = self._em2d_restraint.get_transformation(i)
129  r = tran.get_rotation().get_quaternion()
130  t = tran.get_translation()
131  for j in range(4):
132  output[prefix + '_Rotation%d' % j] = str(r[j])
133  for j in range(3):
134  output[prefix + '_Translation%d' % j] = str(t[j])
135  return output
136 
138  """FFT based image alignment, developed by Javier Velazquez-Muriel"""
139  def __init__(
140  self,
141  representation=None,
142  images=None,
143  pixel_size=None,
144  image_resolution=None,
145  projection_number=None,
146  resolution=None,
147  hier=None):
148  """Constructor.
149  @param representation DEPRECATED, pass 'hier' instead
150  @param images SPIDER FORMAT images (format conversion should be done through EM2EM)
151  @param pixel_size sampling rate of the available EM images (angstroms)
152  @param image_resolution resolution at which you want to generate the projections of the model
153  In principle you want "perfect" projections, so use the highest resolution
154  @param projection_number Number of projections of the model (coarse registration) to
155  estimate the registration parameters
156  @param resolution Which level of
157  [model representation](@ref pmi1_resolution) to use in the fit
158  @param n_components Number of the largest components to be
159  considered for the EM image
160  @param hier The root hierarchy for applying the restraint
161  """
162 
163  import IMP.em2d
164 
165  # check input
166  if images is None:
167  raise Exception("EM2D_FFT: must pass images")
168  if pixel_size is None:
169  raise Exception("EM2D_FFT: must pass pixel size")
170  if image_resolution is None:
171  raise Exception("EM2D_FFT: must pass image resolution")
172  if projection_number is None:
173  raise Exception("EM2D_FFT: must pass projection_number")
174 
175  # PMI1/2 selection
176  if representation is None and hier is not None:
177  self.m = hier.get_model()
178  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
179  elif hier is None and representation is not None:
180  self.m = representation.prot.get_model()
181  particles = IMP.pmi1.tools.select(
182  representation,
183  resolution=resolution)
184  else:
185  raise Exception("EM2D: must pass hier or representation")
186 
187  self.weight=1.0
188  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
189  self.label = "None"
190 
191  # read
193  imgs = IMP.em2d.read_images(images, srw)
194  rows = imgs[0].get_header().get_number_of_rows()
195  cols = imgs[0].get_header().get_number_of_columns()
196 
197  params = IMP.em2d.Em2DRestraintParameters(pixel_size, image_resolution, projection_number)
198 
199  # This method (recommended) uses preprocessing of the images and projections to speed-up the registration
200  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
201  params.optimization_steps = 50
202  params.simplex_initial_length = 0.1
203  params.simplex_minimum_size = 0.02
204 
205  # use true if you want to save the projections from the model that best match the EM images
206  params.save_match_images = False
207 
208  ######################
209  # set up the em2D restraint
210  ######################
211  score_function = IMP.em2d.EM2DScore()
212  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
213  em2d_restraint.setup(score_function, params)
214  em2d_restraint.set_images(imgs)
215  em2d_restraint.set_fast_mode(5)
216  em2d_restraint.set_name("em2d_restraint")
217 
218  print ("len(particles) = ", len(particles))
219  container = IMP.container.ListSingletonContainer(self.m, particles)
220  em2d_restraint.set_particles(container)
221 
222  self.rs.add_restraint(em2d_restraint)
223 
224  def set_label(self, label):
225  self.label = label
226 
227  def add_to_model(self):
229 
230  def get_restraint(self):
231  return self.rs
232 
233  def set_weight(self,weight):
234  self.weight=weight
235  self.rs.set_weight(self.weight)
236 
237  def get_output(self):
238  output = {}
239  score = self.weight*self.rs.unprotected_evaluate(None)
240  output["_TotalScore"] = str(score)
241  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
242  return output
Fit particles against a set of class averages by principal components.
Restraints using electron microscopy 2D images (class averages).
FFT based image alignment, developed by Javier Velazquez-Muriel.
Object used to hold a set of restraints.
Definition: RestraintSet.h:37
Fast scoring of Particles against electron microscopy class averages.
Miscellaneous utilities.
Definition: /tools.py:1
Store a list of ParticleIndexes.
Images read_images(const Strings &names, const ImageReaderWriter *rw)
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: /tools.py:727
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:48
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: /tools.py:53
Functionality for loading, creating, manipulating and scoring atomic structures.
Select hierarchy particles identified by the biological name.
Definition: Selection.h:70