IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/22
The Integrative Modeling Platform
restraint_cache.py
1 ## \example domino/restraint_cache.py
2 # Caching restraint scores so that restraints are not evaluated
3 # repeatedly for the same configuration is an important part of
4 # domino. Without caching, sub assignments that are shared between
5 # subsets will be rescored. The IMP::domino::RestraintCache provides
6 # a centralized place for this. To use it, one creates one and then
7 # adds the restraints you want to use for filtering and scoring to
8 # it. You can then pass the cache to the
9 # IMP::domino::RestraintScoreFilterTable and it will filter based on
10 # those restraints. You can also extract scores from the table
11 # directly, using it to manage the loading of particle states.
12 
13 import IMP.domino
14 import IMP.algebra
15 import IMP.container
16 import IMP.atom
17 import IMP.rmf
18 import RMF
19 import sys
20 
21 IMP.setup_from_argv(sys.argv, "restraint cache")
22 
23 resolution = .5
24 
25 with IMP.SetLogState(IMP.SILENT):
26  m = IMP.Model()
27 
28  # create some particles and a restraint that scores based on the
29  # length of the chain
31  for i in range(0, int(2.0 / resolution))]
33  for i in range(0, 2)]
34  eps[0].set_coordinates(IMP.algebra.Vector3D(-1, 0, 0))
35  eps[1].set_coordinates(IMP.algebra.Vector3D(1, 0, 0))
36  for p in ps:
37  p.set_radius(resolution / 2.0)
38  for p in eps:
39  p.set_radius(.4 * resolution)
40  order = [eps[0]] + ps + [eps[1]]
41  # create a hierarchy with the particles to aid writing them to RMF
43  for i, p in enumerate(order):
45  h.add_child(IMP.atom.Hierarchy.setup_particle(p))
46  color = IMP.display.get_jet_color(float(i) / (len(order) - 1))
48 
52  # allow a maximum distance of 2
53  r.set_maximum_score(0.01)
54 
55  # dumb way to generate points on a gride
57  resolution, IMP.algebra.get_unit_bounding_box_3d())
58  # create some random sites
59  vs = [g.get_center(i) for i in g.get_all_indexes()]
60  print("States are", vs)
62  states = IMP.domino.XYZStates(vs)
63  for p in ps:
64  pst.set_particle_states(p, states)
65 
66  # now create a restraint cache
67  # cache the most recently used one million scores
68  rc = IMP.domino.RestraintCache(pst, 1000000)
69  r.set_log_level(IMP.SILENT)
70  rc.add_restraints([r])
71 
72  s = IMP.domino.DominoSampler(m, pst)
73  s.set_restraints([r])
74  s.set_check_level(IMP.NONE)
76  # pass the cache to the restraint score based filter
77  # it will use all the restraints in the cache
79  s.set_subset_filter_tables([ef, rssft])
80 
81  # create a subset with all the particles
82  # Subsets keep the particles in sorted order and so are different than
83  # a simple list of particles
84  alls = IMP.domino.Subset(ps)
85 
86  # get all the assignments that fit
87  sts = s.get_sample_assignments(alls)
88 
89  # create a temporary file to write things to
90  rmf = RMF.create_rmf_file(IMP.create_temporary_file_name("cache", ".rmf"))
91  print("saving configurations to", rmf.get_name())
92 
93  IMP.rmf.add_hierarchy(rmf, h)
94 
95  # the restraint cache processes the restraints to make them more efficient
96  # for use with domino. So we want to get back the processed restraints
97  # before looking at them further
98  domino_restraints = rc.get_restraints()
99 
100  for r in domino_restraints:
102 
103  IMP.rmf.add_restraints(rmf, domino_restraints)
104  # for each assignment load it, and add the configuration to an rmf file
105  print("found assignments", sts)
106  # we don't care about messages during saving
107  for i, s in enumerate(sts):
108  IMP.domino.load_particle_states(alls, s, pst)
109  # go through the restraints and get the score
110  # here, we only care about its side effect of setting the last score
111  for r in domino_restraints:
112  rc.load_last_score(r, alls, s)
113  # save the configuration and scores to the rmf
114  IMP.rmf.save_frame(rmf)
115  print("done")
Grid3D< float, DenseGridStorage3D< float > > DenseFloatGrid3D
A harmonic upper bound on the distance between two spheres.
RMF::FrameID save_frame(RMF::FileHandle file, std::string name="")
Save the current state of the linked objects as a new RMF frame.
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition: XYZR.h:48
A container which contains all consecutive particle pairs from an input list.
Sample best solutions using Domino.
Definition: DominoSampler.h:32
Filter a configuration of the subset using the Model thresholds.
Represent a subset of the particles being optimized.
Definition: Subset.h:33
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Do not allow two particles to be in the same state.
static Hierarchy setup_particle(Model *m, ParticleIndex pi, ParticleIndexesAdaptor children=ParticleIndexesAdaptor())
Create a Hierarchy of level t by adding the needed attributes.
void add_hierarchy(RMF::FileHandle fh, atom::Hierarchy hs)
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
Definition: Mass.h:48
A class to change and restore log state.
Definition: SetLogState.h:30
static Colored setup_particle(Model *m, ParticleIndex pi, Color color)
Definition: Colored.h:62
void add_restraints(RMF::NodeHandle fh, const Restraints &hs)
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
void show_restraint_hierarchy(ScoringFunctionAdaptor rs, std::ostream &out=std::cout)
Print the hierarchy of restraints.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Class to handle individual particles of a Model object.
Definition: Particle.h:43
Color get_jet_color(double f)
Return the color for f from the jet color map.
void load_particle_states(const Subset &s, const Assignment &ss, const ParticleStatesTable *pst)
Load the appropriate state for each particle in a Subset.
Functionality for loading, creating, manipulating and scoring atomic structures.
Support for the RMF file format for storing hierarchical molecular data and markup.
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.
std::string create_temporary_file_name(std::string prefix="imp_temp", std::string suffix="")
Create a temporary file.