forked from Czh3/NGSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNA_pipeline.py
440 lines (320 loc) · 11.1 KB
/
RNA_pipeline.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
#-*- encoding=utf8 -*-
import sys
sys.path.append('/home/zhangc/bin/git/TEST')
import NGSTools
import argparse
import os
from multiprocessing import Process, Manager
#argparse arguments
parser = argparse.ArgumentParser(description='A pipeline of RNA_seq data analysis. <[email protected]>',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-s', '--sampleList',
help="sample list for RNA samples information.\n"
" A file each line contains:\n"
"sampleName\tsampleCondition\tfastq1Path\tfastq2Path",
required=True)
parser.add_argument('-o', '--outDir',
help='The pipeline output dir',
default='out')
parser.add_argument('-d', '--dataType',
help='fastq data type:\n'
'raw data or clean data.[clean]\n'
' if (clean data): not run cutadapter',
choices=['raw', 'clean'],
default='clean')
parser.add_argument('-l', '--libraryType',
help='library type for tophat2 mapper,default none strand-specific[fr-unstranded]\n',
choices=['fr-unstranded', 'fr-firststrand', 'fr-secondstrand'],
default='fr-unstranded')
parser.add_argument('-a', '--analysis',
help='analysis of the pipeline to do.[1,2]\n'
'Here is some software to choose to analy\n'
'[1:QC, quality control\n'
' 2:Mapping, align the reads to reference genome\n'
' 3:Cufflinks, assemble with cufflinkes\n'
' 4:DESeq, call DEGs(different expression genes) using DESeq package\n'
' 5:DEXSeq, call DEUs(different exon usages) using DEXSeq package\n'
' 6:GATK, call SNP on mRNA using GATK.\n'
' 7:GFold, call DEGs without biological replicates using GFold]',
default='1,2')
parser.add_argument('-c', '--config',
help='the config file of NGSTools package.',
default='~/.NGSTools.cfg')
parser.add_argument('--debug',
help='debug mode[False]',
choices=['False', 'True'],
default='False')
args = parser.parse_args()
# init
__VERSION__ = 'V0.1'
if not os.path.isfile(args.sampleList):
sys.exit('ArgumentError:\tSampleList must be a file.')
args.outDir = os.path.abspath(args.outDir)
if not os.path.exists(args.outDir):
os.mkdir(args.outDir)
analy = args.analysis.split(',')
analy = [int(i) for i in analy]
if max(analy) > 2 and 2 not in analy:
sys.exit('analysis error:\tMust mapping the reads in advance')
QC = Mapping = Cufflinks = DESeq = DEXSeq = GATK = GFold = False
if 1 in analy:
QC = True
if 2 in analy:
Mapping = True
if 3 in analy:
Cufflinks = True
if 4 in analy:
DESeq = True
if 5 in analy:
DEXSeq = True
if 6 in analy:
GATK = True
if 7 in analy:
GFold = True
global _run
_run = ''
if args.debug == 'False':
_run = True
else:
_run = False
##
cfg = NGSTools.getConfig(os.path.abspath(args.config))
def processSample(line, condition, transcripts, countsFiles, finalBam, expressCXB):
cols = line.strip().split('\t')
if len(cols) == 3:
# single end library
fq2 = '-'
else:
# paired end
fq2 = cols[3]
sample = {
'name' : cols[0],
'condition' : cols[1],
'fq1' : cols[2],
'fq2' : fq2,
'bam' : ''
}
########################## 0. init #########################
#__init__(self, sampleName, outdir, fq1, fq2='', quanlityBase='32', cfgfile='~/.NGSTools.cfg'):
mySample = NGSTools.NGSTools(sample['name'], args.outDir, sample['fq1'], sample['fq2'], libType=args.libraryType, cfgfile=os.path.abspath(args.config))
if QC:
#################### 1. Quality Control ####################
###### 1.1 cut adapter ######
if args.dataType == 'raw':
#mySample.cutadapter(adapter5='', adapter3='AATGATACGGCGACCACCGAGATCT', run = _run)
mySample.cutadapter(run = _run)
### Nextera Kit
#mySample.cutadapter(adapter5='CTGTCTCTTATACAC', adapter3='CTGTCTCTTATACAC',run = _run)
#mySample.rm_lowQual(run = _run)
else:
pass
##### 1.2 fastqc #####
mySample.QC_fastqc(run = _run)
if Mapping:
######################## 2. Mapping ########################
sample['bam'] = mySample.tophat2(run = _run)
if condition.has_key(sample['condition']):
#condition[sample['condition']][sample['name']] = sample['bam']
condition[sample['condition']] += ","+sample['bam']
else:
#condition[sample['condition']] = {sample['name'] : sample['bam']}
condition[sample['condition']] = sample['bam']
if GFold:
# GFold count
mySample.gfoldCount(run = _run)
if DESeq:
# DESeq2
count = mySample.HTSeq_count(run = _run)
countsFiles[count] = sample['condition'] + '|' + sample['name']
if GATK:
# remove duplicates
mySample.rmdup(run = _run)
# picard reorder
mySample.picard_reorder(run = _run)
# splitN
mySample.splitN(run = _run)
# realign
realnBam = mySample.realn(run = _run)
# recal need known SNP site
# recal
#recalBam = mySample.recal(run = _run)
finalBam[realnBam] = sample['condition']
# samtools call SNP/InDel
mySample.samtools_call(run = _run)
mySample.samtools_filter(run = _run)
######################## DEGs calling preparation ########################
if Cufflinks:
##### 3. cufflinks #####
cuffdir = os.path.join(args.outDir, 'cufflinks')
if not os.path.exists(cuffdir):
os.mkdir(cuffdir)
# cufflinks #
command = 'cufflinks --library-type %s -p 4 -g %s -o %s %s' % (args.libraryType, cfg.gtf, os.path.join(cuffdir, sample['condition']+'_'+sample['name']), sample['bam'])
NGSTools.writeCommands(command, cuffdir+'/cufflinks_%s.sh' % sample['name'], _run)
transcripts.append(os.path.join(cuffdir, sample['condition']+'_'+sample['name'], 'transcripts.gtf'))
# cuffquant #
#command = 'cuffquant %s -o %s %s' % (cfg.gtf, os.path.join(cuffdir, 'cuffquant' ,sample['condition']+'_'+sample['name'], sample['bam']))
#NGSTools.writeCommands(command, cuffdir+'/cuffquant_%s.sh' % sample['name'], _run)
#cxbFile = os.path.join(cuffdir, 'cuffquant' ,sample['condition']+'_'+sample['name'], 'abundances.cxb')
#if expressCXB.has_key(sample['condition']):
# expressCXB[sample['condition']] += ','+cxbFile
#else:
# expressCXB[sample['condition']] = cxbFile
def catFQ(files, outfile):
# cat several file to a single file
if files[0].endswith(".gz"):
cmd = 'zcat '
else:
cmd = 'cat '
for i in files:
cmd += ' ' + i
if files[0].endswith(".gz"):
cmd += ' | gzip -c > ' + outfile
else:
cmd += ' > ' + outfile
if _run == True:
os.system(cmd)
def mergeFQ():
'''merge the sample' fastq file which from different lane '''
samplesMerge = []
samples = []
samplesDict = {}
output = {}
for line in open(args.sampleList):
if line.startswith('#') or line == "\n":
continue
cols = line.strip().split()
if cols[0] in samples:
# duplicated sample name need to merge fastq
samplesMerge.append(cols[0])
else:
output[cols[0]] = cols
samples.append(cols[0])
if samplesDict.has_key(cols[0]):
samplesDict[cols[0]].append(cols[2:])
else:
samplesDict[cols[0]] = [cols[2:]]
# do not need to merge
if len(samples) == len(set(samples)):
return(args.sampleList)
# need to merge
else:
records = []
for sample in samplesMerge:
sampleFQ = []
sampleFQ_PE = []
for fq in samplesDict[sample]:
if len(fq) == 1: # single end
sampleFQ.append(fq[0])
else: # paired end
sampleFQ.append(fq[0])
sampleFQ_PE.append(fq[1])
# fq1
outfile = os.path.dirname(sampleFQ[0]) + '/merged_' + os.path.basename(sampleFQ[0])
output[sample][2] = outfile
P = Process(name = sample, target = catFQ, args = (sampleFQ, outfile,))
P.start()
records.append(P)
# fq2
if sampleFQ_PE != []:
outfile = os.path.dirname(sampleFQ_PE[0]) + '/merged_' + os.path.basename(sampleFQ_PE[0])
output[sample][3] = outfile
P = Process(name = sample+'r2', target = catFQ, args = (sampleFQ_PE, outfile,))
P.start()
records.append(P)
for rec in records:
rec.join()
out = open(args.sampleList + '.merged', 'w')
for i in output:
out.write("\t".join(output[i]) + '\n')
return(args.sampleList + '.merged')
# process communication
mng = Manager()
condition = mng.dict()
transcripts = mng.list()
countsFiles = mng.dict()
finalBam = mng.dict()
expressCXB = mng.dict()
# multi-process
record = []
for line in open(mergeFQ()):
if line.startswith('#') or line == "\n":
continue
sampleName = line.split('\t')[0]
P = Process(name=sampleName, target=processSample, args=(line, condition, transcripts, countsFiles, finalBam, expressCXB))
P.start()
record.append(P)
for P in record:
P.join()
######################## DEGs calling ########################
if Cufflinks:
# cuffmerge #
cuffdir = os.path.join(args.outDir, 'cufflinks')
with open(cuffdir+'/assemblies.txt', 'w') as writer:
writer.write('\n'.join(transcripts))
command = 'cuffmerge -o %s -g %s -s %s -p 10 %s' % (cuffdir+'/merged_asm', cfg.gtf, cfg.genome, cuffdir+'/assemblies.txt')
NGSTools.writeCommands(command, cuffdir+'/cuffmerge.sh', _run)
# cuffnorm #
cuffnorm_dir = os.path.join(cuffdir, 'cuffnorm')
try:
os.mkdir(cuffnorm_dir)
except:
pass
cond = ','.join(condition.keys())
bams = ' '.join(condition.values())
command = 'mkdir cuffnorm\ncuffnorm --library-type %s -o %s -L %s %s %s' % (args.libraryType, cuffnorm_dir, cond, cfg.gtf, bams)
NGSTools.writeCommands(command, cuffdir+'/cuffnorm.sh', _run)
# cuffdiff #
if len(condition) != 2:
print 'WARNING: condition'
command = 'cuffdiff -o %s -b %s -p 10 -L %s -u %s %s %s' % (cuffdir+'/cuffdiff', cfg.genome, condition.keys()[0]+','+condition.keys()[1], cuffdir+'/merged_asm/merged.gtf', condition.values()[0], condition.values()[1])
NGSTools.writeCommands(command, cuffdir+'/cuffdiff.sh', _run)
if DESeq:
# deseq2 #
deseqDir = os.path.join(args.outDir, 'DESeq')
try:
os.mkdir(deseqDir)
except:
pass
Rcommand = NGSTools.deseq(countsFiles, deseqDir)
with open(deseqDir+'/deseq.R', 'w') as shell:
shell.write(Rcommand)
if _run:
os.system('R %s' % Rcommand)
if GFold:
# GFold #
gfoldDir = os.path.join(args.outDir, 'GFold')
command = '\\\n\t'.join(['%s diff -s1 $1 -s2 $2 ' % cfg.gfold,
'-suf .gfoldCount ',
'-o $1"vs"$2'])
with open(gfoldDir+'/gfold_diff.sh', 'w') as shell:
shell.write(command)
######################## DEU calling ########################
if DEXSeq:
# DEXSeq #
dexseqDir = os.path.join(args.outDir, 'DEXSeq')
try:
os.mkdir(dexseqDir)
except:
pass
######################## SNP calling ########################
if GATK:
# GATK HC (in RNA-seq mode) call variations
outdir = os.path.join(args.outDir, 'variation')
NGSTools._mkdir(outdir)
for condition in set(finalBam.values()):
bams = []
for bam in finalBam.keys():
if finalBam[bam] == condition:
bams.append(bam)
out = os.path.join(outdir, condition)
NGSTools._mkdir(out)
mergedBam = '%s/%s.bam' % (out, condition)
script = NGSTools.picard_merge(bams, mergedBam, cfg)
NGSTools.writeCommands(script, '%s/picard_merge_%s.sh' % (out, condition), False)
script = NGSTools.GATK_HC(mergedBam, cfg, out, condition)
NGSTools.writeCommands(script, '%s/gatk_HC_%s.sh' % (out, condition), False)
rawVcf = '%s/%s.raw.snps.indels.vcf' % (out, condition)
fltVcf = '%s/%s.flt.snps.indels.vcf' % (out, condition)
script = NGSTools.GATK_filter(mergedBam, rawVcf, fltVcf, cfg)
NGSTools.writeCommands(script, '%s/gatk_filter_%s.sh' % (out, condition), False)