def _setup_normal_mover(self, particle, floatkey, stepsize): """setup NormalMover to move particle's floatkey attribute by a gaussian with standard deviation 'stepsize' Returns: mover instance. """ nm = IMP.core.NormalMover(self._m, particle.get_particle_index(), IMP.FloatKeys([floatkey]), stepsize) return nm
def get_nuisance_movers(self, nuisances, maxstep): mvs = [] for nuisance in nuisances: print(nuisance, maxstep) mvs.append( IMP.core.NormalMover([nuisance], IMP.FloatKeys([IMP.FloatKey("nuisance")]), maxstep)) return mvs
def get_movers(self): movers=[] mover_name="Nuisances_MembraneRestraint_Z_" + self.label particle=self.z_center maxstep=2.0 mv=IMP.core.NormalMover([particle], IMP.FloatKeys([IMP.FloatKey("nuisance")]),maxstep) mv.set_name(mover_name) movers.append(mv) return movers
def create_nuisance_mover(self, nuisance_p, step_size, name=None): """Create MC normal mover for nuisance particles. We will add an easier interface to add all of them from a PMI restraint @param nuisance_p The Nuisance particle (an ISD::Scale) @param step_size The maximum step size for Monte Carlo @param name The name of the mover, useful for better output reading. """ mv = IMP.core.NormalMover([nuisance_p], IMP.FloatKeys([IMP.FloatKey("nuisance")]), step_size) if name is not None: mv.set_name(name) self.nuisances.append(nuisance_p) self.movers.append(mv) return [mv]
def get_floppy_body_movers(self, fbs, maxtrans): mvs = [] for fb in fbs: # check is that is a rigid body member: if IMP.core.NonRigidMember.get_is_setup(fb): # if so force the particles to move anyway floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] for fk in floatkeys: fb.set_is_optimized(fk, True) mvs.append( IMP.core.BallMover(fb.get_model(), fb, IMP.FloatKeys(floatkeys), maxtrans)) else: # otherwise use the normal ball mover mvs.append(IMP.core.BallMover(fb.get_model(), fb, maxtrans)) return mvs
def create_non_rigid_members(self,spec,max_trans=1.0): hiers = get_hierarchies(spec) floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] idxs = set(self.rb.get_member_indexes()) for h in hiers: p = h.get_particle() if not p.get_index() in idxs: raise Exception("You tried to create nonrigid members from " "particles that aren't in the RigidBody!") self.rb.set_is_rigid_member(p.get_index(),False) for fk in floatkeys: p.set_is_optimized(fk,True) self.flexible_movers.append(IMP.core.BallMover([p], IMP.FloatKeys(floatkeys), max_trans))
def _setup_opt(self): model = IMP.kernel.Model() lopt = IMP.core.ConjugateGradients(model) opt = IMP.core.MonteCarloWithLocalOptimization(lopt, 1000) opt.set_score_threshold(.01) for value in (-3.0, -1.0, -3.0, -1.0): p = IMP.kernel.Particle(model) p.add_attribute(self.xkey, value, True) fk = IMP.FloatKeys([self.xkey]) mod = IMP.core.BallMover(model.get_particles(), fk, .25) opt.add_mover(mod) rsr = WoodsFunc(model) model.add_restraint(rsr) # print rsr.evaluate(None) # print model.evaluate(False) # model.show() # print "Restaint TO is "+str(rsr.thisown) # opt.show() return (model, opt)
def test_NormalMover_MC_fails(self): "Test nuisance scorestate with MonteCarlo mover" nuis = Nuisance.setup_particle(IMP.Particle(self.m), 50.0) lower = Nuisance.setup_particle(IMP.Particle(self.m), 10.0) upper = Nuisance.setup_particle(IMP.Particle(self.m), 90.0) nuis.set_lower(1.0) nuis.set_lower(lower) nuis.set_upper(upper) nuis.set_upper(80.0) nuis.set_nuisance_is_optimized(True) nmv = IMP.core.NormalMover([nuis], IMP.FloatKeys([IMP.FloatKey("nuisance")]), 10.0) mc = IMP.core.MonteCarlo(self.m) mc.set_return_best(False) mc.set_kt(1.0) mc.add_mover(nmv) for i in range(100): mc.optimize(10) self.assertTrue(nuis.get_nuisance() >= nuis.get_lower() and nuis.get_nuisance() <= nuis.get_upper())
def _setup_opt(self): model = IMP.Model() lopt = IMP.core.ConjugateGradients(model) opt = IMP.core.MonteCarloWithLocalOptimization(lopt, 1000) opt.set_score_threshold(.01) ps = [] for value in (-3.0, -1.0, -3.0, -1.0): p = IMP.Particle(model) p.add_attribute(self.xkey, value, True) ps.append(p) fk = IMP.FloatKeys([self.xkey]) mod = IMP.core.BallMover(ps, fk, .25) opt.add_mover(mod) rsr = WoodsFunc(model) # print rsr.evaluate(None) # print model.evaluate(False) # model.show() # print "Restaint TO is "+str(rsr.thisown) # opt.show() sf = IMP.core.RestraintsScoringFunction([rsr]) lopt.set_scoring_function(sf) opt.set_scoring_function(sf) return (model, opt)
def create_rigid_body(self, rigid_parts, nonrigid_parts=None, max_trans=4.0, max_rot=0.5, nonrigid_max_trans=4.0, resolution='all', name=None): """Create rigid body constraint and mover @param rigid_parts Can be one of the following inputs: IMP Hierarchy, PMI System/State/Molecule/TempResidue, a list/set (of list/set) of them or a RigidBody object. Must be uniform input, however. No mixing object types. @param nonrigid_parts Same input format as rigid_parts. Must be a subset of rigid_parts particles. @param max_trans Maximum rigid body translation @param max_rot Maximum rigid body rotation @param nonrigid_max_trans Maximum step for the nonrigid (bead) particles @param resolution Only used if you pass PMI objects. Probably you want 'all'. @param name Rigid body name (if None, use IMP default) @eturn (rb_movers,rb_object) @note If you want all resolutions, pass PMI objects because this function will get them all. Alternatively you can do your selection elsewhere and just pass hierarchies. """ rb_movers = [] # ADD CHECK: these particles are not already part of some RB or SRB # First, is this already a rigid body? if type(rigid_parts) is IMP.core.RigidBody: print("WARNING: Rigid Body Already Setup") rb = rigid_parts model = rb.get_model() if name is None: name = rb.get_name() hiers = [ IMP.atom.get_leaves(IMP.atom.Hierarchy(m.get_particle(i)))[0] for i in rb.get_member_particle_indexes() ] else: ### Otherwise, setup RB hiers = IMP.pmi.tools.input_adaptor(rigid_parts, resolution, flatten=True) if not hiers: print( "WARNING: No hierarchies were passed to create_rigid_body()" ) return [] # Need to do this before rigid body is set up so that we leave the # system in a consistent state if a sanity check fails nr_hiers = self._get_nonrigid_hiers(nonrigid_parts, hiers, resolution) if type(rigid_parts) is not IMP.core.RigidBody: model = hiers[0].get_model() #we need to use the following constructor because the IMP.core.create_rigid_body seems #to construct an arbitrary reference frame, which will be different for all copies. #therefore, symmetry won't work all the time com = IMP.atom.CenterOfMass.setup_particle(IMP.Particle(model), hiers) comcoor = IMP.core.XYZ(com).get_coordinates() tr = IMP.algebra.Transformation3D( IMP.algebra.get_identity_rotation_3d(), comcoor) rf = IMP.algebra.ReferenceFrame3D(tr) rbp = IMP.Particle(model) rb = IMP.core.RigidBody.setup_particle(rbp, rf) for h in hiers: rb.add_member(h.get_particle()) self.rigid_bodies.append(rb) rb.set_coordinates_are_optimized(True) rb_mover = IMP.core.RigidBodyMover(rb.get_model(), rb, max_trans, max_rot) if name is not None: rb.set_name(name) rb_mover.set_name(name) rb_movers.append(rb_mover) self.movers_particles_map[rb_mover] = [] self.movers_rb_map[rb_mover] = [rb] rb_mover.set_was_used(True) for h in hiers: self.movers_particles_map[rb_mover] += IMP.atom.get_leaves(h) ### setup nonrigid parts if nr_hiers: floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] for h in nr_hiers: self.flexible_beads.append(h) p = h.get_particle() rb.set_is_rigid_member(p.get_index(), False) for fk in floatkeys: p.set_is_optimized(fk, True) fbmv = IMP.core.BallMover(p.get_model(), p, IMP.FloatKeys(floatkeys), nonrigid_max_trans) self.fb_movers.append(fbmv) self.movers_particles_map[fbmv] = IMP.atom.get_leaves(h) self.movers_xyz_map[fbmv] = IMP.atom.get_leaves(h) fbmv.set_was_used(True) rb_movers.append(fbmv) self.movers += rb_movers # probably need to store more info self._rb2mov[rb] = rb_movers #dictionary relating rb to movers return rb_movers, rb
def test_complete(self): mdl = IMP.Model() root = IMP.atom.Hierarchy(IMP.Particle(mdl)) #need some data structure #rigidbody->particles rbs_ps = {} #floppy bodies fbs = [] #rigidbodies rbs = [] #particles->movers ps_mv = {} #movers mvs = [] #particles by clone pss = [] #setting up Hierarchy and RigidBodies with movers for mol in range(10): mol = IMP.atom.Hierarchy(IMP.Particle(mdl)) root.add_child(mol) ps = [] for c in [(0, 0, 0), (5, 0, 0), (0, 5, 0)]: p = IMP.Particle(mdl) dr = IMP.core.XYZR.setup_particle(p) dr.set_coordinates(c) dr.set_radius(1.0) IMP.atom.Mass.setup_particle(p, 1.0) h = IMP.atom.Hierarchy(p) mol.add_child(h) ps.append(p) rb = IMP.atom.create_rigid_body(IMP.atom.get_leaves(mol)) rb_mover = IMP.core.RigidBodyMover(rb.get_model(), rb, 1, 0.5) mvs.append(rb_mover) rbs_ps[rb] = ps rbs.append(rb) pss.append(ps) for p in ps: if p in ps_mv: ps_mv[p].append(rb_mover) else: ps_mv[p] = [rb_mover] #setup NonRigidMembers with movers floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] for rb in rbs_ps: for p in rbs_ps[rb][1:2]: rb.set_is_rigid_member(p.get_index(), False) for fk in floatkeys: p.set_is_optimized(fk, True) fbmv = IMP.core.BallMover(p.get_model(), p, IMP.FloatKeys(floatkeys), 1.0) mvs.append(fbmv) fbs.append(p) if p in ps_mv: ps_mv[p].append(fbmv) else: ps_mv[p] = [fbmv] # enforce rotational symmetry # while removing movers from the clones rotational_axis = IMP.algebra.Vector3D(-0.98477941, 0.15168411, -0.08486137) rotational_point = IMP.algebra.Vector3D(0, 0, 0) disabled_movers = set() for n, c in enumerate(pss[1:]): rotation_angle = 2.0 * pi / len(pss) * float(n + 1) rotation3D = IMP.algebra.get_rotation_about_axis( rotational_axis, rotation_angle) transformation3D = IMP.algebra.get_rotation_about_point( rotational_point, rotation3D) add_symmetry(c, pss[0], transformation3D, mdl) for p in c: for mv in ps_mv[p]: disabled_movers.add(mv) mdl.update() enabled_movers = [] for mv in mvs: if mv in disabled_movers: continue else: enabled_movers.append(mv) #create EV restraint ssps = IMP.core.SoftSpherePairScore(1.0) lsa = IMP.container.ListSingletonContainer(mdl) lsa.add(IMP.get_indexes(IMP.atom.get_leaves(root))) rbcpf = IMP.core.RigidClosePairsFinder() cpc = IMP.container.ClosePairContainer(lsa, 0.0, rbcpf, 10.0) evr = IMP.container.PairsRestraint(ssps, cpc) #setup Score, MC, etc sf = IMP.core.RestraintsScoringFunction([evr]) mc = IMP.core.MonteCarlo(mdl) mc.set_scoring_function(sf) sm = IMP.core.SerialMover(enabled_movers) mc.add_mover(sm) mc.set_return_best(False) mc.set_kt(1.0) rh = RMF.create_rmf_file("out.rmf") IMP.rmf.add_hierarchies(rh, [root]) IMP.rmf.save_frame(rh) #data structure to store transformation classes rb_rbt = {} rb_rbpt = {} for rb in rbs: rbt = RigidBodyTransformation(rb) rb_rbt[rb] = rbt if rb != rbs[0]: rbpt = RigidBodyPairTransformation(rbs[0], rb) rb_rbpt[rb] = rbpt #data structure to store all output rb_rbpt_values = {} rb_rbpt_values["trans"] = {} rb_rbpt_values["angle"] = {} rb_rbpt_values["axis_x"] = {} rb_rbpt_values["axis_y"] = {} rb_rbpt_values["axis_z"] = {} for i in range(10): mc.optimize(10) mdl.update() #first check that all translations and rotation magnitudes are identical trans_list = [] rot_list = [] for i, (rb, rbt) in enumerate(rb_rbt.items()): rbt.set_final() trans_list.append(rbt.get_translation()) rot_list.append(rbt.get_rotation()[0]) rbt.set_initial() for a, b in itertools.combinations(trans_list, 2): self.assertAlmostEqual(a, b, places=7) for a, b in itertools.combinations(rot_list, 2): self.assertAlmostEqual(a, b, places=7) #second store the pairwise transformations for i, (rb, rbpt) in enumerate(rb_rbpt.items()): trans = rbpt.get_translation() (angle, axis) = rbpt.get_rotation() for key in rb_rbpt_values: if rb not in rb_rbpt_values[key]: rb_rbpt_values[key][rb] = [] #if key=="trans": rb_rbpt_values[key][rb].append(trans) if key == "angle": rb_rbpt_values[key][rb].append(angle) if key == "axis_x": rb_rbpt_values[key][rb].append(abs(axis[0])) if key == "axis_y": rb_rbpt_values[key][rb].append(abs(axis[1])) if key == "axis_z": rb_rbpt_values[key][rb].append(abs(axis[2])) #third check the NonRigidMember internal coordinates: ic_xs = [] ic_ys = [] ic_zs = [] for fb in fbs: ic = IMP.core.NonRigidMember(fb).get_internal_coordinates() ic_xs.append(ic[0]) ic_ys.append(ic[1]) ic_zs.append(ic[2]) for a, b in itertools.combinations(ic_xs, 2): self.assertAlmostEqual(a, b, places=7) for a, b in itertools.combinations(ic_ys, 2): self.assertAlmostEqual(a, b, places=7) for a, b in itertools.combinations(ic_zs, 2): self.assertAlmostEqual(a, b, places=7) IMP.rmf.save_frame(rh) rh.flush() # last check the consistency of pairwise rotations along the trajectory for key in rb_rbpt_values: for rb in rb_rbpt_values[key]: for a, b in itertools.combinations(rb_rbpt_values[key][rb], 2): self.assertAlmostEqual(a, b, places=7) del rh
mol.add_child(h) ps.append(p) rb = IMP.atom.create_rigid_body(IMP.atom.get_leaves(mol)) rb_mover = IMP.core.RigidBodyMover(rb.get_model(), rb, 1, 0.5) mvs.append(rb_mover) rbs_ps[rb] = ps #setup 2 floppy bodies floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] for rb in rbs_ps: for p in rbs_ps[rb][0:2]: rb.set_is_rigid_member(p.get_index(), False) for fk in floatkeys: p.set_is_optimized(fk, True) fbmv = IMP.core.BallMover(p.get_model(), p, IMP.FloatKeys(floatkeys), 1.0) mvs.append(fbmv) class _InternalCoordinatesConstraint(IMP.SingletonModifier): """A class that updates internal coordinates between rigid bodies """ def __init__(self, m): IMP.SingletonModifier.__init__(self, "InternalCoordinatesModifier%1%") def apply_index(self, m, pi): ref_p = IMP.core.Reference(m, pi).get_reference_particle() ref_index = ref_p.get_index() nrm1 = IMP.core.NonRigidMember(m, ref_index) nrm2 = IMP.core.NonRigidMember(m, pi)
def create_rigid_body(self, rigid_parts, nonrigid_parts=None, max_trans=1.0, max_rot=0.1, nonrigid_max_trans=0.1, resolution='all', name=None): """Create rigid body constraint and mover @param rigid_parts Can be one of the following inputs: IMP Hierarchy, PMI System/State/Molecule/TempResidue, a list/set (of list/set) of them or a RigidBody object. Must be uniform input, however. No mixing object types. @param nonrigid_parts Same input format as rigid_parts. Must be a subset of rigid_parts particles. @param max_trans Maximum rigid body translation @param max_rot Maximum rigid body rotation @param nonrigid_max_trans Maximum step for the nonrigid (bead) particles @param resolution Only used if you pass PMI objects. Probably you want 'all'. @param name Rigid body name (if None, use IMP default) \note If you want all resolutions, pass PMI objects because this function will get them all. Alternatively you can do your selection elsewhere and just pass hierarchies. Returns tuple (rb_movers,rb_object) """ rb_movers = [] # ADD CHECK: these particles are not already part of some RB or SRB # First, is this already a rigid body? if type(rigid_parts) is IMP.core.RigidBody: rb = rigid_parts if name is not None: name = rb.get_name() else: ### Otherwise, setup RB hiers = IMP.pmi.tools.input_adaptor(rigid_parts, resolution, flatten=True) if not hiers: print( "WARNING: No hierarchies were passed to create_rigid_body()" ) return [] rb = IMP.atom.create_rigid_body(hiers) self.rigid_bodies.append(rb) rb.set_coordinates_are_optimized(True) rb_mover = IMP.core.RigidBodyMover(rb, max_trans, max_rot) if name is not None: rb.set_name(name) rb_mover.set_name(name) rb_movers.append(rb_mover) ### setup nonrigid parts if nonrigid_parts: nr_hiers = IMP.pmi.tools.input_adaptor(nonrigid_parts, resolution, flatten=True) if nr_hiers: floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] rb_idxs = set(rb.get_member_indexes()) for h in nr_hiers: self.flexible_beads.append(h) p = h.get_particle() if not p.get_index() in rb_idxs: raise Exception( "You tried to create nonrigid members from " "particles that aren't in the RigidBody!") rb.set_is_rigid_member(p.get_index(), False) for fk in floatkeys: p.set_is_optimized(fk, True) rb_movers.append( IMP.core.BallMover([p], IMP.FloatKeys(floatkeys), nonrigid_max_trans)) self.movers += rb_movers # probably need to store more info return rb_movers, rb