Skip to content

Commit 9a72fae

Browse files
authored
Merge pull request #74 from AlexandrovLab/u45
U45
2 parents 77fd74d + 96be0ea commit 9a72fae

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

SigProfilerAssignment/DecompositionPlots/PlotDecomposition.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +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")
129129
cosmic_buff_plots = sigPlt.plotDBS(cosmic_mtx, "buffer",
130130
"buffer", cosmic_mtype, percentage=True,
131-
savefig_format="buffer_stream")
131+
savefig_format="PIL_Image")
132132
elif context_type_str == "ID":
133133
cosmic_buff_plots = sigPlt.plotID(cosmic_file_path, "buffer",
134134
"buffer", cosmic_mtype, percentage=True,
135-
savefig_format="buffer_stream")
135+
savefig_format="PIL_Image")
136136

137137
# Process the plots to be stored in JSON file
138138
cosmic_img_dict = {}
139139
for tmp_plot in cosmic_buff_plots.keys():
140-
# start at beggining of binary file
140+
# Convert to bytesIO and encode to base64
141+
plot_bytes = io.BytesIO()
141142
seek_start = cosmic_buff_plots[tmp_plot].seek(0)
142-
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())
143145
cosmic_img_dict[tmp_plot] = encoded.decode('ascii')
146+
plot_bytes.close()
144147

145148
# JSON output processing
146149
json_object = json.dumps(cosmic_img_dict)
@@ -152,22 +155,13 @@ def install_cosmic_plots(context_type="96", genome_build="GRCh37", cosmic_versio
152155
exome_str, "have been successfully installed.")
153156
return cosmic_buff_plots
154157

155-
156-
# Helper function for converting BytesIO to image so it can be plotted by reportlab
157-
def bytes_to_img(byte_png):
158-
byte_png.seek(0)
159-
tmp_im=Image.open(byte_png)
160-
image = ImageReader(tmp_im)
161-
return image
162158

163-
# Helper function to convert byte array to image array
164-
def open_byte_to_img_dict(byte_dict):
165-
166-
img_dict = dict()
167-
for name in byte_dict.keys():
168-
tmp_img = bytes_to_img(byte_dict[name])
169-
img_dict[name] = tmp_img
170-
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
171165

172166
def calculate_similarities(denovo, denovo_name, est_denovo):
173167
from numpy import inf
@@ -240,33 +234,33 @@ def genSBS_pngs(denovo_mtx, basis_mtx, output_path, project, mtype, ss_decomp=Fa
240234
percentage=True)
241235
if basis_mtx is not None:
242236
basis_plots = sigPlt.plotSBS(basis_mtx, output_path, project, "96",
243-
percentage=True, savefig_format="buffer_stream")
237+
percentage=True, savefig_format="PIL_Image")
244238
elif mtype == "96":
245239
denovo_plots = sigPlt.plotSBS(denovo_mtx, output_path, project, mtype,
246-
percentage=(not ss_decomp), savefig_format="buffer_stream")
240+
percentage=(not ss_decomp), savefig_format="PIL_Image")
247241
if basis_mtx is not None:
248242
basis_plots = sigPlt.plotSBS(basis_mtx, output_path, project, mtype,
249-
percentage=True, savefig_format="buffer_stream")
243+
percentage=True, savefig_format="PIL_Image")
250244
return denovo_plots,basis_plots
251245

252246
def genDBS_pngs(denovo_mtx, basis_mtx, output_path, project, mtype):
253247
denovo_plots = dict()
254248
basis_plots = dict()
255249
denovo_plots = sigPlt.plotDBS(denovo_mtx, output_path, project, mtype,
256-
percentage=True, savefig_format="buffer_stream")
250+
percentage=True, savefig_format="PIL_Image")
257251
if basis_mtx is not None:
258252
basis_plots = sigPlt.plotDBS(basis_mtx, output_path, project, mtype,
259-
percentage=True, savefig_format="buffer_stream")
253+
percentage=True, savefig_format="PIL_Image")
260254
return denovo_plots,basis_plots
261255

