-
Notifications
You must be signed in to change notification settings - Fork 0
/
slurm_script.sh
363 lines (276 loc) · 11.5 KB
/
slurm_script.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
#!/bin/bash
#SBATCH --nodes=1
#SBATCH --cpus-per-task=24
#SBATCH --partition normal
#SBATCH --time=2:00:00
#SBATCH --mem=140G
#SBATCH --job-name=metagenomics_pipeline
#SBATCH --error ./log/%x_%j.err.txt
#SBATCH --output ./log/%x_%j.out.txt
## Parameters ############################################################################
R1_base=`basename $1 .fastq.gz`
R2_base=`basename $2 .fastq.gz`
READ_LENGTH=75
TMP_DIR=./tmp/${R1_base}
OUT_DIR=./output/${R1_base}
mkdir tmp
mkdir output
mkdir "$TMP_DIR"
mkdir "$OUT_DIR"
mkdir "$OUT_DIR"/alignment
mkdir "$OUT_DIR"/fastqc
mkdir "$OUT_DIR"/arriba
mkdir "$OUT_DIR"/kraken
mkdir "$OUT_DIR"/bracken
mkdir "$OUT_DIR"/bracken/report
mkdir "$OUT_DIR"/centrifuge
mkdir "$OUT_DIR"/scripts
# Copy markdown scripts in output folder
cp ./scripts/metagenome_report.Rmd "$OUT_DIR"/scripts/metagenome_report.Rmd
cp ./scripts/metagenome_report.R "$OUT_DIR"/scripts/metagenome_report.R
# Bowtie index (human reference)
BOWTIE_INDEX=/path/to/bowtie_index/GRCh38_noalt_as
# Kraken databases
DB_VIRAL=/path/to/k2_viral_20230314
DB_STANDARD=/path/to/k2_standard_20230314
DB_EUPATH=/path/to/k2_eupathdb48_20230407
# Centrifuge index
CENTRIFUGE_INDEX=/path/to/centrifuge/indices/nt
# Arriba & STAR
BASE_DIR=/path/to/arriba_v2.4.0
STAR_INDEX_DIR=$BASE_DIR/STAR_index_GRCh37viral_GENCODE19
ANNOTATION_GTF=$BASE_DIR/GENCODE19.gtf
ASSEMBLY_FA=$BASE_DIR/GRCh37viral.fa
BLACKLIST_TSV=$BASE_DIR/database/blacklist_hg19_hs37d5_GRCh37_v2.4.0.tsv.gz
KNOWN_FUSIONS_TSV=$BASE_DIR/database/known_fusions_hg19_hs37d5_GRCh37_v2.4.0.tsv.gz
TAGS_TSV="$KNOWN_FUSIONS_TSV"
PROTEIN_DOMAINS_GFF3=$BASE_DIR/database/protein_domains_hg19_hs37d5_GRCh37_v2.4.0.gff3
# Threads
THREADS_KRAKEN=8
THREADS=24
##########################################################################################
## TRIM ##################################################################################
##########################################################################################
if [ -f ""$TMP_DIR"/${R1_base}.trimmed.fq.gz" ]; then
echo "Trimming for ${R1_base} already performed."
else
fqtrim -l 30 -D -p $THREADS -q 20 -P33 \
-r "$OUT_DIR"/alignment/fqtrim_report.txt \
-A -o trimmed.fq.gz --outdir "$TMP_DIR" "$1","$2"
fi
##########################################################################################
## FASTQC ################################################################################
##########################################################################################
if [ -f "${OUT_DIR}/fastqc/${R1_base}_fastqc.html" ]; then
echo "Fastqc for ${R1_base} already performed."
else
fastqc --extract -t $THREADS -o "$OUT_DIR"/fastqc "$1" "$2"
fastqc --extract -t $THREADS -o "$OUT_DIR"/fastqc "$TMP_DIR"/*.trimmed.fq.gz
fi
##########################################################################################
## Host removal ##########################################################################
##########################################################################################
if [ -f "${TMP_DIR}/${R1_base}_nonhuman_reads.fastq.gz" ]; then
echo "Host removal for ${R1_base} already performed."
else
bowtie2 -x $BOWTIE_INDEX -p $THREADS -1 "$TMP_DIR"/${R1_base}.trimmed.fq.gz \
-2 "$TMP_DIR"/${R2_base}.trimmed.fq.gz \
--un-conc-gz "$TMP_DIR"/${R1_base}_nonhuman_reads \
-S "$TMP_DIR"/${R1_base}_host_alignment.sam
mv "$TMP_DIR"/${R1_base}_nonhuman_reads.1 "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz
mv "$TMP_DIR"/${R1_base}_nonhuman_reads.2 "$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz
fi
echo $(zcat "$1"|wc -l)/4|bc > "$OUT_DIR"/alignment/total_reads.txt
echo $(zcat "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz|wc -l)/4|bc > "$OUT_DIR"/alignment/non_human_reads.txt
##########################################################################################
## Kraken2 ###############################################################################
##########################################################################################
if [ -f "${OUT_DIR}/kraken/${R1_base}.kraken.standard.txt" ]; then
echo "Kraken output for ${R1_base} already exists."
else
kraken2 \
--db $DB_STANDARD \
--threads $THREADS_KRAKEN \
--minimum-hit-groups 3 \
--use-names \
--report-minimizer-data \
--output "$TMP_DIR"/${R1_base}.kraken \
--report "$OUT_DIR"/kraken/${R1_base}.kraken.standard.txt \
--paired "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz \
"$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz
kraken2 \
--db $DB_VIRAL \
--threads $THREADS_KRAKEN \
--minimum-hit-groups 3 \
--use-names \
--report-minimizer-data \
--output "$TMP_DIR"/${R1_base}.kraken \
--report "$OUT_DIR"/kraken/${R1_base}.kraken.viral.txt \
--paired "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz \
"$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz
kraken2 \
--db $DB_EUPATH \
--threads $THREADS_KRAKEN \
--minimum-hit-groups 3 \
--use-names \
--report-minimizer-data \
--output "$TMP_DIR"/${R1_base}.kraken \
--report "$OUT_DIR"/kraken/${R1_base}.kraken.eupath.txt \
--paired "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz \
"$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz
fi
if [ -f "${OUT_DIR}/bracken/${R1_base}.standard_genus.txt" ]; then
echo "Bracken output for ${R1_base} already exists."
else
# Standard Database
bracken \
-d $DB_STANDARD \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.standard.txt \
-r $READ_LENGTH \
-l G \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.standard_genus.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.standard_genus.report.txt
bracken \
-d $DB_STANDARD \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.standard.txt \
-r $READ_LENGTH \
-l S \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.standard_species.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.standard_species.report.txt
# Viral Database
bracken \
-d $DB_VIRAL \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.viral.txt \
-r $READ_LENGTH \
-l G \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.viral_genus.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.viral_genus.report.txt
bracken \
-d $DB_VIRAL \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.viral.txt \
-r $READ_LENGTH \
-l S \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.viral_species.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.viral_species.report.txt
# EUPATHDB48 Database
bracken \
-d $DB_EUPATH \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.eupath.txt \
-r $READ_LENGTH \
-l G \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.eupath_genus.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.eupath_genus.report.txt
bracken \
-d $DB_EUPATH \
-i "$OUT_DIR"/kraken/${R1_base}.kraken.eupath.txt \
-r $READ_LENGTH \
-l S \
-t 10 \
-o "$OUT_DIR"/bracken/${R1_base}.eupath_species.txt \
-w "$OUT_DIR"/bracken/report/${R1_base}.eupath_species.report.txt
fi
##########################################################################################
## Centrifuge ############################################################################
##########################################################################################
if [ -f ""$OUT_DIR"/centrifuge/${R1_base}.centrifuge.txt" ]; then
echo "Centrifuge output for ${R1_base} already exists."
else
centrifuge -q \
-1 "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz \
-2 "$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz \
--report-file "$OUT_DIR"/centrifuge/${R1_base}.centrifuge.txt \
--threads $THREADS \
-x $CENTRIFUGE_INDEX
fi
##########################################################################################
## Arriba ################################################################################
##########################################################################################
if [ -f ""$OUT_DIR"/arriba/${R1_base}virus_expression1.tsv" ]; then
echo "Arriba analysis for ${R1_base} already exists."
else
STAR \
--runThreadN "$THREADS" \
--outFileNamePrefix "$TMP_DIR"/ \
--genomeDir "$STAR_INDEX_DIR" \
--genomeLoad NoSharedMemory \
--readFilesIn "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz "$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz \
--readFilesCommand zcat \
--outStd BAM_Unsorted \
--outSAMtype BAM Unsorted \
--outSAMunmapped Within \
--outBAMcompression 0 \
--outFilterMultimapNmax 50 \
--peOverlapNbasesMin 10 \
--alignSplicedMateMapLminOverLmate 0.5 \
--alignSJstitchMismatchNmax 5 -1 5 5 \
--chimSegmentMin 10 \
--chimOutType WithinBAM HardClip \
--chimJunctionOverhangMin 10 \
--chimScoreDropMax 30 \
--chimScoreJunctionNonGTAG 0 \
--chimScoreSeparation 1 \
--chimSegmentReadGapMax 3 \
--chimMultimapNmax 50 |
tee "$TMP_DIR"/${R1_base}_Aligned.out.bam |
"$BASE_DIR/arriba" \
-x /dev/stdin -I \
-o "$OUT_DIR"/arriba/${R1_base}fusions.tsv \
-a "$ASSEMBLY_FA" -g "$ANNOTATION_GTF" -b "$BLACKLIST_TSV" \
-k "$KNOWN_FUSIONS_TSV" -t "$TAGS_TSV" -p "$PROTEIN_DOMAINS_GFF3"
samtools sort -@ "$THREADS" -m $((40000/THREADS))M -T tmp \
-O bam "$TMP_DIR"/${R1_base}_Aligned.out.bam > "$TMP_DIR"/${R1_base}_Aligned.sortedByCoord.out.bam
rm -f "$TMP_DIR"/${R1_base}_Aligned.out.bam
samtools index "$TMP_DIR"/${R1_base}_Aligned.sortedByCoord.out.bam
$BASE_DIR/scripts/quantify_virus_expression.sh "$TMP_DIR"/${R1_base}_Aligned.sortedByCoord.out.bam \
"$OUT_DIR"/arriba/${R1_base}virus_expression.tsv
fi
##########################################################################################
## De novo assembly ######################################################################
##########################################################################################
if [ -f ""$OUT_DIR"/assembly/${R1_base}.contigs.fa" ]; then
echo "De novo assambly for ${R1_base} already exists."
else
megahit -1 "$TMP_DIR"/${R1_base}_nonhuman_reads.fastq.gz \
-2 "$TMP_DIR"/${R2_base}_nonhuman_reads.fastq.gz \
-o "$OUT_DIR"/assembly --min-contig-len 1000 \
--out-prefix ${R1_base} -t $THREADS
fi
rm -R "$OUT_DIR"/assembly/intermediate_contigs/*
##########################################################################################
## Kraken of de novo assembled contigs ###################################################
##########################################################################################
if [ -f "$OUT_DIR"/assembly/${R1_base}.kraken.report.txt ]; then
echo "Kraken output for ${R1_base} (de novo assembly) already exists."
else
kraken2 \
--db $DB_STANDARD \
--threads $THREADS_KRAKEN \
--minimum-hit-groups 3 \
--use-names \
--report-minimizer-data \
--output "$OUT_DIR"/assembly/${R1_base}.kraken.output.txt \
--report "$OUT_DIR"/assembly/${R1_base}.kraken.report.txt \
"$OUT_DIR"/assembly/${R1_base}.contigs.fa
fi
##########################################################################################
## Centrifuge of de novo assembled contigs ###############################################
##########################################################################################
if [ -f "$OUT_DIR"/assembly/${R1_base}.centrifuge.txt ]; then
echo "Centrifuge output for ${R1_base} (de novo assembly) already exists."
else
centrifuge -f \
--report-file "$OUT_DIR"/assembly/${R1_base}.centrifuge.report.txt \
--threads $THREADS \
-S "$OUT_DIR"/assembly/${R1_base}.centrifuge.txt \
-x $CENTRIFUGE_INDEX \
"$OUT_DIR"/assembly/${R1_base}.contigs.fa
fi
##########################################################################################
## Report ################################################################################
##########################################################################################
Rscript ./output/${R1_base}/scripts/metagenome_report.R ./output/${R1_base}/scripts