def test__rigid_bodies_drms(self): """ Test drms measure taking into account rigid bodies""" m = IMP.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) hchains1 = atom.get_by_type(prot1, atom.CHAIN_TYPE) hchains2 = atom.get_by_type(prot2, atom.CHAIN_TYPE) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) x = 0 ranges = [] for h in hchains1: ls1 = (atom.get_leaves(h)) y = x + len(ls1) ranges.append((x, y)) x = y drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(rb_drms, 0) self.assertAlmostEqual(drms, rb_drms, delta=1e-3, msg="rb_drms != drms") # Same thing after transformation of each of the chains for h in hchains2: R = alg.get_random_rotation_3d() v = alg.get_random_vector_in(alg.get_unit_bounding_box_3d()) T = alg.Transformation3D(R, v) ls = atom.get_leaves(h) for l in ls: core.transform(l.get_as_xyz(), T) drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(drms, rb_drms, delta=0.3, msg="rb_drms != drms")
def test_sampling_schema(self): """ Test """ subunits = ["subunitA", "subunitB", "subunitC", "subunitD"] anchored = [False, False, False, False] fixed = [False, False, False, False] n_transformations = 50 db = database.Database2() fn = 'temp.db' db.create(fn, overwrite=True) db.connect(fn) transformations = [] table_name = "results" db.create_table(table_name, ["reference_frames"], [str]) for i in range(n_transformations): Ts = [] for j in range(len(subunits)): center = alg.Vector3D(0, 0, 0) T = alg.Transformation3D( alg.get_random_rotation_3d(), alg.get_random_vector_in(alg.Sphere3D(center, 34))) Ts.append(T) transformations.append(Ts) data = [] for Ts in transformations: text = [io.Transformation3DToText(T).get_text() for T in Ts] text = "/".join(text) data.append([ text, ]) db.store_data(table_name, data) db.close() sch = sampling.SamplingSchema(4, fixed, anchored) sch.read_from_database(fn) for i in range(len(transformations)): for j in range(len(subunits)): T = transformations[i][j] t = T.get_translation() q = T.get_rotation().get_quaternion() pos = sch.transformations[j][i].get_translation() ori = sch.transformations[j][i].get_rotation().get_quaternion() for k in range(3): self.assertAlmostEqual(pos[k], t[k]) for k in range(4): self.assertAlmostEqual(q[k], ori[k]) os.remove(fn)
def apply_random_transform(rb, max_trans=100): """ Apply a random transformation to the rigid body and change the reference frame """ bb = alg.BoundingBox3D(alg.Vector3D(-max_trans, -max_trans, -max_trans), alg.Vector3D(max_trans, max_trans, max_trans)) Trand = alg.Transformation3D(alg.get_random_rotation_3d(), alg.get_random_vector_in(bb)) ref = rb.get_reference_frame() Tr = ref.get_transformation_to() T = alg.compose(Trand, Tr) rb.set_reference_frame(alg.ReferenceFrame3D(T))
def test_sampling_schema(self): """ Test """ subunits = [ "subunitA", "subunitB", "subunitC", "subunitD"] anchored = [False, False, False, False] fixed = [False, False, False, False] n_transformations = 50 db = Database.Database2() fn = 'temp.db' db.create(fn, overwrite=True) db.connect(fn) transformations = [] table_name = "results" db.create_table(table_name, ["reference_frames"], [str]) for i in range(n_transformations): Ts = [] for j in range(len(subunits)): center = alg.Vector3D(0, 0, 0) T = alg.Transformation3D(alg.get_random_rotation_3d(), alg.get_random_vector_in(alg.Sphere3D(center,34))) Ts.append(T) transformations.append(Ts) data = [] for Ts in transformations: text = [io.Transformation3DToText(T).get_text() for T in Ts] text = "/".join(text) data.append([text,]) db.store_data(table_name,data) db.close() sch = sampling.SamplingSchema(4, fixed, anchored) sch.read_from_database(fn) for i in range(len(transformations)): for j in range(len(subunits)): T = transformations[i][j] t = T.get_translation() q = T.get_rotation().get_quaternion() pos = sch.transformations[j][i].get_translation() ori = sch.transformations[j][i].get_rotation().get_quaternion() for k in range(3): self.assertAlmostEqual(pos[k], t[k]) for k in range(4): self.assertAlmostEqual(q[k], ori[k]) os.remove(fn)
def move_one_xlink(self): """ Put the residues in a random distance between 0 and the maximum cross-linkin distance """ r1, r2, distance = self.xlinks[0] center = self.get_residue_coordinates(self.h_receptor, r1) sph = alg.Sphere3D(center, distance) v = alg.get_random_vector_in(sph) ref = self.rb_ligand.get_reference_frame() coords = ref.get_transformation_to().get_translation() R = ref.get_transformation_to().get_rotation() lig = self.get_residue_coordinates(self.h_ligand, r2) log.debug("Ligand residue before moving %s", lig) displacement = v - lig T = alg.Transformation3D(R, coords + displacement) self.rb_ligand.set_reference_frame(alg.ReferenceFrame3D(T)) log.debug("ligand after moving %s", self.get_residue_coordinates(self.h_ligand,r2))
def move_one_xlink(self): """ Put the residues in a random distance between 0 and the maximum cross-linkin distance """ xl = self.xlinks_list[0] center = self.get_residue_coordinates(self.h_receptor, xl.first_chain, xl.first_residue) sph = alg.Sphere3D(center, xl.distance) v = alg.get_random_vector_in(sph) ref = self.rb_ligand.get_reference_frame() coords = ref.get_transformation_to().get_translation() R = ref.get_transformation_to().get_rotation() lig = self.get_residue_coordinates(self.h_ligand, xl.second_chain, xl.second_residue) log.debug("Ligand residue before moving %s", lig) displacement = v - lig T = alg.Transformation3D(R, coords + displacement) self.rb_ligand.set_reference_frame(alg.ReferenceFrame3D(T)) new_coords = self.get_residue_coordinates(self.h_ligand, xl.second_chain, xl.second_residue) log.debug("ligand after moving %s", new_coords)
def test__rigid_bodies_drms(self): """ Test drms measure taking into account rigid bodies""" m = IMP.kernel.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) hchains1 = atom.get_by_type(prot1, atom.CHAIN_TYPE) hchains2 = atom.get_by_type(prot2, atom.CHAIN_TYPE) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) x = 0 ranges = [] for h in hchains1: ls1 = (atom.get_leaves(h)) y = x + len(ls1) ranges.append((x, y)) x = y drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(rb_drms, 0) self.assertAlmostEqual( drms, rb_drms, delta=1e-3, msg="rb_drms != drms") # Same thing after transformation of each of the chains for h in hchains2: R = alg.get_random_rotation_3d() v = alg.get_random_vector_in(alg.get_unit_bounding_box_3d()) T = alg.Transformation3D(R, v) ls = atom.get_leaves(h) for l in ls: core.transform(l.get_as_xyz(), T) drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(drms, rb_drms, delta=0.3, msg="rb_drms != drms")
atoms=core.get_leaves(c) rbd=core.RigidBody.setup_particle(c,atoms) rigid_bodies.append(rbd) print "chain has",rbd.get_number_of_members(), \ "atoms","coordinates: ",rbd.get_coordinates() native_chain_centers.append(rbd.get_coordinates()) bb=alg.BoundingBox3D(alg.Vector3D(-25, -40,-60), alg.Vector3D( 25, 40, 60)) # rotate and translate the chains for rbd in rigid_bodies: # rotation rotation= alg.get_random_rotation_3d() transformation1=alg.get_rotation_about_point(rbd.get_coordinates(),rotation) # translation transformation2=alg.Transformation3D(alg.get_random_vector_in(bb)) # Apply final_transformation = alg.compose(transformation1,transformation2) core.transform(rbd,final_transformation) print "Writing transformed assembly" atom.write_pdb (prot,"1z5s-transformed.pdb") # set distance restraints measusring some distances between rigid bodies # for the solution. d01 = alg.get_distance(native_chain_centers[0],native_chain_centers[1]) r01 = core.DistanceRestraint(core.Harmonic(d01,1),chains[0],chains[1]) r01.set_name("distance 0-1") d12 = alg.get_distance(native_chain_centers[1],native_chain_centers[2]) r12 = core.DistanceRestraint(core.Harmonic(d12,1),chains[1],chains[2]) r12.set_name("distance 1-2") d23 = alg.get_distance(native_chain_centers[2],native_chain_centers[3])
def do_packing(conf, op, pymol_file_name, verbose=True): m = I.Model() box_size = [op['box']['x'], op['box']['y'], op['box']['z']] corner0 = IAL.Vector3D(0, 0, 0) corner1 = IAL.Vector3D(box_size[0], box_size[1], box_size[2]) box = IAL.BoundingBox3D(corner0, corner1) expand_n = 1 corner0_expand = IAL.Vector3D(0, 0, 0) corner1_expand = IAL.Vector3D(box_size[0] * expand_n, box_size[1] * expand_n, box_size[2] * expand_n) box_expand = IAL.BoundingBox3D(corner0_expand, corner1_expand) ps = [] for i, b in enumerate(conf): p = I.Particle(m) v = IAL.get_random_vector_in(box_expand) # lsn: radom posotion s = IAL.Sphere3D(v, b['r']) # lsn: position, radius pt = IC.XYZR.setup_particle(p, s) # lsn: particle, parameters pt = IAT.Mass.setup_particle(p, b['mass']) p.add_attribute( I.FloatKey('vx'), 0.0 ) # initial velocity attribute is needed for molecular dynamics simulation p.add_attribute( I.FloatKey('vy'), 0.0 ) # initial velocity attribute is needed for molecular dynamics simulation p.add_attribute(I.FloatKey('vz'), 0.0) ps.append(p) '''for i, p in enumerate(ps): xv = m.get_attribute(I.FloatKey('vx'), p) yv = m.get_attribute(I.FloatKey('vy'), p) zv = m.get_attribute(I.FloatKey('vz'), p) if N.isnan(xv): m.set_attribute(I.FloatKey('vx'), p, 0.0) if N.isnan(yv): m.set_attribute(I.FloatKey('vy'), p, 0.0) if N.isnan(zv): m.set_attribute(I.FloatKey('vz'), p, 0.0) ps[i] = p''' #TODO: Group spheres together for multiBall packing lsc_ps = ICO.ListSingletonContainer(m, ps) sscell = IC.BoundingBox3DSingletonScore(IC.HarmonicUpperBound(0, 1), box) #TODO: New scoring method """sf = IC.RestraintsScoringFunction([ICO.SingletonsRestraint(sscell, lsc_ps), IC.ExcludedVolumeRestraint(lsc_ps)])""" temprature = op['temprature'] o = IAT.MolecularDynamics(m) o.set_particles(ps) o.set_scoring_function(sf) o.assign_velocities(temprature) s = N.inf recent_scores = [] while (s > op['min score']) and (temprature > 0): o.assign_velocities(temprature) md = IAT.VelocityScalingOptimizerState(m, ps, temprature) o.add_optimizer_state(md) s = o.optimize(op['step']) cx = N.array(get_center_coordinates(ps)) is_in = N.array([True] * len(ps)) for dim_i in range(3): is_in[cx[:, dim_i].flatten() < 0] = False is_in[cx[:, dim_i].flatten() >= box_size[dim_i]] = False print( '\r', 'score:', s, ' ', 'temprature:', temprature, ' ', 'center inside box particles: ', is_in.sum(), ' ', ) if True: recent_scores.append(s) while len(recent_scores) > op['recent_scores number']: recent_scores.pop(0) if len(recent_scores) == op['recent_scores number']: x = N.array(range(len(recent_scores))) y = N.array(recent_scores) recent_scores_slope, intercept, r_value, p_value, std_err = stats.linregress( x, y) if N.abs(recent_scores_slope) < op['recent_scores slope min']: temprature *= 1 - op['temprature decrease'] recent_scores = [] sys.stdout.flush() cx = get_center_coordinates(ps) conf = copy.deepcopy(conf) for i, b in enumerate(conf): b['x'] = cx[i] return { 'conf': conf, 'score': s, 'temprature': temprature, 'inside_box_num': is_in.sum() }