IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
displaying_ensembles.py
1## \example display/displaying_ensembles.py
2# The script shows a couple experiments with trying to visualize an
3# ensemble of structures. The ensemble is fairly tight on the assembly
4# scale, but there is significant variation between the location and
5# orientation of the individual proteins (which were modeled as rigid
6# bodies). To save space, the models have had their sidechain atoms
7# removed.
8
9import IMP.display
10import IMP.atom
11import sys
12
14 sys.argv, "Experiments with trying to visualize an ensemble of structures")
15
18
19# turn off internal checks to speed things up
20IMP.set_check_level(IMP.USAGE)
21
22
23def read(m, beyond_file):
24 print("reading")
25 hs = []
26 for i in range(0, beyond_file):
27 # create a simplified version for each chain to speed up computations
29 "ensemble/aligned-" + str(i) + ".pdb")
32 hs.append(hr)
33 for c in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
35 IMP.atom.Chain(c), 4)
36 hr.add_child(simp)
38 print(" ", i)
39 return hs
40
41
42def add_markers(h, c, w):
43 """Add markers to a the passed conformation. The marker locations are chosen
44 pretty thoughtlessly and don't really illustrate the technique well."""
45 def add_marker(s, name):
46 g = IMP.core.XYZRGeometry(s.get_selected_particles()[0])
47 g.set_name(name)
48 g.set_color(c)
49 w.add_geometry(g)
50 s = IMP.atom.Selection(h, chain='B', residue_index=317)
51 add_marker(s, "m0")
52 s = IMP.atom.Selection(h, chain='G', residue_index=212)
53 add_marker(s, "m1")
54 s = IMP.atom.Selection(h, chain='I', residue_index=237)
55 add_marker(s, "m2")
56 s = IMP.atom.Selection(h, chain='F', residue_index=101)
57 add_marker(s, "m3")
58
59
60def get_nice_name(h):
61 nm = h.get_name()
62 return nm[nm.find('-') + 1:nm.rfind('.')]
63
64
65def add_axis(h, c, w, chain_colors):
66 """Add a coordinate axis to show the relative orientation of the protein"""
67 for hc in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
68 rb = IMP.core.RigidMember(hc).get_rigid_body()
69 g = IMP.display.ReferenceFrameGeometry(rb.get_reference_frame())
70 g.set_name(get_nice_name(h) + "_orient")
71 if c:
72 g.set_color(c)
73 else:
74 g.set_color(chain_colors[IMP.atom.Chain(hc).get_id()])
75 w.add_geometry(g)
76
77
78def add_skeleton(h, c, r, w, chain_colors):
79 """Show the connectivity skeleton of the conformation to give an idea of
80 how things are laid out"""
81 for hc0 in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
82 for hc1 in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
83 if hc1 <= hc0:
84 continue
85 d = ps.evaluate_index(h.get_model(),
86 (hc0.get_particle_index(),
87 hc1.get_particle_index()), None)
88 if d < 1:
89 d0 = IMP.core.XYZ(hc0)
90 d1 = IMP.core.XYZ(hc1)
91 mp = .5 * (d0.get_coordinates() + d1.get_coordinates())
93 Cylinder(Segment(d0.get_coordinates(), mp), r))
94 if c:
95 g.set_color(c)
96 else:
97 g.set_color(chain_colors[IMP.atom.Chain(d0).get_id()])
98 g.set_name(get_nice_name(h) + "_skel")
99 w.add_geometry(g)
101 Cylinder(Segment(d1.get_coordinates(), mp), r))
102 if c:
103 g.set_color(c)
104 else:
105 g.set_color(chain_colors[IMP.atom.Chain(d1).get_id()])
106 g.set_name(get_nice_name(h) + "_skel")
107 w.add_geometry(g)
108
109
110IMP.set_log_level(IMP.TERSE)
111m = IMP.Model()
112
113# change to 46 to display all of them
114hs = read(m, 3)
115
116# used to test of two molecules are touching one another
120ps.set_log_level(IMP.SILENT)
121
122
123print("creating rigid bodies")
124base_chains = {}
125for hc in IMP.atom.get_by_type(hs[0], IMP.atom.CHAIN_TYPE):
126 c = IMP.atom.Chain(hc)
127 base_chains[c.get_id()] = c
128
129for i, h in enumerate(hs):
130 for hc in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
131 c = IMP.atom.Chain(hc)
132 if h == hs[0]:
134 else:
135 # Make sure the rigid bodies have equivalent defining reference
136 # frames. If we just used IMP.atom.create_rigid_body, globular
137 # proteins are likely to have different axes computed when
138 # starting in different orientations
140 hc, base_chains[c.get_id()])
141 print(" ", i)
142
143chains = IMP.atom.get_by_type(hs[0], IMP.atom.CHAIN_TYPE)
144chains.sort(key=lambda x: IMP.core.XYZ(x).get_x() + IMP.core.XYZ(x).get_y())
145chain_colors = {}
146for i, c in enumerate(chains):
147 id = IMP.atom.Chain(c).get_id()
149 # IMP.display.get_jet_color(f)
150 chain_colors[id] = color
151
152w = IMP.display.PymolWriter("markers.pym")
153add_markers(hs[0], IMP.display.Color(1, 1, 1), w)
154hso = hs[1:]
155
156
157# sort them spatially so the colors are nicely arranged and allow one to
158# visually connect the position of one end with that of the other
159hso.sort(key=lambda h: IMP.core.XYZ(IMP.atom.Selection(
160 h, chain='I', residue_index=237).get_selected_particles()[0]).get_z())
161print("adding markers", end=' ')
162for i, h in enumerate(hso):
164 IMP.display.Color(1, 0, 0), IMP.display.Color(0, 0, 1), i / 50.)
165 add_markers(h, c, w)
166 print(" ", i)
167w = IMP.display.PymolWriter("axis.pym")
168print("adding axis", end=' ')
169add_axis(hs[0], IMP.display.Color(1, 1, 1), w, chain_colors)
170for i, h in enumerate(hs[1:]):
171 add_axis(h, None, w, chain_colors)
172 print(i, end=' ')
173
174w = IMP.display.PymolWriter("skeletons.pym")
175add_skeleton(hs[0], IMP.display.Color(1, 1, 1), 5, w, chain_colors)
176print("adding skeleton", end=' ')
177for i, h in enumerate(hs[1:]):
178 add_skeleton(h, None, 1, w, chain_colors)
179 print(" ", i)
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
Represent a cylinder in 3D.
Definition Cylinder3D.h:27
Simple implementation of segments in 3D.
Definition Segment3D.h:24
Select all CA ATOM records.
Definition pdb.h:148
Store info for a chain of a protein.
Definition Chain.h:61
static const IMP::core::HierarchyTraits & get_traits()
Get the molecular hierarchy HierarchyTraits.
static Hierarchy setup_particle(Model *m, ParticleIndex pi, ParticleIndexesAdaptor children=ParticleIndexesAdaptor())
Create a Hierarchy of level t by adding the needed attributes.
void destroy(Hierarchy d)
Delete the Hierarchy.
Hierarchies get_by_type(Hierarchy mhd, GetByType t)
Gather all the molecular particles of a certain level in the hierarchy.
Select hierarchy particles identified by the biological name.
Definition Selection.h:76
Upper bound harmonic function (non-zero when feature > mean).
Apply a score to a fixed number of close pairs from the two sets.
Return the hierarchy leaves under a particle.
A score on the distance between the surfaces of two spheres.
Display an IMP::core::XYZR particle as a ball.
Definition XYZR.h:151
A decorator for a particle with x,y,z coordinates.
Definition XYZ.h:30
Represent an RGB color.
Definition Color.h:25
Write a CGO file with the geometry.
Definition PymolWriter.h:34
Functionality for loading, creating, manipulating and scoring atomic structures.
IMP::core::RigidBody create_rigid_body(const Hierarchies &h, std::string name=std::string("created rigid body"))
Rigidify a molecule or collection of molecules.
IMP::core::RigidBody create_compatible_rigid_body(Hierarchy h, Hierarchy reference)
Rigidify a molecule or collection of molecules.
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.
Hierarchy create_simplified_along_backbone(Hierarchy input, int num_res, bool keep_detailed=false, Model *m=nullptr)
Output IMP model data in various file formats.
std::string get_example_path(std::string file_name)
Return the full path to one of this module's example files.
Color get_display_color(unsigned int i)
Color get_interpolated_rgb(const Color &a, const Color &b, double f)
Return a color interpolated between a and b in RGB space.
Definition Color.h:151
void set_log_level(LogLevel l)
Set the current global log level.
void set_check_level(CheckLevel tf)
Control runtime checks in the code.
Definition exception.h:72
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.