def _test_sample_map(self): """Check that sampling particles works""" erw = IMP.em.EMReaderWriter() resolution = 1. voxel_size = 1. model_map = IMP.em.SampledDensityMap( self.particles, resolution, voxel_size, self.rad_key, self.weight_key) IMP.em.write_map(model_map, "a.em", erw) for p in self.particles: v = IMP.core.XYZ(p).get_coordinates() self.assertGreater(model_map.get_value(v), 0.6, "map was not sampled correctly") model_map.calcRMS() mapfile = IMP.create_temporary_file_name("xxx.em") IMP.em.write_map(model_map, mapfile, erw) em_map = IMP.em.DensityMap() em_map = IMP.em.read_map(mapfile, erw) em_map.calcRMS() self.assertAlmostEqual(em_map.get_header().rms, model_map.get_header().rms, delta=.000001, msg="standard deviations of maps differ") os.unlink(mapfile)
def setUp(self): IMP.test.TestCase.setUp(self) self.db = database.Database2() self.fn = IMP.create_temporary_file_name('test.db') self.db.create(self.fn, True) self.db.connect(self.fn) self.tables = ["mytable1", "mytable2"] self.column_names = ["id", "property", "value"] self.column_types = [int, str, float] for t in self.tables: self.db.create_table(t, self.column_names, self.column_types)
def setUp(self): IMP.test.TestCase.setUp(self) self.db = database.Database2() self.fn = IMP.create_temporary_file_name("test.db") self.db.create(self.fn, True) self.db.connect(self.fn) self.tables = ["mytable1", "mytable2"] self.column_names = ["id", "property", "value"] self.column_types = [int, str, float] for t in self.tables: self.db.create_table(t, self.column_names, self.column_types)
def setUp(self): IMP.test.TestCase.setUp(self) self.db = database.Database2() self.fn = IMP.create_temporary_file_name('test.db') self.db.create(self.fn, True) self.db.connect(self.fn) # Speed up tests by not waiting for the disk self.db.cursor.execute('PRAGMA synchronous=OFF') self.tables = ["mytable1", "mytable2"] self.column_names = ["id", "property", "value"] self.column_types = [int, str, float] for t in self.tables: self.db.create_table(t, self.column_names, self.column_types)
m.set_log_level(IMP.base.SILENT) # make sure to break up the mt = IMP.domino.get_merge_tree([r], pst) try: IMP.show_graphviz(mt) except: print "Unable to display graph using 'dot'" rc = IMP.domino.RestraintCache(pst) rc.add_restraints([r]) filters = [IMP.domino.RestraintScoreSubsetFilterTable(rc), IMP.domino.ExclusionSubsetFilterTable(pst)] leaf_table = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters) # create a database to store the results name = IMP.create_temporary_file_name("assignments", ".hdf5") root = RMF.HDF5.create_file(name) # recurse down the tree getting the assignments and printing them def get_assignments(vertex): on = mt.get_out_neighbors(vertex) ss = mt.get_vertex_name(vertex) print "computing assignments for", ss ssn = str(ss) dataset = root.add_child_index_data_set_2d(ssn) dataset.set_size([0, len(ss)]) mine = IMP.domino.WriteHDF5AssignmentContainer( dataset, ss, pst.get_particles(), ssn) if len(on) == 0:
def test_init_from_rmf(self): """ Testing whether initialize_positions_from_rmf indeed restores the beads coordinates correctly """ IMP.set_log_level( IMP.SILENT ); test_util.test_protobuf_installed(self) # First simulation with one seed: output_pb1= self.get_tmp_file_name( "output1.pb" ); output_rmf1= IMP.create_temporary_file_name( "output", ".rmf" ); print("Starting first simulation with RMF file " + output_rmf1) sd = self._make_and_run_simulation( output_pb1, output_rmf1, seed = 1) e1 = sd.get_bd().get_scoring_function().evaluate(False) print ("*** After first simulation") print ("Energy %.2f" % e1) coordsI = self._get_beads_coords( sd ) sd = None # Second simulation with another seed: output_pb2= self.get_tmp_file_name( "output2.pb" ); output_rmf2= IMP.create_temporary_file_name( "output2", ".rmf" ); print("*** Starting second simulation with RMF file " + output_rmf2) print("YYY") sd = self._make_and_run_simulation( output_pb2, output_rmf2, seed = 2) print("XXX") e2 = sd.get_bd().get_scoring_function().evaluate(False) print ("*** After second simulation") print ("Energy %.2f" % e2) coordsII = self._get_beads_coords( sd ) # Restoration from first simulation through rmf file: print("*** Initializing positions from RMF file " + output_rmf1) fl= RMF.open_rmf_file_read_only(output_rmf1) sd.initialize_positions_from_rmf( fl ) e3 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e3) self.assertAlmostEqual(e1, e3, delta=0.0001) self.assertNotAlmostEqual(e1, e2, delta=0.0001) coordsIII = self._get_beads_coords( sd ) # make sure that all coordinates were restored and as sanity # check control, also that the second simulation is different than # the first one: for i in range( len( coordsI ) ): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsIII[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsII[i][j], delta=0.00001) # Simulate more to scramble stuff sd.get_bd().optimize( 1000 ) e4 = sd.get_bd().get_scoring_function().evaluate(False) print ("Energy %.2f" % e4) coordsIV = self._get_beads_coords( sd ) # Restoration from first simulation through output protobuf file: print("*** Initializing positions from ProtoBuf file " + output_pb1) f=open(output_pb1, "rb") config= IMP.npctransport.Output() config.ParseFromString(f.read()) bch = RMF.BufferConstHandle( config.rmf_conformation ) fch= RMF.open_rmf_buffer_read_only( bch ) sd.initialize_positions_from_rmf( fch ) e5 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e5) self.assertAlmostEqual(e1, e5, delta=0.00001) self.assertNotAlmostEqual(e1, e4, delta=0.00001) coordsV = self._get_beads_coords( sd ) for i in range( len( coordsI ) ): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsV[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsIV[i][j], delta=0.00001)
mc.optimize(number_of_mc_steps) if scoring_function.get_had_good_score(): configuration_set.save() return configuration_set model = IMP.Model() universe, restraints, excluded_volume_particles, optimized_particles = create_representation(model) encode_data_as_restraints(universe, restraints) configuration_set = get_configurations(model, restraints, excluded_volume_particles, optimized_particles) print("Found", configuration_set.get_number_of_configurations(), "good configurations") # now lets save them all to a file rmf_file_name = IMP.create_temporary_file_name("nup84", ".rmf") rmf = RMF.create_rmf_file(rmf_file_name) # we want to see the scores of the various restraints also IMP.rmf.add_restraints(rmf, restraints) # and the actual structures IMP.rmf.add_hierarchy(rmf, universe) for i in range(0, configuration_set.get_number_of_configurations()): configuration_set.load_configuration(i) # align all the configurations with the first so they display nicely # if we want to be fancy we can account for flips too if i == 0: base_coords = [IMP.core.XYZ(p).get_coordinates() for p in optimized_particles] else: tr = IMP.algebra.get_transform_taking_first_to_second(optimized_particles, base_coords)
m.set_log_level(IMP.base.SILENT) # make sure to break up the mt = IMP.domino.get_merge_tree([r], pst) try: IMP.show_graphviz(mt) except: print("Unable to display graph using 'dot'") rc = IMP.domino.RestraintCache(pst) rc.add_restraints([r]) filters = [IMP.domino.RestraintScoreSubsetFilterTable(rc), IMP.domino.ExclusionSubsetFilterTable(pst)] leaf_table = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters) # create a database to store the results name = IMP.create_temporary_file_name("assignments", ".hdf5") root = RMF.HDF5.create_file(name) # recurse down the tree getting the assignments and printing them def get_assignments(vertex): on = mt.get_out_neighbors(vertex) ss = mt.get_vertex_name(vertex) print("computing assignments for", ss) ssn = str(ss) dataset = root.add_child_index_data_set_2d(ssn) dataset.set_size([0, len(ss)]) mine = IMP.domino.WriteHDF5AssignmentContainer( dataset, ss, pst.get_particles(), ssn) if len(on) == 0:
#!/usr/bin/env python from __future__ import print_function import IMP from IMP.npctransport import create_range, Configuration import sys if len(sys.argv) > 1: outfile = sys.argv[1] else: outfile = IMP.create_temporary_file_name("config", "pb") def get_basic_config(): config = Configuration() IMP.npctransport.set_default_configuration(config) config.statistics_fraction.lower = 0.9 # config.dump_interval = 1 config.interaction_k.lower = 10 config.interaction_range.lower = 1 # create_range(config.backbone_k, .2, 1, 10 config.backbone_k.lower = 1 # config.time_step_factor.lower = 0.3 config.time_step_factor.lower = 3 # create_range(config.rest_length_factor, .5, 1, 10) config.excluded_volume_k.lower = 20 config.nonspecific_range.lower = 2 config.nonspecific_k.lower = 0.01 config.slack.lower = 8 config.number_of_trials = 1 config.dump_interval_ns = 0.1 config.simulation_time_ns = 500
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))) mapfile = IMP.create_temporary_file_name('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 ...") os.unlink(mapfile)
IMP.atom.Hierarchy.setup_particle(p) d = IMP.core.XYZR.setup_particle(p) d.set_radius(10) d.set_coordinates_are_optimized(True) IMP.atom.Diffusion.setup_particle(p) IMP.atom.Mass.setup_particle(p, 1) slabps = IMP.npctransport.SlabWithCylindricalPorePairScore(k) r = IMP.core.PairRestraint( m, slabps, [p_slab.get_index(), p.get_index()], "slab") bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(-100, -100, -100), IMP.algebra.Vector3D(100, 100, 100)) bbss = IMP.core.BoundingBox3DSingletonScore(IMP.core.HarmonicUpperBound(0, 10), bb) bbr = IMP.core.SingletonRestraint(m, bbss, p.get_index(), "bb") nm = IMP.create_temporary_file_name("slab", ".rmf") rmf = RMF.create_rmf_file(nm) IMP.rmf.add_hierarchies(rmf, [p]) bbg = IMP.display.BoundingBoxGeometry(bb) sg = IMP.npctransport.SlabWithCylindricalPoreWireGeometry(height, radius, 100) sg.set_was_used(True) sgs = sg.get_components() # silliness we have to do for now IMP.rmf.add_static_geometries(rmf, [bbg] + sgs) IMP.rmf.add_restraints(rmf, [bbr, r]) os = IMP.rmf.SaveOptimizerState(m, rmf) bd = IMP.atom.BrownianDynamics(m) bd.set_scoring_function([bbr, r]) bd.set_log_level(IMP.SILENT)
return configuration_set model = IMP.Model() universe, restraints, excluded_volume_particles, optimized_particles = create_representation( model) encode_data_as_restraints(universe, restraints) configuration_set = get_configurations(model, restraints, excluded_volume_particles, optimized_particles) print("Found", configuration_set.get_number_of_configurations(), "good configurations") # now lets save them all to a file rmf_file_name = IMP.create_temporary_file_name("nup84", ".rmf") rmf = RMF.create_rmf_file(rmf_file_name) # we want to see the scores of the various restraints also IMP.rmf.add_restraints(rmf, restraints) # and the actual structures IMP.rmf.add_hierarchy(rmf, universe) for i in range(0, configuration_set.get_number_of_configurations()): configuration_set.load_configuration(i) # align all the configurations with the first so they display nicely # if we want to be fancy we can account for flips too if i == 0: base_coords = [IMP.core.XYZ(p).get_coordinates() for p in optimized_particles] else:
def test_init_from_rmf(self): """ Testing whether initialize_positions_from_rmf indeed restores the beads coordinates correctly """ IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) # First simulation with one seed: output_pb1 = self.get_tmp_file_name("output1.pb") output_rmf1 = IMP.create_temporary_file_name("output", ".rmf") print("Starting first simulation with RMF file " + output_rmf1) sd = self._make_and_run_simulation(output_pb1, output_rmf1, seed=1) e1 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After first simulation") print("Energy %.2f" % e1) coordsI = self._get_beads_coords(sd) sd = None # Second simulation with another seed: output_pb2 = self.get_tmp_file_name("output2.pb") output_rmf2 = IMP.create_temporary_file_name("output2", ".rmf") print("*** Starting second simulation with RMF file " + output_rmf2) print("YYY") sd = self._make_and_run_simulation(output_pb2, output_rmf2, seed=2) print("XXX") e2 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After second simulation") print("Energy %.2f" % e2) coordsII = self._get_beads_coords(sd) # Restoration from first simulation through rmf file: print("*** Initializing positions from RMF file " + output_rmf1) fl = RMF.open_rmf_file_read_only(output_rmf1) sd.initialize_positions_from_rmf(fl) e3 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e3) self.assertAlmostEqual(e1, e3, delta=0.0001) self.assertNotAlmostEqual(e1, e2, delta=0.0001) coordsIII = self._get_beads_coords(sd) # make sure that all coordinates were restored and as sanity # check control, also that the second simulation is different than # the first one: for i in range(len(coordsI)): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsIII[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsII[i][j], delta=0.00001) # Simulate more to scramble stuff sd.get_bd().optimize(1000) e4 = sd.get_bd().get_scoring_function().evaluate(False) print("Energy %.2f" % e4) coordsIV = self._get_beads_coords(sd) # Restoration from first simulation through output protobuf file: print("*** Initializing positions from ProtoBuf file " + output_pb1) f = open(output_pb1, "rb") config = IMP.npctransport.Output() config.ParseFromString(f.read()) bch = RMF.BufferConstHandle(config.rmf_conformation) fch = RMF.open_rmf_buffer_read_only(bch) sd.initialize_positions_from_rmf(fch) e5 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e5) self.assertAlmostEqual(e1, e5, delta=0.00001) self.assertNotAlmostEqual(e1, e4, delta=0.00001) coordsV = self._get_beads_coords(sd) for i in range(len(coordsI)): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsV[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsIV[i][j], delta=0.00001)
p= IMP.Particle(m) IMP.atom.Hierarchy.setup_particle(p) d=IMP.core.XYZR.setup_particle(p) d.set_radius(10) d.set_coordinates_are_optimized(True) IMP.atom.Diffusion.setup_particle(p) IMP.atom.Mass.setup_particle(p, 1) slabps= IMP.npctransport.SlabWithCylindricalPorePairScore(k) r= IMP.core.PairRestraint(m, slabps, [p_slab.get_index(),p.get_index()], "slab") bb= IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(-100, -100, -100), IMP.algebra.Vector3D(100, 100, 100)) bbss= IMP.core.BoundingBox3DSingletonScore(IMP.core.HarmonicUpperBound(0,10), bb) bbr= IMP.core.SingletonRestraint(m, bbss, p.get_index(), "bb") nm=IMP.create_temporary_file_name("slab", ".rmf") rmf= RMF.create_rmf_file(nm) IMP.rmf.add_hierarchies(rmf, [p]) bbg= IMP.display.BoundingBoxGeometry(bb) sg= IMP.npctransport.SlabWithCylindricalPoreWireGeometry(height, radius, 100) sg.set_was_used(True); sgs= sg.get_components() # silliness we have to do for now IMP.rmf.add_static_geometries(rmf, [bbg]+sgs) IMP.rmf.add_restraints(rmf, [bbr, r]) os= IMP.rmf.SaveOptimizerState(m, rmf) bd=IMP.atom.BrownianDynamics(m) bd.set_scoring_function([bbr, r]) bd.set_log_level(IMP.SILENT)
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))) mapfile = IMP.create_temporary_file_name('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 ...") os.unlink(mapfile)
p_slab = IMP.Particle(m, "slab") pi_slab = p_slab.get_index() slab = IMP.npctransport.SlabWithCylindricalPore.setup_particle( p_slab, height, radius) p = IMP.Particle(m) pi = p.get_index() d = IMP.core.XYZR.setup_particle(p) d.set_radius(10) d.set_coordinates_are_optimized(True) IMP.atom.Mass.setup_particle(p, 1) IMP.atom.Hierarchy.setup_particle(p) slabps = IMP.npctransport.SlabWithCylindricalPorePairScore(1) r = IMP.core.PairRestraint(m, slabps, [pi_slab, pi], "slab") r.set_log_level(IMP.WARNING) nm = IMP.create_temporary_file_name("display_slab", ".pym") w = IMP.display.create_writer(nm) if use_rmf: rnm = IMP.create_temporary_file_name("display_slab", ".rmf") rmf = RMF.create_rmf_file(rnm) sg = IMP.npctransport.SlabWithCylindricalPoreWireGeometry(height, radius, 100) sg.set_was_used(True) if use_rmf: sg.set_color(IMP.display.Color(0, 1, 0)) sgs = sg.get_components() nh = rmf.get_root_node().add_child("slab", RMF.GEOMETRY) IMP.rmf.add_geometries(nh, sgs) w.add_geometry(sg) bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(sz, sz, sz))