-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDmicro_preprocess.py
567 lines (526 loc) · 17.6 KB
/
GDmicro_preprocess.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import re
import os
import sys
import argparse
import numpy as np
import pandas as pd
from collections import defaultdict
import uuid
def trans_meta(in1,in2,out):
f1=open(in1,'r')
f2=open(in2,'r')
o=open(out,'w+')
line=f1.readline().strip()
o.write(line+'\tclass\n')
c=0
while True:
line=f1.readline().strip()
if not line:break
o.write(line+'\ttrain\n')
c+=1
line=f2.readline()
while True:
line=f2.readline().strip()
if not line:break
ele=line.split('\t')
ele[0]=str(c)
o.write('\t'.join(ele)+'\ttest\n')
c+=1
o.close()
def trans_meta_train(infile,out):
f=open(infile,'r')
o=open(out,'w+')
line=f.readline().strip()
o.write(line+'\tclass\n')
while True:
line=f.readline().strip()
if not line:break
o.write(line+'\ttrain\n')
o.close()
def extract_tout(tout,d,ofile):
o=open(ofile,'w+')
f=open(tout,'r')
line=f.readline().strip()
ele=line.split('\t')
dc={}
c=0
arr=[]
for e in ele:
if e in d:
dc[c]=''
arr.append(e)
c+=1
o.write('\t'.join(arr)+'\n')
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
o.write(ele[0])
c=0
tem=[]
for e in ele[1:]:
if c in dc:
#o.write('\t'+e)
tem.append(e)
c+=1
o.write('\t'+'\t'.join(tem)+'\n')
o.close()
def normalize_data_small(infile,mtype,meta,dtype,ofile,inmerge,inmerge_meta):
f=open(meta,'r')
meta_content=[]
line=f.readline().strip()
meta_content.append(line)
c=0
ag=0
d={}
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
d[ele[2]]=''
meta_content.append(line)
label=line.split('\t')[3]
if label=='Unknown':
ag=1
c+=1
uid = uuid.uuid1().hex
if ag==1:
n_split_d=int(c/2)
n_split_h=c-n_split_d
ml_d=[dtype for i in range(n_split_d)]
ml_h=['healthy' for i in range(n_split_h)]
ml=ml_d+ml_h
#print(ml)
#exit()
if len(meta_content[1:])<13:
tmeta = 'tem_meta_' + uid + '.tsv'
ot = open(tmeta, 'w+')
ot.write(meta_content[0]+'\n')
ft=open(inmerge_meta,'r')
line=ft.readline()
i=0
while True:
line=ft.readline().strip()
if not line:break
ele=line.split('\t')
if ele[-1]=='train':
ot.write('\t'.join(ele[:-1])+'\n')
else:
ele[3]=ml[i]
i+=1
ot.write('\t'.join(ele[:-1])+'\n')
ot.close()
tout='tem_matrix_' + uid + '.tsv'
os.system('Rscript norm_features.R ' + mtype + ' ' + inmerge + ' ' + tmeta + ' ' + dtype + ' ' + tout)
extract_tout(tout,d,ofile)
os.system('rm ' + tmeta+' '+tout)
else:
tmeta='tem_meta_'+uid+'.tsv'
ot=open(tmeta,'w+')
ot.write(meta_content[0]+'\n')
i=0
for c in meta_content[1:]:
ele=c.split('\t')
ele[3]=ml[i]
i+=1
ot.write('\t'.join(ele)+'\n')
ot.close()
#exit()
os.system('Rscript norm_features.R '+mtype+' '+infile+' '+tmeta+' '+dtype+' '+ofile)
os.system('rm '+tmeta)
else:
if len(meta_content[1:]) < 13:
tout = 'tem_matrix_' + uid + '.tsv'
os.system('Rscript norm_features.R ' + mtype + ' ' + inmerge + ' ' + inmerge_meta + ' ' + dtype + ' ' + tout)
extract_tout(tout, d, ofile)
os.system('rm ' + tout)
else:
os.system('Rscript norm_features.R '+mtype+' '+infile+' '+meta+' '+dtype+' '+ofile)
def normalize_data(infile,mtype,meta,dtype,ofile):
f=open(meta,'r')
meta_content=[]
line=f.readline().strip()
meta_content.append(line)
c=0
ag=0
#d={}
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
#d[ele[2]]=''
meta_content.append(line)
label=line.split('\t')[3]
if label=='Unknown':
ag=1
c+=1
if ag==1:
n_split_d=int(c/2)
n_split_h=c-n_split_d
ml_d=[dtype for i in range(n_split_d)]
ml_h=['healthy' for i in range(n_split_h)]
ml=ml_d+ml_h
#print(ml)
#exit()
uid = uuid.uuid1().hex
tmeta='tem_meta_'+uid+'.tsv'
ot=open(tmeta,'w+')
ot.write(meta_content[0]+'\n')
i=0
for c in meta_content[1:]:
ele=c.split('\t')
ele[3]=ml[i]
i+=1
ot.write('\t'.join(ele)+'\n')
ot.close()
#exit()
os.system('Rscript norm_features.R '+mtype+' '+infile+' '+tmeta+' '+dtype+' '+ofile)
os.system('rm '+tmeta)
else:
os.system('Rscript norm_features.R '+mtype+' '+infile+' '+meta+' '+dtype+' '+ofile)
def load_train_sp(infile,d,all_sp):
f=open(infile,'r')
samples=f.readline().strip().split()
anno={}
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
c=0
anno[ele[0]]=''
for e in ele[1:]:
d[ele[0]][samples[c]]=e
c+=1
all_sp[ele[0]]=''
return samples,anno
def load_test_sp(infile,d,anno):
f=open(infile,'r')
samples=f.readline().strip().split()
count=0
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
c=0
if ele[0] in anno:
count+=1
for e in ele[1:]:
d[ele[0]][samples[c]]=e
c+=1
#print(count,' species of training datasets are detected in test datasets.')
return samples
def merge_sp(in1,in2,out):
d=defaultdict(lambda:{})
all_sp={}
s1,anno=load_train_sp(in1,d,all_sp)
s2=load_test_sp(in2,d,anno)
samples=s1+s2
o=open(out,'w+')
o.write('\t'.join(samples)+'\n')
for e in sorted(all_sp.keys()):
o.write(e)
for s in samples:
if s in d[e]:
o.write('\t'+str(d[e][s]))
else:
o.write('\t'+str(0))
o.write('\n')
def load_item(infile,d,all_item):
f=open(infile,'r')
samples=f.readline().strip().split()
while True:
line=f.readline().strip()
if not line:break
ele=line.split()
c=0
for e in ele[1:]:
d[ele[0]][samples[c]]=e
c+=1
all_item[ele[0]]=''
return samples
def merge_eggNOG(in1,in2,out):
d=defaultdict(lambda:{})
all_item={}
s1=load_item(in1,d,all_item)
s2=load_item(in2,d,all_item)
samples=s1+s2
o=open(out,'w+')
o.write('\t'.join(samples)+'\n')
for e in sorted(all_item.keys()):
o.write(e)
for s in samples:
if s in d[e]:
o.write('\t'+str(d[e][s]))
else:
o.write('\t'+str(0))
o.write('\n')
def trans2node(infile,meta,ofile):
f=open(meta,'r')
status=[]
line=f.readline()
while True:
line=f.readline().strip()
if not line:break
ele=line.split('\t')
if ele[3]=='healthy':
status.append('Health')
else:
status.append(ele[3])
a=pd.read_table(infile)
a=a.T
a=np.array(a)
c=0
o=open(ofile,'w+')
for t in a:
o.write(str(c))
for v in t:
o.write('\t'+str(v))
o.write('\t'+status[c]+'\n')
c+=1
o.close()
def split_file(infile,disease,outdir):
intrain=outdir+'/Split_dir/Train'
intest=outdir+'/Split_dir/Test'
if not os.path.exists(intrain):
os.makedirs(intrain)
if not os.path.exists(intest):
os.makedirs(intest)
f=open(infile,'r')
line=f.readline().strip()
ele=re.split(',',line)
sp=ele[4:]
train_ab=[]
test_ab=[]
sample_train=[]
sample_test=[]
train_meta={}
test_meta={}
while True:
line=f.readline().strip()
if not line:break
ele=re.split(',',line)
if ele[1]=='train':
sample_train.append(ele[0])
train_meta[ele[0]]=[ele[2],ele[3]]
train_ab.append(ele[4:])
else:
sample_test.append(ele[0])
test_meta[ele[0]]=[ele[2],ele[3]]
test_ab.append(ele[4:])
o1=open(intrain+'/'+disease+'_meta.tsv','w+')
o2=open(intrain+'/'+disease+'_sp_matrix.csv','w+')
o1.write('sampleID\tstudyName\tsubjectID\tdisease\tcountry\n')
c=0
for s in sample_train:
o1.write(str(c)+'\t'+train_meta[s][1]+'\t'+s+'\t'+train_meta[s][0]+'\t'+train_meta[s][1]+'\n')
c+=1
o2.write('\t'.join(sample_train)+'\n')
c=0
train_ab=np.array(train_ab)
train_ab=train_ab.T
for s in sp:
tab=0
for x in train_ab[c]:
tab+=float(x)
if tab==0:
c+=1
continue
o2.write(s+'\t'+'\t'.join(train_ab[c])+'\n')
c+=1
if len(sample_test)>0:
o3=open(intest+'/'+disease+'_meta.tsv','w+')
o4=open(intest+'/'+disease+'_sp_matrix.csv','w+')
o3.write('sampleID\tstudyName\tsubjectID\tdisease\tcountry\n')
c=0
for s in sample_test:
o3.write(str(c)+'\t'+test_meta[s][1]+'\t'+s+'\t'+test_meta[s][0]+'\t'+test_meta[s][1]+'\n')
c+=1
o4.write('\t'.join(sample_test)+'\n')
c=0
test_ab=np.array(test_ab)
test_ab=test_ab.T
for s in sp:
tab=0
for x in test_ab[c]:
tab+=float(x)
if tab==0:
c+=1
continue
o4.write(s+'\t'+'\t'.join(test_ab[c])+'\n')
c+=1
return intrain,intest
def check_test_num(meta):
f=open(meta,'r')
line=f.readline()
c=0
while True:
line=f.readline()
if not line:break
c+=1
if c<13:
return True
else:
return False
def pre_load(infile):
f=open(infile,'r')
d={}
while True:
line=f.readline().strip()
if not line:break
ele=re.split(',',line)
d[ele[0]]=[ele[1],ele[2]]
return d
def scan_test_num(infile,disease):
f=open(infile,'r')
arr=[]
tn=0
line=f.readline().strip()
arr.append(line)
d=pre_load('allmeta.tsv')
oin=0
while True:
line=f.readline().strip()
if not line:break
ele=re.split(',',line)
if ele[1]=='test':
if ele[2]=='Unknown' and disease==d[ele[0]][1]:
if ele[0] in d:
ele[2]=d[ele[0]][0]
oin=1
#ele[2]='Unknown'
tn+=1
tem=','.join(ele)
arr.append(tem)
uid = uuid.uuid1().hex
ninfile='inmatrix_'+uid+'.csv'
o=open(ninfile,'w+')
for a in arr:
o.write(a+'\n')
o.close()
return ninfile,oin
'''
else:
return ''
'''
def preprocess(infile,train_mode,disease,outdir):
'''
usage="GDmicro_preprocess - Normalize all input data, merge your own test data with training data, and convert combined matrices to node feature format."
parser=argparse.ArgumentParser(prog="GDmicro_preprcess.py",description=usage)
parser.add_argument('-i','--input_train',dest='input_train',type=str,help="The dir of input training data.")
parser.add_argument('-b','--input_test',dest='input_test',type=str,help="The dir of input test data.")
parser.add_argument('-t','--train_mode',dest='train_mode',type=str,help="If set to 1, then will only normalize and convert all input data. This mode can only be used when input datasets are all training data. You don't need to provide the test data under this mode. (default: 0)")
parser.add_argument('-d','--disease',dest='dtype',type=str,help="The name of disease. (Note: the value should be the same as the one in your metadata file.)")
#parser.add_argument('-t','--data_type',dest='data_type',type=str,help="The type of input data. The value can be: meta (metadata), species (species matrix) or eggNOG (eggNOG matrix). If set to \"species\" or \"eggNOG\", you also need to set \"-m\" and \"-n\" to provide the metadata of training and test datasets..")
parser.add_argument('-o','--outdir',dest='outdir',type=str,help="Output directory of combined and normalized results. (Default: GDmicro_merge")
args=parser.parse_args()
'''
#intrain=args.input_train
#intest=args.input_test
scan_res,oin=scan_test_num(infile,disease)
if not scan_res=='':
infile=scan_res
#print(infile)
#exit()
intrain,intest=split_file(infile,disease,outdir)
train_mode=train_mode
dtype=disease
out=outdir+'/Preprocess_data'
'''
if not out:
out="GDmicro_merge"
'''
if not os.path.exists(out):
os.makedirs(out)
if not train_mode:
train_mode=0
else:
train_mode=int(train_mode)
intrain_meta=''
intest_meta=''
#intest_meta_nl=''
intrain_sp=''
intest_sp=''
intrain_eggnog=''
intest_eggnog=''
for filename in os.listdir(intrain):
if re.search('meta\.tsv',filename):
intrain_meta=intrain+'/'+filename
if re.search('sp_matrix',filename):
intrain_sp=intrain+'/'+filename
'''
if re.search('eggNOG_matrix',filename):
intrain_eggnog=intrain+'/'+filename
'''
if train_mode==0:
for filename in os.listdir(intest):
if re.search('meta\.tsv',filename):
intest_meta=intest+'/'+filename
if re.search('sp_matrix',filename):
intest_sp=intest+'/'+filename
'''
if re.search('eggNOG_matrix',filename):
intest_eggnog=intest+'/'+filename
'''
if train_mode==0:
check_arr=[intrain_meta,intest_meta,intrain_sp,intest_sp]
else:
check_arr=[intrain_meta,intrain_sp]
#
for i in check_arr:
if i=='':
print('Some required files are not provided. Please check!')
exit()
else:
print("Load files -> "+i)
#exit()
if train_mode==0:
print('Preprocess 1 - Merge metadata.')
trans_meta(intrain_meta,intest_meta,out+"/"+dtype+'_meta.tsv')
#exit()
if check_test_num(intest_meta):
merge_sp(intrain_sp,intest_sp,outdir+"/Split_dir/Test/"+dtype+'_merge_sp_raw.csv')
temerge=outdir+"/Split_dir/Test/"+dtype+'_merge_sp_raw.csv'
os.system('cp '+out+"/"+dtype+'_meta.tsv'+' '+outdir+"/Split_dir/Test/"+dtype+'_meta_merge.tsv')
temeta=outdir+"/Split_dir/Test/"+dtype+'_meta_merge.tsv'
print('Preprocess 2 - Normalize all abundance matrices.')
normalize_data(intrain_sp,'species',intrain_meta,dtype,out+"/"+dtype+'_train_sp_norm.csv')
if check_test_num(intest_meta):
normalize_data_small(intest_sp, 'species', intest_meta, dtype, out + "/" + dtype + '_test_sp_norm.csv',temerge,temeta)
#exit()
else:
normalize_data(intest_sp,'species',intest_meta,dtype,out+"/"+dtype+'_test_sp_norm.csv')
#normalize_data(intrain_eggnog,'eggNOG',intrain_meta,dtype,out+"/"+dtype+'_train_eggNOG_norm.csv')
#normalize_data(intest_eggnog,'eggNOG',intest_meta,dtype,out+"/"+dtype+'_test_eggNOG_norm.csv')
print('Preprocess 3 - Merge training and test datasets.')
merge_sp(intrain_sp,intest_sp,out+"/"+dtype+'_sp_merge_raw.csv')
merge_sp(out+"/"+dtype+'_train_sp_norm.csv',out+"/"+dtype+'_test_sp_norm.csv',out+"/"+dtype+'_sp_merge_norm.csv')
#merge_eggNOG(intrain_eggnog,intest_eggnog,out+"/"+dtype+'_eggNOG_merge_raw.csv')
#merge_eggNOG(out+"/"+dtype+'_train_eggNOG_norm.csv',out+"/"+dtype+'_test_eggNOG_norm.csv',out+"/"+dtype+'_eggNOG_merge_norm.csv')
print('Preprocess 4 - Convert combined matrices to node feature format.')
trans2node(out+"/"+dtype+'_sp_merge_norm.csv',out+"/"+dtype+'_meta.tsv',out+"/"+dtype+'_sp_merge_norm_node.csv')
trans2node(out+"/"+dtype+'_sp_merge_raw.csv',out+"/"+dtype+'_meta.tsv',out+"/"+dtype+'_sp_merge_raw_node.csv')
#trans2node(out+"/"+dtype+'_eggNOG_merge_norm.csv',out+"/"+dtype+'_meta.tsv',out+"/"+dtype+'_eggNOG_merge_norm_node.csv')
else:
print('Train mode - Preprocess 1 - Transform metadata.')
trans_meta_train(intrain_meta,out+"/"+dtype+'_meta.tsv')
print('Train mode - Preprocess 2 - Normalize all abundance matrices.')
os.system('cp '+intrain_sp+' '+out+"/"+dtype+'_train_sp_raw.csv')
normalize_data(intrain_sp,'species',intrain_meta,dtype,out+"/"+dtype+'_train_sp_norm.csv')
#os.system('cp '+intrain_eggnog+' '+out+"/"+dtype+'_train_eggNOG_raw.csv')
#normalize_data(intrain_eggnog,'eggNOG',intrain_meta,dtype,out+"/"+dtype+'_train_eggNOG_norm.csv')
print('Train mode - Preprocess 3 - Convert normalized matrices to node feature format.')
trans2node(out+"/"+dtype+'_train_sp_norm.csv',out+"/"+dtype+'_meta.tsv',out+"/"+dtype+'_sp_train_norm_node.csv')
trans2node(out+"/"+dtype+'_train_sp_raw.csv',out+"/"+dtype+'_meta.tsv',out+"/"+dtype+'_sp_train_raw_node.csv')
'''
if os.path.exists(intrain+'/pre_features'):
os.system('cp -rf '+intrain+'/pre_features '+out)
'''
#exit()
if not scan_res=='':
os.system('rm '+scan_res)
return out,oin
'''
if __name__=="__main__":
sys.exit(main())
'''
#preprocess('../Single_test_datasets/train_5studies_1test_single/GuptaA_2019_test_1.csv',0,'CRC','single_sample')