-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_rf_helper.py
351 lines (323 loc) · 9.28 KB
/
N_rf_helper.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import os
import time
# read csv for preprocessing
# conver to np array
def preprocess():
pos_data_path = "./dataset/positive"
neg_data_path = "./dataset/negative"
pos_file = os.listdir(pos_data_path)
neg_file = os.listdir(neg_data_path)
merge_path = './merge/'
intersected = './merge/intersected/'
print("="*20,"start rf","="*20)
print("="*20,"timestamp ",int(time.time()),"="*20)
god = """
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃神兽保佑┣┓
┃永无BUG!┏┛
┗┓┓┏━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
print(god)
p_list = []
n_list = []
for item in pos_file:
if item[0:4] == 'data' and item[len(item)-4:] == '.csv':
p_list.append(item)
for item in neg_file:
if item[0:4] == 'data' and item[len(item)-4:] == '.csv':
n_list.append(item)
print(p_list)
print(n_list)
lis_buffer = []
for csv in p_list:
print('file pos ', csv)
lis_buffer.append(pd.read_csv(pos_data_path+'/'+csv))
p_list = lis_buffer.copy()
lis_buffer = []
for csv in n_list:
print('file neg ', csv)
lis_buffer.append(pd.read_csv(neg_data_path+'/'+csv))
n_list = lis_buffer.copy()
lis_buffer = []
print('====finish putting pd to arrays====')
return p_list, n_list
id_protocol = ['ID', [('id', 1), ('Uniprot accession',0), ('ID',0)]] # [1] of tuple means whether split fetch is needed
site_protocol = ['site', ['Site', 'n', 'site']]
def cut_id(src, col ,do,output =None):
df = pd.read_csv(src, index_col=0)
counter = 0
for i, row in df.iterrows():
counter += 1
if counter %100 == 0:
print('count',counter)
new_v = do(row[col])
df.loc[i,col] = new_v
#print('i',i)
#print(new_v)
print('finish edit id ', src)
if not output == None:
df.to_csv(output)
def concat_dataset(src, output = None):
raw = [pd.read_csv(s) for s in src ]
out = pd.concat(raw)
if not output == None:
out.to_csv(output)
print('finish concat ', src)
return out
def extend(base ,source, output = None):
base_df = pd.read_csv(base)
source_df = pd.read_csv(source)
new_row = {}
new_row['ID'] = []
new_row['site'] = []
for k in list(base_df):
#for furtber optimization, it can change to a take-in lambda
if not k == 'ID' and not k == 'site' and not k == '':
new_row[k] = []
'''
for i, row in base_df.iterrows():
for k in list(base_df):
#for furtber optimization, it can change to a take-in lambda
if not k == 'ID' and not k == 'site' and not k == '':
new_row[k] = []
break
'''
for k in list(source_df):
#for furtber optimization, it can change to a take-in lambda
if not k == 'id' and not k == 'n' and not k == '':
new_row[k] = []
'''
for i, row in source_df.iterrows():
for k in row:
#for furtber optimization, it can change to a take-in lambda
if not k == 'id' and not k == 'n' and not k == '':
new_row[k] = []
break
'''
#print([key for key in new_row])
print('====finish init header===')
counter = 0
mem = {}
for i, row in base_df.iterrows():
#print(row)
row_id = base_df.loc[i,'ID']
row_site = base_df.loc[i,'site']
q= 'n == "' + str(row_site)+ '"'
if not row_id in mem:
x = source_df[source_df.id.str.contains(row_id)]
mem[row_id] = x
else:
x = mem[row_id]
x = x.query(q)
if counter%1000==1:
print('count: ', counter)
#print(q)
#print(x)
counter+=1
#print('====',len(x.index),'====')
if len(x.index) > 0:
for xi, xrow in x.iterrows():
new_row['ID'].append(row_id)
new_row['site'].append(row_site)
for k in list(base_df):
#print('key in row', k)
#print('v in row', base_df.loc[i,k])
if not k == 'ID' and not k == 'site' and k in new_row:
new_row[k].append(base_df.loc[i,k])
for k in list(source_df):
#print('key in x', k)
#print('v in x', source_df.loc[xi,k])
if not k == 'id' and not k == 'n' and k in new_row:
new_row[k].append(source_df.loc[xi,k])
#print('add', row_id, row_site)
out = pd.DataFrame(new_row)
print('finsh', out.shape)
if not output == None:
out.to_csv(output)
return out
def merge(li,logger,fn=None, debug_limit=False ):
#fetch all header
curr_time = str(int(time.time()))
print('=============merge() is called !!!!!=====================')
header = set()
# loop over ther file to build header
for item in li:
h = list(item)
for sing in h:
header.add(sing)
#remove various format of id
for rand_id in id_protocol[1]:
if rand_id[0] in header:
header.remove(rand_id[0])
#remove various format of site
for rand_site in site_protocol[1]:
if rand_id in header:
header.remove(rand_id)
#add genralzied id and site
header.add(id_protocol[0])
header.add(site_protocol[0])
header = list(header)
print('====finish preprocessing and header construction====')
print('length: ', len(header))
#row = [-1 for _ in range(len(header))]
# ID & site and id & n
out = pd.DataFrame(columns = header)
#phase 1
tracker = 0
new_row = {}
for df in li:
print('start df', tracker)
tracker += 1
debug_it = 0
for i, src in df.iterrows(): # handle row by row
if debug_it%1000 == 0:
print("debug_it count:", debug_it)
if not debug_limit == False and debug_it > debug_limit:
break
#print(src.index)
ID = None
site = None
# preprocess id and site
for rand_id in id_protocol[1]:
if rand_id[0] in src.index:
if rand_id[1] == 1:
ID = src[rand_id[0]].split('_')[1] # split and fetch
else:
ID = src[rand_id[0]] # orginal value
for rand_site in site_protocol[1]:
if rand_site in src.index:
site = src[rand_site] #original value
if ID == None:
ID = src[id_protocol[0]]
if site == None:
site = src[site_protocol[0]]
logger.write( str(ID) + ": " + str(site) + '\n')
#if exist in out
if len(out[out[id_protocol[0]].str.contains(ID)].index) > 0:
tmp = out[out[id_protocol[0]].str.contains(ID)]
if len(tmp[tmp[site_protocol[0]].str.contains(site)].index) > 0:
ind = tmp[tmp[site_protocol[0]].str.contains(site)].index[0]
logger.write( "exist: " + str(ID) + ": " + str(site) + '\n')
for h in header:
logger.write("header: "+ h)
logger.write('\n')
if h == 'ID':
pass
elif h =='site':
pass
elif h in src.index:
print(h, src[h])
logger.write("found! val: "+ str(src[h]))
logger.write('\n')
out.loc[ind,h] = src[h]
else:
pass
continue
#otherwise
#print(src.index)
for h in header:
#h = h.lower()
logger.write("header: "+ h)
logger.write('\n')
#print("header: "+ h)
if h == id_protocol[0]:
logger.write("found ID! val: "+ str(ID) )
logger.write('\n')
#new_row.append(str(ID))
if h in new_row:
new_row[h].append(str(ID))
else:
new_row[h] = [str(ID)]
elif h == site_protocol[0]:
logger.write("found site! val: "+ str(site))
logger.write('\n')
#new_row.append(str(site))
#new_row[h] = str(site)
if h in new_row:
new_row[h].append(str(site))
else:
new_row[h] = [str(site)]
elif h in src.index:
logger.write("found! val: "+ str(src[h]))
#print("found! val: "+ str(src[h]))
logger.write('\n')
#new_row.append(str(src[h]))
#new_row[h] = str(src[h])
if h in new_row:
new_row[h].append(src[h])
else:
new_row[h] = [src[h]]
else:
#new_row.append(str(-1))
if h in new_row:
new_row[h].append(str(0))
else:
new_row[h] = [str(0)]
#new_row[h] = str(-1)
logger.write( "="*20 + "finish row" + "="*20 + '\n')
#print("rowlen", len(new_row), "header",len(header))
for h in header:
logger.write("header:"+h+'\n')
#new_df = pd.DataFrame(new_row, index=[0],columns=header)
#out = out.append(new_df)
#collector.append(new_row.copy())
debug_it += 1
#backup of merged file
out = pd.DataFrame(new_row, columns=header)
print(out.shape)
if fn == None:
out.to_csv(merge_path+"merged_"+curr_time+".csv")
else:
out.to_csv(merge_path+fn+".csv")
return out
def intersect_df(list1,list2):
header1 = list(list1)
header2 = list(list2)
head = set()
for h in header1:
head.add(h)
for h in header2:
if h in head:
head.remove(h)
else:
head.add(h)
#head = list(head)
out1 = list1.copy()
out2 = list2.copy()
for h in head:
if h in out1:
out1 = out1.drop(columns=[h])
else:
out2 = out2.drop(columns=[h])
return out1, out2
if __name__=='__main__':
'''
with open("merge_p_list.txt", "w") as logger:
list1 = merge(p_list,logger,fn="merged_positive")
print("finish p_list")
'''
'''
list1 = pd.read_csv("./merge/merged_positive.csv")
print("finish p_list")
with open("merge_n_list.txt", "w") as logger:
list2 = merge(n_list,logger,fn="merged_negative")
print("finish n_list")
list1, list2 = intersect_df(list1, list2)
list1.to_csv(intersected+"processed_positive.csv")
list2.to_csv(intersected+"processed_negative.csv")
'''
print('list1 header len: ' , len(list(list1)))
print('list2 header len: ' , len(list(list2)))
print('list1 len: ' , len(list1.index))
print('list2 len: ' , len(list2.index))