13 class MoverScheme(object):
14 def __init__(self, length):
16 self.mover_scheme =
None
17 self.score_scheme = {}
18 self.score_initial_weights = {}
22 def add_mover_scheme(self, mover_scheme):
23 if len(mover_scheme) != self.length:
27 for mvs
in mover_scheme:
30 mc.set_scoring_function(get_restraint_set(mvs[0].get_model()))
31 mc.set_return_best(
False)
35 mc.set_name(str(self.numbermc))
37 self.mover_scheme = mc_scheme
39 def add_montecarlo_temperatures(self, temps):
42 def add_montecarlo_steps(self, steps):
45 def add_score_scheme(self, score, weigth_scheme):
46 if len(weigth_scheme) != self.length:
49 self.score_scheme[score] = weigth_scheme
50 self.score_initial_weights[score] = score.get_weight()
55 def get_position(self, position):
56 movers = self.mover_scheme[position]
58 for score
in self.score_scheme:
59 score_tmp_dict[score] = self.score_scheme[score][position]
60 return movers, score_tmp_dict
62 def get_and_update(self):
63 mc, score_tmp_dict = self.get_position(self.position)
64 temp = self.temps[self.position]
66 steps = self.steps[self.position]
68 if self.position > self.length - 1: self.position = 0
69 return mc, score_tmp_dict, steps
73 def __init__(self, m, hier, MoverScheme):
74 IMP.core.MonteCarloMover.__init__(
77 "HybridMonteCarloMover")
89 self.ps.append(rb.get_particle())
90 for bead
in self.beads:
91 self.ps.append(bead.get_particle())
93 self.indexes = [p.get_index()
for p
in self.ps]
95 self.do_store_current_coordinates()
97 self.mover_scheme = MoverScheme
99 def do_store_current_coordinates(self):
101 self.refframes[rb] = rb.get_reference_frame()
102 for bead
in self.beads:
105 def do_propose(self):
107 self.rejects.append(0)
108 if len(self.rejects) > self.lenrejects:
111 self.rejects, float(sum(self.rejects)) / len(self.rejects)
113 self.do_store_current_coordinates()
115 rs = get_restraint_set(self.m)
118 for i
in range(self.mover_scheme.get_length()):
119 mc, score_tmp_dict, steps = self.mover_scheme.get_and_update()
121 mc.reset_statistics()
123 for score
in score_tmp_dict:
124 score.set_weight(score_tmp_dict[score])
129 mc.get_name(), float(mc.get_number_of_accepted_steps()) / mc.get_number_of_proposed_steps()
140 for rb
in self.refframes:
141 rb.set_reference_frame(self.refframes[rb])
142 for bead
in self.xyzs:
145 def do_get_inputs(self):
150 def __init__(self, m, hier, resolution=10, depth=4):
152 IMP.core.MonteCarloMover.__init__(
162 self.resolution = resolution
168 self.particle_blocks = []
169 for mol
in self.mols:
172 self.beadsd[mol] = beads
173 for rb
in self.rbsd[mol]:
174 self.ps.append(rb.get_particle())
175 for bead
in self.beadsd[mol]:
176 self.ps.append(bead.get_particle())
178 self.particle_blocks.append(ps)
180 self.do_store_current_coordinates()
182 def do_store_current_coordinates(self):
183 for mol
in self.mols:
184 for rb
in self.rbsd[mol]:
185 self.refframes[rb] = rb.get_reference_frame()
186 for bead
in self.beadsd[mol]:
189 def get_full_graph(self):
191 get the full graph of distances between every particle pair
194 pdist_array = np.array(
196 pdist_mat = scipy.spatial.distance.squareform(pdist_array)
197 pdist_mat[pdist_mat <= 10] = 1
198 pdist_mat[pdist_mat > 10] = 0
199 graph = nx.Graph(pdist_mat)
202 def get_list_of_interacting_molecules(self, succ_dict, pointer):
203 if pointer
in succ_dict:
204 paths = [path
for p
in succ_dict[pointer]
for path
in self.get_list_of_interacting_molecules(succ_dict, p)]
205 ret = [[pointer] + path
for path
in paths]
211 def get_minimum_spanning_tree(self):
213 return the minimum spanning tree
215 graph = self.get_full_graph()
216 graph = nx.minimum_spanning_tree(graph)
219 def get_sublists(self, lst, size):
220 result = [sublist
for sublist
in
221 (lst[x:x + size]
for x
in range(len(lst) - size + 1))]
224 def get_list_of_connected_mols(self, depth):
225 gr = self.get_minimum_spanning_tree()
227 if len(gr.edges()) > 0:
229 for node
in gr.nodes():
230 succ_dict = nx.dfs_successors(gr, node)
231 paths = [path
for p
in succ_dict[node]
for path
in self.get_list_of_interacting_molecules(succ_dict, p)]
232 ret = [[node] + path
for path
in paths]
234 for s
in range(2, min([depth + 1, len(path) + 1, len(self.mols)])):
235 subls = self.get_sublists(path, s)
236 for subl
in subls: all_groups.add(frozenset(subl))
243 def do_compute_pcas(self):
245 for i, ps
in enumerate(self.particle_blocks):
250 self.singletons[(mol,)] = pcan
253 all_groups = self.get_list_of_connected_mols(self.depth)
255 for group
in all_groups:
256 mols = [self.mols[n]
for n
in group]
260 self.singletons[tuple(mols)] = pcan
262 def do_get_pairs(self, temp=20):
263 sk = self.singletons.keys()
266 for s1, s2
in itertools.combinations(sk, 2):
268 self.singletons[s2].get_principal_values())
269 pairs.append(((s1, s2), math.exp(-rms / temp)))
273 def do_compute_transforms(self, pair):
280 arbs += self.rbsd[mol]
281 abeads += self.beadsd[mol]
282 apca = self.singletons[amols]
287 brbs += self.rbsd[mol]
288 bbeads += self.beadsd[mol]
289 bpca = self.singletons[bmols]
294 transbtoa = transatob.get_inverse()
296 return (transatob, transbtoa, arbs, abeads, brbs, bbeads)
298 def weighted_choice(self, choices):
299 total = sum(w
for c, w
in choices)
300 r = random.uniform(0, total)
306 assert False,
"Shouldn't get here"
308 def do_propose(self):
309 self.rejects.append(0)
310 if len(self.rejects) > self.lenrejects:
314 self.do_store_current_coordinates()
315 self.do_compute_pcas()
316 pairs = self.do_get_pairs()
317 pair = self.weighted_choice(pairs)
320 t = self.do_compute_transforms(pair)
323 (ta2b, tb2a) = (t[0], t[1])
328 moved_indexes.append(arb.get_particle_index())
331 moved_indexes.append(arb.get_particle_index())
342 for rb
in self.refframes:
343 rb.set_reference_frame(self.refframes[rb])
344 for bead
in self.xyzs:
347 def do_get_inputs(self):
IMP::algebra::Transformation3Ds PCAalign(const IMP::algebra::PrincipalComponentAnalysis &pca1, const IMP::algebra::PrincipalComponentAnalysis &pca2)
Return value of the MonteCarloMover::propose() function.
IMP::algebra::PrincipalComponentAnalysis NormalizePCA(const IMP::algebra::PrincipalComponentAnalysis &pca, const IMP::Particles &ps)
void transform(XYZ a, const algebra::Transformation3D &tr)
Apply a transformation to the particle.
A base class for classes which perturb particles.
A decorator for a particle with x,y,z coordinates.
PrincipalComponentAnalysisD< D > get_principal_components(const Vector< VectorD< D > > &ps)
Perform principal components analysis on a set of vectors.
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
double get_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the distance between two vectors.
Floats get_list_of_bipartite_minimum_sphere_distance(const ParticlesTemps &pss)
Select hierarchy particles identified by the biological name.
Applies a list of movers one at a time.