IMP  2.0.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 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.
3 
4 import IMP.domino
5 import IMP.algebra
6 import IMP.container
7 import IMP.atom
8 import IMP.rmf
9 import RMF
10 
11 resolution=.5
12 
13 sls=IMP.base.SetLogState(IMP.base.SILENT)
14 
15 m= IMP.Model()
16 
17 # create some particles and a restraint that scores based on the
18 # length of the chain
20  for i in range(0,int(2.0/resolution))]
21 eps= [IMP.core.XYZR.setup_particle(IMP.Particle(m)) for i in range(0,2)]
22 eps[0].set_coordinates(IMP.algebra.Vector3D(-1,0,0))
23 eps[1].set_coordinates(IMP.algebra.Vector3D(1,0,0))
24 for p in ps:
25  p.set_radius(resolution/2.0)
26 for p in eps:
27  p.set_radius(.4*resolution)
28 order=[eps[0]]+ps+[eps[1]]
29 # create a hierarchy with the particles to aid writing them to RMF
31 for i,p in enumerate(order):
33  h.add_child(IMP.atom.Hierarchy.setup_particle(p))
34  color= IMP.display.get_jet_color(float(i)/(len(order)-1))
36 
37 
40 # allow a maximum distance of 2
41 r.set_maximum_score(0.01)
42 
43 # dumb way to generate points on a gride
44 g= IMP.algebra.DenseFloatGrid3D(resolution, IMP.algebra.get_unit_bounding_box_3d())
45 # create some random sites
46 vs= [g.get_center(i) for i in g.get_all_indexes()]
47 print "States are", vs
49 states=IMP.domino.XYZStates(vs)
50 for p in ps:
51  pst.set_particle_states(p, states)
52 
53 
54 # now create a restraint cache
55 # cache the most recently used one million scores
56 rc= IMP.domino.RestraintCache(pst, 1000000)
57 r.set_log_level(IMP.base.SILENT)
58 rc.add_restraints([r])
59 
61 s.set_check_level(IMP.base.NONE)
63 # pass the cache to the restraint score based filter
64 # it will use all the restraints in the cache
66 s.set_subset_filter_tables([ef, rssft])
67 
68 # create a subset with all the particles
69 # Subsets keep the particles in sorted order and so are different than
70 # a simple list of particles
71 alls= IMP.domino.Subset(ps)
72 
73 # get all the assignments that fit
74 sts= s.get_sample_assignments(alls)
75 
76 # create a temporary file to write things to
77 rmf= RMF.create_rmf_file(IMP.create_temporary_file_name("cache", ".rmf"))
78 print "saving configurations to", rmf.get_name()
79 
81 
82 # the restraint cache processes the restraints to make them more efficient
83 # for use with domino. So we want to get back the processed restraints before
84 # looking at them further
85 domino_restraints= rc.get_restraints()
86 
87 for r in domino_restraints:
88  IMP.show_restraint_hierarchy(r)
89 
90 IMP.rmf.add_restraints(rmf, domino_restraints)
91 # for each assignment load it, and add the configuration to an rmf file
92 print "found assignments", sts
93 # we don't care about messages during saving
94 for i, s in enumerate(sts):
96  # go through the restraints and get the score
97  # here, we only care about its side effect of setting the last score
98  for r in domino_restraints:
99  rc.load_last_score(r, alls, s)
100  # save the configuration and scores to the rmf
101  IMP.rmf.save_frame(rmf, i)