IMP logo
IMP Reference Guide  2.20.2
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 slower run times.
6 
7 import IMP
8 import IMP.domino
9 import IMP.core
10 import IMP.container
11 import sys
12 
13 IMP.setup_from_argv(sys.argv, "custom filter")
14 
15 # set restraints
16 
17 
18 def create_scoring(m, ps):
19  pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
21  # the restraint will be broken apart during optimization
23  [(ps[p[0]], ps[p[1]]) for p in pairs],
24  "Restrained pairs")
25  pr = IMP.container.PairsRestraint(score, pc)
26  pr.set_maximum_score(.01)
27  # d = IMP.core.DistanceToSingletonScore(IMP.core.HarmonicUpperBound(2, 1),
28  # IMP.algebra.Vector3D(2, 0, 0))
29  return [pr]
30 
31 
32 def create_representation(m):
33  ps = []
34  for i in range(0, 6):
35  p = m.add_particle("P%d" % i)
37  ps.append(p)
38  return ps
39 
40 
41 def create_discrete_states(m, ps):
43  vs = [IMP.algebra.Vector3D(1, 0, 0),
44  IMP.algebra.Vector3D(0, 1, 0),
45  IMP.algebra.Vector3D(1, 1, 0),
46  IMP.algebra.Vector3D(2, 1, 0),
47  IMP.algebra.Vector3D(2, 0, 0)]
48  vs = vs + [-v for v in vs]
49  print(len(vs), "states for each particle")
50  print(vs[1])
51  states = IMP.domino.XYZStates(vs)
52  # special case ps[0] to remove a sliding degree of freedom
53  for p in ps[1:]:
54  pst.set_particle_states(m.get_particle(p), states)
55  return pst
56 
57 # force particle p to be in state s
58 
59 
60 class MyFilterTable(IMP.domino.SubsetFilterTable):
61 
62  class MyFilter(IMP.domino.SubsetFilter):
63 
64  def __init__(self, pos, value):
65  # print "Filtering with", pos, value
66  IMP.domino.SubsetFilter.__init__(
67  self, "MF" + str(pos) + " " + str(value))
68  self.pos = pos
69  self.value = value
70 
71  def get_next_state(self, pos, s):
72  # suggest that the sampler try the correct state
73  # this method is only called if the filter failed, so pos must be
74  # self.pos
75  if s[self.pos] > self.value:
76  # a very large number
77  return 2 ** 29
78  else:
79  return self.value
80 
81  def get_is_ok(self, state):
82  # it is only OK if it has the required state
83  ret = state[self.pos] == self.value
84  return ret
85 
86  def get_strength(self, s, excluded):
87  # return the maximum value since it dictates the state for the particle
88  return 1
89 
90  def __init__(self, p, s):
91  # set the name of the object to something vaguely useful
92  IMP.domino.SubsetFilterTable.__init__(
93  self, "MFT" + p.get_name() + " at " + str(s))
94  self.p = p
95  self.s = s
96 
97  def get_subset_filter(self, subset, excluded):
98  # create a filter if self.p is in subset but not in excluded
99  if (self.p in subset
100  and self.p not in sum([list(x) for x in excluded], [])):
101  # pass the position of self.p and the value that it must have
102  return self.MyFilter(list(subset).index(self.p), self.s)
103  else:
104  return None
105 
106 
107 def create_sampler(m, ps, rs, pst):
108  s = IMP.domino.DominoSampler(m, pst)
109  s.set_restraints(rs)
110  # s.set_log_level(IMP.VERBOSE)
111  # the following lines recreate the defaults and so are optional
112  filters = []
113  # do not allow particles with the same ParticleStates object
114  # to have the same state index
115  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
116  rc = IMP.domino.RestraintCache(pst)
117  rc.add_restraints(rs)
118  # filter states that score worse than the cutoffs in the Model
120  filters[-1].set_log_level(IMP.SILENT)
121  mf = MyFilterTable(m.get_particle(ps[1]), 0)
122  # try with and without this line
123  filters.append(mf)
124  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
125  # states.set_log_level(IMP.SILENT);
126  s.set_assignments_table(states)
127  s.set_subset_filter_tables(filters)
128  return s
129 
130 
131 IMP.set_log_level(IMP.TERSE)
132 m = IMP.Model()
133 m.set_log_level(IMP.SILENT)
134 
135 print("creating representation")
136 ps = create_representation(m)
137 print("creating discrete states")
138 pst = create_discrete_states(m, ps)
139 print("creating score function")
140 rs = create_scoring(m, ps)
141 
142 print("creating sampler")
143 s = create_sampler(m, ps, rs, pst)
144 
145 # s.set_log_level(IMP.SILENT)
146 print("sampling")
147 cs = s.create_sample()
148 
149 print("found ", cs.get_number_of_configurations(), "solutions")
151 for i in range(cs.get_number_of_configurations()):
152  cs.load_configuration(i)
153  print("solution number:", i, " is:", sf.evaluate(False))
154  for p in ps:
155  print(IMP.core.XYZ(m, p).get_x(), IMP.core.XYZ(m, p).get_y())
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
Sample best solutions using Domino.
Definition: DominoSampler.h:32
Filter a configuration of the subset using the Model thresholds.
Create a scoring function on a list of restraints.
Score distance between two particle centers using a harmonic function.
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Do not allow two particles to be in the same state.
Store a list of ParticleIndexPairs.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
void set_log_level(LogLevel l)
Set the current global log level.
Basic functionality that is expected to be used by a wide variety of IMP users.
VectorD< 3 > Vector3D
Definition: VectorD.h:425
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.