IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
plot_search.py
1"""
2Path searches
3=============
4IMP.bff can to search for an optimal path on a grid with obstacles
5between two points using established algorithms (e.g., Dijkstra and
6A star). These path search algorithms can be used to implement accessible
7volume calculations of label for fluorescence or EPR experiments in a
81:N (one starting node N end nodes) path search (Dijkstra) or for 1:1
9(one start node one end node) path searches (Astar), e.g., for
10implementing cross-linking restraints.
11"""
12import pylab as plt
13import pathlib
14import numpy as np
15
16import IMP
17import IMP.core
18import IMP.algebra
19import IMP.atom
20import IMP.bff
21import IMP.em
22
23# %%
24# Setup system
25# ------------
26# First, we create an IMP Model and a hierarchy from a PDB file. We select
27# a residue in the hierarchy as a starting point for the path search. The
28# starting point of the path search will be the central point of a 3D grid.
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# Create PathMapHeader
43# --------------------
44# The PathMapHeader defines the grid spacing and the size of the grid used
45# in the path search. The grid will have an edge that has twice the maximum
46# path length.
47max_path_length = 20.0
48grid_spacing = 1.0
49path_map_header = IMP.bff.PathMapHeader(max_path_length, grid_spacing)
50path_map_header.set_path_origin(path_origin)
51
52# %%
53# Create PathMap, sample obstacles
54# --------------------------------
55# After creating a PathMap assign particles to the map and sample
56# the obstacles (assign densities to the grid).
57path_map = IMP.bff.PathMap(path_map_header)
58ps = [a.get_particle() for a in IMP.atom.get_leaves(hier)]
59path_map.set_particles(ps)
60path_map.sample_obstacles(extra_radius=0.0)
61
62
63# %%
64# Get start/stop index
65# --------------------
66# A path is searched from a start to an end point on the grid of the
67# path map. The start and the end point are identified by their index.
68# a points on the grid can be found by their cartesian coordinates.
69start_idx = path_map.get_voxel_by_location(path_origin)
70end_idx = 8062
71r_end = path_map.get_location_by_voxel(end_idx)
72r_start = path_map.get_location_by_voxel(start_idx)
73
74# %%
75# Update tiles
76# ------------
77# Points on the grid are associated to tiles that have edges that connect to other
78# (neighboring) tiles. Tiles that are occupied are not considered as neighbors and
79# do not have edges connecting to them. After sampling obstacles tiles and edges
80# of tiles should be updated. Updating the tiles resets the costs of visiting a tile.
81# the cost of visiting a tile are updated by the path search (here Djikstra).
82
83path_map.find_path_dijkstra(start_idx, end_idx)
84
85# %%
86# Path-backtracking
87# -----------------
88# After searching for a path, each tile has a predecessor (i.e., a tile that was the
89# previous tile in a path search). Tile that have no predecessor were not visited. The
90# predecessors are used to back track path from specific tile to start of the path
91# search. We compare two path found by Djikstra and A-Star.
92tiles = path_map.get_tiles()
93path_dijkstra = tiles[end_idx].backtrack_to_path()
94
95path_map.find_path_astar(start_idx, end_idx)
96path_astar = path_map.get_tiles()[end_idx].backtrack_to_path()
97
98# %%
99# Compare paths
100# -------------
101# The A star algorithm uses the Eucledian distance to the end point as a
102# heuristic in the search.
103# While Djikstra solves the 1:N problem, i.e., searches for path from all
104# points to the starting point. Thus, A-star and Djikstra find different
105# path and A star is usually faster.
106
107def get_distances(path):
108 locations = [path_map.get_location_by_voxel(i) for i in path]
109 distances_start = [IMP.algebra.get_distance(r_start, l) for l in locations]
110 distances_end = [IMP.algebra.get_distance(r_end, l) for l in locations]
111 return distances_start, distances_end
112
113dsd, ded = get_distances(path_dijkstra)
114dsa, dea = get_distances(path_astar)
115fig, axs = plt.subplots(1, 2, sharex=True)
116axs[0].set(xlabel='step', ylabel='Distance from end')
117axs[0].plot(ded, label="Dijkstra")
118axs[0].plot(dea, label="Astar")
119axs[1].set(xlabel='step', ylabel='Cummulative path length')
120axs[1].plot(np.cumsum(dsd), label="Dijkstra")
121axs[1].plot(np.cumsum(dsa), label="Astar")
122axs[1].set_title('A-star')
123axs[0].legend()
124axs[1].legend()
125plt.savefig('plot.png')
126#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
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
double get_distance(const Line3D &s, const Vector3D &p)
Get closest distance between a line and a point.
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.