#!/usr/bin/python3 import random , math from pyrosetta import * from pyrosetta.toolbox import * init() #Probability To Make A Choice def Decision(probability): ''' Takes float 0 to 1, probability to accept increases when integer is closer to 1 and decreases when integer is closer to 0 ''' ''' Returns True for accept and False for reject ''' return(random.random() < probability) #------------------------------------------------------------------------------------------------------ pose = pose_from_sequence('A'*10) size = pose.total_residue() #Get total residue number of the structure for samples in range(1,100): #Monte Carlo number of simulations #1- Random Trial Move scorefxn = get_fa_scorefxn() score_before = scorefxn(pose) #Find score of the structure before changing it start = random.randint(1 , size) #Monte Carlo random residue number as start position choice = random.randint(0 , 1) #Setup to randomly choose weather to change Phi or Psi angles if choice == 0: #Choose Phi Angle phi_angle_now = pose.phi(start) #Get current Phi angle phi_angle_new = random.gauss(phi_angle_now , 25) #Randomly move current angel by any angel within 25 degrees, gauss mean = current angle and standard deviation = 25 pose.set_phi(start, phi_angle_new) #Set Phi angle to new value #2- Score Structure score_after = scorefxn(pose) #Find score of the structure after changing it #3- Accept/Reject #Metropolis Criterion - P = 1 to accept all the time when final score is lower than starting score, P closer to 0 = accept less and less but sometimes to escape local energy minima E = score_after - score_before if E >= 0: negE = (math.fabs(E)) * -1 kt = 1 e = math.e P = e**(negE/kt) elif E < 0: P = 1 if Decision(P) == True: #Accept continue else: #Reject pose.set_phi(start, phi_angle_now) #Revert back to original angle (before angle move) else: #Choose Psi Angle psi_angle_now = pose.psi(start) #Get current Psi angle psi_angle_new = random.gauss(psi_angle_now , 25) #Randomly move current angel by any angel within 25 degrees. gauss mean = current angle and standard deviation = 25 pose.set_psi(start, psi_angle_new) #Set Psi angle to new value #2- Score Structure score_after = scorefxn(pose) #Find score of the structure after changing it #3- Accept/Reject #Metropolis Criterion - P = 1 to accept all the time when final score is lower than starting score, P closer to 0 = accept less and less but sometimes to escape local energy minima E = score_after - score_before if E >= 0: negE = (math.fabs(E)) * -1 kt = 1 e = math.e P = e**(negE/kt) elif E < 0: P = 1 if Decision(P) == True: #Accept continue else: #Reject pose.set_psi(start, psi_angle_now) #Revert back to original angle (before angle move)