-
Notifications
You must be signed in to change notification settings - Fork 0
/
eric_functions.py
228 lines (175 loc) · 7.65 KB
/
eric_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import dendropy
from dendropy.calculate import treecompare
from dendropy.simulate import treesim
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import os
import re
import numpy as np
import random
folder_dir = "/"
def unweighted_distance(tree1,tree2):
# establish common taxon namespace
tns = dendropy.TaxonNamespace()
# ensure all trees loaded use common namespace
tree1d = dendropy.Tree.get(
data=tree1,
schema='newick',
taxon_namespace=tns)
tree2d = dendropy.Tree.get(
data=tree2,
schema='newick',
taxon_namespace=tns)
## Unweighted Robinson-Foulds distance
return treecompare.symmetric_difference(tree1d, tree2d)
def weighted_distance(tree1,tree2):
# establish common taxon namespace
tns = dendropy.TaxonNamespace()
# ensure all trees loaded use common namespace
tree1d = dendropy.Tree.get(
data=tree1,
schema='newick',
taxon_namespace=tns)
tree2d = dendropy.Tree.get(
data=tree2,
schema='newick',
taxon_namespace=tns)
return treecompare.weighted_robinson_foulds_distance(tree1d, tree2d)
def euclidean_distance(tree1,tree2):
# establish common taxon namespace
tns = dendropy.TaxonNamespace()
# ensure all trees loaded use common namespace
tree1d = dendropy.Tree.get(
data=tree1,
schema='newick',
taxon_namespace=tns)
tree2d = dendropy.Tree.get(
data=tree2,
schema='newick',
taxon_namespace=tns)
return treecompare.euclidean_distance(tree1d, tree2d)
def generate_sim_tree_files(r_tree2,tree_path):
sp_tree_str = """\
[&R] {}""".format(r_tree2)
for i in range(0,304,1):
sp_tree = dendropy.Tree.get(data=sp_tree_str, schema="newick")
gene_to_species_map = dendropy.TaxonNamespaceMapping.create_contained_taxon_mapping(
containing_taxon_namespace=sp_tree.taxon_namespace,
num_contained=1)
gene_tree = treesim.contained_coalescent_tree(containing_tree=sp_tree,
gene_to_containing_taxon_map=gene_to_species_map,default_pop_size = .06)
gene_tree_string = gene_tree.as_string(schema='newick')
#print(gene_tree_string[5:len(gene_tree_string)])
new_sim_folder = folder_dir + tree_path
new_sim_file = new_sim_folder + "/SimMltree_{}.txt".format(i+1)
# if not os.path.exists(new_sim_folder):
# os.mkdir(folder_dir + "SimMlTrees304_{}".format(copy_num))
if os.path.exists(new_sim_file):
os.remove(new_sim_file)
f= open(new_sim_file,"w+")
f.write(gene_tree_string[5:len(gene_tree_string)])
f.close()
def generate_comparison_files(copy_num,sim_file_path,comparison_type):
# data_dir = "SimMlTrees304_{}/".format(copy_num)
tree_array = []
tree_s = ""
for i in range(1,305,1):
#change the file pattern here
file_pat = "/SimMltree_{}.txt".format(i)
dir_tree = sim_file_path + file_pat
tree_string = open(dir_tree).read()
tree_array.append(tree_string)
tree_s += tree_string
tree_file = open(sim_file_path+ "/SimMltree_merged.txt", "w+")
tree_file.write(tree_s)
tree_file.close()
#CREATE A DATAFRAME TO STORE ALL COMPARISONS OF TREES AND DIFFERENT METHODS OF DISTANCE
col_names = ['Tree_comparison',"Num_First_tree","Num_Second_tree", 'weighted_distance']
len1 = len(tree_array) - 1
len2 = int((len1*(len1+1))/2)
print(len2)
tree_data = pd.DataFrame(0, index= range(0,len2), columns = col_names)
#print(tree_data)
#print("START!!!")
count = 0
k = 0
for i in range(k,304,1):
for j in range(k+1,304,1):
#print(str(i+1) + "vs" + str(j+1))
tree_data.loc[count,'Tree_comparison'] = str(i+1) + "vs" + str(j+1)
tree_data.loc[count,'Num_First_tree'] = str(i+1)
tree_data.loc[count,'Num_Second_tree'] = str(j+1)
if comparison_type == "unweighted_distance":
tree_data.loc[count,'unweighted_distance'] = unweighted_distance(tree_array[i],tree_array[j])
elif comparison_type == "weighted_distance":
tree_data.loc[count,'weighted_distance'] = weighted_distance(tree_array[i],tree_array[j])
else:
tree_data.loc[count,'euclidean_distance'] = euclidean_distance(tree_array[i],tree_array[j])
count += 1
k += 1;
tree_data.to_csv(folder_dir+ 'Sim_Trees_distances_comparisons_{}.csv'.format(copy_num),index=False)
return tree_data
#print("END!!")
#merged_file : string that contains all the trees.
def generate_comparison_files_simphy(merged_file,copy_num,comparison_type):
# data_dir = "SimMlTrees304_{}/".format(copy_num)
print("*********************************************")
tree_array = merged_file.split("\n")
tree_s = ""
# print(tree_array)
# for i in range(1,305,1):
# #change the file pattern here
# file_pat = "/SimMltree_{}.txt".format(i)
# dir_tree = sim_file_path + file_pat
# tree_string = open(dir_tree).read()
# tree_array.append(tree_string)
# tree_s += tree_string
# tree_file = open(sim_file_path+ "/SimMltree_merged.txt", "w+")
# tree_file.write(tree_s)
# tree_file.close()
#CREATE A DATAFRAME TO STORE ALL COMPARISONS OF TREES AND DIFFERENT METHODS OF DISTANCE
col_names = ['Tree_comparison',"Num_First_tree","Num_Second_tree", 'weighted_distance']
len1 = len(tree_array) - 1
len2 = int((len1*(len1+1))/2)
# print(len2)
tree_data = pd.DataFrame(0, index= range(0,len2), columns = col_names)
#print(tree_data)
#print("START!!!")
count = 0
# print("count= ",count)
# print("comparisons num= ",len2)
# print("dataframe size= ",len(tree_data))
k = 0
for i in range(k,len(tree_array),1):
for j in range(k+1,len(tree_array),1):
#print(str(i+1) + "vs" + str(j+1))
tree_data.loc[count,'Tree_comparison'] = str(i+1) + "vs" + str(j+1)
tree_data.loc[count,'Num_First_tree'] = str(i+1)
tree_data.loc[count,'Num_Second_tree'] = str(j+1)
if comparison_type == "unweighted_distance":
tree_data.loc[count,'unweighted_distance'] =unweighted_distance(tree_array[i],tree_array[j])
elif comparison_type == "weighted_distance":
tree_data.loc[count,'weighted_distance'] = weighted_distance(tree_array[i],tree_array[j])
else:
tree_data.loc[count,'euclidean_distance'] = euclidean_distance(tree_array[i],tree_array[j])
count += 1
k += 1;
tree_data.to_csv('Sim_Trees_distances_comparisons_{}.csv'.format(copy_num),index=False)
return tree_data
# #print("END!!")
def merge_files(file1,file2,output_path,copy_num):
t_data = open(file1)
s1 = str(t_data.read()) + "\n" #added "\n" so last line of file 1 is separated from first line of file 2
s1
t_data2 = open(file2)
s2 = str(t_data2.read())
s2
s3 = s1 + s2
f= open(output_path + f"{copy_num}.txt","w+")
f.write(s3)
f.close()
return s3
def eric_sum_test(num1,num2):
s = "you sum for the testing is : " + str(num1 + num2)
return s