IMP logo
IMP Reference Guide  2.7.0
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 from __future__ import print_function
14 import IMP.domino
15 import IMP.algebra
16 import IMP.container
17 import IMP.atom
18 import IMP.rmf
19 import RMF
20 import sys
21 
22 IMP.setup_from_argv(sys.argv, "restraint cache")
23 
24 resolution = .5
25 
26 with IMP.SetLogState(IMP.SILENT):
27  m = IMP.Model()
28 
29  # create some particles and a restraint that scores based on the
30  # length of the chain
32  for i in range(0, int(2.0 / resolution))]
34  for i in range(0, 2)]
35  eps[0].set_coordinates(IMP.algebra.Vector3D(-1, 0, 0))
36  eps[1].set_coordinates(IMP.algebra.Vector3D(1, 0, 0))
37  for p in ps:
38  p.set_radius(resolution / 2.0)
39  for p in eps:
40  p.set_radius(.4 * resolution)
41  order = [eps[0]] + ps + [eps[1]]
42  # create a hierarchy with the particles to aid writing them to RMF
44  for i, p in enumerate(order):
46  h.add_child(IMP.atom.Hierarchy.setup_particle(p))
47  color = IMP.display.get_jet_color(float(i) / (len(order) - 1))
49 
53  # allow a maximum distance of 2
54  r.set_maximum_score(0.01)
55 
56  # dumb way to generate points on a gride
58  resolution, IMP.algebra.get_unit_bounding_box_3d())
59  # create some random sites
60  vs = [g.get_center(i) for i in g.get_all_indexes()]
61  print("States are", vs)
63  states = IMP.domino.XYZStates(vs)
64  for p in ps:
65  pst.set_particle_states(p, states)
66 
67  # now create a restraint cache
68  # cache the most recently used one million scores
69  rc = IMP.domino.RestraintCache(pst, 1000000)
70  r.set_log_level(IMP.SILENT)
71  rc.add_restraints([r])
72 
73  s = IMP.domino.DominoSampler(m, pst)
74  s.set_restraints([r])
75  s.set_check_level(IMP.NONE)
77  # pass the cache to the restraint score based filter
78  # it will use all the restraints in the cache
80  s.set_subset_filter_tables([ef, rssft])
81 
82  # create a subset with all the particles
83  # Subsets keep the particles in sorted order and so are different than
84  # a simple list of particles
85  alls = IMP.domino.Subset(ps)
86 
87  # get all the assignments that fit
88  sts = s.get_sample_assignments(alls)
89 
90  # create a temporary file to write things to
91  rmf = RMF.create_rmf_file(IMP.create_temporary_file_name("cache", ".rmf"))
92  print("saving configurations to", rmf.get_name())
93 
94  IMP.rmf.add_hierarchy(rmf, h)
95 
96  # the restraint cache processes the restraints to make them more efficient
97  # for use with domino. So we want to get back the processed restraints
98  # before looking at them further
99  domino_restraints = rc.get_restraints()
100 
101  for r in domino_restraints:
103 
104  IMP.rmf.add_restraints(rmf, domino_restraints)
105  # for each assignment load it, and add the configuration to an rmf file
106  print("found assignments", sts)
107  # we don't care about messages during saving
108  for i, s in enumerate(sts):
109  IMP.domino.load_particle_states(alls, s, pst)
110  # go through the restraints and get the score
111  # here, we only care about its side effect of setting the last score
112  for r in domino_restraints:
113  rc.load_last_score(r, alls, s)
114  # save the configuration and scores to the rmf
115  IMP.rmf.save_frame(rmf)
116  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:72
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:44
A class to change and restore log state.
Definition: SetLogState.h:31
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:395
Class to handle individual particles of a Model object.
Definition: Particle.h:41
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.