-
Notifications
You must be signed in to change notification settings - Fork 2
/
ribopipe_halt_step_2_4.sh
executable file
·595 lines (484 loc) · 19 KB
/
ribopipe_halt_step_2_4.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env bash
# Ribosome profiling pipeline
#
# Authors:
# Jan Karlsen, KTH
# Johannes Asplund-Samuelsson, KTH
# Input variables
# Read input variables from config file specified via command line
# e.g. "./ribopipe.sh ribopipe_config.sh"
source $1
# Work in output directory
cd $OUTDIR
# Manually place input fastq.gz files in rawfastqgz subdirectory
# Store input filenames in array
INPUT_FILES=(rawfastqgz/*fastq.gz)
################################################################################
# Step 1: Import data to working directory
S=1
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Setting up output directory...\e[0m\n"
# Set up remaining output subdirectories automatically
mkdir qualityCheck cutadapt lengthDistr highQuality
mkdir tANDrRNAremoval mapped readcount RPM readsPerGene
mkdir analysis
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 2: Make one fastq.gz file per sample
S=2
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Concatenating fastq files per sample...\e[0m\n"
# Concatenate input files
# Iterate over all infiles
for infile in "${INPUT_FILES[@]}"
do
# Extract the sample name from each infile
sample_name=$(echo "$infile" | rev | cut -f 1 -d \/ | rev | cut -f 1 -d _)
# Construct an outfile name
outfile="rawfastqgz/${EXPERIMENT_NAME}.${sample_name}.fastq"
# Unzip the infile, keeping it intact, and add data to end of outfile
gunzip -c $infile >> $outfile
done
# Gzip concatenated files
pigz rawfastqgz/*fastq
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
if [[ HALT_S2 -eq 1 ]]
then
# Halt script
echo -e "\n\e[33mStep $S: Halting. Check results and modify inputs.\e[0m\n"
exit 1
fi
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 3: Assess read quality
S=3
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Assessing read quality with FastQC...\e[0m\n"
# Calculate number of threads to use
# Count concatenated input files
number_files=$(ls rawfastqgz/${EXPERIMENT_NAME}.*.fastq.gz | wc -l)
# Pick lowest of number_files and THREADS
threads_fastqc=$(dc -e "[${THREADS}]sM ${number_files}d ${THREADS}<Mp")
# Run fastqc on all concatenated input files
fastqc -t $threads_fastqc -o qualityCheck \
rawfastqgz/${EXPERIMENT_NAME}.*.fastq.gz
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 4: Trim away adapter sequences (PARALLEL)
S=4
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Trimming adapter sequences...\e[0m\n"
# Store input filenames in array (specific for step 4; files from step 2)
input_files_4=(rawfastqgz/${EXPERIMENT_NAME}.*.fastq.gz)
# Define run_cutadapt function to be used with GNU parallel application
run_cutadapt() {
# The infile name is stored in positional argument 1
infile=$1
# Extract the sample name from the infile
sample_name=$(echo "$infile" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="cutadapt/${EXPERIMENT_NAME}.${sample_name}"
out_cutadapt="${prefix}.cutadapt.fastq.gz"
out_tooShort="${prefix}.tooShort.fastq.gz"
out_untrimmed="${prefix}.untrimmed.fastq.gz"
out_report="${prefix}.cutadapt.report.txt"
# Run cutadapt with the supplied options
cutadapt -a $cutadapt_a -O $cutadapt_O -m $cutadapt_m -n $cutadapt_n \
-e $cutadapt_e --too-short-output=$out_tooShort \
--untrimmed-output=$out_untrimmed -o $out_cutadapt $infile > $out_report
}
# Export function and variables so that each subprocess can access them
export -f run_cutadapt
export cutadapt_a cutadapt_O cutadapt_m cutadapt_n cutadapt_e
export EXPERIMENT_NAME
# Run cutadapt in parallel for the input files
parallel --no-notice --jobs $THREADS run_cutadapt ::: ${input_files_4[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
if [[ HALT_S4 -eq 1 ]]
then
# Halt script
echo -e "\n\e[33mStep $S: Halting. Check results and modify inputs.\e[0m\n"
exit 1
fi
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 5: Assess footprint-length distribution
S=5
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Assessing footprint-length distribution...\e[0m\n"
# Calculate number of threads to use
# Count concatenated input files
number_files=$(ls cutadapt/${EXPERIMENT_NAME}.*.cutadapt.fastq.gz | wc -l)
# Pick lowest of number_files and THREADS
threads_fastqc=$(dc -e "[${THREADS}]sM ${number_files}d ${THREADS}<Mp")
# Run fastqc on all concatenated input files
fastqc -t $threads_fastqc -o lengthDistr \
cutadapt/${EXPERIMENT_NAME}.*.cutadapt.fastq.gz
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
if [[ HALT_S5 -eq 1 ]]
then
# Halt script
echo -e "\n\e[33mStep $S: Halting. Check results and modify inputs.\e[0m\n"
exit 1
fi
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 6: Filter reads with low-quality base calls (PARALLEL)
S=6
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Removing reads with low-quality base calls...\e[0m\n"
# Store input filenames in array (specific for step 6; files from step 4)
input_files_6=(cutadapt/${EXPERIMENT_NAME}.*.cutadapt.fastq.gz)
# Define run_sickle function to be used with GNU parallel application
run_filter() {
# The infile name is stored in positional argument 1
infile=$1
# Extract the sample name from the infile
sample_name=$(echo "$infile" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="highQuality/${EXPERIMENT_NAME}.${sample_name}"
out_quality="${prefix}.quality.fastq"
out_report="${prefix}.quality.report.txt"
# Run seqmagick quality-filter with the supplied options
seqmagick quality-filter \
--min-mean-quality $filter_q --min-length $filter_l \
$infile $out_quality > $out_report
}
# Export function and variables so that each subprocess can access them
export -f run_filter
export filter_q filter_l
export EXPERIMENT_NAME
# Run sickle in parallel for the input files
parallel --no-notice --jobs $THREADS run_filter ::: ${input_files_6[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 7: Remove rRNA and tRNA sequences
S=7
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Removing rRNA and tRNA sequences...\e[0m\n"
# Calculate number of threads to use
# Use lowest number out of THREADS and bowtie_p
bowtie_7_p=$(dc -e "[${THREADS}]sM ${bowtie_7_p}d ${THREADS}<Mp")
# Run bowtie1 for each input file
ls highQuality/*quality.fastq | while read infile
do
# Create output filenames for each input file
out_fastq=$(echo $infile | sed -e 's/highQuality/tANDrRNAremoval/' | \
sed -e 's/quality/tANDrRNAdeplete/')
out_sam=$(echo $out_fastq | sed -e 's/deplete\.fastq/.sam/')
out_report=$(echo $out_fastq | sed -e 's/\.fastq/.report.txt/')
# Run bowtie1
bowtie -a --best --strata -t -n $bowtie_7_n -l $bowtie_7_l \
-p $bowtie_7_p --un $out_fastq $bowtie_7_ref $infile /dev/null \
2> $out_report
done
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 8: Map reads to the genome
S=8
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Mapping reads to the genome...\e[0m\n"
# Calculate number of threads to use
# Use lowest number out of THREADS and bowtie_p
bowtie_8_p=$(dc -e "[${THREADS}]sM ${bowtie_8_p}d ${THREADS}<Mp")
# Run bowtie1 for each input file
ls tANDrRNAremoval/*tANDrRNAdeplete.fastq | while read infile
do
# Create output filenames for each input file
out_not=$(echo $infile | sed -e 's/tANDrRNAremoval/mapped/' | \
sed -e 's/tANDrRNAdeplete/notmapped/')
out_more=$(echo $out_not | sed -e 's/\.notmapped\./.moremapped./')
out_mapped=$(echo $out_not | sed -e 's/\.fastq/.bwt1/' | \
sed -e 's/\.notmapped\./.mapped./')
out_report=$(echo $out_not | sed -e 's/\.notmapped\.fastq/.report.txt/')
# Run bowtie1
bowtie -a --best --strata -m $bowtie_8_m -n $bowtie_8_n -l $bowtie_8_l \
-p $bowtie_8_p --un $out_not --max $out_more $bowtie_8_ref $infile \
$out_mapped 2> $out_report
done
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 9: Count the number of reads on read-occupied positions in genome (PARALLEL)
S=9
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Counting reads on read-occupied positions in genome...\e[0m\n"
# Store input filenames in array (specific for step 9; files from step 8)
input_files_9=(mapped/${EXPERIMENT_NAME}.*.mapped.bwt1)
# Define run_count function to be used with GNU parallel application
run_count() {
# The infile name is stored in positional argument 1
infile=$1
# Extract the sample name from the infile
sample_name=$(echo "$infile" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="readcount/${EXPERIMENT_NAME}.${sample_name}"
out_p="${prefix}.readCount.p"
out_m="${prefix}.readCount.m"
# Run the read count script with the supplied options
$readCountScript -i $infile --min $min_length --max $max_length \
--outP $out_p --outM $out_m
}
# Export function and variables so that each subprocess can access them
export -f run_count
export readCountScript min_length max_length
export EXPERIMENT_NAME
# Run read count in parallel for the input files
parallel --no-notice --jobs $THREADS run_count ::: ${input_files_9[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 10: Calculate total number of mapped reads (PARALLEL)
S=10
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Counting total number of mapped reads...\e[0m\n"
# Store input filenames in array (specific for step 10; files from step 9)
input_files_10=(readcount/${EXPERIMENT_NAME}.*.readCount.p)
# Define run_total function to be used with GNU parallel application
run_total() {
# The "p" infile name is stored in positional argument 1
infile_p=$1
# Reconstruct the corresponding "m" infile name
infile_m=$(echo $infile_p | sed -e 's/\.p$/.m/')
# Extract the sample name from the infile
sample_name=$(echo "$infile_p" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile name
prefix="readcount/${EXPERIMENT_NAME}.${sample_name}"
out="${prefix}.totalNbrMappedReads"
# Run the total read count script with the supplied options
$totalNbrMappedReadsScript --inP $infile_p --inM $infile_m --out $out
}
# Export function and variables so that each subprocess can access them
export -f run_total
export totalNbrMappedReadsScript
export EXPERIMENT_NAME
# Run total count in parallel for the input files
parallel --no-notice --jobs $THREADS run_total ::: ${input_files_10[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 11: Calculate RPM on read-occupied positions in genome (PARALLEL)
S=11
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Calculating RPM on read-occupied positions in genome...\e[0m\n"
# Store input filenames in array (specific for step 11; files from step 9)
input_files_11=(readcount/${EXPERIMENT_NAME}.*.readCount.p)
# Define run_rpm function to be used with GNU parallel application
run_rpm() {
# The "p" infile name is stored in positional argument 1
infile_p=$1
# Reconstruct the corresponding "m" infile name
infile_m=$(echo $infile_p | sed -e 's/\.p$/.m/')
# Reconstruct the corresponding "total" infile name (from step 10)
infile_tot=$(echo $infile_p | sed -e 's/\.readCount\.p$/.totalNbrMappedReads/')
# Extract the sample name from the infile
sample_name=$(echo "$infile_p" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="RPM/${EXPERIMENT_NAME}.${sample_name}"
out_p="${prefix}.RPM.p"
out_m="${prefix}.RPM.m"
# Run the read count script with the supplied options
$RPMscript --inP $infile_p --inM $infile_m --number $infile_tot \
--outP $out_p --outM $out_m
}
# Export function and variables so that each subprocess can access them
export -f run_rpm
export RPMscript
export EXPERIMENT_NAME
# Run total count in parallel for the input files
parallel --no-notice --jobs $THREADS run_rpm ::: ${input_files_11[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 12: Complete RPM list by assigning “0” to all unoccupied positions (PARALLEL)
S=12
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Completing RPM list with zeros...\e[0m\n"
# Store input filenames in array (specific for step 12; files from step 11)
input_files_12=(RPM/${EXPERIMENT_NAME}.*.RPM.p)
# Define run_complete function to be used with GNU parallel application
run_complete() {
# The "p" infile name is stored in positional argument 1
infile_p=$1
# Reconstruct the corresponding "m" infile name
infile_m=$(echo $infile_p | sed -e 's/\.p$/.m/')
# Extract the sample name from the infile
sample_name=$(echo "$infile_p" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="RPM/${EXPERIMENT_NAME}.${sample_name}"
out_p="${prefix}.RPM0.p"
out_m="${prefix}.RPM0.m"
# Run the zero count completion script with the supplied options
$RPMcompleteScript --inP $infile_p --inM $infile_m --inG $GENOME_FASTA \
--outP $out_p --outM $out_m
}
# Export function and variables so that each subprocess can access them
export -f run_complete
export RPMcompleteScript
export EXPERIMENT_NAME
export GENOME_FASTA
# Run zero count completion in parallel for the input files
parallel --no-notice --jobs $THREADS run_complete ::: ${input_files_12[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 13: Count the number of reads on every gene
S=13
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Counting the number of reads on every gene...\e[0m\n"
# Store input filenames in array (specific for step 13; files from step 9)
input_files_13=(readcount/${EXPERIMENT_NAME}.*.readCount.p)
# Define run_genes function to be used with GNU parallel application
run_genes() {
# The "p" infile name is stored in positional argument 1
infile_p=$1
# Reconstruct the corresponding "m" infile name
infile_m=$(echo $infile_p | sed -e 's/\.p$/.m/')
# Extract the sample name from the infile
sample_name=$(echo "$infile_p" | rev | cut -f 1 -d \/ | rev | cut -f 2 -d \.)
# Construct outfile names
prefix="readsPerGene/${EXPERIMENT_NAME}.${sample_name}"
out_p="${prefix}.readsPerGene.p"
out_m="${prefix}.readsPerGene.m"
# Run the reads per gene script with the supplied options
$readsPerGeneScript --inP $infile_p --inM $infile_m \
--listP $genelistP --listM $genelistM --outP $out_p --outM $out_m \
--inG $GENOME_FASTA
}
# Export function and variables so that each subprocess can access them
export -f run_genes
export readsPerGeneScript genelistP genelistM GENOME_FASTA
export EXPERIMENT_NAME
# Run reads per gene counting in parallel for the input files
parallel --no-notice --jobs $THREADS run_genes ::: ${input_files_13[@]}
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 14: Create CDS RPKM table
S=14
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Creating CDS RPKM table...\e[0m\n"
# Run script
$table_script $GENE_LIST $SHIFT
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi
################################################################################
# Step 15: Plot average gene profiles
S=15
# Check starting step
if [[ START_STEP -le S ]]
then
# Report progress
echo -e "\n\e[94mStep $S: Plotting average gene profiles...\e[0m\n"
# Run script
$profile_script $GENE_LIST $SHIFT
# Compress large output file
pigz analysis/gene_PauseScore_profiles.all_genes.tab
# Report step done
echo -e "\n\e[92mStep $S: Done.\e[0m\n"
else
# Report skip
echo -e "\n\e[31mStep $S: Skipping...\e[0m\n"
fi