-
Notifications
You must be signed in to change notification settings - Fork 9
/
116_runGATK.py
247 lines (194 loc) · 8.28 KB
/
116_runGATK.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
#-----------------------------------------------------------+
# |
# 116_runGATK.py - script to run GATK analysis |
# |
#-----------------------------------------------------------+
# |
# AUTHOR: Vikas Gupta |
# CONTACT: [email protected] |
# STARTED: 09/06/2013 |
# UPDATED: 09/06/2013 |
# |
# DESCRIPTION: |
# |
# LICENSE: |
# GNU General Public License, Version 3 |
# http://www.gnu.org/licenses/gpl.html |
# |
#-----------------------------------------------------------+
# Example:
# python ~/script/python/116_runGATK.py -i 02_Stegodyphous_cdna.refined.fa.orf.tr_longest_frame
### import modules
import os,sys,getopt, re
### global variables
global bams, ref, picard, gatk, threads, variant, sort_bams, tmp_dir
### make a logfile
import datetime
now = datetime.datetime.now()
o = open(str(now.strftime("%Y-%m-%d_%H%M."))+'logfile','w')
### write logfile
def logfile(infile):
o.write("Program used: \t\t%s" % "116_runGATK.py"+'\n')
o.write("Program was run at: \t%s" % str(now.strftime("%Y-%m-%d_%H%M"))+'\n')
o.write("Infile used: \t\t%s" % infile+'\n')
def help():
print '''
python 116_runGATK.py
-b <bams> [One bam file per sample seperated by commas]
-r <ref> [Reference sequence]
-p <picard> [Path to picard folder MarkDuplicates.jar]
-g <gatk> [Path to GATK folder containing GenomeAnalysisTK.jar]
-t <threads> [Number of threads to be used]
'''
sys.exit(2)
### main argument to
def options(argv):
global bams, ref, picard, gatk, threads, variant, sort_bams, tmp_dir
bams = ''
ref = ''
picard = ''
gatk = ''
threads = str(1)
variant = ''
sort_bams = False
tmp_dir="/home/vgupta/temp"
try:
opts, args = getopt.getopt(argv,"hb:r:p:g:t:v:s",["bams=","ref=","picard=","gatk=","threads=","variant=", "sort_bams="])
except getopt.GetoptError:
help()
for opt, arg in opts:
if opt == '-h':
help()
elif opt in ("-b", "--bams"):
bams = arg
elif opt in ("-r", "--ref"):
ref = arg
elif opt in ("-p", "--picard"):
picard = arg
elif opt in ("-g", "--gatk"):
gatk = arg
elif opt in ("-t", "--threads"):
threads = str(arg)
elif opt in ("-v", "--variant"):
variant = arg
elif opt in ("-s", "--sort"):
sort_bams = True
logfile(bams)
def Index():
if not os.path.isfile(ref + '.fai'):
os.system('nice -n 19 samtools faidx '+ ref)
def files():
files = []
print bams
for file in bams.split(','):
print file
files.append(file.strip())
return files
def sortBams(file_list):
if sort_bams == True:
file_list_bams = []
for file in file_list:
os.system('samtools sort '+file+' '+file+'_sorted')
file_list_bams.append(file+'_sorted.bam')
return file_list_bams
return file_list
def MarkDuplicates(file_list):
for file in file_list:
print 'Marking duplicates for', file
os.system('java -Xmx50g -jar '+picard+'/MarkDuplicates.jar VALIDATION_STRINGENCY=LENIENT TMP_DIR='+tmp_dir +' INPUT='+ file + ' OUTPUT=' + file+'.dedup.bam' +' METRICS_FILE='+ file +'.dups')
def ReAlign(file_list):
for file in file_list:
print 'Realigning', file
os.system('java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar --fix_misencoded_quality_scores -fixMisencodedQuals -U -T RealignerTargetCreator '+' -I ' + file+'.dedup.bam' +' -nt '+ threads +' -R ' + ref +' -o '+ file+'.intervals')
os.system('java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar --fix_misencoded_quality_scores -fixMisencodedQuals -U -T IndelRealigner ' + ' -targetIntervals ' + file+'.intervals '+ ' -I ' + file+'.dedup.bam' +' -R ' + ref \
+ ' -o ' + file+'.realigned.bam')
def UnifiedGenotyper(file_list):
if variant == '':
in_string = ''
### make input string
for file in file_list:
os.system('samtools index '+file)
in_string += ' -I '+file
print 'Running UnifiedGenotyper'
'''
### for sample
os.system(' java -jar '+gatk+'/GenomeAnalysisTK.jar '\
+ ' -R ' + ref \
+ ' -T UnifiedGenotyper '\
+ in_string \
+ ' -nt ' + threads \
+ ' -o snps.90.raw.vcf ' \
+ '-stand_call_conf 20 ' \
+ '-stand_emit_conf 10.0 '\
+ '-dcov 2 ')
'''
os.system(' java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar '\
+ ' -R ' + ref \
+ ' -T UnifiedGenotyper -glm BOTH '\
+ in_string \
+ ' -nt ' + threads \
+ ' -o '+ file +'.90.vcf ' \
+ '-stand_call_conf 90 ' \
+ '-stand_emit_conf 10.0 '\
+ '-dcov 200 ')
def recal(file_list):
global variant
if variant == '':
variant = file_list[-1] +'.90.vcf'
for file in file_list:
print 'Running BaseRecalibrator for ', file
os.system('java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar -U -T BaseRecalibrator -rf BadCigar ' + ' -knownSites ' +variant + ' -I ' + file+'.realigned.bam '+ ' -R ' + ref \
+ ' -o ' + file+'.recal.table')
os.system('java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar -T PrintReads -R '+ref +' -I '+ file+'.realigned.bam -L 20 '+ ' -BQSR '+file+'.recal.table' +' -o '+ file+ '.recal_reads.bam' )
def ReduceReads(file_list):
for file in file_list:
print 'Running ReduceReads for ', file
os.system('java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar -U -T ReduceReads -rf BadCigar -I ' + file+ '.recal_reads.bam'+ ' -R ' + ref \
+ ' -o ' + file+'.reduced.bam')
def ReUnifiedGenotyper(file_list):
print 'Running ReUnifiedGenotyper'
in_string = ''
### make input string
for file in file_list:
os.system('samtools index '+ file+'.reduced.bam')
in_string += ' -I '+file+'.reduced.bam'
os.system(' java -jar -Djava.io.tmpdir='+tmp_dir+' '+gatk+'/GenomeAnalysisTK.jar '\
+ ' -R ' + ref \
+ ' -T UnifiedGenotyper -glm BOTH'\
+ in_string \
+ ' -nt ' + threads \
+ ' -o snps.raw.vcf ')
def BuildErrorModelWithVQSR(file , var):
os.system('java -jar '+gatk+'/GenomeAnalysisTK.jar '\
+ ' -T VariantRecalibrator ' \
+ ' -R '+ ref \
+ ' -input '+ file \
+ ' -recalFile output.recal ' \
+ ' -tranchesFile output.tranches ' \
+ ' -nt ' + threads \
+ ' -mode ' + var)
if __name__ == "__main__":
options(sys.argv[1:])
### check if index exits
Index()
### return the list of the bam files
file_list = files()
### sort Bams file
file_list = sortBams(file_list)
### mark duplicates
MarkDuplicates(file_list)
### realign the reads
ReAlign(file_list)
### call UnifiedGenotyper to make a primary list of variants
UnifiedGenotyper(file_list)
### Baserecalibration
recal(file_list)
### reducing BAM files
ReduceReads(file_list)
### Run UnifiedGenotyper
ReUnifiedGenotyper(file_list)
### BuildErrorModelWithVQSR
#BuildErrorModelWithVQSR('snps.raw.vcf', 'SNP')
#BuildErrorModelWithVQSR('snps.raw.vcf', 'INDEL')
### close the logfile
o.close()