-
Notifications
You must be signed in to change notification settings - Fork 0
/
gather_attn_stats.py
268 lines (234 loc) · 8.16 KB
/
gather_attn_stats.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import json
import os
import shutil
import matplotlib
matplotlib.use("Agg")
import colorcet as cc
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import torch
import umap
from gprofiler import GProfiler
from scipy.stats import median_abs_deviation
from sklearn import metrics
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics.pairwise import cosine_distances, cosine_similarity
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm
from utils.plot_enriched import plot_enrich
UNIQUE_CATS = list(
pd.read_csv("annotations/location_group_mapping.csv")[
"Original annotation"
].unique()
)
UNIQUE_CATS.append("Negative")
cats_ordered = [
"Nuclear membrane",
"Nucleoli",
"Nucleoli fibrillar center",
"Nucleoli rim",
"Nucleoplasm",
"Nuclear bodies",
"Nuclear speckles",
"Actin filaments",
"Focal adhesion sites",
"Centrosome",
"Centriolar satellite",
"Cytosol",
"Cytoplasmic bodies",
"Intermediate filaments",
"Microtubules",
"Mitochondria",
"Endoplasmic reticulum",
"Golgi apparatus",
"Plasma membrane",
"Cell Junctions",
"Vesicles",
"Lipid droplets",
"Peroxisomes",
"Negative",
]
def add_basename(df):
df["basename"] = (
df["if_plate_id"].astype(str)
+ "_"
+ df["position"].astype(str)
+ "_"
+ df["sample"].astype(str)
)
return df
def add_loc_cols(df):
df["locations"] = df["locations"].fillna("Negative")
locations_list = df["locations"].str.split(",").tolist()
labels_onehot = np.array(
[[1 if cat in x else 0 for cat in UNIQUE_CATS] for x in locations_list]
)
df[UNIQUE_CATS] = labels_onehot
return df
def plot_umap(df, feat_cols, save_folder):
umap_ = umap.UMAP(
n_components=2, metric="cosine", min_dist=0.2, n_neighbors=20, n_jobs=-1
)
umap_res = umap_.fit_transform(df[feat_cols])
df["UMAP1"] = umap_res[:, 0]
df["UMAP2"] = umap_res[:, 1]
fig, ax = plt.subplots(figsize=(12, 8))
sns.scatterplot(
x="UMAP1",
y="UMAP2",
hue="locations",
data=df,
palette=cc.glasbey_light,
s=6,
alpha=0.5,
ax=ax,
)
ax.set_box_aspect(1)
handles, labels = ax.get_legend_handles_labels()
for handle in handles:
handle.set_alpha(1)
lgd = ax.legend(
handles=handles,
labels=labels,
bbox_to_anchor=(1.01, 1),
loc=2,
borderaxespad=0.0,
markerscale=3,
)
plt.savefig(f"{save_folder}/umap_locations.pdf", dpi=300, bbox_inches="tight")
fig, ax = plt.subplots(figsize=(12, 8))
sns.scatterplot(
x="UMAP1",
y="UMAP2",
hue="atlas_name",
data=df,
palette=cc.glasbey_light,
s=6,
alpha=0.5,
ax=ax,
)
ax.set_box_aspect(1)
handles, labels = ax.get_legend_handles_labels()
for handle in handles:
handle.set_alpha(1)
lgd = ax.legend(
handles=handles,
labels=labels,
bbox_to_anchor=(1.01, 1),
loc=2,
borderaxespad=0.0,
markerscale=3,
)
plt.savefig(f"{save_folder}/umap_atlas.pdf", dpi=300, bbox_inches="tight")
def plot_channel_clustermap(df, channel_names, save_folder):
protein_localizations = df["locations"].unique()
loc_group_mapping = pd.read_csv("annotations/location_group_mapping.tsv", sep="\t")
loc_group_mapping = loc_group_mapping[
(loc_group_mapping["Original annotation"].isin(protein_localizations))
& (~loc_group_mapping["Grouping 3"].isna())
].reset_index(drop=True)
df = df[df["locations"].isin(loc_group_mapping["Original annotation"])]
df["Location Group"] = df["locations"].map(
dict(
zip(
loc_group_mapping["Original annotation"],
loc_group_mapping["Grouping 3"],
)
)
)
df.index = df["locations"]
for channel_name in channel_names:
channel_name_cols = [col for col in df.columns if f"{channel_name}-" in col]
channel_df = df[channel_name_cols]
channel_df = channel_df.rename(
columns={col: col.split("-")[1] for col in channel_df.columns}
)
lut = dict(zip(df["Location Group"].unique(), cc.glasbey_light))
row_colors = df["Location Group"].map(lut)
sns.clustermap(
channel_df,
cmap="Blues",
row_colors=row_colors,
# center=0,
metric="cosine",
)
plt.savefig(f"{save_folder}/clustermap_{channel_name}.pdf", dpi=300)
if __name__ == "__main__":
exp_folder = "/proj/hpa_subcell/allhpa_ablations_results/"
exp_name_dir_dict = {
"ViT-ProtS-Pool": "rybg_448_nc8_vitb16_pool_fp32_supcon",
"MAE-CellS-ProtS-Pool": "rybg_448_nc8_contrast_mae_pool_vitb16_fp32_mr0.25_objmr0.0_supcon",
}
crop_size = 640
resize = -1
save_folder = (
"/proj/hpa_subcell/allhpa_ablations_results/subcell_results/attention_analysis/"
)
os.makedirs(save_folder, exist_ok=True)
n_channels = 4
channel_names = ["MT", "ER", "Nuclei", "Protein"]
n_transformer_attn_channels = 12
n_pool_attn_channels = 2
channel_cols = channel_names
attn_cols = (
channel_names
+ [f"AH {i+1}" for i in range(n_transformer_attn_channels)]
+ [f"PAH {i+1}" for i in range(n_pool_attn_channels)]
)
feature_path = f"ssl_model_ContrastiveLoss/analysis/best_model_ap/crop_{crop_size}_resize_{resize}/data/hpa_features"
gene_info_df = pd.read_csv("annotations/subcellular_location.tsv", sep="\t")
for i, (method, result_path) in enumerate(exp_name_dir_dict.items()):
method_feature_path = (
feature_path.replace("ssl_model_ContrastiveLoss/", "")
if "CellS" not in method
else feature_path
)
df, corr_mat = torch.load(
f"{exp_folder}/{result_path}/{method_feature_path}/all_corr.pth",
map_location="cpu",
)
exp_save_folder = f"{save_folder}/{method}"
os.makedirs(exp_save_folder, exist_ok=True)
nan_corr_idxs = np.isnan(corr_mat.numpy()).any(axis=(1, 2))
corr_mat = corr_mat[~nan_corr_idxs]
df = df[~nan_corr_idxs].reset_index(drop=True)
nan_loc_idx = df["locations"].isna()
df = df[~nan_loc_idx].reset_index(drop=True)
corr_mat = corr_mat[~nan_loc_idx]
reliable_genes = gene_info_df[
gene_info_df["Reliability"].isin(["Enhanced", "Supported"])
]["Gene"].tolist()
keep_genes = gene_info_df[
gene_info_df["Single-cell variation intensity"].isna()
& gene_info_df["Single-cell variation spatial"].isna()
& gene_info_df["Cell cycle dependency"].isna()
& gene_info_df["Reliability"].isin(["Enhanced", "Supported"])
]["Gene"].tolist()
keep_idxs = df["ensembl_ids"].apply(
lambda x: any([gene in x for gene in keep_genes])
)
print(f"Keep genes: {keep_idxs.sum()}")
df = df[keep_idxs].reset_index(drop=True)
corr_mat = corr_mat[keep_idxs]
attn_cols_flatten = [
f"{c}-{attn}" for c in channel_cols for attn in attn_cols[4:]
]
df = add_loc_cols(df)
corr = corr_mat[:, :n_channels, n_channels:].reshape(len(df), -1)
loc_attn_df = df.copy()
loc_attn_df[attn_cols_flatten] = corr
loc_attn_df = loc_attn_df.groupby("locations")[attn_cols_flatten].mean()
loc_attn_df["locations"] = loc_attn_df.index
plot_channel_clustermap(loc_attn_df, channel_names, exp_save_folder)
df = add_basename(df)
df_fov_attn = df.copy()
df_fov_attn[attn_cols_flatten] = corr
df_fov_attn = df_fov_attn.groupby("basename")[attn_cols_flatten].mean()
df_fov_attn["locations"] = df.groupby("basename")["locations"].first()
df_fov_attn["atlas_name"] = df.groupby("basename")["atlas_name"].first()
df_fov_attn["locations"] = df_fov_attn["locations"].apply(
lambda x: x if "," not in x else "Multilocalizing"
)
plot_umap(df_fov_attn, attn_cols_flatten, exp_save_folder)