-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsh_RNAseq.sh
executable file
·2735 lines (2310 loc) · 104 KB
/
sh_RNAseq.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# shellcheck disable=SC2028,SC2030,SC2031
#
# Usage: sh_RNAseq.sh </path/to/fastq(.gz)/folder> </path/to/destination/folder> [/path/to/config/file.ini] [OutputDirName]
#
##############################################################
## Description ##
##############################################################
#
# This script will process fastq(.gz) files and align them to
# a reference genome using either STAR (recommended), hishat2
# or tophat2.
# If STAR is used then RSEM will also be used and differential
# expression will be analysed using DESeq2.
# Differential expression can also be computed using cufflinks
# (cufflinks is pretty much deprecated, should be avoided
# unless trying to reproduce old results).
# Local Splicing Variation can now be computed using MAJIQ
# and/or LeafCutter.
# If a 4th '[OutputDirName]' argument is provided only the
# secondary analyses selected in the config file will be queued
# using the already aligned and processed files from a previous
# run, and results will be saved in a '_OutputDirName' directory.
#
## Options:
#
# --help : Display help message.
# --version : Display version number.
#
##############################################################
## Configurable variables ##
##############################################################
#
#
## General options
#
# Maximum number of threads (or CPUs) to request and allocate to programs.
# In some case less than this value may automatically be allowed.
threads=$(nproc --all --ignore=1)
#
# Maximum amount of memory (in GB) to request and allocate to programs.
# In some case less than this value may automatically be allowed.
mem="128"
#
# Log folder, will be created in your destination folder.
logs="logs"
#
# Advanced: Path to temporary folder. Useful if your cluster system have a fast local I/O disk.
# Leave empty to use the destination folder. In all cases temporary files will be purged at the end of the script.
tmp="\${L_SCRATCH_JOB}"
#
# Debug mode.
# For troubleshooting only. Will keep all intermediate files,
# sbatch files and logs.
# 0 = No ; 1 = Yes
debug="0"
#
#
## SLURM options
#
# email to use to receive SLURM notifications.
# By default only send FAIL notifications for all jobs and FAIL,END for the last one.
SLURMemail=""
#
# SLURM account, partition and qos settings if required.
SLURMaccount=""
SLURMpartition=""
SLURMqos=""
#
# Custom commands to run before any other program.
# Use it to load modules for example:
# customcmd="module load R"
customcmd=""
#
#
## FastQ options
#
# If sample files are split into multiple FastQ files, merge them.
# 0 = No ; 1 = Yes
merge="0"
#
# Process undetermined/unmatched files (tag not properly recognized).
# 0 = No ; 1 = Yes
underdet="0"
#
# Process BLANK files (Sample name should be "BLANK").
# 0 = No ; 1 = Yes
blank="0"
#
# Perform fastqc on fastq files.
# If sequences are trimmed, fastqc will be performed on trimmed sequences.
# 0 = No ; 1 = Yes
fastqc="0"
#
# Trim sequences with Trimmomatic.
# 0 = No ; 1 = Yes
trim="0"
#
# Trimmomatic folder, should contain a trimmomatic.jar file
Trimmomatic="/Tools/Trimmomatic"
#
# Keep unpaired trimmed sequences.
# 0 = No ; 1 = Yes
unpaired="0"
#
#
## Alignment options
#
# Select which software to use for alignment jobs.
# This should be installed in your $PATH and corresponding indexes need to be created.
# Valid values: star, hisat2, tophat2
align="star"
#
# tophat2 (bowtie2) indexed reference genome location.
th_refgenome="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Sequence/Bowtie2Index/genome"
#
# hisat2 indexed reference genome location.
ha_refgenome="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Sequence/Hisat2Index/genome"
#
# STAR indexed reference genome location.
st_refgenome="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Sequence/StarIndex/"
#
# RSEM indexed reference genome location.
rs_refgenome="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Sequence/RsemIndex/genome"
#
# Annotation files folder.
# It must be compatible with the indexed reference genome used for alignment.
# This folder may contain your gtf / gff3 files.
# If tophat2 is used it should contains the known.gff, known.fa, etc…, files
# else they will be generated during the first run.
annotations="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Annotation/Genes"
#
# GTF formated annotation file.
# By default looks for genes.gtf located in the ${annotation} folder.
gtf="${annotations}/genes.gtf"
#
# Reference genome (fasta) file.
# It must be compatible with your indexed genomes, and annotation files.
fasta_refgenome="/Tools/RefGenomes/Homo_sapiens/UCSC/hg19/Sequence/WholeGenomeFasta/genome.fa"
#
# Read group parameters.
# Library: if empty LB will be set to the destination folder name.
LB=""
#
# Platform used to produce the reads.
# Valid values: CAPILLARY, LS454, ILLUMINA, SOLID, HELICOS, IONTORRENT and PACBIO.
PL="ILLUMINA"
#
#
## Differential Expression options
#
# Select which software to use for differential expression jobs. Leave empty if not needed.
# Corresponding software or modules should be installed in your $PATH
# Valid values: deseq2, cufflinks, both
diffexp="deseq2"
#
# Delimit replicate samples by a COMMA and groups by a SPACE. Sample name is case sensitive.
# example:
# groupedsamples="1d-Control-Rep1,1d-Control-Rep2,1d-Control-Rep3 5d-Control-Rep1,5d-Control-Rep2,5d-Control-Rep3 1d-Experiment-Rep1,1d-Experiment-Rep2,1d-Experiment-Rep3 5d-Experiment-Rep1,5d-Experiment-Rep2,5d-Experiment-Rep3"
groupedsamples=""
#
# Delimit group labels by a COMMA. Groups should be named in the same order than the samples.
# example:
# labels="1d-Control,5d-Control,1d-Experiment,5d-Experiment"
labels=""
#
#
## Local Splicing Variation options
#
# Select which software to use for LSV jobs. Leave empty if not needed.
# Corresponding software or modules should be installed in your $PATH
# or location configured properly below.
# Valid values: majiq, leafcutter, both
splicing="majiq"
#
# MAJIQ options
#
# Length of the RNA-seq reads. Not needed since MAJIQ 2.2, uncomment in the code if using an older MAJIQ version.
# MAJIQ can handle experiments with multiple read lengths, just indicate the longest read length.
# Leave empty for it to be automatically detected, can be overwritten here if needed.
# example:
# readlength="250"
#readlength=""
#
# Preparation strandness.
# Some of the RNASeq preparations are strand specific.
# This preparations can be reverse strand specific [reverse],
# forward strand specific [forward], or non-strand specific [none].
# This parameter is optional, in which case [none] is used as default.
# Valid values: forward, reverse, none
strandness="none"
#
# Transcriptome file with the annotation database to be used with MAJIQ.
# It must be compatible with the indexed reference genome used for alignment.
# Currently, only accepts GFF3 format. By default looks for genes.gff3 located in the ${annotation} folder.
gff3="${annotations}/genes.gff3"
#
# Genome assembly.
# Name of the genome assembly used. Can also be determined automatically from your directory structure (see examples).
# examples:
# genomebuild="hg19"
# genomebuild=$(echo ${gff3} | sed 's/\/Annotation.*//g' | awk -F/ '{print $NF}')
genomebuild=$(echo ${gff3} | sed 's/\/Annotation.*//g' | awk -F/ '{print $NF}')
#
# Perform PSI quantification.
# 0 = No ; 1 = Yes
psi="0"
#
# LeafCutter options
#
# LeafCutter folder, should contain the scripts, clustering and leafviz folders.
leafCutterDir="/Tools/leafcutter"
#
# Optional exon_file location.
# For more details see http://davidaknowles.github.io/leafcutter/articles/Usage.html
exonfile="${annotations}/leafcutter_exons.txt.gz"
#
# Annotation code for Leafcutter Shiny App.
# path to annotation files and <annotation_code> used in step 0 of
# http://davidaknowles.github.io/leafcutter/articles/Visualization.html
# From Step 1 concrete example section this would be:
# anncode="../leafviz/annotation_codes/gencode_hg19/gencode_hg19"
anncode="${annotations}/leafcutter"
#
#
## Notifications options
#
# Event name used in your webhooks or IFTTT recipes.
# The IFTTT maker channel will look for the combination private key + event name to then trigger your recipe.
# You can then create a recipe to send an email, a text message or a push notification.
# In the case of a Slack webhook this will be used in the message title
notifevent="RNAseq"
#
# Trigger a Slack Webhook when script is done.
# You must create an "Incoming WebHooks" associated to your slack workspace on https://slack.com/apps
# Copy your full private webhook URL here. Leave blank to disable this function.
# slackwebhookURL="https://hooks.slack.com/services/T5HF5GTUK85/AHUIK456HJG/GSD27f5gGQ7SD5r2fg" # Not a real webhook URL, you have to use your own private one.
slackwebhookURL=""
#
# Trigger IFTTT when script is done.
# You must register the "Maker channel" on https://ifttt.com/maker and create an event (limit of 3 free event since September 2020...)
# Copy your private key here. Leave blank to disable this function.
# iftttkey="AbCd_15CdhUIvbsFJTHGMcfgjsdHRTgcyjt" # Not a real key, you have to use your own private one.
iftttkey=""
#
#
## Setup done. You should not need to edit below this point ##
# Help!
if [ "${1}" = "--help" ] || [ "${2}" = "--help" ] || [ "${3}" = "--help" ] || [ "${4}" = "--help" ]
then
echo "Usage: $(basename "${0}") </path/to/fastq(.gz)/folder> </path/to/destination/folder> [/path/to/config/file.ini] [OutputDirName]"
echo ""
echo "Description"
echo ""
echo "This script will process fastq(.gz) files and align them to"
echo "a reference genome using either STAR (recommended), hishat2"
echo "or tophat2."
echo "If STAR is used then RSEM will also be used and differential"
echo "expression will be analysed using DESeq2."
echo "Differential expression can also be computed using Cufflinks"
echo "(Cufflinks is pretty much deprecated, should be avoided"
echo "unless trying to reproduce old results)"
echo "If a 4th '[OutputDirName]' argument is provided"
echo "only the secondary analyses selected in the config file will be queued"
echo "using the already aligned and processed files from a previous run, "
echo "and results will be saved in a '_OutputDirName' directory."
echo ""
echo "Options:"
echo "$(basename "${0}") --help : Display this help message."
echo "$(basename "${0}") --version : Display version number."
echo ""
exit
fi
# Version
if [ "${1}" = "--version" ] || [ "${2}" = "--version" ] || [ "${3}" = "--version" ] || [ "${4}" = "--version" ]
then
echo "$(basename "${0}") version 2.1.2"
echo "Now easier to add analyses in a rerun and improved default R graphs (2.1.1)"
echo "Alternative splicing support (2.1)"
echo "DESeq2 and Single-Read support (2.0)"
exit
fi
# Get fastq directory
dir="${1}"
# Get destination directory
dir2="${2}"
# Get config file location
config="${3}"
# Check paths and trailing / in directories
if [ -z "${dir}" ] || [ -z "${dir2}" ]
then
${0} --help
exit
fi
if [ "${dir: -1}" = "/" ]
then
dir=${dir%?}
fi
if [ "${dir2: -1}" = "/" ]
then
dir2=${dir2%?}
fi
if [ -n "${config}" ]
then
if [ "${config: -4}" = ".ini" ]
then
# shellcheck disable=SC1090
source "${config}"
else
echo "Invalid config file detected. Is it an .ini file?"
echo ""
${0} --help
exit
fi
fi
if [ -z "${labels}" ] || [ -z "${groupedsamples}" ]
then
echo "Error: both \"groupedsamples\" and \"labels\" variables need to be configured properly"
exit
fi
if [ -n "${4}" ]
then
reanalysis="_${4}"
fi
# Test if sequence files are .fastq or .fastq.gz
fastqgz=$(find -L "${dir}" -maxdepth 1 -name '*.fastq.gz')
fastq=$(find -L "${dir}" -maxdepth 1 -name '*.fastq')
if [ -z "${fastqgz}" ] && [ -z "${fastq}" ]
then
echo ""
echo "No .fastq or .fastq.gz files are present in ${dir}/"
exit
fi
if [ -n "${fastqgz}" ] && [ -n "${fastq}" ]
then
echo ""
echo "Both .fastq and .fastq.gz files are present in ${dir}/"
echo "Existing .fastq.gz files will now be converted to .fastq files"
gunzip "${dir}/*.fastq.gz"
fileext=".fastq"
fi
if [ -n "${fastqgz}" ] && [ -z "${fastq}" ]
then
fileext=".fastq.gz"
fi
if [ -n "${fastq}" ] && [ -z "${fastqgz}" ]
then
fileext=".fastq"
fi
# Displaying variables to shell
echo ""
echo "-- Informations --"
echo ""
if [ -z "${config}" ]
then
echo "You can change the following parameters by using a custom config file"
else
echo "You are using a custom config file: ${config}"
fi
echo "Your ${fileext} files are located in ${dir}/"
echo "Final results will be located in ${dir2}/"
echo "Log files will be created in ${dir2}/${logs}"
echo ""
echo "-- Alignment options --"
echo ""
if [ "${align}" = "star" ]
then
echo "Using ${st_refgenome} as reference genome"
elif [ "${align}" = "hisat2" ]
then
echo "Using ${ha_refgenome} as reference genome"
elif [ "${align}" = "tophat2" ]
then
echo "Using ${th_refgenome} as reference genome"
fi
if [ "${underdet}" = "0" ]
then
echo "Undetermined files will not be processed"
fi
if [ "${blank}" = "1" ]
then
echo "\"BLANK\" files will be processed"
fi
if [ "${merge}" = "1" ]
then
echo "Individual files will also be merged into a big \"MERGED\" file"
fi
if [ "${trim}" = "1" ]
then
echo "Sequence will be trimmed"
echo "Trimmomatic is installed in ${Trimmomatic}"
fi
if [ -n "${diffexp}" ]
then
echo ""
echo "-- Differential Expression options --"
echo ""
echo "Differential expression will be analysed using $(if [ "${diffexp}" = "deseq2" ] || [ "${diffexp}" = "both" ]; then echo "DESeq2"; fi)$(if [ "${diffexp}" = "both" ]; then echo " and "; fi)$(if [ "${diffexp}" = "cufflinks" ] || [ "${diffexp}" = "both" ]; then echo "Cufflinks"; fi)."
if [ "${diffexp}" = "cufflinks" ] || [ "${diffexp}" = "both" ]
then
echo "WARNING - The use of Cufflinks for RNAseq analysis is pretty much deprecated, should be avoided - WARNING"
fi
fi
if [ -n "${splicing}" ]
then
echo ""
echo "-- Local Splicing Variation options --"
echo ""
echo "Local Splicing Variants will be analysed using $(if [ "${splicing}" = "majiq" ] || [ "${splicing}" = "both" ]; then echo "MAJIQ"; fi)$(if [ "${splicing}" = "both" ]; then echo " and "; fi)$(if [ "${splicing}" = "leafcutter" ] || [ "${splicing}" = "both" ]; then echo "LeafCutter"; fi)."
fi
echo ""
echo "-- Hardware --"
echo ""
echo "Up to ${threads} CPUs will be used"
echo "Up to ${mem}GB of memory will be allocated to the programs"
echo ""
echo "---------------"
# Initialize
mkdir -p "${dir2}/"
[ -f "${dir2}/files1" ] && rm "${dir2}/files1"
[ -f "${dir2}/files2" ] && rm "${dir2}/files2"
[ -f "${dir2}/Fastqs" ] && rm "${dir2}/Fastqs"
[ -f "${dir2}/matrices" ] && rm "${dir2}/matrices"
[ -f "${dir2}/assembly_GTF_list.txt" ] && rm "${dir2}/assembly_GTF_list.txt"
[ -d "${dir2}/logs" ] && rm -rf "${dir2}/logs"
mkdir -p "${dir2}/${logs}"
if [ -z "${tmp}" ]
then
tmp="${dir2}/tmp"
fi
# Concatenate files if split
if [ "${merge}" = "1" ]
then
echo ""
echo "-- Merging files --"
echo ""
mkdir -p "${dir}/FastQbackup/"
filestomergeR1=$(ls "${dir}"/*_L[0-9][0-9][0-9]_R1)
filestomergeR2=$(ls "${dir}"/*_L[0-9][0-9][0-9]_R2)
echo "Processing R1 files"
echo ""
for i in ${filestomergeR1}
do
sampleoutput="${i//_L[0-9][0-9][0-9]_R1/_R1}"
echo "Processing ${i}"
cat "${i}" >> "${sampleoutput}"
mv "${i}" "${dir}/FastQbackup/"
done
echo ""
echo "Processing R2 files"
echo ""
for i in ${filestomergeR2}
do
sampleoutput="${i//_L[0-9][0-9][0-9]_R2/_R2}"
echo "Processing ${i}"
cat "${i}" >> "${sampleoutput}"
mv "${i}" "${dir}/FastQbackup/"
done
echo ""
echo "-- Files merged --"
fi
if [ "${underdet}" = "0" ]
then
if [ "${blank}" = "0" ]
then
# Remove all Undetermined_* and BLANK* files
find -L "${dir}" -maxdepth 1 -name '*_R1*' -not -name '*ndetermined*' -not -name '*nmatched*' -not -name 'BLANK*' | sed 's#.*/##' | sort -n > "${dir2}/files1"
find -L "${dir}" -maxdepth 1 -name '*_R2*' -not -name '*ndetermined*' -not -name '*nmatched*' -not -name 'BLANK*' | sed 's#.*/##' | sort -n > "${dir2}/files2"
paste "${dir2}/files1" "${dir2}/files2" > "${dir2}/Fastqs"
else
# Remove all Undetermined_* files
find -L "${dir}" -maxdepth 1 -name '*_R1*' -not -name '*ndetermined*' -not -name '*nmatched*' | sed 's#.*/##' | sort -n > "${dir2}/files1"
find -L "${dir}" -maxdepth 1 -name '*_R2*' -not -name '*ndetermined*' -not -name '*nmatched*' | sed 's#.*/##' | sort -n > "${dir2}/files2"
paste "${dir2}/files1" "${dir2}/files2" > "${dir2}/Fastqs"
fi
else
if [ "${blank}" = "0" ]
then
# Remove all BLANK* files
find -L "${dir}" -maxdepth 1 -name '*_R1*' -not -name 'BLANK*' | sort -n | sed 's#.*/##' > "${dir2}/files1"
find -L "${dir}" -maxdepth 1 -name '*_R2*' -not -name 'BLANK*' | sort -n | sed 's#.*/##' > "${dir2}/files2"
paste "${dir2}/files1" "${dir2}/files2" > "${dir2}/Fastqs"
else
# Process all the files!
find -L "${dir}" -maxdepth 1 -name '*_R1*' | sed 's#.*/##' | sort -n > "${dir2}/files1"
find -L "${dir}" -maxdepth 1 -name '*_R2*' | sed 's#.*/##' | sort -n > "${dir2}/files2"
paste "${dir2}/files1" "${dir2}/files2" > "${dir2}/Fastqs"
fi
fi
######################
## Processing samples
######################
if [ -z "${reanalysis}" ]
then
echo ""
echo "-- Queuing sample jobs --"
while read -r line;
do
# General variables
read1=$(echo "${line}" | cut -f1)
read2=$(echo "${line}" | cut -f2)
samplename=$(echo "${read1}" | awk -F_R1 '{print $1}')
if [ "${read1}" != "${read2}" ]
then
pairedend="1"
fi
echo ""
echo -e "\t-- Processing" "${samplename}"
echo ""
######################
## Prepare and queue trimming job
job="trim"
# Variables
trimout1="${dir2}/$(basename "${read1}" ${fileext}).trim.fastq"
trimout2="${dir2}/$(basename "${read2}" ${fileext}).trim.fastq"
if [ -n "${unpaired}" ] && [ "${unpaired}" = "1" ]
then
unpaired1="${dir2}/$(basename "${read1}" ${fileext}).unpaired.fastq" # Save unpaired reads
unpaired2="${dir2}/$(basename "${read2}" ${fileext}).unpaired.fastq" # Save unpaired reads
else
unpaired1="/dev/null" # Unpaired reads are discarded
unpaired2="/dev/null" # Unpaired reads are discarded
fi
if [ -n "${trim}" ] && [ "${trim}" = "1" ]
then
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH $(if [ -n "${mem}" ] && [ ${mem} -gt "8" ]; then echo "--mem=8000"; else echo "--mem=${mem}000"; fi) $(if [ -n "${threads}" ] && [ "${threads}" -gt "2" ]; then echo "--cpus-per-task=2"; else echo "--cpus-per-task=${threads}"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=2:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
# Trim fastq files with trimmomatic
if [ -n "${pairedend}" ] && [ "${pairedend}" = "1" ]
then
echo "java -Xmx$(if [ -n "${mem}" ] && [ ${mem} -gt "8" ]; then echo "8"; else echo "${mem}"; fi)g -Djava.io.tmpdir=\"${tmp}\" -jar ${Trimmomatic}/trimmomatic.jar PE -threads $(if [ -n "${threads}" ] && [ "${threads}" -gt "2" ]; then echo "2"; else echo "${threads}"; fi) -phred33 ${dir}/${read1} ${dir}/${read2} ${trimout1} ${unpaired1} ${trimout2} ${unpaired2} ILLUMINACLIP:${Trimmomatic}/adapters/TruSeq3-PE.fa:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:36 || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
else
echo "java -Xmx$(if [ -n "${mem}" ] && [ ${mem} -gt "8" ]; then echo "8"; else echo "${mem}"; fi)g -Djava.io.tmpdir=\"${tmp}\" -jar ${Trimmomatic}/trimmomatic.jar SE -threads $(if [ -n "${threads}" ] && [ "${threads}" -gt "2" ]; then echo "2"; else echo "${threads}"; fi) -phred33 ${dir}/${read1} ${trimout1} ILLUMINACLIP:${Trimmomatic}/adapters/TruSeq3-SE.fa:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:36 || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
fi
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
fi
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBtrim=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
echo -e "\t Trimming job queued"
else
# Need to rename some variables
trimout1="${dir}/${read1}"
trimout2="${dir}/${read2}"
fi
######################
## Align reads with hisat2 / tophat2 / star
job="align"
# Variables
samout=$(basename "${read1}" | sed "s/_R1${fileext}/.sam/g")
bamout=$(basename "${read1}" | sed "s/_R1${fileext}/.bam/g")
bamsortedout=$(basename "${read1}" | sed "s/_R1${fileext}/.sorted.bam/g")
if [ -z "$LB" ]
then
LB=$(basename "${dir2}")
fi
SM=$(echo "${read1}" | awk -F_ '{print $1}')
if [ "${fileext}" = ".fastq.gz" ]
then
CN=$(gzip -cd "${dir}/${read1}" | head -n 1 | awk -F: '{print $1}' | sed 's/@//')
PU=$(gzip -cd "${dir}/${read1}" | head -n 1 | awk -F: '{print $3}')
else
CN=$(head -n 1 "${dir}/${read1}" | awk -F: '{print $1}' | sed 's/@//')
PU=$(head -n 1 "${dir}/${read1}" | awk -F: '{print $3}')
fi
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=8:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMaccount}" ]
then
echo "#SBATCH --account=${SLURMaccount}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# Require previous job successful completion
if [ -n "${trim}" ] && [ "${trim}" = "1" ]
then
echo "#SBATCH --dependency=afterok:${SBtrim##* }"
fi
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
if [ ${align} = "star" ]
then
# star alignment job using ENCODE standard options
echo "STAR --runMode alignReads --runThreadN ${threads} --limitBAMsortRAM ${mem}000000000 --outTmpDir \"${tmp}\"/${samplename}_aRtmp --twopassMode Basic --genomeDir ${st_refgenome} --readFilesIn ${trimout1} $(if [ -n "${pairedend}" ] && [ "${pairedend}" = "1" ]; then echo "${trimout2}"; fi) $(if [ -n "${fastqgz}" ] && [ ${trim} -ne "1" ]; then echo "--readFilesCommand zcat"; fi) --outFilterType BySJout --outFilterMultimapNmax 20 --alignSJoverhangMin 8 --alignSJDBoverhangMin 1 --outFilterMismatchNmax 999 --outFilterMismatchNoverReadLmax 0.04 --alignIntronMin 20 --alignIntronMax 1000000 --alignMatesGapMax 1000000 --quantMode TranscriptomeSAM GeneCounts --alignEndsType EndToEnd --outSAMstrandField intronMotif --outFilterIntronMotifs RemoveNoncanonical --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${dir2}/${samplename}. || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
echo "mv ${dir2}/${samplename}.Aligned.sortedByCoord.out.bam ${dir2}/${bamsortedout}"
elif [ ${align} = "hisat2" ]
then
# hisat2 alignment job
echo "hisat2 -p ${threads} --phred33 --dta-cufflinks --no-softclip --rg-id ${LB}_${SM} --rg CN:${CN} --rg LB:${LB} --rg PL:${PL} --rg PU:${PU} --rg SM:${SM} -x ${ha_refgenome} -1 ${trimout1} $(if [ -n "${pairedend}" ] && [ "${pairedend}" = "1" ]; then echo "-2 ${trimout2}"; fi) -S ${dir2}/${samout} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
elif [ ${align} = "tophat2" ]
then
# tophat2 alignment job
echo "tophat2 -G ${gtf} --transcriptome-index ${annotations}/known -p ${threads} -o ${dir2} ${th_refgenome} ${trimout1} $(if [ -n "${pairedend}" ] && [ "${pairedend}" = "1" ]; then echo "${trimout2}"; fi) || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
fi
if [ ${align} = "hisat2" ] || [ ${align} = "tophat2" ]
then
# Convert .sam to .bam
echo "samtools view -bS -o ${dir2}/${bamout} ${dir2}/${samout} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
# Sort .bam file
echo "samtools sort -@ ${threads} -o ${dir2}/${bamsortedout} -O bam -T \"${tmp}\" ${dir2}/${bamout} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
fi
# Index sorted .bam
echo "samtools index ${dir2}/${bamsortedout} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
# Cleaning commands
# Remove trim.fastq files from destination folder
if [ ${trim} = "1" ] && [ ${fastqc} = "0" ]
then
echo "rm ${trimout1} ${trimout2}"
fi
# Remove .sam file
echo "rm ${dir2}/${samout}"
# Remove unsorted .bam file
echo "rm ${dir2}/${bamout}"
# Clean STAR intermediate files
echo "rm -rf ${dir2}/${samplename}.Log.progress.out ${dir2}/${samplename}._STARgenome ${dir2}/${samplename}._STARpass1"
echo "mv ${dir2}/${samplename}.Log.out ${dir2}/${logs}/${samplename}.star.log"
echo "mv ${dir2}/${samplename}.Log.final.out ${dir2}/${logs}/${samplename}.star.stats"
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBalign=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
SBalignIDs=${SBalignIDs}:${SBalign##* }
echo -e "\t Alignment job queued"
######################
## Run fastqc to generate quality control files
job="fqc"
if [ "${fastqc}" = "1" ]
then
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH $(if [ -n "${threads}" ] && [ "${threads}" -gt "1" ]; then echo "--cpus-per-task=1"; else echo "--cpus-per-task=${threads}"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=1:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# Require previous job successful completion
if [ -n "${SBtrim}" ]
then
echo "#SBATCH --dependency=afterok:${SBalign##* }"
fi
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "fastqc -o ${dir2}/ --noextract ${trimout1} ${trimout2} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove trim.fastq files from destination folder
if [ ${trim} = "1" ]
then
echo "rm ${trimout1} ${trimout2}"
fi
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
fi
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBfqc=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBfqc##* }" ]
then
SBfqcIDs=${SBfqcIDs}:${SBfqc##* }
fi
echo -e "\t FastQC job queued"
fi
######################
## Count reads
job="rsem-calcexp"
if [ "${align}" = "star" ] && { [ "${diffexp}" = "deseq2" ] || [ "${diffexp}" = "both" ]; }
then
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=8:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMaccount}" ]
then
echo "#SBATCH --account=${SLURMaccount}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# Require previous job successful completion
echo "#SBATCH --dependency=afterok:${SBalign##* }"
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "rsem-calculate-expression -p ${threads} --temporary-folder \"${tmp}\"/${samplename} $(if [ -n "${pairedend}" ] && [ "${pairedend}" = "1" ]; then echo "--paired-end"; fi) --bam --no-bam-output --calc-ci ${dir2}/${samplename}.Aligned.toTranscriptome.out.bam ${rs_refgenome} ${dir2}/${samplename}.rsem || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
echo "mv ${dir2}/${samplename}.rsem.stat ${dir2}/${samplename}.rsem"
# Cleaning commands
if [ "${debug}" != "1" ]
then
echo "rm ${dir2}/${samplename}.Aligned.toTranscriptome.out.bam"
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
fi
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBrsem=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
SBrsemIDs=${SBrsemIDs}:${SBrsem##* }
echo -e "\t RSEM counting job queued"
fi
######################
## Assemble transcriptomes
job="cufflinks"
if [ "${diffexp}" = "cufflinks" ] || [ "${diffexp}" = "both" ]
then
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=8:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMaccount}" ]
then
echo "#SBATCH --account=${SLURMaccount}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# Require previous job successful completion
echo "#SBATCH --dependency=afterok:${SBalign##* }"
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
# Create a sample specific folder, and as cufflinks do not allow to specify an output directory then cd to it.
echo "mkdir -p ${dir2}/${samplename}.cuff"
echo "cd ${dir2}/${samplename}.cuff || exit"
# Create cufflinks job
echo "cufflinks -u -p ${threads} -g ${gtf} -b ${fasta_refgenome} ${dir2}/${bamsortedout} || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
fi
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBcufflinks=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
SBcufflinksIDs=${SBcufflinksIDs}:${SBcufflinks##* }
echo -e "\t Cufflinks job queued"
fi
######################
## LeafCutter convert bams to juncs
job="leaf-junc"
if [ "${splicing}" = "leafcutter" ] || [ "${splicing}" = "both" ]
then
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${job}_${samplename} --output=${dir2}/${logs}/${job}_${samplename}.out --error=${dir2}/${logs}/${job}_${samplename}.err --open-mode=append"
echo "#SBATCH $(if [ -n "${threads}" ] && [ "${threads}" -gt "1" ]; then echo "--cpus-per-task=1"; else echo "--cpus-per-task=${threads}"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=2:00:00"
if [ -n "${SLURMemail}" ]
then
echo "#SBATCH --mail-type=FAIL --mail-user=${SLURMemail}"
fi
if [ -n "${SLURMaccount}" ]
then
echo "#SBATCH --account=${SLURMaccount}"
fi
if [ -n "${SLURMpartition}" ]
then
echo "#SBATCH --partition=${SLURMpartition}"
fi
if [ -n "${SLURMqos}" ]
then
echo "#SBATCH --qos=${SLURMqos}"
fi
# Require previous job successful completion
echo "#SBATCH --dependency=afterok:${SBalign##* }"
# General commands
echo "mkdir -p \"${tmp}\""
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
# adapted from https://github.com/davidaknowles/leafcutter/blob/master/scripts/bam2junc.sh
echo "samtools view ${dir2}/${samplename}.sorted.bam | ${leafCutterDir}/scripts/filter_cs.py | ${leafCutterDir}/scripts/sam2bed.pl --use-RNA-strand - ${dir2}/${samplename}.bed || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
echo "${leafCutterDir}/scripts/bed2junc.pl ${dir2}/${samplename}.bed ${dir2}/${samplename}.junc || if [ -f ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err ]; then echo \"\${SLURM_JOB_NODELIST}\" >> ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && exit 1; else echo \"\${SLURM_JOB_NODELIST}\" > ${dir2}/\"\${SLURM_JOBID}\"-${job}_${samplename}.err && scontrol requeue \"\${SLURM_JOBID}\" && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
echo "rm ${dir2}/${samplename}.bed"
# Remove .sbatch
echo "rm ${dir2}/${job}_${samplename}.sbatch"
fi
echo "exit 0"
} > "${dir2}/${job}_${samplename}.sbatch"
# Queue job
SBleafjunc=$(until sbatch "${dir2}/${job}_${samplename}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
SBleafjuncIDs=${SBleafjuncIDs}:${SBleafjunc##* }
echo -e "\t JUNC conversion job queued"
fi
done < "${dir2}/Fastqs"
fi # End of initial run of sample specific jobs