IMP logo
IMP Reference Guide  2.20.1
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.atom
8 import IMP.core
9 import IMP.algebra
10 import IMP.em2d
13 import time
14 import logging
15 log = logging.getLogger("io")
16 
17 
18 def get_vectors_from_points(points, vector_type="2d"):
19  if vector_type == "2d":
20  return [IMP.algebra.Vector2D(p[0], p[1]) for p in points]
21  elif vector_type == "3d":
22  return [IMP.algebra.Vector3D(p[0], p[1], p[2]) for p in points]
23  else:
24  raise ValueError("vector type not recognized")
25 
26 
27 def get_particles_from_points(points, model):
28  """ Simply creates IMP particles from a set of 2D points
29  model - is the model to store the particles
30  """
31  particles = []
32  for x in points:
33  p = IMP.Particle(model)
35  d.set_coordinates(IMP.algebra.Vector3D(x[0], x[1], x[2]))
36  d.set_radius(2)
37  particles.append(p)
38  return particles
39 
40 
41 def get_particles_from_vector3ds(vs, model):
42  particles = []
43  for v in vs:
44  p = IMP.Particle(model)
46  d.set_coordinates(v)
47  d.set_radius(2)
48  particles.append(p)
49  return particles
50 
51 
52 def write_particles_as_text(leaves, fn_output):
53  """ Writes a set of particles with coordinates to a file """
54  xyzs = IMP.core.XYZs(leaves)
55  f_output = open(fn_output, "w")
56  f_output.write(imp_info([IMP, IMP.em2d]))
57 
58  f_output.write(get_common_title())
59  for xyz in xyzs:
60  x, y, z = xyz.get_coordinates()
61  f_output.write("%8.3f %8.3f %8.3f\n" % (x, y, z))
62  f_output.close()
63 
64 
66  """ writes a text files in the format required
67  for point alignment in multifit2
68  """
69  import IMP.multifit as mfit
70  edges = []
71  for v in vs:
72  edges.append([0, 0])
73 
74  ap = mfit.AnchorsData(vs, edges)
75  mfit.write_txt(fn_output, ap)
76 
77 
78 def get_common_title():
79  return "# coordinates x | y | z\n"
80 
81 
83 
84  """
85  Parseable output for a IMP Transformation3D
86  """
87 
88  def __init__(self, T, delimiter="|"):
89  q = T.get_rotation().get_quaternion()
90  tr = T.get_translation()
91  self.rot_text = delimiter.join([str(i) for i in q])
92  self.tr_text = delimiter.join([str(i) for i in tr])
93  self.delimiter = delimiter
94 
95  def get_text(self):
96  return self.delimiter.join([self.rot_text, self.tr_text])
97 
98 
99 class TextToTransformation3D:
100 
101  def __init__(self, text, delimiter="|"):
102  vals = [float(x) for x in text.split(delimiter)]
103  if len(vals) != 7:
104  raise ValueError("The text is not a transformation", vals)
105  R = IMP.algebra.Rotation3D(vals[0], vals[1], vals[2], vals[3])
106  t = IMP.algebra.Vector3D(vals[4], vals[5], vals[6])
107  self.t = IMP.algebra.Transformation3D(R, t)
108 
109  def get_transformation(self):
110  return self.t
111 
112 
114 
115  """
116  Transform a IMP reference frame into parseable output
117  """
118 
119  def __init__(self, ref, delimiter="|"):
120  T = ref.get_transformation_to()
121  Transformation3DToText.__init__(self, T, delimiter)
122 
123 
124 class TextToReferenceFrame(TextToTransformation3D):
125 
126  def __init__(self, text, delimiter="|"):
127  TextToTransformation3D.__init__(self, text, delimiter)
128  self.ref = IMP.algebra.ReferenceFrame3D(self.get_transformation())
129 
130  def get_reference_frame(self):
131  return self.ref
132 
133 
134 def read_transforms(fn, n=False):
135  """
136  Read a file of IMP.algebra.Transformation3D. The it is assumed that the
137  transformations are the only thing contained in a line
138  """
139  f = open(fn, "r")
140  Ts = []
141  for line in f:
142  if n:
143  if len(Ts) == n:
144  break
145  T = TextToTransformation3D(line).get_transformation()
146  Ts.append(T)
147  f.close()
148  return Ts
149 
150 
151 def write_transforms(Ts, fn):
152  """
153  Write a file with the Transformation3Ds contained in Ts
154  """
155  f = open(fn, "w")
156  for T in Ts:
157  txt = Transformation3DToText(T).get_text()
158  f.write(txt + "\n")
159  f.close()
160 
161 
162 def read_reference_frames(fn, n=10):
163  """
164  Read the reference frames contained in a solutions file from sampling
165  n is the maximum number of ref frames to read.
166  NOTE: Currently the function returns only the reference frames and
167  discards the score, the first element of a row
168  """
169  rows = IMP.EMageFit.csv_related.read_csv(fn, delimiter="/")
170  x = min(n, len(rows))
171  all_refs = []
172  for i, row in enumerate(rows[0:x]):
173  refs = [TextToReferenceFrame(t).get_reference_frame() for t in row[1:]]
174  all_refs.append(refs)
175  return all_refs
176 
177 
178 def write_pdb_for_reference_frames(fn_pdbs, refs_texts, fn_output):
179  """
180  Read the PDB files, apply reference frames to them, and write a pdb
181  """
182  model = IMP.Model()
184  model, fn_pdbs)
186  for t, rb in zip(refs_texts, rbs):
187  r = TextToReferenceFrame(t).get_reference_frame()
188  rb.set_reference_frame(r)
189  IMP.atom.write_pdb(assembly, fn_output)
190 
191 
192 def imp_info(imp_modules=None):
193  """
194  Returns text with the time and information about the modules employed
195  imp_modules is the set of modules whose infos are requested
196  """
197 
198  tt = time.asctime()
199  if imp_modules is None:
200  versions = [IMP.get_module_version()]
201  else:
202  versions = [p.get_module_version() for p in imp_modules]
203  text = "# " + tt + "\n"
204  for x in versions:
205  text += "# " + x + "\n"
206  return text
Parseable output for a IMP Transformation3D.
Definition: io.py:82
Simple 3D transformation class.
def write_particles_as_text
Writes a set of particles with coordinates to a file.
Definition: io.py:52
VectorD< 2 > Vector2D
Definition: VectorD.h:421
Restraints using electron microscopy 2D images (class averages).
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:27
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition: XYZR.h:48
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:65
def create_rigid_bodies
set the children of a molecule type hierarchy as rigid bodies In this case, all the children are the ...
Transform a IMP reference frame into parseable output.
Definition: io.py:113
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
A reference frame in 3D.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
def imp_info
Returns text with the time and information about the modules employed imp_modules is the set of modul...
Definition: io.py:192
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:162
Fitting atomic structures into a cryo-electron microscopy density map.
def create_assembly
Read all the PDBs given in the list of names fn_pdbs and adds the hierarchies to the model...
3D rotation class.
Definition: Rotation3D.h:52
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...
VectorD< 3 > Vector3D
Definition: VectorD.h:425
Class to handle individual particles of a Model object.
Definition: Particle.h:43
def read_transforms
Read a file of IMP.algebra.Transformation3D.
Definition: io.py:134
Functionality for loading, creating, manipulating and scoring atomic structures.
def write_transforms
Write a file with the Transformation3Ds contained in Ts.
Definition: io.py:151
def write_pdb_for_reference_frames
Read the PDB files, apply reference frames to them, and write a pdb.
Definition: io.py:178
std::string get_module_version()
Return the version of this module, as a string.