IMP logo
IMP Reference Guide  2.5.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.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.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.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 text files in the format required
83  for point alignment in multifit2
84  """
85  import IMP.multifit as mfit
86  edges = []
87  for v in vs:
88  edges.append([0, 0])
89 
90  ap = mfit.AnchorsData(vs, edges)
91  mfit.write_txt(fn_output, ap)
92 
93 
94 def get_common_title():
95  return "# coordinates x | y | z\n"
96 
97 
99 
100  """
101  Parseable output for a IMP Transformation3D
102  """
103 
104  def __init__(self, T, delimiter="|"):
105  q = T.get_rotation().get_quaternion()
106  tr = T.get_translation()
107  self.rot_text = delimiter.join([str(i) for i in q])
108  self.tr_text = delimiter.join([str(i) for i in tr])
109  self.delimiter = delimiter
110 
111  def get_text(self):
112  return self.delimiter.join([self.rot_text, self.tr_text])
113 
114 
115 class TextToTransformation3D:
116 
117  def __init__(self, text, delimiter="|"):
118  vals = [float(x) for x in text.split(delimiter)]
119  if(len(vals) != 7):
120  raise ValueError("The text is not a transformation", vals)
121  R = alg.Rotation3D(vals[0], vals[1], vals[2], vals[3])
122  t = alg.Vector3D(vals[4], vals[5], vals[6])
123  self.t = alg.Transformation3D(R, t)
124 
125  def get_transformation(self):
126  return self.t
127 
128 
130 
131  """
132  Transform a IMP reference frame into parseable output
133  """
134 
135  def __init__(self, ref, delimiter="|"):
136  T = ref.get_transformation_to()
137  Transformation3DToText.__init__(self, T, delimiter)
138 
139 
140 class TextToReferenceFrame(TextToTransformation3D):
141 
142  def __init__(self, text, delimiter="|"):
143  TextToTransformation3D.__init__(self, text, delimiter)
144  self.ref = alg.ReferenceFrame3D(self.get_transformation())
145 
146  def get_reference_frame(self):
147  return self.ref
148 
149 
150 def read_transforms(fn, n=False):
151  """
152  Read a file of alg.Transformation3D. The it is assumed that the
153  transformations are the only thing contained in a line
154  """
155  f = open(fn, "r")
156  Ts = []
157  for l in f:
158  if(n):
159  if(len(Ts) == n):
160  break
161  T = TextToTransformation3D(l).get_transformation()
162  Ts.append(T)
163  f.close()
164  return Ts
165 
166 
167 def write_transforms(Ts, fn):
168  """
169  Write a file with the Transformation3Ds contained in Ts
170  """
171  f = open(fn, "w")
172  for T in Ts:
173  txt = Transformation3DToText(T).get_text()
174  f.write(txt + "\n")
175  f.close()
176 
177 
178 def read_reference_frames(fn, n=10):
179  """
180  Read the reference frames contained in a solutions file from sampling
181  n is the maximum number of ref frames to read.
182  NOTE: Currently the function returns only the reference frames and
183  discards the score, the first element of a row
184  """
185  rows = csv_related.read_csv(fn, delimiter="/")
186  x = min(n, len(rows))
187  all_refs = []
188  for i, row in enumerate(rows[0:x]):
189  refs = [TextToReferenceFrame(t).get_reference_frame() for t in row[1:]]
190  all_refs.append(refs)
191  return all_refs
192 
193 
194 def write_pdb_for_reference_frames(fn_pdbs, refs_texts, fn_output):
195  """
196  Read the PDB files, apply reference frames to them, and write a pdb
197  """
198  model = IMP.Model()
199  assembly = representation.create_assembly(model, fn_pdbs)
200  rbs = representation.create_rigid_bodies(assembly)
201  for t, rb in zip(refs_texts, rbs):
202  r = TextToReferenceFrame(t).get_reference_frame()
203  rb.set_reference_frame(r)
204  atom.write_pdb(assembly, fn_output)
205 
206 def imp_info(imp_modules=None):
207  """
208  Returns text with the time and information about the modules employed
209  imp_modules is the set of modules whose infos are requested
210  """
211 
212  tt = time.asctime()
213  if(imp_modules is None):
214  versions = [IMP.get_module_version()]
215 # versions = [IMP.get_module_version_info()] # until SVN 11063
216  else:
217 # versions = [p.get_module_version_info() for p in imp_modules] # until
218 # SVN 11063
219  versions = [p.get_module_version() for p in imp_modules]
220  text = "# " + tt + "\n"
221  for x in versions:
222 # text += "# " + x.get_module() + " " + x.get_version() + "\n" # until SVN
223 # 11063
224  text += "# " + x + "\n"
225  return text
Parseable output for a IMP Transformation3D.
Definition: io.py:98
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).
Various classes to hold sets of particles.
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:81
Transform a IMP reference frame into parseable output.
Definition: io.py:129
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
def imp_info
Returns text with the time and information about the modules employed imp_modules is the set of modul...
Definition: io.py:206
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:178
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
Fitting atomic structures into a cryo-electron microscopy density map.
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...
Class to handle individual model particles.
Definition: Particle.h:37
def read_transforms
Read a file of alg.Transformation3D.
Definition: io.py:150
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:167
def write_pdb_for_reference_frames
Read the PDB files, apply reference frames to them, and write a pdb.
Definition: io.py:194
std::string get_module_version()