262256
def genID_pngs(denovo_mtx, basis_mtx, output_path, project, mtype):
263257
denovo_plots = dict()
264258
basis_plots = dict()
265259
denovo_plots = sigPlt.plotID(denovo_mtx, output_path, project, mtype,
266-
percentage=True, savefig_format="buffer_stream")
260+
percentage=True, savefig_format="PIL_Image")
267261
if basis_mtx is not None:
268262
basis_plots = sigPlt.plotID(basis_mtx, output_path, project, mtype,
269-
percentage=True, savefig_format="buffer_stream")
263+
percentage=True, savefig_format="PIL_Image")
270264
return denovo_plots,basis_plots
271265

272266
def genCNV_pngs(denovo_mtx, basis_mtx, output_path, project, mtype):
@@ -314,33 +308,39 @@ def gen_sub_plots(denovo_mtx, basis_mtx, output_path, project, mtype, ss_decomp)
314308

315309

316310
# generate the plot for the reconstruction
317-
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):
318313
reconstruction_plot=dict()
319314
mut_col = basis_mtx.iloc[:,0]
320315

321316
recon_plot = basis_mtx[basis_names[0]]*float(weights[0].strip("%"))/100
322317

323318
for i in range(1,len(weights)):
324-
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)
325320

326321
recon_plot = pd.Series(recon_plot, name=denovo_name)
327322
reconstruction_mtx = pd.concat([mut_col, recon_plot], axis=1)
328323
if mtype in SBS_CONTEXTS:
329324
if mtype == "1536" or mtype == "288":
330-
reconstruction_plot=sigPlt.plotSBS(reconstruction_mtx, output_path, \
331-
"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')
332328
else:
333-
reconstruction_plot=sigPlt.plotSBS(reconstruction_mtx, output_path, \
334-
"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')
335332
elif mtype in DBS_CONTEXTS:
336333
reconstruction_plot=sigPlt.plotDBS(reconstruction_mtx, output_path,
337-
"reconstruction_" + project, mtype, percentage=True, savefig_format='buffer_stream')
334+
"reconstruction_" + project, mtype, percentage=True,
335+
savefig_format='PIL_Image')
338336
elif mtype in ID_CONTEXTS:
339337
reconstruction_plot=sigPlt.plotID(reconstruction_mtx, output_path,
340-
"reconstruction_" + project, mtype, percentage=True, savefig_format='buffer_stream')
338+
"reconstruction_" + project, mtype, percentage=True,
339+
savefig_format='PIL_Image')
341340
elif mtype in CNV_CONTEXTS:
342-
reconstruction_plot = sigPlt.plotCNV(reconstruction_mtx, output_path, "reconstruction_"+project, plot_type="pdf", \
343-
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)
344344
else:
345345
print("ERROR: mtype is " + mtype + " and is not yet supported.")
346346

