IMP logo
IMP Reference Guide  develop.0d0d3c41d6,2026/09/13
The Integrative Modeling Platform
pmi/em.py
1 ## \example pmi/em.py
2 """This script shows how to create DENSITY representations.
3 and set up the Gaussian EM restraint.
4 
5 These representations are useful when you are doing EM fitting with
6 rigid bodies.
7 
8 Preliminary step: you should convert your EM map to a GMM file
9 with the command line utility create_gmm.py
10 (located in isd/pyext/src/create_gmm.py):
11 python create_gmm.py emd_1883.mrc 50 emd_1883.gmm50.txt -m emd_1883.gmm50.mrc
12 """
13 
14 import IMP
15 import IMP.atom
16 import IMP.algebra
17 import IMP.rmf
18 import IMP.pmi
19 import IMP.pmi.topology
20 import IMP.pmi.dof
21 import IMP.pmi.macros
23 import sys
24 try:
25  import sklearn
26 except ImportError:
27  print("To run this example, please first install the 'sklearn'")
28  print("Python module.")
29  sys.exit(0)
30 
31 IMP.setup_from_argv(sys.argv, "Set up the EM restraint")
32 
33 # ##################### SYSTEM SETUP #####################
34 # Preliminaries
35 mdl = IMP.Model()
37 
38 # Setup just one molecule
40 st = s.create_state()
41 mol = st.create_molecule("Rpn4", sequence=seqs["1WCM:D"], chain_id="D")
42 atomic_res = mol.add_structure(
43  IMP.pmi.get_example_path('data/1WCM_fitted.pdb'), chain_id="D", offset=0)
44 
45 # Below we create a GMM approximation for this moleucle
46 # This "DENSITY" representation is used in the GaussianEMRestraint (and others
47 # in the future)
48 # For structure regions we "fit" GMM components to all atom centers
49 mol.add_representation(
50  atomic_res,
51  resolutions=[1, 10],
52  # how much to coarsen this representation
53  density_residues_per_component=10,
54  # will write a .txt and .mrc file forcomponent
55  density_prefix="Rpn4_gmm",
56  # set True if you want to overwrite
57  density_force_compute=False,
58  # set to 0 if you don't care about writing the map
59  # if rasterizing takes too long, increase this value
60  density_voxel_size=3.0)
61 
62 # for the unstructured regions, we simply decorate each bead AS a Gaussian,
63 # so no fitting is necessary
64 mol.add_representation(mol.get_non_atomic_residues(),
65  resolutions=[10],
66  # just set this flag, nothing is written
67  setup_particles_as_densities=True)
68 
69 hier = s.build()
70 
71 # You can always check the representations with:
73 
74 # ##################### RESTRAINTS #####################
75 output_objects = []
76 
77 # To add the GaussianEMRestraint, first select all densities
78 densities = IMP.atom.Selection(
79  hier, representation_type=IMP.atom.DENSITIES).get_selected_particles()
81  densities,
82  # created by user, see top of file
83  target_fn=IMP.pmi.get_example_path('data/emd_1883.gmm50.txt'),
84  # a small number, helps drag bits into map
85  slope=0.01,
86  # if the model is the same size as map, usually set to True
87  scale_target_to_mass=False,
88  # manually set the mass of the target map (remove if you set above to True)
89  target_mass_scale=100000,
90  # the data weight
91  weight=100.0)
92 emr.add_to_model()
93 output_objects.append(emr)
94 mdl.update()
95 print(emr.evaluate())