IMP logo
IMP Reference Guide  develop.7400db2aee,2024/11/24
The Integrative Modeling Platform
local_fitting.py
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 import IMP.em
11 import IMP.core
12 import IMP.atom
13 import random
14 import math
15 import sys
16 
17 IMP.setup_from_argv(sys.argv, "local fitting")
18 
19 IMP.set_log_level(IMP.SILENT)
20 IMP.set_check_level(IMP.NONE)
21 m = IMP.Model()
22 # 1. setup the input protein
23 # 1.1 select a selector.
24 # using NonWater selector is more accurate but slower
25 # sel=IMP.atom.NonWaterPDBSelector()
27 # 1.2 read the protein
28 mh = IMP.atom.read_pdb(IMP.em.get_example_path("input.pdb"), m, sel)
29 mh_ref = IMP.atom.read_pdb(IMP.em.get_example_path("input.pdb"), m, sel)
30 # 1.3 add radius info to each atom, otherwise the resampling would fail.
32 IMP.atom.add_radii(mh_ref)
33 ps = IMP.core.get_leaves(mh)
34 ps_ref = IMP.core.get_leaves(mh_ref)
35 # 2. read the density map of the protein
36 resolution = 8.
37 voxel_size = 1.5
38 dmap = IMP.em.read_map(
40 dmap.get_header_writable().set_resolution(resolution)
41 # 3. The protein is now fitted correctly in the density. We can validate
42 # that by making sure that the cross-correlation score is close to 1.
43 
44 # 3.1 generate a sampled density map to the same resolution and spacing as
45 # the target density map. Note that the function we are going to use
46 # (cross_correlation_coefficient) expects to get the same map dimensions as
47 # the target density map.
48 sampled_input_density = IMP.em.SampledDensityMap(dmap.get_header())
49 sampled_input_density.set_particles(ps)
50 sampled_input_density.resample()
51 sampled_input_density.calcRMS()
52 IMP.em.write_map(sampled_input_density, "vv0.mrc", IMP.em.MRCReaderWriter())
53 # 3.2 calculate the cross-correlation score, which should be close to 1
55  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
56 print("The CC score of the native transformation is:", best_score)
57 
58 # 4. To demonstrate local fitting we locally rotate and translate the
59 # protein and show how we can go back to the correct placement.
60 
61 # 4.1 define a local transformation
63  IMP.algebra.get_unit_bounding_box_3d())
64 axis = IMP.algebra.get_random_vector_on(IMP.algebra.get_unit_sphere_3d())
65 rand_angle = random.uniform(-70. / 180 * math.pi, 70. / 180 * math.pi)
66 r = IMP.algebra.get_rotation_about_axis(axis, rand_angle)
67 local_trans = IMP.algebra.Transformation3D(r, translation)
68 # 4.2 rotate the protein
69 # prot_xyz=IMP.core.XYZs(IMP.core.get_leaves(mh))
70 # for xyz in prot_xyz:
71 # xyz.set_coordinates(local_trans.get_transformed(xyz.get_coordinates()))
72 # 4.2 set the protein as a rigid body
74 prot_rb = IMP.core.RigidMember(IMP.core.get_leaves(mh)[0]).get_rigid_body()
75 # 4.3 apply the transformation to the protein
76 IMP.core.transform(prot_rb, local_trans)
77 m.update() # to make sure the transformation was applied
78 # 4.4 print the new correlation score, should be lower than before
79 print(len(IMP.core.get_leaves(mh)))
80 IMP.atom.write_pdb(mh, "input2.pdb")
81 sampled_input_density.resample()
82 sampled_input_density.calcRMS()
83 IMP.em.write_map(sampled_input_density, "vv.mrc", IMP.em.MRCReaderWriter())
85  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
86 start_rmsd = IMP.atom.get_rmsd(IMP.core.XYZs(ps), IMP.core.XYZs(ps_ref))
87 print("The start score is:", start_score, "with rmsd of:", start_rmsd)
88 # 5. apply local fitting
89 # 5.1 run local fitting
90 print("performing local refinement, may run for 3-4 minutes")
91 # translate the molecule to the center of the density
93  prot_rb,
96  dmap.get_centroid() - IMP.core.get_centroid(ps)))
97 m.update() # to make sure the transformation was applied
98 sampled_input_density.resample()
99 sampled_input_density.calcRMS()
100 rmsd = IMP.atom.get_rmsd(IMP.core.XYZs(ps), IMP.core.XYZs(ps_ref))
102  dmap, sampled_input_density, sampled_input_density.get_header().dmin)
103 print("The score after centering is:", score2, "with rmsd of:", rmsd)
104 
106 fitting_sols = IMP.em.local_rigid_fitting(
107  mh.get_particle(), refiner,
109  dmap, [], 2, 10, 10)
110 
111 # 5.2 report best result
112 # 5.2.1 transform the protein to the preferred transformation
113 print("The start score is:", start_score, "with rmsd of:", start_rmsd)
114 for i in range(fitting_sols.get_number_of_solutions()):
115  IMP.core.transform(prot_rb, fitting_sols.get_transformation(i))
116  m.update() # to make sure the transformation was applied
117  # 5.2.2 calc rmsd to native configuration
118  rmsd = IMP.atom.get_rmsd(
120  IMP.atom.write_pdb(mh, "temp_" + str(i) + ".pdb")
121  print("Fit with index:", i, " with cc: ", 1. - fitting_sols.get_score(i),
122  " and rmsd to native of:", rmsd)
123  IMP.atom.write_pdb(mh, "sol_" + str(i) + ".pdb")
125  prot_rb, fitting_sols.get_transformation(i).get_inverse())
126 print("done")
Simple 3D transformation class.
static FloatKey get_mass_key()
Return the hierarchy leaves under a particle.
Definition: LeavesRefiner.h:25
double get_coarse_cc_coefficient(const DensityMap *grid1, const DensityMap *grid2, double grid2_voxel_data_threshold, bool allow_padding=false, FloatPair norm_factors=FloatPair(0., 0.))
Calculates the cross correlation coefficient between two maps.
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
void add_radii(Hierarchy d, const ForceFieldParameters *ffp=get_all_atom_CHARMM_parameters(), FloatKey radius_key=FloatKey("radius"))
Add vdW radius from given force field.
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
algebra::Vector3D get_centroid(const XYZs &ps)
Get the centroid.
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
void read_pdb(TextInput input, int model, Hierarchy h)
Vector3D get_random_vector_in(const Cylinder3D &c)
Generate a random vector in a cylinder with uniform density.
Rotation3D get_rotation_about_axis(const Vector3D &axis, double angle)
Generate a Rotation3D object from a rotation around an axis.
Definition: Rotation3D.h:417
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Class for sampling a density map from particles.
double get_rmsd(const Selection &s0, const Selection &s1)
void transform(XYZ a, const algebra::Transformation3D &tr)
Apply a transformation to the particle.
Basic utilities for handling cryo-electron microscopy 3D density maps.
static const IMP::core::HierarchyTraits & get_traits()
Get the molecular hierarchy HierarchyTraits.
std::string get_example_path(std::string file_name)
Return the full path to one of this module's example files.
void set_log_level(LogLevel l)
Set the current global log level.
FittingSolutions local_rigid_fitting(Particle *p, Refiner *refiner, const FloatKey &weight_key, DensityMap *dmap, OptimizerStates display_log, Int number_of_optimization_runs=5, Int number_of_mc_steps=10, Int number_of_cg_steps=100, Float max_translation=2., Float max_rotation=.3, bool fast=true)
Local rigid fitting of a rigid body.
Basic functionality that is expected to be used by a wide variety of IMP users.
IMP::core::RigidBody create_rigid_body(Hierarchy h)
Rotation3D get_identity_rotation_3d()
Return a rotation that does not do anything.
Definition: Rotation3D.h:352
VectorD< D > get_random_vector_on(const UnitSimplexD< D > &s)
Generate a random vector on a unit simplex with uniform density.
Select all CA ATOM records.
Definition: pdb.h:142
Functionality for loading, creating, manipulating and scoring atomic structures.
void set_check_level(CheckLevel tf)
Control runtime checks in the code.
Definition: exception.h:72