-
Notifications
You must be signed in to change notification settings - Fork 6
/
depth_plot.py
executable file
·242 lines (198 loc) · 8.89 KB
/
depth_plot.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
#!/usr/bin/env python
# Copyright (C) 2012-2013 Collin Tokheim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# graphing imports
import matplotlib
# matplotlib.use('Agg') # prevent figure pop-ups
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredText # import anchored text
import itertools as it
import argparse
import os
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, DrawingArea, HPacker # import for offset text box
import traceback # debugging import
# important packages
# import numpy as np
# own imports
import wig
import utils
class DropFormatter(ticker.ScalarFormatter):
"""
git rid of bottom left labels
"""
def __call__(self, x, pos=None):
if pos==0: return ''
return ticker.ScalarFormatter.__call__(self, x, pos=None)
class NoFormatter(ticker.ScalarFormatter):
"""
remove all tick labels
"""
def __call__(self, x, pos=None):
return ''
def offset_text(ax, txt, loc, bbox_to_anchor=(-.03, .65), text_props={"color": "k", "size": "13"}):
"""This function is a copy of the one in draw.py"""
box1 = TextArea(txt, textprops=text_props)
box = HPacker(children=[box1],
align="center",
pad=2, sep=5)
anchored_box = AnchoredOffsetbox(loc=loc,
child=box, pad=0.,
frameon=False,
prop=dict(size=5),
bbox_to_anchor=bbox_to_anchor,
bbox_transform=ax.transAxes,
borderpad=0.1,
)
ax.add_artist(anchored_box)
def draw_text(ax, txt):
"""
Add text in right corner that identifies the data
"""
at = AnchoredText(txt,
loc=1, prop=dict(size=13), frameon=True,)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)
# add commas to an integer for display on figure
def addCommas(myNum):
"""
A long function for a simple task, add commas to a number
"""
strNum = str(int(myNum))[::-1]
numLength = len(strNum)
# figure out the number of commas needed
numCommas = numLength / 3
# find the remainder
remainder = numLength % 3
# get 3 digit substrings
subStrings = []
i = 1
for i in range(0, numCommas):
tmp = strNum[i*3:i*3+3]
revTemp = tmp[::-1]
subStrings = [revTemp] + subStrings
if not remainder == 0:
# add first digits in number
tmp = strNum[-remainder:]
revTmp = tmp[::-1]
subStrings = [revTmp] + subStrings
# return comma seperated integer
return ",".join(subStrings)
def scale_depth(depth_dictionary, start, stop, step):
list_of_ranges = []
all_pos = sorted(it.chain(start, stop))
for i in range(len(all_pos)-1):
if not i % 2:
list_of_ranges.append(range(all_pos[i], all_pos[i+1]))
else:
list_of_ranges.append(range(all_pos[i], all_pos[i+1], step))
scaled_dict = {}
pos_counter = start[0]
for pos in it.chain.from_iterable(list_of_ranges):
scaled_dict[pos_counter] = depth_dictionary.get(pos, 0)
pos_counter += 1
return scaled_dict, start[0], pos_counter
## Plot generating functions ##
def generate_plot(ax, bw, chr, start, stop, options):
"""
Calls the matplotlib hist method to create the histogram.
"""
tmp_start, tmp_stop = (start[0], stop[-1]) if type(start) == type(tuple()) else (start, stop)
if bw == '':
# if user doesn't specify a bigwig file then just return
return 1, tmp_start, tmp_stop
wig_obj = wig.Wig(bw, ext='wig')
wig_obj.extractBigRegion(chr, tmp_start, tmp_stop)
wig_obj.load_wig_file()
depth_dict = wig_obj.get_annotation()
if options['step'] != 1: depth_dict, tmp_start, tmp_stop = scale_depth(depth_dict, start, stop, options['step'])
max_count = max(depth_dict.itervalues()) if len(list(depth_dict.itervalues())) > 0 else 1
# depth_counts = list(it.chain.from_iterable([key] * depth_dict[key] for key in depth_dict)) if depth_dict else [0]
# ax.hist(depth_counts, range(tmp_start, tmp_stop + 1), facecolor='k')
if max_count > 1:
ax.hist(map(lambda x: (x[0] + x[1]) / 2., depth_dict.keys()),
# range(tmp_start, tmp_stop + 1),
wig_obj.bins,
weights=depth_dict.values(),
facecolor='k')
return max_count, tmp_start, tmp_stop
def read_depth_plot(my_bigwigs, output, options):
if type(options['position']) == type(list()):
chr = utils.get_chr(options['position'][0])
start, stop = zip(*map(lambda x: utils.get_pos(x), options['position']))
else:
chr = utils.get_chr(options['position'])
start, stop = utils.get_pos(options['position'])
bigwigs = my_bigwigs.split(',')
num_subplots = len(bigwigs) # num of bam files equals number of subplots
fig, axes = plt.subplots(num_subplots, 1, sharex=True, sharey=True, figsize=(6, options['size'] * num_subplots))
gray = (0.9, 0.9, 0.9)
# iterate over subplots (bigwig files)
max_count_holder = 0
if num_subplots == 1:
# axes.set_title('Read Depth Plot on %s' % chr)
iterable = [axes]
else:
# axes.flat[0].set_title('Read Depth Plot on %s' % chr)
iterable = axes.flat
for i, ax in enumerate(iterable):
#ax.locator_params(nbins=2)
ax.yaxis.set_label_text('')
# set bg
ax.patch.set_facecolor(gray)
ax.patch.set_edgecolor(gray)
ax.grid()
# plot/label
max_count, real_start, real_stop = generate_plot(ax, bigwigs[i], chr, start, stop, options) # does the actual work
draw_text(ax, '%s -- ' % options['gene'] + os.path.splitext(os.path.basename(bigwigs[i]))[0])
# format options
ax.xaxis.grid(color='white', linestyle='--', linewidth=1.5)
ax.yaxis.grid(color='white', linestyle='--', linewidth=1.5)
ax.xaxis.set_major_formatter(DropFormatter())
ax.yaxis.set_major_formatter(DropFormatter())
ax.set_axisbelow(True)
# hide some ugly lines
for line in ax.xaxis.get_ticklines() + ax.yaxis.get_ticklines():
line.set_color(gray)
# set y-axis
if max_count > max_count_holder:
ax.set_ylim(0, 1.5 * max_count)
ax.set_yticks([0, int( .375 * max_count ), int( .75 * max_count ), int( 1.125 * max_count ), int(1.5 * max_count)])
max_count_holder = max_count
# set x-axis options
ax.set_xlim(real_start, real_stop) # set x limits
ax.set_xticks([real_start, real_stop]) # explicitly set ticks
ax.xaxis.set_ticklabels(map(addCommas, [real_start, real_stop])) # make nice looking text for labels
ax.get_xticklabels()[0].set_horizontalalignment('left')
ax.get_xticklabels()[1].set_horizontalalignment('right')
# make text box to display chromosome information
if i == num_subplots - 1:
offset_text(ax, '%s:' % chr, 3, (-.15, -.17))
# adjust spacing between subplots
fig.subplots_adjust(wspace=0.05, hspace=0.05, bottom=.12)
# save figure
plt.savefig(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-b', dest='bigwig', action='store', required=True, help='Comma separated list of bigwig files')
parser.add_argument('-p', dest='position', action='store', required=True, help='Zero based position chr:start-stop')
parser.add_argument('-g', dest='gene', action='store', required=True, help='the name')
parser.add_argument('-s', dest='size', action='store', default=2.)
parser.add_argument('--step', dest='step', action='store', type=int, default=1)
parser.add_argument('-o', dest='output', action='store', required=True, help='path to output figure')
options = vars(parser.parse_args())
if len(options['position']) > 1:
options['position'] = options['position'].split(',')
read_depth_plot(options)