-
Notifications
You must be signed in to change notification settings - Fork 1
/
GATK_pipeline_v2_part1.sh
executable file
·357 lines (249 loc) · 11.8 KB
/
GATK_pipeline_v2_part1.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
#!/bin/bash
#
# all submission arguments here
########################################################################################################
# PART 1: From raw basecalls to GATK-ready reads
########################################################################################################
## Clean BAM
# - mark duplicates using Picard tools MarkDuplicates (removed)
# - CleanSam using Picard tools
# - FixMateInformation using Picard tools
# - reformat BAM header (HiSeq -> illumina)
# - ValidateSamFile using Picard tools
# - generate BAM index using samtools
## Metrics
# - verify BAM id using verifyBamID
# - get stats of BAM using Samtools flagstat
# - wgs metrics using Picard tools CollectWgsMetrics
# - insert size, alignment and GC bias metrics using Picard tools CollectMultipleMetrics
## File cleanup
# - file cleanup
########################################################################################################
# execute using:
# bsub -J "GATKp1[1-20]" < GATK_pipeline_v2_part1.sh
source ./utils.sh
mkdir -p $root_folder/logs
####################################################
# Variables dependent on sample
####################################################
# sample=$(sed "${LSB_JOBINDEX}q;d" bams_to_process_part1.txt)
sample=$(sed "${LSB_JOBINDEX}q;d" bams_to_process.txt)
samplelog=$root_folder/logs/$sample.log
count=$(grep $sample exclude.txt | wc -l)
if [ "$count" -ge "1" ]; then
exit
fi
####################################################
# Mark Duplicates
####################################################
# if [ ! -f $root_folder/logs/Picard_MarkDuplicates_finished_$sample.txt ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting Picard MarkDuplicates---" >> "$samplelog"
# time (\
# java -Xmx2g -Djava.io.tmpdir=/tmp \
# -jar $path_picard MarkDuplicates \
# I=$original_path/$sample.bam \
# O=$root_folder/$sample.md.bam \
# M=$root_folder/logs/$sample.duplicate_metrics.txt \
# CREATE_INDEX=true \
# VALIDATION_STRINGENCY=SILENT \
# REMOVE_DUPLICATES=true \
# )
# exitValue=$?
# if [ $exitValue == 0 ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'),Finished Picard MarkDuplicates" >> "$samplelog" \
# && touch $root_folder/logs/Picard_MarkDuplicates_finished_$sample.txt
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'), ERROR: Picard MarkDuplicates on $sample.bam not completed. ExitValue = $exitValue" >> "$samplelog"
# exit $exitValue
# fi
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping Picard MarkDuplicates since it was already computed***" >> "$samplelog"
# fi
####################################################
# Clean BAM
####################################################
if [ ! -f $root_folder/logs/part_1_Picard_CleanSam_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Cleaning BAM---" >> "$samplelog"
java -Xmx8g -Djava.io.tmpdir=/tmp \
-jar $path_picard CleanSam \
I=$original_path/$sample.cram \
R=$path_ref \
O=$root_folder/input_bams/$sample.clean.bam >> "$samplelog"
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished cleaning BAM---" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Picard_CleanSam_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), ERROR: Picard CleanSam not completed. ExitValue = $exitValue" >> "$samplelog"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping Picard CleanSam since it was already computed***" >> "$samplelog"
fi
####################################################
# FixMateInformation
####################################################
if [ ! -f $root_folder/logs/part_1_Picard_FixMateInformation_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting FixMateInformation---" >> "$samplelog"
time (java -Xmx8g -Djava.io.tmpdir=$tmp \
-jar $path_picard FixMateInformation \
I=$root_folder/input_bams/$sample.clean.bam \
O=$root_folder/input_bams/$sample.fixed.bam \
TMP_DIR=$tmp \
)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished Picard FixMateInformation" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Picard_FixMateInformation_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), ERROR: Picard FixMateInformation not completed. ExitValue = $exitValue" >> "$samplelog"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping FixMateInformation since it was already computed***" >> "$samplelog"
fi
####################################################
# Reformat BAM header (PL: HiSeq -> illumina)
####################################################
if [ ! -f $root_folder/logs/part_1_Reformat_header_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting Reformat header---" >> "$samplelog"
samtools view -H $root_folder/input_bams/$sample.fixed.bam | sed -e 's/PL:HiSeq/PL:illumina/g' | samtools reheader - $root_folder/input_bams/$sample.fixed.bam > $root_folder/input_bams/$sample.reheader.bam \
&& echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished reformating header" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Reformat_header_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping reformating header since it was already computed***" >> "$samplelog"
fi
####################################################
# Generate BAM index
####################################################
if [ ! -f $root_folder/input_bams/$sample.reheader.bam.bai ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Generating BAM index---" >> "$samplelog"
time (samtools index $root_folder/input_bams/$sample.reheader.bam) \
&& echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished generating BAM index" >> "$samplelog"
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping generating BAM index since it was already created***" >> "$samplelog"
fi
####################################################
# Validate BAM
####################################################
if [ ! -f $root_folder/logs/part_1_Picard_ValidateSamFile_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Validating BAM---" >> "$samplelog"
java -Xmx8g -Djava.io.tmpdir=/tmp \
-jar $path_picard ValidateSamFile \
I=$root_folder/input_bams/$sample.reheader.bam \
MODE=SUMMARY >> "$samplelog"
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Finished BAM Validation---" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Picard_ValidateSamFile_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping BAM Validation since it was already computed***" >> "$samplelog"
fi
####################################################
# verifyBamID
####################################################
if [ ! -f $root_folder/logs/part_1_VerifyBamID_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting VerifyBamID---" >> "$samplelog"
time (verifyBamID \
--vcf $vcf_REF \
--bam $root_folder/input_bams/$sample.reheader.bam \
--out $root_folder/logs/$sample.VerifyBamID \
--ignoreRG \
--verbose)
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished VerifyBamID" >> "$samplelog" \
&& touch $root_folder/logs/part_1_VerifyBamID_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping VerifyBamID since it was already computed***" >> "$samplelog"
fi
####################################################
# SAM FILE FLAG STATISTICS (samtools flagstat)
####################################################
if [ ! -f $root_folder/logs/part_1_Samtools_flagstats_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting Samtools flagstat---" >> "$samplelog"
samtools flagstat $root_folder/input_bams/$sample.reheader.bam >> "$samplelog" \
&& touch $root_folder/logs/part_1_Samtools_flagstats_finished_$sample.txt \
&& echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished samtools flagstat" >> "$samplelog"
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping samtools flagstat since it was already computed***" >> "$samplelog"
fi
####################################################
# INSERT SIZE METRICS
####################################################
# if [ ! -f $root_folder/logs/part_1_Picard_CollectInsertSizeMetrics_finished_$sample.txt ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting Picard CollectInsertSizeMetrics---" >> "$samplelog"
# time (java -Xmx8g -Djava.io.tmpdir=/tmp \
# -jar $path_picard CollectInsertSizeMetrics \
# I=$root_folder/input_bams/$sample.reheader.bam \
# O=$root_folder/logs/$sample.insertsize_metrics.txt \
# METRIC_ACCUMULATION_LEVEL=null \
# METRIC_ACCUMULATION_LEVEL=READ_GROUP \
# VALIDATION_STRINGENCY=SILENT \
# H=$root_folder/logs/$sample.insertsize_graph.pdf)
# exitValue=$?
# if [ $exitValue == 0 ]; then
# echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished Picard CollectInsertSizeMetrics" >> "$samplelog" \
# && touch $root_folder/logs/part_1_Picard_CollectInsertSizeMetrics_finished_$sample.txt
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'), ERROR: Picard CollectInsertSizeMetrics not completed. ExitValue = $exitValue" >> "$samplelog"
# exit $exitValue
# fi
# else
# echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping Picard CollectInsertSizeMetrics since it was already computed***" >> "$samplelog"
# fi
####################################################
# CollectWgsMetrics
####################################################
if [ ! -f $root_folder/logs/part_1_Picard_CollectWgsMetrics_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Starting Picard CollectWgsMetrics---" >> "$samplelog"
time (java -Xmx12g -Djava.io.tmpdir=/tmp \
-jar $path_picard CollectWgsMetrics \
R=$path_ref \
I=$root_folder/input_bams/$sample.reheader.bam \
O=$root_folder/logs/$sample.wgs_metrics.txt \
INCLUDE_BQ_HISTOGRAM=true)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished Picard CollectWgsMetrics" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Picard_CollectWgsMetrics_finished_$sample.txt
else
echo "$(date '+%d/%m/%y_%H:%M:%S'), ERROR: Picard CollectWgsMetrics not completed. ExitValue = $exitValue" >> "$samplelog"
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping Picard CollectWgsMetrics since it was already computed***" >> "$samplelog"
fi
##################################################
# Collect multiple metrics
##################################################
# Collect metrics on insert size, GC bias, alignment summary
if [ ! -f $root_folder/logs/part_1_Picard_CollectMultiMetrics_finished_$sample.txt ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Collecting multiple metrics---" >> "$samplelog"
time (java -Xmx12g -Djava.io.tmpdir=/tmp \
-jar $path_picard CollectMultipleMetrics \
R=$path_ref \
I=$root_folder/input_bams/$sample.reheader.bam \
O=$root_folder/logs/$sample.multiple_metrics \
PROGRAM=null \
PROGRAM=CollectAlignmentSummaryMetrics \
PROGRAM=CollectInsertSizeMetrics \
PROGRAM=CollectGcBiasMetrics \
METRIC_ACCUMULATION_LEVEL=null \
METRIC_ACCUMULATION_LEVEL=READ_GROUP \
METRIC_ACCUMULATION_LEVEL=SAMPLE \
VALIDATION_STRINGENCY=SILENT)
exitValue=$?
if [ $exitValue == 0 ]; then
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished collecting multiple metrics" >> "$samplelog" \
&& touch $root_folder/logs/part_1_Picard_CollectMultiMetrics_finished_$sample.txt
else
exit $exitValue
fi
else
echo "$(date '+%d/%m/%y_%H:%M:%S'),***Skipping collecting multiple metrics since it was already computed***" >> "$samplelog"
fi
####################################################
# File cleanup
####################################################
echo "$(date '+%d/%m/%y_%H:%M:%S'),---Removing intermediate files---" >> "$samplelog"
rm -f $root_folder/input_bams/$sample.bam
rm -f $root_folder/input_bams/$sample.clean.bam
rm -f $root_folder/input_bams/$sample.fixed.bam
echo "$(date '+%d/%m/%y_%H:%M:%S'), Finished removing intermediate files" >> "$samplelog"