forked from Biocomputing-Research-Group/DeepFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataUtils.py
More file actions
303 lines (266 loc) · 9.27 KB
/
DataUtils.py
File metadata and controls
303 lines (266 loc) · 9.27 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torch.utils.data as Data
import torchvision
import time
import json
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer
import pandas as pd
from datetime import timedelta
from sklearn import metrics
from sklearn.model_selection import StratifiedKFold
import numpy as np
import sys
PEP = 0.93
def expToDict(fp):
exp_dic = dict()
scan = []
for line_id, line in enumerate(fp):
line = line.strip()
if line_id % 5 == 0:
if len(scan) != 0:
exp_dic[key] = scan
scan = []
key = line
else:
junk = line.split(' ')
x = []
if len(junk) > 1:
for each in junk:
x.append(float(each)) # timediff=3.96
scan.append(x)
else:
scan.append([])
exp_dic[key] = scan # timediff=8.97
return exp_dic
def theoryToDict(fp):
theory_dic = dict()
scan = []
for line_id, line in enumerate(fp):
line = line.strip()
if line_id % 7 == 0:
if len(scan) != 0:
theory_dic[key] = scan
scan = []
key = line
else:
junk = line.split(' ')
x = []
if len(junk) > 1:
for each in junk:
x.append(float(each))
scan.append(x)
else:
scan.append([])
theory_dic[key] = scan # timediff=29.8
return theory_dic
def featureToDict(fp):
feature = fp.read().strip().split('\n\n')
feature_dic = dict()
for scan in feature:
lines = scan.strip().split('\n')
pepidx = lines[0]
feature = lines[2].strip().split()
# feature_dic[pepidx] = feature #timediff=0.23
# feature_dic[pepidx] = np.asarray(feature,dtype=float) #timediff=1.14
feature_dic[pepidx] = [float(x) for x in feature] # timediff=0.55
fp.close()
return feature_dic
def LabelToDict(fp):
sample = fp.read().strip().split('\n')[1:]
label_dic = dict()
for scan in sample:
scan = scan.strip().split(',')
idx = '{0}_{1}_{2}_{3}'.format(str(scan[2]), str(scan[3]), str(scan[4]), str(scan[5]))
weight = float(scan[1])
pep = float(scan[8])
if scan[0] == 'True':
label = 1
else:
label = 0
label_dic[idx] = [pep, label, weight]
return label_dic
def readData(expPrefix, theoryPrefix, featurePrefix, LabelPrefix, filenum):
L = []
Y = []
weight = []
for i in range(1, int(filenum) + 1):
filename = theoryPrefix + '_' + str(i) + '.txt'
f = open(filename)
D_theory = theoryToDict(f)
filename = featurePrefix + '_' + str(i) + '.txt'
f = open(filename)
D_feature = featureToDict(f)
filename = expPrefix + '_' + str(i) + '.txt'
f = open(filename)
D_exp = expToDict(f)
filename = LabelPrefix + '_' + str(i) + '.csv'
f = open(filename)
D_Label = LabelToDict(f)
for j in D_Label.keys():
if D_Label[j][0] < PEP:
l = []
if j[:-4] not in D_exp.keys():
continue
else:
l.append(D_exp[j[:-4]])
l.append(D_theory[j])
l.append(D_feature[j])
L.append(l)
Y.append(D_Label[j][1])
weight.append(D_Label[j][2])
D_theory = dict()
D_exp = dict()
D_feature = dict()
D_Label = dict()
return L, Y, weight
def readTestData(iexp,itheory,ifeature):
L=[]
idx=[]
filename=itheory
f=open(filename)
D_theory=theoryToDict(f)
filename=ifeature
f=open(filename)
D_feature=featureToDict(f)
filename=iexp
f=open(filename)
D_exp=expToDict(f)
for j in D_theory.keys():
l = []
if j[:-4] not in D_exp.keys():
idx.append(False)
continue
l.append(D_exp[j[:-4]])
l.append(D_theory[j])
l.append(D_feature[j])
L.append(l)
idx.append(j)
D_theory = dict()
D_exp = dict()
D_feature = dict()
return L, idx
class DefineDataset(Data.Dataset):
def __init__(self, X_index, L, Y, weight):
self.X_index = X_index
self.L = L
self.Y = Y
self.weight = weight
def __len__(self):
return len(self.X_index)
def __getitem__(self, idx):
idx = self.X_index[idx]
expvec = self.L[idx][0]
theoryvec = self.L[idx][1]
addFeatvec = self.L[idx][2]
width = 0.5
count = 0
construction = []
for i in range(10):
s = []
for j in range(3600):
s.append(0)
construction.append(s)
for construction_id, chargeMZ in enumerate(expvec):
# m/z by charge is not exist
if len(chargeMZ) == 0:
continue
for line_id, line in enumerate(chargeMZ):
if line_id % 2 == 0:
matrix_idx = int((line - 100) / width)
if (line > 1899.9) | (line < 100):
flag = 1
else:
flag = 0
else:
if flag == 0:
if construction_id == 3:
construction[0][matrix_idx] = construction[0][matrix_idx] + line
else:
construction[construction_id + 1][matrix_idx] = construction[construction_id + 1][
matrix_idx] + line
for construction_id, chargeMZ in enumerate(theoryvec):
# m/z by charge is not exist
if len(chargeMZ) == 0:
continue
for line_id, line in enumerate(chargeMZ):
if line_id % 2 == 0:
matrix_idx = int((line - 100) / width)
if (line > 1899.9) | (line < 100):
flag = 1
else:
flag = 0
else:
if flag == 0:
construction[construction_id + 4][matrix_idx] = construction[construction_id + 4][
matrix_idx] + line
construction = np.asarray(construction, dtype=float)
transformer = Normalizer()
construction = transformer.fit_transform(construction)
X = torch.FloatTensor([construction])
feature = torch.FloatTensor(addFeatvec)
y = self.Y[idx]
weight = self.weight[idx]
return X, y, feature, weight
class DefineTestDataset(Data.Dataset):
def __init__(self, X_index, L):
self.X_index = X_index
self.L = L
def __len__(self):
return len(self.X_index)
def __getitem__(self, idx):
idx = self.X_index[idx]
expvec = self.L[idx][0]
theoryvec = self.L[idx][1]
addFeatvec = self.L[idx][2]
width = 0.5
count = 0
construction = []
for i in range(10):
s = []
for j in range(3600):
s.append(0)
construction.append(s)
for construction_id, chargeMZ in enumerate(expvec):
# m/z by charge is not exist
if len(chargeMZ) == 0:
continue
for line_id, line in enumerate(chargeMZ):
if line_id % 2 == 0:
matrix_idx = int((line - 100) / width)
if (line > 1899.9) | (line < 100):
flag = 1
else:
flag = 0
else:
if flag == 0:
if construction_id == 3:
construction[0][matrix_idx] = construction[0][matrix_idx] + line
else:
construction[construction_id + 1][matrix_idx] = construction[construction_id + 1][
matrix_idx] + line
for construction_id, chargeMZ in enumerate(theoryvec):
# m/z by charge is not exist
if len(chargeMZ) == 0:
continue
for line_id, line in enumerate(chargeMZ):
if line_id % 2 == 0:
matrix_idx = int((line - 100) / width)
if (line > 1899.9) | (line < 100):
flag = 1
else:
flag = 0
else:
if flag == 0:
construction[construction_id + 4][matrix_idx] = construction[construction_id + 4][
matrix_idx] + line
construction = np.asarray(construction, dtype=float)
transformer = Normalizer()
construction = transformer.fit_transform(construction)
X = torch.FloatTensor([construction])
feature = torch.FloatTensor(addFeatvec)
return X, feature