IMP logo
IMP Reference Guide  2.20.2
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
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 from __future__ import print_function
21 import IMP
22 import IMP.core
23 import IMP.atom
24 import IMP.algebra
25 import IMP.rotamer
26 
27 
28 def transform(input_pdb, input_lib, output_pdb):
29  # read the original PDB
30  m = IMP.Model()
31  orig_h = IMP.atom.read_pdb(input_pdb, m)
32  mh = IMP.atom.get_by_type(orig_h, IMP.atom.RESIDUE_TYPE)
33 
34  # read rotamer library
36  rl.read_library_file(input_lib)
38 
39  # get the most probable rotamers
40  rotamers = list()
41  for h in mh:
42  rd = IMP.atom.Residue(h)
43  rr = rc.get_rotamer(rd, 0.01)
44  rotamers.append((rd, rr))
45 
46  # now set the coordinates of all atoms in the residues to the rotated
47  # coordinates
48  for rd, rr in rotamers:
49  for h in IMP.atom.get_by_type(rd, IMP.atom.ATOM_TYPE):
50  at = IMP.atom.Atom(h)
51  at_t = at.get_atom_type()
52  if rr.get_atom_exists(at_t):
53  # some atoms might not be rotated
54  idx = min(rr.get_number_of_cases(at_t) - 1, 1)
55  v = rr.get_coordinates(idx, at_t)
56  xyz = IMP.core.XYZ(at)
57  xyz.set_coordinates(v)
58 
59  # save the rotated atoms to output PDB
60  IMP.atom.write_pdb(orig_h, output_pdb)
61 
62 
63 def quick_test():
66  rc.set_was_used(True)
67 
68 
69 if __name__ == '__main__':
70 
71  import sys
72 
73  P = IMP.ArgumentParser()
74  P.add_argument('--input_pdb', '-i', action='store',
75  help='input PDB file (required)')
76  P.add_argument('--input_lib', '-l', action='store',
77  help='input rotamer library file (required)')
78  P.add_argument('--output_pdb', '-o', action='store',
79  help='output PDB file (required)')
80  P.add_argument('--verbose', '-v', action='store_true',
81  help='show more messages')
82  args = P.parse_args()
83  if IMP.get_bool_flag('run_quick_test') or \
84  not (args.input_pdb or args.input_lib or args.output_pdb):
85  quick_test()
86  sys.exit(0)
87  if not args.input_pdb:
88  print('--input_pdb is required')
89  sys.exit(1)
90  if not args.output_pdb:
91  print('--output_pdb is required')
92  sys.exit(1)
93  if not args.input_lib:
94  print('--input_lib is required')
95  sys.exit(1)
96  if args.verbose:
97  IMP.set_log_level(IMP.VERBOSE)
98  else:
99  IMP.set_log_level(IMP.SILENT)
100  transform(args.input_pdb, args.input_lib, args.output_pdb)
A class storing a whole rotamer library read from a file.
bool get_bool_flag(std::string name)
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
void read_pdb(TextInput input, int model, Hierarchy h)
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
void transform(Hierarchy h, const algebra::Transformation3D &tr)
Transform a hierarchy. This is aware of rigid bodies.
A decorator for a particle representing an atom.
Definition: atom/Atom.h:238
A class performing the rotations of atoms in the residues.
Sampling of sidechain rotamers.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
IMP-specific subclass of argparse.ArgumentParser.
Definition: __init__.py:9596
void set_log_level(LogLevel l)
Set the current global log level.
A decorator for a residue.
Definition: Residue.h:137
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
Functionality for loading, creating, manipulating and scoring atomic structures.