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