def test_round_trip(self): """Test RMF write/read representations""" def get_color(p): c = IMP.display.Colored(p).get_color() return [ int(c.get_red() * 255), int(c.get_green() * 255), int(c.get_blue() * 255) ] base_res = 0 bead_res = 1 model = IMP.Model() s = IMP.pmi.topology.System(model) st1 = s.create_state() seqs = IMP.pmi.topology.Sequences( self.get_input_file_name('seqs.fasta')) m1 = st1.create_molecule("Prot1", sequence=seqs["Protein_1"]) atomic_res = m1.add_structure(self.get_input_file_name('prot.pdb'), chain_id='A', res_range=(55, 63), offset=-54) non_atomic_res = m1.get_non_atomic_residues() m1.add_representation(atomic_res, resolutions=[base_res, bead_res], color='salmon') m1.add_representation(non_atomic_res, resolutions=[bead_res], color=(0.4, 0.3, 0.2)) s.build() orig_hier = s.get_hierarchy() fname = self.get_tmp_file_name('test_round_trip.rmf3') rh = RMF.create_rmf_file(fname) IMP.rmf.add_hierarchy(rh, orig_hier) IMP.rmf.save_frame(rh) del rh rh2 = RMF.open_rmf_file_read_only(fname) h2 = IMP.rmf.create_hierarchies(rh2, model)[0] IMP.rmf.load_frame(rh2, 0) self.assertEqual(len(IMP.atom.get_leaves(orig_hier)), len(IMP.atom.get_leaves(h2))) # check all coordinates and colors selA0 = IMP.atom.Selection( orig_hier, resolution=base_res).get_selected_particles() coordsA0 = [ list(map(float, IMP.core.XYZ(p).get_coordinates())) for p in selA0 ] colorsA0 = [get_color(p) for p in selA0] selB0 = IMP.atom.Selection( h2, resolution=base_res).get_selected_particles() coordsB0 = [ list(map(float, IMP.core.XYZ(p).get_coordinates())) for p in selB0 ] colorsB0 = [get_color(p) for p in selB0] self.assertEqual(coordsA0, coordsB0) self.assertEqual(colorsA0, colorsB0) selA1 = IMP.atom.Selection( orig_hier, resolution=bead_res).get_selected_particles() coordsA1 = [ list(map(float, IMP.core.XYZ(p).get_coordinates())) for p in selA1 ] selB1 = IMP.atom.Selection( h2, resolution=bead_res).get_selected_particles() coordsB1 = [ list(map(float, IMP.core.XYZ(p).get_coordinates())) for p in selB1 ] self.assertEqual(coordsA1, coordsB1)
def test_rigid_body_with_densities(self): """Test still works when you add densities""" try: import sklearn except ImportError: self.skipTest("no sklearn package") mdl = IMP.Model() s = IMP.pmi.topology.System(mdl) st1 = s.create_state() seqs = IMP.pmi.topology.Sequences( self.get_input_file_name('seqs.fasta')) m1 = st1.create_molecule("Prot1", sequence=seqs["Protein_1"]) atomic_res = m1.add_structure(self.get_input_file_name('prot.pdb'), chain_id='A', res_range=(55, 63), offset=-54) m1.add_representation(atomic_res, resolutions=[1, 10], density_prefix='tmpgmm', density_residues_per_component=5) m1.add_representation(m1.get_non_atomic_residues(), resolutions=[1], setup_particles_as_densities=True) hier = s.build() na = 0 #57 na1 = 7 na10 = 2 naD = 2 nn1 = 3 dof = IMP.pmi.dof.DegreesOfFreedom(mdl) mvs, rb = dof.create_rigid_body( m1, nonrigid_parts=m1.get_non_atomic_residues()) self.assertEqual(len(mvs), 4) all_members = rb.get_member_indexes() rigid_members = rb.get_rigid_members() num_nonrigid = len(all_members) - len(rigid_members) selD = IMP.atom.Selection(st1.get_hierarchy(), representation_type=IMP.atom.DENSITIES) selA = IMP.atom.Selection(st1.get_hierarchy(), representation_type=IMP.atom.BALLS, resolution=IMP.atom.ALL_RESOLUTIONS) psD = selD.get_selected_particles() psA = selA.get_selected_particles() self.assertEqual(len(rigid_members), na + na1 + na10 + naD) self.assertEqual(num_nonrigid, nn1) IMP.atom.show_with_representations(hier) itest = IMP.pmi.tools.input_adaptor(m1, pmi_resolution='all', flatten=True) itest2 = IMP.pmi.tools.input_adaptor(m1.get_non_atomic_residues(), pmi_resolution='all', flatten=True) self.assertEqual(len(itest), na + na1 + na10 + naD + nn1) self.assertEqual(len(itest2), nn1) orig_coords = [IMP.core.XYZ(p).get_coordinates() for p in psD + psA] trans = IMP.algebra.get_random_local_transformation( IMP.algebra.Vector3D(0, 0, 0)) IMP.core.transform(rb, trans) new_coords = [IMP.core.XYZ(p).get_coordinates() for p in psD + psA] for c1, c2 in zip(orig_coords, new_coords): c1T = trans * c1 print(c1T, c2) self.assertAlmostEqual(IMP.algebra.get_distance(c1T, c2), 0.0) os.unlink('tmpgmm.mrc') os.unlink('tmpgmm.txt')
def test_particles(self): """Check particle methods""" m = IMP.Model("particle methods") IMP.set_log_level(IMP.VERBOSE) p = IMP.Particle(m) self.assertEqual(len(m.get_particle_indexes()), 1)
def main(): # sample_indexes = [ int(sys.argv[1]) ] time_start = time.time() print "-- create the universe" m = IMP.Model() m.set_log_level(IMP.SILENT) # m.set_log_level(IMP.TERSE) # m.set_log_level(IMP.VERBOSE) m.set_maximum_score(model_max_score) tfiihInfos = build_subunits_info(m) bbEM = tfiihInfos.emSpheres.compute_bbox() print "BBOX EM:", bbEM v = bbEM.get_corner(1) - bbEM.get_corner(0) bb = IMP.algebra.BoundingBox3D( bbEM.get_corner(0) - v / 2., bbEM.get_corner(1) + v / 2.) HGM.helpers.mute_all_restraints(m) times = HGM.times.Times() # mcs = HGM.representation.MyConfigurationSet(tfiihInfos) print "-- generate sample" loop_i = 1 for i in sample_indexes: time_loop_start = time.time() # generate_sample_md(i,tfiihInfos,mcs,bb) # generate_sample_mymccg(i,tfiihInfos,mcs,bb) print "MCCG SAMPLING ", "-" * 10 mcs = generate_sample_mccg(tfiihInfos, bb) # mc = HGM.representation.MyConfigurationSet(tfiihInfos) # mc.save_current_config() print "MCCG REFINING ", "-" * 10 mcsr = refine_sample_mccg(tfiihInfos, mcs) # mcsr = refine_sample_mccg(tfiihInfos,mc) configs = [(800, 100, 2), (600, 200, 1.5), (400, 300, 1.5), (200, 500, 1)] # configs = [ (800,100,2),(600,50,1.5),(400,100,1.5),(200,500,1)] # mc = HGM.representation.MyConfigurationSet(tfiihInfos) # mc.save_current_config() print "MCCG SUBUNITS REFINING ", "-" * 10 mcsr2 = refine_sample_mccg_per_subunits(tfiihInfos, mcsr, configs) # mcsr2 = refine_sample_mccg_per_subunits(tfiihInfos,mc,configs) print "MCCG SUBUNITS REREFINING ", "-" * 10 # mc = HGM.representation.MyConfigurationSet(tfiihInfos) # mc.save_current_config() configs = [(400, 200, 1.5), (300, 400, 1), (100, 600, .5)] # mcsr3 = refine_sample_mccg_per_subunits(tfiihInfos,mc,configs) mcsr3 = refine_sample_mccg_per_subunits(tfiihInfos, mcsr2, configs) # mcs.read_configurationSet(mcsr) # mcs.read_configurationSet(mcsr2) # mcs.read_configurationSet(mcsr3) print "SAVE ", "-" * 10 print " ", mcs.get_number_of_configurations(), "configurations" save_sample(mcsr3, i) # save_sample(mcsr,i) time_loop_stop = time.time() elapse_time = int(time_loop_stop - time_loop_start) times.set_sample_time(i, elapse_time) print "{0:d}({1:d}s)..".format(i, elapse_time), if loop_i % 40 == 0: print "" sys.stdout.flush() loop_i += 1 mcs.delete_all_configs() time_stop = time.time() print "full sample generated in {0:d}s".format(int(time_stop - time_start))
def test_deriv(self): """Test calculated derivatives for a distorted model's map""" if modeller is None: self.skipTest("modeller module unavailable") modeller.log.level = (0, 0, 0, 0, 1) self.env = modeller.environ() self.env.edat.dynamic_sphere = False self.env.libs.topology.read(file='$(LIB)/top_heav.lib') self.env.libs.parameters.read(file='$(LIB)/par.lib') # init IMP model ( the environment) self.imp_model = IMP.Model() self.particles = [] # - create a set of three particles in imp for i in range(3): self.particles.append(IMP.Particle(self.imp_model)) # Load the same model into Modeller self.modeller_model = copy_to_modeller(self.env, self.particles) # - add the particles attributes rad = 1.0 wei = 1.0 wei_key = IMP.FloatKey("weight") prot_key = IMP.IntKey("protein") id_key = IMP.IntKey("id") for i, p_data in enumerate([[9.0, 9.0, 9.0, rad, wei, 1], [12.0, 3.0, 3.0, rad, wei, 1], [3.0, 12.0, 12.0, rad, wei, 1]]): p = self.particles[i] center = IMP.algebra.Vector3D(*p_data[0:3]) sphere = IMP.algebra.Sphere3D(center, p_data[3]) IMP.core.XYZR.setup_particle(p, sphere) p.add_attribute(wei_key, p_data[4]) p.add_attribute(prot_key, p_data[5]) p.add_attribute(id_key, i) self.atmsel = modeller.selection(self.modeller_model) print("initialization done ...") resolution = 3. voxel_size = 1. model_map = IMP.em.SampledDensityMap(self.particles, resolution, voxel_size, wei_key) erw = IMP.em.EMReaderWriter() xorigin = model_map.get_header().get_xorigin() yorigin = model_map.get_header().get_yorigin() zorigin = model_map.get_header().get_zorigin() print(("x= " + str(xorigin) + " y=" + str(yorigin) + " z=" + str(zorigin))) with IMP.test.temporary_directory() as tempdir: mapfile = os.path.join(tempdir, 'xxx.em') IMP.em.write_map(model_map, mapfile, erw) # EM restraint em_map = IMP.em.read_map(mapfile, erw) em_map.get_header_writable().set_xorigin(xorigin) em_map.get_header_writable().set_yorigin(yorigin) em_map.get_header_writable().set_zorigin(zorigin) em_map.get_header_writable().compute_xyz_top() em_map.get_header_writable().set_resolution(resolution) print("rms_calc", em_map.get_rms_calculated()) em_map.calcRMS() print("rms_calc", em_map.get_rms_calculated()) ind_emrsr = [] ind_emrsr.append( IMP.em.FitRestraint(self.particles, em_map, [0., 0.], wei_key, 1.0)) sf = IMP.core.RestraintsScoringFunction(ind_emrsr) # add IMP Restraints into the modeller scoring function t = self.modeller_model.env.edat.energy_terms t.append(IMP.modeller.IMPRestraints(self.particles, sf)) print(("EM-score score: " + str(self.atmsel.energy()))) self.atmsel.randomize_xyz(1.0) nviol = self.atmsel.debug_function(debug_function_cutoff=(.010, 0.010, 0.01), detailed_debugging=True) self.assertLess(nviol, 1, "at least one partial derivative is wrong!") print(" derivs done ...")
def test_restraint_copy_ambiguity(self): """Test restraint works for systems with configuration ambiguity in PMI2""" # setup system m = IMP.Model() s = IMP.pmi.topology.System(m) st = s.create_state() protA = st.create_molecule("ProtA",sequence='A'*30,chain_id='A') protA.add_representation(protA[0:10],resolutions=[1],bead_default_coord=[0,0,0]) protA.add_representation(protA[10:20],resolutions=[1],bead_default_coord=[10,0,0]) protA.add_representation(protA[20:30],resolutions=[1],bead_default_coord=[20,0,0]) protA2 = protA.create_clone('C') protB = st.create_molecule("ProtB",sequence='A'*30,chain_id='B') protB.add_representation(protB[0:10],resolutions=[1],bead_default_coord=[0,0,0]) protB.add_representation(protB[10:20],resolutions=[1],bead_default_coord=[10,0,0]) protB.add_representation(protB[20:30],resolutions=[1],bead_default_coord=[20,0,0]) protB2 = protB.create_clone('D') hier = s.build() dof = IMP.pmi.dof.DegreesOfFreedom(m) dof.create_flexible_beads([protA,protA2,protB,protB2]) xlbeads,cldb = self.setup_crosslinks_beads(root_hier=hier,mode="single_category") # check enough clones were created self.assertEqual(len(cldb.data_base['1']),8) self.assertEqual(len(cldb.data_base['2']),4) self.assertEqual(len(cldb.data_base['3']),4) self.assertEqual(len(cldb.data_base['4']),4) # check score for j in range(100): IMP.pmi.tools.shuffle_configuration(hier,max_translation=10) cross_link_dict={} for xl in xlbeads.xl_list: p0 = xl["Particle1"] p1 = xl["Particle2"] prob = xl["Restraint"].get_probability() resid1 = xl[cldb.residue1_key] chain1 = xl[cldb.protein1_key] resid2 = xl[cldb.residue2_key] chain2 = xl[cldb.protein2_key] xlid=xl[cldb.unique_id_key] d0 = IMP.core.XYZ(p0) d1 = IMP.core.XYZ(p1) sig1 = xl["Particle_sigma1"] sig2 = xl["Particle_sigma2"] psi = xl["Particle_psi"] if xlid not in cross_link_dict: cross_link_dict[xlid]=([d0],[d1],[sig1],[sig2],[psi],prob) else: cross_link_dict[xlid][0].append(d0) cross_link_dict[xlid][1].append(d1) cross_link_dict[xlid][2].append(sig1) cross_link_dict[xlid][3].append(sig2) cross_link_dict[xlid][4].append(psi) for xlid in cross_link_dict: test_prob=get_probability(cross_link_dict[xlid][0], cross_link_dict[xlid][1], cross_link_dict[xlid][2], cross_link_dict[xlid][3], cross_link_dict[xlid][4],21.0,0.01) prob=cross_link_dict[xlid][5] self.assertAlmostEqual(test_prob,prob, delta=0.0001) for output in ['excluded.None.xl.db', 'included.None.xl.db', 'missing.None.xl.db']: os.unlink(output)
def test_read_static_restraints(self): """Check loading of Modeller static restraints""" e = self.get_modeller_environ() modmodel = model(e) modmodel.build_sequence('GGCC') m = IMP.Model() loader = IMP.modeller.ModelLoader(modmodel) protein = loader.load_atoms(m) at = modmodel.atoms restraints = [] # Typical distance restraints or stereochemical bonds: r = forms.gaussian(feature=features.distance(at[0], at[1]), mean=1.54, stdev=0.1, group=physical.xy_distance) restraints.append(r) r = forms.lower_bound(feature=features.distance(at[0], at[1]), mean=10.0, stdev=0.1, group=physical.xy_distance) restraints.append(r) r = forms.upper_bound(feature=features.distance(at[0], at[1]), mean=10.0, stdev=0.1, group=physical.xy_distance) restraints.append(r) # Typical stereochemical angle restraint: r = forms.gaussian(feature=features.angle(at[0], at[1], at[2]), mean=1.92, stdev=0.07, group=physical.xy_distance) restraints.append(r) # Typical stereochemical improper dihedral restraint: r = forms.gaussian(feature=features.dihedral(at[0], at[1], at[2], at[3]), mean=3.14, stdev=0.1, group=physical.xy_distance) restraints.append(r) # Typical stereochemical dihedral restraint: r = forms.cosine(feature=features.dihedral(at[0], at[1], at[2], at[3]), group=physical.xy_distance, phase=0.0, force=2.5, period=2) restraints.append(r) # Typical splined restraint: r = forms.spline(feature=features.distance(at[0], at[1]), open=True, low=1.0, high=5.0, delta=1.0, group=physical.xy_distance, lowderiv=0.0, highderiv=0.0, values=[100.0, 200.0, 300.0, 200.0, 100.0]) restraints.append(r) # Test forms.factor r = forms.factor(feature=features.angle(at[0], at[1], at[2]), factor=100.0, group=physical.xy_distance) restraints.append(r) # Test periodic splined restraint: r = forms.spline(feature=features.dihedral(at[0], at[1], at[2], at[3]), open=False, low=0.0, high=6.2832, delta=1.2566, group=physical.xy_distance, lowderiv=0.0, highderiv=0.0, values=[100.0, 200.0, 300.0, 400.0, 300.0]) restraints.append(r) # Test multiple binormal restraint r = forms.multi_binormal(features=(features.dihedral(*at[0:4]), features.dihedral(*at[4:8])), group=physical.xy_distance, weights=[0.2, 0.8, 0.3], means=[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]], stdevs=[[0.1, 0.2], [0.3, 0.4], [0.1, 0.3]], correls=[0.3, 0.6, 0.9]) restraints.append(r) for r in restraints: modmodel.restraints.clear() modmodel.restraints.add(r) rset = IMP.RestraintSet(m, 1.0, "RS") for rsr in loader.load_static_restraints(): rset.add_restraint(rsr) sf = IMP.core.RestraintsScoringFunction([rset]) assertSimilarModellerIMPScores(self, sf, modmodel, protein) rset.set_weight(0)
def test_score_multiple_restraints(self): """Intensive random test, it tests manifold ambiguity, sameparticle, particle positions totality of the score, individual scores, get_log_prob=True, multiple radii, multiple sigma, multiple psi""" import random m = IMP.Model() bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(30, 30, 30)) restraints = [] restraints_lp = [] test_restraints = [] for n in range(100): length = random.uniform(1, 40) slope = random.uniform(0, 0.1) dr = IMP.isd.CrossLinkMSRestraint(m, length, slope) dr_lp = IMP.isd.CrossLinkMSRestraint(m, length, slope, True) testdr = CrossLinkMS(length, slope) ambiguity = random.randint(1, 6) for a in range(ambiguity): sameparticle = bool(random.randint(0, 1)) p1 = IMP.Particle(m) d1 = IMP.core.XYZR.setup_particle(p1) d1.set_radius(random.uniform(1.0, 10.0)) d1.set_coordinates(IMP.algebra.get_random_vector_in(bb)) d1.set_coordinates_are_optimized(True) s1 = setupnuisance(m, random.uniform(1.0, 11.0), 0, 100, False) if sameparticle: p2 = p1 s2 = s1 else: p2 = IMP.Particle(m) d2 = IMP.core.XYZR.setup_particle(p2) d2.set_radius(random.uniform(1.0, 10.0)) d2.set_coordinates(IMP.algebra.get_random_vector_in(bb)) d2.set_coordinates_are_optimized(True) s2 = setupnuisance(m, random.uniform(1.0, 11.0), 0, 100, False) psi = setupnuisance(m, random.uniform(0.01, 0.49), 0.0, 0.5, False) dr.add_contribution((p1, p2), (s1, s2), psi) dr_lp.add_contribution((p1, p2), (s1, s2), psi) testdr.add_contribution(p1, p2, s1, s2, psi) restraints.append(dr) restraints_lp.append(dr_lp) test_restraints.append(testdr) lw = IMP.isd.LogWrapper(restraints, 1.0) restraint_set_lp = IMP.RestraintSet(restraints_lp, 1.0) test_score = sum([-log(r.get_probability()) for r in test_restraints]) for nr, r in enumerate(restraints): score = -log(r.unprotected_evaluate(None)) score_lp = restraints_lp[nr].unprotected_evaluate(None) score_test = -log(test_restraints[nr].get_probability()) self.assertAlmostEqual(score, score_test, places=4) self.assertAlmostEqual(score_lp, score_test, places=4) self.assertAlmostEqual(lw.unprotected_evaluate(None), restraint_set_lp.unprotected_evaluate(None), places=4) self.assertAlmostEqual(lw.unprotected_evaluate(None), test_score, places=4) self.assertAlmostEqual(restraint_set_lp.unprotected_evaluate(None), test_score, places=4)
def test_score_two_fold_ambiguity(self): IMP.test.TestCase.setUp(self) m = IMP.Model() p1 = IMP.Particle(m) p2 = IMP.Particle(m) p3 = IMP.Particle(m) slope = 0.01 length = 10 xyz1 = IMP.core.XYZ.setup_particle(p1) xyz2 = IMP.core.XYZ.setup_particle(p2) xyz3 = IMP.core.XYZ.setup_particle(p3) xyz1.set_coordinates((0, 0, 0)) xyz2.set_coordinates((0, 0, 0)) xyz3.set_coordinates((40, 0, 0)) sigma1 = setupnuisance(m, 5, 0, 100, False) sigma2 = setupnuisance(m, 5, 0, 100, False) sigma3 = setupnuisance(m, 5, 0, 100, False) psi = setupnuisance(m, 0.1, 0.0, 0.5, False) dr = IMP.isd.CrossLinkMSRestraint(m, length, slope) dr.add_contribution((p1, p2), (sigma1, sigma2), psi) dr.add_contribution((p3, p2), (sigma3, sigma2), psi) lw = IMP.isd.LogWrapper([dr], 1.0) testdr = CrossLinkMS(length, slope) testdr.add_contribution(xyz1, xyz2, sigma1, sigma2, psi) testdr.add_contribution(xyz3, xyz2, sigma3, sigma2, psi) # initialize also a restraint which output -log(prob) dr_lp = IMP.isd.CrossLinkMSRestraint(m, length, slope, True) dr_lp.add_contribution((p1, p2), (sigma1, sigma2), psi) dr_lp.add_contribution((p3, p2), (sigma3, sigma2), psi) maxdist = 40.0 npoints = 30 sigmas1 = sample([0.01, 0.1, 0.5, 1.0, 5.0, 10.0, 50.0, 100.0], 5) sigmas2 = sample([0.01, 0.1, 0.5, 1.0, 5.0, 10.0, 50.0, 100.0], 5) sigmas3 = sample([0.01, 0.1, 0.5, 1.0, 5.0, 10.0, 50.0, 100.0], 5) psis = sample( [0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.49], 5) for s1 in sigmas1: sigma1.set_scale(s1) for s2 in sigmas2: sigma2.set_scale(s2) for s3 in sigmas3: sigma3.set_scale(s3) for p1 in psis: psi.set_scale(p1) for i in range(npoints): xyz2.set_coordinates( IMP.algebra.Vector3D( maxdist / npoints * float(i), 0.0, 0.0)) dist = IMP.core.get_distance(xyz1, xyz2) scoretest = - \ log(testdr.get_probability()) score = lw.unprotected_evaluate(None) score_lp = dr_lp.unprotected_evaluate(None) self.assertAlmostEqual(score, scoretest, places=4) self.assertAlmostEqual(score_lp, scoretest, places=4)
def test_surface_geometry_init(self): """Test creation of geometry constrained to Surface.""" m = IMP.Model() surf = IMP.core.Surface.setup_particle(IMP.Particle(m)) geo = IMP.core.get_constrained_surface_geometry(surf) geo.set_was_used(True)
def test_coerce_particle_to_index(self): """Check that Particle is coerced to ParticleIndex in Python""" m = IMP.Model() pi = m.add_particle("P1") p = m.get_particle(pi) self.assertEqual(m.get_particle(p), p)
def test_build_system(self): """Test the new BuildSystem macro including beads and ideal helix""" try: import sklearn except ImportError: self.skipTest("no sklearn package") mdl = IMP.Model() tfile = self.get_input_file_name('topology_new.txt') input_dir = os.path.dirname(tfile) t = IMP.pmi.topology.TopologyReader(tfile, pdb_dir=input_dir, fasta_dir=input_dir, gmm_dir=input_dir) bs = IMP.pmi.macros.BuildSystem(mdl) bs.add_state(t) root_hier, dof = bs.execute_macro() # check a few selections sel1 = IMP.atom.Selection(root_hier,molecule="Prot1", resolution=1,copy_index=0).get_selected_particles() # res1 bead self.assertEqual(len(sel1), 7 + 2 ) sel1 = IMP.atom.Selection(root_hier,molecule="Prot1", resolution=1,copy_index=1).get_selected_particles() # res1 bead self.assertEqual(len(sel1), 7 + 2 ) sel1D = IMP.atom.Selection(root_hier,molecule="Prot1", representation_type=IMP.atom.DENSITIES).get_selected_particles() self.assertEqual(len(sel1D),6) #3 each sel2 = IMP.atom.Selection(root_hier,molecule="Prot2", resolution=10, copy_index=0).get_selected_particles() self.assertEqual(len(sel2),2) sel2 = IMP.atom.Selection(root_hier,molecule="Prot2", resolution=10, copy_index=1).get_selected_particles() self.assertEqual(len(sel2),2) sel3 = IMP.atom.Selection(root_hier,molecule="Prot3",resolution=5).get_selected_particles() self.assertEqual(len(sel3),2) sel4_1 = IMP.atom.Selection(root_hier,molecule="Prot4", resolution=1).get_selected_particles() sel4_10 = IMP.atom.Selection(root_hier,molecule="Prot4", resolution=10).get_selected_particles() sel4_D = IMP.atom.Selection(root_hier,molecule="Prot4", representation_type=IMP.atom.DENSITIES).get_selected_particles() sel5 = IMP.atom.Selection(root_hier, molecule='Prot5', resolution=1).get_selected_particles() color=IMP.display.Colored(sel5[0]).get_color() self.assertAlmostEqual(color.get_red(), 0.1, delta=1e-6) self.assertAlmostEqual(color.get_green(), 0.2, delta=1e-6) self.assertAlmostEqual(color.get_blue(), 0.3, delta=1e-6) self.assertEqual(len(sel4_1),10) self.assertEqual(len(sel4_10),1) self.assertEqual(len(sel4_D),2) # check rigid bodies rbs = dof.get_rigid_bodies() fbs = dof.get_flexible_beads() self.assertEqual(len(rbs),3) # Prot1x2 Prot3 self.assertEqual(len(fbs), 4 + 2)
def test_all_derivatives_correct(self): m = IMP.Model() nlevels = 4 nbeads = 10 prob_nonrigid = 0.5 tf = IMP.algebra.get_random_local_transformation((0, 0, 0), 10) rf = IMP.algebra.ReferenceFrame3D(tf) rb = IMP.core.RigidBody.setup_particle(IMP.Particle(m), rf) rb.set_coordinates_are_optimized(True) all_beads = [] all_rbs = [rb] for level in range(nlevels): center = tf.get_translation() ps = self._make_beads(m, nbeads, center=center, radius=10) all_beads += ps for p in ps: if np.random.uniform() < prob_nonrigid: rb.add_non_rigid_member(p) nrm = IMP.core.NonRigidMember(p) for k in nrm.get_internal_coordinate_keys(): nrm.get_particle().set_is_optimized(k, True) else: rb.add_member(p) tf = IMP.algebra.get_random_local_transformation((0, 0, 0), 10) rf = IMP.algebra.ReferenceFrame3D(tf) rb_nested = IMP.core.RigidBody.setup_particle(IMP.Particle(m), rf) if np.random.uniform() < prob_nonrigid: rb.add_non_rigid_member(rb_nested) nrm = IMP.core.NonRigidMember(rb_nested) for k in nrm.get_internal_coordinate_keys( ) + nrm.get_internal_rotation_keys(): nrm.get_particle().set_is_optimized(k, True) else: rb.add_member(rb_nested) rb = rb_nested all_rbs.append(rb) m.update() ss = IMP.core.DistanceToSingletonScore(IMP.core.Harmonic(0, 1), (0, 0, 0)) rs = [ IMP.core.SingletonRestraint(m, ss, p.get_particle_index()) for p in all_beads ] sf = IMP.core.RestraintsScoringFunction(rs) pis, fks = list( zip(*[(p.get_particle_index(), fk) for p in all_beads + all_rbs[1:] for fk in IMP.core.RigidBodyMember( p).get_internal_coordinate_keys()])) gc = GradientCalculator(sf, pis, fks) grad_approx = gc.get_approximate_gradient(eps=1e-6) grad_exact = gc.get_exact_gradient() self.assertSequenceAlmostEqual(list(grad_exact), list(grad_approx), delta=1e-3) for rb in all_rbs[1:]: pi = rb.get_particle_index() nrm = IMP.core.RigidBodyMember(rb) fks = nrm.get_internal_rotation_keys() gc = GradientCalculator(sf, [pi] * 4, fks) grad_approx = gc.get_approximate_gradient(eps=1e-6) grad_exact = gc.get_exact_gradient() self.assertSequenceAlmostEqual(list(grad_exact), list(grad_approx), delta=1e-3) rb = all_rbs[0] pi = rb.get_particle_index() fks = rb.get_rotation_keys() gc = GradientCalculator(sf, [pi] * 4, fks) grad_approx = gc.get_approximate_gradient(eps=1e-6) grad_exact = gc.get_exact_gradient() self.assertSequenceAlmostEqual(list(grad_exact), list(grad_approx), delta=1e-3)
def test_no_transform(self): """Test pair score with no transforms""" m = IMP.Model() # Should return the regular sphere distance self.assertAlmostEqual(self.get_score(m, []), 6.5, delta=1e-6)
def test_restraint_probability_complex(self): """Test restraint gets correct probabilities""" for i in range(2): m = IMP.Model() print("Testing PMI version",i+1) if i==0: rcomplex = self.init_representation_complex(m) xlc,cldb = self.setup_crosslinks_complex(rcomplex,"single_category") else: rcomplex,dof=self.init_representation_complex_pmi2(m) xlc,cldb = self.setup_crosslinks_complex(root_hier=rcomplex, mode="single_category") self.assertEqual(len(dof.get_movers()),42) dof.get_nuisances_from_restraint(xlc) self.assertEqual(len(dof.get_movers()),44) # check all internals didn't change since last time o=IMP.pmi.output.Output() o.write_test("expensive_test_new_cross_link_ms_restraint.dat", [xlc]) passed=o.test(self.get_input_file_name("expensive_test_new_cross_link_ms_restraint.dat"), [xlc]) self.assertEqual(passed, True) rs = xlc.get_restraint() # check the probability of cross-links restraints=[] for xl in xlc.xl_list: p0 = xl["Particle1"] p1 = xl["Particle2"] prob = xl["Restraint"].get_probability() resid1 = xl[cldb.residue1_key] chain1 = xl[cldb.protein1_key] resid2 = xl[cldb.residue2_key] chain2 = xl[cldb.protein2_key] d0 = IMP.core.XYZ(p0) d1 = IMP.core.XYZ(p1) sig1 = xl["Particle_sigma1"] sig2 = xl["Particle_sigma2"] psi = xl["Particle_psi"] d0 = IMP.core.XYZ(p0) d1 = IMP.core.XYZ(p1) dist=IMP.core.get_distance(d0, d1) test_prob=get_probability([d0],[d1],[sig1],[sig2],[psi],21.0,0.0) restraints.append(xl["Restraint"]) # check that the probability is the same for # each cross-link self.assertAlmostEqual(prob, test_prob, delta=0.00001) # check the log_wrapper log_wrapper_score=rs.unprotected_evaluate(None) test_log_wrapper_score=log_evaluate(restraints) self.assertAlmostEqual(log_wrapper_score, test_log_wrapper_score, delta=0.00001) if i==0: rex0 = IMP.pmi.macros.ReplicaExchange0(m, rcomplex, monte_carlo_sample_objects=[rcomplex], number_of_frames=2, test_mode=True, replica_exchange_object = rem) rex0.execute_macro() else: rex = IMP.pmi.macros.ReplicaExchange0(m, root_hier=rcomplex, monte_carlo_sample_objects=dof.get_movers(), number_of_frames=2, test_mode=True, replica_exchange_object = rem) rex.execute_macro() for output in ['excluded.None.xl.db', 'expensive_test_new_cross_link_ms_restraint.dat', 'included.None.xl.db', 'missing.None.xl.db']: os.unlink(output)
def setUp(self): IMP.test.TestCase.setUp(self) IMP.set_log_level(0) self.m = IMP.Model() self.rs = XTransRestraint(self.m)
def test_restraint_probability_beads(self): """Test restraint works for all-bead systems""" for i in range(2): m = IMP.Model() if i==0: rbeads=self.init_representation_beads(m) xlbeads,cldb=self.setup_crosslinks_beads(rbeads,"single_category") else: rbeads,dof=self.init_representation_beads_pmi2(m) xlbeads,cldb=self.setup_crosslinks_beads(root_hier=rbeads,mode="single_category") self.assertEqual(len(dof.get_movers()),60) dof.get_nuisances_from_restraint(xlbeads) self.assertEqual(len(dof.get_movers()),62) for xl in xlbeads.xl_list: chain1 = xl[cldb.protein1_key] chain2 = xl[cldb.protein2_key] res1 = xl[cldb.residue1_key] res2 = xl[cldb.residue2_key] ids = xl[cldb.unique_id_key] # randomize coordinates and check that the probability is OK print("testing PMI version "+str(i+1)) for j in range(100): if i==0: rbeads.shuffle_configuration(max_translation=10) else: IMP.pmi.tools.shuffle_configuration(rbeads,max_translation=10) cross_link_dict={} for xl in xlbeads.xl_list: p0 = xl["Particle1"] p1 = xl["Particle2"] prob = xl["Restraint"].get_probability() resid1 = xl[cldb.residue1_key] chain1 = xl[cldb.protein1_key] resid2 = xl[cldb.residue2_key] chain2 = xl[cldb.protein2_key] xlid=xl[cldb.unique_id_key] d0 = IMP.core.XYZ(p0) d1 = IMP.core.XYZ(p1) sig1 = xl["Particle_sigma1"] sig2 = xl["Particle_sigma2"] psi = xl["Particle_psi"] if xlid not in cross_link_dict: cross_link_dict[xlid]=([d0],[d1],[sig1],[sig2],[psi],prob) else: cross_link_dict[xlid][0].append(d0) cross_link_dict[xlid][1].append(d1) cross_link_dict[xlid][2].append(sig1) cross_link_dict[xlid][3].append(sig2) cross_link_dict[xlid][4].append(psi) for xlid in cross_link_dict: test_prob=get_probability(cross_link_dict[xlid][0], cross_link_dict[xlid][1], cross_link_dict[xlid][2], cross_link_dict[xlid][3], cross_link_dict[xlid][4],21.0,0.01) prob=cross_link_dict[xlid][5] self.assertAlmostEqual(test_prob,prob, delta=0.0001) if i==0: rex0 = IMP.pmi.macros.ReplicaExchange0(m, rbeads, monte_carlo_sample_objects=[rbeads], number_of_frames=2, test_mode=True, replica_exchange_object = rem) rex0.execute_macro() else: rex = IMP.pmi.macros.ReplicaExchange0(m, root_hier=rbeads, monte_carlo_sample_objects=dof.get_movers(), number_of_frames=2, test_mode=True, replica_exchange_object = rem) rex.execute_macro() for output in ['excluded.None.xl.db', 'included.None.xl.db', 'missing.None.xl.db']: os.unlink(output)
def test_atomic_xl(self): """ test PMI setup of atomic XL restraint """ # fake data tname = self.get_tmp_file_name("test.txt") with open(tname, "w") as fh: fh.write("prot1,res1,prot2,res2\nProt1,7,Prot1,39\n") cldbkc = IMP.pmi.io.crosslink.CrossLinkDataBaseKeywordsConverter() cldbkc.set_protein1_key("prot1") cldbkc.set_protein2_key("prot2") cldbkc.set_residue1_key("res1") cldbkc.set_residue2_key("res2") cldb = IMP.pmi.io.crosslink.CrossLinkDataBase(cldbkc) cldb.create_set_from_file(tname) self.assertEqual(cldb.get_number_of_xlid(), 1) # create two states, each with two copies of the protein mdl = IMP.Model() s = IMP.pmi.topology.System(mdl) seqs = IMP.pmi.topology.Sequences( self.get_input_file_name('multi_seq.fasta'), name_map={'Protein_1': 'Prot1'}) # build state 1 st1 = s.create_state() m1 = st1.create_molecule("Prot1", sequence=seqs["Prot1"], chain_id='A') m1_res = m1.add_structure(self.get_input_file_name('multi.pdb'), chain_id='A', offset=-54, model_num=0) m1.add_representation(m1_res, resolutions=[0]) m1a = m1.create_copy(chain_id='G') m1a_res = m1a.add_structure(self.get_input_file_name('multi.pdb'), chain_id='G', offset=-54) m1a.add_representation(m1a_res, resolutions=[0]) # build state 2 st2 = s.create_state() m2 = st2.create_molecule("Prot1", sequence=seqs["Prot1"], chain_id='A') m2_res = m2.add_structure(self.get_input_file_name('multi.pdb'), chain_id='A', offset=-54, model_num=1) m2.add_representation(m2_res, resolutions=[0]) m2a = m2.create_copy(chain_id='G') m2a_res = m2a.add_structure(self.get_input_file_name('multi.pdb'), chain_id='G', offset=-54) m2a.add_representation(m2a_res, resolutions=[0]) hier = s.build() #IMP.atom.show_molecular_hierarchy(hier) xl = IMP.pmi.restraints.crosslinking.AtomicCrossLinkMSRestraint( hier, cldb, nstates=[0, 1], atom_type="CA") # check that you created 8 restraints: # Each state: A1-B1, A1-B2, A2-B1, A2-B2 rs = xl.get_restraint_set() self.assertEqual(rs.get_number_of_restraints(), 1) xlrs = IMP.isd.AtomicCrossLinkMSRestraint.get_from(rs.get_restraint(0)) self.assertIsInstance(xlrs, IMP.isd.AtomicCrossLinkMSRestraint) self.assertEqual(xlrs.get_number_of_contributions(), 8) dof = IMP.pmi.dof.DegreesOfFreedom(mdl) dof.get_nuisances_from_restraint(xl) self.assertEqual(len(dof.get_movers()), 2)
def main(): m = IMP.Model() m.set_log_level(IMP.SILENT) cplxInfos = build_subunits_info(m) HGM.helpers.mute_all_restraints(m) # dataDirSample = "../../data/ARP" # sampleFileName = "save-1TYQ-HGM.txt" # sampleFilePath = os.path.join(dataDirSample,sampleFileName) # index = 0 # sampleFilePath = os.path.join( saveDirSample, HGM.helpers.forge_sample_name(savePrefix, index) ) # mcs.load_configuration(0) # print "WITH differenciation" # print HGM.helpers.get_current_pause_as_pdbCA_string(arpInfos, True) # # print "\n\nWITHOUT differenciation" # print HGM.helpers.get_current_pause_as_pdbCA_string(arpInfos, False) # mcs = load_configurations_from_sample_index_list(cplxInfos,sample_indexes) mcs = load_configurations_from_sorted_subsample(cplxInfos, nb_configs, type) # mcs = HGM.representation.MyConfigurationSet(arpInfos) # loop_index=0 # print "loading configurations" # for sidx in sample_indexes : # loop_index+=1 # if loop_index % 15 == 0 : print "" # print sidx, # sampleFilePath = os.path.join( saveDirSample, HGM.helpers.forge_sample_name(savePrefix, sidx) ) # mcs.read_all_configs_from_file(sampleFilePath) print "got", mcs.get_number_of_configurations(), "configurations" loop_index = 0 print "writing pdb_files" for i in range(mcs.get_number_of_configurations()): loop_index += 1 if loop_index % 150 == 0: print "" if loop_index % 15 == 0: print i, mcs.load_configuration(i) filePath = os.path.join(pdbDir, pdbPrefix + "--{0:06d}.pdb".format(i)) chainMap = { "arp3": "A", "arp2": "B", "arpc1": "C", "arpc2": "D", "arpc3": "E", "arpc4": "F", "arpc5": "G" } # chainMap={"p8":"A", # "p52":"B", # "p44":"C", # "p34":"D", # "p62":"E", # "XPB":"F", # "XPD":"G", # "MAT1":"H", # "CDK7":"I", # "CyclinH":"J" # } write_current_pause_as_pdbCA_with_chains(filePath, cplxInfos, chainMap)
def test_loop_reconstruction(self): """Test loop reconstruction""" # input parameter pdbfile = self.get_input_file_name( "loop_reconstruction/starting.structure.pdb") fastafile = self.get_input_file_name( "loop_reconstruction/sequence.fasta") sequences = IMP.pmi.topology.Sequences(fastafile) # create the representation log_objects = [] optimizable_objects = [] sw = tools.Stopwatch() log_objects.append(sw) m = IMP.Model() s = IMP.pmi.topology.System(m) st = s.create_state() cA = st.create_molecule("chainA", sequence=sequences[0]) atomic = cA.add_structure(pdbfile, chain_id='A') cA.add_representation(atomic, resolutions=[1, 10], color=0.) cA.add_representation(cA.get_non_atomic_residues(), resolutions=[1], color=0.) cB = st.create_molecule("chainB", sequence=sequences[0]) atomic = cB.add_structure(pdbfile, chain_id='B') cB.add_representation(atomic, resolutions=[1, 10], color=0.5) cB.add_representation(cB.get_non_atomic_residues(), resolutions=[1], color=0.) root_hier = s.build() dof = IMP.pmi.dof.DegreesOfFreedom(m) dof.create_rigid_body(cA) dof.create_rigid_body(cB) cr = IMP.pmi.restraints.stereochemistry.ConnectivityRestraint( root_hier) cr.add_to_model() log_objects.append(cr) listofexcludedpairs = [] lof = [ cA[:12], cB[:12], cA[293:339], cB[293:339], cA[685:701], cB[685:701], cA[453:464], cB[453:464], cA[471:486], cB[471:486], cA[813:859], cB[813:859] ] # add bonds and angles for l in lof: print(l) rbr = IMP.pmi.restraints.stereochemistry.ResidueBondRestraint( objects=l) rbr.add_to_model() listofexcludedpairs += rbr.get_excluded_pairs() log_objects.append(rbr) rar = IMP.pmi.restraints.stereochemistry.ResidueAngleRestraint( objects=l) rar.add_to_model() listofexcludedpairs += rar.get_excluded_pairs() log_objects.append(rar) # add excluded volume ev = IMP.pmi.restraints.stereochemistry.ExcludedVolumeSphere( included_objects=root_hier, resolution=10.0) ev.add_excluded_particle_pairs(listofexcludedpairs) ev.add_to_model() log_objects.append(ev) mc = samplers.MonteCarlo(m, dof.get_movers(), 1.0) log_objects.append(mc) o = output.Output() print("Starting test") o.write_test("test.current.dict", log_objects) o.test( self.get_input_file_name( "loop_reconstruction/test.IMP-ee1763c6.PMI-4669cfca.dict"), log_objects) os.unlink('test.current.dict')
mh2 = IMP.atom.get_by_type(h2, IMP.atom.ATOM_TYPE) r2 = [x.get_particle() for x in mh2] rb2 = IMP.atom.setup_as_rigid_body(h2) return (r1, r2, rb1, rb2) def perform_benchmark(model, tr_list, r1, r2, rb1, rb2, maxiter): rest = IMP.multifit.ComplementarityRestraint(r1, r2) rest.set_complementarity_value(-1) rest.set_complementarity_thickness(1) rest.set_interior_layer_thickness(1) maxiter = min(maxiter, len(tr_list)) for i in xrange(maxiter): start = time.time() IMP.core.transform(rb2, tr_list[i][0]) score = 8 * rest.evaluate(False) orig_score = tr_list[i][1] pct_error = 100 * abs((score - orig_score) / orig_score) IMP.core.transform(rb2, tr_list[i][0].get_inverse()) elapsed = time.time() - start IMP.benchmark.report("ComplementarityRestraint %d" % i, "", elapsed, pct_error) IMP.setup_from_argv(sys.argv, "Geometric complementarity benchmark.") IMP.set_log_level(IMP.SILENT) tr_list = read_transformations() model = IMP.Model() r1, r2, rb1, rb2 = load_proteins(model) perform_benchmark(model, tr_list, r1, r2, rb1, rb2, 4)
def test_restraint_probability_beads(self): m = IMP.Model() rbeads = init_representation_beads(m) xlb, restraints_beads = setup_crosslinks_beads(rbeads, "single_category") # check internal data structure ds = xlb.pairs nxl = 0 for l in restraints_beads.split("\n"): if not l.strip(): continue if l[0] == "#": continue t = l.split() chain1 = t[0] chain2 = t[1] res1 = int(t[2]) res2 = int(t[3]) ids = float(t[4]) xlid = int(t[5]) dsres1 = ds[nxl][3] dschain1 = ds[nxl][4] dsres2 = ds[nxl][5] dschain2 = ds[nxl][6] dsxlid = int(ds[nxl][11]) ''' self.assertEqual(chain1,dschain1) self.assertEqual(chain2,dschain2) self.assertEqual(res1,dsres1) self.assertEqual(res2,dsres2) self.assertEqual(xlid,dsxlid) ''' nxl += 1 # randomize coordinates and check that the probability is OK for i in range(100): rbeads.shuffle_configuration(max_translation=10) cross_link_dict = {} for p in xlb.pairs: p0 = p[0] p1 = p[1] prob = p[2].get_probability() resid1 = p[3] chain1 = p[4] resid2 = p[5] chain2 = p[6] attribute = p[7] xlid = p[11] d0 = IMP.core.XYZ(p0) d1 = IMP.core.XYZ(p1) sig1 = xlb.get_sigma(p[8])[0] sig2 = xlb.get_sigma(p[9])[0] psi = xlb.get_psi(p[10])[0] if xlid not in cross_link_dict: cross_link_dict[xlid] = ([d0], [d1], [sig1], [sig2], [psi], prob) else: cross_link_dict[xlid][0].append(d0) cross_link_dict[xlid][1].append(d1) cross_link_dict[xlid][2].append(sig1) cross_link_dict[xlid][3].append(sig2) cross_link_dict[xlid][4].append(psi) for xlid in cross_link_dict: test_prob = get_probability(cross_link_dict[xlid][0], cross_link_dict[xlid][1], cross_link_dict[xlid][2], cross_link_dict[xlid][3], cross_link_dict[xlid][4], 21.0, 0.01) prob = cross_link_dict[xlid][5] self.assertAlmostEqual(prob / test_prob, 1.0, delta=0.0001) for output in [ 'excluded.None.xl.db', 'included.None.xl.db', 'missing.None.xl.db' ]: os.unlink(output)
# flexible beads are automatically added to missing regions and sampled rigid_bodies = [["Rpb4"], ["Rpb7"]] super_rigid_bodies = [["Rpb4","Rpb7"]] chain_of_super_rigid_bodies = [["Rpb4"], ["Rpb7"]] # ################################################ # #-------------------------------- # Build the Model Representation #-------------------------------- # Initialize model m = IMP.Model() # Create list of components from topology file topology = IMP.pmi.topology.TopologyReader(topology_file) domains = topology.component_list bm = IMP.pmi.macros.BuildModel(m, component_topologies=domains, list_of_rigid_bodies=rigid_bodies, list_of_super_rigid_bodies=super_rigid_bodies, chain_of_super_rigid_bodies=chain_of_super_rigid_bodies) representation = bm.get_representation() # add colors to the components for nc,component in enumerate(domains):
"""This script shows how to simulate an atomic system with MD, with a secondary structure elastic network to speed things up. """ import IMP import RMF import IMP.atom import IMP.rmf import IMP.pmi import IMP.pmi.topology import IMP.pmi.dof import IMP.pmi.macros import IMP.pmi.restraints.stereochemistry # Setup System and add a State mdl = IMP.Model() s = IMP.pmi.topology.System(mdl) st1 = s.create_state() # Read sequences and create Molecules seqs = IMP.pmi.topology.Sequences(IMP.pmi.get_example_path('data/gcp2.fasta')) gcp2 = st1.create_molecule("GCP2", sequence=seqs["GCP2_YEAST"], chain_id='A') # Add structure. This function returns a list of the residues that now have structure a1 = gcp2.add_structure(IMP.pmi.get_example_path('data/gcp2.pdb'), chain_id='A') # Add structured part representation and then build gcp2.add_representation(a1, resolutions=[0]) print('building molecule') hier = s.build()
def test_make_topology(self): """Test construction of topology""" m = IMP.Model() pdb = IMP.atom.read_pdb(self.get_input_file_name('1z5s_C.pdb'), m) atoms = IMP.atom.get_by_type(pdb, IMP.atom.ATOM_TYPE) residues = IMP.atom.get_by_type(pdb, IMP.atom.RESIDUE_TYPE) last_atom = atoms[-1].get_particle() # PDB reader will have already set radii but not CHARMM type: self.assertEqual(IMP.core.XYZR.get_is_setup(last_atom), True) self.assertEqual(IMP.atom.CHARMMAtom.get_is_setup(last_atom), False) ff = IMP.atom.CHARMMParameters(IMP.atom.get_data_path("top.lib"), IMP.atom.get_data_path("par.lib")) topology = ff.create_topology(pdb) topology.apply_default_patches() self.assertEqual(topology.get_number_of_segments(), 1) segment = topology.get_segment(0) self.assertEqual(segment.get_number_of_residues(), 156) self.assertRaises(IMP.ValueException, segment.apply_default_patches, ff) for typ in (IMP.atom.Charged, IMP.atom.LennardJones): self.assertEqual(typ.get_is_setup(last_atom), False) topology.add_atom_types(pdb) self.assertEqual(IMP.atom.CHARMMAtom.get_is_setup(last_atom), True) self.assertEqual( IMP.atom.CHARMMAtom(last_atom).get_charmm_type(), 'OC') topology.add_charges(pdb) self.assertAlmostEqual(IMP.atom.Charged(last_atom).get_charge(), -0.67, delta=1e-3) bonds = topology.add_bonds(pdb) self.assertEqual(len(bonds), 1215) angles = ff.create_angles(bonds) self.assertEqual(len(angles), 1651) dihedrals = ff.create_dihedrals(bonds) self.assertEqual(len(dihedrals), 2161) for (bondr1, bondr2, bonda1, bonda2, atyp1, atyp2, bondlen, fcon) in [ # intraresidue bond (residues[0], residues[0], IMP.atom.AT_CA, IMP.atom.AT_CB, 'CT1', 'CT3', 1.5380, 21.095), # backbone bond (residues[0], residues[1], IMP.atom.AT_C, IMP.atom.AT_N, 'C', 'NH1', 1.3450, 27.203), # CTER bond (residues[-1], residues[-1], IMP.atom.AT_OXT, IMP.atom.AT_C, 'OC', 'CC', 1.2600, 32.404) ]: r1 = IMP.atom.Residue(bondr1) r2 = IMP.atom.Residue(bondr2) a1 = IMP.atom.get_atom(r1, bonda1) a2 = IMP.atom.get_atom(r2, bonda2) self.assertAtomsBonded(a1, a2, atyp1, atyp2, bondlen, fcon) impropers = topology.add_impropers(pdb) self.assertEqual(len(impropers), 509) ff.add_radii(pdb, 1.2) ff.add_well_depths(pdb) self.assertAlmostEqual(IMP.core.XYZR(last_atom).get_radius(), 2.04, delta=1e-3) self.assertAlmostEqual( IMP.atom.LennardJones(last_atom).get_well_depth(), 0.12, delta=1e-3) last_cg1 = atoms[-3].get_particle() self.assertAlmostEqual(IMP.core.XYZR(last_cg1).get_radius(), 2.472, delta=1e-3)
def test_selection(self): """Test selection""" # input parameter pdbfile = self.get_input_file_name("1WCM.pdb") fastafile = self.get_input_file_name("1WCM.fasta.txt") components = ["Rpb3", "Rpb3.copy", "Rpb4"] chains = "CCD" colors = [0., 0.5, 1.0] beadsize = 20 fastids = ['1WCM:C', '1WCM:C', '1WCM:D'] m = IMP.Model() simo = representation.Representation(m) hierarchies = {} for n in range(len(components)): simo.create_component(components[n], color=colors[n]) simo.add_component_sequence(components[n], fastafile, id=fastids[n]) hierarchies[components[n]] \ = simo.autobuild_model( components[n], pdbfile, chains[n], resolutions=[1, 10, 100], missingbeadsize=beadsize) simo.setup_component_sequence_connectivity(components[n], 1) def test(a, b): assert a == b, "%d != %d" % (a, b) result_dict = { "All": 803, "resolution=1": 721, "resolution=1,resid=10": 3, "resolution=1,resid=10,name=Rpb3": 1, "resolution=1,resid=10,name=Rpb3,ambiguous": 2, "resolution=1,resid=10,name=Rpb4,ambiguous": 1, "resolution=1,resrange=(10,20),name=Rpb3": 11, "resolution=1,resrange=(10,20),name=Rpb3,ambiguous": 22, "resolution=10,resrange=(10,20),name=Rpb3": 2, "resolution=10,resrange=(10,20),name=Rpb3,ambiguous": 4, "resolution=100,resrange=(10,20),name=Rpb3": 1, "resolution=100,resrange=(10,20),name=Rpb3,ambiguous": 2, "Rpb4_1-3_bead resolution=1": 1, "Rpb4_1-3_bead resolution=10": 1, "Rpb4_1-3_bead resolution=100": 1, "Rpb4_4-76_pdb resolution=1": 73, "Rpb4_4-76_pdb resolution=10": 0, "Rpb4_4-76_pdb resolution=100": 0, "Rpb4_4-76_pdb#2 resolution=1": 0, "Rpb4_4-76_pdb#2 resolution=10": 8, "Rpb4_4-76_pdb#2 resolution=100": 0, "Rpb4_4-76_pdb#3 resolution=1": 0, "Rpb4_4-76_pdb#3 resolution=10": 0, "Rpb4_4-76_pdb#3 resolution=100": 1, "Rpb4_77-96_bead resolution=1": 1, "Rpb4_77-96_bead resolution=10": 1, "Rpb4_77-96_bead resolution=100": 1, "Rpb4_97-116_bead resolution=1": 1, "Rpb4_97-116_bead resolution=10": 1, "Rpb4_97-116_bead resolution=100": 1, "Rpb4_117_bead resolution=1": 1, "Rpb4_117_bead resolution=10": 1, "Rpb4_117_bead resolution=100": 1, "Rpb4_118-221_pdb resolution=1": 104, "Rpb4_118-221_pdb resolution=10": 0, "Rpb4_118-221_pdb resolution=100": 0, "Rpb4_118-221_pdb#2 resolution=1": 0, "Rpb4_118-221_pdb#2 resolution=10": 11, "Rpb4_118-221_pdb#2 resolution=100": 0, "Rpb4_118-221_pdb#3 resolution=1": 0, "Rpb4_118-221_pdb#3 resolution=10": 0, "Rpb4_118-221_pdb#3 resolution=100": 2, "Rpb3.copy_1-2_bead resolution=1": 1, "Rpb3.copy_1-2_bead resolution=1": 1, "Rpb3.copy_1-2_bead resolution=10": 1, "Rpb3.copy_1-2_bead resolution=100": 1, "Rpb3.copy_3-268_pdb resolution=1": 266, "Rpb3.copy_3-268_pdb resolution=10": 0, "Rpb3.copy_3-268_pdb resolution=100": 0, "Rpb3.copy_3-268_pdb#2 resolution=1": 0, "Rpb3.copy_3-268_pdb#2 resolution=10": 27, "Rpb3.copy_3-268_pdb#2 resolution=100": 0, "Rpb3.copy_3-268_pdb#3 resolution=1": 0, "Rpb3.copy_3-268_pdb#3 resolution=10": 0, "Rpb3.copy_3-268_pdb#3 resolution=100": 3, "Rpb3.copy_269-288_bead resolution=1": 1, "Rpb3.copy_269-288_bead resolution=10": 1, "Rpb3.copy_269-288_bead resolution=100": 1, "Rpb3.copy_289-308_bead resolution=1": 1, "Rpb3.copy_289-308_bead resolution=10": 1, "Rpb3.copy_289-308_bead resolution=100": 1, "Rpb3.copy_309-318_bead resolution=1": 1, "Rpb3.copy_309-318_bead resolution=10": 1, "Rpb3.copy_309-318_bead resolution=100": 1, "Rpb3_1-2_bead resolution=1": 1, "Rpb3_1-2_bead resolution=10": 1, "Rpb3_1-2_bead resolution=100": 1, "Rpb3_3-268_pdb resolution=1": 266, "Rpb3_3-268_pdb resolution=10": 0, "Rpb3_3-268_pdb resolution=100": 0, "Rpb3_3-268_pdb#2 resolution=1": 0, "Rpb3_3-268_pdb#2 resolution=10": 27, "Rpb3_3-268_pdb#2 resolution=100": 0, "Rpb3_3-268_pdb#3 resolution=1": 0, "Rpb3_3-268_pdb#3 resolution=10": 0, "Rpb3_3-268_pdb#3 resolution=100": 3, "Rpb3_269-288_bead resolution=1": 1, "Rpb3_269-288_bead resolution=10": 1, "Rpb3_269-288_bead resolution=100": 1, "Rpb3_289-308_bead resolution=1": 1, "Rpb3_289-308_bead resolution=10": 1, "Rpb3_289-308_bead resolution=100": 1, "Rpb3_309-318_bead resolution=1": 1, "Rpb3_309-318_bead resolution=10": 1, "Rpb3_309-318_bead resolution=100": 1, "Beads": 12, "Molecule": 803, "resolution=1,Molecule": 721, "resolution=10,Molecule": 85, "resolution=100,Molecule": 21, "resolution=1,Beads": 12, "resolution=10,Beads": 12, "resolution=100,Beads": 12, "resolution=2": 721, "resolution=7": 85, "resolution=10": 85, "resolution=100": 21 } test(result_dict["All"], len(tools.select(simo))) test(result_dict["resolution=1"], len(tools.select(simo, resolution=1))) test(result_dict["resolution=1,resid=10"], len(tools.select(simo, resolution=1, residue=10))) test(result_dict["resolution=1,resid=10,name=Rpb3"], len(tools.select(simo, resolution=1, name="Rpb3", residue=10))) test( result_dict["resolution=1,resid=10,name=Rpb3,ambiguous"], len( tools.select(simo, resolution=1, name="Rpb3", name_is_ambiguous=True, residue=10))) test( result_dict["resolution=1,resid=10,name=Rpb4,ambiguous"], len( tools.select(simo, resolution=1, name="Rpb4", name_is_ambiguous=True, residue=10))) test( result_dict["resolution=1,resrange=(10,20),name=Rpb3"], len( tools.select(simo, resolution=1, name="Rpb3", first_residue=10, last_residue=20))) test( result_dict["resolution=1,resrange=(10,20),name=Rpb3,ambiguous"], len( tools.select(simo, resolution=1, name="Rpb3", name_is_ambiguous=True, first_residue=10, last_residue=20))) test( result_dict["resolution=10,resrange=(10,20),name=Rpb3"], len( tools.select(simo, resolution=10, name="Rpb3", first_residue=10, last_residue=20))) test( result_dict["resolution=10,resrange=(10,20),name=Rpb3,ambiguous"], len( tools.select(simo, resolution=10, name="Rpb3", name_is_ambiguous=True, first_residue=10, last_residue=20))) test( result_dict["resolution=100,resrange=(10,20),name=Rpb3"], len( tools.select(simo, resolution=100, name="Rpb3", first_residue=10, last_residue=20))) test( result_dict["resolution=100,resrange=(10,20),name=Rpb3,ambiguous"], len( tools.select(simo, resolution=100, name="Rpb3", name_is_ambiguous=True, first_residue=10, last_residue=20))) for key in hierarchies: seen = {} for h in hierarchies[key]: # Handle duplicate names if h.get_name() in seen: name = h.get_name() + "#%d" % seen[h.get_name()] seen[h.get_name()] += 1 else: name = h.get_name() seen[h.get_name()] = 2 test(result_dict[name + " resolution=1"], len(tools.select(simo, resolution=1, hierarchies=[h]))) test(result_dict[name + " resolution=10"], len(tools.select(simo, resolution=10, hierarchies=[h]))) test(result_dict[name + " resolution=100"], len(tools.select(simo, resolution=100, hierarchies=[h]))) test(result_dict["Beads"], len(tools.select(simo, representation_type="Beads"))) test(result_dict["Molecule"], len(tools.select(simo, representation_type="Molecule"))) test( result_dict["resolution=1,Molecule"], len( tools.select(simo, resolution=1, representation_type="Molecule"))) test( result_dict["resolution=10,Molecule"], len( tools.select(simo, resolution=10, representation_type="Molecule"))) test( result_dict["resolution=100,Molecule"], len( tools.select(simo, resolution=100, representation_type="Molecule"))) test( result_dict["resolution=1,Beads"], len(tools.select(simo, resolution=1, representation_type="Beads"))) test( result_dict["resolution=10,Beads"], len(tools.select(simo, resolution=10, representation_type="Beads"))) test( result_dict["resolution=100,Beads"], len(tools.select(simo, resolution=100, representation_type="Beads"))) test(result_dict["resolution=2"], len(tools.select(simo, resolution=2))) test(result_dict["resolution=7"], len(tools.select(simo, resolution=7))) test(result_dict["resolution=10"], len(tools.select(simo, resolution=10))) test(result_dict["resolution=100"], len(tools.select(simo, resolution=100)))
def test_constraint_symmetry(self): """Test setup and activity of symmetry constraint""" ### create representation mdl = IMP.Model() s = IMP.pmi.topology.System(mdl) st1 = s.create_state() seqs = IMP.pmi.topology.Sequences( self.get_input_file_name('seqs.fasta')) m1 = st1.create_molecule("Prot1", sequence=seqs["Protein_1"]) a1 = m1.add_structure(self.get_input_file_name('prot.pdb'), chain_id='A', res_range=(55, 63), offset=-54) m1.add_representation(a1, resolutions=[0, 1]) m1.add_representation(m1.get_non_atomic_residues(), resolutions=[1]) m3 = m1.create_clone(chain_id='C') m2 = st1.create_molecule("Prot2", sequence=seqs["Protein_2"]) a2 = m2.add_structure(self.get_input_file_name('prot.pdb'), chain_id='B', res_range=(180, 192), offset=-179) m2.add_representation(a2, resolutions=[0, 1]) m4 = m2.create_clone(chain_id='D') root = s.build() ### create movers and constraints dof = IMP.pmi.dof.DegreesOfFreedom(mdl) rb1_movers = dof.create_rigid_body( m1, nonrigid_parts=m1.get_non_atomic_residues()) rb2_movers = dof.create_rigid_body( m2, nonrigid_parts=m2.get_non_atomic_residues()) dof.create_rigid_body(m3, nonrigid_parts=m3.get_non_atomic_residues()) dof.create_rigid_body(m4, nonrigid_parts=m4.get_non_atomic_residues()) sym_trans = IMP.algebra.get_random_local_transformation( IMP.algebra.Vector3D(0, 0, 0)) dof.constrain_symmetry([m1, m2], [m3, m4], sym_trans) m1_leaves = IMP.pmi.tools.select_at_all_resolutions(m1.get_hierarchy()) m3_leaves = IMP.pmi.tools.select_at_all_resolutions(m3.get_hierarchy()) ### test symmetry initially correct mdl.update() for p1, p3 in zip(m1_leaves, m3_leaves): c1 = IMP.core.XYZ(p1).get_coordinates() c3 = sym_trans * IMP.core.XYZ(p3).get_coordinates() for i in range(3): self.assertAlmostEqual(c1[i], c3[i]) ### test transformation propagates rbs, beads = IMP.pmi.tools.get_rbs_and_beads(m1_leaves) test_trans = IMP.algebra.get_random_local_transformation( IMP.algebra.Vector3D(0, 0, 0)) IMP.core.transform(rbs[0], test_trans) mdl.update() for p1, p3 in zip(m1_leaves, m3_leaves): c1 = IMP.core.XYZ(p1).get_coordinates() c3 = sym_trans * IMP.core.XYZ(p3).get_coordinates() for i in range(3): self.assertAlmostEqual(c1[i], c3[i])
def test_macro(self): """setting up the representation PMI 1.0 representation. Creates two particles and an harmonic distance restraints between them""" import shutil import itertools m = IMP.Model() r = IMP.pmi.representation.Representation(m) r.create_component("A") r.add_component_beads("A", [(1, 1), (2, 2)]) ps = IMP.atom.get_leaves(r.prot) dr = IMP.pmi.restraints.basic.DistanceRestraint( r, (1, 1, "A"), (2, 2, "A"), 10, 10) dr.add_to_model() rex = IMP.pmi.macros.ReplicaExchange0( m, r, monte_carlo_sample_objects=[r], output_objects=[r, dr], monte_carlo_temperature=1.0, replica_exchange_minimum_temperature=1.0, replica_exchange_maximum_temperature=2.5, number_of_best_scoring_models=10, monte_carlo_steps=10, number_of_frames=10000, write_initial_rmf=True, initial_rmf_name_suffix="initial", stat_file_name_suffix="stat", best_pdb_name_suffix="model", do_clean_first=True, do_create_directories=True, global_output_directory="./test_replica_exchange_macro_output", rmf_dir="rmfs/", best_pdb_dir="pdbs/", replica_stat_file_suffix="stat_replica", em_object_for_rmf=None, replica_exchange_object=None) # check whether the directory is existing, in case remove it try: shutil.rmtree('./test_replica_exchange_macro_output') except OSError: pass rex.execute_macro() # check that each replica index is below the total number of replicas my_index = rex.replica_exchange_object.get_my_index() nreplicas = rex.replica_exchange_object.get_number_of_replicas() temperatures = rex.replica_exchange_object.get_my_parameter("temp") self.assertLess(my_index, nreplicas) # check that each replica has a unique index tf = open( "./test_replica_exchange_macro_output/" + str(my_index) + ".test", "w") tf.write(str(my_index)) tf.close() # sleep to synchronize time.sleep(1) if my_index == 0: for k in range(nreplicas): self.assertTrue( os.path.isfile("./test_replica_exchange_macro_output/" + str(k) + ".test")) #extract the info form the stat files rex_out_files = glob.glob( "./test_replica_exchange_macro_output/stat_replica.*.out") temp_key = "ReplicaExchange_CurrentTemp" maxtf_key = "ReplicaExchange_MaxTempFrequency" mintf_key = "ReplicaExchange_MinTempFrequency" ssr_key = "ReplicaExchange_SwapSuccessRatio" score_key = "score" score_temp_dict = {} avtemps_replicas = [] for f in rex_out_files: o = IMP.pmi.output.ProcessOutput(f) d = o.get_fields( [temp_key, maxtf_key, mintf_key, ssr_key, score_key]) temps = [float(f) for f in d[temp_key]] scores = [float(f) for f in d[score_key]] avtemp = sum(temps) / len(temps) avtemps_replicas.append(avtemp) for n, t in enumerate(temps): s = scores[n] if t not in score_temp_dict: score_temp_dict[t] = [s] else: score_temp_dict[t].append(s) # test that the average temperature per replica are similar for c in itertools.combinations(avtemps_replicas, 2): self.assertAlmostEqual(c[0], c[1], delta=0.05) for t in score_temp_dict: avscore = sum(score_temp_dict[t]) / len(score_temp_dict[t]) #check that the score is the energy of an 1D harmonic oscillator self.assertAlmostEqual(avscore, t / 2, delta=0.1) rex_out_file = "./test_replica_exchange_macro_output/stat." + str( my_index) + ".out" dist_key = "DistanceRestraint_None" mc_nframe_key = "MonteCarlo_Nframe" mc_temp_key = "MonteCarlo_Temperature" rex_temp_key = "ReplicaExchange_CurrentTemp" rex_max_temp_key = "ReplicaExchange_MaxTempFrequency" rex_min_temp_key = "ReplicaExchange_MinTempFrequency" rex_swap_key = "ReplicaExchange_SwapSuccessRatio" rex_score_key = "SimplifiedModel_Total_Score_None" rmf_file_key = "rmf_file" rmf_file_index = "rmf_frame_index" o = IMP.pmi.output.ProcessOutput(rex_out_file) d = o.get_fields([ dist_key, mc_temp_key, mc_nframe_key, rex_temp_key, rex_max_temp_key, rex_min_temp_key, rex_swap_key, rex_score_key, rmf_file_key, rmf_file_index ]) nframes = len(d[mc_nframe_key]) self.assertNotEqual(float(d[mc_nframe_key][-1]), 0) self.assertEqual(map(float, d[mc_temp_key]), [1.0] * nframes) self.assertGreater(float(d[rex_min_temp_key][-1]), 0.0)
def model_pipeline(project): """ Model pipeline for IMP project """ logging.info('model_pipeline title %s!' % project["title"]) #--------------------------- # Define Input Files # The first section defines where input files are located. # The topology file defines how the system components are structurally represented. # target_gmm_file stores the EM map for the entire complex, which has already been converted into a Gaussian mixture model. #--------------------------- datadirectory = project["data_directory"] #"C:/dev/project/py_imp/py_imp/pmi_tut/rnapolii/data/" # C:/Users/adminL/source/repos/py_imp/py_imp/pmi_tut/rnapolii/data/ logging.info('config data_directory %s!' % datadirectory) print('config data_directory %s!' % datadirectory) # Start by getting directory paths #this_path = os.path.dirname(os.path.realpath(__file__)) + "/" #cwd = os.getcwd() this_path = project["data_directory"] mkdir(this_path) print('config this_path %s!' % this_path) # these paths are relative to the topology file pdb_dir = this_path + "data/xtal" fasta_dir = this_path + "data/fasta" gmm_dir = this_path + "data/em" xl_dir = this_path + "data/xl" topo_dir = this_path + "data/topo" #pdb_dir = this_path + "../data/xtal" #fasta_dir = this_path + "../data/fasta" #gmm_dir = this_path + "../data/em" #xl_dir = this_path + "../data/xl" #topo_dir = this_path + "../data/topo" logging.info('this_path %s!' % this_path) print('this_path %s!' % this_path) logging.info('pdb_dir %s!' % pdb_dir) logging.info('fasta_dir %s!' % fasta_dir) logging.info('gmm_dir %s!' % gmm_dir) print('gmm_dir %s!' % gmm_dir) logging.info('xl_dir %s!' % xl_dir) logging.info('topo_dir %s!' % topo_dir) #if not os.path.exists(pdb_dir): # os.makedirs(pdb_dir) #mkdir(pdb_dir) #mkdir(fasta_dir) #mkdir(gmm_dir) #mkdir(xl_dir) #mkdir(topo_dir) topology_file = topo_dir + '/' + project["topology_file"] target_gmm_file = gmm_dir + '/' + project["target_gmm_file"] #logging.info('data_directory %s!' % datadirectory) logging.info('model_pipeline topology_file %s!' % topology_file) logging.info('target_gmm_file %s!' % target_gmm_file) class MSStudioCrosslinks: # Class that converts an MS Studio crosslink file # into a csv file and corresponding IMP CrossLinkDataBase object def __init__(self, infile): self.infile = infile self.xldbkc = self.get_xldbkc() self.xldb = IMP.pmi.io.crosslink.CrossLinkDataBase(self.xldbkc) self.xldb.create_set_from_file(self.infile, self.xldbkc) def get_xldbkc(self): # Creates the keyword converter database to translate MS Studio column names # into IMP XL database keywords xldbkc = IMP.pmi.io.crosslink.CrossLinkDataBaseKeywordsConverter( IMP.pmi.io.crosslink.ResiduePairListParser("MSSTUDIO")) xldbkc.set_site_pairs_key("Selected Sites") xldbkc.set_protein1_key("Protein 1") xldbkc.set_protein2_key("Protein 2") xldbkc.set_unique_id_key("Peptide ID") return xldbkc def parse_infile(self): # Returns a list of each crosslink's attributes as a dictionary. import csv return csv.DictReader(open(self.infile), delimiter=',', quotechar='"') def get_database(self): return self.xldb # Topology file should be in the same directory as this script #topology_file = this_path +"../topology/topology.txt" logging.info('Initialize model') # Initialize model mdl = IMP.Model() # Build the Model Representation Using a Topology File Using the topology file we define the overall topology: we introduce the molecules with their # sequence and their known structure, and define the movers. Each line in the file is a user-defined molecular Domain, # and each column contains the specifics needed to build the system. See the TopologyReader documentation for a full description of the topology file format. #topology file example: #|molecule_name |color |fasta_fn |fasta_id|pdb_fn |chain|residue_range|pdb_offset|bead_size|em_residues_per_gaussian|rigid_body|super_rigid_body|chain_of_super_rigid_bodies| #|Rpb1 |blue |1WCM_new.fasta.txt|1WCM:A |1WCM_map_fitted.pdb|A |1,1140 |0 |20 |0 |1 | 1 | | #|Rpb1 |blue |1WCM_new.fasta.txt|1WCM:A |1WCM_map_fitted.pdb|A |1141,1274 |0 |20 |0 |2 | 1 | # https://integrativemodeling.org/2.10.1/doc/ref/classIMP_1_1pmi_1_1topology_1_1TopologyReader.html #|molecule_name|color|fasta_fn|fasta_id|pdb_fn|chain|residue_range|pdb_offset|bead_size|em_residues_per_gaussian|rigid_body|super_rigid_body|chain_of_super_rigid_bodies|flags| #|Rpb1 |blue |1WCM.fasta|1WCM:A|1WCM.pdb|A|1,1140 |0|10|0|1|1,3|1|| #|Rpb1 |blue |1WCM.fasta|1WCM:A|1WCM.pdb|A|1141,1274|0|10|0|2|1,3|1|| #|Rpb1 |blue |1WCM.fasta|1WCM:A|1WCM.pdb|A|1275,END |0|10|0|3|1,3|1|| # fasta.txt files are what is expected # Read in the topology file. We must handle multiple topology files: meaning we need to handle either consolidate as one OR handle multiple sets of XL csv files # Specify the directory where the PDB files, fasta files and GMM files are logging.info( 'Specify the directory where the PDB files, fasta files and GMM files are' ) toporeader = IMP.pmi.topology.TopologyReader(topology_file, pdb_dir=pdb_dir, fasta_dir=fasta_dir, gmm_dir=gmm_dir) # Use the BuildSystem macro to build states from the topology file bldsys = IMP.pmi.macros.BuildSystem(mdl) # Each state can be specified by a topology file. logging.info('add_state(toporeader)') bldsys.add_state(toporeader) #Building the System Representation and Degrees of Freedom #Here we can set the Degrees of Freedom parameters, which should be optimized according to MC acceptance ratios. There are three kind of movers: Rigid Body, Bead, and Super Rigid Body (super rigid bodies are sets of rigid bodies and beads that will move together in an additional Monte Carlo move). #max_rb_trans and max_rb_rot are the maximum translation and rotation of the Rigid Body mover, max_srb_trans and max_srb_rot are the maximum translation and rotation of the Super Rigid Body mover and max_bead_trans is the maximum translation of the Bead Mover. #The execution of the macro will return the root hierarchy (root_hier) and the degrees of freedom (dof) objects, both of which are used later on. # Build the system representation and degrees of freedom """ root_hier, dof = bldsys.execute_macro(max_rb_trans=project.degree_of_freedom.max_rb_trans, max_rb_rot=project.degree_of_freedom.max_rb_rot, max_bead_trans=project.degree_of_freedom.max_bead_trans, max_srb_trans=project.degree_of_freedom.max_srb_trans, max_srb_rot=project.degree_of_freedom.max_srb_rot) """ logging.info('bldsys.execute_macro') root_hier, dof = bldsys.execute_macro() """ fb = dof.create_flexible_beads(mol.get_non_atomic_residues(), max_trans=bead_max_trans) """ #print(dof.get_rigid_bodies() ) #print(toporeader.get_rigid_bodies() ) outputobjects = [] # Stereochemistry restraints ev = IMP.pmi.restraints.stereochemistry.ExcludedVolumeSphere( included_objects=bldsys.get_molecules()[0].values(), resolution=20) ev.add_to_model() outputobjects.append(ev) crs = [] for mol in bldsys.get_molecules()[0].values(): #dof.create_flexible_beads(mol.get_non_atomic_residues(), # max_trans=bead_max_trans) cr = IMP.pmi.restraints.stereochemistry.ConnectivityRestraint([mol]) cr.add_to_model() crs.append(cr) outputobjects.append(cr) logging.info('IMP.pmi.tools.shuffle_configuration') IMP.pmi.tools.shuffle_configuration( root_hier, max_translation= 100, # raise for larger systems if shuffling fails at niterations, want it ~1.5x size of system in angstrom verbose=True, cutoff=5.0, niterations=100) logging.info(ev.evaluate()) dof.optimize_flexible_beads( 100 ) #if beads are not connecting at initial rmf, increase; number of steps to optimize connectivity logging.info(ev.evaluate()) #TODO: obtain XL filenames from yaml # Convert crosslink file into IMP database #xl_file1 = xl_dir + "/PRC2_BS3.csv" #xl_file2 = xl_dir + "/PRC2_DSS.csv" #xldb1 = MSStudioCrosslinks(xl_file1).get_database() #xldb2 = MSStudioCrosslinks(xl_file2).get_database() #for i in range(len(project.xl_dbA) ): # logging.info(project.xl_dbA[i]) # Getting length of list length = len(project["xl_groupA"]) i = 0 xlList = [] logging.info('Iterating using while loop') # Iterating using while loop while i < length: logging.info(project["xl_groupA"][i]) #"refid","length","slope","resolution","label","weight","crosslink_distance" logging.info(project["xl_groupA"][i]["refid"]) logging.info(project["xl_groupA"][i]["length"]) logging.info(project["xl_groupA"][i]["slope"]) logging.info(project["xl_groupA"][i]["resolution"]) logging.info(project["xl_groupA"][i]["label"]) logging.info(project["xl_groupA"][i]["weight"]) logging.info(project["xl_groupA"][i]["crosslink_distance"]) # Set up crosslinking restraint xlA = XLRestraint( root_hier=root_hier, CrossLinkDataBase=MSStudioCrosslinks( xl_dir + "/" + project["xl_groupA"][i]["refid"]).get_database(), length=project["xl_groupA"][i] ["length"], #midpoint? Double check with Daniel and excel function thing resolution=project["xl_groupA"][i] ["resolution"], #keep 1, lower limit slope=project["xl_groupA"][i] ["slope"], # 0.01 for longer XL and 0.03 for shorter, range - check by making sure midpoint is less than 0.5 e.g 30 * 0.01 label=project["xl_groupA"][i]["label"], filelabel=project["xl_groupA"][i]["label"], weight=project["xl_groupA"][i] ["weight"]) #ignore weight, calculated via IMP logging.info(xlA) xlList.append(xlA) xlA.add_to_model() outputobjects.append(xlA) dof.get_nuisances_from_restraint(xlA) i += 1 for i in range(len(xlList)): logging.info(xlList[i]) """ # Set up crosslinking restraint xl1 = XLRestraint(root_hier=root_hier, CrossLinkDataBase=xldb1, length=30.0, #midpoint? Double check with Daniel and excel function thing resolution=1, #keep 1, lower limit slope=0.01, # 0.01 for longer XL and 0.03 for shorter, range - check by making sure midpoint is less than 0.5 e.g 30 * 0.01 label="DSS", filelabel="DSS_missing", weight=1.) #ignore weight, calculated via IMP xl1.add_to_model() outputobjects.append(xl1) dof.get_nuisances_from_restraint(xl1) xl2 = XLRestraint(root_hier=root_hier, CrossLinkDataBase=xldb2, length=30.0, resolution=1, slope=0.01, label="BS3", filelabel="BS3_missing", weight=1.) xl2.add_to_model() outputobjects.append(xl2) dof.get_nuisances_from_restraint(xl2) """ #xl_rests = [xl1, xl2] + crs xl_rests = xlList + crs logging.info('EM Restraint') #EM Restraint densities = IMP.atom.Selection( root_hier, representation_type=IMP.atom.DENSITIES).get_selected_particles() ''' IMP.isd.gmm_tools.decorate_gmm_from_text( "../data/em/Ciferri_PRC2.50.gmm.txt", target_ps, m, radius_scale=3.0, mass_scale=1.0) ''' #coords=[IMP.core.XYZ(p) for p in target_ps] #print coords #TODO: add in the EM data file processing logic once we have the em data file # https://github.com/salilab/imp/ # github\imp\modules\isd\pyext\src\create_gmm.py # python.exe create_gmm.py ../data/em/Ciferri_CEM_PRC2.map.mrc 50 Ciferri_CEM_PRC2_map.gmm50.txt -m Ciferri_CEM_PRC2_map.gmm50.mrc # Ciferri_CEM_PRC2_map.gmm50.txt # "../data/em/Ciferri_CEM_PRC2_map.gmm50.txt", # alias is gmm_file_ouput.txt # TODO: skip this step if the gmm.txt is absent. logging.info('EM filename check for %s!' % project["target_gmm_file"]) #print('EM filename check for %s!' % project["target_gmm_file"]) if os.path.isfile(target_gmm_file): logging.info('EM file exists %s!' % target_gmm_file) #print('EM file exists %s!' % target_gmm_file) #print('EM file exists %s!' % project["target_gmm_file"]) gemt = IMP.pmi.restraints.em.GaussianEMRestraint( densities, #project["target_gmm_file"], target_gmm_file, scale_target_to_mass=True, slope=0, weight=200.0) gemt.set_label("GaussianEMRestraint") gemt.add_to_model() outputobjects.append(gemt) else: logging.info('skip gemt addition: EM file does NOT exist %s!' % target_gmm_file) print('skip gemt addition: EM file does NOT exist %s!' % target_gmm_file) # Gaussian functions are widely used in statistics to describe the normal distributions, in signal processing to define Gaussian filters # , in image processing where two-dimensional Gaussians are used for Gaussian blurs, and in mathematics to solve heat equations and diffusion equations # and to define the Weierstrass transform. # https://en.wikipedia.org/wiki/Gaussian_function # Electron Microscopy Restraint # The GaussianEMRestraint uses a density overlap function to compare model to data # First the EM map is approximated with a Gaussian Mixture Model (done separately) # Second, the components of the model are represented with Gaussians (forming the model GMM) # Other options: scale_to_target_mass ensures the total mass of model and map are identical # slope: nudge model closer to map when far away # weight: experimental, needed becaues the EM restraint is quasi-Bayesian # #em_components = IMP.pmi.tools.get_densities(root_hier) # substitute em_components with densities in the call given below """ gemt = IMP.pmi.restraints.em.GaussianEMRestraint(densities, target_gmm_file, scale_target_to_mass=True, slope=0.000001, weight=200.0) #gemt.set_label("Ciferri_PRC2") gemt.add_to_model() outputobjects.append(gemt) """ #print("Monte-Carlo Sampling:") logging.info("Monte-Carlo Sampling:") #-------------------------- # Monte-Carlo Sampling #-------------------------- #-------------------------- # Set MC Sampling Parameters #-------------------------- #num_frames = 20000 #num_frames = 50 num_frames = project["sampling_frame"] #if '--test' in sys.argv: num_frames=100 num_mc_steps = 10 logging.info('set states %s!' % project["states"]) logging.info('set sampling_frame %s!' % project["sampling_frame"]) logging.info('set num_frames %s!' % num_frames) logging.info('set output_dir %s!' % project["output_dir"]) logging.info('set num_mc_steps %s!' % num_mc_steps) #TODO: add config setup for these fixed values logging.info('set monte_carlo_temperature=1.0') logging.info('set simulated_annealing=True') logging.info('set simulated_annealing_minimum_temperature=1.0') logging.info('set simulated_annealing_maximum_temperature=2.5') logging.info('set simulated_annealing_minimum_temperature_nframes=200') logging.info('set simulated_annealing_maximum_temperature_nframes=20') logging.info('set replica_exchange_minimum_temperature=1.0') logging.info('set replica_exchange_maximum_temperature=2.5') logging.info('set number_of_best_scoring_models=0') logging.info('set monte_carlo_steps %s!' % num_mc_steps) logging.info('set number_of_frames %s!' % num_frames) logging.info('set global_output_directory %s!' % project["output_dir"]) # https://integrativemodeling.org/2.10.1/doc/ref/classIMP_1_1pmi_1_1macros_1_1ReplicaExchange0.html#a239c4009cc04c70236730479f9f79744 # This object defines all components to be sampled as well as the sampling protocol mc1 = IMP.pmi.macros.ReplicaExchange0( mdl, root_hier=root_hier, monte_carlo_sample_objects=dof.get_movers(), output_objects=outputobjects, crosslink_restraints=xl_rests, # allows XLs to be drawn in the RMF files monte_carlo_temperature=1.0, simulated_annealing=True, simulated_annealing_minimum_temperature=1.0, simulated_annealing_maximum_temperature=2.5, simulated_annealing_minimum_temperature_nframes=200, simulated_annealing_maximum_temperature_nframes=20, replica_exchange_minimum_temperature=1.0, replica_exchange_maximum_temperature=2.5, number_of_best_scoring_models=0, monte_carlo_steps=num_mc_steps, #keep at 10 number_of_frames=num_frames, global_output_directory=project["output_dir"], test_mode=False) # start sampling mc1.execute_macro() #logging.info("GEMT", gemt.evaluate()); #logging.info("XL1", xl1.evaluate(), xl2.evaluate()); for i in range(len(xlList)): logging.info(xlList[i].evaluate()) logging.info("EV", ev.evaluate()) logging.info("CR", cr.evaluate())
def test_molecule_set_behaviour2(self): ''' Here we assert that residue sets are not changed before and after the model build. The current behaviour does not make the test pass. ''' model = IMP.Model() s = IMP.pmi.topology.System(model) st1 = s.create_state() # Read sequences and create Molecules seqs = IMP.pmi.topology.Sequences( IMP.pmi.get_example_path('data/gcp2.fasta')) mol = st1.create_molecule("GCP2", sequence=seqs["GCP2_YEAST"][:120], chain_id='A') # Add structure. This function returns a list of the residues that now # have structure a1 = mol.add_structure(IMP.pmi.get_example_path('data/gcp2.pdb'), res_range=(1, 100), chain_id='A') # Add representations. For structured regions, created a few beads as well as densities # For unstructured regions, create a single bead level and set those up # as densities gmm_prefix = self.get_input_file_name('gcp2_gmm.txt').strip('.txt') mol.add_representation(a1, resolutions=[10, 100], density_prefix=gmm_prefix, density_residues_per_component=20, density_voxel_size=3.0) # before we add the beads representation we chack the consistency # of mol.represented with the list obtained by removing it from the # whole molecule, and store the list of results mol_beads_1 = mol[:] - mol.get_represented() #we will need this later mol_beads_2 = mol[:] - mol.get_represented() status_1 = [] for x in mol: status_1.append(x in mol_beads_1) self.assertEqual(x in mol_beads_1, not x in mol.get_represented()) mol.add_representation(mol_beads_1, resolutions=[10], setup_particles_as_densities=True) before1 = len(mol_beads_1) inter = mol[0:100] & mol_beads_1 mol_beads_1 -= inter after1 = len(mol_beads_1) # these are the number of residues mapped to anty bead self.assertEqual(before1, 83) # these are thwe number of residues mapped in beads but no in the range # [1:100] self.assertEqual(after1, 20) hier = s.build() before2 = len(mol_beads_2) inter = mol[0:100] & mol_beads_2 mol_beads_2 -= inter after2 = len(mol_beads_2) self.assertEqual(before2, before1) #we expect this to work self.assertEqual(after2, after1)