IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
transforms.py
1 #!/usr/bin/env python
2 
3 import IMP
4 import IMP.multifit
5 from IMP import ArgumentParser
6 
7 __doc__ = "Write assembly transformation file in other formats."
8 
9 
10 class Formatter:
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  """Each line in 'chimera' format lists the transformation index, the
21  cross correlation score, and then the transformation for each component,
22  as a rotation matrix (row-major order) and a translation in angstroms.
23  """
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  """Each line in 'dockref' format lists the transformation for each
40  component, as a set of three Euler angles (in radians about the
41  fixed x, y and z axes) and a translation in angstroms."""
42 
43  def write_header(self, sd):
44  # write the name of the proteins
45  for i in range(sd.get_number_of_component_headers()):
46  self.fh.write(sd.get_component_header(i).get_name() + "|")
47  self.fh.write("\n")
48 
49  def write_line(self, ind, score, transforms):
50  for t in transforms:
51  r = t.get_rotation()
52  tr = t.get_translation()
54  self.fh.write("%.6g %.6g %.6g %.6g %.6g %.6g|"
55  % (eulers.get_x(), eulers.get_y(), eulers.get_z(),
56  tr[0], tr[1], tr[2]))
57  self.fh.write("\n")
58 
59 
60 formatters = {'chimera': ChimeraFormatter,
61  'dockref': DockRefFormatter}
62 
63 
64 def parse_args():
65  desc = """
66 Write assembly transformation file in other formats.
67 
68 """ + "\n\n".join(x.__doc__ for x in formatters.values())
69 
70  p = ArgumentParser(description=desc)
71  p.add_argument("-f", "--format", default='chimera',
72  choices=list(formatters.keys()),
73  help="type of output to generate ("
74  + ", ".join(formatters.keys())
75  + "; default: chimera)")
76  p.add_argument("assembly_file", help="assembly file name")
77  p.add_argument("combinations_file", help="combinations file name")
78  p.add_argument("output_file", help="output file name")
79  return p.parse_args()
80 
81 
82 def run(asmb_fn, combs_fn, fmt):
83  sd = IMP.multifit.read_settings(asmb_fn)
84  sd.set_was_used(True)
85  fmt.write_header(sd)
86  # read the fitting files
87  fits = []
88  for i in range(sd.get_number_of_component_headers()):
89  fn = sd.get_component_header(i).get_transformations_fn()
91  for ind, line in enumerate(open(combs_fn)):
92  s = line.split("|")
93  score = s[2]
94  comb = s[1]
95  fmt.write_line(ind, score,
96  [fits[i][int(c)].get_fit_transformation()
97  for i, c in enumerate(comb.split())])
98 
99 
100 def main():
101  args = parse_args()
102  fmt = formatters[args.format](open(args.output_file, 'w'))
103  run(args.assembly_file, args.combinations_file, fmt)
104 
105 
106 if __name__ == "__main__":
107  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:38
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:9592
FittingSolutionRecords read_fitting_solutions(const char *fitting_fn)
Fitting solutions reader.