IMP logo
IMP Reference Guide  develop.b3a5ae88fa,2024/05/06
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 ArgumentParser
7 
8 __doc__ = "Write assembly transformation file in other formats."
9 
10 
11 class Formatter(object):
12 
13  def __init__(self, fh):
14  self.fh = fh
15 
16  def write_header(self, settings):
17  pass
18 
19 
20 class ChimeraFormatter(Formatter):
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 
26  def write_line(self, ind, score, transforms):
27  self.fh.write(str(ind) + "\t" + score + "\t")
28  for t in transforms:
29  for k in range(3):
30  v = t.get_rotation().get_rotation_matrix_row(k)
31  self.fh.write(
32  str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " ")
33  v = t.get_translation()
34  self.fh.write(
35  str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " | ")
36  self.fh.write("\n")
37 
38 
39 class DockRefFormatter(Formatter):
40  """Each line in 'dockref' format lists the transformation for each
41  component, as a set of three Euler angles (in radians about the
42  fixed x, y and z axes) 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  desc = """
67 Write assembly transformation file in other formats.
68 
69 """ + "\n\n".join(x.__doc__ for x in formatters.values())
70 
71  p = ArgumentParser(description=desc)
72  p.add_argument("-f", "--format", default='chimera',
73  choices=list(formatters.keys()),
74  help="type of output to generate ("
75  + ", ".join(formatters.keys())
76  + "; default: chimera)")
77  p.add_argument("assembly_file", help="assembly file name")
78  p.add_argument("combinations_file", help="combinations file name")
79  p.add_argument("output_file", help="output file name")
80  return p.parse_args()
81 
82 
83 def run(asmb_fn, combs_fn, fmt):
84  sd = IMP.multifit.read_settings(asmb_fn)
85  sd.set_was_used(True)
86  fmt.write_header(sd)
87  # read the fitting files
88  fits = []
89  for i in range(sd.get_number_of_component_headers()):
90  fn = sd.get_component_header(i).get_transformations_fn()
92  for ind, line in enumerate(open(combs_fn)):
93  s = line.split("|")
94  score = s[2]
95  comb = s[1]
96  fmt.write_line(ind, score,
97  [fits[i][int(c)].get_fit_transformation()
98  for i, c in enumerate(comb.split())])
99 
100 
101 def main():
102  args = parse_args()
103  fmt = formatters[args.format](open(args.output_file, 'w'))
104  run(args.assembly_file, args.combinations_file, fmt)
105 
106 
107 if __name__ == "__main__":
108  main()
SettingsData * read_settings(const char *filename)
Each line in 'dockref' format lists the transformation for each component, as a set of three Euler an...
Definition: transforms.py:39
FixedXYZ get_fixed_xyz_from_rotation(const Rotation3D &r)
The inverse of rotation_from_fixed_xyz()
Fitting atomic structures into a cryo-electron microscopy density map.
IMP-specific subclass of argparse.ArgumentParser.
Definition: __init__.py:9596
FittingSolutionRecords read_fitting_solutions(const char *fitting_fn)
Fitting solutions reader.