-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.py
273 lines (257 loc) · 12.4 KB
/
getData.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
269
270
271
272
273
import dgl
import torch
import pandas as pd
import torch.nn.functional as F
class getData:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def seq_to_seq_edge_regression(self):
df = pd.read_csv('Protein-Protein/sequences/unique_sequences.csv', na_filter = False, sep=',',
dtype={'sequence': str, 'id': int})
sequences = df['sequence'].values
id_uniq_seq = df['id'].values
uniq_sequences = {}
for i, data in enumerate(sequences):
if sequences[i] not in uniq_sequences:
uniq_sequences[sequences[i]] = id_uniq_seq[i]
df = pd.read_csv('Protein-Protein/datasets/pip_database.csv', na_filter = False, sep=',',
dtype={'seq_0': str, 'seq_1': str, 'target': float})
id1 = df['seq_0'].values
id2 = df['seq_1'].values
sc = df['target'].values
q1 = df['target'].quantile(0.25)
q3 = df['target'].quantile(0.75)
iqr = q3 - q1
id_dic = {}
#########################
count = 0
for i, data in enumerate(id1):
if id1[i] not in id_dic and id1[i] in uniq_sequences and sc[i] <= q3 + 1.5*iqr:
id_dic[id1[i]] = count
count = count + 1
if id2[i] not in id_dic and id2[i] in uniq_sequences and sc[i] <= q3 + 1.5*iqr:
id_dic[id2[i]] = count
count = count + 1
# EDGE INDEX Y LABEL (TAMBIEN EL SCORE)
new_id1 = []
new_id2 = []
new_score = []
label = []
for i, data in enumerate(id1):
if id1[i] in uniq_sequences and id2[i] in uniq_sequences and sc[i] <= q3 + 1.5*iqr:
new_id1.append(id_dic[id1[i]])
new_id2.append(id_dic[id2[i]])
new_score.append(sc[i])
if sc[i] < 0.865739974975586:
label.append(0)
else:
label.append(1)
label = torch.tensor(label)
edge_label = torch.Tensor(new_score)
# NODES FEATURES
df_2 = pd.read_csv('features/Seq2Feature_summarized/resultados/result_values_normalized.csv', na_filter = False, header=None, sep='|')
columna_features = df_2.values
old_id_score_dic = {}
num_features = 0
for i, col in enumerate(columna_features):
split = columna_features[i][0].split(',')
id = columna_features[i][0].split(',')[0]
old_id_score_dic[id] = [round(float(x), 3) for x in split[1:]]
new_score = []
new_scoreid = []
for i, data in enumerate(id_dic.keys()):
new_scoreid.append(data)
new_score.append(old_id_score_dic[data])
nodes_features = torch.tensor(new_score)
g = dgl.graph((new_id1, new_id2))
g.ndata['feat'] = nodes_features
edge_label_normalized = F.normalize(edge_label, p=2, dim=0)
# g.edata['weight'] = edge_label_normalized
g.edata['weight'] = edge_label
g.edata['label'] = label
return g
def encoding_edge_regression(self, method):
df = pd.read_csv('Protein-Protein/sequences/unique_sequences.csv', na_filter = False, sep=',',
dtype={'sequence': str, 'id': int})
sequences = df['sequence'].values
id_uniq_seq = df['id'].values
uniq_sequences = {}
for i, data in enumerate(sequences):
if sequences[i] not in uniq_sequences:
uniq_sequences[sequences[i]] = id_uniq_seq[i]
# NODES FEATURES EXTRACT
df_2 = pd.read_csv('Protein-Protein/encoded_sequences/' + method + '/dataset_encoding.csv', na_filter = False, sep='|')
columna_features = df_2.values
old_id_score_dic = {}
for i, col in enumerate(columna_features):
if method[:3] == 'phy' or method[:3] == 'phy':
split = columna_features[i][0].split(',')
id = int(float(columna_features[i][0].split(',')[0]))
old_id_score_dic[id] = [round(float(x), 3) for x in split[1:]]
else:
split = columna_features[i][0].split(',')
id = int(float(columna_features[i][0].split(',')[-1]))
old_id_score_dic[id] = [round(float(x), 3) for x in split[:-1]]
df = pd.read_csv('Protein-Protein/datasets/pip_database.csv', na_filter = False, sep=',',
dtype={'seq_0': str, 'seq_1': str, 'target': float})
id1 = df['seq_0'].values
id2 = df['seq_1'].values
sc = df['target'].values
q1 = df['target'].quantile(0.25)
q3 = df['target'].quantile(0.75)
iqr = q3 - q1
#########################
count = 0
id_dic = {}
for i, data in enumerate(id1):
if id1[i] not in id_dic and id1[i] in uniq_sequences and sc[i] <= q3 + 1.5*iqr:
id_dic[id1[i]] = count
count = count + 1
if id2[i] not in id_dic and id2[i] in uniq_sequences and sc[i] <= q3 + 1.5*iqr:
id_dic[id2[i]] = count
count = count + 1
#########################
# EDGES
index_1 = []
index_2 = []
new_score = []
label = []
for i, data in enumerate(id1):
if id1[i] in id_dic and id2[i] in id_dic and sc[i] <= q3 + 1.5*iqr:
index_1.append(id_dic[id1[i]])
index_2.append(id_dic[id2[i]])
new_score.append(sc[i])
if sc[i] < 0.865739974975586:
label.append(0)
else:
label.append(1)
label = torch.tensor(label)
edge_label = torch.tensor(new_score)
nodes_feat = []
for i, data in enumerate(id_dic.keys()):
id_uniq = uniq_sequences[data]
nodes_feat.append(old_id_score_dic[id_uniq])
nodes_features = torch.tensor(nodes_feat)
g = dgl.graph((index_1, index_2))
g.ndata['feat'] = nodes_features
g.edata['weight'] = edge_label.float()
g.edata['label'] = label
return g
# def seq_to_seq_linkpred(self):
# df = pd.read_csv('Protein-Protein/sequences/unique_sequences.csv', na_filter = False, sep=',',
# dtype={'sequence': str, 'id': int})
# sequences = df['sequence'].values
# id_uniq_seq = df['id'].values
# uniq_sequences = {}
# for i, data in enumerate(sequences):
# if sequences[i] not in uniq_sequences:
# uniq_sequences[sequences[i]] = id_uniq_seq[i]
# df = pd.read_csv('Protein-Protein/datasets/pip_database.csv', na_filter = False, sep=',',
# dtype={'seq_0': str, 'seq_1': str, 'target': float})
# id1 = df['seq_0'].values
# id2 = df['seq_1'].values
# sc = df['target'].values
# id_dic = {}
# ######################### SI SE USA sc[i] >= 1 PARA FILTRAR FUNCIONA BASTANTE BIEN #######################
# count = 0
# for i, data in enumerate(id1):
# if id1[i] not in id_dic and id1[i] in uniq_sequences:
# id_dic[id1[i]] = count
# count = count + 1
# if id2[i] not in id_dic and id2[i] in uniq_sequences:
# id_dic[id2[i]] = count
# count = count + 1
# # EDGE INDEX Y LABEL (TAMBIEN EL SCORE)
# new_id1 = []
# new_id2 = []
# for i, data in enumerate(id1):
# if id1[i] in id_dic and id2[i] in id_dic:
# new_id1.append(id_dic[id1[i]])
# new_id2.append(id_dic[id2[i]])
# # NODES FEATURES
# df_2 = pd.read_csv('features/Seq2Feature_summarized/resultados/result_values_normalized.csv', na_filter = False, header=None, sep='|')
# columna_features = df_2.values
# old_id_score_dic = {}
# num_features = 0
# for i, col in enumerate(columna_features):
# split = columna_features[i][0].split(',')
# id = columna_features[i][0].split(',')[0]
# # features primeras 20 positivas
# # features_to_use = [split[121], split[88], split[87], split[120], split[122],
# # split[102], split[65], split[79], split[20], split[53],
# # split[63], split[92], split[64], split[108], split[42],
# # split[94], split[107], split[67], split[116], split[109]]
# # features primeras 10 positivas y ultimas 10 negativas, de mas negativa a menos
# # features_to_use = [split[121], split[88], split[87], split[120], split[122],
# # split[102], split[65], split[79], split[20], split[53],
# # split[73], split[112], split[78], split[117], split[85],
# # split[22], split[119], split[110], split[118], split[105]]
# # features primeras 8 positivas y ultimas 8 negativas, de mas negativa a menos
# # features_to_use = [split[121], split[88], split[87], split[120], split[122],
# # split[102], split[65], split[79], split[20],
# # split[73], split[112], split[78], split[117], split[85],
# # split[22], split[119], split[110], split[118]]
# # if num_features == 0:
# # num_features = len(features_to_use)
# old_id_score_dic[id] = [round(float(x), 3) for x in split[1:]]
# new_score = []
# new_scoreid = []
# for i, data in enumerate(id_dic.keys()):
# new_scoreid.append(data)
# new_score.append(old_id_score_dic[data])
# nodes_features = torch.tensor(new_score)
# g = dgl.graph((new_id1, new_id2))
# g.ndata['feat'] = nodes_features
# return g
# def encoding_link_pred(self, method):
# # NODES FEATURES EXTRACT
# df = pd.read_csv('Protein-Protein/sequences/unique_sequences.csv', na_filter = False, sep=',',
# dtype={'sequence': str, 'id': int})
# sequences = df['sequence'].values
# id_uniq_seq = df['id'].values
# uniq_sequences = {}
# for i, data in enumerate(sequences):
# if sequences[i] not in uniq_sequences:
# uniq_sequences[sequences[i]] = id_uniq_seq[i]
# # NODES FEATURES EXTRACT
# df_2 = pd.read_csv('Protein-Protein/encoded_sequences/' + method + '/dataset_encoding.csv', na_filter = False, sep='|')
# columna_features = df_2.values
# old_id_score_dic = {}
# for i, col in enumerate(columna_features):
# if method[:3] == 'phy':
# split = columna_features[i][0].split(',')
# id = int(float(columna_features[i][0].split(',')[0]))
# old_id_score_dic[id] = [round(float(x), 3) for x in split[1:]]
# else:
# split = columna_features[i][0].split(',')
# id = int(float(columna_features[i][0].split(',')[-1]))
# old_id_score_dic[id] = [round(float(x), 3) for x in split[:-1]]
# df = pd.read_csv('Protein-Protein/datasets/pip_database.csv', na_filter = False, sep=',',
# dtype={'seq_0': str, 'seq_1': str, 'target': float})
# id1 = df['seq_0'].values
# id2 = df['seq_1'].values
# #########################
# count = 0
# id_dic = {}
# for i, data in enumerate(id1):
# if id1[i] not in id_dic and id1[i] in uniq_sequences:
# id_dic[id1[i]] = count
# count = count + 1
# if id2[i] not in id_dic and id2[i] in uniq_sequences:
# id_dic[id2[i]] = count
# count = count + 1
# #########################
# # EDGES
# index_1 = []
# index_2 = []
# for i, data in enumerate(id1):
# if id1[i] in id_dic and id2[i] in id_dic:
# index_1.append(id_dic[id1[i]])
# index_2.append(id_dic[id2[i]])
# nodes_feat = []
# for i, data in enumerate(id_dic.keys()):
# id_uniq = uniq_sequences[data]
# nodes_feat.append(old_id_score_dic[id_uniq])
# nodes_features = torch.tensor(nodes_feat)
# g = dgl.graph((index_1, index_2))
# g.ndata['feat'] = nodes_features
# return g