forked from akdemirlab/MutationalDistribution
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MutationAggregate.py
327 lines (251 loc) · 10.8 KB
/
MutationAggregate.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
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
import os,sys
from pylab import *
from math import sqrt, isnan, floor, ceil, pi
from numpy import log2, log10, array, max
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MultipleLocator
from matplotlib.patches import Polygon, Rectangle, Circle
from scipy.signal import argrelextrema
from scipy import ndimage
from matplotlib import pyplot as plt
import numpy as np
import argparse
import bisect
import logging
import pybedtools
import glob
from shutil import copyfile
import scipy.stats
from scipy import stats
from matplotlib.ticker import FormatStrFormatter
#import seaborn as sns
from matplotlib import colors as clrs
def read_bedGraph(filename,resolution): # add stopping after certain chromosome passed
'''
reads bedGraph files for various file type plottings
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
returns:
x_scores = location along the given chromosome - start sites
x_scores2 = location along the given chromosome - end sites
y_scores = signal scores for the assay
colors = allow for colors option
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
x_scores={}
x_scores2={}
y_scores={}
average = 0.0
count = 1
for line in fone.xreadlines():
tags = line.strip().split("\t")
if line[0]!='t':
if tags[0] not in x_scores.keys() and tags[0] != 'chrY':
x_scores[tags[0]]=[]
x_scores2[tags[0]]=[]
y_scores[tags[0]]=[]
x_scores[tags[0]].append(float(tags[1])/resolution)
x_scores2[tags[0]].append(float(tags[2])/resolution)
if len(tags) > 3:
if tags[3]=='.': y_scores[tags[0]].append(0.0)
else: y_scores[tags[0]].append(float(tags[3]))
elif tags[0] != 'chrY':
x_scores[tags[0]].append(float(tags[1])/resolution)
x_scores2[tags[0]].append(float(tags[2])/resolution)
if len(tags) > 3:
if tags[3]=='.': y_scores[tags[0]].append(0.0)
else: y_scores[tags[0]].append(float(tags[3]))
average += (float(tags[2])-float(tags[1]))/resolution
count +=1
average = int(round(average/count,0))
return x_scores,x_scores2,y_scores,average
def read_bed(filename,resolution): # add stopping after certain chromosome passed
'''
reads bedGraph files for various file type plottings
parameters:
filename: file name. format could be either "chr\tstart\tend" or "chr\tstart\tend\tvalue..."
resolution: bin size for the matrix
returns:
x_scores = location along the given chromosome - start sites
x_scores2 = location along the given chromosome - end sites
y_scores = signal scores for the assay
colors = allow for colors option
'''
try:
fone=open(filename,'r')
except IOError:
print >>sys.stderr, 'cannot open', filename
raise SystemExit
x_scores={}
x_scores2={}
average = 0.0
ave = []
count = 1
for line in fone.xreadlines():
tags = line.strip().split("\t")
if line[0]=='#': continue
if line[0]!='t':
if tags[0] not in x_scores.keys() and tags[0] != 'chrY':
x_scores[tags[0]]=[]
x_scores2[tags[0]]=[]
x_scores[tags[0]].append(float(tags[1])/resolution)
x_scores2[tags[0]].append(float(tags[2])/resolution)
elif tags[0] != 'chrY':
x_scores[tags[0]].append(float(tags[1])/resolution)
x_scores2[tags[0]].append(float(tags[2])/resolution)
average+=(float(tags[2])-float(tags[1]))/resolution
count +=1
average = int(round(average/count,0))
return x_scores,x_scores2,average
def where(start,end,arr):
"""Find where the start location and end location indexes in an array"""
astart = bisect.bisect_left(arr, start)
aend = bisect.bisect_right(arr[start:], end) + start
return astart, aend
def profiler(file1,file2):
resolution = 25000
x_comps,x_comps2,y_comps,_ = read_bedGraph(file1,resolution)
x2_comps,x2_comps2,average = read_bed(file2,resolution)
pseudocount = 0
regions = []
for key in x2_comps.keys():
for item in range(0,len(x2_comps[key])):
midpoint = int(round(x2_comps[key][item],0))+int(round((x2_comps2[key][item]-x2_comps[key][item])/2,0))
if midpoint-20 > 0 : ystart,yend = where(midpoint-20,midpoint+20,x_comps[key])
else: print 'Discarding a region with midpoint', midpoint #ystart,yend = where(0,midpoint+20,x_comps[key])
if len(y_comps[key][ystart:yend]) < 41:
continue
print 'Short region in', file2
if len(regions)==0 : regions = np.lib.pad(y_comps[key][ystart:yend], (0,21-len(y_comps[key][ystart:yend])), 'constant', constant_values=(0))
else: regions = np.vstack([regions,np.lib.pad(y_comps[key][ystart:yend], (0,21-len(y_comps[key][ystart:yend])), 'constant', constant_values=(0))])
else:
if len(regions)==0 : regions = y_comps[key][ystart:yend]#;writer.write(str(y_comps[key][ystart:yend]));writer.write('\n')
else: regions = np.vstack([regions,np.array(y_comps[key][ystart:yend])])#;writer.write(str(y_comps[key][ystart:yend]));writer.write('\n')
final = np.mean(regions,axis=0)
return final,average
def mut_profiler(mutations='',boundaries='',output='',name=''):
fig, ax = plt.subplots(1, 1)
p_average3,l_average3 = profiler(mutations,boundaries)
x = np.linspace(0, len(p_average3), 1)
ax.plot(p_average3,linewidth = 4, color='#008080',label='Mutations')
ax.set_ylabel('Mut Load / 25 Kb',fontsize=20)
ax.locator_params(axis='y',tight=False, nbins=5)
ax.locator_params(axis='x',tight=False, nbins=3)
ticks= ax.get_xticks().tolist()
ticks = ['-500Kb','Boundary','+500Kb']
ax.set_xticklabels(ticks,fontsize=20)
#ax.axvspan(0,20,facecolor='#fc9929',alpha=0.45)
#ax.axvspan(20,40,facecolor='#cbcbcb',alpha=0.45)
ax.set_xlim(0,40)
ax.set_title(name)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.savefig(output+'.png',dpi=200)
def dom_profiler(mutations='',domain='',output='',name=''):
mutations = pybedtools.BedTool(mutations).sort()
doms = pybedtools.BedTool(domain).sort()
overlap = doms.map(mutations,c=4,o='sum')
fig, ax = plt.subplots(1, 1)
domains = {}
data = []
for tags in overlap:
if tags[3] not in domains.keys():
domains[tags[3]]=[]
domains[tags[3]].append(float(tags[5])*25000/(int(tags[2])-int(tags[1])))
else:
domains[tags[3]].append(float(tags[5])*25000/(int(tags[2])-int(tags[1])))
data = [domains['0'],domains['1'],domains['2'],domains['3'],domains['4']]
colors=["#8a91d0","#cbcbcb","#4da6ff","#fc9929","#ff4444"]
for i in [1,2,3,4,5]:
y = data[i-1]
x = np.random.normal(i, 0.04, len(y))
ax.plot(x, y, colors[i-1], alpha=0.4,mec='k', ms=7, marker="o", linestyle="None")
ax.set_facecolor("white")
#if counter != len(files)-1: plt.setp(ax.get_xticklabels(), visible=False)
#ax.set_title(name,fontsize=10)
#ax.get_yaxis().set_label_coords(-0.2,0.5)
ax.locator_params(axis='y',tight=False, nbins=4)
print scipy.stats.ranksums(domains['1'],domains['3'])
box = ax.boxplot(data, notch=False, patch_artist=True,widths=(0.6,0.6,0.6,0.6,0.6),showfliers=False)
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor('none');patch.set_edgecolor('none');
for median in box['medians']: median.set(color='black', linewidth=2)
#for median in box['fliers']: median.set(color='gray', marker='o')
plt.setp(box['whiskers'], color='black')
doms = ['Heterochromatin','Inactive','Repressed','Active','Active-2']
ax.set_xticks(range(1,6,1))
ticks= ax.get_xticks().tolist()
for ditem in range(0,len(ticks)): ticks[ditem]=doms[ditem]
ax.set_xticklabels(ticks, fontsize=8, fontweight='bold')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_title(name)
plt.savefig(output+'.png',dpi=200)
def Plotter(mutation='',output='',region='',domain='',folder='',name=''):
if len(mutation)>0 and len(domain)==0 and len(folder)==0: mut_profiler(mutation,region,output,name)
if len(mutation)>0 and len(domain)>0: dom_profiler(mutation,domain,output,name)
if len(folder)>0:
tog,l_average3 = profiler(mutation,region)
files = glob.glob(folder+"/*.bedGraph")
data = []
for item in files:
p_average3,l_average3 = profiler(item,region)
if len(data)==0 : data = p_average3[:]
else: data = np.vstack([data,np.array(p_average3[:])])
x = np.linspace(0, len(data[0]), 1)
norm_data = (data - np.mean(data, axis=1)[:, np.newaxis]) / np.ptp(data, axis=1)[:, np.newaxis]
scmap = clrs.ListedColormap(['#f662ff', '#55adff'])
bounds=[0,1]
fig, (ax,ax1) = plt.subplots(2,1,figsize=(8,16))
fig.subplots_adjust(hspace=0.25,wspace=0.25)
ax.plot(tog,linewidth = 4, color='#008080',label='Mutations')
ax.set_ylabel('Mut Load / 25 Kb',fontsize=20)
ax.locator_params(axis='y',tight=False, nbins=5)
ax.locator_params(axis='x',tight=False, nbins=3)
ticks= ax.get_xticks().tolist()
ticks = ['-500Kb','Boundary','+500Kb']
ax.set_xticklabels(ticks,fontsize=20)
ax.set_xlim([0,40])
ax.set_title(name)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
with np.errstate(divide='ignore'): img = ax1.imshow(norm_data,interpolation='nearest',aspect='auto',cmap='binary')
ax1.set_xlim([0,40])
ax1.xaxis.set_ticks([])
ax1.yaxis.set_ticks([])
ax1.set_frame_on(False)
ax1.set_ylabel(name,fontsize=15)
ax1.get_yaxis().set_label_coords(-0.075,0.5)
plt.setp(ax1.get_xticklabels(), visible=False)
ax1.axvspan(19, 21, facecolor='#5781A4', alpha=0.30, linestyle='dashed')
plt.savefig(output+'.png',dpi=200)
if __name__=='__main__':
parser = argparse.ArgumentParser(usage='MutationAggregate.py -m Mutation.bedGraph -r Boundaries.bed -o output -n title',add_help=False,formatter_class=argparse.RawDescriptionHelpFormatter)
group = parser.add_argument_group("Required Parameters")
group.add_argument('-m','--mutation',default='', help='',metavar='',required=True)
group.add_argument('-o', '--output',default='',metavar='',required=True)
group.add_argument('-n', '--name',default='',metavar='',required=True)
group1 = parser.add_argument_group("Optional Parameters")
group1.add_argument('-h', '--help', action="help")
group1.add_argument('-r', '--region',default='',metavar='',help='')
group1.add_argument('-d', '--domain',default='',metavar='',help='')
group1.add_argument('-f', '--folder',default='',metavar='',help='')
args = vars(parser.parse_args())
if len(args['domain'])==0 and len(args['region'])==0 and len(args['folder'])==0:
print >>sys.stderr, 'Upps!! Please activate one of the modules with -r, -d or -r & -h'
raise SystemExit
if len(args['domain'])>0 and len(args['folder'])>0:
print >>sys.stderr, 'Upps!! Please either provide a domain file or a folder.'
raise SystemExit
Plotter(**args)