-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.py
380 lines (312 loc) · 12.9 KB
/
script.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
# -*- coding: utf-8 -*-
import os
import sys
def get_file(inputdir1,filename1):
file1_stat = os.path.join(inputdir1,str(filename1 + '.annot.stat1'))
file1_junc = os.path.join(inputdir1,str(filename1 + '.annot.junction'))
file1_anno = os.path.join(inputdir1,str(filename1 + '.annot.multiAnno'))
file1_anno_file = os.path.join(inputdir1,str(filename1 + '.annot.multiAnno.filter'))
trans_out_exp1 = os.path.join(inputdir1,str('merge\\' + filename1 + '.transcript.exp'))
mkdir(os.path.join(inputdir1,str('merge')))
return file1_stat,file1_junc,file1_anno,file1_anno_file,trans_out_exp1
def mkdir(dir):
if not os.path.exists(dir):
os.mkdir(dir)
def get_stat_info(file1_stat):
dic_exp_T2 = {}
dic_tpm_T2 = {}
for row in open(file1_stat,encoding = 'utf-8'):
line = row[:-1].split('\t')
dic_exp_T2[line[0]] = line[-2]
dic_tpm_T2[line[0]] = line[-1]
return dic_exp_T2,dic_tpm_T2
def get_junc_info(file1_junc):
dic_T2_con = {}
for row in open(file1_junc):
line = row.split('\t')
if line[0] in dic_T2_con:
dic_T2_con[line[0]].append(line[10])
else:
dic_T2_con[line[0]] = [line[10]]
return dic_T2_con
def get_anno_info(file1_anno,file1_anno_file,dic_T2_con):
list_T2_trans = []
fo = open(file1_anno_file,'w')
for row in open(file1_anno):
line = row[:-1].split('\t')
if '|' in line[3]:
continue
if ':' in line[1]:
continue
if line[2] == 'NA':
if line[0] in dic_T2_con:
if set(dic_T2_con[line[0]]) == {'canonical'}:
fo.write(row)
list_T2_trans.append(line[0])
continue
else:
continue
else:
continue
fo.write(row)
list_T2_trans.append(line[0])
fo.close()
return list_T2_trans
def write_new_exp(file2_exp_file,out_exp1,list_T2_trans,dic_exp_T2,dic_tpm_T2):
fo = open(out_exp1,'w')
for row in open(file2_exp_file):
line = row[:-1].split('\t')
if line[0] == 'Chrom':
fo.write(row)
continue
if line[4] == '':
fo.write(row)
continue
list_new = []
exp = 0
tpm = 0
for i in line[4].split(','):
if i in list_T2_trans:
#print(i)
#print(dic_exp_T2)
exp = exp + int(dic_exp_T2[i])
tpm = tpm + float(dic_tpm_T2[i])
list_new.append(i)
fo.write('\t'.join(line[0:3]) + '\t' + str(len(list_new)) + '\t' + ','.join(list_new) + '\t' + str(tpm) + '\t' + str(exp) + '\n')
fo.close()
def runtime_file(inputdir2,filename2,file2_exp_file):
file2_stat,file2_junc,file2_anno,file2_anno_file,trans_out_exp2 = get_file(inputdir2,filename2)
dic_exp_T2,dic_tpm_T2 = get_stat_info(file2_stat)
dic_T2_con = get_junc_info(file2_junc)
list_T2_trans = get_anno_info(file2_anno,file2_anno_file,dic_T2_con)
write_new_exp(file2_exp_file,trans_out_exp2,list_T2_trans,dic_exp_T2,dic_tpm_T2)
return trans_out_exp2
def get_info(trans_out_exp2,dic_gene,list_trans):
dic_1_t = {}
for row in open(trans_out_exp2):
if row.startswith('Chrom'):
continue
line = row[:-1].split('\t')
if line[0] not in list_trans:
list_trans.append(line[2])
dic_1_t[line[2]] = int(line[6])
dic_gene[line[2]] = line[0] + '\t' + line[1]
return dic_gene,list_trans,dic_1_t
def write_merge(trans_out_exp1,trans_out_exp2,trans_outfile,filename1,filename2):
dic_gene = {}
list_trans = []
dic_gene,list_trans,dic_1_t = get_info(trans_out_exp1,dic_gene,list_trans)
dic_gene,list_trans,dic_2_t = get_info(trans_out_exp2,dic_gene,list_trans)
fo = open(trans_outfile,'w')
fo.write('Chrom\tGene\tTranscript\t' + filename1 + '\t' + filename2 + '\n')
for i in sorted(list(set(list_trans))):
fo.write(dic_gene[i] + '\t' + i + '\t')
t1 = 0
t2 = 0
if i in dic_1_t:
t1 = dic_1_t[i]
if i in dic_2_t:
t2 = dic_2_t[i]
fo.write(str(t1) + '\t' + str(t2) + '\n')
fo.close()
def write_merge_known(trans_outfile):
outfile_known = trans_outfile + '.known'
fo = open(outfile_known,'w')
for row in open(trans_outfile):
if row.startswith('Chrom'):
fo.write(row)
continue
if ':' in row:
continue
if 'Genic' in row:
continue
if '\t0\t0' in row:
continue
if 'NNC' in row:
continue
if 'NIC' in row:
continue
fo.write(row)
fo.close()
return outfile_known
def runtime_gene(gtf_file,trans_outfile_known,filename1,filename2,gene_outfile):
dic_gtf_site,gtf_site,dic_gene_trans = get_gtf_info(gtf_file)
list_site = get_trans_merge_info(trans_outfile_known,dic_gtf_site)
try:
write_gene_merge(gene_outfile,trans_outfile_known,filename1,filename2)
print('Gene合并文件成功生成。')
except:
print('Gene合并文件未生成。')
def get_gtf_info(gtf_file):
dic_gtf_site = {}
gtf_site = []
dic_gene_trans = {}
for row in open(gtf_file):
line = row[:-1].split('\t')
if line[0] == 'Chr':
continue
if line[4] not in dic_gtf_site:
dic_gtf_site[line[4]] = [int(line[1])]
dic_gtf_site[line[4]].append(int(line[2]))
else:
dic_gtf_site[line[4]].append(int(line[1]))
dic_gtf_site[line[4]].append(int(line[2]))
gene = '-'.join(line[4].split('-')[:-1])
if gene not in dic_gene_trans:
dic_gene_trans[gene] = [line[4]]
else:
dic_gene_trans[gene].append(line[4])
for key in dic_gtf_site:
dic_gtf_site[key] = sorted(dic_gtf_site[key])[1:-1]
gtf_site.append(dic_gtf_site[key])
return dic_gtf_site,gtf_site,dic_gene_trans
def get_trans_merge_info(trans_outfile_known,dic_gtf_site):
list_site = {}
for row in open(trans_outfile_known):
line = row[:-1].split('\t')
if 'Chrom' in row:
continue
if line[1] not in list_site:
try:
list_site[line[1]] = [dic_gtf_site[line[2]][1:-1]]
except:
continue
else:
if dic_gtf_site[line[2]][1:-1] in list_site[line[1]]:
list_site[line[1]].append(dic_gtf_site[line[2]][1:-1])
else:
list_site[line[1]].append(dic_gtf_site[line[2]][1:-1])
return list_site
def write_gene_other(gene_outfile):
file1 = gene_outfile + '.nozero'
file2 = gene_outfile + '.count2'
fo = open(file1,'w')
for row in open(gene_outfile):
if '\t0' in row:
continue
fo.write(row)
fo.close()
fo = open(file2,'w')
for row in open(gene_outfile):
line = row[:-1].split('\t')
if line[0] == 'Gene':
fo.write(row)
continue
if ((int(line[1]) >= 2)&(int(line[2]) >= 2)):
fo.write(row)
fo.close()
return file1,file2
def write_gene_merge(gene_outfile,trans_outfile_known,filename1,filename2):
list_gene = []
dic_gene_1 = {}
dic_gene_2 = {}
fo = open(gene_outfile,'w')
for row in open(trans_outfile_known):
line = row[:-1].split('\t')
if line[0] == 'Chrom':
continue
if line[1] not in list_gene:
list_gene.append(line[1])
dic_gene_1[line[1]] = int(line[3])
dic_gene_2[line[1]] = int(line[4])
else:
dic_gene_1[line[1]] = dic_gene_1[line[1]] + int(line[3])
dic_gene_2[line[1]] = dic_gene_2[line[1]] + int(line[4])
fo.write('Gene\t' + filename1 + '\t' + filename2 + '\n')
for i in list_gene:
fo.write(i + '\t' + str(dic_gene_1[i]) + '\t' + str(dic_gene_2[i]) + '\n')
fo.close()
def get_info_from_config(config_file):
for row in open(config_file,encoding = 'utf-8'):
if row.startswith('#'):
continue
line = row.strip().split('\t')
if line[0] == 'inputdir1':
inputdir1 = line[1]
elif line[0] == 'filename1':
filename1 = line[1]
elif line[0] == 'inputexp1':
file1_exp_file = line[1]
elif line[0] == 'inputdir2':
inputdir2 = line[1]
elif line[0] == 'filename2':
filename2 = line[1]
elif line[0] == 'inputexp2':
file2_exp_file = line[1]
elif line[0] == 'exon_gtf':
gtf_file = line[1]
elif line[0] == 'out_dir':
trans_out_dir = line[1]
elif line[0] == 'out_sample':
out_sample = line[1]
elif line[0] == 'diff_script_file':
diff_script_file = line[1]
elif line[0] == 'diff':
DEG_runtime = line[1]
print(trans_out_dir)
if os.path.exists(trans_out_dir):
pass
else:
os.mkdir(trans_out_dir)
diff_dir = os.path.join(trans_out_dir,'diff')
trans_outfile_pre = os.path.join(trans_out_dir,out_sample)
trans_outfile = trans_outfile_pre + '.trans.exp'
gene_outfile = trans_outfile_pre + '.gene.exp.known'
return inputdir1,filename1,file1_exp_file,inputdir2,filename2,file2_exp_file,gtf_file,out_sample,diff_script_file,diff_dir,trans_outfile,gene_outfile,DEG_runtime
def main():
config_file = sys.argv[1]
inputdir1,filename1,file1_exp_file,inputdir2,filename2,file2_exp_file,gtf_file,out_sample,diff_script_file,diff_dir,trans_outfile,gene_outfile,DEG_runtime = get_info_from_config(config_file)
#print(inputdir1,filename1,file1_exp_file,inputdir2,filename2,file2_exp_file,gtf_file,out_sample,diff_script_file,diff_dir,trans_outfile,gene_outfile,DEG_runtime)
trans_out_exp2 = runtime_file(inputdir2,filename2,file2_exp_file)
trans_out_exp1 = runtime_file(inputdir1,filename1,file1_exp_file)
#try:
write_merge(trans_out_exp1,trans_out_exp2,trans_outfile,filename1,filename2)
print('transcript合并文件成功生成。')
#except:
# print('transcript合并文件未生成。')
try:
trans_outfile_known = write_merge_known(trans_outfile)
print('transcript合并过滤文件成功生成。')
if DEG_runtime:
trans_outfile1_diff = os.path.join(diff_dir, str(out_sample + '.trans.exp.known'))
mkdir(diff_dir)
mkdir(trans_outfile1_diff)
try:
print('transcript合并过滤文件差异分析开始运行.')
diff(trans_outfile_known,trans_outfile1_diff,diff_script_file)
except:
print('transcript合并过滤文件差异分析未运行.')
except:
print('transcript合并过滤文件未生成。')
runtime_gene(gtf_file,trans_outfile_known,filename1,filename2,gene_outfile)
try :
gene_out_nozero,gene_out_count2 = write_gene_other(gene_outfile)
print('Gene合并过滤文件(去0、count>=2)成功生成。')
if DEG_runtime:
gene_outfile1_diff = os.path.join(diff_dir, str(out_sample + '.gene.exp.known.nozero'))
gene_outfile2_diff = os.path.join(diff_dir, str(out_sample + '.gene.exp.known.count2'))
mkdir(diff_dir)
mkdir(gene_outfile1_diff)
mkdir(gene_outfile2_diff)
try:
print('Gene合并过滤文件(去0)差异分析开始运行.')
diff(gene_out_nozero,gene_outfile1_diff,diff_script_file)
except:
print('Gene合并过滤文件(去0)差异分析未运行.')
try:
print('Gene合并过滤文件(count>=2)差异分析开始运行.')
diff(gene_out_count2,gene_outfile2_diff,diff_script_file)
except:
print('Gene合并过滤文件(count>=2)差异分析未运行.')
except:
print('Gene合并过滤文件(去0、count>=2)未生成。')
def diff(inputfile,outfile,diff_script_file):
if 'trans' in inputfile:
diff1 = diff_script_file.replace('.r','_trans.r')
cmd = 'Rscript "' + diff1 + '" "' + inputfile + '" "' + outfile + '"'
else:
cmd = 'Rscript "' + diff_script_file + '" "' + inputfile + '" "' + outfile + '"'
os.system(cmd)
if __name__ == '__main__' :
main()
print ("Finish!")