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