IMP  2.0.1
The Integrative Modeling Platform
rotamer_pdb.py
1 ## \example rotamer/rotamer_pdb.py
2 ## rotamer_pdb.py is a script demonstrating the usage of RotamerCalculator and RotamerLibrary.
3 ## It reads a PDB file and a rotamer library file, and tries to rotate the atoms based on the most
4 ## probable chi angles from the rotamer library. Then it saves the rotated atoms to a specified output
5 ## PDB file.
6 ##
7 ## Usage:
8 ##
9 ## `python rotamer_pdb.py -i <input>.pdb -l <rotamer_library>.lib -o <output>.pdb`
10 ##
11 ## Example (the result will be saved into transformed_1z5s_A.pdb):
12 ##
13 ## `../../../tools/imppy.sh python rotamer_pdb.py -i ../../atom/test/input/1z5s_A.pdb \
14 ## -l /path/to/ALL.bbdep.rotamers.lib -o transformed_1z5s_A.pdb`
15 ##
16 
17 #!/usr/bin/env python
18 
19 import IMP
20 import IMP.core
21 import IMP.atom
22 import IMP.algebra
23 import IMP.rotamer
24 
25 
26 def transform(input_pdb, input_lib, output_pdb):
27  # read the original PDB
28  m = IMP.Model()
29  orig_h = IMP.atom.read_pdb(input_pdb, m)
30  mh = IMP.atom.get_by_type(orig_h, IMP.atom.RESIDUE_TYPE)
31 
32  # read rotamer library
34  rl.read_library_file(input_lib)
36 
37  # get the most probable rotamers
38  rotamers = list()
39  for h in mh:
40  rd = h.get_as_residue()
41  rr = rc.get_rotamer(rd, 0.01)
42  rotamers.append((rd, rr))
43 
44  # now set the coordinates of all atoms in the residues to the rotated coordinates
45  for rd, rr in rotamers:
46  for h in IMP.atom.get_by_type(rd, IMP.atom.ATOM_TYPE):
47  at = h.get_as_atom()
48  at_t = at.get_atom_type()
49  if rr.get_atom_exists(at_t):
50  # some atoms might not be rotated
51  idx = min(rr.get_number_of_cases(at_t) - 1, 1)
52  v = rr.get_coordinates(idx, at_t)
53  xyz = IMP.core.XYZ(at)
54  xyz.set_coordinates(v)
55 
56  # save the rotated atoms to output PDB
57  IMP.atom.write_pdb(orig_h, output_pdb)
58 
59 
60 def quick_test():
63  rc.set_was_used(True)
64 
65 
66 if __name__ == '__main__':
67 
68  import sys
69  import optparse
70 
71  P = optparse.OptionParser()
72  P.add_option('--input_pdb', '-i', action='store', type='string',
73  help='input PDB file (required)')
74  P.add_option('--input_lib', '-l', action='store', type='string',
75  help='input rotamer library file (required)')
76  P.add_option('--output_pdb', '-o', action='store', type='string',
77  help='output PDB file (required)')
78  P.add_option('--verbose', '-v', action='store_true',
79  help='show more messages')
80  P.add_option('--run_quick_test', action='store_true',
81  help='run quick test')
82  opts, args = P.parse_args()
83  if opts.run_quick_test or \
84  not (opts.input_pdb or opts.input_lib or opts.output_pdb):
85  quick_test()
86  sys.exit(0)
87  if not opts.input_pdb:
88  print '--input_pdb is required'
89  sys.exit(1)
90  if not opts.output_pdb:
91  print '--output_pdb is required'
92  sys.exit(1)
93  if not opts.input_lib:
94  print '--input_lib is required'
95  sys.exit(1)
96  if opts.verbose:
97  IMP.base.set_log_level(IMP.base.VERBOSE)
98  else:
99  IMP.base.set_log_level(IMP.base.SILENT)
100  transform(opts.input_pdb, opts.input_lib, opts.output_pdb)