-
Notifications
You must be signed in to change notification settings - Fork 0
/
variantCalling0.py
272 lines (229 loc) · 9.15 KB
/
variantCalling0.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
# import python modules
import re, glob, sys, os, subprocess, getopt, time
def mkdir(*folders):
for folder in folders:
subprocessRun('Make folder',folder ,'mkdir -p {0}'.format(folder))
def cp(file, folder):
subprocessRun('Copy files',file +" > "+ folder,'cp {0} {1}'.format(file,folder))
def filePath2ID(file):
searchObj = re.search('.*\/([^\n\t\r\f]+)_1\..*',file)
ID = searchObj.group(1)
return(ID)
def subprocessRun(title,name,cmd):
title1 = title + " start"
print(time.asctime(time.localtime(time.time()))+" "+title1.center(30,' ').center(70,'*'))
print(time.asctime(time.localtime(time.time()))+" "+name.center(30,' ').center(70,'*'))
sys.stdout.flush()
p = subprocess.Popen([cmd],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
print("stdout".center(20,' ').center(40,'='))
sys.stdout.flush()
for i in p.stdout.readlines():
print(str(i).strip('\n'))
sys.stdout.flush()
print("stderr".center(20,' ').center(40,'='))
sys.stdout.flush()
for i in p.stderr.readlines():
print(str(i).strip('\n'))
sys.stdout.flush()
title2 = title + " end"
print(time.asctime(time.localtime(time.time()))+" "+title2.center(30,' ').center(70,'*'))
print(time.asctime(time.localtime(time.time()))+" "+name.center(30,' ').center(70,'*'))
print("\n")
sys.stdout.flush()
def fastp(in1,out1):
in2 = re.sub('_1\.fastq','_2\.fastq',in1)
out2 = re.sub('_1\.fastq','_2\.fastq',out1)
html = re.sub('\.[^\/\n\t\r\f]+','.html',out1)
json = re.sub('\.[^\/\n\t\r\f]+','.json',out1)
fastpPath = "/home/suofang/Software/fastp-0.20.0/fastp"
cmd = "{0} --length_required 70 \
--in1 {1} \
--in2 {2} \
--out1 {3} \
--out2 {4} \
--html {5} \
--json {6}".format(fastpPath,in1,in2,out1,out2,html,json)
subprocessRun("Fastp",in1,cmd)
def BWA_MEM(ID,ref,in1,out):
in2 = re.sub('_1\.fastq','_2\.fastq',in1)
STR = re.sub('\.[^\/\n\t\r\f]+','',out)
BWA = "/home/suofang/Software/bwa-0.7.17/bwa"
cmd = "{0} mem \
-R \'@RG\\tID:{1}\\tLB:lib1\\tPL:illumina\\tSM:{1}\\tPU:unit1\' \
{2} \
{3} \
{4} \
1> {5} \
2> {6}".format(BWA,ID,ref,in1,in2,out,STR)
subprocessRun("BWA MEM",in1,cmd)
def samtoolsSort(inputFile,outputFile):
samtools = "/home/suofang/Software/samtools-1.9/samtools"
cmd = "{0} sort -O bam \
-o {1} \
{2}".format(samtools,outputFile,inputFile)
subprocessRun("samtools sort",inputFile,cmd)
def markduplicate(inputFile,outputFile):
mark_dup = re.sub('\.[^\/\n\t\r\f]+','\.marked_dup_metrics.txt',outputFile)
Picard = "java -jar /home/suofang/Software/picard_2.18.15.jar"
cmd = "{0} MarkDuplicates I={1} \
O={2} \
M={3} \
CREATE_INDEX=true".format(Picard,inputFile,outputFile,mark_dup)
subprocessRun("markduplicate",inputFile,cmd)
def bcftoolsCall(inputFile,outputFile,ref):
bcftools = "/home/suofang/Software/bcftools-1.9/bcftools"
cmd = "{0} mpileup -a FORMAT/DP4 -Ou -f {1} {2} |\
{0} call -f GQ -mv -Ov -o {3}".format(bcftools,ref,inputFile,outputFile)
subprocessRun("bcftools Call",inputFile,cmd)
def HaplotypeCaller(inputFile,outputFile,ref):
GATK = "java -jar /home/suofang/Software/gatk-4.1.4.1/gatk-package-4.1.4.1-local.jar"
cmd = "{0} HaplotypeCaller \
-R {1} \
-I {2} \
-O {3} \
--dont-use-soft-clipped-bases true \
-A BaseQuality \
-A FisherStrand \
-A MappingQuality \
-A QualByDepth \
-A StrandBiasBySample \
-A StrandOddsRatio".format(GATK,ref,inputFile,outputFile)
subprocessRun("HaplotypeCaller",inputFile,cmd)
def HaplotypeCaller_GVCF(inputFile,outputFile,ref):
GATK = "java -jar /home/suofang/Software/gatk-4.1.4.1/gatk-package-4.1.4.1-local.jar"
cmd = "{0} HaplotypeCaller \
-R {1} \
-I {2} \
-O {3} \
--dont-use-soft-clipped-bases true \
-ERC GVCF \
-A BaseQuality \
-A FisherStrand \
-A MappingQuality \
-A QualByDepth \
-A StrandBiasBySample \
-A StrandOddsRatio".format(GATK,ref,inputFile,outputFile)
subprocessRun("HaplotypeCaller GVCF",inputFile,cmd)
def Deepvariant(inputFile,outputFolder,reference):
file = re.sub('/.*/',"",inputFile)
inputFolder = re.sub(file,"",inputFile)
cp(reference,inputFolder)
cp(reference+".fai",inputFolder)
ref = re.sub("/.*/",'',reference)
ID = re.sub("\..*","",file)
BIN_VERSION = "0.10.0"
cmd = 'docker run \
-v "{0}":"/input" \
-v "{1}:/output" \
google/deepvariant:"{2}" \
/opt/deepvariant/bin/run_deepvariant \
--model_type=WGS \
--ref=/input/{3} \
--reads=/input/{4} \
--output_vcf=/output/{5}.bwa.Deepvariant.vcf \
--output_gvcf=/output/{5}.bwa.Deepvariant.g.vcf'.format(inputFolder,outputFolder,BIN_VERSION,ref,file,ID)
subprocessRun("Deepvariant",inputFile,cmd)
def variantCalling(project,reference,annotation,rawData,suffix,callers):
print("args")
print("#"*70)
print("Project: " + project)
print("Reference: " + reference)
print("Annotation: " + annotation)
print("rawData: " + rawData)
print("suffix: " + suffix)
print("\n")
samples = glob.glob('{0}/*{1}'.format(rawData,suffix))
Fastp = "/home/suofang/Software/fastp-0.20.0/fastp"
BWA = "/home/suofang/Software/bwa-0.7.17/bwa"
samtools = "/home/suofang/Software/samtools-1.9/samtools"
bcftools = "/home/suofang/Software/bcftools-1.9/bcftools"
Picard = "java -jar /home/suofang/Software/picard_2.18.15.jar"
GATK = "java -jar /home/suofang/Software/gatk-4.1.4.1/gatk-package-4.1.4.1-local.jar"
print("Software used in this pipeline")
print("#"*70)
print(Fastp)
print(BWA)
print(samtools)
print(bcftools)
print(Picard)
print(GATK)
print("\n\n")
print("Procedure")
print("#"*70)
sys.stdout.flush()
for i in samples:
print(i)
sys.stdout.flush()
# Get the ID
inputFolder = rawData
inputFile = i
for j in callers:
if j == "Samtools":
# bcftools
outputFolder = "{0}/1_Files/4_Variants_Samtools".format(project)
mkdir(outputFolder)
outputFile = re.sub('\.[^\/\n\t\r\f]+','.bwa.Samtools.vcf',inputFile)
outputFile = re.sub(inputFolder,outputFolder,outputFile)
bcftoolsCall(inputFile,outputFile,reference)
elif j == "GATK":
#GATK
outputFolder = "{0}/1_Files/4_Variants_GATK".format(project)
mkdir(outputFolder)
outputFile = re.sub('\.[^\/\n\t\r\f]+','.bwa.GATK.vcf',inputFile)
outputFile = re.sub(inputFolder,outputFolder,outputFile)
HaplotypeCaller(inputFile,outputFile,reference)
elif j == "GATK_GVCF":
#GATK_GVCF
outputFolder = "{0}/1_Files/4_Variants_GATK_GVCF".format(project)
mkdir(outputFolder)
outputFile = re.sub('\.[^\/\n\t\r\f]+','.bwa.GATK.g.vcf',inputFile)
outputFile = re.sub(inputFolder,outputFolder,outputFile)
HaplotypeCaller_GVCF(inputFile,outputFile,reference)
elif j == "Deepvariant":
#Deepvariant
outputFolder = "{0}/1_Files/4_Variants_Deepvariant".format(project)
mkdir(outputFolder)
Deepvariant(inputFile,outputFolder,reference)
def main(argv):
usage = '''
variantCalling.py
Usage: python variantCalling.py [options]
options:
-h/--help
-P/--project path to the project, like /data/yangyusheng/projectName
-R/--reference path to the reference genome
-A/--annotation path to the annotation
-D/--data raw data, folder stores the raw data, like /data/yangyusheng/
-S/--suffix raw data suffix, the suffix of *.fastq is .fastq
-C/--caller Samtools, GATK, GATK_GVCF, Deepvariant
'''
try:
opts, args = getopt.getopt(argv,"hP:R:A:D:S:C:",["help","project=","reference=","annotation=","rawData=","suffix=","caller="])
except getopt.GetoptError:
print(usage)
sys.exit(2)
project = ""
reference = ""
annotation = ""
rawData = ""
suffix = ""
callers = []
for opt, arg in opts:
if opt == "-h" or opt == "--help":
print(usage)
elif opt == "-P" or opt == "--project":
project = arg
elif opt == "-R" or opt == "--reference":
reference = arg
elif opt == "-A" or opt == "--annotation":
annotation = arg
elif opt == "-D" or opt == "--rawData":
rawData = arg
elif opt == "-S" or opt == "--suffix":
suffix = arg
elif opt == "-C" or opt == "--caller":
callers.append(arg)
variantCalling(project,reference,annotation,rawData,suffix,callers)
if __name__ == "__main__":
main(sys.argv[1:])