def makeStarting(tips,RF_norm_start,name,RF_norm_cloud,cloud_size): #Choose starting trees print(name) print("Making starting trees...") # Create random tree. Average branch length of 1. No variation in branch length. Don't print out tree structure t1 = readTree.rand_tree(tips=tips,brl_avg=1,brl_std=None,verbose='F') # Calculate number of NNI moves based on desired normalized RF distance. RF_max = 2*(tips-2) NNI_moves_start = int((RF_max * RF_norm_start)/2) if NNI_moves_start == 0: NNI_moves_start = 1 NNI_moves_cloud = int((RF_max * RF_norm_cloud)/2) # Create second starting tree t2 = readTree.NNI_mult_moves(in_tree=t1,num_moves=NNI_moves_start,node_choice='random',no_dup_start_tree='F', req_min_RF=RF_norm_start) # Write out tree files readTree.write_single_tree(t1,'%s_starting_tree_1.tree' % name) readTree.write_single_tree(t2,'%s_starting_tree_2.tree' % name) # Write out log file # Calculate emperical distance between two start trees as a gut check RF_emp = readTree.rf_unweighted(t1,t2,normalized='T')[1] # write to log file with open('%s.log' % name, "w") as log_file: line1 = "File name: "+str(name) line1b = "Tips: "+str(tips)+", Trees per cloud: "+str(cloud_size) line2 = "Starting trees - RF_input: "+str(RF_norm_start)+", RF_calc: "+str(RF_emp)+", NNI_moves: "+str(NNI_moves_start) line3 = "Cloud of trees - RF_input: "+str(RF_norm_cloud)+", NNI_moves: "+str(NNI_moves_cloud) log_file.write("%s\n%s\n%s" % (line1, line2, line3)) # Pass tree files to next function return t1,t2
def makeStarting(tips, RF_norm_start, name, RF_norm_cloud, cloud_size): #Choose starting trees print(name) print("Making starting trees...") # Create random tree. Average branch length of 1. No variation in branch length. Don't print out tree structure t1 = readTree.rand_tree(tips=tips, brl_avg=1, brl_std=None, verbose='F') # Calculate number of NNI moves based on desired normalized RF distance. RF_max = 2 * (tips - 2) NNI_moves_start = int((RF_max * RF_norm_start) / 2) if NNI_moves_start == 0: NNI_moves_start = 1 # Calc cloud for log file NNI_moves_cloud = int((RF_max * RF_norm_cloud) / 2) # Create second starting tree t2 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) readTree.rf_unweighted(t1, t2, "T") t3 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) d12 = readTree.rf_unweighted(t1, t2, "T")[1] d23 = readTree.rf_unweighted(t2, t3, "T")[1] d13 = readTree.rf_unweighted(t1, t3, "T")[1] print("first t2 to t3 RF" + str(d23)) #t1 and t2 are guarenteed to be RF start apart, same with t2 and t3. So we only need to check t1 and t3. while d23 != RF_norm_start: print("redoing third start tree") t3 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) d23 = readTree.rf_unweighted(t2, t3, "T")[1] print(d23) # Write out tree files readTree.write_single_tree(t1, '%s_starting_tree_1.tree' % name) readTree.write_single_tree(t2, '%s_starting_tree_2.tree' % name) readTree.write_single_tree(t3, '%s_starting_tree_3.tree' % name) # Write out log file rfs = str(d12) + ", " + str(d13) + ", " + str(d23) with open('%s.log' % name, "w") as log_file: line1 = "File name: " + str(name) line1b = "Tips: " + str(tips) + ", Trees per cloud: " + str(cloud_size) line2 = "Starting trees - RF_input: " + str( RF_norm_start) + ", RF_calc: " + str(rfs) + ", NNI_moves: " + str( NNI_moves_start) line3 = "Cloud of trees - RF_input: " + str( RF_norm_cloud) + ", NNI_moves: " + str(NNI_moves_cloud) log_file.write("%s\n%s\n%s" % (line1, line2, line3)) # Pass tree files to next function return t1, t2, t3
cloud_size = 1000 # Desired normalized RF distance between starting trees and for each cloud of trees RF_norm_start = 1.0 RF_norm_cloud = 0.125 # Title run name = ("%stip_%strees_%s_%sstart_taco" % (tips, cloud_size, RF_norm_cloud, RF_norm_start)) ############################################################ #Choose starting trees ############################################################ print("Making starting trees...") # Create random tree. Average branch length of 1. No variation in branch length. Don't print out tree structure t1 = readTree.rand_tree(tips=tips, brl_avg=1, brl_std=None, verbose='F') # Calculate number of NNI moves based on desired normalized RF distance. RF_max = 2 * (tips - 2) NNI_moves_start = int((RF_max * RF_norm_start) / 2) if NNI_moves_start == 0: NNI_moves_start = 1 # Create second starting tree t2 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) # Write out tree files readTree.write_single_tree(t1, '%s_starting_tree_1.tree' % name)
def makeStarting(tips, RF_norm_start_far, RF_norm_start, name, RF_norm_cloud, cloud_size): #Choose starting trees print(name) print("Making starting trees...") #Round the things RF_norm_start_far = round(RF_norm_start_far, 2) RF_norm_start = round(RF_norm_start, 2) # Calculate number of NNI moves based on desired normalized RF distance. RF_max = 2 * (tips - 2) NNI_moves_start = int((RF_max * RF_norm_start) / 2) if NNI_moves_start == 0: NNI_moves_start = 1 NNI_moves_start_far = int((RF_max * RF_norm_start_far) / 2) print("NNI: " + str(NNI_moves_start) + "NNI far: " + str(NNI_moves_start_far)) # Calc cloud for log file NNI_moves_cloud = int((RF_max * RF_norm_cloud) / 2) # Create random tree. Average branch length of 1. No variation in branch length. Don't print out tree structure t1 = readTree.rand_tree(tips=tips, brl_avg=1, brl_std=None, verbose='F') # Create second starting tree t2 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) t3 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) d12 = readTree.rf_unweighted(t1, t2, "T")[1] d23 = readTree.rf_unweighted(t2, t3, "T")[1] d13 = readTree.rf_unweighted(t1, t3, "T")[1] print("first t1 to t2 RF - " + str(round(d12, 2))) print("first t1 to t3 RF - " + str(round(d13, 2))) print("first t2 to t3 RF - " + str(round(d23, 2))) #t1 and t2 are guarenteed to be RF start apart, same with t2 and t3. So we only need to check t1 and t3. while round(d23, 2) != RF_norm_start: print("redoing third start tree - " + name) print(round(d23, 2), RF_norm_start) t3 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start) d23 = readTree.rf_unweighted(t2, t3, "T")[1] print("t1 to t2 RF - " + str(round(d12, 2))) print("t1 to t3 RF - " + str(round(d13, 2))) print("t2 to t3 RF - " + str(round(d23, 2))) # do it all again with another starting tree t4 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start_far, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start_far) # now we need to check d24 and d34 d14 = readTree.rf_unweighted(t1, t4, "T")[1] d24 = readTree.rf_unweighted(t2, t4, "T")[1] d34 = readTree.rf_unweighted(t3, t4, "T")[1] print("first t1 to t4 RF - " + str(round(d14, 2))) print("first t2 to t4 RF - " + str(round(d24, 2))) print("first t3 to t4 RF - " + str(round(d34, 2))) while round(d24, 2) != RF_norm_start_far or round(d34, 2) != RF_norm_start_far: print("redoing fourth start tree, Far Tree - " + name) print(round(d24, 2), round(d34, 2), RF_norm_start_far) t4 = readTree.NNI_mult_moves(in_tree=t1, num_moves=NNI_moves_start_far, node_choice='random', no_dup_start_tree='F', req_min_RF=RF_norm_start_far) d24 = readTree.rf_unweighted(t2, t4, "T")[1] d34 = readTree.rf_unweighted(t3, t4, "T")[1] # Write out tree files readTree.write_single_tree(t1, '%s_starting_tree_1.tree' % name) readTree.write_single_tree(t2, '%s_starting_tree_2.tree' % name) readTree.write_single_tree(t3, '%s_starting_tree_3.tree' % name) readTree.write_single_tree(t4, '%s_starting_tree_4.tree' % name) # Write out log file rfs = str(d12) + ", " + str(d13) + ", " + str(d23) + ", " + str( d14) + ", " + str(d24) + ", " + str(d34) with open('%s.log' % name, "w") as log_file: line1 = "File name: " + str(name) line1b = "Tips: " + str(tips) + ", Trees per cloud: " + str(cloud_size) line2 = "Starting trees - RF_input: " + str( RF_norm_start) + "RF_input_far: " + str( RF_norm_start_far) + ", RF_calc: " + str( rfs) + ", NNI_moves: " + str( NNI_moves_start) + ", NNI_moves_far: " + str( NNI_moves_start_far) line3 = "Cloud of trees - RF_input: " + str( RF_norm_cloud) + ", NNI_moves: " + str(NNI_moves_cloud) log_file.write("%s\n%s\n%s" % (line1, line2, line3)) # Pass tree files to next function return t1, t2, t3, t4
############################## #Input tree options ############################## # From string doo="(P:0.09,(Q:0.07,(X:0.02,((Y:0.03,Z:0.01):0.02,W:0.08):0.06):0.03):0.04)" boo="(P:0.01,(Q:0.01,(X:0.01,((Y:0.01,Z:0.01):0.01,W:0.01):0.01):0.01):0.01)" # Create readTree.tree object in_tree_object = Tree(doo) # From file in_tree_object=readTree.read_nexus('oneTree.t',0) # Create random starting tree in_tree_object = readTree.rand_tree(tips=10,brl_avg=1,brl_std=None,verbose='T') ############################## # Make moves ############################## # Print newick string in_tree_object.newick(in_tree_object.root) # Make NNI move new_tree_object = readTree.NNI(orig_tree=in_tree_object,node_choice='exponential') # Make multiple moves on a single tree new_tree_object = readTree.NNI_mult_moves(in_tree=in_tree,num_moves=1,node_choice='exponential',no_dup_start_tree='T') # Make multiple trees, each with one NNI move from starting tree. Output as nexus file. readTree.NNI_mult_trees(in_tree=in_tree,num_out_trees=10,num_nni_moves=2,out_file='outFile2.t',node_choice='random',no_dup_start_tree='T')