Skip to content

Commit

Permalink
Merge pull request #50 from AlexandrovLab/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
mdbarnesUCSD authored Aug 5, 2024
2 parents dbe0f27 + 1e541a4 commit db6d54c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 44 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.3.24] - 2024-08-01

### Added
- Added environmental variable `SIGPROFILERPLOTTING_VOLUME` to enhance configurability.

### Fixed
- Ensured correct file path construction with `os.path.join()`.
- Updated CLI to handle boolean arguments using `str2bool`, resolving an issue where boolean strings like `False` were incorrectly evaluated as `True`.
- Stopped unecessary output directory creation when the output format is PIL_Image.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://osf.io/2aj6t/wiki/home/) [![License](https://img.shields.io/badge/License-BSD\%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause) [![Build Status](https://travis-ci.com/AlexandrovLab/SigProfilerPlotting.svg?branch=master)](https://app.travis-ci.com/AlexandrovLab/SigProfilerPlotting)
[![Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://osf.io/2aj6t/wiki/home/) [![License](https://img.shields.io/badge/License-BSD\%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause) [![Build Status](https://app.travis-ci.com/AlexandrovLab/SigProfilerPlotting.svg?branch=master)](https://app.travis-ci.com/AlexandrovLab/SigProfilerPlotting)

# SigProfilerPlotting
SigProfilerPlotting provides a standard tool for displaying all types of mutational signatures as well as all types of mutational patterns in cancer genomes. The tool seamlessly integrates with other SigProfiler tools.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def readme():
return f.read()


VERSION = "1.3.23"
VERSION = "1.3.24"


def write_version_py(filename="sigProfilerPlotting/version.py"):
Expand All @@ -23,7 +23,7 @@ def write_version_py(filename="sigProfilerPlotting/version.py"):
# THIS FILE IS GENERATED FROM SIGPROFILERPLOTTING SETUP.PY
short_version = '%(version)s'
version = '%(version)s'
update = 'Upgrade v1.3.23: Update SBS4608 custom_text_upper, custom_text_lower, custom_text_middle.'
update = 'Upgrade v1.3.24: Make output path handling more robust with os.path.join() and add environmental variable SIGPROFILERPLOTTING_VOLUME'
"""
fh = open(filename, "w")
Expand Down
52 changes: 45 additions & 7 deletions sigProfilerPlotting/controllers/cli_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
import sigProfilerPlotting as sigPlt


def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")


# Common parser setup for shared arguments
def common_plotting_arguments(parser):
parser.add_argument("matrix_path", help="The path to the input matrix file.")
Expand All @@ -12,7 +23,12 @@ def common_plotting_arguments(parser):
parser.add_argument("project", help="The name of the project.")
parser.add_argument("plot_type", help="The type of plot to generate.")
parser.add_argument(
"--percentage", action="store_true", help="Display percentages in the plot."
"--percentage",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Display percentages in the plot.",
)
parser.add_argument(
"--custom_text_upper", help="Custom text to display at the top of the plot."
Expand Down Expand Up @@ -40,7 +56,7 @@ def common_plotting_arguments(parser):

def parse_arguments_sbs(args: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="SigProfilerPlotting plotDBS", description="Generate DBS plots."
prog="SigProfilerPlotting plotSBS", description="Generate SBS plots."
)
common_plotting_arguments(parser)
return parser.parse_args(args)
Expand Down Expand Up @@ -72,10 +88,20 @@ def parse_arguments_sv(args: List[str]) -> argparse.Namespace:
)
parser.add_argument("project", help="The name of the project.")
parser.add_argument(
"--percentage", action="store_true", help="Display percentages in the plot."
"--percentage",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Display percentages in the plot.",
)
parser.add_argument(
"--aggregate", action="store_true", help="Aggregate all samples."
"--aggregate",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Aggregate all samples.",
)
parser.add_argument(
"--savefig_format",
Expand All @@ -102,14 +128,26 @@ def parse_arguments_cnv(args: List[str]) -> argparse.Namespace:
)
parser.add_argument("project", help="The name of the project.")
parser.add_argument(
"--percentage", action="store_true", help="Display percentages in the plot."
"--percentage",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Display percentages in the plot.",
)
parser.add_argument(
"--aggregate", action="store_true", help="Aggregate data for the plot."
"--aggregate",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Aggregate data for the plot.",
)
parser.add_argument(
"--read_from_file",
action="store_true",
type=str2bool,
nargs="?",
const=True,
default=True,
help="Read data from a file for the plot.",
)
Expand Down
72 changes: 41 additions & 31 deletions sigProfilerPlotting/sigProfilerPlotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ def clear_plotting_memory():
# the figures are saved to a dictionary of buffers
def output_results(savefig_format, output_path, project, figs, context_type, dpi=100):
if savefig_format.lower() == "pdf":
pp = PdfPages(output_path + context_type + "_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"{context_type}_plots_{project}.pdf")
pp = PdfPages(file_path)
for fig in figs:
if context_type in ("CNV_48", "SV_32"):
figs[fig].savefig(pp, format="pdf", bbox_inches="tight")
Expand Down Expand Up @@ -241,6 +242,12 @@ def get_default_96labels():


def make_pickle_file(context="SBS96", return_plot_template=False, volume=None):

# The environmental variable takes precedence over the volume argument
# If the environmental variable is not set, the volume argument is used
volume = os.getenv("SIGPROFILERPLOTTING_VOLUME", volume)

# Use the default volume when no environmental variable or volume argument is provided
if volume is None:
volume = SPP_TEMPLATES

Expand Down Expand Up @@ -743,7 +750,6 @@ def make_pickle_file(context="SBS96", return_plot_template=False, volume=None):
else:
pickle.dump(plot1, open(path, "wb"))
return plot1

elif context == "DBS78":
plot_custom_text = False
pcawg = False
Expand Down Expand Up @@ -1016,7 +1022,6 @@ def make_pickle_file(context="SBS96", return_plot_template=False, volume=None):
else:
pickle.dump(plot1, open(path, "wb"))
return plot1

elif context == "ID83":
plt.rcParams["axes.linewidth"] = 2
plot1 = plt.figure(figsize=(43.93, 12))
Expand Down Expand Up @@ -2090,7 +2095,7 @@ def plot(counts, labels, sample, project, percentage, aggregate=False):
return fig

# create the output directory if it doesn't exist
if not os.path.exists(output_path):
if not os.path.exists(output_path) and savefig_format.lower() != "pil_image":
os.makedirs(output_path)

# To reindex the input data
Expand Down Expand Up @@ -2625,7 +2630,7 @@ def plot(counts, labels, sample, project, percentage, aggregate=False):
return fig

# create the output directory if it doesn't exist
if not os.path.exists(output_path):
if not os.path.exists(output_path) and savefig_format.lower() != "pil_image":
os.makedirs(output_path)

df = pd.DataFrame()
Expand Down Expand Up @@ -2710,7 +2715,7 @@ def plotSBS(
load_custom_fonts()

# create the output directory if it doesn't exist
if not os.path.exists(output_path):
if not os.path.exists(output_path) and savefig_format.lower() != "pil_image":
os.makedirs(output_path)

if plot_type == "96":
Expand Down Expand Up @@ -3021,7 +3026,8 @@ def plotSBS(
sys.exit(
"The matrix does not match the correct SBS192 format. Please check you formatting and rerun this plotting function."
)
pp = PdfPages(output_path + "SBS_384_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_384_plots_{project}.pdf")
pp = PdfPages(file_path)
mutations = OrderedDict()
try:
with open(matrix_path) as f:
Expand Down Expand Up @@ -3537,7 +3543,8 @@ def plotSBS(
"The matrix does not match the correct SBS288 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_384_extended_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_384_extended_plots_{project}.pdf")
pp = PdfPages(file_path)
mutations = OrderedDict()
try:
with open(matrix_path) as f:
Expand Down Expand Up @@ -4087,8 +4094,8 @@ def plotSBS(
sys.exit(
"The matrix does not match the correct SBS6 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_6_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_6_plots_{project}.pdf")
pp = PdfPages(file_path)

mutations = OrderedDict()
total_count = []
Expand Down Expand Up @@ -4322,8 +4329,8 @@ def plotSBS(
sys.exit(
"The matrix does not match the correct SBS192 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_24_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_24_plots_{project}.pdf")
pp = PdfPages(file_path)
mutations = OrderedDict()

try:
Expand Down Expand Up @@ -4580,7 +4587,8 @@ def plotSBS(
"The matrix does not match the correct SBS1536 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_1536_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_1536_plots_{project}.pdf")
pp = PdfPages(file_path)

mutations_96 = OrderedDict()
path_list = matrix_path.split("/")
Expand Down Expand Up @@ -5911,7 +5919,8 @@ def plotSBS(
"The matrix does not match the correct SBS4608 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_4608_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_4608_plots_{project}.pdf")
pp = PdfPages(file_path)

path_list = matrix_path.split("/")
extension = path_list[-1].split(".")
Expand Down Expand Up @@ -7033,7 +7042,7 @@ def plotSBS(

fontsize_custom = 25
fontweight_custom = "bold"
fontcolor_custom = ("black")
fontcolor_custom = "black"
fontname_custom = "Arial"
ha_custom = "left"

Expand Down Expand Up @@ -7751,7 +7760,8 @@ def plotSBS(
"The matrix does not match the correct SBS288 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "SBS_288_Normalized_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"SBS_288_Normalized_plots_{project}.pdf")
pp = PdfPages(file_path)

mutations = OrderedDict()
mutations_TSB = OrderedDict()
Expand Down Expand Up @@ -8315,9 +8325,9 @@ def plotSBS(

else:
print(
"The provided plot_type:",
"Error: The function plotSBS does not support plot_type",
plot_type,
"is not supported by this plotting function",
"so no plot has been generated."
)


Expand All @@ -8335,7 +8345,7 @@ def plotID(
dpi=100,
):
# create the output directory if it doesn't exist
if not os.path.exists(output_path):
if not os.path.exists(output_path) and savefig_format.lower() != "pil_image":
os.makedirs(output_path)

# load custom fonts for plotting
Expand Down Expand Up @@ -8676,7 +8686,8 @@ def plotID(
sys.exit(
"The matrix does not match the correct SBS96 format. Please check you formatting and rerun this plotting function."
)
pp = PdfPages(output_path + "ID_simple_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"ID_simple_plots_{project}.pdf")
pp = PdfPages(file_path)

indel_types = [
"1:Del:C:1",
Expand Down Expand Up @@ -9211,10 +9222,7 @@ def plotID(
os.remove(pdf_path)

elif (
plot_type == "96"
or plot_type == "ID96"
or plot_type == "96ID"
or plot_type == "IDSB"
plot_type == "IDSB"
or plot_type == "415"
):
with open(matrix_path) as f:
Expand All @@ -9229,7 +9237,8 @@ def plotID(
"The matrix does not match the correct ID-96 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "ID_TSB_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"ID_TSB_plots_{project}.pdf")
pp = PdfPages(file_path)

indel_types_tsb = []
tsb_I = ["T", "U", "N", "B", "Q"]
Expand Down Expand Up @@ -10294,9 +10303,9 @@ def plotID(

else:
print(
"The provided plot_type:",
"Error: The function plotID does not support plot_type",
plot_type,
"is not supported by this plotting function",
"so no plot has been generated."
)


Expand All @@ -10314,7 +10323,7 @@ def plotDBS(
dpi=100,
):
# create the output directory if it doesn't exist
if not os.path.exists(output_path):
if not os.path.exists(output_path) and savefig_format.lower() != "pil_image":
os.makedirs(output_path)

# load custom fonts for plotting
Expand Down Expand Up @@ -10736,7 +10745,8 @@ def plotDBS(
"The matrix does not match the correct SBS96 format. Please check you formatting and rerun this plotting function."
)

pp = PdfPages(output_path + "DBS_186_plots_" + project + ".pdf")
file_path = os.path.join(output_path, f"DBS_186_plots_{project}.pdf")
pp = PdfPages(file_path)

dinucs = [
"TT>GG",
Expand Down Expand Up @@ -11222,7 +11232,7 @@ def plotDBS(

else:
print(
"The provided plot_type:",
"Error: The function plotDBS does not support plot_type",
plot_type,
"is not supported by this plotting function",
"so no plot has been generated."
)
6 changes: 3 additions & 3 deletions sigProfilerPlotting/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# THIS FILE IS GENERATED FROM SIGPROFILERPLOTTING SETUP.PY
short_version = '1.3.22'
version = '1.3.22'
update = 'Upgrade v1.3.22: Update input_processing index.name handling to be more robust.'
short_version = '1.3.24'
version = '1.3.24'
update = 'Upgrade v1.3.24: Make output path handling more robust with os.path.join() and add environmental variable SIGPROFILERPLOTTING_VOLUME'


0 comments on commit db6d54c

Please # to comment.