Skip to content

Commit

Permalink
Issue #222 yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfreeman88 authored and andrewprzh committed Aug 22, 2024
1 parent 8ed062d commit b035a3d
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 93 deletions.
85 changes: 51 additions & 34 deletions src/plot_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import pprint


class PlotOutput:
Expand Down Expand Up @@ -198,42 +199,58 @@ def plot_transcript_usage(self):
def make_pie_charts(self):
"""
Create pie charts for transcript alignment classifications and read assignment consistency.
Handles both combined and separate sample data structures.
"""
print("self.reads_and_class structure:")
pprint.pprint(self.reads_and_class)

titles = ["Transcript Alignment Classifications", "Read Assignment Consistency"]

for i, (title, reads_dict) in enumerate(zip(titles, self.reads_and_class)):
labels = reads_dict.keys()
sizes = reads_dict.values()
total = sum(sizes)

# Generate a file-friendly title
file_title = title.lower().replace(" ", "_")

plt.figure()
wedges, texts, autotexts = plt.pie(
sizes,
labels=labels,
autopct="%1.1f%%",
startangle=140,
textprops=dict(color="w"),
)
plt.setp(autotexts, size=10, weight="bold")
plt.setp(texts, size=9)
for title, data in zip(titles, self.reads_and_class):
if isinstance(data, dict):
if any(isinstance(v, dict) for v in data.values()):
# Separate 'Mutants' and 'WildType' case
for sample_name, sample_data in data.items():
self._create_pie_chart(f"{title} - {sample_name}", sample_data)
else:
# Combined data case
self._create_pie_chart(title, data)
else:
print(f"Skipping unexpected data type for {title}: {type(data)}")

plt.axis(
"equal"
) # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title(f"{title}\nTotal: {total}")
def _create_pie_chart(self, title, data):
"""
Helper method to create a single pie chart.
"""
labels = list(data.keys())
sizes = list(data.values())
total = sum(sizes)

plt.legend(
wedges,
labels,
title="Categories",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1),
)
plot_path = os.path.join(
self.visualization_dir, f"{file_title}_pie_chart.png"
)
plt.savefig(plot_path, bbox_inches="tight")
plt.close()
# Generate a file-friendly title
file_title = title.lower().replace(" ", "_").replace("-", "_")

plt.figure(figsize=(12, 8))
wedges, texts, autotexts = plt.pie(
sizes,
labels=labels,
autopct=lambda pct: f"{pct:.1f}%\n({int(pct/100.*total):d})",
startangle=140,
textprops=dict(color="w"),
)
plt.setp(autotexts, size=8, weight="bold")
plt.setp(texts, size=7)

plt.axis("equal") # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title(f"{title}\nTotal: {total}")

plt.legend(
wedges,
labels,
title="Categories",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1),
fontsize=8,
)
plot_path = os.path.join(self.visualization_dir, f"{file_title}_pie_chart.png")
plt.savefig(plot_path, bbox_inches="tight", dpi=300)
plt.close()
Loading

0 comments on commit b035a3d

Please # to comment.