IMP  2.4.0
The Integrative Modeling Platform
io.py
1 """@namespace IMP.EMageFit.imp_general.io
2  Utility functions to handle IO.
3 """
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.display as display
8 import IMP.container as container
9 import IMP.atom as atom
10 import IMP.core as core
11 import IMP.algebra as alg
12 import IMP.em2d as em2d
13 import IMP.EMageFit.imp_general.representation as representation
14 import IMP.EMageFit.csv_related as csv_related
15 import time
16 import logging
17 log = logging.getLogger("io")
18 
19 
20 def get_vectors_from_points(points, vector_type="2d"):
21  if(vector_type == "2d"):
22  return [alg.Vector2D(p[0], p[1]) for p in points]
23  elif(vector_type == "3d"):
24  return [alg.Vector3D(p[0], p[1], p[2]) for p in points]
25  else:
26  raise ValueError("vector type not recognized")
27 
28 
29 def get_particles_from_points(points, model):
30  """ Simply creates IMP particles from a set of 2D points
31  model - is the model to store the particles
32  """
33  particles = []
34  for x in points:
35  p = IMP.kernel.Particle(model)
37  d.set_coordinates(alg.Vector3D(x[0], x[1], x[2]))
38  d.set_radius(2)
39  particles.append(p)
40  return particles
41 
42 
43 def get_particles_from_points(points, model):
44  """ Simply creates IMP particles from a set of 2D points
45  model - is the model to store the particles
46  """
47  particles = []
48  for x in points:
49  p = IMP.kernel.Particle(model)
51  d.set_coordinates(alg.Vector3D(x[0], x[1], x[2]))
52  d.set_radius(2)
53  particles.append(p)
54  return particles
55 
56 
57 def get_particles_from_vector3ds(vs, model):
58  particles = []
59  for v in vs:
60  p = IMP.kernel.Particle(model)
62  d.set_coordinates(v)
63  d.set_radius(2)
64  particles.append(p)
65  return particles
66 
67 
68 def write_particles_as_text(leaves, fn_output):
69  """ Writes a set of particles with coordinates to a file """
70  xyzs = core.XYZs(leaves)
71  f_output = open(fn_output, "w")
72  f_output.write(io.imp_info([IMP, em2d]))
73 
74  f_output.write(get_common_title())
75  for xyz in xyzs:
76  x, y, z = xyz.get_coordinates()
77  f_output.write("%8.3f %8.3f %8.3f\n" % (x, y, z))
78  f_output.close()
79 
80 
82  """ Writes a hierarchy contained in h to the file fn """
83  chw = display.ChimeraWriter(fn)
84  g = display.HierarchyGeometry(h)
85  chw.add_geometry(g)
86  g.set_name(fn)
87 
88 
89 def write_particles_to_chimera(ps, fn, name="particles"):
90  """ Writes a bunch o particles to the file fn
91  It is assumed that the particles can be decorated with XYZR
92  """
93  chw = display.ChimeraWriter(fn)
94  lsc = container.ListSingletonContainer(ps, name)
95  g = display.XYZRsGeometry(lsc)
96  chw.add_geometry(g)
97  g.set_name(fn)
98 
99 
100 def write_xyzrs_to_chimera(xyzrs, fn):
101  """ Writes a bunch o particles to the file fn
102  It is assumed that the particles can be decorated with XYZR
103  """
104  ps = [a.get_particle() for a in xyzrs]
106 
107 
108 def write_points_to_chimera(points, radius, fn, name="points"):
109  """ Writes a bunch o particles to the file fn
110  It is assumed that the particles can be decorated with XYZR
111  """
112  m = IMP.kernel.Model()
113  ps = []
114  for p in points:
115  pa = IMP.kernel.Particle(m)
116  xyzr = core.XYZR.setup_particle(pa)
117  xyzr.set_radius(radius)
118  xyzr.set_coordinates(alg.Vector3D(p[0], p[1], p[2]))
119  ps.append(pa)
120  write_particles_to_chimera(ps, fn, name)
121 
122 
123 def write_vectors_to_chimera(vs, radius, fn, name="vectors"):
124  """
125  Writes vectors as points in chimera
126  """
127 
128  m = IMP.kernel.Model()
129  ps = []
130  for v in vs:
131  pa = IMP.kernel.Particle(m)
132  xyzr = core.XYZR.setup_particle(pa)
133  xyzr.set_radius(radius)
134  xyzr.set_coordinates(v)
135  ps.append(pa)
136  write_particles_to_chimera(ps, fn, name)
137 
138 
140  """ writes a text files in the format required
141  for point alignment in multifit2
142  """
143  import IMP.multifit as mfit
144  edges = []
145  for v in vs:
146  edges.append([0, 0])
147 
148  ap = mfit.AnchorsData(vs, edges)
149  mfit.write_txt(fn_output, ap)
150 
151 
152 def get_common_title():
153  return "# coordinates x | y | z\n"
154 
155 
157 
158  """
159  Parseable output for a IMP Transformation3D
160  """
161 
162  def __init__(self, T, delimiter="|"):
163  q = T.get_rotation().get_quaternion()
164  tr = T.get_translation()
165  self.rot_text = delimiter.join([str(i) for i in q])
166  self.tr_text = delimiter.join([str(i) for i in tr])
167  self.delimiter = delimiter
168 
169  def get_text(self):
170  return self.delimiter.join([self.rot_text, self.tr_text])
171 
172 
173 class TextToTransformation3D:
174 
175  def __init__(self, text, delimiter="|"):
176  vals = [float(x) for x in text.split(delimiter)]
177  if(len(vals) != 7):
178  raise ValueError("The text is not a transformation", vals)
179  R = alg.Rotation3D(vals[0], vals[1], vals[2], vals[3])
180  t = alg.Vector3D(vals[4], vals[5], vals[6])
181  self.t = alg.Transformation3D(R, t)
182 
183  def get_transformation(self):
184  return self.t
185 
186 
188 
189  """
190  Transform a IMP reference frame into parseable output
191  """
192 
193  def __init__(self, ref, delimiter="|"):
194  T = ref.get_transformation_to()
195  Transformation3DToText.__init__(self, T, delimiter)
196 
197 
198 class TextToReferenceFrame(TextToTransformation3D):
199 
200  def __init__(self, text, delimiter="|"):
201  TextToTransformation3D.__init__(self, text, delimiter)
202  self.ref = alg.ReferenceFrame3D(self.get_transformation())
203 
204  def get_reference_frame(self):
205  return self.ref
206 
207 
208 def read_transforms(fn, n=False):
209  """
210  Read a file of alg.Transformation3D. The it is assumed that the
211  transformations are the only thing contained in a line
212  """
213  f = open(fn, "r")
214  Ts = []
215  for l in f:
216  if(n):
217  if(len(Ts) == n):
218  break
219  T = TextToTransformation3D(l).get_transformation()
220  Ts.append(T)
221  f.close()
222  return Ts
223 
224 
225 def write_transforms(Ts, fn):
226  """
227  Write a file with the Transformation3Ds contained in Ts
228  """
229  f = open(fn, "w")
230  for T in Ts:
231  txt = Transformation3DToText(T).get_text()
232  f.write(txt + "\n")
233  f.close()
234 
235 
236 def read_reference_frames(fn, n=10):
237  """
238  Read the reference frames contained in a solutions file from sampling
239  n is the maximum number of ref frames to read.
240  NOTE: Currently the function returns only the reference frames and
241  discards the score, the first element of a row
242  """
243  rows = csv_related.read_csv(fn, delimiter="/")
244  x = min(n, len(rows))
245  all_refs = []
246  for i, row in enumerate(rows[0:x]):
247  refs = [TextToReferenceFrame(t).get_reference_frame() for t in row[1:]]
248  all_refs.append(refs)
249  return all_refs
250 
251 
252 def write_pdb_for_reference_frames(fn_pdbs, refs_texts, fn_output):
253  """
254  Read the PDB files, apply reference frames to them, and write a pdb
255  """
256  model = IMP.kernel.Model()
257  assembly = representation.create_assembly(model, fn_pdbs)
258  rbs = representation.create_rigid_bodies(assembly)
259  for t, rb in zip(refs_texts, rbs):
260  r = TextToReferenceFrame(t).get_reference_frame()
261  rb.set_reference_frame(r)
262  atom.write_pdb(assembly, fn_output)
263 
264 
265 def show_model_info(model, assembly, components_rbs):
266  """ Prints information about the representation
267  Prints the number of components (hierarchies), its children,
268  and information about the chains and rigid bodies
269  """
270  print("##################################################################################")
271  print("Model Info")
272  print("##################################################################################")
273 
274  print(model.show())
275  print("#########################")
276  print("Hierarchies in the assembly:")
277  print("#########################")
278  for c in assembly.get_children():
279  print(c.get_name() + " Is valid? " + c.get_is_valid(True)
280  + " children " + str(c.get_number_of_children()))
281  print("Child info: " + c.show())
282 
283  hchains = IMP.atom.get_by_type(c, IMP.atom.CHAIN_TYPE)
284  print("Number of chains in the hierarchy: %d" % len(hchains))
285  for h in hchains:
286  chain = atom.Chain(h.get_particle())
287  print(chain.get_name() + " particles %d" % len(atom.get_leaves(chain)))
288  print("#########################")
289  print("Rigid bodies")
290  print("#########################")
291  for r in components_rbs:
292  print("rigid body: Particles: %d coordinates: %s"
293  % (r.get_number_of_members(), r.get_coordinates()))
294 
295  print("#########################")
296  print("Restraints")
297  print("#########################")
298  n = model.get_number_of_restraints()
299  for i in range(n):
300  print(model.get_restraint(i))
301 
302 
303 def imp_info(imp_modules=None):
304  """
305  Returns text with the time and information about the modules employed
306  imp_modules is the set of modules whose infos are requested
307  """
308 
309  tt = time.asctime()
310  if(imp_modules is None):
311  versions = [IMP.get_module_version()]
312 # versions = [IMP.get_module_version_info()] # until SVN 11063
313  else:
314 # versions = [p.get_module_version_info() for p in imp_modules] # until
315 # SVN 11063
316  versions = [p.get_module_version() for p in imp_modules]
317  text = "# " + tt + "\n"
318  for x in versions:
319 # text += "# " + x.get_module() + " " + x.get_version() + "\n" # until SVN
320 # 11063
321  text += "# " + x + "\n"
322  return text
Parseable output for a IMP Transformation3D.
Definition: io.py:156
def show_model_info
Prints information about the representation Prints the number of components (hierarchies), its children, and information about the chains and rigid bodies.
Definition: io.py:265
def write_particles_as_text
Writes a set of particles with coordinates to a file.
Definition: io.py:68
Restraints using electron microscopy 2D images (class averages).
Write geometry to a Python file for Chimera to read.
Definition: ChimeraWriter.h:29
Various classes to hold sets of particles.
def write_hierarchy_to_chimera
Writes a hierarchy contained in h to the file fn.
Definition: io.py:81
def write_particles_to_chimera
Writes a bunch o particles to the file fn It is assumed that the particles can be decorated with XYZR...
Definition: io.py:89
Utility functions to handle representation.
def write_vectors_in_multifit2_format
writes a text files in the format required for point alignment in multifit2
Definition: io.py:139
Transform a IMP reference frame into parseable output.
Definition: io.py:187
def write_vectors_to_chimera
Writes vectors as points in chimera.
Definition: io.py:123
def imp_info
Returns text with the time and information about the modules employed imp_modules is the set of modul...
Definition: io.py:303
static XYZR setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: XYZR.h:48
def read_reference_frames
Read the reference frames contained in a solutions file from sampling n is the maximum number of ref ...
Definition: io.py:236
def write_xyzrs_to_chimera
Writes a bunch o particles to the file fn It is assumed that the particles can be decorated with XYZR...
Definition: io.py:100
Store a kernel::ParticleIndexes.
Hierarchies get_by_type(Hierarchy mhd, GetByType t)
def get_particles_from_points
Simply creates IMP particles from a set of 2D points model - is the model to store the particles...
Definition: io.py:29
def write_points_to_chimera
Writes a bunch o particles to the file fn It is assumed that the particles can be decorated with XYZR...
Definition: io.py:108
Fitting atomic structures into a cryo-electron microscopy density map.
Class to handle individual model particles.
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...
Store info for a chain of a protein.
Definition: Chain.h:21
def read_transforms
Read a file of alg.Transformation3D.
Definition: io.py:208
Output IMP model data in various file formats.
Functionality for loading, creating, manipulating and scoring atomic structures.
def write_transforms
Write a file with the Transformation3Ds contained in Ts.
Definition: io.py:225
def write_pdb_for_reference_frames
Read the PDB files, apply reference frames to them, and write a pdb.
Definition: io.py:252
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73