IMP logo
IMP Reference Guide  2.22.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 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")