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