forked from Czh3/NGSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WGS.py
234 lines (163 loc) · 6.22 KB
/
WGS.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
#!/usr/bin/env python
# -*- encoding=utf8 -*-
import sys
#sys.path.append('/home/zhangc/bin/git/NGSTools')
import NGSTools
import argparse
import re
import os
from multiprocessing import Process, Manager
#argparse arguments
parser = argparse.ArgumentParser(description='A pipeline for WGS 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"
"sampleID\tsampleName\tfastq1Path\tfastq2Path",
required=True)
parser.add_argument('-o', '--outDir',
help='The pipeline output dir',
default='out')
parser.add_argument('-c', '--config',
help='the config file of NGSTools package.',
default='~/.NGSTools.cfg')
parser.add_argument('-a', '--analysis',
help='analysis of the pipeline to do.\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:picard_rmdup, remove PCR duplicates using picard\n'
' 4:mpileup, call SNP using samtools mpileup]\n',
default='1,2,3')
parser.add_argument('-b', '--qbase',
help="quality base of base calling: 33(default) or 64\n",
default='33')
parser.add_argument('--debug',
help='debug mode',
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]
QC = BWA = RMDUP = MPILEUP = False
if 1 in analy:
QC = True
if 2 in analy:
BWA = True
if 3 in analy:
RMDUP = True
if 4 in analy:
MPILEUP = True
if args.debug:
_run = False
else:
_run = True
#read config file
cfg = NGSTools.getConfig(os.path.abspath(args.config))
# sample list parse
def sampleListParser():
''' parse the sample list file.
return a dictionary:
{
sampleName1:
{
sample ID:
[
fastq1 file path,
fastq2 file path
]
},
sampleName2:
{
...
}
}
'''
sampleListDict = {}
for line in open(args.sampleList):
if line.startswith('#') or line == "\n":
continue
# "sampleID\tsampleName\tfastq1Path\tfastq2Path"
cols = line.strip().split()
# fastq exists
if not (os.path.exists(cols[2]) and os.path.exists(cols[3])):
os.exit("fastq file don't exists!")
if sampleListDict.has_key(cols[1]):
if sampleListDict[cols[1]].has_key(cols[0]):
sys.exit('duplication sample IDs are not allowed')
else:
sampleListDict[cols[1]][cols[0]] = [cols[2], cols[3]]
else:
sampleListDict[cols[1]] = {cols[0] : [cols[2], cols[3]]}
return sampleListDict
def processSampleFromLibarary(sampleID, sampleIDList, libraryBamFileList):
''''''
########################## 0. init #########################
#__init__(self, sampleName, outdir, fq1, fq2='', qualityBase='33', cfgfile='~/.NGSTools.cfg')
mySample = NGSTools.NGSTools(sampleID, args.outDir, fq1=sampleIDList[0], fq2=sampleIDList[1], qualityBase=args.qbase, cfgfile=os.path.abspath(args.config))
########################## 1. QC #########################
if QC:
mySample.cutadapter(run=_run)
mySample.QC_fastqc(run=_run)
########################## 2. Mapping #######################
if BWA:
finalBam = mySample.bwa(run=_run)
#finalBam = mySample.bowtie2(mode='--end-to-end', run=_run)
finalBam = mySample.samtools_sort(run=_run)
libraryBamFileList.append(finalBam)
def processSample(sampleName, sampleNameDict):
'''pipeline for sample '''
# process communication
mng = Manager()
libraryBamFileList = mng.list()
# This libraryBamFileList is a list contains seval bams from one sample.
# init mult-processer
record = []
for sampleID in sampleNameDict:
Processer = Process(name = sampleID, target = processSampleFromLibarary, args = (sampleID, sampleNameDict[sampleID], libraryBamFileList, ))
Processer.start()
record.append(Processer)
# wait for processer
for proc in record:
proc.join()
###################### 2.1 post mapping ####################
bamFileDir = os.path.dirname(libraryBamFileList[0])
mergedBamFilePath = os.path.join(bamFileDir, "%s_merged.bam" % sampleName)
finalBamFilePath = mergedBamFilePath
# merge the bam from each lane to one final bam
command = NGSTools.picard_merge(libraryBamFileList, mergedBamFilePath, cfg)
NGSTools.writeCommands(command, bamFileDir+'/picard_mergebam_'+sampleName+'.sh', run=_run)
###################### 2.2. filter bam ######################
#command = 'samtools view -Sb -h -f 2 -q 10 %s > %s ' % (mergedBamFilePath, finalBamFilePath)
#command = 'samtools view -Sb -h -q 10 %s > %s ' % (mergedBamFilePath, finalBamFilePath)
#NGSTools.writeCommands(command, bamFileDir+'/filterBam_'+sampleName+'.sh', run=_run)
###################### 3. romove duplicates ##################
if RMDUP:
command = NGSTools.picard_rmdup(mergedBamFilePath, True, cfg)
NGSTools.writeCommands(command, bamFileDir+'/picard_rmdup_'+sampleName+'.sh', run=_run)
finalBamFilePath = re.sub(r'.bam$', '.rmdup.bam', finalBamFilePath)
####################### 4. call SNP ######################
if MPILEUP:
SNP_out = os.path.join(args.outDir, "SNP", sampleName)
NGSTools._mkdir(SNP_out)
command, rawVcf = NGSTools.bcftools_call(finalBamFilePath, cfg, outdir=SNP_out, sampleName=sampleName)
NGSTools.writeCommands(command, SNP_out+'/bcftools_call_'+sampleName+'.sh', run=_run)
outVcf = rawVcf.replace("vcf$", "flt.vcf")
command = NGSTools.bcftools_filter(rawVcf, outVcf, cfg)
NGSTools.writeCommands(command, SNP_out+'/bcftools_filter_'+sampleName+'.sh', run=_run)
########### main function
sampleListDict = sampleListParser()
# init mult-processer
records = []
for sampleName in sampleListDict:
P = Process(name = sampleName, target = processSample, args = (sampleName, sampleListDict[sampleName], ))
P.start()
records.append(P)
for rec in records:
rec.join()