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