Skip to content

Commit 83e70aa

Browse files
authored
Merge pull request #177 from AlexandrovLab/custom-sigs
- Add decomposition plot support for custom signatures
2 parents ae8b9d4 + 057ef48 commit 83e70aa

File tree

6 files changed

+398
-725
lines changed

6 files changed

+398
-725
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
## [0.2.5] - 2025-08-07
10+
11+
### Added
12+
- Support for generating decomposition plots for custom signature sets.
13+
14+
### Changed
15+
- Refactored the test script to be more readable and maintainable.
16+
17+
### Fixed
18+
- Removed redundant plotting of COSMIC signatures in the decomposition plots.
19+
920
## [0.2.4] - 2025-08-07
1021

1122
### Added

SigProfilerAssignment/DecompositionPlots/PlotDecomposition.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ def run_PlotDecomposition(
897897
exome=False,
898898
custom_text=None,
899899
volume=None,
900+
use_custom_basis=False,
900901
):
901902
"""
902903
Generates a decomposition plot of the denovo_mtx using the basis_mtx.
@@ -936,16 +937,48 @@ def run_PlotDecomposition(
936937
None.
937938
"""
938939

939-
# Create the denovo plots and load basis plots
940-
denovo_plots_dict, basis_plots_dict = gen_sub_plots(
941-
denovo_mtx,
942-
basis_mtx,
943-
output_path,
944-
project,
945-
mtype,
946-
ss_decomp=False,
947-
volume=volume,
948-
)
940+
# --- Efficiently Generate or Load Plots ---
941+
# This logic avoids redundant plot generation for COSMIC signatures.
942+
if use_custom_basis:
943+
# For custom sets, generate plots for BOTH the de novo and basis matrices.
944+
denovo_plots_dict, basis_plots_dict = gen_sub_plots(
945+
denovo_mtx,
946+
basis_mtx,
947+
output_path,
948+
project,
949+
mtype,
950+
ss_decomp=False,
951+
volume=volume,
952+
)
953+
else: # This is the standard COSMIC case
954+
# For COSMIC sets, ONLY generate plots for the de novo matrix to save time.
955+
# The basis plots will be loaded from the pre-computed cache.
956+
# We assume gen_sub_plots can handle a None value for the second matrix
957+
# and we use a placeholder for the second return value.
958+
denovo_plots_dict, _ = gen_sub_plots(
959+
denovo_mtx,
960+
None, # Pass None to avoid generating plots for the basis_mtx
961+
output_path,
962+
project,
963+
mtype,
964+
ss_decomp=False,
965+
volume=volume,
966+
)
967+
968+
# Now, load the real basis plots from the cache.
969+
basis_plots_dict = install_cosmic_plots(
970+
context_type=mtype,
971+
genome_build=genome_build,
972+
cosmic_version=cosmic_version,
973+
exome=exome,
974+
volume=volume,
975+
)
976+
# And filter for only the specific signatures present in this decomposition.
977+
basis_plots_dict = {key: basis_plots_dict[key] for key in basis_names}
978+
979+
# --- Continue with Plot Assembly ---
980+
981+
# Generate the reconstructed profile plot
949982
reconstructed_mtx, reconstruction_plot_dict = gen_reconstructed_png_percent(
950983
denovo_name,
951984
basis_mtx,
@@ -956,26 +989,20 @@ def run_PlotDecomposition(
956989
mtype,
957990
volume=volume,
958991
)
959-
# Create a subset matrix with the present signatures
992+
993+
# Create a subset matrix with the present signatures for statistics
960994
present_sigs = np.array(basis_mtx[basis_names])
961995
reconstructed_mtx = np.dot(present_sigs, nonzero_exposures)
962-
# Convert dictionary of bytes to dictionary of images
996+
997+
# Convert all plot dictionaries from bytes to image objects for assembly
963998
denovo_plots_dict = convert_to_imgReaderDict(denovo_plots_dict)
964-
# Load in the COSMIC plots
965-
basis_plots_dict = install_cosmic_plots(
966-
context_type=mtype,
967-
genome_build=genome_build,
968-
cosmic_version=cosmic_version,
969-
exome=exome,
970-
volume=volume,
971-
)
972-
basis_plots_dict = {key: basis_plots_dict[key] for key in basis_names}
973999
basis_plots_dict = convert_to_imgReaderDict(basis_plots_dict)
974-
# Generate the reconstruction plot
9751000
reconstruction_plot_dict = convert_to_imgReaderDict(reconstruction_plot_dict)
1001+
9761002
# Get the reconstruction statistics
9771003
statistics = calculate_similarities(denovo_mtx, denovo_name, reconstructed_mtx)
978-
# Return the decomposition plot as a byte array
1004+
1005+
# Generate the final decomposition plot as a byte array
9791006
byte_plot = gen_decomposition(
9801007
denovo_name,
9811008
basis_names,
@@ -991,6 +1018,7 @@ def run_PlotDecomposition(
9911018
cosmic_version=cosmic_version,
9921019
custom_text=custom_text,
9931020
)
1021+
9941022
# Clear the plotting memory
9951023
sigPlt.clear_plotting_memory()
9961024

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Type SpecialSignatureOne SpecialSignatureA SpecialSignatureAB SpecialSignatureABC SpecialSignatureABCD SpecialSignatureABCDE SpecialSignatureABCDEF SpecialSignatureABCDEFG SpecialSignatureABCDEFGH
2+
A[C>A]A 0.000886157 5.80E-07 0.020808323 0.042196498 0.0119976 0.000425233 6.70E-05 0.002329386 0.004830406
3+
A[C>A]C 0.002280405 0.000148004 0.016506603 0.033297236 0.009438112 0.000524287 0.000179116 0.000460879 0.001150097
4+
A[C>A]G 0.000177031 5.23E-05 0.0017507 0.015598705 0.00184963 5.20E-05 7.12E-05 0.000185951 0.000377032
5+
A[C>A]T 0.001280227 9.78E-05 0.012204882 0.029497552 0.006608678 0.000180099 0.000248161 0.000709813 0.001960165
6+
A[C>G]A 0.00186033 2.23E-16 0.019707883 0.006889428 0.01009798 0.000471258 6.49E-05 8.56E-06 0.001120094
7+
A[C>G]C 0.001220217 0.000133004 0.011704682 0.002839764 0.00569886 0.000289158 2.16E-05 0.00018895 0.000319027
8+
A[C>G]G 0.00011502 1.52E-05 0.000253101 0.001279894 0.001719656 2.22E-16 5.08E-05 0.000504867 2.22E-16
9+
A[C>G]T 0.001140202 9.12E-05 0.017406963 0.003549705 0.01009798 0.000254139 9.57E-05 0.000284925 0.001930162
10+
A[C>T]A 0.025004441 6.11E-05 0.014205682 0.008699278 0.032593481 0.06413516 0.000122079 0.001409629 0.001330112
11+
A[C>T]C 0.006321123 0.00138004 0.012404962 0.004179653 0.017896421 0.022912561 0.017711498 0.038089959 0.007680645
12+
A[C>T]G 0.36506483 3.27E-05 0.002571028 0.000782935 0.006178764 0.120065821 2.23E-16 0.000542857 0.000927078
13+
A[C>T]T 0.009581702 0.001860054 0.012104842 0.004249647 0.021995601 0.021311683 0.007424819 0.010797154 0.007480629
14+
A[T>A]A 0.000800142 9.59E-05 0.005492197 0.009419218 0.007378524 9.83E-05 0.00194126 0.002489344 0.012401042
15+
A[T>A]C 0.002230396 0.000878025 0.007212885 0.003529707 0.006928614 0.000546299 0.000670435 0.000466877 0.003420287
16+
A[T>A]G 0.001140202 2.55E-05 0.009643858 0.012398971 0.009118176 2.22E-16 0.000587381 0.001559589 0.000211018
17+
A[T>A]T 0.000183032 2.23E-16 0.006112445 0.004209651 0.00619876 0.00166091 0.00371241 0.008287815 0.050704262
18+
A[T>C]A 0.001090193 6.17E-05 0.016506603 0.00794934 0.046190762 0.002211213 0.000490318 0.001019731 0.000288024
19+
A[T>C]C 0.003040539 2.15E-05 0.007763105 0.000714941 0.013397321 0.00113062 0.000227147 0.00045588 0.000533045
20+
A[T>C]G 0.000106019 1.32E-05 0.012304922 0.004999585 0.038192362 0.004242326 0.000187121 0.001499605 0.00107009
21+
A[T>C]T 0.00574102 0.000155004 0.017306923 0.001559871 0.038292342 0.000502275 0.000615399 0.002149434 0.002650223
22+
A[T>G]A 0.000172031 0.000238007 0.003951581 0.001049913 0.003859228 2.57E-05 0.000473307 8.44E-05 0.011901
23+
A[T>G]C 0.000207037 7.46E-05 0.00260104 0.000158987 0.00259948 0.000204112 0.00024716 0.000931754 0.001050088
24+
A[T>G]G 0.000268048 2.05E-06 0.006302521 0.001419882 0.007938412 0.000107059 0.000276179 0.001469613 0.000604051
25+
A[T>G]T 0.00011202 3.76E-06 0.003971589 0.000215982 0.004829034 0.000386212 0.000309201 0.000732807 2.22E-16
26+
C[C>A]A 0.000312055 0.000208006 0.022509004 0.080693302 0.007428514 0.001820998 0.000455295 0.0011397 0.000109009
27+
C[C>A]C 0.001790318 9.53E-05 0.025310124 0.079693385 0.006138772 0.004092243 0.000240156 0.001549592 0.00309026
28+
C[C>A]G 9.32E-05 2.23E-16 0.002511004 0.024497967 0.003459308 0.001650905 2.23E-16 0.000407893 0.001910161
29+
C[C>A]T 2.23E-16 0.000421012 0.015406162 0.068994273 0.006488702 0.009605265 0.000372242 0.001489607 0.013501135
30+
C[C>G]A 2.41E-05 2.23E-16 0.019307723 0.00782935 0.006958608 4.11E-05 2.23E-16 0.000457879 0.000206017
31+
C[C>G]C 7.68E-05 0.00035101 0.014105642 0.00686943 0.009068186 3.72E-05 0.000131085 0.000959747 0.00226019
32+
C[C>G]G 0.000352062 4.87E-05 0.0015006 0.003399718 0.002489502 8.43E-05 7.76E-05 0.000790792 0.000262022
33+
C[C>G]T 2.23E-16 6.36E-05 0.022308924 0.006819434 0.009528094 1.44E-05 9.57E-05 0.000889766 0.004240356
34+
C[C>T]A 0.002000355 0.004320125 0.016106443 0.012698946 0.019396121 0.01360746 0.050032478 0.106971801 0.002710228
35+
C[C>T]C 0.000270048 0.000170005 0.020208083 0.011499046 0.018996201 0.014808118 0.05533592 0.181952034 0.005970502
36+
C[C>T]G 0.196034813 0.00277008 0.002240896 0.003309725 0.017496501 0.122066918 0.006714358 0.022094175 0.008940751
37+
C[C>T]T 0.000196035 0.001450042 0.023009204 0.015998672 0.023095381 0.01380757 0.056636764 0.125966794 0.020601731
38+
C[T>A]A 4.30E-05 0.000243007 0.007943177 0.014398805 0.003539292 6.66E-05 0.000177115 0.001119705 0.003470292
39+
C[T>A]C 0.00039307 0.000244007 0.017807123 0.012198987 0.005188962 0.000551302 0.000261169 0.000957748 0.013401126
40+
C[T>A]G 0.000324057 0.000149004 0.013205282 0.032497303 0.005008998 0.000115063 0.000112073 0.000882767 0.000961081
41+
C[T>A]T 0.000260046 2.23E-16 0.012705082 0.011799021 0.00429914 0.000179098 0.000371241 0.00011197 0.048704094
42+
C[T>C]A 2.23E-16 0.000302009 0.008833533 0.00517957 0.013297341 0.00166091 0.000451293 0.001529597 0.007570636
43+
C[T>C]C 0.002500444 2.85E-05 0.014505802 0.002359804 0.01039792 0.001030565 0.000259168 0.000839779 0.003580301
44+
C[T>C]G 0.000360064 2.23E-16 0.010104042 0.005499544 0.020795841 0.005322918 0.000104068 0.000729808 0.002170182
45+
C[T>C]T 4.26E-05 0.000186005 0.015806323 0.002559788 0.014897021 0.001270696 0.001821182 0.00572849 0.043103622
46+
C[T>G]A 3.55E-05 0.000153004 0.004371749 0.000869928 0.002589482 4.95E-05 8.43E-05 0.000358905 0.003280276
47+
C[T>G]C 0.000212038 0.000155004 0.007042817 0.000440963 0.004609078 0.00078443 0.000172112 0.000791791 0.005580469
48+
C[T>G]G 0.000128023 0.000121003 0.010704282 0.003789685 0.006128774 0.000924507 2.23E-16 0.00128966 0.0011901
49+
C[T>G]T 0.00017103 0.000203006 0.007032813 0.0011999 0.00729854 0.001300713 2.65E-05 3.68E-06 0.003400286
50+
G[C>A]A 0.00158028 8.62E-05 0.007132853 0.031597377 0.01019796 0.000427234 4.06E-05 0.000745803 0.001050088
51+
G[C>A]C 0.00033906 2.23E-16 0.010904362 0.034797112 0.00764847 0.000913501 0.00013809 0.000214943 0.000742062
52+
G[C>A]G 0.000587104 1.39E-05 0.001430572 0.01529873 0.002339532 0.000357196 4.25E-05 7.29E-05 2.22E-16
53+
G[C>A]T 2.23E-16 5.12E-05 0.010004002 0.021198241 0.006818636 0.001230674 2.23E-16 0.000146961 0.000363031
54+
G[C>G]A 9.59E-06 6.54E-05 0.012705082 0.005289561 0.004679064 1.94E-05 1.96E-05 5.11E-05 0.000182015
55+
G[C>G]C 0.000164029 0.000261008 0.008633453 0.005749523 0.005078984 0.000685376 3.32E-05 0.000451881 0.00047304
56+
G[C>G]G 0.000166029 2.23E-16 0.002420968 0.003299726 0.001509698 2.22E-16 4.51E-05 0.000336911 0.000180015
57+
G[C>G]T 2.23E-16 0.000134004 0.014705882 0.003039748 0.006718656 0.000321176 4.20E-05 0.000276927 0.001280108
58+
G[C>T]A 0.004440789 2.23E-16 0.016406563 0.004089661 0.019396121 0.09365134 8.50E-05 0.000854775 0.000157013
59+
G[C>T]C 9.28E-05 1.29E-07 0.013405362 0.00445963 0.021595681 0.111060885 0.011707599 0.026293069 9.51E-06
60+
G[C>T]G 0.218038721 2.23E-16 0.000528211 0.00048196 0.01229754 0.178097635 5.52E-06 0.000175954 3.43E-05
61+
G[C>T]T 3.84E-05 5.04E-05 0.011304522 0.003809684 0.018596281 0.074440809 0.016110458 0.00538858 0.000758064
62+
G[T>A]A 8.12E-05 2.23E-16 0.007062825 0.009219235 0.003669266 5.10E-05 0.00040126 0.000525861 0.001730145
63+
G[T>A]C 0.000129023 0.000131004 0.007482993 0.003599701 0.002909418 0.000489268 0.000224145 0.000255933 0.002270191
64+
G[T>A]G 0.000246044 6.38E-05 0.011604642 0.013098913 0.004109178 0.000195107 0.000290188 0.000655827 2.22E-16
65+
G[T>A]T 0.000258046 6.28E-05 0.012304922 0.005519542 0.003219356 0.000248136 0.000306199 5.59E-05 0.020301706
66+
G[T>C]A 0.001050186 2.23E-16 0.009983994 0.002899759 0.014097181 0.00257141 0.000101066 0.00026593 2.22E-16
67+
G[T>C]C 0.001900337 2.74E-05 0.005642257 0.001179902 0.007938412 0.001110609 7.41E-05 2.22E-16 0.000423036
68+
G[T>C]G 0.001170208 0.000107003 0.010904362 0.002369803 0.014997001 0.002451344 7.25E-05 0.000528861 0.000232019
69+
G[T>C]T 7.13E-05 1.25E-05 0.010204082 0.000754937 0.013697261 0.000906497 0.000811527 0.006848194 0.007070594
70+
G[T>G]A 2.23E-16 0.000209006 0.004421769 0.00059795 0.002519496 4.35E-05 9.07E-05 0.000322915 0.002480208
71+
G[T>G]C 2.23E-16 0.000133004 0.002340936 0.000252979 0.001709658 0.000213117 0.000121079 0.000320915 0.00166014
72+
G[T>G]G 0.000348062 3.71E-05 0.010904362 0.002429798 0.00529894 0.00027415 0.000325211 0.002409365 0.006030507
73+
G[T>G]T 1.46E-05 2.23E-16 0.005832333 0.000251979 0.00234953 0.000787431 0.000835542 0.001829518 0.006750567
74+
T[C>A]A 6.58E-05 0.000639018 0.008413365 0.039796697 0.007858428 0.000356195 0.000390253 0.002899236 0.002520212
75+
T[C>A]C 0.002530449 0.000170005 0.016806723 0.046896108 0.009168166 0.000498273 0.000636413 0.003519073 0.014201193
76+
T[C>A]G 2.23E-16 8.53E-05 0.001410564 0.010899095 0.00229954 0.00020011 3.44E-05 0.000299921 2.22E-16
77+
T[C>A]T 5.88E-06 0.000440013 0.00997399 0.041296572 0.012697461 0.00074741 0.000613398 0.001409629 0.003730313
78+
T[C>G]A 2.23E-16 2.23E-16 0.013305322 0.003579703 0.007808438 4.97E-05 2.23E-16 0.000253933 0.001940163
79+
T[C>G]C 0.000203036 9.87E-05 0.020308123 0.005949506 0.00759848 0.000113062 5.09E-05 0.001619573 0.004610387
80+
T[C>G]G 6.94E-05 4.12E-05 0.000520208 0.001829848 0.001709658 1.95E-06 6.35E-06 0.000363904 8.75E-06
81+
T[C>G]T 0.001560277 0.001180034 0.023509404 0.003379719 0.0124975 0.00012807 0.000281182 0.000713812 0.012301034
82+
T[C>T]A 0.001110197 0.53601551 0.005762305 0.00373969 0.020295941 0.012006582 0.238154593 0.006608258 0.017301454
83+
T[C>T]C 3.73E-05 0.097302815 0.013305322 0.00698942 0.022995401 0.01050576 0.331215001 0.210944392 0.013701151
84+
T[C>T]G 0.110019538 0.044201279 0.000989396 0.000280977 0.015896821 0.052028522 0.074148132 0.057384873 4.64E-05
85+
T[C>T]T 2.23E-16 0.300008682 0.008173269 0.007219401 0.020995801 0.006993834 0.107069502 0.101973118 0.002320195
86+
T[T>A]A 0.006721194 0.000422012 0.006512605 0.008879263 0.006308738 0.000454249 0.000356231 0.003239146 0.015401294
87+
T[T>A]C 2.23E-16 0.000192006 0.010504202 0.004309642 0.004139172 0.000114063 0.000389253 0.00223941 0.034202874
88+
T[T>A]G 2.80E-05 5.01E-05 0.006712685 0.009539208 0.004789042 2.95E-05 2.23E-16 0.001699552 0.012201025
89+
T[T>A]T 0.002250399 0.000205006 0.013005202 0.005619534 0.00874825 0.000426234 0.000798518 2.22E-16 0.279023452
90+
T[T>C]A 0.000255045 2.23E-16 0.015206082 0.0024098 0.017996401 0.001480811 0.002151396 0.005998419 0.030202538
91+
T[T>C]C 0.003390603 4.35E-05 0.008993597 0.000502958 0.009708058 0.001090598 0.000596387 0.001729544 0.020901757
92+
T[T>C]G 0.000416074 0.000117003 0.006932773 0.001739856 0.012697461 0.002131169 0.000319207 0.003808996 0.012901084
93+
T[T>C]T 0.004330769 3.58E-05 0.013905562 0.000635947 0.018496301 0.001740954 0.000812527 0.002969217 0.088107406
94+
T[T>G]A 2.23E-16 1.67E-05 0.007252901 0.000376969 0.005218956 0.000105058 0.000128083 0.000954748 0.019301622
95+
T[T>G]C 5.51E-05 7.04E-05 0.006282513 0.000173986 0.006558688 0.000287157 0.000116075 0.001549592 0.017401462
96+
T[T>G]G 0.000583103 9.54E-05 0.008053221 0.002319807 0.006938612 0.000324178 2.23E-16 0.001349644 0.007640642
97+
T[T>G]T 2.23E-16 2.23E-16 0.010504202 0.000567953 0.013497301 0.001010554 8.30E-05 0.001769534 0.021701824

SigProfilerAssignment/decompose_subroutines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ def signature_decomposition(
574574
if (
575575
mtype_par != "none"
576576
and make_decomposition_plots == True
577-
and signature_database is None
578577
):
579578
# reformat the first column of cosmic signature dataframe
580579
cosmic_sigs_DF = sigDatabases_DF.copy(deep=True)
@@ -605,6 +604,7 @@ def signature_decomposition(
605604
genome_build=genome_build,
606605
exome=exome,
607606
volume=volume,
607+
use_custom_basis=(signature_database is not None)
608608
)
609609

610610
byte_plot.seek(0)

setup.py

Lines changed: 2 additions & 2 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.2.4"
9+
VERSION = "0.2.5"
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.2.4: Add rn7 and mm39 reference signatures'
18+
Update = 'v0.2.5: Support for generating decomposition plots for custom signature sets.'
1919
2020
"""
2121
fh = open(filename, "w")

0 commit comments

Comments
 (0)