3535import logging
3636
3737try :
38- import bokeh .plotting as bp
38+ try :
39+ import bokeh .plotting as bp
40+ except :
41+ bokeh_import_error = ('Bokeh module not found; consider running the following command to install: '
42+ 'sudo pip install bokeh' )
43+ raise
44+
45+ try :
46+ # bamliquidatorbatch was originally developed for bokeh 0.4.4, but
47+ # around version 0.7 the bokeh API had some breaking changes (like requiring the explicit use of vplot).
48+ # I confirmed things work with version 0.9.3, and I hope future versions maintain compatability.
49+ bp .vplot
50+ except :
51+ bokeh_import_error = ('Bokeh version is incompatible; consider running the following command to upgrade: '
52+ 'sudo pip install bokeh --upgrade' )
53+ raise
3954except :
4055 bp = None
4156
@@ -105,12 +120,13 @@ def file_keys(counts, cell_type):
105120 return file_keys_memo [cell_type ]
106121
107122def plot_summaries (output_directory , normalized_counts , chromosomes ):
108- bp .output_file (output_directory + "/summary.html" )
109-
123+ bp .output_file (output_directory + "/summary.html" , mode = "cdn" )
124+ plots = []
110125 for chromosome in chromosomes :
111- plot_summary (normalized_counts , chromosome )
112-
113- bp .save ()
126+ plot = plot_summary (normalized_counts , chromosome )
127+ if plot :
128+ plots .append (plot )
129+ bp .save (bp .vplot (* plots ))
114130
115131def plot_summary (normalized_counts , chromosome ):
116132 logging .debug (" - plotting %s summary" , chromosome )
@@ -126,14 +142,22 @@ def plot_summary(normalized_counts, chromosome):
126142 logging .info ("-- skipping plotting %s because not enough bins (only %d)" , chromosome , num_bins )
127143 return
128144
129- overall = bp .scatter (chromosome_count_by_bin .keys (), chromosome_count_by_bin .values ())
130- overall .title = chromosome + " counts per bin across all bam files"
145+ plot = bp .figure ()
146+ plot .title = chromosome + " counts per bin across all bam files"
147+ plot .scatter (chromosome_count_by_bin .keys (), chromosome_count_by_bin .values ())
148+ return plot
131149
132150def plot (output_directory , normalized_counts , chromosome , cell_types ):
133- bp .output_file (output_directory + "/" + chromosome + ".html" )
134-
135- plot_summary (normalized_counts , chromosome )
136-
151+ bp .output_file (output_directory + "/" + chromosome + ".html" , mode = "cdn" )
152+ plots = []
153+
154+ summary = plot_summary (normalized_counts , chromosome )
155+ if summary :
156+ plots .append (summary )
157+ else :
158+ # if summary can't be plotted, then rest probably can't be plotted either,
159+ # so return without even saving the file (the file is never created on disk if not saved)
160+ return
137161 for cell_type in cell_types :
138162 logging .debug (" - plotting %s" , cell_type )
139163
@@ -146,10 +170,11 @@ def plot(output_directory, normalized_counts, chromosome, cell_types):
146170 bin_number .append (row ["bin_number" ])
147171 count .append (row ["count" ])
148172
149- cell_type_plot = bp .scatter (bin_number , count )
150- cell_type_plot .title = "%s counts per bin" % cell_type
151-
152- bp .save ()
173+ plot = bp .figure ()
174+ plot .title = "%s counts per bin" % cell_type
175+ plot .scatter (bin_number , count )
176+ plots .append (plot )
177+ bp .save (bp .vplot (* plots ))
153178
154179def populate_normalized_counts (normalized_counts , counts , file_key , bin_size , files ):
155180 total_count = length_for_file_key (files , file_key )
@@ -375,10 +400,9 @@ def normalize_plot_and_summarize(counts_file, output_directory, bin_size, skip_p
375400
376401 if not skip_plot :
377402 if bp is None :
378- logging .error ('Skipping plotting because plots require bokeh and it is not installed -- '
379- 'see https://github.com/BradnerLab/pipeline/wiki/bamliquidator#Install . '
380- 'Consider running the following command to install bokeh: '
381- 'sudo pip install bokeh==0.4.4 "openpyxl>=1.6.1,<2.0.0"' )
403+ logging .error ('Skipping plotting because plots require a compatible version of bokeh -- '
404+ 'see https://github.com/BradnerLab/pipeline/wiki/bamliquidator#Install . %s'
405+ % bokeh_import_error )
382406 else :
383407 logging .info ("Plotting" )
384408 for chromosome in chromosomes :
0 commit comments