IMP  2.2.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 
20 resolution = .5
21 
22 sls = IMP.base.SetLogState(IMP.base.SILENT)
23 
24 m = IMP.kernel.Model()
25 
26 # create some particles and a restraint that scores based on the
27 # length of the chain
29  for i in range(0, int(2.0 / resolution))]
31  for i in range(0, 2)]
32 eps[0].set_coordinates(IMP.algebra.Vector3D(-1, 0, 0))
33 eps[1].set_coordinates(IMP.algebra.Vector3D(1, 0, 0))
34 for p in ps:
35  p.set_radius(resolution / 2.0)
36 for p in eps:
37  p.set_radius(.4 * resolution)
38 order = [eps[0]] + ps + [eps[1]]
39 # create a hierarchy with the particles to aid writing them to RMF
41 for i, p in enumerate(order):
43  h.add_child(IMP.atom.Hierarchy.setup_particle(p))
44  color = IMP.display.get_jet_color(float(i) / (len(order) - 1))
46 
47 
51 # allow a maximum distance of 2
52 r.set_maximum_score(0.01)
53 
54 # dumb way to generate points on a gride
56  resolution, IMP.algebra.get_unit_bounding_box_3d())
57 # create some random sites
58 vs = [g.get_center(i) for i in g.get_all_indexes()]
59 print "States are", vs
61 states = IMP.domino.XYZStates(vs)
62 for p in ps:
63  pst.set_particle_states(p, states)
64 
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.base.SILENT)
70 rc.add_restraints([r])
71 
72 s = IMP.domino.DominoSampler(m, pst)
73 s.set_check_level(IMP.base.NONE)
75 # pass the cache to the restraint score based filter
76 # it will use all the restraints in the cache
78 s.set_subset_filter_tables([ef, rssft])
79 
80 # create a subset with all the particles
81 # Subsets keep the particles in sorted order and so are different than
82 # a simple list of particles
83 alls = IMP.domino.Subset(ps)
84 
85 # get all the assignments that fit
86 sts = s.get_sample_assignments(alls)
87 
88 # create a temporary file to write things to
89 rmf = RMF.create_rmf_file(IMP.create_temporary_file_name("cache", ".rmf"))
90 print "saving configurations to", rmf.get_name()
91 
93 
94 # the restraint cache processes the restraints to make them more efficient
95 # for use with domino. So we want to get back the processed restraints before
96 # looking at them further
97 domino_restraints = rc.get_restraints()
98 
99 for r in domino_restraints:
100  IMP.show_restraint_hierarchy(r)
101 
102 IMP.rmf.add_restraints(rmf, domino_restraints)
103 # for each assignment load it, and add the configuration to an rmf file
104 print "found assignments", sts
105 # we don't care about messages during saving
106 for i, s in enumerate(sts):
107  IMP.domino.load_particle_states(alls, s, pst)
108  # go through the restraints and get the score
109  # here, we only care about its side effect of setting the last score
110  for r in domino_restraints:
111  rc.load_last_score(r, alls, s)
112  # save the configuration and scores to the rmf
113  IMP.rmf.save_frame(rmf, i)
114 print "done"