IMP logo
IMP Reference Guide  2.20.1
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  """
135 
136  import IMP.em2d
137 
138  # check input
139  if images is None:
140  raise Exception("EM2D_FFT: must pass images")
141  if pixel_size is None:
142  raise Exception("EM2D_FFT: must pass pixel size")
143  if image_resolution is None:
144  raise Exception("EM2D_FFT: must pass image resolution")
145  if projection_number is None:
146  raise Exception("EM2D_FFT: must pass projection_number")
147 
148  # PMI2 selection
149  self.m = hier.get_model()
150  particles = IMP.atom.Selection(
151  hier, resolution=resolution).get_selected_particles()
152 
153  self.weight = 1.0
154  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
155  self.label = "None"
156 
157  # read
159  imgs = IMP.em2d.read_images(images, srw)
160 
162  pixel_size, image_resolution, projection_number)
163 
164  # This method (recommended) uses preprocessing of the images and
165  # projections to speed-up the registration
166  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
167  params.optimization_steps = 50
168  params.simplex_initial_length = 0.1
169  params.simplex_minimum_size = 0.02
170 
171  # use true if you want to save the projections from the model that
172  # best match the EM images
173  params.save_match_images = False
174 
175  ######################
176  # set up the em2D restraint
177  ######################
178  score_function = IMP.em2d.EM2DScore()
179  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
180  em2d_restraint.setup(score_function, params)
181  em2d_restraint.set_images(imgs)
182  em2d_restraint.set_fast_mode(5)
183  em2d_restraint.set_name("em2d_restraint")
184 
185  print("len(particles) = ", len(particles))
186  container = IMP.container.ListSingletonContainer(self.m, particles)
187  em2d_restraint.set_particles(container)
188 
189  self.rs.add_restraint(em2d_restraint)
190 
191  def set_label(self, label):
192  self.label = label
193 
194  def add_to_model(self):
195  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
196 
197  def get_restraint(self):
198  return self.rs
199 
200  def set_weight(self, weight):
201  self.weight = weight
202  self.rs.set_weight(self.weight)
203 
204  def get_output(self):
205  output = {}
206  score = self.weight*self.rs.unprotected_evaluate(None)
207  output["_TotalScore"] = str(score)
208  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
209  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.