def dijkstra(G, s): """no negative edge""" init_single_source(G, s) S = {} Q = [] # min-priority queue # added to conditionally push node to Q seen = {} c = count() pop = heappop push = heappush push(Q, (s.d, next(c), s)) while Q: dist, _, u = pop(Q) if u in S: continue # shortest path for 'u' are calculated over before checking adj S[u] = dist for v in G.adj[u]: w = G.adj[u][v].get('w', 1) uv_dist = dist + w if v not in S and (v not in seen or uv_dist < seen[v]): relax(u, v, w) push(Q, (v.d, next(c), v)) return S
def dag_shortest_path(G, s): """no cycle""" vertices_sorted = topological_sort(G) init_single_source(G, s) for u in vertices_sorted: for v in G.adj[u]: relax(u, v, G.adj[u][v]['w'])
def dijkstra(self, g, s): initialize_single_source(g, s) S = [] Q = [v for v in g.get_vertices()] while len(Q): u = extract_min(Q) u.set_visitado() S.append(u) for v in u.get_vertices_adjacentes(): if v.get_visitado(): continue relax(u, v)
def bellman_ford(self, g, s): initialize_single_source(g, s) for u in g.get_vertices(): for u, v in g.get_arestas(): if v.get_visitado(): continue relax(u, v) for u, v in g.get_arestas(): if v.get_distancia() > u.get_distancia() + u.get_peso(v): return False return True
def simulate(self, sim_index: int, seed: int = 9001) -> Tuple[int, float, float]: """ Run full MCMC simulation from start_temp to end_temp. Be sure to save the best (lowest-energy) structure, so you can access it after. It is also a good idea to track certain variables during the simulation (temp, energy, and more). :param sim_index: simulation index :param seed: int :return: log information """ random.seed(seed) log = {"temperature": [], "energy": []} while True: if self.step(): # store best pdb sim_path, log_folder_path, sim_index = self.storeSim( self.protein, log, sim_index) # calculate relaxed, cd to the folder!! cur_dir = os.getcwd() os.chdir(sim_path) protein, rmsd, score = utils.relax("best.pdb", "target.pdb") os.chdir(cur_dir) break else: # keep track of log log['temperature'].append(self.temp) log['energy'].append(self.compute_energy()) return sim_index, score, rmsd
def bellman_ford(G, s): """no negative cycle""" init_single_source(G, s) # for every node in G for u in G.node[:-1]: # for every edge in G for u, vs in G.adj.items(): for v in vs: w = G.adj[u][v]['w'] relax(u, v, w) for u in G.node: for v in G.adj[u]: w = G.adj[u][v]['w'] if v.d > u.d + w: # when w is negative print("Error", u, v, w) return False return True
def main(): #Read in arguments from the command line parser = argparse.ArgumentParser( description='Performs ab initio protein structure prediction') parser.add_argument('--fasta', help='name of the fasta file containing the sequence') parser.add_argument( '--logdir', help='directory to save all log files', default='.') #changed so does not have the / afterwards parser.add_argument('--nsims', type=int, help='number of simulations', default=1) parser.add_argument('--nfrags', type=int, help='number of fragments to sample at each iteration', default=3) parser.add_argument('--anneal_rate', type=float, help='temperature annealing parameter', default=0.999) args = parser.parse_args() #load sequence from fasta file my_file = open(args.fasta) my_sequence = my_file.readlines() my_sequence = my_sequence[1].strip() my_file.close() #initialize protein into extended conformation my_protein = Protein(sequence=my_sequence) #set up FragmentSets for later use protein_name = args.fasta[:-6] fragfile_9 = protein_name + '_9mers.frag' rmsdfile_9 = protein_name + '_9mers.rmsd' fragment_set_9mer = FragmentSet(fragfile_9, rmsdfile_9) fragfile_3 = protein_name + '_3mers.frag' rmsdfile_3 = protein_name + '_3mers.rmsd' fragment_set_3mer = FragmentSet(fragfile_3, rmsdfile_3) #create lists for saving certain information about each simulation sim_numbers = [] sim_rmsds = [] sim_energies = [] sim_proteins = [] #loop through 10 sims for sim_number in range(1, args.nsims + 1): #create FragmentSampler object and run coarse simulation with 9mers coarse_sampler = MCMCSampler(my_protein, fragment_set_9mer, nmers=9, start_temp=100, end_temp=1, nfrags=3, anneal_rate=args.anneal_rate) coarse_result = coarse_sampler.simulate() #create FragmentSampler object and run refinement simulation with 3mers fine_sampler = MCMCSampler(coarse_sampler.best_protein, fragment_set_3mer, nmers=3, start_temp=1, end_temp=0.1, nfrags=3, anneal_rate=args.anneal_rate) fine_result = fine_sampler.simulate() #save the final structure from the simulation fine_sampler.best_protein.save_pdb(filename='best.pdb') #save a log_file from the simulation total_result = pd.concat([ coarse_result, fine_result ]) #might need some fixing depending on what the quiz asks for total_result.to_csv( (args.logdir + '/sim_' + str(sim_number) + '_log.txt'), sep='\t') #relax the simulated structure relaxed_protein, final_rmsd, final_energy = utils.relax( pdb='best.pdb', native=args.fasta.split(sep='.')[0] + '.pdb') #log some parameters...sim_number, energy, rmsd sim_numbers.append(sim_number) sim_rmsds.append(final_rmsd) sim_energies.append(final_energy) sim_proteins.append(relaxed_protein) #return sim log sim_log = pd.DataFrame(list(zip(sim_numbers, sim_energies, sim_rmsds)), columns=['sim_number', 'energy', 'rmsd']) sim_log.to_csv( (args.logdir + '/simulation_summary.txt'), sep='\t', index=False ) #may need to be tweaked, i.e. removing indices in first column