Skip to content

Commit

Permalink
Merge pull request #44 from AlexandrovLab/u59b
Browse files Browse the repository at this point in the history
U59b
  • Loading branch information
mdbarnesUCSD authored Feb 9, 2024
2 parents faafb4c + 66f2594 commit 99a1418
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 543 deletions.
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.18"
VERSION = "1.3.19"


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.18: Update SV and CNV output handling to support pdf, png, pil_image.'
update = 'Upgrade v1.3.19: Fix plotSV input handling and refactor tests.'
"""
fh = open(filename, "w")
Expand Down
48 changes: 27 additions & 21 deletions sigProfilerPlotting/sigProfilerPlotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def output_results(savefig_format, output_path, project, figs, context_type, dpi
if savefig_format.lower() == "pdf":
pp = PdfPages(output_path + context_type + "_plots_" + project + ".pdf")
for fig in figs:
if context_type == "CNV_48" or "SV_32":
if context_type in ("CNV_48", "SV_32"):
figs[fig].savefig(pp, format="pdf", bbox_inches="tight")
else:
figs[fig].savefig(pp, format="pdf")
Expand All @@ -116,7 +116,9 @@ def output_results(savefig_format, output_path, project, figs, context_type, dpi
for fig in figs:
if context_type in ("CNV_48", "SV_32"):
figs[fig].savefig(
output_path + context_type + "_plots_" + fig + ".png", dpi=dpi, bbox_inches="tight"
output_path + context_type + "_plots_" + fig + ".png",
dpi=dpi,
bbox_inches="tight",
)
else:
figs[fig].savefig(
Expand All @@ -127,8 +129,10 @@ def output_results(savefig_format, output_path, project, figs, context_type, dpi
image_list = {}
for fig in figs:
tmp_buffer = io.BytesIO()
if context_type == "CNV_48" or "SV_32":
figs[fig].savefig(tmp_buffer, format="png", bbox_inches="tight", dpi=dpi)
if context_type in ("CNV_48", "SV_32"):
figs[fig].savefig(
tmp_buffer, format="png", bbox_inches="tight", dpi=dpi
)
else:
figs[fig].savefig(tmp_buffer, format="png", dpi=dpi)
# convert tmp_buffer to a PIL and close buffer
Expand Down Expand Up @@ -2063,31 +2067,25 @@ 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):
os.makedirs(output_path)

df = pd.read_csv(
matrix_path, sep=None, engine="python"
) # flexible reading of tsv or csv

# To reindex the input data
df = process_input(matrix_path, "32")
df.reset_index(inplace=True)
label = df.columns[0]
labels = df[label]


figs={}
figs = {}
if aggregate:
num_samples = len(df.columns) - 1
df["total_count"] = df.sum(axis=1) / num_samples # NORMALIZE BY # of SAMPLES
counts = list(df["total_count"])
if percentage and sum(counts) != 0:
counts = [(x / sum(counts)) * 100 for x in counts]
sample = ""
figs[sample]=plot(counts, labels, sample, project, percentage, aggregate=True)
figs[sample] = plot(counts, labels, sample, project, percentage, aggregate=True)
else:
# each column vector in dataframe contains counts for a specific sample
samples = list(df)[1:]
Expand All @@ -2097,7 +2095,7 @@ def plot(counts, labels, sample, project, percentage, aggregate=False):
counts = [(x / sum(counts)) * 100 for x in counts]
assert (len(counts)) == 32
assert (len(labels)) == 32
figs[sample]=plot(counts, labels, sample, project, percentage)
figs[sample] = plot(counts, labels, sample, project, percentage)

return output_results(savefig_format, output_path, project, figs, "SV_32", dpi=dpi)

Expand All @@ -2124,9 +2122,7 @@ def plotCNV(
"""

# inner function to construct plot
def plot(
counts, labels, sample, project, percentage, aggregate=False
):
def plot(counts, labels, sample, project, percentage, aggregate=False):
counts_ordered = list()
labels_ordered = list()
labels_updated = list()
Expand Down Expand Up @@ -2652,7 +2648,9 @@ def plot(
counts = [(x / sum(counts)) * 100 for x in counts]
assert len(counts) == 48
assert len(labels) == 48
figs[sample] = plot(counts, labels, sample, project, percentage, aggregate=False)
figs[sample] = plot(
counts, labels, sample, project, percentage, aggregate=False
)

return output_results(savefig_format, output_path, project, figs, "CNV_48", dpi=dpi)

Expand Down Expand Up @@ -2977,7 +2975,9 @@ def plotSBS(
[i.set_color("black") for i in plt.gca().get_yticklabels()]
sample_count += 1

return output_results(savefig_format, output_path, project, figs, "SBS_96", dpi=dpi)
return output_results(
savefig_format, output_path, project, figs, "SBS_96", dpi=dpi
)
except:
print("There may be an issue with the formatting of your matrix file.")
pdf_path = output_path + "SBS_96_plots_" + project + ".pdf"
Expand Down Expand Up @@ -7730,7 +7730,9 @@ def plotSBS(
panel2.legend(handles[:3], labels[:3], loc="best", prop={"size": 30})
sample_count += 1

return output_results(savefig_format, output_path, project, figs, "SBS_288", dpi=dpi)
return output_results(
savefig_format, output_path, project, figs, "SBS_288", dpi=dpi
)

except:
print("There may be an issue with the formatting of your matrix file.")
Expand Down Expand Up @@ -8653,7 +8655,9 @@ def plotID(
[i.set_color("black") for i in plt.gca().get_yticklabels()]
sample_count += 1

return output_results(savefig_format, output_path, project, figs, "ID_83", dpi=dpi)
return output_results(
savefig_format, output_path, project, figs, "ID_83", dpi=dpi
)
except:
print("There may be an issue with the formatting of your matrix file.")
pdf_path = output_path + "ID_83_plots_" + project + ".pdf"
Expand Down Expand Up @@ -10711,7 +10715,9 @@ def plotDBS(
[i.set_color("grey") for i in plt.gca().get_xticklabels()]
sample_count += 1

return output_results(savefig_format, output_path, project, figs, "DBS_78", dpi=dpi)
return output_results(
savefig_format, output_path, project, figs, "DBS_78", dpi=dpi
)

except:
print("There may be an issue with the formatting of your matrix file.")
Expand Down
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.18'
version = '1.3.18'
update = 'Upgrade v1.3.18: Update SV and CNV output handling to support pdf, png, pil_image.'
short_version = '1.3.19'
version = '1.3.19'
update = 'Upgrade v1.3.19: Fix plotSV input handling and refactor tests.'


Loading

0 comments on commit 99a1418

Please sign in to comment.