IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
The Integrative Modeling Platform
rotamer/rotamer_pdb.py

rotamer_pdb.py is a script demonstrating the usage of RotamerCalculator and RotamerLibrary. It reads a PDB file and a rotamer library file, and tries to rotate the atoms based on the most probable chi angles from the rotamer library. Then it saves the rotated atoms to a specified output PDB file.

Usage:

python rotamer_pdb.py -i <input>.pdb -l <rotamer_library>.lib -o <output>.pdb

Example (the result will be saved into transformed_1z5s_A.pdb):

python rotamer_pdb.py -i ../../atom/test/input/1z5s_A.pdb \ -l /path/to/ALL.bbdep.rotamers.lib -o transformed_1z5s_A.pdb

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