-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
193 lines (154 loc) · 7.99 KB
/
Copy pathvisualization.py
File metadata and controls
193 lines (154 loc) · 7.99 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
# -*- coding: utf-8 -*-
"""
Part for the data vizualization
"""
from import_package import *
def fullheatmap(quanti, save_dir):
# Clustered heat map from all normalized abundance
heatmap_all = sns.clustermap(quanti, method='average', metric='euclidean',
cbar_kws={"label": "Normalized Abundance", "orientation": "horizontal"},
cbar_pos=(0.75, 0.90, 0.1, 0.05))
x_labels = [label.get_text().replace('abundance_', '').replace('D__Data_', '') for label in heatmap_all.ax_heatmap.get_xticklabels()]
heatmap_all.ax_heatmap.set_xticklabels(x_labels, rotation=90)
# Add a title to the graph
heatmap_all.fig.suptitle("Heatmap of all proteins", fontsize=20, y=1.05)
heatmap_all.savefig(f"{save_dir}/heatmap_all.svg", bbox_inches='tight')
heatmap_all.savefig(f"{save_dir}/heatmap_all.png", bbox_inches='tight')
plt.clf() # Clear the current figure to start a new one
def volcano_plot(dataframe, logfc, adjpva, color_group, threshold_pval, threshold_fc, directory):
"""
Generate a volcano plot of a 2 by 2 comparison
:param dataframe: the dataframe for the vulcanoplot with the accession for the name, description,
and the logFC adjusted p-val and significant for each comparison
:param logfc: column of the LogFC from a dataframe
:param adjpva: column of the adjusted p-value from a dataframe
:param color_group: Color groups if they are significant
:param threshold_pval: threshold of the p-value
:param threshold_fc: threshold of the LogFC
:param directory: Path of the directory to save the graph
:return: Save the graph
"""
colors = dataframe[color_group].unique()
colors = sorted(colors, reverse=True)
# Create volcano plot with Plotly Express
fig = px.scatter(dataframe, x=logfc, y=adjpva,
color=color_group, color_discrete_sequence=['orange', 'lightgrey'],
category_orders={color_group: colors},
labels={'color': 'Significant'},
hover_data={'description': True},
hover_name='accession')
# Add threshold lines
fig.add_hline(y=threshold_pval, line_width=2, line_dash="dash", line_color="black", layer="below")
fig.add_vline(x=threshold_fc, line_width=2, line_dash="dash", line_color="black", layer="below")
fig.add_vline(x=-threshold_fc, line_width=2, line_dash="dash", line_color="black", layer="below")
# Styling & legend part
fig.update_xaxes(title_text='<b>Log<sub>2</sub> Fold Change</b>', showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(title_text='<b>Log<sub>10</sub> Adjusted <i>p</i>-value</b>', showline=True, linewidth=2,
linecolor='black')
fig.update_layout(
legend_title_text='<b>Significant</b>',
title=f'<b>{logfc.replace("(", "").replace(")", "").replace("_", " ").replace(" logFC", "")}</b>',
plot_bgcolor='white'
)
# Save as HTML (enable interactive)
fig.write_html(f"{directory}/volcano_{'_'.join(logfc.split('_')[:-1])}.html")
def volcanos(looking_FC, looking_2x2compare, threshold_pvalue, threshold_FC, save_dir):
# Volcano plot for all comparisons
print("Creation oof volcano plot")
for col in looking_FC: # col correspond to the log FC column to plot
corresponding_pval = looking_2x2compare.columns.get_loc(col) + 1 # search the n° of the adjusted p-val column
corresponding_pval = looking_2x2compare.columns[corresponding_pval] # define the corect column of adjusted pval to plot in the graph
group = looking_2x2compare.columns.get_loc(col) + 2 # search the n° of the significant column
group = looking_2x2compare.columns[group] # group pas the significant column
volcano_plot(dataframe=looking_2x2compare,logfc=col, adjpva=corresponding_pval, color_group=group,
threshold_pval=threshold_pvalue, threshold_fc=threshold_FC,
directory=save_dir)
progress_bar(looking_FC.columns.get_loc(col) + 1, len(looking_FC.columns), "Volcano plot")
def scree_plot(scree, save_dir):
# 1 ) Scree plots
scree.plot.bar(x="Dimension", y="% Explaine Variance")
plt.savefig(f"{save_dir}/scree_polt_ExplainVar.svg")
plt.clf() # Clear the current figure to start a new one
print("Plot Explain variance done")
# 2 ) Cumulatve plot
scree.plot.bar(x="Dimension", y="% Cumulative")
plt.text(0.5, 90, "95%")
plt.axhline(y=95, linewidth=0.5, color="dimgray", linestyle="--")
plt.savefig(f"{save_dir}/scree_polt_CumVar.svg")
plt.clf() # Clear the current figure to start a new one
print("Plot Cumumul Explain variance done")
def plot_PCA(table_pca, scree, save_dir, pca, table_stats):
# define the condition to color the point in the PCA
unique_conditions = table_pca['Condition'].unique()
# Graph
fig, ax = plt.subplots()
scatter_handles = []
for condition in unique_conditions:
subset_df = table_pca[table_pca['Condition'] == condition]
handle = plt.scatter(subset_df['PCA1'],
subset_df['PCA2'],
label=f'{condition}',
alpha=0.7, edgecolor='k')
scatter_handles.append(handle)
for index, sample in table_pca.iterrows():
ax.annotate(" ".join(index.split("_")[1:]), (sample['PCA1'], sample['PCA2']), fontsize=4)
plt.legend(handles=scatter_handles, loc='upper left', bbox_to_anchor=(1.05, 1))
plt.title('PCA Analysis')
plt.axhline(y=0, linewidth=0.5, color="dimgray", linestyle="--")
plt.axvline(x=0, linewidth=0.5, color="dimgray", linestyle="--")
plt.xlabel(f'1st dim ({scree.at[0, "% Explaine Variance"]}%)')
plt.ylabel(f'2nd dim ({scree.at[1, "% Explaine Variance"]}%)')
plt.tight_layout()
plt.savefig(f"{save_dir}/PCA.svg", dpi=300)
plt.clf() # Clear the current figure to start a new one
print("Plot PCA done")
# Multiple PCA
total_5 = np.sum(pca.explained_variance_ratio_[:5]*100).round(1)
label = {
"PCA"+str(i+1): f"PC {i+1} ({var:.1f}%)"
for i, var in enumerate(pca.explained_variance_ratio_ * 100)
}
fig = px.scatter_matrix(
table_pca,
dimensions=['PCA1', 'PCA2', 'PCA3', 'PCA4', 'PCA5'],
color=table_pca["Condition"],
title=f'Total Explained Variance: {total_5}%',
labels=label,
hover_name=table_pca.index.str.replace("_", " ").str.replace("abundance", ""))
fig.write_html(f"{save_dir}/PCA_multiple.html")
print("Plot Multi PCA done")
# 3D
total_3 = np.sum(pca.explained_variance_ratio_[:3]*100).round(1)
fig = px.scatter_3d(
table_pca, x='PCA1', y='PCA2', z='PCA3',
color=table_pca['Condition'],
title=f'Total Explained Variance: {total_3}%',
labels=label,
hover_name=table_pca.index.str.replace("_", " ").str.replace("abundance", ""))
fig.write_html(f"{save_dir}/PCA_3D.html")
print("Plot 3D PCA done")
# Loading
table_stats.drop('Condition', axis=1, inplace=True)
table_stats = table_stats.T
loadings_matrix = pca.components_.T * np.sqrt(pca.explained_variance_)
table_stats = table_stats.T
loadings_df = pd.DataFrame(loadings_matrix, columns=[f'PC{j+1}' for j in range(pca.n_components_)],
index=table_stats.columns)
loadings_df.reset_index(inplace=True)
print("Creation of Loading plot")
for i in range(5):
# plotly graph object
trace = go.Bar(
x=loadings_df['index'],
y=loadings_df[f'PC{i+1}'],
)
# disposition
layout = go.Layout(
title=f'Loading PC {i+1}',
xaxis=dict(title='Proteins'),
yaxis=dict(title='Loading'),
plot_bgcolor='rgba(255, 255, 255, 0)')
# plot the figure
fig = go.Figure(data=[trace], layout=layout)
fig.write_html(f"{save_dir}/PCA_loading{i+1}.html")
progress_bar(i+1, 5, "Creation of Loading plot")