-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestUtils.py
165 lines (133 loc) · 4.08 KB
/
testUtils.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
import random
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
def cales(pos, total):
t = -100000
es = 0
for i in range(total):
if i in pos:
es += 1
else:
es -= len(pos) / (total - len(pos))
if es > t:
t = es
return t
def calres(pos, total):
t = -100000
es = 0
res = []
for i in range(total):
if i in pos:
es += 1
else:
es -= len(pos) / (total - len(pos))
if es > t:
t = es
res.append(es)
return res
def calpermutation(pos, total, permutation=100000):
res = []
for i in tqdm(range(permutation)):
q = random.sample(range(total), pos)
t = cales(q, total)
res.append(t)
return res
def gsea(pos, total, permutation=100000, t=None):
es = cales(pos, total)
cnt = 0
if t is None:
for i in tqdm(range(permutation)):
q = random.sample(range(total), len(pos))
t = cales(q, total)
if t >= es:
cnt += 1
return cnt / permutation
else:
for i in t:
if i >= es:
cnt += 1
return cnt / len(t)
def yeast(path, ingore_structure=False, col="yeast_1"):
a = np.loadtxt(path)
df = pd.read_csv("temp/expres.csv", index_col=0)
df["new"] = a
df = df.sort_values("new", ascending=False)
df["rank"] = range(len(df))
if ingore_structure:
df = df[df[col] == 1]
else:
df = df[df[col] >= 1]
return df
def getEmbeddings(
model, dl, device, trunc=30000, model_type="esm3", take_embed="first"
):
res = []
labels = []
model = model.to(device)
model.eval()
pbar = tqdm(dl)
cnt = 0
with torch.no_grad():
for i, j in enumerate(pbar):
if isinstance(j, list):
if j[1].dim() == 2:
labels.append(j[1][0][0].item())
else:
labels.append(j[1][0].item())
j = j[0]
if cnt == trunc:
break
cnt += 1
for track in ["seq_t", "structure_t", "ss8_t", "sasa_t"]:
if track not in j:
j[track] = None
else:
j[track] = j[track].to(device)
if len(j[track].size()) == 1:
j[track] = j[track].unsqueeze(0)
if model_type == "esm3":
representations = model(
sequence_tokens=j["seq_t"],
structure_tokens=j["structure_t"],
ss8_tokens=j["ss8_t"],
sasa_tokens=j["sasa_t"],
)
elif model_type == "esmc":
representations = model(
sequence_tokens=j["seq_t"],
)
if take_embed == "first":
representations = representations.embeddings[:, 0]
else:
representations = torch.mean(representations.embeddings, dim=1)
res.append(representations.cpu().numpy())
return res, labels
def tsnedf(embed, labels):
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
q = np.array(embed)
q = q.squeeze()
pca = PCA(n_components=30)
pca_seq = pca.fit_transform(q)
pca_seq.shape
X_embedded = TSNE(n_components=2).fit_transform(pca_seq)
df = pd.DataFrame({"x": X_embedded[:, 0], "y": X_embedded[:, 1], "label": labels})
return df
def umapdf(embed, labels, min_dist=0.3, n_neighbors=15):
import umap
q = np.array(embed)
q = q.squeeze()
reducer = umap.UMAP(min_dist=min_dist, n_neighbors=n_neighbors)
embedding = reducer.fit_transform(q)
df = pd.DataFrame({"x": embedding[:, 0], "y": embedding[:, 1], "label": labels})
return df
def ERcorrelation(path, col="experiment_1"):
from scipy import stats
a = np.loadtxt(path)
df = pd.read_csv("temp/expres.csv", index_col=0)
df["new"] = a
df = df[df[col] > 0]
print(stats.spearmanr(df["new"], -df[col]))
return df