-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsh_CRISPR.sh
executable file
·1725 lines (1460 loc) · 60.7 KB
/
sh_CRISPR.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_CRISPR.sh </path/to/fastq(.gz)/folder> </path/to/destination/folder> [/path/to/config/file.ini]
#
##############################################################
## Description ##
##############################################################
#
# This script will process the fastq(.gz) files generated in
# a typical CRISPR screen using either casTLE or MAGeCK.
#
# If using casTLE, a reference file of all the Indices will be
# automatically created using bowtie (NOT bowtie2).
# It will then use casTLE scripts to analyze the screen and
# generate basic graphs
# Download casTLE from https://bitbucket.org/dmorgens/castle/
#
# If using MAGeCK counts, tests, mle and pathway analysis
# will be performed. It will also run the R package MAGeCKFlute
# and in all cases generate basic graphs.
# Download MAGeCK from https://sourceforge.net/projects/mageck/
#
## 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=""
#
#
## Screen options
#
# day0-label.
# Specify the label for control sample (usually day 0 or plasmid).
# If using MAGeCK it will also turn on the negative selection QC for every other sample label.
# The negative selection QC will compare each other sample with day0 sample, and thus estimate the degree of negative selections in essential genes.
# If using casTLE, only the first TWO replicates will be used.
# day0="Plasmid,Control_t0_rep1,Control_t0_rep2,Treated_t0_rep1,Treated_t0_rep2"
day0=""
#
# Tests groups.
# Enter your sample names (not including ".fastq" or ".fastq.gz") for comparisons.
# Delimit replicate samples by a COMMA and groups by a SPACE.
# If using casTLE, only the first TWO replicates will be used.
# testgroups="Control_rep1,Control_rep2 Treated_condA_rep1,Treated_condA_rep Treated_condB_rep1,Treated_condB_rep2"
testgroups=""
#
#
## casTLE options
# download the last version from https://bitbucket.org/dmorgens/castle/
#
# Use casTLE.
# 0 = No ; 1 = Yes
usecastle="0"
#
# Python version.
# Are you using python2.7 (original) or python3 (included in this repo) casTLE scripts?
# If you also use MAGeCK python3 is REQUIRED
# use "python" or "python3"
python="python3"
#
# casTLE folder location.
castlepath="/Tools/dmorgens-castle"
#
# Number of permutations to generate p-values.
# For a first pass, use 5x the permutations (so for 20,000 genes that is 100,000 permutations).
# For publication, use 50x (Default, takes time).
permutations="1000000"
#
# Perform permutations on the individual result files.
# Permutations are always calculated for the combo file.
# 0 = No ; 1 = Yes
permres="0"
#
# Output format for graphs.
# If left empty default value is png.
# (png, pdf, eps)
graphformat="pdf"
#
# Screen performed in mouse cells.
# Default is 0, for human cells.
# 0 = No ; 1 = Yes
mouse="0"
#
# Path to bowtie, bowtie-build needs to be in the same folder (probably is already the case).
# bowtie="/usr/local/bin/bowtie"
bowtie="/usr/local/bin/bowtie"
#
# Type of screen. Will be used to create Indices for the guides.
screentype="Cas9-10"
#
# Name of the guides index file. Will be saved in the Indices folder.
# It will overwrite any files with this name prefix.
outputbowtieindex=""
#
# Oligo file location.
# Leave empty if it was previously used and the corresponding Index are already generated for this type of screen.
oligofile=""
#
#
## MAGeCK options.
# download the last version from https://sourceforge.net/projects/mageck/
#
# Use MAGeCK.
# 0 = No ; 1 = Yes
usemageck="1"
#
# MAGeCK list of sgRNA names (see https://sourceforge.net/p/mageck/wiki/input/#sgrna-library-file ) location.
magecksgRNAlibrary=""
#
# Use the reverse complement of the MAGeCK list of sgRNA names.
# 0 = No ; 1 = Yes
mageckrevcomplib="0"
#
# MAGeCK list of control sgRNA names (see https://sourceforge.net/p/mageck/wiki/input/#negative-control-sgrna-list ) location.
mageckcontrolsgrna=""
#
# GMT file for MAGeCK pathway analysis (see https://sourceforge.net/p/mageck/wiki/input/#pathway-file-gmt ) location.
gmtfile=""
#
# Matrix file for mle analysis ( see https://sourceforge.net/p/mageck/wiki/input/#design-matrix-file ) location.
# By default will look for a "matrix.txt" file stored with the FastQ files.
matrixfile=$([ -f "${1}"/matrix.txt ] && echo "${1}/matrix.txt")
#
#
## 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="CRISPR"
#
# 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]"
echo ""
echo "Description"
echo ""
echo "This script will process the fastq(.gz) files generated in"
echo "a typical CRISPR screen using either casTLE or MAGeCK."
echo ""
echo "If using casTLE a reference file of all the Indices will be"
echo "automatically created using bowtie (NOT bowtie2)."
echo "It will then use casTLE scripts to analyze the screen and"
echo "generate basic graphs"
echo "Download casTLE from https://bitbucket.org/dmorgens/castle/"
echo ""
echo "If using MAGeCK counts, tests, mle and pathway analysis"
echo "will be performed. It will also run the R package MAGeCKFlute"
echo "and in all cases generate basic graphs."
echo "Download MAGeCK from https://sourceforge.net/projects/mageck/"
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.0.2"
echo "Major code cleaning (2.0.1)"
echo "casTLE and MAGeCK 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 "${usecastle}" ] || [ -z "${usemageck}" ]
then
if [ -z "${usecastle}" ]
then
usecastle="0"
else
usemageck="0"
fi
fi
if [ ${usecastle} -eq "0" ] && [ ${usemageck} -eq "0" ]
then
echo "Neither casTLE or MAGeCK are set up to be used right now, edit your config file."
echo ""
exit
fi
if [ ${usecastle} -eq "1" ] ## casTLE checks
then
if [ -n "${oligofile}" ]
then
if [ "${oligofile: -4}" != ".csv" ]
then
echo "Invalid population oligo file file detected. Is ${oligofile} a .csv file?"
echo ""
exit
fi
fi
if [ -n "${testgroups}" ]
then
IFS=" " read -r -a testsarray <<< "${day0} ${testgroups}"
for replicates in "${testsarray[@]}"
do
IFS="," read -r -a replicatesarray <<< "${replicates}"
if [ "${#replicatesarray[@]}" -gt "2" ]
then
echo ""
echo "WARNING"
echo "This casTLE pipeline is only designed to work with a maximum of TWO replicates, only the first 2 replicates will be used"
echo ""
fi
done
else
echo "You need to provide groups so casTLE can perform group by group comparisons."
echo ""
exit
fi
fi
if [ ${usemageck} -eq "1" ] ## MAGeCK checks
then
if [ -z "${magecksgRNAlibrary}" ]
then
echo "Providing MAGeCK with a list of sgRNA names and sequences is mandatory, edit your config file."
echo ""
exit
fi
if [ -z "${testgroups}" ]
then
echo "You need to provide groups so MAGeCK can perform group by group comparisons."
echo ""
exit
fi
if [ "${python}" != "python3" ]
then
echo "MAGeCK require python 3, please install and rerun or disable MAGeCK in your config file."
echo ""
exit
fi
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 "-- 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}/Fastqs" ] && rm "${dir2}/Fastqs"
[ -d "${dir2}/casTLE/${logs}" ] && rm -rf "${dir2}/casTLE/${logs}"
[ -d "${dir2}/MAGeCK/${logs}" ] && rm -rf "${dir2}/MAGeCK/${logs}"
if [ -z "${tmp}" ]
then
tmp="${dir2}/tmp"
fi
######################
## Processing samples
######################
find -L "${dir}" -maxdepth 1 -name "*${fileext}" | sed 's#.*/##' | sort -n > "${dir2}/Fastqs"
if [ ${usecastle} -eq "1" ] ## Start of casTLE
then
###################
## casTLE ##
###################
echo ""
echo "-- casTLE --"
mkdir -p "${dir2}/casTLE/${logs}"
if [ -n "${oligofile}" ]
then
######################
## Make bowtie index
job="bowtie_casTLE"
# Variables
samplename="index"
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.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 "1" ]; then echo "--cpus-per-task=1"; else echo "--cpus-per-task=${threads}"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=10: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
echo "cd ${castlepath}"
echo "${python} ./Scripts/makeIndices.py $(if [ -n "${bowtie}" ]; then echo "-b ${bowtie}-build"; fi) -t -o ${oligofile} ${screentype} ${outputbowtieindex} || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBbowdex=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
echo ""
echo -e "\t-- makeIndices job queued --"
fi
######################
## Generate count files
job="counts_casTLE"
echo ""
echo -e "\t-- Queuing makeCounts jobs"
echo ""
while read -r line;
do
# Variables
samplename=$(echo "${line}" | awk -F${fileext} '{print $1}')
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
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 "${SBbowdex}" ]
then
echo "#SBATCH --dependency=afterok:${SBbowdex##* }"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/makeCounts.py $(if [ -n "${bowtie}" ]; then echo "-b ${bowtie}"; fi) -p ${threads} ${dir}/${line} ${dir2}/casTLE/${samplename} ${screentype} || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove map files, not used after that step.
echo "rm ${dir2}/casTLE/${samplename}.map.gz ${dir2}/casTLE/${samplename}.unmapped.gz"
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBcounts=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBcounts##* }" ]
then
SBcountsIDs=${SBcountsIDs}:${SBcounts##* }
fi
echo -e "\t ${samplename} makeCounts job queued"
done < "${dir2}/Fastqs"
######################
## Plot distribution of elements from count file
job="plot_casTLE"
# Variables
samplename="dist"
countfiles=$(find "${dir2}"/casTLE -name '*_counts_casTLE.sbatch' | sed ':a;N;$!ba;s/\n/ /g' | sed "s/_counts_casTLE.sbatch/_counts.csv/g")
distlegends=$(sed ':a;N;$!ba;s/\n/ /g' "${dir2}"/Fastqs | sed "s/${fileext}//g")
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH $(if [ -n "${mem}" ] && [ ${mem} -gt "8" ]; then echo "--mem=8000"; else echo "--mem=${mem}000"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=15: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 "${SBcountsIDs}" ]
then
echo "#SBATCH --dependency=afterok${SBcountsIDs}"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/plotDist.py -of ${dir2}/casTLE/DiversityPlot ${countfiles} -l ${distlegends} $(if [ -n "${graphformat}" ]; then echo "-f ${graphformat}"; fi) || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBplotdist=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
echo ""
echo -e "\t-- plotDist job queued --"
######################
## Compare count files with casTLE
echo ""
echo -e "\t-- Queuing analysis$(if [ -n "${permres}" ] && [ ${permres} -eq "1" ]; then echo " and permutations"; fi) jobs"
echo ""
# Variables
IFS=" " read -r -a testsarray <<< "${day0} ${testgroups}"
condA="0"
condB="0"
while [ ${condA} -lt ${#testsarray[@]} ]
do
condB=$((condA + 1))
IFS="," read -r -a condAarray <<< "${testsarray[${condA}]}"
while [ ${condB} -lt ${#testsarray[@]} ]
do
IFS="," read -r -a condBarray <<< "${testsarray[${condB}]}"
for replicates in 0 1
do
# Variables
job="analyze_casTLE"
samplename=$(if [ -z "${condAarray[${replicates}]}" ]; then echo "${condAarray[0]}"; else echo "${condAarray[${replicates}]}";fi)_vs_$(if [ -z "${condBarray[${replicates}]}" ]; then echo "${condBarray[0]}"; else echo "${condBarray[${replicates}]}";fi)
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=30: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 "${SBcountsIDs}" ]
then
echo "#SBATCH --dependency=afterok${SBcountsIDs}"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/analyzeCounts.py $(if [ -n "${mouse}" ] && [ "${mouse}" -eq "1" ] ; then echo "-m"; fi) -p ${threads} ${dir2}/casTLE/$(if [ -z "${condAarray[${replicates}]}" ]; then echo "${condAarray[0]}"; else echo "${condAarray[${replicates}]}";fi)_counts.csv ${dir2}/casTLE/$(if [ -z "${condBarray[${replicates}]}" ]; then echo "${condBarray[0]}"; else echo "${condBarray[${replicates}]}";fi)_counts.csv ${dir2}/casTLE/${samplename}_results || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBanalyze=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBanalyze##* }" ]
then
SBanalyzeIDs=${SBanalyzeIDs}:${SBanalyze##* }
fi
echo -e "\t ${samplename} analyzeCounts job queued"
if [ -n "${permres}" ] && [ ${permres} -eq "1" ]
then
######################
## Calculate p-values for casTLE result file
job="permut_casTLE"
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=3: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 "${SBanalyze}" ]
then
echo "#SBATCH --dependency=afterok:${SBanalyze##* }"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/addPermutations.py $(if [ -n "${mouse}" ] && [ "${mouse}" -eq "1" ] ; then echo "-m"; fi) -p ${threads} ${dir2}/casTLE/${samplename}_results.csv ${permutations} || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBpermres=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBpermres##* }" ]
then
SBpermresIDs=${SBpermresIDs}:${SBpermres##* }
fi
echo -e "\t ${samplename} addPermutations job queued"
echo ""
fi
done
# Move to the next comparison
condB=$((condB + 1))
done
# Move to the next comparison
condA=$((condA + 1))
done
######################
## Combine multiple casTLE result files
echo ""
echo -e "\t-- Queuing combo analysis, permutation and graph jobs"
echo ""
# Variables
IFS=" " read -r -a testsarray <<< "${day0} ${testgroups}"
condA="0"
condB="0"
while [ ${condA} -lt ${#testsarray[@]} ]
do
condB=$((condA + 1))
IFS="," read -r -a condAarray <<< "${testsarray[${condA}]}"
while [ ${condB} -lt ${#testsarray[@]} ]
do
# Variables
job="analyze_casTLE"
IFS="," read -r -a condBarray <<< "${testsarray[${condB}]}"
samplename="combo_${condAarray[0]}_vs_${condBarray[0]}_VS_$(if [ -z "${condAarray[1]}" ]; then echo "${condAarray[0]}"; else echo "${condAarray[1]}";fi)_vs_$(if [ -z "${condBarray[1]}" ]; then echo "${condBarray[0]}"; else echo "${condBarray[1]}";fi)"
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=3: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 "${SBanalyzeIDs}" ]
then
echo "#SBATCH --dependency=afterok${SBanalyzeIDs}"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/analyzeCombo.py $(if [ -n "${mouse}" ] && [ "${mouse}" -eq "1" ] ; then echo "-m"; fi) -p ${threads} ${dir2}/casTLE/${condAarray[0]}_vs_${condBarray[0]}_results.csv ${dir2}/casTLE/$(if [ -z "${condAarray[1]}" ]; then echo "${condAarray[0]}"; else echo "${condAarray[1]}";fi)_vs_$(if [ -z "${condBarray[1]}" ]; then echo "${condBarray[0]}"; else echo "${condBarray[1]}";fi)_results.csv ${dir2}/casTLE/${samplename} || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBcombo=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBcombo##* }" ]
then
SBcomboIDs=${SBcomboIDs}:${SBcombo##* }
fi
echo -e "\t ${samplename} analyzeCombo job queued"
######################
## Calculate p-values for combination of multiple casTLE results
# Variables
job="permut_casTLE"
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH --mem=${mem}000 --cpus-per-task=${threads}"
echo "#SBATCH --requeue"
echo "#SBATCH --time=6: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 "${SBcombo}" ]
then
echo "#SBATCH --dependency=afterok:${SBcombo##* }"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/addCombo.py $(if [ -n "${mouse}" ] && [ "${mouse}" -eq "1" ] ; then echo "-m"; fi) -p ${threads} ${dir2}/casTLE/${samplename}.csv ${permutations} || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBpermcombo=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBpermcombo##* }" ]
then
SBpermcomboIDs=${SBpermcomboIDs}:${SBpermcombo##* }
fi
echo -e "\t ${samplename} addCombo job queued"
######################
## Plot casTLE result file
# Variables
job="volcano_plot_casTLE"
{
# General SLURM parameters
echo '#!/bin/bash'
echo "#SBATCH --job-name=${samplename}_${job} --output=${dir2}/casTLE/${logs}/${samplename}_${job}.out --error=${dir2}/casTLE/${logs}/${samplename}_${job}.err --open-mode=append"
echo "#SBATCH $(if [ -n "${mem}" ] && [ ${mem} -gt "8" ]; then echo "--mem=8000"; else echo "--mem=${mem}000"; fi)"
echo "#SBATCH --requeue"
echo "#SBATCH --time=15: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 "${SBcombo}" ]
then
echo "#SBATCH --dependency=afterok:${SBcombo##* }"
fi
# General commands
echo "mkdir -p ${tmp}"
if [ -n "${customcmd}" ]
then
echo "${customcmd}"
fi
# Job specific commands
echo "cd ${castlepath}"
echo "${python} ./Scripts/plotVolcano.py $(if [ -n "${mouse}" ] && [ "${mouse}" -eq "1" ] ; then echo "-m"; fi) ${dir2}/casTLE/${samplename}.csv -n \$(csvcut -c Symbol,'Combo casTLE Score','# elements 1','# elements 2' ${dir2}/casTLE/${samplename}.csv | csvgrep -c '# elements 1','# elements 2' -r ^'[6-9]|10' | csvsort -c 'Combo casTLE Score' | csvcut -c Symbol | tail -n 10 | sed ':a;N;$!ba;s/\n/ /g') $(if [ -n "${graphformat}" ]; then echo "-f ${graphformat}"; fi) || if [ -f ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err ]; then echo \${SLURM_JOB_NODELIST} >> ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && exit 1; else echo \${SLURM_JOB_NODELIST} > ${dir2}/casTLE/\${SLURM_JOBID}-${samplename}_${job}.err && scontrol requeue \${SLURM_JOBID} && sleep 42m; fi"
# Cleaning commands
if [ "${debug}" != "1" ]
then
# Remove .sbatch
echo "rm ${dir2}/casTLE/${samplename}_${job}.sbatch"
fi
echo "exit 0"
} > "${dir2}/casTLE/${samplename}_${job}.sbatch"
# Queue job
SBplotvolcano=$(until sbatch "${dir2}/casTLE/${samplename}_${job}.sbatch"; do echo "Job submission failed (exit code ${?}) - Trying again in 5s"; sleep 5; done)
if [ -n "${SBplotvolcano##* }" ]
then
SBplotvolcanoIDs=${SBplotvolcanoIDs}:${SBplotvolcano##* }
fi
echo -e "\t ${samplename} plotVolcano job queued"
######################
## Plot individual gene results from casTLE result file
# Variables
job="genes_plot_casTLE"
{
# General SLURM parameters
echo '#!/bin/bash'