def generate_IMPmodel(rand_init, HiCRestraints, use_HiC=True, use_confining_environment=True, use_excluded_volume=True, single_particle_restraints=None): """ Generates one IMP model :param rand_init: random number kept as model key, for reproducibility. :returns: a model, that is a dictionary with the log of the objective function value optimization, and the coordinates of each particles. """ verbose = VERBOSE # Set the IMP.random_number_generator.seed to get a different reproducible model IMP.random_number_generator.seed(rand_init) #print IMP.random_number_generator() log_energies = [] model = { 'radius': IMP.FloatKey("radius"), 'model': Model(), 'particles': None, 'restraints': None } # 2.6.1 compat model['particles'] = ListSingletonContainer( model['model'], IMP.core.create_xyzr_particles(model['model'], len(LOCI), RADIUS, 100000 / SCALE)) # last number is box size try: model['restraints'] = IMP.RestraintSet(model['model']) # 2.6.1 compat except: pass # OPTIONAL:Set the name of each particle for i in range(0, len(LOCI)): p = model['particles'].get(i) p.set_name(str(LOCI[i])) #print p.get_name() # Separated function for the confining environment restraint if use_confining_environment: # print "\nEnforcing the confining environment restraint" add_confining_environment(model) # Separated function for the bending rigidity restraints if CONFIG['kbending'] > 0.0: # print "\nEnforcing the bending rigidity restraint" theta0 = 0.0 bending_kforce = CONFIG['kbending'] add_bending_rigidity_restraint(model, theta0, bending_kforce) # Add restraints on single particles if single_particle_restraints: for ap in single_particle_restraints: ap[1] = [(c / SCALE) for c in ap[1]] ap[4] /= SCALE # This function is specific for IMP add_single_particle_restraints(model, single_particle_restraints) # Separated function fot the HiC-based restraints if use_HiC: # print "\nEnforcing the HiC-based Restraints" HiCbasedRestraints = HiCRestraints.get_hicbased_restraints() add_hicbased_restraints(model, HiCbasedRestraints) # Separated function for the excluded volume restraint if use_excluded_volume: # print "\nEnforcing the excluded_volume_restraints" excluded_volume_kforce = CONFIG['kforce'] evr = add_excluded_volume_restraint(model, model['particles'], excluded_volume_kforce) if verbose == 1: try: print("Total number of restraints: %i" % (model['model'].get_number_of_restraints())) if use_HiC: print(len(HiCbasedRestraints)) except: print("Total number of restraints: %i" % (model['restraints'].get_number_of_restraints()) ) # 2.6.1 compat if use_HiC: print(len(HiCbasedRestraints)) # Separated function for the Conjugate gradient optimization if verbose == 1: print("\nPerforming the optimization of the Hi-C-based restraints " "using conjugate gradient optimisation\n") conjugate_gradient_optimization(model, log_energies) #try: # log_energies.append(model['model'].evaluate(False)) #except: # log_energies.append(model['restraints'].evaluate(False)) # 2.6.1 compat if verbose >= 1: if verbose >= 2 or not rand_init % 100: print('Model %s IMP Objective Function: %s' % (rand_init, log_energies[-1])) x, y, z, radius = (FloatKey("x"), FloatKey("y"), FloatKey("z"), FloatKey("radius")) result = IMPmodel({ 'log_objfun': log_energies, 'objfun': log_energies[-1], 'x': [], 'y': [], 'z': [], 'radius': None, 'cluster': 'Singleton', 'rand_init': str(rand_init) }) for part in model['particles'].get_particles(): result['x'].append(part.get_value(x) * SCALE) result['y'].append(part.get_value(y) * SCALE) result['z'].append(part.get_value(z) * SCALE) if verbose == 3: print((part.get_name(), part.get_value(x), part.get_value(y), part.get_value(z), part.get_value(radius))) # gets radius from last particle, assuming that all are the same # include in the loop when radius changes... should be a list then result['radius'] = part.get_value(radius) result['radius'] = SCALE / 2 #for log_energy in log_energies: # print "%.30f" % log_energy #print rand_init, log_energies[-1] #stdout.flush() return result # rand_init, result
def _get_restraints(): """ Same function as addAllHarmonic but just to get restraints """ model = {'rk': IMP.FloatKey("radius"), 'model': Model(), 'ps': None} model['ps'] = ListSingletonContainer( IMP.core.create_xyzr_particles(model['model'], len(LOCI), RADIUS, 100000)) model['ps'].set_name("") # set container model['container'] = CONFIG['container'] if model['container']['shape'] == 'cylinder': # define a segment of a given size segment = IMP.algebra.Segment3D( IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(model['container']['height'], 0, 0)) bb = IMP.algebra.get_bounding_box(segment) rb = IMP.container.SingletonsRestraint( IMP.core.BoundingBox3DSingletonScore( IMP.core.HarmonicUpperBound(model['container']['radius'], model['container']['cforce']), bb), model['ps']) model['model'].add_restraint(rb) rb.evaluate(False) # elif model['container']['shape']: # raise noti for i in range(0, len(LOCI)): p = model['ps'].get_particle(i) p.set_name(str(LOCI[i])) #p.set_value(model['rk'], RADIUS) restraints = {} for i in range(len(LOCI)): p1 = model['ps'].get_particle(i) x = p1.get_name() if model['container']['shape'] == 'sphere': ub = IMP.core.HarmonicUpperBound( model['container']['properties'][0], CONFIG['kforce'] * 10) ss = IMP.core.DistanceToSingletonScore( ub, model['container']['center']) rss = IMP.core.SingletonRestraint(ss, p1) model['model'].add_restraint(rss) rss.evaluate(False) for j in range(i + 1, len(LOCI)): p2 = model['ps'].get_particle(j) y = p2.get_name() typ, dist, frc = addHarmonicPair(model, p1, p2, x, y, j, dry=True) if VERBOSE >= 1: stdout.write('%s\t%s\t%s\t%s\t%s\n' % (typ, x, y, dist, frc)) if typ[-1] == 'a': typ = 'H' elif typ[-1] == 'l': typ = 'L' elif typ[-1] == 'u': typ = 'U' elif typ[-1] == 'n': typ = 'C' else: continue restraints[tuple(sorted((x, y)))] = typ[-1], dist, frc return restraints
def generate_IMPmodel(rand_init): """ Generates one IMP model :param rand_init: random number kept as model key, for reproducibility. :returns: a model, that is a dictionary with the log of the objective function value optimization, and the coordinates of each particles. """ verbose = VERBOSE IMP.random_number_generator.seed(rand_init) log_energies = [] model = { 'rk': IMP.FloatKey("radius"), 'model': Model(), 'ps': None, 'pps': None } model['ps'] = ListSingletonContainer( IMP.core.create_xyzr_particles(model['model'], len(LOCI), RADIUS, 100000)) model['ps'].set_name("") # initialize each particles for i in range(0, len(LOCI)): p = model['ps'].get_particle(i) p.set_name(str(LOCI[i])) # computed following the relationship with the 30nm vs 40nm fiber #p.set_value(model['rk'], RADIUS) # Restraints between pairs of LOCI proportional to the PDIST try: # version: 847e65d44da7d06718bcad366b09264c818752d5 model['pps'] = IMP.ParticlePairs() except AttributeError: model['pps'] = IMP.kernel.ParticlePairsTemp() # CALL BIG FUNCTION if rand_init == 1 and verbose == 0.5: verbose = 1 stdout.write("# Harmonic\tpart1\tpart2\tdist\tkforce\n") # set container model['container'] = CONFIG['container'] if model['container']['shape'] == 'cylinder': # define a segment of a given size segment = IMP.algebra.Segment3D( IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(model['container']['height'], 0, 0)) bb = IMP.algebra.get_bounding_box(segment) rb = IMP.container.SingletonsRestraint( IMP.core.BoundingBox3DSingletonScore( IMP.core.HarmonicUpperBound(model['container']['radius'], model['container']['cforce']), bb), model['ps']) model['model'].add_restraint(rb) rb.evaluate(False) # elif model['container']['shape']: # raise noti addAllHarmonics(model) # Setup an excluded volume restraint between a bunch of particles # with radius r = IMP.core.ExcludedVolumeRestraint(model['ps'], CONFIG['kforce']) model['model'].add_restraint(r) if verbose == 3: print "Total number of restraints: %i" % ( model['model'].get_number_of_restraints()) # Set up optimizer lo = IMP.core.ConjugateGradients() lo.set_model(model['model']) o = IMP.core.MonteCarloWithLocalOptimization(lo, LSTEPS) o.set_return_best(True) fk = IMP.core.XYZ.get_xyz_keys() ptmp = model['ps'].get_particles() mov = IMP.core.NormalMover(ptmp, fk, 0.25) o.add_mover(mov) # o.add_optimizer_state(log) # Optimizer's parameters if verbose == 3: print "nrounds: %i, steps: %i, lsteps: %i" % (NROUNDS, STEPS, LSTEPS) # Start optimization and save an VRML after 100 MC moves log_energies.append(model['model'].evaluate(False)) if verbose == 3: print "Start", log_energies[-1] #"""simulated_annealing: preform simulated annealing for at most nrounds # iterations. The optimization stops if the score does not change more than # a value defined by endLoopValue and for stopCount iterations. # @param endLoopCount = Counter that increments if the score of two models # did not change more than a value # @param stopCount = Maximum values of iteration during which the score # did not change more than a specific value # @paramendLoopValue = Threshold used to compute the value that defines # if the endLoopCounter should be incremented or not""" # IMP.fivec.simulatedannealing.partial_rounds(m, o, nrounds, steps) endLoopCount = 0 stopCount = 10 endLoopValue = 0.00001 # alpha is a parameter that takes into account the number of particles in # the model (len(LOCI)). # The multiplier (in this case is 1.0) is used to give a different weight # to the number of particles alpha = 1.0 * len(LOCI) # During the firsts hightemp iterations, do not stop the optimization hightemp = int(0.025 * NROUNDS) for i in range(0, hightemp): temperature = alpha * (1.1 * NROUNDS - i) / NROUNDS o.set_kt(temperature) log_energies.append(o.optimize(STEPS)) if verbose == 3: print i, log_energies[-1], o.get_kt() # After the firsts hightemp iterations, stop the optimization if the score # does not change by more than a value defined by endLoopValue and # for stopCount iterations lownrj = log_energies[-1] for i in range(hightemp, NROUNDS): temperature = alpha * (1.1 * NROUNDS - i) / NROUNDS o.set_kt(temperature) log_energies.append(o.optimize(STEPS)) if verbose == 3: print i, log_energies[-1], o.get_kt() # Calculate the score variation and check if the optimization # can be stopped or not if lownrj > 0: deltaE = fabs((log_energies[-1] - lownrj) / lownrj) else: deltaE = log_energies[-1] if (deltaE < endLoopValue and endLoopCount == stopCount): break elif (deltaE < endLoopValue and endLoopCount < stopCount): endLoopCount += 1 lownrj = log_energies[-1] else: endLoopCount = 0 lownrj = log_energies[-1] #"""simulated_annealing_full: preform simulated annealing for nrounds # iterations""" # # IMP.fivec.simulatedannealing.full_rounds(m, o, nrounds, steps) # alpha = 1.0 * len(LOCI) # for i in range(0,nrounds): # temperature = alpha * (1.1 * nrounds - i) / nrounds # o.set_kt(temperature) # e = o.optimize(steps) # print str(i) + " " + str(e) + " " + str(o.get_kt()) log_energies.append(model['model'].evaluate(False)) if verbose >= 1: if verbose >= 2 or not rand_init % 100: print 'Model %s IMP Objective Function: %s' % (rand_init, log_energies[-1]) x, y, z, radius = (FloatKey("x"), FloatKey("y"), FloatKey("z"), FloatKey("radius")) result = IMPmodel({ 'log_objfun': log_energies, 'objfun': log_energies[-1], 'x': [], 'y': [], 'z': [], 'radius': None, 'cluster': 'Singleton', 'rand_init': str(rand_init) }) for part in model['ps'].get_particles(): result['x'].append(part.get_value(x)) result['y'].append(part.get_value(y)) result['z'].append(part.get_value(z)) if verbose == 3: print(part.get_name(), part.get_value(x), part.get_value(y), part.get_value(z), part.get_value(radius)) # gets radius from last particle, assuming that all are the same result['radius'] = part.get_value(radius) return result # rand_init, result
def generate_IMPmodel(rand_init, HiCRestraints, use_HiC=True, use_confining_environment=True, use_excluded_volume=True, single_particle_restraints=None, initial_conformation=None): """ Generates one IMP model :param rand_init: random number kept as model key, for reproducibility. :returns: a model, that is a dictionary with the log of the objective function value optimization, and the coordinates of each particles. """ verbose = VERBOSE # Set the IMP.random_number_generator.seed to get a different reproducible model IMP.random_number_generator.seed(rand_init) log_energies = [] model = { 'radius': IMP.FloatKey("radius"), 'model': Model(), 'particles': None, 'restraints': None } # 2.6.1 compat if initial_conformation: ps = [] for pos in xrange(len(LOCI)): ps.append(IMP.Particle(model['model'], str(pos))) d = IMP.core.XYZR.setup_particle(ps[-1], float(RADIUS)) d.set_coordinates( IMP.algebra.Vector3D(initial_conformation['x'][pos], initial_conformation['y'][pos], initial_conformation['z'][pos])) d.set_coordinates_are_optimized(True) model['particles'] = ListSingletonContainer(model['model'], ps) else: model['particles'] = ListSingletonContainer( IMP.core.create_xyzr_particles( model['model'], len(LOCI), RADIUS, 100000 / float(CONFIG['resolution'] * CONFIG['scale']))) # last number is box size # OPTIONAL:Set the name of each particle for i in range(0, len(LOCI)): p = model['particles'].get_particle(i) p.set_name(str(LOCI[i])) #model['particles'] = ListSingletonContainer( # IMP.core.create_xyzr_particles(model['model'], len(LOCI), RADIUS, 100000)) # last number is box size try: model['restraints'] = IMP.RestraintSet(model['model']) # 2.6.1 compat except: pass # Separated function for the confining environment restraint: # This function is specific for IMP if use_confining_environment: # print "\nEnforcing the confining environment restraint" add_confining_environment(model) # Separated function for the bending rigidity restraints # This function is specific for IMP if CONFIG['kbending'] > 0.0: # print "\nEnforcing the bending rigidity restraint" theta0 = 0.0 bending_kforce = CONFIG['kbending'] add_bending_rigidity_restraint(model, theta0, bending_kforce) # Anchor particles to fixed points in the model if single_particle_restraints: for ap in single_particle_restraints: ap[1] = [ c / (float(CONFIG['resolution'] * CONFIG['scale'])) for c in ap[1] ] ap[4] /= float(CONFIG['resolution'] * CONFIG['scale']) # This function is specific for IMP add_single_particle_restraints(model, single_particle_restraints) # Separated function fot the HiC-based restraints if use_HiC: # This function is general HiCbasedRestraints = HiCRestraints.get_hicbased_restraints() # print "\nEnforcing the HiC-based Restraints # This function is specific for IMP add_hicbased_restraints(model, HiCbasedRestraints) # Separated function for the excluded volume restraint if use_excluded_volume: # print "\nEnforcing the excluded_volume_restraints" # This function is specific for IMP excluded_volume_kforce = CONFIG[ 'ev_kforce'] if 'ev_kforce' in CONFIG else CONFIG['kforce'] evr = add_excluded_volume_restraint(model, model['particles'], excluded_volume_kforce) if verbose == 1: try: print "Total number of restraints: %i" % ( model['model'].get_number_of_restraints()) if use_HiC: print len(HiCbasedRestraints) except: print "Total number of restraints: %i" % ( model['restraints'].get_number_of_restraints()) # 2.6.1 compat if use_HiC: print len(HiCbasedRestraints) # Separated function for the Conjugate gradient optimization if verbose == 1: print( "\nPerforming the optimization of the Hi-C-based restraints " "using conjugate gradient optimisation\n") conjugate_gradient_optimization(model, log_energies) if verbose >= 1: if verbose >= 2 or not rand_init % 100: print 'Model %s IMP Objective Function: %s' % (rand_init, log_energies[-1]) x, y, z, radius = (FloatKey("x"), FloatKey("y"), FloatKey("z"), FloatKey("radius")) result = IMPmodel({ 'log_objfun': log_energies, 'objfun': log_energies[-1], 'x': [], 'y': [], 'z': [], 'radius': None, 'cluster': 'Singleton', 'rand_init': str(rand_init) }) for part in model['particles'].get_particles(): result['x'].append( part.get_value(x) * float(CONFIG['resolution'] * CONFIG['scale'])) result['y'].append( part.get_value(y) * float(CONFIG['resolution'] * CONFIG['scale'])) result['z'].append( part.get_value(z) * float(CONFIG['resolution'] * CONFIG['scale'])) # for part in model['particles'].get_particles(): # result['x'].append(part.get_value(x)) # result['y'].append(part.get_value(y)) # result['z'].append(part.get_value(z)) if verbose == 3: print(part.get_name(), part.get_value(x), part.get_value(y), part.get_value(z), part.get_value(radius)) # gets radius from last particle, assuming that all are the same # include in the loop when radius changes... should be a list then #result['radius'] = part.get_value(radius) #result['radius'] = RADIUS result['radius'] = float(CONFIG['resolution'] * CONFIG['scale']) / 2 return result # rand_init, result