IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
em/local_fitting.py

Shows how to locally refine a fit of a protein inside its density using a MC/CG optimization protocol. This example does not necessarily converge to the global minimum as that may require more optimization steps. If one wishes to use this example as a template for real refinement purposes, please adjust the parameters of the function IMP.em.local_rigid_fitting accordingly.

1 ## \example em/local_fitting.py
2 # Shows how to locally refine a fit of a protein inside
3 # its density using a MC/CG optimization protocol.
4 # This example does not necessarily converge to the global minimum
5 # as that may require more optimization steps.
6 # If one wishes to use this example as a template for real refinement purposes,
7 # please adjust the parameters of the function IMP.em.local_rigid_fitting
8 # accordingly.
9 
10 from __future__ import print_function
11 import IMP.em
12 import IMP.core
13 import IMP.atom
14 import random
15 import math
16 import sys
17 
18 IMP.setup_from_argv(sys.argv, "local fitting")
19 
20 IMP.set_log_level(IMP.SILENT)
21 IMP.set_check_level(IMP.NONE)
22 m = IMP.Model()
23 # 1. setup the input protein
24 # 1.1 select a selector.
25 # using NonWater selector is more accurate but slower
26 # sel=IMP.atom.NonWaterPDBSelector()
28 # 1.2 read the protein
29 mh = IMP.atom.read_pdb(IMP.em.get_example_path("input.pdb"), m, sel)
30 mh_ref = IMP.atom.read_pdb(IMP.em.get_example_path("input.pdb"), m, sel)
31 # 1.3 add radius info to each atom, otherwise the resampling would fail.
33 IMP.atom.add_radii(mh_ref)
34 ps = IMP.core.get_leaves(mh)
35 ps_ref = IMP.core.get_leaves(mh_ref)
36 # 2. read the density map of the protein
37 resolution = 8.
38 voxel_size = 1.5
39 dmap = IMP.em.read_map(
41 dmap.get_header_writable().set_resolution(resolution)
42 # 3. The protein is now fitted correctly in the density. We can validate
43 # that by making sure that the cross-correlation score is close to 1.
44 
45 # 3.1 generate a sampled density map to the same resolution and spacing as
46 # the target density map. Note that the function we are going to use
47 # (cross_correlation_coefficient) expects to get the same map dimensions as
48 # the target density map.
49 sampled_input_density = IMP.em.SampledDensityMap(dmap.get_header())
50 sampled_input_density.set_particles(ps)
51 sampled_input_density.resample()
52 sampled_input_density.calcRMS()
53 IMP.em.write_map(sampled_input_density, "vv0.mrc", IMP.em.MRCReaderWriter())
54 # 3.2 calculate the cross-correlation score, which should be close to 1
56  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
57 print("The CC score of the native transformation is:", best_score)
58 
59 # 4. To demonstrate local fitting we locally rotate and translate the
60 # protein and show how we can go back to the correct placement.
61 
62 # 4.1 define a local transformation
64  IMP.algebra.get_unit_bounding_box_3d())
65 axis = IMP.algebra.get_random_vector_on(IMP.algebra.get_unit_sphere_3d())
66 rand_angle = random.uniform(-70. / 180 * math.pi, 70. / 180 * math.pi)
67 r = IMP.algebra.get_rotation_about_axis(axis, rand_angle)
68 local_trans = IMP.algebra.Transformation3D(r, translation)
69 # 4.2 rotate the protein
70 # prot_xyz=IMP.core.XYZs(IMP.core.get_leaves(mh))
71 # for xyz in prot_xyz:
72 # xyz.set_coordinates(local_trans.get_transformed(xyz.get_coordinates()))
73 # 4.2 set the protein as a rigid body
75 prot_rb = IMP.core.RigidMember(IMP.core.get_leaves(mh)[0]).get_rigid_body()
76 # 4.3 apply the transformation to the protein
77 IMP.core.transform(prot_rb, local_trans)
78 m.update() # to make sure the transformation was applied
79 # 4.4 print the new correlation score, should be lower than before
80 print(len(IMP.core.get_leaves(mh)))
81 IMP.atom.write_pdb(mh, "input2.pdb")
82 sampled_input_density.resample()
83 sampled_input_density.calcRMS()
84 IMP.em.write_map(sampled_input_density, "vv.mrc", IMP.em.MRCReaderWriter())
86  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
87 start_rmsd = IMP.atom.get_rmsd(IMP.core.XYZs(ps), IMP.core.XYZs(ps_ref))
88 print("The start score is:", start_score, "with rmsd of:", start_rmsd)
89 # 5. apply local fitting
90 # 5.1 run local fitting
91 print("performing local refinement, may run for 3-4 minutes")
92 # translate the molecule to the center of the density
94  prot_rb,
97  dmap.get_centroid() - IMP.core.get_centroid(ps)))
98 m.update() # to make sure the transformation was applied
99 sampled_input_density.resample()
100 sampled_input_density.calcRMS()
101 rmsd = IMP.atom.get_rmsd(IMP.core.XYZs(ps), IMP.core.XYZs(ps_ref))
103  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
104 print("The score after centering is:", score2, "with rmsd of:", rmsd)
105 
107 fitting_sols = IMP.em.local_rigid_fitting(
108  mh.get_particle(), refiner,
110  dmap, [], 2, 10, 10)
111 
112 # 5.2 report best result
113 # 5.2.1 transform the protein to the preferred transformation
114 print("The start score is:", start_score, "with rmsd of:", start_rmsd)
115 for i in range(fitting_sols.get_number_of_solutions()):
116  IMP.core.transform(prot_rb, fitting_sols.get_transformation(i))
117  m.update() # to make sure the transformation was applied
118  # 5.2.2 calc rmsd to native configuration
119  rmsd = IMP.atom.get_rmsd(
121  IMP.atom.write_pdb(mh, "temp_" + str(i) + ".pdb")
122  print("Fit with index:", i, " with cc: ", 1. - fitting_sols.get_score(i),
123  " and rmsd to native of:", rmsd)
124  IMP.atom.write_pdb(mh, "sol_" + str(i) + ".pdb")
126  prot_rb, fitting_sols.get_transformation(i).get_inverse())
127 print("done")