@@ -366,13 +366,13 @@ def gen_reconstructed_png_numerical(denovo_mtx, denovo_name, basis_mtx, basis_na
366366
else:
367367
reconstruction_plot=sigPlt.plotSBS(reconstruction_mtx, output_path,
368368
"reconstruction_" + project, mtype, percentage=False,
369-
savefig_format="buffer_stream")
369+
savefig_format="PIL_Image")
370370
elif mtype in DBS_CONTEXTS:
371371
reconstruction_plot=sigPlt.plotDBS(reconstruction_mtx, output_path,
372-
"reconstruction_" + project, mtype, percentage=False, savefig_format="buffer_stream")
372+
"reconstruction_" + project, mtype, percentage=False, savefig_format="PIL_Image")
373373
elif mtype in ID_CONTEXTS:
374374
reconstruction_plot=sigPlt.plotID(reconstruction_mtx, output_path,
375-
"reconstruction_" + project, mtype, percentage=False, savefig_format="buffer_stream")
375+
"reconstruction_" + project, mtype, percentage=False, savefig_format="PIL_Image")
376376
elif mtype in CNV_CONTEXTS:
377377
reconstruction_plot = sigPlt.plotCNV(reconstruction_mtx, output_path,
378378
"reconstruction_"+project, plot_type="pdf", percentage=True,
@@ -510,24 +510,26 @@ def run_PlotDecomposition(denovo_mtx, denovo_name, basis_mtx, basis_names,
510510
present_sigs=np.array(basis_mtx[basis_names])
511511
reconstructed_mtx = np.dot(present_sigs,nonzero_exposures)
512512
# Convert dictionary of bytes to dictionary of images
513-
denovo_plots_dict = open_byte_to_img_dict(denovo_plots_dict)
513+
denovo_plots_dict = convert_to_imgReaderDict(denovo_plots_dict)
514514
# Load in the COSMIC plots
515515
if mtype != "48":
516516
basis_plots_dict = install_cosmic_plots(context_type=mtype,
517517
genome_build=genome_build, cosmic_version=cosmic_version,
518518
exome=exome)
519519
basis_plots_dict = {key: basis_plots_dict[key] for key in basis_names}
520-
basis_plots_dict = open_byte_to_img_dict(basis_plots_dict)
520+
basis_plots_dict = convert_to_imgReaderDict(basis_plots_dict)
521521
# Generate the reconstruction plot
522-
reconstruction_plot_dict = open_byte_to_img_dict(reconstruction_plot_dict)
522+
reconstruction_plot_dict = convert_to_imgReaderDict(reconstruction_plot_dict)
523523
# Get the reconstruction statistics
524524
statistics=calculate_similarities(denovo_mtx, denovo_name, reconstructed_mtx)
525525
# Return the decomposition plot as a byte array
526526
byte_plot = gen_decomposition(denovo_name, basis_names, weights, output_path, project,
527527
mtype, denovo_plots_dict, basis_plots_dict, reconstruction_plot_dict,
528528
reconstruction=True, statistics=statistics, cosmic_version=cosmic_version,
529529
custom_text=custom_text)
530-
530+
# Clear the plotting memory
531+
sigPlt.clear_plotting_memory()
532+
531533
return byte_plot
532534

533535
# context="96", genome_build="GRCh37", cosmic_version="3.3", exome=False
@@ -590,18 +592,19 @@ def run_PlotSSDecomposition(denovo_mtx, denovo_name, basis_mtx, basis_names, \
590592
denovo_mtx, denovo_name, basis_mtx,
591593
basis_names, weights, output_path, project, context_type)
592594

593-
denovo_plots_dict = open_byte_to_img_dict(denovo_plots_dict)
595+
denovo_plots_dict = convert_to_imgReaderDict(denovo_plots_dict)
594596
# subset basis_plots_dict to only the plots used
595597
basis_plots_dict = {key: basis_plots_dict[key] for key in basis_names}
596-
basis_plots_dict = open_byte_to_img_dict(basis_plots_dict)
598+
basis_plots_dict = convert_to_imgReaderDict(basis_plots_dict)
597599

598-
reconstruction_plot_dict = open_byte_to_img_dict(reconstruction_plot_dict)
600+
reconstruction_plot_dict = convert_to_imgReaderDict(reconstruction_plot_dict)
599601

600602
statistics=calculate_similarities(denovo_mtx, denovo_name, reconstructed_mtx[denovo_name])
601603
byte_plot = gen_decomposition(denovo_name, basis_names, weights, output_path, project,
602604
context_type, denovo_plots_dict, basis_plots_dict, reconstruction_plot_dict,
603605
reconstruction=True, statistics=statistics, cosmic_version=cosmic_version,
604606
custom_text=custom_text)
605-
607+
# Clear the plotting memory
608+
sigPlt.clear_plotting_memory()
606609

607610
return byte_plot

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if os.path.exists("dist"):
77
shutil.rmtree("dist")
88

9-
VERSION = '0.0.25'
9+
VERSION = '0.0.26'
1010

1111

1212
def write_version_py(filename='SigProfilerAssignment/version.py'):
@@ -15,7 +15,7 @@ def write_version_py(filename='SigProfilerAssignment/version.py'):
1515
# THIS FILE IS GENERATED FROM SigProfilerAssignment SETUP.PY
1616
short_version = '%(version)s'
1717
version = '%(version)s'
18-
Update = 'v0.0.25: DBS Decomposition Plots Plotting'
18+
Update = 'v0.0.26: Clear decomposition plots from memory'
1919
2020
2121
"""
@@ -31,8 +31,7 @@ def write_version_py(filename='SigProfilerAssignment/version.py'):
3131
'numpy>=1.21.2',
3232
'pandas>=1.2.4',
3333
'SigProfilerMatrixGenerator>=1.2.13',
34-
'sigProfilerPlotting>=1.3.10',
35-
'pillow>=9.1.1',
34+
'sigProfilerPlotting>=1.3.11',
3635
'statsmodels>=0.9.0',
3736
'scikit-learn>=0.24.2',
3837
'psutil>=5.6.1',

0 commit comments

Comments
 (0)