IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
domino/restraint_cache.py

Caching restraint scores so that restraints are not evaluated repeatedly for the same configuration is an important part of domino. Without caching, sub assignments that are shared between subsets will be rescored. The IMP::domino::RestraintCache provides a centralized place for this. To use it, one creates one and then adds the restraints you want to use for filtering and scoring to it. You can then pass the cache to the IMP::domino::RestraintScoreFilterTable and it will filter based on those restraints. You can also extract scores from the table directly, using it to manage the loading of particle states.

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")