-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunPaml_BranchTests.py
241 lines (163 loc) · 7.71 KB
/
RunPaml_BranchTests.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
229
230
231
232
233
234
235
236
237
238
239
#Created on 7/15/2014
__author__ = 'Juan Ugalde'
#TODO
#Clean and document the script
#Incomplete!
def run_branch_test(cluster_name, treefile, alignment, folder_temp, folder_plots):
from ete2 import EvolTree
from ete2.treeview.layouts import evol_clean_layout
import os
from collections import defaultdict
import math
from scipy.stats import chi2
print "Processing cluster: " + cluster_name
tree = EvolTree(treefile)
tree.link_to_alignment(alignment, alg_format="fasta", nucleotides=True)
#Create temporal folder
temp_cluster_folder = folder_temp + "/" + cluster_name
if not os.path.exists(temp_cluster_folder):
os.makedirs(temp_cluster_folder)
tree.workdir = temp_cluster_folder
#Run M0 as the null model
tree.run_model("M0")
#Look at the site selection on each branch
printed_tree = 0
i = 0
#Output list with the results
output_list = []
for node in tree.iter_descendants():
#Mark the tree for the leaf under analysis
tree.mark_tree([node.node_id], marks=["#1"])
#Use the node id as folder name
temp_leaf_name = str(node.node_id)
print "Processing: " + cluster_name + " " + temp_leaf_name + " " + ",".join(node.get_leaf_names())
#Run computation of each model.
#From the notes on ETE:
# to organize a bit, we name model with the name of the marked node
# any character after the dot, in model name, is not taken into account
# for computation. (have a look in /tmp/ete2.../bsA.. directory)
tree.run_model("bsA." + temp_leaf_name)
tree.run_model("bsA1." + temp_leaf_name)
bsA = tree.get_evol_model("bsA." + temp_leaf_name)
bsA1 = tree.get_evol_model("bsA1." + temp_leaf_name)
ps_sites = defaultdict()
total_sites = 0
sites_over_95 = 0
for s in range(len(bsA.sites['BEB']['aa'])):
p_value_site = float(bsA.sites['BEB']['p2'][s])
if p_value_site > 0.50:
ps_sites[s] = [bsA.sites['BEB']['aa'][s], bsA.sites['BEB']['p2'][s]]
total_sites += 1
if p_value_site > 0.95:
sites_over_95 += 1
#ps = float(tree.get_most_likely("bsA." + temp_leaf_name, "bsA1." + temp_leaf_name))
rx = float(tree.get_most_likely("bsA1." + temp_leaf_name, "M0"))
lrt_value = 2 * math.fabs(bsA1.lnL - bsA.lnL) # LRT test value
ps = 1 - chi2.cdf(lrt_value, 1) # p-value based on chi-square
test_status = None
#Evidence of positive selection in the branch
omega_value = float(bsA.classes['foreground w'][2])
proportion_sites = float(bsA.classes['proportions'][2])
#Plot file
plot_file = folder_plots + "/" + cluster_name
if ps < 0.05 and omega_value > 1:
#Save plots, both in jpg and svg of the clusters with evidence of positive selection
test_status = "Positive"
if printed_tree == 0:
#tree.render(plot_file + ".svg", layout=evol_clean_layout)
#tree.render(plot_file + ".jpg", layout=evol_clean_layout)
printed_tree = 1
else:
continue
elif rx < 0.05 and ps >= 0.05:
test_status = "Relaxed"
else:
#print "no signal"
test_status = None
#Remove marks on the tree
tree.mark_tree(map(lambda x: x.node_id, tree.get_descendants()), marks=[''] * len(tree.get_descendants()),
verbose=False)
result_entry = [cluster_name, node.node_id, omega_value, proportion_sites, ps, test_status,
total_sites, sites_over_95, ",".join(node.get_leaf_names())]
# print result_entry
#print ps_sites
#node_results[node.node_id] = [result_entry, ps_sites]
output_list = [result_entry, ps_sites]
return output_list
if __name__ == '__main__':
import os
import argparse
from collections import defaultdict
import multiprocessing
from ete2 import EvolTree
from ete2.treeview.layouts import evol_clean_layout
import shutil
import sys
program_description = "Script that takes a list of clusters, their trees and nucleotide alignments and run the" \
"site-branch test on them." \
"This script will test all the branches on the tree."
parser = argparse.ArgumentParser(description=program_description)
parser.add_argument("-c", "--cluster_list", type=str, help="Cluster file", required=True)
parser.add_argument("-n", "--align_folder", type=str, help="Alignment folder", required=True)
parser.add_argument("-t", "--tree_folder", type=str, help="Tree folder", required=True)
parser.add_argument("-o", "--output_directory", type=str, help="Output folder", required=True)
parser.add_argument("-p", "--num_processors", type=int, help="Number of processors to use (Default is 1)",
default=1)
args = parser.parse_args()
#Check for the output folder and also create the temporal folder
if not os.path.exists(args.output_directory):
os.makedirs(args.output_directory)
temp_folder = args.output_directory + "/tmp"
if not os.path.exists(temp_folder):
os.makedirs(temp_folder)
plot_folder = args.output_directory + "/plots"
if not os.path.exists(plot_folder):
os.makedirs(plot_folder)
sites_folder = args.output_directory + "/sites"
if not os.path.exists(sites_folder):
os.makedirs(sites_folder)
#Create output files
output_file = open(args.output_directory + "/paml_results.txt", 'w')
no_results_file = open(args.output_directory + "/no_results.txt", 'w')
#Read the cluster file
clusters_to_analyze = [line.rsplit()[0] for line in open(args.cluster_list) if line.strip()]
results_list = []
#Prepare for multiprocessing
#Function to store the results
def store_results(combined_results):
entry_results, sites_results = combined_results
output_file.write("\t".join(str(x) for x in entry_results) + "\n")
cluster_id = entry_results[0]
node = entry_results[0]
site_file = open(sites_folder + "/" + cluster_id + "_" + node + ".txt", 'w')
for position in sites_results:
aa, prob = sites_results[position]
site_file.write("\t".join(str(x) for x in [position, aa, prob]) + "\n")
results_list.append(entry_results)
site_file.close()
#Create the pool of processors
pool = multiprocessing.Pool(args.num_processors)
run_results = []
for cluster in clusters_to_analyze:
tree_file = args.tree_folder + "/" + cluster + ".tre"
align_file = args.align_folder + "/" + cluster + ".fna"
node_id_2_names = defaultdict()
for entry in EvolTree(tree_file).iter_descendants():
node_id_2_names[entry.node_id] = entry.get_leaf_names()
#Check that the files exists
if not os.path.exists(tree_file):
print "Tree file missing: " + tree_file
no_results_file.write(cluster + "\n")
continue
if not os.path.exists(align_file):
print "Alignment missing: " + align_file
no_results_file.write(cluster + "\n")
#Results, the first element has:
#The second is a dictionary with the positive selected sites
#results_dict[cluster] = run_site_branch(cluster, tree_file, align_file, temp_folder, plot_folder)
p = pool.apply_async(run_site_branch, args=(cluster, tree_file, align_file, temp_folder, plot_folder,),
callback=store_results)
run_results.append(p)
pool.close()
pool.join()
output_file.close()