-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGATK_pipeline_v2_part4.sh
executable file
·501 lines (363 loc) · 16.6 KB
/
GATK_pipeline_v2_part4.sh
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/bin/bash
#
# all submission arguments here
########################################################################################################
# PART 4: Joint genotyping of all samples, Variant Filtration
########################################################################################################
## CatVariants (merge all joint_<chr>.vcf into joint.vcf)
## Variant Recalibration
# - VariantRecalibrator (SNPs)
# - ApplyRecalibration (SNPs)
# - validate variants (SNPs)
# - VariantRecalibrator (indels)
# - ApplyRecalibration (indels)
# - validate variants (indels)
## Refinement
# - CalculateGenotypePosteriors
# - VariantFiltration (skipped)
# - validate variants
## clean VCF
# - LeftAlignAndTrimVariants
# - validate variants
## File cleanup
# - file cleanup
########################################################################################################
# execute using:
# bsub < scripts/GATK_pipeline_v2_part4.sh
source ./utils.sh
mkdir -p $root_folder/output_gatk
mkdir -p $root_folder/logs
# num_samples=$( wc -l $root_folder/bams_to_process_part3.txt )
num_samples=$( ls $path_rds_vcfs/*.g.vcf.gz | wc -l )
# num_samples=$( echo $num_samples | cut -d' ' -f1 )
# output_name="joint_"$num_samples"samples"
output_name="227uk.germline_calls.noQC"
####################################################
# GATK CatVariants
####################################################
if [ ! -f $root_folder/logs/part_4_CatVariants_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting GATK CatVariants: merging all joint_<chr>.vcf into joint.vcf---" >> "$logfile"
gvcf_paths=$( find output_gatk/ -iname joint_chr*.vcf )
# Create an array variable that contains location of all joint_<chr>.vcf
vcf_array=$(for i in $gvcf_paths; do echo "--variant $i"; done)
time(java -Xmx16g -Djava.io.tmpdir=/tmp \
-cp $path_GATK \
org.broadinstitute.gatk.tools.CatVariants \
-R $path_ref \
${vcf_array[@]} \
-out $root_folder/output_gatk/$output_name.vcf )
# Catching failed java jobs ("time" doesn't change the exit value of java above)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished GATK CatVariants" >> "$logfile" \
&& touch $root_folder/logs/part_4_CatVariants_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), GATK CatVariants NOT FINISHED" >> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping GATK CatVariants since it was previously computed***" >> "$logfile"
fi
####################################################
# Check the validity of the vcf file
####################################################
if [ ! -f $root_folder/logs/part_4_VCF_validation_after_CatVariants_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Validating VCF after CatVariants---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ValidateVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.vcf \
--validationTypeToExclude ALL \
) >> "$logfile"
# NOTE that in case of invalid VCF, GATK will exit anyway
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), VCF validation after CatVariants COMPLETED">> "$logfile" \
&& touch $root_folder/logs/part_4_VCF_validation_after_CatVariants_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), VCF validation after CatVariants NOT COMPLETED">> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping VCF validation after CatVariants since it was previously computed***" >> "$logfile"
fi
############################################################################
# SNP Filtering (VQSR) - Variant Recalibrator
############################################################################
if [ ! -f $root_folder/logs/part_4_GATK_SNP_VQSR_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting GATK SNP VQSR---" >> "$logfile"
# Creating the machine learning SNP calibration model
time(java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T VariantRecalibrator \
-nt 16 \
-R $path_ref \
-input $root_folder/output_gatk/$output_name.vcf \
-recalFile $root_folder/output_gatk/$output_name.SNP.recal \
-tranchesFile $root_folder/output_gatk/$output_name.SNP.tranches \
-resource:hapmap,known=false,training=true,truth=true,prior=15.0 $bundle2_8/b37/hapmap_3.3.b37.vcf \
-resource:omni,known=false,training=true,truth=true,prior=12.0 $bundle2_8/b37/1000G_omni2.5.b37.vcf \
-resource:1000G,known=false,training=true,truth=false,prior=10.0 $bundle2_8/b37/1000G_phase1.snps.high_confidence.b37.vcf \
-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $bundle2_8/b37/dbsnp_138.b37.vcf \
-an QD -an MQ -an MQRankSum -an ReadPosRankSum -an FS -an SOR -an DP -an InbreedingCoeff \
-rscriptFile $root_folder/output_gatk/$output_name.recalibrate_SNP_plots.R \
-tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 90.0 \
-mode SNP)
# note:
# added -an DP
# added line -tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 90.0 \
# eventually, we should have:
# -an QD -an MQ -an MQRankSum -an ReadPosRankSum -an FS -an SOR -an DP -an InbreedingCoeff \
# see http://gatkforums.broadinstitute.org/gatk/discussion/1259/which-training-sets-arguments-should-i-use-for-running-vqsr
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished SNP VQSR VariantRecalibrator---" >> "$logfile"
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK SNP VQSR VariantRecalibrator NOT FINISHED---" >> "$logfile" \
exit $exitValue
fi
# Applying the SNP model
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ApplyRecalibration \
-R $path_ref \
-input $root_folder/output_gatk/$output_name.vcf \
-tranchesFile $root_folder/output_gatk/$output_name.SNP.tranches \
-recalFile $root_folder/output_gatk/$output_name.SNP.recal \
-o $root_folder/output_gatk/$output_name.SNP.recal.vcf \
--ts_filter_level 99.9 \
-mode SNP)
# --excludeFiltered \
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished GATK SNP VQSR ApplyRecalibration---" >> "$logfile" \
&& touch $root_folder/logs/part_4_GATK_SNP_VQSR_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK SNP VQSR ApplyRecalibration NOT FINISHED---" >> "$logfile" \
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping GATK SNP VQSR since it was previously computed***" >> "$logfile"
fi
####################################################
# Check the validity of the vcf file
####################################################
if [ ! -f $root_folder/logs/part_4_GATK_SNP_VQSR_VCF_validation_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Checking SNP_VQSR vcf validity---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ValidateVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.SNP.recal.vcf \
--validationTypeToExclude ALL \
) >> "$logfile"
# NOTE that in case of invalid VCF, GATK will exit anyway
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), SNP_VQSR vcf validation completed">> "$logfile" \
&& touch $root_folder/logs/part_4_GATK_SNP_VQSR_VCF_validation_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), SNP_VQSR vcf validation NOT FINISHED">> "$logfile" \
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping SNP_VQSR vcf validation since it was previously computed***" >> "$logfile"
fi
############################################################################
# INDEL Filtering (VQSR) - Variant Recalibrator
############################################################################
if [ ! -f $root_folder/logs/part_4_GATK_indel_VQSR_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting GATK indel VQSR---" >> "$logfile"
# Creating the machine learned INDEL calibration model
# NOTE:
# changed -resource:mills,known=true...
# to -resource:mills,known=false...
time(java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T VariantRecalibrator \
-nt 16 \
-R $path_ref \
-input $root_folder/output_gatk/$output_name.SNP.recal.vcf \
-recalFile $root_folder/output_gatk/$output_name.indel.recal \
-tranchesFile $root_folder/output_gatk/$output_name.indel.tranches \
-resource:mills,known=false,training=true,truth=true,prior=12.0 $bundle2_8/b37/Mills_and_1000G_gold_standard.indels.b37.vcf \
-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $bundle2_8/b37/dbsnp_138.b37.vcf \
-an QD -an DP -an FS -an SOR -an ReadPosRankSum -an MQRankSum -an InbreedingCoeff \
-tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 90.0 \
--maxGaussians 4 \
-rscriptFile $root_folder/output_gatk/$output_name.recalibrate_INDEL_plots.R \
-mode INDEL)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished indel VQSR VariantRecalibrator---" >> "$logfile"
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK indel VQSR VariantRecalibrator NOT FINISHED---" >> "$logfile"
exit $exitValue
fi
# Applying the INDEL model
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ApplyRecalibration \
-R $path_ref \
-input $root_folder/output_gatk/$output_name.SNP.recal.vcf \
-tranchesFile $root_folder/output_gatk/$output_name.indel.tranches \
-recalFile $root_folder/output_gatk/$output_name.indel.recal \
-o $root_folder/output_gatk/$output_name.SNP.indel.recal.vcf \
--ts_filter_level 99.9 \
-mode INDEL)
# --excludeFiltered \
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished GATK indel VQSR ApplyRecalibration---" >> "$logfile" \
&& touch $root_folder/logs/part_4_GATK_indel_VQSR_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK indel VQSR ApplyRecalibration NOT FINISHED---" >> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping GATK indel VQSR since it was previously computed***" >> "$logfile"
fi
####################################################
# Check the validity of the vcf file
####################################################
if [ ! -f $root_folder/logs/part_4_indel_VQSR_VCF_validation_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Checking indel_VQSR vcf validity---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ValidateVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.SNP.indel.recal.vcf \
--validationTypeToExclude ALL \
) >> "$logfile"
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK indel_VQSR vcf validation completed---">> "$logfile" \
&& touch $root_folder/logs/part_4_indel_VQSR_VCF_validation_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Validation of $root_folder/output_gatk/$output_name.SNP.indel.recal.vcf NOT FINISHED---">> "$logfile" \
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping indel_VQSR vcf validation since it was previously computed***" >> "$logfile"
fi
####################################################
# Genotype Refinement
####################################################
if [ ! -f $root_folder/logs/part_4_GATK_CalculateGenotypePosteriors_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting GATK CalculateGenotypePosteriors---" >> "$logfile"
# Derive posterior probabilities of genotypes (using 1000G phase 3 data)
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-R $path_ref \
-T CalculateGenotypePosteriors \
--supporting $bundle2_8/b37/1000G_phase3_v4_20130502.sites.vcf.gz \
-V $root_folder/output_gatk/$output_name.SNP.indel.recal.vcf \
-o $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.vcf)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished GATK CalculateGenotypePosteriors---" >> "$logfile"
touch $root_folder/logs/part_4_GATK_CalculateGenotypePosteriors_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK CalculateGenotypePosteriors NOT FINISHED---" >> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping GATK CalculateGenotypePosteriors since it was previously computed***" >> "$logfile"
fi
####################################################
# VariantFiltration
####################################################
# if [ ! -f $root_folder/logs/part_4_GATK_VariantFiltration_finished.txt ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting GATK VariantFiltration---" >> "$logfile"
# # set FILTER with GQ < 20 or DP < 8 to "low_GQ_or_DP", and set their GT to no call.
# time (java -Xmx15g -Djava.io.tmpdir=/tmp \
# -jar $path_GATK \
# -T VariantFiltration \
# -R $path_ref \
# -V $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.vcf \
# --genotypeFilterExpression "GQ < 20.0 || DP < 8" --genotypeFilterName "low_GQ_or_DP" \
# --setFilteredGtToNocall \
# -o $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.filtered.vcf)
# exitValue=$?
# if [ $exitValue == 0 ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished GATK VariantFiltration---" >> "$logfile" \
# && touch $root_folder/logs/part_4_GATK_VariantFiltration_finished.txt
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'),---GATK VariantFiltration NOT FINISHED---" >> "$logfile"
# exit $exitValue
# fi
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping GATK VariantFiltration since it was previously computed***" >> "$logfile"
# fi
####################################################
# Check the validity of the vcf file
####################################################
if [ ! -f $root_folder/logs/part_4_postCGP_VCF_validation_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Checking postCGP vcf validity---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ValidateVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.vcf \
--validationTypeToExclude ALL \
) >> "$logfile"
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), postCGP vcf validation completed">> "$logfile" \
&& touch $root_folder/logs/part_4_postCGP_VCF_validation_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), postCGP vcf validation NOT COMPLETED">> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping postCGP vcf validation since it was previously computed***" >> "$logfile"
fi
####################################################
# LEFT ALIGN, TRIM, SPLIT VARIANTS
####################################################
# Left align and trim the variants, split multiple into biallelic (pre-processing for annotation)
if [ ! -f $root_folder/logs/part_4_leftalign_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Left aligning, trimming and splitting variants---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T LeftAlignAndTrimVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.vcf \
-o $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.aln_trim_split.vcf \
--splitMultiallelics)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), Left aligning, trimming and splitting variants completed">> "$logfile" \
&& touch $root_folder/logs/part_4_leftalign_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), Left aligning, trimming and splitting variants NOT COMPLETED">> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping Left aligning, trimming and splitting variants since it was previously computed***" >> "$logfile"
fi
####################################################
# Check the validity of the vcf file
####################################################
if [ ! -f $root_folder/logs/part_4_postTrim_VCF_validation_finished.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Checking postTrim vcf validity---" >> "$logfile"
time (java -Xmx15g -Djava.io.tmpdir=/tmp \
-jar $path_GATK \
-T ValidateVariants \
-R $path_ref \
-V $root_folder/output_gatk/$output_name.SNP.indel.recal.postCGP.aln_trim_split.vcf \
--validationTypeToExclude ALL \
) >> "$logfile"
# NOTE that in case of invalid VCF, GATK will exit anyway
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), postTrim vcf validation completed">> "$logfile" \
&& touch $root_folder/logs/part_4_postTrim_VCF_validation_finished.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), postTrim vcf validation NOT COMPLETED">> "$logfile"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping postTrim vcf validation since it was previously computed***" >> "$logfile"
fi