-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
228 lines (191 loc) · 11.9 KB
/
plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
import matplotlib.patches as patches
class Plotter:
def __init__(self):
pass
def gettime(time_assign, timepoints):
realt = time_assign[timepoints]
return (realt)
def plt_heatmap(self, tgap, barcode_number, df_dict, sample_list, assay_list, tp):
# Create a dictonary for timepoints
time_assign = {}
for cycle in range(1,len(tp)+1):
tpoint = "t" + str(cycle)
time_assign[tpoint] = tgap + 3 + (cycle-1) * 5
fig_timepoints = {}
half_samples = int(len(sample_list)/2)
if len(sample_list) == 192:
# Split the sample list into two halves
first_half_samples = sample_list[:half_samples]
second_half_samples = sample_list[half_samples:]
for i in tqdm(tp):
df_dict[i] = df_dict[i].transpose()
# Split heatmap into two subplots (2-row, 1-column layout)
fig, axes = plt.subplots(2, 1, figsize=(len(first_half_samples) * 0.5, len(assay_list) * 0.5 * 2))
# Add space between the two subplots (vertical spacing)
plt.subplots_adjust(hspace=1)
# First heatmap (first 96 samples)
frame1 = df_dict[i][first_half_samples].reindex(assay_list)
annot1 = frame1.map(lambda x: 'X' if (pd.isna(x) or x == 'NaN' or x is None) else '')
ax1 = sns.heatmap(frame1, cmap='Reds', square=True, cbar_kws={'pad': 0.002}, annot = None, fmt='', annot_kws={"size": 1000, "color": "black"}, ax=axes[0],
linewidths = 1, linecolor = "black")
# Track x-axis labels that need a dagger
dagger_labels = set()
# Add cross-hatches for "X"-marked cells
if not annot1.empty:
for y in range(annot1.shape[0]):
for x in range(annot1.shape[1]):
if annot1.iloc[y, x] == 'X':
# Calculate cell coordinates
x_start, y_start = x, y
x_end, y_end = x + 1, y + 1
# Add cross-hatches
ax1.add_line(plt.Line2D([x_start, x_end], [y_start, y_end], color='black', linewidth=1.5)) # Top-left to bottom-right
ax1.add_line(plt.Line2D([x_start, x_end], [y_end, y_start], color='black', linewidth=1.5)) # Bottom-left to top-right
# Collect x-axis labels that correspond to the "X"
dagger_labels.add(frame1.columns[x])
# Modify x-axis labels to include daggers
x_labels = ax1.get_xticklabels()
new_labels = [
f"† {label.get_text()}" if label.get_text() in dagger_labels else label.get_text()
for label in x_labels
]
ax1.set_xticklabels(new_labels, rotation=90, ha='right')
# Place the legend below the first heatmap
left1, right1 = ax1.get_xlim()
top1, bottom1 = ax1.get_ylim()
ax1.text(left1, top1 + 7,
'†: The NTC sample for this assay was removed from the analysis due to potential contamination.',
ha='left', fontsize=12, style='italic')
# Adjust layout
ax1.set_title(f'Heatmap for {barcode_number} at {time_assign[i]} minutes (Plate #1: {half_samples} Samples)', size=28)
ax1.set_xlabel('Samples', size=18)
ax1.set_ylabel('Assays', size=18)
top1, bottom1 = ax1.get_ylim()
ax1.set_ylim(top1 + 0.25, bottom1 - 0.25)
left1, right1 = ax1.get_xlim()
ax1.set_xlim(left1 - 0.25, right1 + 0.25)
ax1.tick_params(axis="y", labelsize=16, width = 2, length = 5)
ax1.tick_params(axis="x", labelsize=16, width = 2, length = 5)
plt.yticks(rotation=0)
plt.tight_layout()
ax1.axhline(y=top1 + 0.16, color='k',linewidth=6)
ax1.axhline(y=bottom1 - 0.14, color='k',linewidth=6)
ax1.axvline(x=left1 - 0.14, color='k',linewidth=6)
ax1.axvline(x=right1 + 0.15, color='k',linewidth=6)
# Second heatmap (next 96 samples)
frame2 = df_dict[i][second_half_samples].reindex(assay_list)
annot2 = frame2.map(lambda x: 'X' if (pd.isna(x) or x == 'NaN' or x is None) else '')
ax2 = sns.heatmap(frame2, cmap='Reds', square=True, cbar_kws={'pad': 0.002}, annot = None, annot_kws={"size": 20}, ax=axes[1],
linewidths = 1, linecolor = "black")
# Track x-axis labels that need a dagger
dagger_labels = set()
# Add cross-hatches for "X"-marked cells
if not annot2.empty:
for y in range(annot2.shape[0]):
for x in range(annot2.shape[1]):
if annot2.iloc[y, x] == 'X':
# Calculate cell coordinates
x_start, y_start = x, y
x_end, y_end = x + 1, y + 1
# Add cross-hatches
ax2.add_line(plt.Line2D([x_start, x_end], [y_start, y_end], color='black', linewidth=1.5)) # Top-left to bottom-right
ax2.add_line(plt.Line2D([x_start, x_end], [y_end, y_start], color='black', linewidth=1.5)) # Bottom-left to top-right
# Collect x-axis labels that correspond to the "X"
dagger_labels.add(frame2.columns[x])
# Modify x-axis labels to include daggers
x_labels = ax2.get_xticklabels()
new_labels = [
f"† {label.get_text()}" if label.get_text() in dagger_labels else label.get_text()
for label in x_labels
]
ax2.set_xticklabels(new_labels, rotation=90, ha='right')
# Place the legend below the first heatmap
left2, right2 = ax1.get_xlim()
top2, bottom2 = ax1.get_ylim()
ax2.text(left2, top2 + 7,
'†: The NTC sample for this assay was removed from the analysis due to potential contamination.',
ha='left', fontsize=12, style='italic')
# Adjust layout
ax2.set_title(f'Heatmap for {barcode_number} at {time_assign[i]} minutes (Plate #2: {half_samples} Samples)', size=28)
ax2.set_xlabel('Samples', size=14)
ax2.set_ylabel('Assays', size=14)
top, bottom = ax2.get_ylim()
ax2.set_ylim(top + 0.25, bottom - 0.25)
left, right = ax2.get_xlim()
ax2.set_xlim(left - 0.25, right + 0.25)
ax2.tick_params(axis="y", labelsize=16)
ax2.tick_params(axis="x", labelsize=16)
plt.yticks(rotation=0)
plt.tight_layout()
ax2.axhline(y=top + 0.16, color='k',linewidth=6)
ax2.axhline(y=bottom - 0.14, color='k',linewidth=6)
ax2.axvline(x=left - 0.14, color='k',linewidth=6)
ax2.axvline(x=right + 0.15, color='k',linewidth=6)
# Save the figure to the dictionary
fig_timepoints[i] = fig
else:
for i in tqdm(tp):
df_dict[i] = df_dict[i].transpose()
# Do not split heatmap into two subplots (2-row, 1-column layout)
fig, axes = plt.subplots(1, 1, figsize=(len(frame.columns.values)*0.5,len(frame.index.values)*0.5 * 2))
# Add space between the two subplots (vertical spacing)
plt.subplots_adjust(hspace=1)
# Plot heatmap (all samples)
frame = df_dict[i][sample_list].reindex(assay_list)
annot1 = frame.map(lambda x: 'X' if (pd.isna(x) or x == 'NaN' or x is None) else '')
ax = sns.heatmap(frame, cmap='Reds', square=True, cbar_kws={'pad': 0.002}, annot = None, fmt='', annot_kws={"size": 1000, "color": "black"}, ax=axes[0],
linewidths = 1, linecolor = "black")
# calculate the real timing of the image
rt = time_assign[i]
# Track x-axis labels that need a dagger
dagger_labels = set()
# Add cross-hatches for "X"-marked cells
if not annot1.empty:
for y in range(annot1.shape[0]):
for x in range(annot1.shape[1]):
if annot1.iloc[y, x] == 'X':
# Calculate cell coordinates
x_start, y_start = x, y
x_end, y_end = x + 1, y + 1
# Add cross-hatches
ax.add_line(plt.Line2D([x_start, x_end], [y_start, y_end], color='black', linewidth=1.5)) # Top-left to bottom-right
ax.add_line(plt.Line2D([x_start, x_end], [y_end, y_start], color='black', linewidth=1.5)) # Bottom-left to top-right
# Collect x-axis labels that correspond to the "X"
dagger_labels.add(frame.columns[x])
# Modify x-axis labels to include daggers
x_labels = ax.get_xticklabels()
new_labels = [
f"† {label.get_text()}" if label.get_text() in dagger_labels else label.get_text()
for label in x_labels
]
ax.set_xticklabels(new_labels, rotation=90, ha='right')
# Place the legend below the first heatmap
left, right = ax.get_xlim()
top, bottom = ax.get_ylim()
ax.text(left, top + 7,
'†: The NTC sample for this assay was removed from the analysis due to potential contamination.',
ha='left', fontsize=12, style='italic')
# Adjust layout
ax.set_title(f'Heatmap for {barcode_number} at '+str(rt)+' minutes', size=28)
ax.set_xlabel('Samples', size=18)
ax.set_ylabel('Assays', size=18)
top, bottom = ax.get_ylim()
ax.set_ylim(top + 0.25, bottom - 0.25)
left, right = ax.get_xlim()
ax.set_xlim(left - 0.25, right + 0.25)
ax.tick_params(axis="y", labelsize=16, width = 2, length = 5)
ax.tick_params(axis="x", labelsize=16, width = 2, length = 5)
plt.yticks(rotation=0)
plt.tight_layout()
ax.axhline(y=top + 0.16, color='k',linewidth=6)
ax.axhline(y=bottom - 0.14, color='k',linewidth=6)
ax.axvline(x=left - 0.14, color='k',linewidth=6)
ax.axvline(x=right + 0.15, color='k',linewidth=6)
# Save the figure to the dictionary
fig_timepoints[i] = fig
return fig_timepoints