IMP  2.3.1
The Integrative Modeling Platform
custom_filter.py
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)
24  IMP.algebra.Vector3D(2, 0, 0))
25  return [pr]
26 
27 
28 def create_representation(m):
29  ps = []
30  for i in range(0, 6):
31  p = IMP.kernel.Particle(m)
33  ps.append(p)
34  return ps
35 
36 
37 def create_discrete_states(ps):
39  vs = [IMP.algebra.Vector3D(1, 0, 0),
40  IMP.algebra.Vector3D(0, 1, 0),
41  IMP.algebra.Vector3D(1, 1, 0),
42  IMP.algebra.Vector3D(2, 1, 0),
43  IMP.algebra.Vector3D(2, 0, 0)]
44  vs = vs + [-v for v in vs]
45  print len(vs), "states for each particle"
46  print vs[1]
47  states = IMP.domino.XYZStates(vs)
48  # special case ps[0] to remove a sliding degree of freedom
49  for p in ps[1:]:
50  pst.set_particle_states(p, states)
51  return pst
52 
53 # force particle p to be in state s
54 
55 
56 class MyFilterTable(IMP.domino.SubsetFilterTable):
57 
58  class MyFilter(IMP.domino.SubsetFilter):
59 
60  def __init__(self, pos, value):
61  # print "Filtering with", pos, value
62  IMP.domino.SubsetFilter.__init__(
63  self, "MF" + str(pos) + " " + str(value))
64  self.pos = pos
65  self.value = value
66 
67  def get_next_state(self, pos, s):
68  # suggest that the sampler try the correct state
69  # this method is only called if the filter failed, so pos must be
70  # self.pos
71  if s[self.pos] > self.value:
72  # a very large number
73  return 2 ** 29
74  else:
75  return self.value
76 
77  def get_is_ok(self, state):
78  # it is only OK if it has the required state
79  ret = state[self.pos] == self.value
80  return ret
81 
82  def get_strength(self, s, excluded):
83  # return the maximum value since it dictates the state for the particle
84  return 1
85 
86  def __init__(self, p, s):
87  # set the name of the object to something vaguely useful
88  IMP.domino.SubsetFilterTable.__init__(
89  self, "MFT" + p.get_name() + " at " + str(s))
90  self.p = p
91  self.s = s
92 
93  def get_subset_filter(self, subset, excluded):
94  # create a filter if self.p is in subset but not in excluded
95  if self.p in subset and self.p not in sum([list(x) for x in excluded], []):
96  # pass the position of self.p and the value that it must have
97  return self.MyFilter(list(subset).index(self.p), self.s)
98  else:
99  return None
100 
101 
102 def create_sampler(m, ps, rs, pst):
103  s = IMP.domino.DominoSampler(m, pst)
104  # s.set_log_level(IMP.base.VERBOSE)
105  # the following lines recreate the defaults and so are optional
106  filters = []
107  # do not allow particles with the same ParticleStates object
108  # to have the same state index
109  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
110  rc = IMP.domino.RestraintCache(pst)
111  rc.add_restraints(rs)
112  # filter states that score worse than the cutoffs in the Model
114  filters[-1].set_log_level(IMP.base.SILENT)
115  mf = MyFilterTable(ps[1], 0)
116  # try with and without this line
117  filters.append(mf)
118  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
119  # states.set_log_level(IMP.base.SILENT);
120  s.set_assignments_table(states)
121  s.set_subset_filter_tables(filters)
122  return s
123 
124 IMP.base.set_log_level(IMP.base.TERSE)
125 m = IMP.kernel.Model()
126 m.set_log_level(IMP.base.SILENT)
127 
128 print "creating representation"
129 ps = create_representation(m)
130 print "creating discrete states"
131 pst = create_discrete_states(ps)
132 print "creating score function"
133 rs = create_scoring(m, ps)
134 
135 print "creating sampler"
136 s = create_sampler(m, ps, rs, pst)
137 
138 # s.set_log_level(IMP.base.SILENT)
139 print "sampling"
140 cs = s.create_sample()
141 
142 print "found ", cs.get_number_of_configurations(), "solutions"
143 for i in range(cs.get_number_of_configurations()):
144  cs.load_configuration(i)
145  print "solution number:", i, " is:", m.evaluate(False)
146  for p in ps:
147  print IMP.core.XYZ(p).get_x(), IMP.core.XYZ(p).get_y()
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
void set_log_level(LogLevel l)
Set the current global log level.
Sample best solutions using Domino.
Definition: DominoSampler.h:32
static XYZ setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Filter a configuration of the subset using the kernel::Model thresholds.
Do not allow two particles to be in the same state.
Store a kernel::ParticleIndexPairs.
Apply a function to the distance to a fixed point.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
Class to handle individual model particles.
Basic functionality that is expected to be used by a wide variety of IMP users.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73