IMP logo
IMP Reference Guide  2.7.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
62  self.datasets.append(d)
63 
64  if representation:
65  for p in representation._protocol_output:
66  for i in range(len(self.datasets)):
67  p.add_em2d_restraint(self, i, resolution, pixel_size,
68  image_resolution, projection_number)
69 
70  # PMI1/2 selection
71  if representation is None and hier is not None:
72  self.m = hier.get_model()
73  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
74  elif hier is None and representation is not None:
75  self.m = representation.prot.get_model()
76  particles = IMP.pmi.tools.select(
77  representation,
78  resolution=resolution)
79  else:
80  raise Exception("EM2D: must pass hier or representation")
81  self.weight = 1.0
82  self.rs = IMP.RestraintSet(self.m, 'em2d')
83  self.label = "None"
84 
85  # read PGM FORMAT images
86  # format conversion recommendataion - first run "e2proc2d.py $FILE ${NEW_FILE}.pgm"
87  # then, run "convert ${NEW_FILE}.pgm -compress none ${NEW_FILE2}.pgm"
88  if (n_components >= 2) : # Number of the largest components to be considered for the EM image
90  particles, images, pixel_size, image_resolution, projection_number, True, n_components)
91  else :
93  particles, images, pixel_size, image_resolution, projection_number, True)
94  self.rs.add_restraint(em2d)
95 
96  def set_label(self, label):
97  self.label = label
98 
99  def add_to_model(self):
100  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
101 
102  def get_restraint(self):
103  return self.rs
104 
105  def set_weight(self,weight):
106  self.weight = weight
107  self.rs.set_weight(self.weight)
108 
109  def get_output(self):
110  self.m.update()
111  output = {}
112  score = self.weight*self.rs.unprotected_evaluate(None)
113  output["_TotalScore"] = str(score)
114  output["ElectronMicroscopy2D_" + self.label] = str(score)
115  return output
116 
118  """FFT based image alignment, developed by Javier Velazquez-Muriel"""
119  def __init__(
120  self,
121  representation=None,
122  images=None,
123  pixel_size=None,
124  image_resolution=None,
125  projection_number=None,
126  resolution=None,
127  hier=None):
128  """Constructor.
129  @param representation DEPRECATED, pass 'hier' instead
130  @param images SPIDER FORMAT images (format conversion should be done through EM2EM)
131  @param pixel_size sampling rate of the available EM images (angstroms)
132  @param image_resolution resolution at which you want to generate the projections of the model
133  In principle you want "perfect" projections, so use the highest resolution
134  @param projection_number Number of projections of the model (coarse registration) to
135  estimate the registration parameters
136  @param resolution Which level of
137  [model representation](@ref pmi_resolution) to use in the fit
138  @param n_components Number of the largest components to be
139  considered for the EM image
140  @param hier The root hierarchy for applying the restraint
141  """
142 
143  import IMP.em2d
144 
145  # check input
146  if images is None:
147  raise Exception("EM2D_FFT: must pass images")
148  if pixel_size is None:
149  raise Exception("EM2D_FFT: must pass pixel size")
150  if image_resolution is None:
151  raise Exception("EM2D_FFT: must pass image resolution")
152  if projection_number is None:
153  raise Exception("EM2D_FFT: must pass projection_number")
154 
155  # PMI1/2 selection
156  if representation is None and hier is not None:
157  self.m = hier.get_model()
158  particles = IMP.atom.Selection(hier,resolution=resolution).get_selected_particles()
159  elif hier is None and representation is not None:
160  self.m = representation.prot.get_model()
161  particles = IMP.pmi.tools.select(
162  representation,
163  resolution=resolution)
164  else:
165  raise Exception("EM2D: must pass hier or representation")
166 
167  self.weight=1.0
168  self.rs = IMP.RestraintSet(self.m, 'em2d_FFT')
169  self.label = "None"
170 
171  # read
173  imgs = IMP.em2d.read_images(images, srw)
174  rows = imgs[0].get_header().get_number_of_rows()
175  cols = imgs[0].get_header().get_number_of_columns()
176 
177  params = IMP.em2d.Em2DRestraintParameters(pixel_size, image_resolution, projection_number)
178 
179  # This method (recommended) uses preprocessing of the images and projections to speed-up the registration
180  params.coarse_registration_method = IMP.em2d.ALIGN2D_PREPROCESSING
181  params.optimization_steps = 50
182  params.simplex_initial_length = 0.1
183  params.simplex_minimum_size = 0.02
184 
185  # use true if you want to save the projections from the model that best match the EM images
186  params.save_match_images = False
187 
188  ######################
189  # set up the em2D restraint
190  ######################
191  score_function = IMP.em2d.EM2DScore()
192  em2d_restraint = IMP.em2d.Em2DRestraint(self.m)
193  em2d_restraint.setup(score_function, params)
194  em2d_restraint.set_images(imgs)
195  em2d_restraint.set_fast_mode(5)
196  em2d_restraint.set_name("em2d_restraint")
197 
198  print ("len(particles) = ", len(particles))
199  container = IMP.container.ListSingletonContainer(self.m, particles)
200  em2d_restraint.set_particles(container)
201 
202  self.rs.add_restraint(em2d_restraint)
203 
204  def set_label(self, label):
205  self.label = label
206 
207  def add_to_model(self):
208  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
209 
210  def get_restraint(self):
211  return self.rs
212 
213  def set_weight(self,weight):
214  self.weight=weight
215  self.rs.set_weight(self.weight)
216 
217  def get_output(self):
218  self.m.update()
219  output = {}
220  score = self.weight*self.rs.unprotected_evaluate(None)
221  output["_TotalScore"] = str(score)
222  output["ElectronMicroscopy2D_FFT_" + self.label] = str(score)
223  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:37
Images read_images(const Strings &names, const ImageReaderWriter *rw)
An individual file or directory.
Definition: metadata.py:166
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:731
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:117