2626import SigProfilerAssignment
2727import SigProfilerAssignment .DecompositionPlots
2828from SigProfilerAssignment .DecompositionPlots import SigProfilerPlottingMatrix as mPlt
29- from SigProfilerAssignment .DecompositionPlots import PlotDecomposition_SBS96 as spd_96
30- from SigProfilerAssignment .DecompositionPlots import PlotDecomposition_SBS288 as spd_288
3129from SigProfilerAssignment .DecompositionPlots import (
30+ PlotDecomposition_SBS96 as spd_96 ,
31+ PlotDecomposition_SBS288 as spd_288 ,
3232 PlotDecomposition_SBS1536 as spd_1536 ,
33+ PlotDecomposition_DBS78 as spd_78 ,
34+ PlotDecomposition_ID83 as spd_83 ,
35+ PlotDecomposition_CNV48 as cnv_48 ,
36+ PlotDecomposition_SV32 as sv_32 ,
3337)
34- from SigProfilerAssignment .DecompositionPlots import PlotDecomposition_DBS78 as spd_78
35- from SigProfilerAssignment .DecompositionPlots import PlotDecomposition_ID83 as spd_83
36- from SigProfilerAssignment .DecompositionPlots import PlotDecomposition_CNV48 as cnv_48
3738from SigProfilerAssignment import decompose_subroutines as sub
3839
3940# imports for working with plots in memory
4344import json
4445import base64
4546
47+
4648# Global Variables
4749SBS_CONTEXTS = ["6" , "24" , "96" , "288" , "384" , "1536" , "6144" ]
4850DBS_CONTEXTS = ["78" , "186" , "1248" , "2976" ]
4951ID_CONTEXTS = ["28" , "83" , "415" ]
5052CNV_CONTEXTS = ["48" ]
53+ SV_CONTEXTS = ["32" ]
5154MTYPE_OPTIONS = [
5255 "6" ,
5356 "24" ,
6265 "186" ,
6366 "1248" ,
6467 "2976" ,
68+ "32" ,
6569]
6670DECOMPOSITION_PATH = SigProfilerAssignment .DecompositionPlots .__path__ [0 ]
6771REFSIG_PATH = os .path .join (
6872 SigProfilerAssignment .__path__ [0 ], "data/Reference_Signatures"
6973)
7074TEMPLATE_PATH = os .path .join (DECOMPOSITION_PATH , "CosmicTemplates" )
7175
72-
7376# Remove templates so that they can be rebuilt
7477def remove_cosmic_templates ():
7578 if not os .path .exists (TEMPLATE_PATH ):
@@ -84,10 +87,11 @@ def remove_cosmic_templates():
8487def install_cosmic_plots (
8588 context_type = "96" , genome_build = "GRCh37" , cosmic_version = "3.4" , exome = False
8689):
90+
8791 if not os .path .exists (TEMPLATE_PATH ):
8892 os .mkdir (TEMPLATE_PATH )
8993
90- # determine if context is from SBS, ID, DBS, or CNV
94+ # determine if context is from SBS, ID, DBS, CNV or SV
9195 context_type_str = ""
9296 if context_type in SBS_CONTEXTS :
9397 context_type_str = "SBS"
@@ -102,6 +106,9 @@ def install_cosmic_plots(
102106 elif context_type in CNV_CONTEXTS :
103107 context_type_str = "CNV"
104108 cosmic_mtype = "48"
109+ elif context_type in SV_CONTEXTS :
110+ context_type_str = "SV"
111+ cosmic_mtype = "32"
105112 else :
106113 raise ValueError ("ERROR: context" , context_type , "not in context lists." )
107114
@@ -138,6 +145,20 @@ def install_cosmic_plots(
138145 genome_build = "GRCh37"
139146 exome_str = ""
140147
148+ # CNV signatures exome=False, genome_build=GRCh37
149+ if context_type in CNV_CONTEXTS :
150+ cosmic_file_name = "COSMIC_v" + str (cosmic_version ) + "_CN_GRCh37.txt"
151+ json_file_name = "COSMIC_v" + str (cosmic_version ) + "_CN_GRCh37.json"
152+ genome_build = "GRCh37"
153+ exome_str = ""
154+
155+ # SV signatures exome=False, genome_build=GRCh37
156+ if context_type in SV_CONTEXTS :
157+ cosmic_file_name = "COSMIC_v" + str (cosmic_version ) + "_SV_GRCh38.txt"
158+ json_file_name = "COSMIC_v" + str (cosmic_version ) + "_SV_GRCh38.json"
159+ genome_build = "GRCh38"
160+ exome_str = ""
161+
141162 # Load cosmic plots if they exist
142163 filename = os .path .join (TEMPLATE_PATH , json_file_name )
143164 if os .path .exists (filename ):
@@ -165,7 +186,6 @@ def install_cosmic_plots(
165186 + exome_str ,
166187 "now..." ,
167188 )
168-
169189 # Create the respective plots
170190 if context_type_str == "SBS" :
171191 cosmic_buff_plots = sigPlt .plotSBS (
@@ -195,6 +215,25 @@ def install_cosmic_plots(
195215 percentage = True ,
196216 savefig_format = "PIL_Image" ,
197217 )
218+ elif context_type_str == "CNV" :
219+ cosmic_buff_plots = sigPlt .plotCNV (
220+ cosmic_file_path ,
221+ "buffer" ,
222+ "buffer" ,
223+ percentage = True ,
224+ aggregate = False ,
225+ read_from_file = False ,
226+ savefig_format = "PIL_Image" ,
227+ )
228+ elif context_type_str == "SV" :
229+ cosmic_buff_plots = sigPlt .plotSV (
230+ cosmic_file_path ,
231+ "buffer" ,
232+ "buffer" ,
233+ percentage = True ,
234+ aggregate = False ,
235+ savefig_format = "PIL_Image" ,
236+ )
198237
199238 # Process the plots to be stored in JSON file
200239 cosmic_img_dict = {}
@@ -413,8 +452,33 @@ def genCNV_pngs(denovo_mtx, basis_mtx, output_path, project, mtype):
413452 return denovo_plots , basis_plots
414453
415454
455+ def genSV_pngs (denovo_mtx , basis_mtx , output_path , project , mtype ):
456+ denovo_plots = dict ()
457+ basis_plots = dict ()
458+ denovo_plots = sigPlt .plotSV (
459+ denovo_mtx ,
460+ output_path ,
461+ project ,
462+ percentage = True ,
463+ aggregate = False ,
464+ savefig_format = "PIL_Image" ,
465+ )
466+
467+ if basis_mtx is not None :
468+ basis_plots = sigPlt .plotSV (
469+ basis_mtx ,
470+ output_path ,
471+ project ,
472+ percentage = True ,
473+ aggregate = False ,
474+ savefig_format = "PIL_Image" ,
475+ )
476+ return denovo_plots , basis_plots
477+
478+
416479# signames, weights
417480def gen_sub_plots (denovo_mtx , basis_mtx , output_path , project , mtype , ss_decomp ):
481+
418482 # Make output directory
419483 if not os .path .exists (output_path ):
420484 os .makedirs (output_path )
@@ -442,6 +506,11 @@ def gen_sub_plots(denovo_mtx, basis_mtx, output_path, project, mtype, ss_decomp)
442506 denovo_mtx , basis_mtx , output_path , project , mtype
443507 )
444508 return denovo_plots , basis_plots
509+ elif mtype in SV_CONTEXTS :
510+ denovo_plots , basis_plots = genSV_pngs (
511+ denovo_mtx , basis_mtx , output_path , project , mtype
512+ )
513+ return denovo_plots , basis_plots
445514
446515 else :
447516 print ("ERROR: mtype is " + mtype + " and is not yet supported." )
@@ -510,6 +579,15 @@ def gen_reconstructed_png_percent(
510579 read_from_file = False ,
511580 savefig_format = "PIL_Image" ,
512581 )
582+ elif mtype in SV_CONTEXTS :
583+ reconstruction_plot = sigPlt .plotSV (
584+ reconstruction_mtx ,
585+ output_path ,
586+ "reconstruction_" + project ,
587+ percentage = True ,
588+ aggregate = False ,
589+ savefig_format = "PIL_Image" ,
590+ )
513591 else :
514592 print ("ERROR: mtype is " + mtype + " and is not yet supported." )
515593
@@ -591,6 +669,16 @@ def gen_reconstructed_png_numerical(
591669 read_from_file = False ,
592670 savefig_format = "PIL_Image" ,
593671 )
672+ elif mtype in SV_CONTEXTS :
673+ reconstruction_plot = sigPlt .plotSV (
674+ reconstruction_mtx ,
675+ output_path ,
676+ "reconstruction_" + project ,
677+ percentage = True ,
678+ aggregate = False ,
679+ read_from_file = False ,
680+ savefig_format = "PIL_Image" ,
681+ )
594682 else :
595683 print ("ERROR: mtype is " + mtype + " and is not yet supported." )
596684
@@ -612,6 +700,7 @@ def gen_decomposition(
612700 cosmic_version = None ,
613701 custom_text = None ,
614702):
703+
615704 """
616705 Generate the correct plot based on mtype.
617706
@@ -744,6 +833,22 @@ def gen_decomposition(
744833 custom_text ,
745834 )
746835 return byte_plot
836+ elif mtype == "32" :
837+ byte_plot = sv_32 .gen_decomposition (
838+ denovo_name ,
839+ basis_names ,
840+ weights ,
841+ output_path ,
842+ project ,
843+ denovo_plots_dict ,
844+ basis_plots_dict ,
845+ reconstruction_plot_dict ,
846+ reconstruction ,
847+ statistics ,
848+ cosmic_version ,
849+ custom_text ,
850+ )
851+ return byte_plot
747852
748853
749854def run_PlotDecomposition (
@@ -773,8 +878,7 @@ def run_PlotDecomposition(
773878
774879 basis_mtx: Pandas Dataframe. This format represents the catalog of mutations seperated by tab.
775880
776- basis_names: List of Strings. The names of the samples in denovo_mtx that
777- the denovo_name sample from denovo_mtx is decomposed into.
881+ basis_names: List of Strings. The names of the samples in denovo_mtx that the denovo_name sample from denovo_mtx is decomposed into.
778882 ie. basis_names=["SBS1", "SBS5", "SBS15", "SBS20"]
779883
780884 weights: List of Strings. The percentile weight corresponding to each basis
@@ -796,17 +900,9 @@ def run_PlotDecomposition(
796900 None.
797901 """
798902 # Create the denovo plots and load basis plots
799- if mtype != "48" :
800- denovo_plots_dict = gen_sub_plots (
801- denovo_mtx , None , output_path , project , mtype , ss_decomp = False
802- )
803- denovo_plots_dict = denovo_plots_dict [0 ]
804- else :
805- # cnv basis plots need to be generated and not loaded
806- denovo_plots_dict , basis_plots_dict = gen_sub_plots (
807- denovo_mtx , basis_mtx , output_path , project , mtype , ss_decomp = False
808- )
809- # Create the matrix and plot for the reconstructed matrix
903+ denovo_plots_dict , basis_plots_dict = gen_sub_plots (
904+ denovo_mtx , basis_mtx , output_path , project , mtype , ss_decomp = False
905+ )
810906 reconstructed_mtx , reconstruction_plot_dict = gen_reconstructed_png_percent (
811907 denovo_name , basis_mtx , basis_names , weights , output_path , project , mtype
812908 )
@@ -816,14 +912,13 @@ def run_PlotDecomposition(
816912 # Convert dictionary of bytes to dictionary of images
817913 denovo_plots_dict = convert_to_imgReaderDict (denovo_plots_dict )
818914 # Load in the COSMIC plots
819- if mtype != "48" :
820- basis_plots_dict = install_cosmic_plots (
821- context_type = mtype ,
822- genome_build = genome_build ,
823- cosmic_version = cosmic_version ,
824- exome = exome ,
825- )
826- basis_plots_dict = {key : basis_plots_dict [key ] for key in basis_names }
915+ basis_plots_dict = install_cosmic_plots (
916+ context_type = mtype ,
917+ genome_build = genome_build ,
918+ cosmic_version = cosmic_version ,
919+ exome = exome ,
920+ )
921+ basis_plots_dict = {key : basis_plots_dict [key ] for key in basis_names }
827922 basis_plots_dict = convert_to_imgReaderDict (basis_plots_dict )
828923 # Generate the reconstruction plot
829924 reconstruction_plot_dict = convert_to_imgReaderDict (reconstruction_plot_dict )
0 commit comments