-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphy_v2.py
602 lines (519 loc) · 19.3 KB
/
graphy_v2.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import pandas as pd
# import numpy as np
# from matplotlib import pyplot as plt
# from matplotlib import gridspec
# from matplotlib.patches import Rectangle
# from matplotlib import rc
# import matplotlib.patches as mpatches
# import subprocess
# from operator import itemgetter
#import seaborn as sns
import itertools
import sys
import os
import spectra
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
sys.path.insert(0,'/Users/DarthRNA/Documents/Robin/PythonModules')
sys.path.insert(0,'/Users/DarthRNA/Documents/Robin/HitsSummary')
import clip_tools as ct
from functions import *
from defaults import *
from ipywidgets import interact
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import FloatSlider
from IPython.display import clear_output
updates = "\
3/23/17: Now the tracks are displayed in the order they were selected! <p> \
4/11/17: Added custom section to bedfiles. Covers custom bedfiles, including custom miRNA tracks <p> \
4/27/17: Updated LeftToRight button\
"
###################################################
# Required: Reference.
# file_utr3 is used in finding the names of genes,
# though a bed of entire gene transcripts might be more useful (and should work)
# file_refseq is used to graph the refseq track.
# file_utr3 = "/Users/DarthRNA/Documents/Robin/genomes/refseq_3utr.bed"
# file_refseq = "/Users/DarthRNA/Documents/Robin/genomes/refseq_names.txt"
# file_targetscan = "/Users/DarthRNA/Documents/Robin/genomes/Targetscan.bed"
# file_refgenebed = '/Users/DarthRNA/Documents/Robin/genomes/refGene.bed'
# default_track_folder = "/Users/DarthRNA/Documents/Robin/GRAPHY_TEST/"
bedfiles_allowed = {"Peaks":{"name":"Peaks",
"bedfile":"/Users/DarthRNA/Documents/Robin/FASTUTR/peaks.bed",
"bedtype":"bed"},
"Targetscan":{"name":"Targetscan",
"bedfile":file_targetscan,
"bedtype":"targetscan"}}
### Lookup 3'UTR by Refseq
df_refseq_3utr = pd.read_table("/Users/DarthRNA/Documents/Robin/genomes/mm10_refseq_3utr.bed",names=['chrom','start','stop','name','score','strand'])
df_refseq_3utr.name = ["_".join(x.split("_")[0:2]) for x in df_refseq_3utr.name]
df_refseq_3utr = df_refseq_3utr.drop_duplicates("name")
df_refseq_3utr = df_refseq_3utr.set_index("name")
###################################################
# Make a genelookup object.
bd = ct.BetweenDict(file_refgenebed)
##############################################
print_plotting_parameters = True
##############################################
legend = True # adds legend box to alignment bam track.
staticaxes = False # keeps axes the same across all bam files. (for normalized data)
LeftToRight = False # Makes all genes read left to right.
axis_off = False # Don't show the axis labels.
### FILE OUTPUT OPTIONS ###
#dpi = 300
# figwidth = "scale" # Can be "scale" or <int> (15 is a good basic value)
# outputformat = "pdf"
# outputsuffix = ""
# Mostly Untouched Default Variables.
# Set static parameters
limits = "default" # can also be [int_start,int_stop]
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
##############################################
defaultloc = 'chr5:142,903,034-142,906,867'
defaultgeneid = 'Actb'
defaultrefseqid = 'NM_007393'
defaultstrand = "-"
#### Accordian Change File Locations ####
w_default_folder = widgets.Text(
description='RnaSeq Folder',
value=default_track_folder,
)
w_default_folder.width="80%"
w_default_folder_valid = widgets.Valid(value=True)
hboxdefaultfolder = widgets.HBox([w_default_folder,w_default_folder_valid])
page1 = widgets.VBox(children=[hboxdefaultfolder])
accord = widgets.Accordion(children=[page1], width="80%")
display(accord)
accord.set_title(0, 'Defaults')
#### Define Widgets ###
lookuptype_widget = widgets.RadioButtons(
options={'By location':'location','By geneid (3\'UTR only)':"geneid"},
value='location',
description='Lookup:',
disabled=False
)
location_widget = widgets.Text(
value=defaultloc,
placeholder='Type something',
description='Location:',
disabled=False
)
strand_widget = widgets.RadioButtons(
options=['+','-'],
value=defaultstrand,
description='Strand:',
disabled=False
)
geneid_widget = widgets.Dropdown(
options=[defaultgeneid],
value=defaultgeneid,
description='GeneID:',
disabled=False,
button_style='' # 'success', 'info', 'warning', 'danger' or ''
)
refseqid_widget = widgets.Dropdown(
options=[defaultrefseqid],
value=defaultrefseqid,
description='RefseqID:',
disabled=False,
button_style='' # 'success', 'info', 'warning', 'danger' or ''
)
## Updating Widgets ##
def update_by_type(*args):
if lookuptype_widget.value == "location":
location_widget.value = defaultloc
if lookuptype_widget.value == "geneid":
location_widget.value = defaultgeneid
def update_geneid(*args):
if lookuptype_widget.value == "location":
chrom,start,stop = ct.easylocation(location_widget.value)
strand = strand_widget.value
genelist = list(get_gene_id(chrom,start,stop,strand,bd,choice='all'))
if len(genelist)>0:
geneid_widget.options = genelist
else:
geneid_widget.options = ["None"]
elif lookuptype_widget.value == "geneid":
geneid_widget.options = [location_widget.value]
geneid_widget.value = location_widget.value
def update_refseqid(*args):
reflist = get_refseq_id(file_refseq,geneid_widget.value,choice="all")
print reflist
if len(reflist)>0:
refseqid_widget.options = reflist
else:
refseqid_widget.options = ["None"]
lookuptype_widget.observe(update_by_type,'value')
location_widget.observe(update_geneid, 'value')
strand_widget.observe(update_geneid, 'value')
geneid_widget.observe(update_refseqid,'value')
display(widgets.HTML(value="<h5>Updates:</h5><p>" + updates))
display(widgets.HTML(value="<h3>Select Region</h3>"))
### Define INTERACT: All interdependent widgets must be in here ###
def printer(lookuptype,loc, strand, geneid,refseqid):
print("")
interact(printer,
lookuptype = lookuptype_widget,
loc=location_widget,
strand=strand_widget,
geneid=geneid_widget,
refseqid = refseqid_widget,
continuous_update=False)
### Select File ###
all_files = os.listdir(default_track_folder)
file_types = ["bam","bw"]
file_names = []
for f in all_files:
if f.split(".")[-1] in file_types:
file_names.append(f)
file_widget = widgets.SelectMultiple(
options=file_names,
disabled=False
)
def update_antisense_check(*args):
if len(file_widget.value)==0:
w_as_selectors.layout.display="none"
w_antisense_check.options = []
if len(file_widget.value)>0:
w_as_selectors.layout.display=""
# Add or remove from antisence_check.options
options = w_antisense_check.options
for i in file_widget.value:
if not i in options:
options.append(i)
for i in options:
if not i in file_widget.value:
options.remove(i)
w_antisense_check.options = options
file_widget.observe(update_antisense_check,'value')
w_aschecktitle= widgets.HTML("Are any of these tracks antisense?")
# The antisense_check box is also holding the order of the files selected
w_antisense_check = widgets.SelectMultiple(
options=[]
)
w_as_selectors = widgets.VBox([w_aschecktitle,w_antisense_check])
w_as_selectors.layout.display="none"
### Style Selectors ###
buttonwidth="15%"
buttonstyle='info'
w_labels = widgets.ToggleButton(
value=False,
description='BedLabels',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip='Label each element of a bedtrack',
width=buttonwidth,
)
w_invert = widgets.ToggleButton(
value=False,
description='Left to Right',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Always display genes from left to right regardless of strand.",
width=buttonwidth,
)
w_legend = widgets.ToggleButton(
value=True,
description='Legend',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Show Legend for Seq-Tracks",
width=buttonwidth,
)
w_stagger = widgets.ToggleButton(
value=False,
description='Stagger Bedtracks',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Stagger Bedtracks",
width=buttonwidth,
)
w_refseq = widgets.ToggleButton(
value=True,
description='Show Refseq',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Refseq track?",
width=buttonwidth,
)
w_shadeBed = widgets.ToggleButton(
value=False,
description='Shade Bed',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Highlight bedtrack regions",
width=buttonwidth,
)
w_staticaxes = widgets.ToggleButton(
value=True,
description='Autoscale',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Scale the rnaseq tracks?",
width=buttonwidth,
)
w_boundingbox = widgets.ToggleButton(
value=True,
description='Show BoundingBox',
button_style=buttonstyle, # 'success', 'info', 'warning', 'danger' or ''
tooltip="Show axes and border box?",
width=buttonwidth,
)
formattingwidth ="20%"
w_xscale = widgets.FloatText(
description="x scale (inches)",
placeholder="Int",
tooltip="can enter 0 to autoscale",
width=formattingwidth,
)
w_fontsize = widgets.FloatText(
description="Font Size",
placeholder="Int",
tooltip="can enter 0 to autoscale",
value=12,
width=formattingwidth,
)
w_dpi = widgets.FloatText(
description="DPI",
placeholder="300",
value="300",
width=formattingwidth,
)
w_fileformat = widgets.Text(
description="File Format",
placeholder="Pdf,Svg,PNG",
value="pdf",
width=formattingwidth,
)
w_filesuffix = widgets.Text(
description="File Suffix",
placeholder="None",
value="",
width=formattingwidth,
)
w_outputfolder = widgets.Text(
value="/Users/DarthRNA/Documents/Robin/TrackImages/",
description="Output Folder",
width="80%",
)
w_outputfolder_valid = widgets.Valid(value=True)
hboxoutputfolder = widgets.HBox([w_outputfolder,w_outputfolder_valid])
def output_folder_check(*args):
if os.path.isdir(w_outputfolder.value) and w_outputfolder.value[-1] == "/" :
w_outputfolder_valid.value=True
else:
w_outputfolder_valid.value=False
w_outputfolder.observe(output_folder_check,'value')
### Color Selectors ###
w_cp1=widgets.ColorPicker(value="#0080ff",width="100px",concise=False)
w_cp2=widgets.ColorPicker(value="#ff0000",width="100px",concise=False)
w_cp3=widgets.ColorPicker(value="#00ff00",width="100px",concise=False)
w_cp4=widgets.ColorPicker(value="#800080",width="100px",concise=False)
selectors_color = widgets.HBox([w_cp1,w_cp2,w_cp3,w_cp4])
selectors1 = widgets.HBox([w_labels,
w_invert,
w_legend,
w_stagger,
w_refseq,
w_shadeBed,
w_staticaxes,
w_boundingbox,
])
selectors2 = widgets.HBox([w_xscale,
w_fontsize,
w_dpi,
w_fileformat,
w_filesuffix,
])
### MiRNA and Bedtrack Picker ###
bedtracks_widget = widgets.RadioButtons(
options=["None"] + bedfiles_allowed.keys() + ["Custom","Other"],
description='Bedfiles:',
disabled=False
)
other_bedtracks_widget = widgets.Text(
value=placeholder_file_bedfile,
description="Other:",
width="90%"
)
miRNA_widget = widgets.Select(
options=["None"],
value="None",
description = "miRNA Family",
)
def update_miRNAlist(*args):
miRNAwrapper.layout.display="none"
otherbedtrackwrapper.layout.display="none"
def get_targetscan_families(targetscan_file):
beddb = pd.read_table(targetscan_file,names=["chrom","start","stop","miRNA"],usecols=[0,1,2,3])
families = beddb.miRNA.unique()
f = families.tolist()
return sorted(f[1:])
if bedtracks_widget.value=="Targetscan":
miRNA_widget.description = "miRNA Family"
miRNAwrapper.layout.display=""
tsfamilies = get_targetscan_families(bedfiles_allowed[bedtracks_widget.value]["bedfile"])
miRNA_widget.options = ["ALL"] + tsfamilies
if bedtracks_widget.value=="Other":
otherbedtrackwrapper.layout.display=""
if bedtracks_widget.value in ["None","Peaks"]:
miRNAwrapper.layout.display="none"
miRNA_widget.options = ["None"]
miRNA_widget.value = "None"
if bedtracks_widget.value=="Custom":
miRNAwrapper.layout.display=""
if os.path.isdir(w_default_folder.value) and w_default_folder.value[-1] == "/" :
w_default_folder_valid.value=True
track_folder = w_default_folder.value
all_files = os.listdir(track_folder)
file_names = []
file_types = ["bed"]
for f in all_files:
if f.split(".")[-1] in file_types:
file_names.append(f)
miRNA_widget.options = file_names
miRNA_widget.description = "Bedfile"
else:
w_default_folder_valid.value=False
file_names = []
miRNA_widget.options =[]
# def update_bedfile_other(*args):
# if bedtracks_widget.value == "Other":
bedtracks_widget.observe(update_miRNAlist,'value')
#otherbedtrackwrapper.observe(update_bedfile_other,'value')
display(widgets.HTML(value="<h3>Files</h3>"))
display(bedtracks_widget)
otherbedtrackwrapper = widgets.VBox([other_bedtracks_widget])
display(otherbedtrackwrapper)
otherbedtrackwrapper.layout.display="none"
# Puts miRNA_widget in a VBox that can have its display toggled.
miRNAwrapper = widgets.VBox([miRNA_widget])
display(miRNAwrapper)
miRNAwrapper.layout.display="none"
def miRNAUpdater(bedfile,miRNApick):
print(bedfile)
miRNA_widget.layout.display=""
# interact(miRNAUpdater,
# bedfile=bedtracks_widget,
# miRNApick = miRNA_widget,
# continuous_update=False,
# font_size=4)
miRNA_widget.layout.width="100%"
file_widget.layout.width="100%"
def update_filelist(*args):
if os.path.isdir(w_default_folder.value) and w_default_folder.value[-1] == "/" :
w_default_folder_valid.value=True
print w_default_folder.value
track_folder = w_default_folder.value
all_files = os.listdir(track_folder)
file_names = []
file_types = ["bam","bw"]
for f in all_files:
if f.split(".")[-1] in file_types:
file_names.append(f)
file_widget.options = file_names
else:
w_default_folder_valid.value=False
file_names = []
file_widget.options =[]
w_default_folder.observe(update_filelist,'value')
# Display #
display(widgets.HTML(value="Alignments to Use"))
display(file_widget)
display(w_as_selectors)
display(widgets.HTML(value="<h3>Formatting</h3>"))
display(selectors1)
display(widgets.HTML(value="<h4>Color Picker</h4>"))
display(selectors_color)
display(widgets.HTML(value="<h4>Output Format</h4>"))
display(selectors2)
display(hboxoutputfolder)
display(widgets.HTML(value="<br><br>"))
accord.selected_index=1 #starts the accordian closed.
###### LAUNCH BUTTON #####
button = widgets.Button(description="Graph!")
display(button)
def on_button_clicked(b):
clear_output()
## SANITY CHECKS ###
if not len(file_widget.value)>0:
print "ERROR: It's required to pick a .bam alignment!"
return
#####################
if lookuptype_widget.value=="location":
chrom,start,stop = ct.easylocation(location_widget.value)
strand = strand_widget.value
elif lookuptype_widget.value=="geneid":
chrom,start,stop,value,strand = df_refseq_3utr.loc[refseqid_widget.value]
if bedtracks_widget.value == "None":
bedtrack=False
bedfile=None
bedtype=None
name=None
elif bedtracks_widget.value == "Peaks":
bedtrack=True
bedfile = bedfiles_allowed[bedtracks_widget.value]["bedfile"]
bedtype = "bed"
name = "Peaks"
elif bedtracks_widget.value == "Other":
bedtrack=True
bedfile = other_bedtracks_widget.value
bedtype = "bed"
name = "Bedtrack"
elif bedtracks_widget.value == "Targetscan":
bedtrack=True
bedfile = bedfiles_allowed[bedtracks_widget.value]["bedfile"]
if miRNA_widget.value == "ALL":
bedtype = bedfiles_allowed[bedtracks_widget.value]["bedtype"]
name = bedfiles_allowed[bedtracks_widget.value]["name"]
else:
bedtype = "targetscan"
name = miRNA_widget.value
elif bedtracks_widget.value == "Custom":
bedtrack=True
bedfile = w_default_folder.value+miRNA_widget.value
bedtype = "bed"
name = miRNA_widget.value
track_type=[]
for t in w_antisense_check.options:
if t in w_antisense_check.value:
track_type.append("as")
else:
track_type.append("s")
geneid= geneid_widget.value
refseqid = refseqid_widget.value
track_names = [f for f in w_antisense_check.options if f.split(".")[-1]=="bam"]
bigwignames = [f for f in w_antisense_check.options if f.split(".")[-1]=="bw"]
track_files = [w_default_folder.value + f for f in track_names]
bigwigfiles = [w_default_folder.value + f for f in bigwignames]
depths = get_depth_data(track_files,track_names,chrom,start,stop,strand,track_type)
wig_df_list = get_wig_data(bigwigfiles,bigwignames,chrom,start,stop)
outputformat = w_fileformat.value
figwidth = w_xscale.value
dpi = w_dpi.value
LeftToRight = w_invert.value
annotate_bed = w_labels.value
staggerbed = w_stagger.value
refseqtrack = w_refseq.value
shade_by_bed = w_shadeBed.value
outputsuffix = w_filesuffix.value
staticaxes = w_staticaxes.value
axis_off= not w_boundingbox.value
fontsize = w_fontsize.value
legend = w_legend.value
figheight = 2.5*len(w_antisense_check.options)
output_folder = w_outputfolder.value
color_values = [w_cp1.value,w_cp2.value,w_cp3.value]
shade = itertools.cycle(color_values)
colors = itertools.cycle([spectra.html(i).darken(20).hexcode for i in color_values])
plot(figwidth,figheight,refseqtrack,LeftToRight,strand,depths,
colors,shade,limits,bedtrack,start,stop,staggerbed,bigwignames,
wig_df_list,shade_by_bed,output_folder,geneid,outputsuffix,outputformat,dpi,track_names,axis_off,
legend,staticaxes,bedfile,bedtype,name,chrom,refseqid,annotate_bed,fontsize)
button.on_click(on_button_clicked)