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