IMP  2.1.0
The Integrative Modeling Platform
domino/custom_filter.py

This example looks like the six particle optimization example except a filter is used instead of a restraint to remove the flip degree of freedom. The filter is written is python, which makes for quick prototyping, but slow run times.

1 ## \example domino/custom_filter.py
2 # This example looks like the six particle optimization example except a
3 # filter is used instead of a restraint to remove the flip degree of
4 # freedom. The filter is written is python, which makes for quick
5 # prototyping, but slow run times.
6 
7 import IMP
8 import IMP.domino
9 import IMP.core
10 import IMP.container
11 
12 # set restraints
13 
14 
15 def create_scoring(m, ps):
16  pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
18  # the restraint will be broken apart during optimization
19  pc = IMP.container.ListPairContainer([(ps[p[0]], ps[p[1]]) for p in pairs],
20  "Restrained pairs")
21  pr = IMP.container.PairsRestraint(score, pc)
22  pr.set_maximum_score(.01)
23  pr.set_model(m)
25  IMP.algebra.Vector3D(2, 0, 0))
26  return [pr]
27 
28 
29 def create_representation(m):
30  ps = []
31  for i in range(0, 6):
32  p = IMP.kernel.Particle(m)
34  ps.append(p)
35  return ps
36 
37 
38 def create_discrete_states(ps):
40  vs = [IMP.algebra.Vector3D(1, 0, 0),
41  IMP.algebra.Vector3D(0, 1, 0),
42  IMP.algebra.Vector3D(1, 1, 0),
43  IMP.algebra.Vector3D(2, 1, 0),
44  IMP.algebra.Vector3D(2, 0, 0)]
45  vs = vs + [-v for v in vs]
46  print len(vs), "states for each particle"
47  print vs[1]
48  states = IMP.domino.XYZStates(vs)
49  # special case ps[0] to remove a sliding degree of freedom
50  for p in ps[1:]:
51  pst.set_particle_states(p, states)
52  return pst
53 
54 # force particle p to be in state s
55 
56 
57 class MyFilterTable(IMP.domino.SubsetFilterTable):
58 
59  class MyFilter(IMP.domino.SubsetFilter):
60 
61  def __init__(self, pos, value):
62  # print "Filtering with", pos, value
63  IMP.domino.SubsetFilter.__init__(
64  self, "MF" + str(pos) + " " + str(value))
65  self.pos = pos
66  self.value = value
67 
68  def get_next_state(self, pos, s):
69  # suggest that the sampler try the correct state
70  # this method is only called if the filter failed, so pos must be
71  # self.pos
72  if s[self.pos] > self.value:
73  # a very large number
74  return 2 ** 29
75  else:
76  return self.value
77 
78  def get_is_ok(self, state):
79  # it is only OK if it has the required state
80  ret = state[self.pos] == self.value
81  return ret
82 
83  def get_strength(self, s, excluded):
84  # return the maximum value since it dictates the state for the particle
85  return 1
86 
87  def __init__(self, p, s):
88  # set the name of the object to something vaguely useful
89  IMP.domino.SubsetFilterTable.__init__(
90  self, "MFT" + p.get_name() + " at " + str(s))
91  self.p = p
92  self.s = s
93 
94  def get_subset_filter(self, subset, excluded):
95  # create a filter if self.p is in subset but not in excluded
96  if self.p in subset and self.p not in sum([list(x) for x in excluded], []):
97  # pass the position of self.p and the value that it must have
98  return self.MyFilter(list(subset).index(self.p), self.s)
99  else:
100  return None
101 
102 
103 def create_sampler(m, ps, rs, pst):
104  s = IMP.domino.DominoSampler(m, pst)
105  # s.set_log_level(IMP.base.VERBOSE)
106  # the following lines recreate the defaults and so are optional
107  filters = []
108  # do not allow particles with the same ParticleStates object
109  # to have the same state index
110  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
111  rc = IMP.domino.RestraintCache(pst)
112  rc.add_restraints(rs)
113  # filter states that score worse than the cutoffs in the Model
115  filters[-1].set_log_level(IMP.base.SILENT)
116  mf = MyFilterTable(ps[1], 0)
117  # try with and without this line
118  filters.append(mf)
119  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
120  # states.set_log_level(IMP.base.SILENT);
121  s.set_assignments_table(states)
122  s.set_subset_filter_tables(filters)
123  return s
124 
125 IMP.base.set_log_level(IMP.base.TERSE)
126 m = IMP.kernel.Model()
127 m.set_log_level(IMP.base.SILENT)
128 
129 print "creating representation"
130 ps = create_representation(m)
131 print "creating discrete states"
132 pst = create_discrete_states(ps)
133 print "creating score function"
134 rs = create_scoring(m, ps)
135 
136 print "creating sampler"
137 s = create_sampler(m, ps, rs, pst)
138 
139 # s.set_log_level(IMP.base.SILENT)
140 print "sampling"
141 cs = s.get_sample()
142 
143 print "found ", cs.get_number_of_configurations(), "solutions"
144 for i in range(cs.get_number_of_configurations()):
145  cs.load_configuration(i)
146  print "solution number:", i, " is:", m.evaluate(False)
147  for p in ps:
148  print IMP.core.XYZ(p).get_x(), IMP.core.XYZ(p).get_y()