IMP logo
IMP Reference Guide  2.11.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_points(points, model):
42  """ Simply creates IMP particles from a set of 2D points
43  model - is the model to store the particles
44  """
45  particles = []
46  for x in points:
47  p = IMP.Particle(model)
49  d.set_coordinates(IMP.algebra.Vector3D(x[0], x[1], x[2]))
50  d.set_radius(2)
51  particles.append(p)
52  return particles
53 
54 
55 def get_particles_from_vector3ds(vs, model):
56  particles = []
57  for v in vs:
58  p = IMP.Particle(model)
60  d.set_coordinates(v)
61  d.set_radius(2)
62  particles.append(p)
63  return particles
64 
65 
66 def write_particles_as_text(leaves, fn_output):
67  """ Writes a set of particles with coordinates to a file """
68  xyzs = IMP.core.XYZs(leaves)
69  f_output = open(fn_output, "w")
70  f_output.write(io.imp_info([IMP, IMP.em2d]))
71 
72  f_output.write(get_common_title())
73  for xyz in xyzs:
74  x, y, z = xyz.get_coordinates()
75  f_output.write("%8.3f %8.3f %8.3f\n" % (x, y, z))
76  f_output.close()
77 
78 
80  """ writes a text files in the format required
81  for point alignment in multifit2
82  """
83  import IMP.multifit as mfit
84  edges = []
85  for v in vs:
86  edges.append([0, 0])
87 
88  ap = mfit.AnchorsData(vs, edges)
89  mfit.write_txt(fn_output, ap)
90 
91 
92 def get_common_title():
93  return "# coordinates x | y | z\n"
94 
95 
97 
98  """
99  Parseable output for a IMP Transformation3D
100  """
101 
102  def __init__(self, T, delimiter="|"):
103  q = T.get_rotation().get_quaternion()
104  tr = T.get_translation()
105  self.rot_text = delimiter.join([str(i) for i in q])
106  self.tr_text = delimiter.join([str(i) for i in tr])
107  self.delimiter = delimiter
108 
109  def get_text(self):
110  return self.delimiter.join([self.rot_text, self.tr_text])
111 
112 
113 class TextToTransformation3D:
114 
115  def __init__(self, text, delimiter="|"):
116  vals = [float(x) for x in text.split(delimiter)]
117  if(len(vals) != 7):
118  raise ValueError("The text is not a transformation", vals)
119  R = IMP.algebra.Rotation3D(vals[0], vals[1], vals[2], vals[3])
120  t = IMP.algebra.Vector3D(vals[4], vals[5], vals[6])
121  self.t = IMP.algebra.Transformation3D(R, t)
122 
123  def get_transformation(self):
124  return self.t
125 
126 
128 
129  """
130  Transform a IMP reference frame into parseable output
131  """
132 
133  def __init__(self, ref, delimiter="|"):
134  T = ref.get_transformation_to()
135  Transformation3DToText.__init__(self, T, delimiter)
136 
137 
138 class TextToReferenceFrame(TextToTransformation3D):
139 
140  def __init__(self, text, delimiter="|"):
141  TextToTransformation3D.__init__(self, text, delimiter)
142  self.ref = IMP.algebra.ReferenceFrame3D(self.get_transformation())
143 
144  def get_reference_frame(self):
145  return self.ref
146 
147 
148 def read_transforms(fn, n=False):
149  """
150  Read a file of IMP.algebra.Transformation3D. The it is assumed that the
151  transformations are the only thing contained in a line
152  """
153  f = open(fn, "r")
154  Ts = []
155  for l in f:
156  if(n):
157  if(len(Ts) == n):
158  break
159  T = TextToTransformation3D(l).get_transformation()
160  Ts.append(T)
161  f.close()
162  return Ts
163 
164 
165 def write_transforms(Ts, fn):
166  """
167  Write a file with the Transformation3Ds contained in Ts
168  """
169  f = open(fn, "w")
170  for T in Ts:
171  txt = Transformation3DToText(T).get_text()
172  f.write(txt + "\n")
173  f.close()
174 
175 
176 def read_reference_frames(fn, n=10):
177  """
178  Read the reference frames contained in a solutions file from sampling
179  n is the maximum number of ref frames to read.
180  NOTE: Currently the function returns only the reference frames and
181  discards the score, the first element of a row
182  """
183  rows = IMP.EMageFit.csv_related.read_csv(fn, delimiter="/")
184  x = min(n, len(rows))
185  all_refs = []
186  for i, row in enumerate(rows[0:x]):
187  refs = [TextToReferenceFrame(t).get_reference_frame() for t in row[1:]]
188  all_refs.append(refs)
189  return all_refs
190 
191 
192 def write_pdb_for_reference_frames(fn_pdbs, refs_texts, fn_output):
193  """
194  Read the PDB files, apply reference frames to them, and write a pdb
195  """
196  model = IMP.Model()
198  model, fn_pdbs)
200  for t, rb in zip(refs_texts, rbs):
201  r = TextToReferenceFrame(t).get_reference_frame()
202  rb.set_reference_frame(r)
203  IMP.atom.write_pdb(assembly, fn_output)
204 
205 def imp_info(imp_modules=None):
206  """
207  Returns text with the time and information about the modules employed
208  imp_modules is the set of modules whose infos are requested
209  """
210 
211  tt = time.asctime()
212  if(imp_modules is None):
213  versions = [IMP.get_module_version()]
214 # versions = [IMP.get_module_version_info()] # until SVN 11063
215  else:
216 # versions = [p.get_module_version_info() for p in imp_modules] # until
217 # SVN 11063
218  versions = [p.get_module_version() for p in imp_modules]
219  text = "# " + tt + "\n"
220  for x in versions:
221 # text += "# " + x.get_module() + " " + x.get_version() + "\n" # until SVN
222 # 11063
223  text += "# " + x + "\n"
224  return text
Parseable output for a IMP Transformation3D.
Definition: io.py:96
Simple 3D transformation class.
def write_particles_as_text
Writes a set of particles with coordinates to a file.
Definition: io.py:66
VectorD< 2 > Vector2D
Definition: VectorD.h:391
Restraints using electron microscopy 2D images (class averages).
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:79
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:127
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: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:205
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:176
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
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:46
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:395
Class to handle individual particles of a Model object.
Definition: Particle.h:41
def read_transforms
Read a file of IMP.algebra.Transformation3D.
Definition: io.py:148
Functionality for loading, creating, manipulating and scoring atomic structures.
def write_transforms
Write a file with the Transformation3Ds contained in Ts.
Definition: io.py:165
def write_pdb_for_reference_frames
Read the PDB files, apply reference frames to them, and write a pdb.
Definition: io.py:192
std::string get_module_version()