@@ -123,23 +123,27 @@ def install_cosmic_plots(context_type="96", genome_build="GRCh37", cosmic_versio
123123 if context_type_str == "SBS" :
124124 cosmic_buff_plots = sigPlt .plotSBS (cosmic_file_path , "buffer" ,
125125 "buffer" , cosmic_mtype , percentage = True ,
126- savefig_format = "buffer_stream " )
126+ savefig_format = "PIL_Image " )
127127 elif context_type_str == "DBS" :
128128 cosmic_mtx = pd .read_csv (cosmic_file_path , sep = "\t " )
129- cosmic_buff_plots = mPlt .plotDBS (cosmic_mtx , "buffer" ,
130- "buffer" , cosmic_mtype , percentage = True )
129+ cosmic_buff_plots = sigPlt .plotDBS (cosmic_mtx , "buffer" ,
130+ "buffer" , cosmic_mtype , percentage = True ,
131+ savefig_format = "PIL_Image" )
131132 elif context_type_str == "ID" :
132133 cosmic_buff_plots = sigPlt .plotID (cosmic_file_path , "buffer" ,
133134 "buffer" , cosmic_mtype , percentage = True ,
134- savefig_format = "buffer_stream " )
135+ savefig_format = "PIL_Image " )
135136
136137 # Process the plots to be stored in JSON file
137138 cosmic_img_dict = {}
138139 for tmp_plot in cosmic_buff_plots .keys ():
139- # start at beggining of binary file
140+ # Convert to bytesIO and encode to base64
141+ plot_bytes = io .BytesIO ()
140142 seek_start = cosmic_buff_plots [tmp_plot ].seek (0 )
141- encoded = base64 .b64encode (cosmic_buff_plots [tmp_plot ].getvalue ())
143+ cosmic_buff_plots [tmp_plot ].save (plot_bytes , "png" )
144+ encoded = base64 .b64encode (plot_bytes .getvalue ())
142145 cosmic_img_dict [tmp_plot ] = encoded .decode ('ascii' )
146+ plot_bytes .close ()
143147
144148 # JSON output processing
145149 json_object = json .dumps (cosmic_img_dict )
@@ -151,22 +155,13 @@ def install_cosmic_plots(context_type="96", genome_build="GRCh37", cosmic_versio
151155 exome_str , "have been successfully installed." )
152156 return cosmic_buff_plots
153157
154-
155- # Helper function for converting BytesIO to image so it can be plotted by reportlab
156- def bytes_to_img (byte_png ):
157- byte_png .seek (0 )
158- tmp_im = Image .open (byte_png )
159- image = ImageReader (tmp_im )
160- return image
161158
162- # Helper function to convert byte array to image array
163- def open_byte_to_img_dict (byte_dict ):
164-
165- img_dict = dict ()
166- for name in byte_dict .keys ():
167- tmp_img = bytes_to_img (byte_dict [name ])
168- img_dict [name ] = tmp_img
169- return img_dict
159+ # Convert array of Images to ImageReader array for ReportLab
160+ def convert_to_imgReaderDict (image_dict ):
161+ imgReader_dict = dict ()
162+ for name in image_dict .keys ():
163+ imgReader_dict [name ] = ImageReader (image_dict [name ])
164+ return imgReader_dict
170165
171166def calculate_similarities (denovo , denovo_name , est_denovo ):
172167 from numpy import inf
@@ -239,33 +234,33 @@ def genSBS_pngs(denovo_mtx, basis_mtx, output_path, project, mtype, ss_decomp=Fa
239234 percentage = True )
240235 if basis_mtx is not None :
241236 basis_plots = sigPlt .plotSBS (basis_mtx , output_path , project , "96" ,
242- percentage = True , savefig_format = "buffer_stream " )
237+ percentage = True , savefig_format = "PIL_Image " )
243238 elif mtype == "96" :
244239 denovo_plots = sigPlt .plotSBS (denovo_mtx , output_path , project , mtype ,
245- percentage = (not ss_decomp ), savefig_format = "buffer_stream " )
240+ percentage = (not ss_decomp ), savefig_format = "PIL_Image " )
246241 if basis_mtx is not None :
247242 basis_plots = sigPlt .plotSBS (basis_mtx , output_path , project , mtype ,
248- percentage = True , savefig_format = "buffer_stream " )
243+ percentage = True , savefig_format = "PIL_Image " )
249244 return denovo_plots ,basis_plots
250245
251246def genDBS_pngs (denovo_mtx , basis_mtx , output_path , project , mtype ):
252247 denovo_plots = dict ()
253248 basis_plots = dict ()
254- denovo_plots = mPlt .plotDBS (denovo_mtx , output_path , project , mtype ,
255- percentage = True )
249+ denovo_plots = sigPlt .plotDBS (denovo_mtx , output_path , project , mtype ,
250+ percentage = True , savefig_format = "PIL_Image" )
256251 if basis_mtx is not None :
257- basis_plots = mPlt .plotDBS (basis_mtx , output_path , project , mtype ,
258- percentage = True )
252+ basis_plots = sigPlt .plotDBS (basis_mtx , output_path , project , mtype ,
253+ percentage = True , savefig_format = "PIL_Image" )
259254 return denovo_plots ,basis_plots
260255
261256def genID_pngs (denovo_mtx , basis_mtx , output_path , project , mtype ):
262257 denovo_plots = dict ()
263258 basis_plots = dict ()
264259 denovo_plots = sigPlt .plotID (denovo_mtx , output_path , project , mtype ,
265- percentage = True , savefig_format = "buffer_stream " )
260+ percentage = True , savefig_format = "PIL_Image " )
266261 if basis_mtx is not None :
267262 basis_plots = sigPlt .plotID (basis_mtx , output_path , project , mtype ,
268- percentage = True , savefig_format = "buffer_stream " )
263+ percentage = True , savefig_format = "PIL_Image " )
269264 return denovo_plots ,basis_plots
270265
271266def genCNV_pngs (denovo_mtx , basis_mtx , output_path , project , mtype ):
@@ -313,33 +308,39 @@ def gen_sub_plots(denovo_mtx, basis_mtx, output_path, project, mtype, ss_decomp)
313308
314309
315310# generate the plot for the reconstruction
316- def gen_reconstructed_png_percent (denovo_name , basis_mtx , basis_names , weights , output_path , project , mtype ):
311+ def gen_reconstructed_png_percent (denovo_name , basis_mtx , basis_names ,
312+ weights , output_path , project , mtype ):
317313 reconstruction_plot = dict ()
318314 mut_col = basis_mtx .iloc [:,0 ]
319315
320316 recon_plot = basis_mtx [basis_names [0 ]]* float (weights [0 ].strip ("%" ))/ 100
321317
322318 for i in range (1 ,len (weights )):
323- recon_plot = recon_plot + basis_mtx [basis_names [i ]]* (float (weights [i ].strip ("%" ))/ 100 )
319+ recon_plot = recon_plot + basis_mtx [basis_names [i ]]* (float (weights [i ].strip ("%" ))/ 100 )
324320
325321 recon_plot = pd .Series (recon_plot , name = denovo_name )
326322 reconstruction_mtx = pd .concat ([mut_col , recon_plot ], axis = 1 )
327323 if mtype in SBS_CONTEXTS :
328324 if mtype == "1536" or mtype == "288" :
329- reconstruction_plot = sigPlt .plotSBS (reconstruction_mtx , output_path , \
330- "reconstruction_" + project , "96" , percentage = True , savefig_format = 'buffer_stream' )
325+ reconstruction_plot = sigPlt .plotSBS (reconstruction_mtx ,
326+ output_path , "reconstruction_" + project , "96" ,
327+ percentage = True , savefig_format = 'PIL_Image' )
331328 else :
332- reconstruction_plot = sigPlt .plotSBS (reconstruction_mtx , output_path , \
333- "reconstruction_" + project , mtype , percentage = True , savefig_format = 'buffer_stream' )
329+ reconstruction_plot = sigPlt .plotSBS (reconstruction_mtx , output_path ,
330+ "reconstruction_" + project , mtype , percentage = True ,
331+ savefig_format = 'PIL_Image' )
334332 elif mtype in DBS_CONTEXTS :
335- reconstruction_plot = mPlt .plotDBS (reconstruction_mtx , output_path ,
336- "reconstruction_" + project , mtype , percentage = True )
333+ reconstruction_plot = sigPlt .plotDBS (reconstruction_mtx , output_path ,
334+ "reconstruction_" + project , mtype , percentage = True ,
335+ savefig_format = 'PIL_Image' )
337336 elif mtype in ID_CONTEXTS :
338337 reconstruction_plot = sigPlt .plotID (reconstruction_mtx , output_path ,
339- "reconstruction_" + project , mtype , percentage = True , savefig_format = 'buffer_stream' )
338+ "reconstruction_" + project , mtype , percentage = True ,
339+ savefig_format = 'PIL_Image' )
340340 elif mtype in CNV_CONTEXTS :
341- reconstruction_plot = sigPlt .plotCNV (reconstruction_mtx , output_path , "reconstruction_" + project , plot_type = "pdf" , \
342- percentage = True , aggregate = False , read_from_file = False , write_to_file = False )
341+ reconstruction_plot = sigPlt .plotCNV (reconstruction_mtx , output_path ,
342+ "reconstruction_" + project , plot_type = "pdf" , percentage = True ,
343+ aggregate = False , read_from_file = False , write_to_file = False )
343344 else :
344345 print ("ERROR: mtype is " + mtype + " and is not yet supported." )
345346
@@ -365,13 +366,13 @@ def gen_reconstructed_png_numerical(denovo_mtx, denovo_name, basis_mtx, basis_na
365366 else :
366367 reconstruction_plot = sigPlt .plotSBS (reconstruction_mtx , output_path ,
367368 "reconstruction_" + project , mtype , percentage = False ,
368- savefig_format = "buffer_stream " )
369+ savefig_format = "PIL_Image " )
369370 elif mtype in DBS_CONTEXTS :
370- reconstruction_plot = mPlt .plotDBS (reconstruction_mtx , output_path ,
371- "reconstruction_" + project , mtype , percentage = False )
371+ reconstruction_plot = sigPlt .plotDBS (reconstruction_mtx , output_path ,
372+ "reconstruction_" + project , mtype , percentage = False , savefig_format = "PIL_Image" )
372373 elif mtype in ID_CONTEXTS :
373374 reconstruction_plot = sigPlt .plotID (reconstruction_mtx , output_path ,
374- "reconstruction_" + project , mtype , percentage = False )
375+ "reconstruction_" + project , mtype , percentage = False , savefig_format = "PIL_Image" )
375376 elif mtype in CNV_CONTEXTS :
376377 reconstruction_plot = sigPlt .plotCNV (reconstruction_mtx , output_path ,
377378 "reconstruction_" + project , plot_type = "pdf" , percentage = True ,
@@ -493,40 +494,42 @@ def run_PlotDecomposition(denovo_mtx, denovo_name, basis_mtx, basis_names,
493494 -------
494495 None.
495496 """
496-
497497 # Create the denovo plots and load basis plots
498- if mtype == "48" :
499- denovo_plots_dict , basis_plots_dict = gen_sub_plots (denovo_mtx , basis_mtx ,
500- output_path , project , mtype , ss_decomp = False )
501- else :
498+ if mtype != "48" :
502499 denovo_plots_dict = gen_sub_plots (denovo_mtx , None ,
503500 output_path , project , mtype , ss_decomp = False )
504501 denovo_plots_dict = denovo_plots_dict [0 ]
502+ else :
503+ # cnv basis plots need to be generated and not loaded
504+ denovo_plots_dict , basis_plots_dict = gen_sub_plots (denovo_mtx , basis_mtx ,
505+ output_path , project , mtype , ss_decomp = False )
505506 # Create the matrix and plot for the reconstructed matrix
506507 reconstructed_mtx ,reconstruction_plot_dict = gen_reconstructed_png_percent (
507508 denovo_name , basis_mtx , basis_names , weights , output_path , project , mtype )
508509 # Create a subset matrix with the present signatures
509510 present_sigs = np .array (basis_mtx [basis_names ])
510511 reconstructed_mtx = np .dot (present_sigs ,nonzero_exposures )
511512 # Convert dictionary of bytes to dictionary of images
512- denovo_plots_dict = open_byte_to_img_dict (denovo_plots_dict )
513+ denovo_plots_dict = convert_to_imgReaderDict (denovo_plots_dict )
513514 # Load in the COSMIC plots
514515 if mtype != "48" :
515516 basis_plots_dict = install_cosmic_plots (context_type = mtype ,
516517 genome_build = genome_build , cosmic_version = cosmic_version ,
517518 exome = exome )
518519 basis_plots_dict = {key : basis_plots_dict [key ] for key in basis_names }
519- basis_plots_dict = open_byte_to_img_dict (basis_plots_dict )
520+ basis_plots_dict = convert_to_imgReaderDict (basis_plots_dict )
520521 # Generate the reconstruction plot
521- reconstruction_plot_dict = open_byte_to_img_dict (reconstruction_plot_dict )
522+ reconstruction_plot_dict = convert_to_imgReaderDict (reconstruction_plot_dict )
522523 # Get the reconstruction statistics
523524 statistics = calculate_similarities (denovo_mtx , denovo_name , reconstructed_mtx )
524525 # Return the decomposition plot as a byte array
525526 byte_plot = gen_decomposition (denovo_name , basis_names , weights , output_path , project ,
526527 mtype , denovo_plots_dict , basis_plots_dict , reconstruction_plot_dict ,
527528 reconstruction = True , statistics = statistics , cosmic_version = cosmic_version ,
528529 custom_text = custom_text )
529-
530+ # Clear the plotting memory
531+ sigPlt .clear_plotting_memory ()
532+
530533 return byte_plot
531534
532535# context="96", genome_build="GRCh37", cosmic_version="3.3", exome=False
@@ -589,18 +592,19 @@ def run_PlotSSDecomposition(denovo_mtx, denovo_name, basis_mtx, basis_names, \
589592 denovo_mtx , denovo_name , basis_mtx ,
590593 basis_names , weights , output_path , project , context_type )
591594
592- denovo_plots_dict = open_byte_to_img_dict (denovo_plots_dict )
595+ denovo_plots_dict = convert_to_imgReaderDict (denovo_plots_dict )
593596 # subset basis_plots_dict to only the plots used
594597 basis_plots_dict = {key : basis_plots_dict [key ] for key in basis_names }
595- basis_plots_dict = open_byte_to_img_dict (basis_plots_dict )
598+ basis_plots_dict = convert_to_imgReaderDict (basis_plots_dict )
596599
597- reconstruction_plot_dict = open_byte_to_img_dict (reconstruction_plot_dict )
600+ reconstruction_plot_dict = convert_to_imgReaderDict (reconstruction_plot_dict )
598601
599602 statistics = calculate_similarities (denovo_mtx , denovo_name , reconstructed_mtx [denovo_name ])
600603 byte_plot = gen_decomposition (denovo_name , basis_names , weights , output_path , project ,
601604 context_type , denovo_plots_dict , basis_plots_dict , reconstruction_plot_dict ,
602605 reconstruction = True , statistics = statistics , cosmic_version = cosmic_version ,
603606 custom_text = custom_text )
604-
607+ # Clear the plotting memory
608+ sigPlt .clear_plotting_memory ()
605609
606610 return byte_plot
0 commit comments