-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_generator.py
More file actions
213 lines (161 loc) · 8.49 KB
/
plot_generator.py
File metadata and controls
213 lines (161 loc) · 8.49 KB
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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
import os
class PlotGenerator:
def __init__(self, df, plots_dir):
self.df = df
self.plots_dir = plots_dir
def generate_all_plots(self):
plot_files = []
plot_files.append(self.generate_binding_distribution())
plot_files.append(self.generate_density_plot())
plot_files.append(self.generate_box_plot())
plot_files.append(self.generate_rmsd_distribution())
plot_files.append(self.generate_3d_scatter())
plot_files.append(self.generate_affinity_vs_rmsd_ub())
plot_files.append(self.generate_affinity_vs_rmsd_lb())
plot_files.append(self.generate_pairwise_plot())
return [p for p in plot_files if p is not None]
def generate_binding_distribution(self):
if 'binding_affinity' not in self.df.columns:
return None
fig = plt.figure(figsize=(10, 6))
plt.hist(self.df['binding_affinity'], bins=30, edgecolor='black', alpha=0.7, color='skyblue')
plt.xlabel('Binding Affinity (kcal/mol)')
plt.ylabel('Frequency')
plt.title('Distribution of Binding Affinity')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '1_binding_affinity_distribution.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('1_binding_affinity_distribution.png', 'Distribution of Binding Affinity')
def generate_density_plot(self):
if 'binding_affinity' not in self.df.columns:
return None
fig = plt.figure(figsize=(10, 6))
sns.kdeplot(data=self.df['binding_affinity'], fill=True, color='green', alpha=0.5)
plt.xlabel('Binding Affinity (kcal/mol)')
plt.ylabel('Density')
plt.title('Density Plot of Binding Affinity')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '2_density_plot_binding_affinity.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('2_density_plot_binding_affinity.png', 'Density Plot of Binding Affinity')
def generate_box_plot(self):
if 'binding_affinity' not in self.df.columns:
return None
fig = plt.figure(figsize=(8, 6))
plt.boxplot(self.df['binding_affinity'].dropna(), patch_artist=True,
boxprops=dict(facecolor='lightblue'))
plt.ylabel('Binding Affinity (kcal/mol)')
plt.title('Box Plot of Binding Affinity')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '3_box_plot_binding_affinity.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('3_box_plot_binding_affinity.png', 'Box Plot of Binding Affinity')
def generate_rmsd_distribution(self):
if not all(col in self.df.columns for col in ['rmsd_ub', 'rmsd_lb']):
return None
fig = plt.figure(figsize=(10, 6))
plt.hist(self.df['rmsd_ub'].dropna(), bins=30, alpha=0.7, label='RMSD ub', color='blue')
plt.hist(self.df['rmsd_lb'].dropna(), bins=30, alpha=0.7, label='RMSD lb', color='red')
plt.xlabel('RMSD (Å)')
plt.ylabel('Frequency')
plt.title('Distribution of RMSD')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '4_distribution_rmsd.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('4_distribution_rmsd.png', 'Distribution of RMSD')
def generate_3d_scatter(self):
if not all(col in self.df.columns for col in ['binding_affinity', 'rmsd_ub', 'rmsd_lb']):
return None
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(self.df['rmsd_ub'], self.df['rmsd_lb'],
self.df['binding_affinity'],
c=self.df['binding_affinity'], cmap='viridis',
s=50, alpha=0.7)
ax.set_xlabel('RMSD ub (Å)')
ax.set_ylabel('RMSD lb (Å)')
ax.set_zlabel('Binding Affinity (kcal/mol)')
ax.set_title('3D Scatter: Binding Affinity vs RMSD')
plt.colorbar(sc, ax=ax, label='Binding Affinity', shrink=0.5)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '5_3d_scatter_affinity_vs_rmsd.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('5_3d_scatter_affinity_vs_rmsd.png', '3D Scatter Plot')
def generate_affinity_vs_rmsd_ub(self):
if 'rmsd_ub' not in self.df.columns or 'binding_affinity' not in self.df.columns:
return None
fig = plt.figure(figsize=(10, 6))
plt.scatter(self.df['rmsd_ub'], self.df['binding_affinity'],
alpha=0.6, s=50, c=self.df['binding_affinity'], cmap='coolwarm')
plt.xlabel('RMSD ub (Å)')
plt.ylabel('Binding Affinity (kcal/mol)')
plt.title('Binding Affinity vs RMSD ub')
plt.colorbar(label='Binding Affinity')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '6_affinity_vs_rmsd_ub.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('6_affinity_vs_rmsd_ub.png', 'Binding Affinity vs RMSD ub')
def generate_affinity_vs_rmsd_lb(self):
if 'rmsd_lb' not in self.df.columns or 'binding_affinity' not in self.df.columns:
return None
fig = plt.figure(figsize=(10, 6))
plt.scatter(self.df['rmsd_lb'], self.df['binding_affinity'],
alpha=0.6, s=50, c=self.df['binding_affinity'], cmap='plasma')
plt.xlabel('RMSD lb (Å)')
plt.ylabel('Binding Affinity (kcal/mol)')
plt.title('Binding Affinity vs RMSD lb')
plt.colorbar(label='Binding Affinity')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '7_affinity_vs_rmsd_lb.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('7_affinity_vs_rmsd_lb.png', 'Binding Affinity vs RMSD lb')
def generate_pairwise_plot(self):
if not all(col in self.df.columns for col in ['binding_affinity', 'rmsd_ub', 'rmsd_lb']):
return None
try:
plot_data = self.df[['binding_affinity', 'rmsd_ub', 'rmsd_lb']].dropna()
fig, axes = plt.subplots(3, 3, figsize=(12, 12))
fig.suptitle('Pairwise Relationships', fontsize=16, y=1.02)
variables = ['binding_affinity', 'rmsd_ub', 'rmsd_lb']
names = ['Binding Affinity', 'RMSD ub', 'RMSD lb']
for i in range(3):
for j in range(3):
ax = axes[i, j]
if i == j:
ax.hist(plot_data[variables[i]], bins=20, alpha=0.7, edgecolor='black')
ax.set_xlabel(names[i])
ax.set_ylabel('Frequency')
if i == 0:
ax.set_title('Distribution')
else:
ax.scatter(plot_data[variables[j]], plot_data[variables[i]],
alpha=0.6, s=20)
ax.set_xlabel(names[j])
ax.set_ylabel(names[i])
ax.grid(True, alpha=0.3)
plt.tight_layout()
plot_path = os.path.join(self.plots_dir, '8_pairwise_relationships.png')
plt.savefig(plot_path, dpi=300, bbox_inches='tight')
plt.close(fig)
return ('8_pairwise_relationships.png', 'Pairwise Relationships')
except:
return None