示例#1
0
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
                                   num_out_trees=c_size,
                                   num_nni_moves=NNI_moves_cloud,
                                   out='list')
cluster2 = readTree.NNI_mult_trees(in_tree=t2,
                                   num_out_trees=c_size,
                                   num_nni_moves=NNI_moves_cloud,
                                   out='list')
# Make a nexus file with starting trees and cloud trees
readTree.list_to_out(cluster1, cluster2, '%s_cloud.tree' % name)

############################################################
# Print info to log file
############################################################
print("Calculating stats on trees...")
# Calculate emperical distance between two start trees
RF_emp = readTree.rf_unweighted(t1, t2, normalized='T')[1]
# Calculate average density of each cloud
RF_cloud1 = readTree.cluster_density_avg(in_file='%s_cloud.tree' % name,
                                         NNI_trees=int(cloud_size) - 1,
                                         starting_tree_number=0)
RF_cloud2 = readTree.cluster_density_avg(in_file='%s_cloud.tree' % name,
                                         NNI_trees=int(cloud_size) - 1,
                                         starting_tree_number=cloud_size)
# 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(
示例#4
0
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
示例#5
0
############################## 
# 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')

############################## 
# Look at and compare trees
############################## 

# Print newick string
new_tree_object.newick(new_tree_object.root)

# View tree in terminal along with newick string
readTree.view_phylo(new_tree_object)

# Check that all trees have the right number of moves from start tree
readTree.compare_tree_file(in_file='outFile1.t',total_trees=11,distance_metric="uRF")

# Compare RF distance between two trees.
readTree.rf_unweighted(in_tree_object,new_tree_object,normalized='T')