-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
330 lines (299 loc) · 12.3 KB
/
utils.py
File metadata and controls
330 lines (299 loc) · 12.3 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
* DeepFavored - A deep learning based method to identify favored(adaptive) mutations in population genomes.
* Copyright (C) 2022 Ji Tang, Hao Zhu
*
* This program is licensed for academic research use only
* unless otherwise stated. Contact jitang1024@outlook.com, zhuhao@smu.edu.cn for
* commercial licensing options.
*
* For use in any publications please cite: Ji Tang, Maosheng Huang, Sha He, Junxiang Zeng, Hao Zhu (2022). Uncovering the extensive trade-off between adaptive evolution and disease susceptibility. Cell Reports, Volume 40, Issue 11, 111351.
"""
import os
import time
import re
import warnings
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# ====================================================================================================
# generic tools
# {
def shuffle(X, Y=None):
np.random.seed(13)
indices = np.random.permutation(len(X))
X = X[indices]
if Y is None:
return X
else:
Y = Y[indices]
return X, Y
def RecurReadFilePaths(inPath, files):
"""
Read file paths recursively
:param inPath:
:param files:
:return:
"""
if os.path.isfile(inPath):
files.append(inPath)
if os.path.isdir(inPath):
for term in os.listdir(inPath):
files = RecurReadFilePaths(inPath + '/' + term, files)
return files
def format_time_differ(time_differ):
hour = int(time_differ // 3600)
minute = int((time_differ % 3600) // 60)
second = int(time_differ % 60)
fmt = '{:0>2d}h:{:0>2d}m:{:0>2d}s'
return fmt.format(hour, minute, second)
def WriteToLogFile(logFile, info):
info = '(' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ')' + info
if info[-1] != '\n':
info += '\n'
logFileObj = open(logFile, 'a')
logFileObj.write(info)
logFileObj.close()
# }
# ====================================================================================================
# ====================================================================================================
# functions for performance evaluation
# {
def get_cutoff_list(start, end, step, decimal=3):
term = np.arange(start, end, step)
if start > end:
term = np.arange(end, start, step)
term = sorted(term, reverse=True)
cutoff_list = [round(item, decimal) for item in term]
return cutoff_list
def CountConfusionMtxNumbers(Y_pred, Y_true, outFile, cutoffs=None, annotation=None):
if cutoffs is None:
cutoffs = get_cutoff_list(1.01, -0.01, 0.01)
headerAnnotation = '#P: number of favored mutations\n'
headerAnnotation += '#N: number of neutral mutations\n'
headerAnnotation += '#PP: number of the mutations that actually is favored and is predicted as favored\n'
headerAnnotation += '#PN: number of the mutations that actually is favored but is predicted as neutral\n'
headerAnnotation += '#NP: number of the mutations that actually is neutral but is predicted as favored\n'
headerAnnotation += '#NN: number of the mutations that actually is neutral and is predicted as neutral\n'
header = ['#Cutoff', 'P', 'N', 'PP', 'PN', 'NP', 'NN']
with open(outFile, 'w') as fw:
if annotation:
if annotation[0] != '#':
annotation = '#' + annotation
if annotation[-1] != '\n':
annotation += '\n'
fw.write(annotation)
fw.write(headerAnnotation)
fw.write('\t'.join(header) + '\n')
for c in cutoffs:
pp, pn, n_p, nn = 0, 0, 0, 0 # n_p avoid "import numpy as np"
for y_pred, y_true in zip(Y_pred, Y_true):
if y_pred >= c:
if y_true:
pp += 1
else:
n_p += 1
else:
if y_true:
pn += 1
else:
nn += 1
row = [c, pp + pn, n_p + nn, pp, pn, n_p, nn]
fw.write('\t'.join([str(i) for i in row]) + '\n')
print(' save confusion matrix numbers to ' + outFile)
def CalcAuc(ratePairs, baseIdx, heightIdx):
ratePairs.sort(key=lambda x: x[heightIdx], reverse=False)
auc = 0
for idx, ratePair in enumerate(ratePairs):
base_rate, height_rate = ratePair[baseIdx], ratePair[heightIdx]
if idx > 0:
height = height_rate - ratePairs[idx - 1][heightIdx]
if height > 0:
base_bottom = ratePairs[idx - 1][baseIdx]
base_upper = base_rate
trapezoid_area = ((base_upper + base_bottom) * height) / 2
auc += trapezoid_area
return auc
def UniformDistrOnYaxis(xList, yList, cutoffs=None, y_bottom=0, y_top=1, step=0.05, decimal=3):
xListNew, yListNew = [], []
cutoffsNew = []
# Generate a dict
vals = np.arange(y_bottom, y_top + step, step)
valsList = [round(i, decimal) for i in vals]
valNum = len(valsList)
noValDict = {}
for i in range(1, valNum):
noValDict[(valsList[i - 1], valsList[i])] = True
for start, end in noValDict:
for idx, y in enumerate(yList):
if noValDict[(start, end)]:
if start <= y <= end:
xListNew.append(xList[idx])
yListNew.append(y)
if cutoffs is not None:
cutoffsNew.append(cutoffs[idx])
noValDict[(start, end)] = False
else:
break
if (0 not in xListNew) and (0 not in yListNew):
xListNew, yListNew = [0] + xListNew, [0] + yListNew
if cutoffs is not None:
cutoffsNew = ['0'] + cutoffsNew
if (1 not in xListNew) and (1 not in yListNew):
xListNew, yListNew = xListNew + [1], yListNew + [1]
if cutoffs is not None:
cutoffsNew = cutoffsNew + ['1']
if cutoffs is not None:
return xListNew, yListNew, cutoffsNew
else:
return xListNew, yListNew
def PlotPowerFDRcurve(dfOutPath, outFig):
print('plot power-FDR curve ...')
confusionMtxNumsFile = '/'.join(outFig.split('/')[:-1]) + '/confusionMtxNumbers'
Y_pred, Y_true = [], []
dfOutFiles = RecurReadFilePaths(dfOutPath, files=[])
for file in dfOutFiles:
df = pd.read_csv(file, sep='\t')
fileName = file.split('/')[-1]
if fileName[:5] == 'sweep':
favPos = int(fileName.split('_')[1])
try:
favScore = df[df.POS == favPos].DF.values[0]
scores = df[df.POS != favPos].DF.values.tolist()
Y_pred.append(favScore)
Y_true.append(1)
Y_pred += scores
Y_true += [0] * len(scores)
except:
pass
else:
scores = df.DF.values.tolist()
Y_pred += scores
Y_true += [0] * len(scores)
CountConfusionMtxNumbers(Y_pred, Y_true, confusionMtxNumsFile)
confusionMtxNums = []
with open(confusionMtxNumsFile, 'r') as fr:
for row in fr.readlines():
if row[0] != '#':
cols = row.rstrip('\n').split('\t')
confusionMtxNums.append([int(i) for i in cols[1:]])
powerFDRs, tprs, fdrs = [], [], []
all_fav_num, all_neut_num = 0, 0
for all_fav_num, all_neut_num, pp, pn, n_p, nn in confusionMtxNums:
predicted_P = pp + n_p
tpr = pp / (pp + pn) # tpr=power=recall
if predicted_P == 0:
fdr = 0.0
else:
fdr = float(n_p) / predicted_P
powerFDRs.append((tpr, fdr))
tprs.append(tpr)
fdrs.append(fdr)
powerFDRcurveAUC = CalcAuc(powerFDRs, baseIdx=0, heightIdx=1)
fontsize = 15
fontsize_tick_params = fontsize - 3
line_width = 5
fig_size = [8, 10]
plt.clf()
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=fig_size)
y_lim_bottom, y_lim_top = 0, 1
# xList, yList = UniformDistrOnYaxis(fdrs, tprs, y_bottom=y_lim_bottom, y_top=y_lim_top)
xList, yList = fdrs, tprs
ax.plot(xList, yList, color='black', linestyle='-', linewidth=line_width)
ax.set_title("AUC=%s, favored:neutral=1:%i" % (str(round(powerFDRcurveAUC, 2)), round(all_neut_num / all_fav_num)),
fontsize=fontsize)
ax.locator_params(nbins=5)
ax.set_xlabel('False Discovery Rate', fontsize=fontsize)
ax.set_ylabel('Power', fontsize=fontsize)
ax.set_xlim(left=0, right=1) # , fontsize=fontsize)
ax.set_ylim(bottom=y_lim_bottom, top=y_lim_top) # , fontsize=fontsize)
ax.tick_params(axis='both', labelsize=fontsize_tick_params)
plt.savefig(outFig, dpi=200, bbox_inches='tight')
plt.close()
print('Plotted to ' + outFig)
def MhtPlot(dfOutPath, outPath):
files = RecurReadFilePaths(dfOutPath, files=[])
for file in files:
outFig = re.sub(dfOutPath, outPath, file.rstrip('.df.out')) + '.png'
outFigPath = '/'.join(outFig.split('/')[:-1])
os.makedirs(outFigPath, exist_ok=True)
fileName = file.split('/')[-1]
favPos = int(fileName.split('_')[0])
df = pd.read_csv(file, sep='\t')
df.sort_values(by='DF', inplace=True, ascending=False, ignore_index=True)
try:
favPosRank = df[df.POS == favPos].index.values[0] + 1
favScore = df[df.POS == favPos].DF.values[0]
except:
warnings.warn('favored mutation site not found in ' + file, UserWarning)
continue
regionLen = str(round((df.POS.max() - df.POS.min()) / 1000))
posList = df.POS.values.tolist()
scoreList = df.DF.values.tolist()
fig_size = [8, 4]
fontsize = 15
fontsize_tick_params = fontsize - 3
plt.clf()
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=fig_size, squeeze=True)
y_lim_below, y_lim_upper = -0.02, 1.1
ax.set_xlabel('Position(bp)', fontsize=fontsize)
ax.set_ylabel('DFscore', fontsize=fontsize)
ax.set_ylim(bottom=y_lim_below, top=y_lim_upper) # , fontsize=fontsize)
ax.scatter(posList, scoreList, 50, 'gray')
ax.scatter(favPos, favScore, 180, 'red', marker='v')
ax.set_title('favored site rank=%s, region length=%sKb' % (str(favPosRank), regionLen), fontsize=fontsize)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.tick_params(axis='both', labelsize=fontsize_tick_params)
# Save fig
plt.xlim()
plt.savefig(outFig, dpi=200, bbox_inches='tight')
print('Output to:\n' + outFig)
plt.close()
def PlotRankCDFcurve(dfOutPath, outFig):
print('plot rankCDF curve ...')
dfOutFiles = RecurReadFilePaths(dfOutPath, files=[])
ranks = []
regionLens = []
for file in dfOutFiles:
fileName = file.split('/')[-1]
if fileName[:5] == 'sweep':
favPos = int(fileName.split('_')[1])
df = pd.read_csv(file, sep='\t')
df.sort_values(by='DF', inplace=True, ascending=False, ignore_index=True)
try:
rank = df[df.POS == favPos].index.values[0] + 1
ranks.append(rank)
regionLens.append(df.POS.max() - df.POS.min())
except:
pass
mean_region_len = np.mean(regionLens)
plt.clf()
fig_size = [8, 10]
fontsize = 15
fontsize_tick_params = fontsize - 3
line_width = 5
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=fig_size)
counts, cdf_bins = np.histogram(ranks, bins=max(ranks), density=True)
cdf = np.cumsum(counts)
ax.plot(cdf_bins[:-1], cdf, color='black', linewidth=line_width)
ax.locator_params(nbins=5)
if mean_region_len < 5000:
xlim_right = 5
elif mean_region_len < 10000:
xlim_right = 10
else:
xlim_right = 50
ax.set_xlim(left=1, right=xlim_right) # , fontsize=fontsize)
ax.set_ylim(bottom=0, top=1) # , fontsize=fontsize)
ax.tick_params(axis='both', labelsize=fontsize_tick_params)
ax.set_title("mean regions length=%iKb, regions number=%i" % (round(mean_region_len / 1000), len(regionLens)),
fontsize=fontsize)
plt.ylabel('Cumulative Distribution Function', fontsize=fontsize)
plt.xlabel('Rank', fontsize=fontsize)
plt.savefig(outFig, dpi=200, bbox_inches='tight')
plt.close()
print('plotted to ' + outFig)
# }
# ====================================================================================================