IMP logo
IMP Reference Guide  2.7.0
The Integrative Modeling Platform
transforms.py
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 import IMP
5 import IMP.multifit
6 from IMP import OptionParser
7 
8 __doc__ = "Write assembly transformation file in other formats."
9 
10 class Formatter(object):
11 
12  def __init__(self, fh):
13  self.fh = fh
14 
15  def write_header(self, settings):
16  pass
17 
18 
19 class ChimeraFormatter(Formatter):
20  __doc__ = \
21 """Each line in 'chimera' format lists the transformation index, the
22 cross correlation score, and then the transformation for each component,
23 as a rotation matrix (row-major order) and a translation in angstroms."""
24 
25  def write_line(self, ind, score, transforms):
26  self.fh.write(str(ind) + "\t" + score + "\t")
27  for t in transforms:
28  for k in range(3):
29  v = t.get_rotation().get_rotation_matrix_row(k)
30  self.fh.write(
31  str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " ")
32  v = t.get_translation()
33  self.fh.write(
34  str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " | ")
35  self.fh.write("\n")
36 
37 
38 class DockRefFormatter(Formatter):
39  __doc__ = \
40 """Each line in 'dockref' format lists the transformation for each component,
41 as a set of three Euler angles (in radians about the fixed x, y and z axes)
42 and a translation in angstroms."""
43 
44  def write_header(self, sd):
45  # write the name of the proteins
46  for i in range(sd.get_number_of_component_headers()):
47  self.fh.write(sd.get_component_header(i).get_name() + "|")
48  self.fh.write("\n")
49 
50  def write_line(self, ind, score, transforms):
51  for t in transforms:
52  r = t.get_rotation()
53  tr = t.get_translation()
55  self.fh.write("%.6g %.6g %.6g %.6g %.6g %.6g|"
56  % (eulers.get_x(), eulers.get_y(), eulers.get_z(),
57  tr[0], tr[1], tr[2]))
58  self.fh.write("\n")
59 
60 
61 formatters = {'chimera': ChimeraFormatter,
62  'dockref': DockRefFormatter}
63 
64 
65 def parse_args():
66  usage = """%prog [options] <asmb.input> <scores> <output file>
67 
68 Write assembly transformation file in other formats.
69 
70 """ + "\n\n".join(x.__doc__ for x in formatters.values())
71 
72  parser = OptionParser(usage)
73  parser.add_option("-f", "--format", default='chimera', type="choice",
74  choices=list(formatters.keys()),
75  help="type of output to generate ("
76  + ", ".join(formatters.keys())
77  + "; default: chimera)")
78  options, args = parser.parse_args()
79  if len(args) != 3:
80  parser.error("incorrect number of arguments")
81  return options, args
82 
83 
84 def run(asmb_fn, combs_fn, fmt):
85  sd = IMP.multifit.read_settings(asmb_fn)
86  sd.set_was_used(True)
87  fmt.write_header(sd)
88  # read the fitting files
89  fits = []
90  for i in range(sd.get_number_of_component_headers()):
91  fn = sd.get_component_header(i).get_transformations_fn()
93  for ind, line in enumerate(open(combs_fn)):
94  s = line.split("|")
95  score = s[2]
96  comb = s[1]
97  fmt.write_line(ind, score,
98  [fits[i][int(c)].get_fit_transformation()
99  for i, c in enumerate(comb.split())])
100 
101 
102 def main():
103  options, args = parse_args()
104  fmt = formatters[options.format](open(args[2], 'w'))
105  run(args[0], args[1], fmt)
106 
107 if __name__ == "__main__":
108  main()
SettingsData * read_settings(const char *filename)
FixedXYZ get_fixed_xyz_from_rotation(const Rotation3D &r)
The inverse of rotation_from_fixed_xyz()
def __init__
Set up a new graphXL object.
Definition: topology.py:26
Fitting atomic structures into a cryo-electron microscopy density map.
FittingSolutionRecords read_fitting_solutions(const char *fitting_fn)
Fitting solutions reader.