IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
plot_path_maps.py
1"""
2Path search
3===========
4Here, PathMap is used to find paths to all other points
5on a grid around an attachment site. Grid points (voxels)
6that are occupied by the structure are obstacles in the
7path search.
8
9"""
10import pylab as plt
11import pathlib
12
13import IMP
14import IMP.core
15import IMP.atom
16import IMP.bff
17import IMP.em
18
19# %%
20# Setup of system
21# ---------------
22# In this example we search the path from a single attachment site
23# of a label to all other points in space within the reach of the
24# label. First, we create a new IMP model and load the coordinates
25# from a PDB file. The atoms in the PDB file will serve as obstacles
26# the coordinates of an atom of the corresponding structure will
27# be the starting point for the path search.
28
29m = IMP.Model()
30pdb_fn = pathlib.Path(IMP.bff.get_example_path('structure')) / "T4L/3GUN.pdb"
31hier = IMP.atom.read_pdb(str(pdb_fn), m)
32
33sel = IMP.atom.Selection(hier)
34sel.set_chain_id('A')
35sel.set_residue_index(132)
36sel.set_atom_type(IMP.atom.AtomType('CB'))
37sel_particles = sel.get_selected_particles()
38labeled_p = IMP.core.XYZ(sel_particles[0])
39path_origin = labeled_p.get_coordinates()
40
41# %%
42# Initializing a PathMap
43# ----------------------
44# Next, we create a header for the PathMap. The PathMap header contains
45# information on the maximum path-length, the grid spacing, the 'neighbor
46# radius' (a parameter that is used to define the neighboring grid points),
47# and an obstacle threshold. The maximal path length parameter determines
48# the size of the grid and the grid spacing determines the resolution of the
49# grid. In a first step the density of obstacles on the grid is samples.
50# The obstacle threshold parameter defines what density is used to define
51# a grid point as occupied. Grid points (voxels) are connected by edges. The
52# neighbor_radius parameter defines how far apart voxels can be separated and
53# still be considered as connected. With a PathMapHeader a new PathMap can
54# be created.
55path_map_header_parameter = {
56 "max_path_length": 20.0,
57 "grid_spacing": 1.0,
58 "neighbor_radius": 2,
59 "obstacle_threshold": 1e-5
60}
61path_map_header = IMP.bff.PathMapHeader(**path_map_header_parameter)
62path_map_header.set_path_origin(path_origin)
63path_map = IMP.bff.PathMap(path_map_header)
64
65# %%
66# Setup of obstacles
67# ------------------
68# IMP particles (with XYZR decorator) can be used to define obstacles in
69# a PathMap. The obstacles can be sampled with an additional (extra radius).
70# Each voxel in a PathMap has an associated tile, that is connected via
71# edges to other tiles of the map. Tiles that are on places of obstacles do
72# not have edges. After sampling the tiles and corresponding edges are updated.
73ps = [a.get_particle() for a in IMP.atom.get_leaves(hier)]
74path_map.set_particles(ps)
75path_map.sample_obstacles(extra_radius=0.0)
76path_map.update_tiles()
77
78# %%
79# The obstacles in a PathMap can be written to density files using
80# standard IMP routines.
81
82# IMP.em.write_map(path_map, "OBSTACLES.mrc")
83
84# %%
85# Path search
86# -----------
87# The path search starts from a tile in the PathMap. Here, we start the path search
88# at the origin (the center) of the PathMap. To find the distance from the start index
89# to all other tiles we use the Dijkstra algorithm and search for an index not in the
90# PathMap (default: -1)
91start_idx = path_map.get_voxel_by_location(path_origin)
92path_map.find_path_dijkstra(start_idx, -1) # if the end_idx
93# %%
94# Now, we can backtrace the shortest path from every tile (voxel) to the
95# start index. A path is a sequence of tile/voxel ids.
96t = path_map.get_tiles()
97end_idx_1 = 829
98path_1 = t[end_idx_1].backtrack_to_path()
99print(path_1)
100
101end_idx_2 = 229
102path_2 = t[end_idx_2].backtrack_to_path()
103print(path_2)
104
105# %%
106# Not to all tiles a path can be found. A tile without a path to the
107# starting tile has a path-length of 1.
108paths = [t[e].backtrack_to_path() for e in range(path_map.get_number_of_voxels())]
109path_length = [len(p) for p in paths]
110plt.hist(path_length, 27)
111plt.savefig('plot1.png')
112#plt.show()
113
114no_paths = [i for i, p in enumerate(paths) if len(p) <= 1]
115
116
117# %%
118# Tile feature
119# ------------
120# The most important attributes of tiles are the penalty and the cost
121# of visiting a tile (from the starting point). Additionally, a tile
122# has a density, e.g., used to implement weighted accessible volumes
123# and a set of other (user-defined) features that are stored in a
124# dictionary.
125bounds = 0.0, 30
126BFF_TILE_PENALTY = path_map.get_tile_values(IMP.bff.PM_TILE_PENALTY, (0, 1))
127PM_TILE_COST = path_map.get_tile_values(IMP.bff.PM_TILE_COST, bounds)
128PM_TILE_COST_DENSITY = path_map.get_tile_values(IMP.bff.PM_TILE_COST_DENSITY, bounds)
129
130#%%
131# These features are returned as 3D arrays.
132plt.imshow(PM_TILE_COST_DENSITY[16])
133plt.savefig('plot2.png')
134#plt.show()
135
136#%%
137# These features are returned as three dimensional arrays.
138fig, axs = plt.subplots(1, 3, sharex=True)
139axs[0].imshow(BFF_TILE_PENALTY[16])
140axs[1].imshow(PM_TILE_COST[16])
141axs[2].imshow(PM_TILE_COST_DENSITY[16])
142plt.savefig('plot3.png')
143#plt.show()
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Select hierarchy particles identified by the biological name.
Definition Selection.h:76
Class to search path on grids.
Definition PathMap.h:45
A decorator for a particle with x,y,z coordinates.
Definition XYZ.h:30
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchy read_pdb(TextInput input, Model *model, PDBSelector *selector=get_default_pdb_selector(), bool select_first_model=true)
Read all the molecules in the first model of the PDB file.
Hierarchies get_leaves(const Selection &h)
Bayesian Fluorescence Framework.
std::string get_example_path(std::string file_name)
Return the full path to one of this module's example files.
Basic functionality that is expected to be used by a wide variety of IMP users.
Basic utilities for handling cryo-electron microscopy 3D density maps.