def main( args ): wts = Weights( args.density_score_wt, args.overlap_score_wt, args.closab_score_wt, args.clash_score_wt ) scorefxn = ScoreFunction( args.density_scorefile, args.overlap_scorefile, args.nonoverlap_scorefile, wts, args.null_frag_score ) for model in range( args.nstruct ): pose = Pose() # empty pose pose.initialization( scorefxn._density_score_dict, args.initialization ) # default initialize by random pose.show_state( "initialization" ) temp = args.starting_temperature steps = args.steps_per_temp mc = MonteCarlo( scorefxn, temp ) trajectory_tracker_dict = {} while temp>=0 or anneal_temp==0: # it would never reach this criteria run_tag = "model_" + str( model ) + "_temp_" + str( temp ) mc.apply( pose, steps ) pose.show_state( run_tag ) #pose.dump_pickle( run_tag + ".pickle" ) trajectory_tracker_dict[ run_tag ] = pose anneal_temp = round( temp*args.cooling_rate, 1 ) temp -= anneal_temp if temp == mc.temperature(): break mc.set_temperature( temp ) pickle.dump( trajectory_tracker_dict, open( "model_" + str( model ) + ".pickle", "w" ) ) pose.show_state( "model_" + str( model ) + "_end", True ) # True for verbose
def main( args ): print print_args( args ) wts = Weights( args.density_score_wt, args.overlap_score_wt, args.closab_score_wt, args.clash_score_wt ) scorefxn = ScoreFunction( args.density_scorefile, args.overlap_scorefile, args.nonoverlap_scorefile, wts, args.null_frag_score ) pose = Pose() # empty pose pose.initialization( scorefxn._density_score_dict ) # default initialize by random temp = args.temperature mc = MonteCarlo( scorefxn, temp ) for each_round in range( 1, args.round+1 ): mc.apply( pose, args.steps ) pose.show_state( temp ) temp -= round( temp*0.1, 2 ) if temp <= 0: break mc.set_temperature( temp ) pose.dump_pickle()
self._density_score_dict[ pos ][ frag_id ] = ( densityScore, rmsd ) else: self._density_score_dict[ pos ] = { frag_id : ( densityScore, rmsd ) } self._density_score_dict[ pos ][ ( mer, pos, 0, 0 ) ] = ( 0, 0 ) # for each position, create a Null fragment fake density score, because you will need to print out density score when you select a null frag for a position pickle.dump( self._density_score_dict, open("density_score_Dict.pickle", "w") ) stdout.write("done\n") if __name__=="__main__": parser = ArgumentParser() parser.add_argument("-d", "--density_scorefile", required=True, help="") parser.add_argument("-t", "--rmsd_threshold", default=2.5, type=float, help="") args = parser.parse_args() dens = DensityScore( args.density_scorefile ) dict = dens._density_score_dict pose = Pose() pose.initialization( dict, "lowrmsd" ) print pose.total_score() #for pos in dens._density_score_dict.keys(): # selected_frag = (9,pos,0,0) # for frag in dens._density_score_dict[ pos ]: # rmsd = dens._density_score_dict[ pos ][ frag ][1] # if rmsd < args.rmsd_threshold and rmsd != 0: # selected_frag = frag # try: # print "after_rotation_frags.%s.????.pdb" %(".".join( map( str, selected_frag ) )), dens._density_score_dict[ pos ][ selected_frag ] # except: # continue
parser.add_argument("--mer", default=9, type=int, help="") parser.add_argument("--verbose", default=False, action="store_true", help="") args = parser.parse_args() print print_args(args) Wts = Weights(args.density_score_wt, args.overlap_score_wt, args.closab_score_wt, args.clash_score_wt) Scorefxn = ScoreFunction( args.density_scorefile, args.overlap_scorefile, args.nonoverlap_scorefile, Wts, args.null_frag_score ) for each_round in range(1, args.round + 1): Pose = Pose() Pose.initialization(Scorefxn.density_score_dict) # default initialize by random Scorefxn.update_pose(Pose) working_temp = args.temperature for each_step in range(args.steps): print "round: %s cycle: %s" % (each_round, each_step) pos = random.sample(Pose.dict.keys(), 1)[0] # pick a residue number to start with null_frag_id = (args.mer, pos, 0, 0) Boltzmann = PerRsdBoltzmann(working_temp) """ After picking a position to optimize, calculate compatibility scores for all the candidate placements at that residue """ for candidate_frag_id in Scorefxn.density_score_dict[
class SimulatedAnnealing: ''' 1. easy to dump the object as a resulting model, and the trajectory? 2. linear cooling rate ''' def __init__( self, scorefxn, steps, temp, cooling_rate ): ''' ''' assert isScoreFxn( scorefxn ) self._log = "" self._scorefxn = scorefxn self._steps = steps # MC steps per annealing cycle self._temperature = temp # declare objects self.__pose = Pose() self._initialization = "random" self._cooling_rate = cooling_rate self._anneal_temp = 0.0 # recover_low ( quench ) self._quench = False self._lowest_score_pose = Pose() self._record_trajectory = False def set_annealing_temperature( self ): ''' ''' self._anneal_temp = round( self._temperature*self._cooling_rate, 1 ) self._temperature -= self._anneal_temp def run( self, runid ): ''' run id is a identification for a model to dump ''' self.__pose.clear() self.__pose.initialization( self._scorefxn.get_density_score_dict(), self._initialization ) # default initialize by random print self.__pose.show_state( "initialization" ) mc = MonteCarlo( self._scorefxn, self._temperature ) mc.apply( self.__pose, self._steps ) # to prevent a silly bug that at the very first state SCORE==0, such that you wouldn't lower than that during high temperatures sampling self._lowest_score_pose = self.__pose.clone() tracker = TrajectoryTracker( runid ) tag = "model_" + str( runid ) while self._temperature >=0 : # it would never reach this criteria if self._quench: self.recover_low() # retrieve the lowest-score pose from previous runs mc.apply( self.__pose, self._steps ) print self.__pose.show_state( tag + "_" + str( self._temperature ) ) self.set_annealing_temperature() # update self._temperature if self._temperature == mc.get_temperature(): break mc.set_temperature( self._temperature ) if self._record_trajectory: tracker.save( self._temperature, self.__pose ) tracker.save( self._temperature, self.__pose ) tracker.dump_pickle( tag ) self.__pose.show_state( tag + "_final", True ) # True for verbose showing all residues states def set_to_quench( self ): self._quench = True def record_trajectory( self ): self._record_trajectory = True def recover_low( self ): # recover_low ''' last accept score pose, and lowest score pose; the MC.recover_low has this is because it uses Metropolis to thermally accept pose ''' # this is for the first around # need to think about a better way to initiate it if not self._lowest_score_pose._initialized: self._lowest_score_pose._initialized = self.__pose # .total_score() method will re-evaluate pose whenever it is called if self.__pose.total_score() > self._lowest_score_pose.total_score(): stdout.write("recover previous found lowest-score pose with score: %s\n" % self._lowest_score_pose.total_score() ) self.__pose = self._lowest_score_pose.clone() else: stdout.write("found a newer lowest-score pose\n") self._lowest_score_pose = self.__pose.clone() def dump( self, id ): return def quench( self ): return