-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamrbox.sh
1041 lines (904 loc) · 31.3 KB
/
hamrbox.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
set -u
# Harry Li, University of Pennsylvania
usage () {
echo ""
echo "Usage : sh $0"
echo ""
cat <<'EOF'
######################################### COMMAND LINE OPTIONS #############################
REQUIRED:
-o <project directory>
-t <SRA accession list.txt or folder of raw fastq files>
-c <filenames for each fastq.csv>
-g <reference genome.fa>
-i <reference genome annotation.gff3>
-l <read length>
-s <genome size in bp >
-e <genome annotation generator>
OPTIONAL:
-n number of threads (default 4)
-a [use TopHat2 instead of STAR]
-b [Tophat library choice: fr-unstranded, fr-firststrand, fr-secondstrand]
-f [filter]
-Q [HAMR: minimum qualuty score, default=30]
-C [HAMR: minimum coveragem default=50]
-E [HAMR: sequencing error, default=0.01]
-P [HAMR: maximum p-value, default=1]
-F [HAMR: maximum fdr, default=0.05]
-m [HAMR model]
-n [number of threads]
-h [help message]
################################################# END ########################################
EOF
exit 0
}
curdir=$(dirname $0)
threads=4
tophat=false
quality=30
coverage=50
err=0.01
pvalue=1
fdr=0.05
tophatlib="fr-firststrand"
filter=$curdir/filter_SAM_number_hits.pl
model=$curdir/euk_trna_mods.Rdata
generator=""
#############Grabbing arguments############
while getopts ":o:t:c:g:i:l:e:s:A:f:m:n:hQCabEPF:" opt; do
case $opt in
o)
out=$OPTARG # project output directory root
;;
t)
acc=$OPTARG # SRA accession
;;
c)
csv=$OPTARG # SRR to filename table
;;
g)
genome=$OPTARG # reference genome directory
;;
i)
annotation=$OPTARG # reference genome annotation
;;
l)
length+=$OPTARG # read length
;;
e)
generator=$OPTARG # organism abbreviation for annotationGenerate
;;
s)
genomelength=$OPTARG # length or size of the genome
;;
f)
filter=$OPTARG
;;
m)
model=$OPTARG
;;
n)
threads=$OPTARG
;;
Q)
quality=$OPTARG
;;
C)
coverage=$OPTARG
;;
b)
tophatlib=$OPTARG
;;
E)
err=$OPTARG
;;
a)
tophat=true
;;
P)
pvalue=$OPTARG
;;
F)
fdr=$OPTARG
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Assigning the appropriate annotationGenerate.R
if [[ $generator == "AT" ]]; then
generator="/annotationGenerate/annotationGenerateAT.R"
echo "Model organism detected: Arabidopsis thaliana"
elif [[ $generator == "BD" ]]; then
generator="/annotationGenerate/annotationGenerateBD.R"
echo "Model organism detected: Brachypodium distachyon"
elif [[ $generator == "ZM" ]]; then
generator="/annotationGenerate/annotationGenerateZM.R"
echo "Model organism detected: Zea mays"
elif [[ $generator == "OSJ" ]]; then
generator="/annotationGenerate/annotationGenerateOSJ.R"
echo "Model organism detected: Oryza sativa japonica"
elif [[ $generator == "OSI" ]]; then
generator="/annotationGenerate/annotationGenerateOSI.R"
echo "Model organism detected: Oryza sativa indica"
elif [[ $generator == "OSIR64" ]]; then
generator="/annotationGenerate/annotationGenerateOSIR64.R"
echo "Model organism detected: Oryza sativa IR64"
else
echo "##################################################"
echo "model organism code not recognized, please check your input"
echo "HAMRbox will proceed with limited functionalities"
echo "##################################################"
fi
######################################################### Subprogram Definition #########################################
fqgrab () {
echo "begin downloading $line..."
fasterq-dump "$line" -O $dumpout/raw --verbose
# automatically detects the suffix
echo $dumpout/raw/$line
if [[ -f $dumpout/raw/$line"_1.fastq" ]]; then
suf="fastq"
PE=true
echo "$line is a paired-end file ending in .fastq"
elif [[ -f $dumpout/raw/$line"_1.fq" ]]; then
suf="fq"
PE=true
echo "$line is a paired-end file ending in .fq"
elif [[ -f $dumpout/raw/$line".fastq" ]]; then
suf="fastq"
PE=false
echo "$line is a single-end file ending in .fastq"
elif [[ -f $dumpout/raw/$line".fq" ]]; then
suf="fq"
PE=false
echo "$line is a single-end file ending in .fq"
else
echo "suffix not recognized, please check your datasets"
exit 1
fi
if [[ "$PE" = false ]]; then
echo "[$line] performing fastqc on raw file..."
fastqc $dumpout/raw/$line.$suf -o $dumpout/fastqc_results &
echo "[$line] trimming..."
trim_galore -o $dumpout/trimmed $dumpout/raw/$line.$suf
echo "[$line] trimming complete, performing fastqc..."
fastqc $dumpout/trimmed/$line"_trimmed.fq" -o $dumpout/fastqc_results
else
echo "[$line] performing fastqc on raw file..."
fastqc $dumpout/raw/$line"_1.$suf" -o $dumpout/fastqc_results &
fastqc $dumpout/raw/$line"_2.$suf" -o $dumpout/fastqc_results &
echo "[$line] trimming..."
trim_galore -o $dumpout/trimmed $dumpout/raw/$line"_1.$suf"
trim_galore -o $dumpout/trimmed $dumpout/raw/$line"_2.$suf"
echo "[$line] trimming complete, performing fastqc..."
fastqc $dumpout/trimmed/$line"_1_trimmed.fq" -o $dumpout/fastqc_results
fastqc $dumpout/trimmed/$line"_2_trimmed.fq" -o $dumpout/fastqc_results
fi
echo "finished processing $line"
echo ""
}
fqgrab2 () {
sname=$(basename $fq)
tt=${sname%.*}
echo "[$sname] performing fastqc on raw file..."
fastqc $fq -o $dumpout/fastqc_results &
echo "[$sname] trimming..."
trim_galore -o $dumpout/trimmed $fq
echo "[$sname] trimming complete, performing fastqc..."
fastqc $dumpout/trimmed/$tt"_trimmed.fq" -o $dumpout/fastqc_results
}
fastq2hamr () {
# last check on HAMR
if [ ! -n "/HAMR/hamr.py" ]; then
echo "HAMR not installed, please check."
exit 1
fi
smpext=$(basename "$smp")
smpdir=$(dirname "$smp")
smpkey="${smpext%.*}"
smpname=""
original_ext="${smpext##*.}"
if [[ $smpkey == *_1* ]]; then
smpkey="${smpkey%_1*}"
smp1="$smpdir/${smpkey}_1_trimmed.$original_ext"
smp2="$smpdir/${smpkey}_2_trimmed.$original_ext"
# Paired end recognized
det=0
echo "$smpext is a part of a paired-end sequencing file"
elif [[ $smpkey == *_2* ]]; then
# If _2 is in the filename, this file was processed along with its corresponding _1 so we skip
echo "$smpext has already been processed with its _1 counter part. Skipped."
echo ""
exit 1
else
det=1
echo "$smpext is a single-end sequencing file"
echo ""
fi
# Read the CSV file into a DataFrame
mapfile -t names < <(awk -F, '{ print $1 }' "$csv")
mapfile -t smpf < <(awk -F, '{ print $2 }' "$csv")
# Create a dictionary from the DataFrame
declare -A dictionary
for ((i=0; i<${#names[@]}; i++)); do
dictionary[${names[i]}]=${smpf[i]}
done
if [[ $smpkey == *_trimmed* ]]; then
smpkey="${smpkey%_trimmed*}"
fi
# Retrieve the translated value
if [[ ${dictionary[$smpkey]+_} ]]; then
smpname="${dictionary[$smpkey]}"
smpname="${smpname//$'\r'}"
echo "[$smpkey] Sample group name found: $smpname"
else
echo "[$smpkey] Could not locate sample group name, exiting..."
exit 1
fi
# Reassign / declare pipeline file directory
if [ ! -d "$out/pipeline/$smpkey""_temp" ]; then
mkdir "$out/pipeline/$smpkey""_temp"
echo "[$smpkey] created path: $out/pipeline/$smpkey""_temp"
fi
smpout=$out/pipeline/$smpkey"_temp"
echo "[$smpkey] You can find all the intermediate files for $smpkey at $smpout"
# Reassign hamr output directory
if [ ! -d "$out/hamr_out" ]; then
mkdir $out/hamr_out
echo "created path: $out/hamr_out"
fi
hamrout=$out/hamr_out
echo "[$smpkey] You can find the HAMR output file for $smpkey at $hamrout/$smpname.mod.txt"
echo "[$smpkey] Begin HAMR pipeline"
cd $smpout
# maps the trimmed reads to provided annotated genome, can take ~1.5hr
if [[ "$tophat" = false ]]; then
echo "Using STAR for mapping..."
if [ "$det" -eq 1 ]; then
echo "[$smpkey] Performing STAR with a single-end file."
STAR \
--runThreadN 2 \
--genomeDir $out/ref \
--readFilesIn $smp \
--sjdbOverhang $overhang \
--sjdbGTFfile $annotation \
--sjdbGTFtagExonParentTranscript Parent \
--outFilterMultimapNmax 10 \
--outFilterMismatchNmax $mismatch \
--outSAMtype BAM SortedByCoordinate
else
echo "[$smpkey] Performing STAR with a paired-end file."
STAR \
--runThreadN 2 \
--genomeDir $out/ref \
--readFilesIn $smp1 $smp2 \
--sjdbOverhang $overhang \
--sjdbGTFfile $annotation \
--sjdbGTFtagExonParentTranscript Parent \
--outFilterMultimapNmax 10 \
--outFilterMismatchNmax $mismatch \
--outSAMtype BAM SortedByCoordinate
fi
else
echo "Using TopHat2 for mapping..."
# set read distabce based on mistmatch num
red=8
if [[ $mismatch > 8 ]]; then red=$((mismatch +1)); fi
if [ "$det" -eq 1 ]; then
echo "[$smpkey] Performing TopHat2 with a single-end file."
tophat2 \
--library-type $tophatlib \
--read-mismatches $mismatch \
--read-edit-dist $red \
--max-multihits 10 \
--b2-very-sensitive \
--transcriptome-max-hits 10 \
--no-coverage-search \
-G $annotation \
-p $threads \
$out/btref \
$smp
else
echo "[$smpkey] Performing TopHat2 with a paired-end file."
tophat2 \
--library-type $tophatlib \
--read-mismatches $mismatch \
--read-edit-dist $red \
--max-multihits 10 \
--b2-very-sensitive \
--transcriptome-max-hits 10 \
--no-coverage-search \
-G $annotation \
-p $threads \
$out/btref \
$smp1 $smp2
fi
fi
cd
wait
#sorts the accepted hits
echo "[$smpkey] sorting..."
samtools sort \
-n $smpout/Aligned.sortedByCoord.out.bam \
-o $smpout/sort_accepted.bam
echo "[$smpkey] finished sorting"
echo ""
wait
#filter the accepted hits by uniqueness
echo "[$smpkey] filter unique..."
samtools view \
-h $smpout/sort_accepted.bam \
| perl $filter 1 \
| samtools view -bS - \
| samtools sort \
-o $smpout/unique.bam
echo "[$smpkey] finished filtering"
echo ""
wait
#adds read groups using picard, note the RG arguments are disregarded here
echo "[$smpkey] adding/replacing read groups..."
gatk AddOrReplaceReadGroups \
I=$smpout/unique.bam \
O=$smpout/unique_RG.bam \
RGID=1 \
RGLB=xxx \
RGPL=illumina_100se \
RGPU=HWI-ST1395:97:d29b4acxx:8 \
RGSM=sample
echo "[$smpkey] finished adding/replacing read groups"
echo ""
wait
#reorder the reads using picard
echo "[$smpkey] reordering..."
echo "$genome"
gatk --java-options "-Xmx2g -Djava.io.tmpdir=$smpout/tmp" ReorderSam \
I=$smpout/unique_RG.bam \
O=$smpout/unique_RG_ordered.bam \
R=$genome \
CREATE_INDEX=TRUE \
SEQUENCE_DICTIONARY=$dict \
TMP_DIR=$smpout/tmp
echo "[$smpkey] finished reordering"
echo ""
wait
#splitting and cigarring the reads, using genome analysis tool kit
#note can alter arguments to allow cigar reads
echo "[$smpkey] getting split and cigar reads..."
gatk --java-options "-Xmx2g -Djava.io.tmpdir=$smpout/tmp" SplitNCigarReads \
-R $genome \
-I $smpout/unique_RG_ordered.bam \
-O $smpout/unique_RG_ordered_splitN.bam
# -U ALLOW_N_CIGAR_READS
echo "[$smpkey] finished splitting N cigarring"
echo ""
wait
#final resorting using picard
echo "[$smpkey] resorting..."
gatk --java-options "-Xmx2g -Djava.io.tmpdir=$smpout/tmp" SortSam \
I=$smpout/unique_RG_ordered_splitN.bam \
O=$smpout/unique_RG_ordered_splitN.resort.bam \
SORT_ORDER=coordinate
echo "[$smpkey] finished resorting"
echo ""
wait
#hamr step, can take ~1hr
echo "[$smpkey] hamr..."
python /HAMR/hamr.py \
-fe $smpout/unique_RG_ordered_splitN.resort.bam $genome $model $smpout $smpname $quality $coverage $err H4 $pvalue $fdr .05
wait
if [ ! -e "$smpout/${smpname}.mods.txt" ]; then
cd $hamrout
printf "${smpname} \n" >> zero_mod.txt
cd
else
# HAMR needs separate folders to store temp for each sample, so we move at the end
cp $smpout/${smpname}.mods.txt $hamrout
fi
# Move the unique_RG_ordered.bam and unique_RG_ordered.bai to a folder for read depth analysis
cp $smpout/unique_RG_ordered.bam $out/pipeline/depth/$smpname.bam
cp $smpout/unique_RG_ordered.bai $out/pipeline/depth/$smpname.bai
}
consensusOverlap () {
IFS="/" read -ra sections <<< "$smp"
temp="${sections[-1]}"
IFS="." read -ra templ <<< "$temp"
smpname="${templ[0]}"
echo "consensus file prefix: $smpname"
echo ""
count=`ls -1 $genomedir/*_CDS.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
cds=$(find $genomedir -maxdepth 1 -name "*_CDS.bed")
#overlap with cds
intersectBed \
-a $cds \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_CDS".bed
echo "finished finding overlap with CDS library"
fi
count=`ls -1 $genomedir/*_fiveUTR.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
fiveutr=$(find $genomedir -maxdepth 1 -name "*_fiveUTR.bed")
#overlap with 5utr
intersectBed \
-a $fiveutr \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_fiveUTR".bed
echo "finished finding overlap with 5UTR library"
fi
count=`ls -1 $genomedir/*_threeUTR.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
threeutr=$(find $genomedir -maxdepth 1 -name "*_threeUTR.bed")
#overlap with 3utr
intersectBed \
-a ${threeutr} \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_threeUTR".bed
echo "finished finding overlap with 3UTR library"
fi
count=`ls -1 $genomedir/*_gene.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
gene=$(find $genomedir -maxdepth 1 -name "*_gene.bed")
#overlap with gene
intersectBed \
-a $gene \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_gene".bed
echo "finished finding overlap with gene library"
fi
count=`ls -1 $genomedir/*_primarymRNA.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
mrna=$(find $genomedir -maxdepth 1 -name "*_primarymRNA.bed")
#overlap with mrna
intersectBed \
-a $mrna \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_primarymRNA".bed
echo "finished finding overlap with primary mRNA library"
fi
count=`ls -1 $genomedir/*_exon.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
exon=$(find $genomedir -maxdepth 1 -name "*_exon.bed")
#overlap with exon
intersectBed \
-a $exon \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_exon".bed
echo "finished finding overlap with exon library"
fi
count=`ls -1 $genomedir/*_ncRNA.bed 2>/dev/null | wc -l`
if [ $count != 0 ]; then
nc=$(find $genomedir -maxdepth 1 -name "*_ncRNA.bed")
#overlap with nc rna
intersectBed \
-a $nc \
-b $smp \
-wa -wb \
> $out/lap/$smpname"_ncRNA".bed
echo "finished finding overlap with ncRNA library"
fi
}
######################################################### Main Program Begins #########################################
echo ""
echo "##################################### Begin HAMRbox #################################"
echo ""
# Check if the required arguments are provided
if [ -z "$out" ]; then
echo "output directory not detected, exiting..."
exit 1
elif [ -z "$acc" ]; then
echo "input SRR or fastq files not detected, exiting..."
exit 1
elif [ -z "$csv" ]; then
echo "filename dictionary csv not detected, exiting..."
exit 1
elif [ -z "$genome" ]; then
echo "model organism genmome fasta not detected, exiting..."
exit 1
elif [ -z "$annotation" ]; then
echo "model organism genmome annotation gff3 not detected, exiting..."
exit 1
elif [ -z "$length" ]; then
echo "read length not detected, exiting..."
exit 1
elif [ -z "$genomelength" ]; then
echo "genome size not detected, exiting..."
exit 1
else
echo "all required arguments provided, proceding..."
fi
mismatch=$(($length*6/100))
overhang=$(($mismatch-1))
##########fqgrab housekeeping begins#########
if [ ! -d "$out" ]; then mkdir $out; echo "created path: $out"; fi
if [ ! -d "$out/datasets" ]; then mkdir $out/datasets; echo "created path: $out/datasets"; fi
dumpout=$out/datasets
# first see what input is provided
if [[ $acc == *.txt ]]; then
echo "SRR accession list provided, using fasterq-dump for .fastq acquisition..."
# Create directory to store original fastq files
if [ ! -d "$out/datasets/raw" ]; then mkdir $out/datasets/raw; fi
echo "You can find your original fastq files at $out/datasets/raw"
mode=1
elif [[ -d $acc ]]; then
echo "Directory $acc is found, assuming raw fastq files are provided..."
mode=2
else
echo "Error recognizing input source, exiting..."
exit 1
fi
# relocate user-provided inputs
if [ ! -d "$out/ref" ]; then
mkdir "$out/ref"
echo "created path: $out/ref"
#cp $genome "$out/ref"
#genome="$out/ref/$(basename $genome)"
#cp $annotation "$out/ref"
#annotation="$out/ref/$(basename $annotation)"
fi
if [ ! -d "$out/fileprep" ]; then
mkdir "$out/fileprep"
echo "created path: $out/fileprep"
#cp $acc "$out/fileprep"
#cp $csv "$out/fileprep"
fi
# Create directory to store trimmed fastq files
if [ ! -d "$out/datasets/trimmed" ]; then mkdir $out/datasets/trimmed; fi
echo "You can find your trimmed fastq files at $out/datasets/trimmed"
# Create directory to store fastqc results
if [ ! -d "$out/datasets/fastqc_results" ]; then mkdir $out/datasets/fastqc_results; fi
echo "You can find all the fastqc test results at $out/datasets/fastqc_results"
# Run a series of command checks to ensure the entire script can run smoothly
if ! command -v fasterq-dump > /dev/null; then
echo "Failed to call fasterq-dump command. Please check your installation."
exit 1
fi
if ! command -v fastqc > /dev/null; then
echo "Failed to call fastqc command. Please check your installation."
exit 1
fi
if ! command -v trim_galore > /dev/null; then
echo "Failed to call trim_galore command. Please check your installation."
exit 1
fi
if ! command -v gatk > /dev/null; then
echo "Failed to call gatk command. Please check your installation."
exit 1
fi
##########fqgrab housekeeping ends#########
##########fqgrab main begins#########
if [[ $mode -eq 1 ]]; then
# Grabs the fastq files from acc list provided into the dir ~/datasets
i=0
while IFS= read -r line
do ((i=i%$threads)); ((i++==0)) && wait
fqgrab &
done < "$acc"
elif [[ $mode -eq 2 ]]; then
i=0
for fq in $acc/*; do
((i=i%$threads)); ((i++==0)) && wait
fqgrab2 &
done
fi
wait
##################fqgrab main ends#################
echo ""
echo "################ Finished downloading and processing all fastq files. Entering pipeline for HAMR analysis. ######################"
echo ""
############fastq2hamr housekeeping begins##############
# Checks if the files were trimmed or cleaned, and if so, take those files for downstream
hamrin=""
suf=""
# If trimmed folder present, then user specified trimming, we take trimmed files with .fq
if [ -d "$dumpout/trimmed" ]; then
hamrin=$dumpout/trimmed
suf="fq"
else
echo "failed to locate trimmed fastq files"
exit 1
fi
# Creating some folders
if [ ! -d "$out/pipeline" ]; then mkdir $out/pipeline; echo "created path: $out/pipeline"; fi
if [ ! -d "$out/hamr_out" ]; then mkdir $out/hamr_out; echo "created path: $out/hamr_out"; fi
# Check if zero_mod is present already, if not then create one
if [ ! -e "$out/hamr_out/zero_mod.txt" ]; then
cd $out/hamr_out
echo "Below samples have 0 HAMR predicted mods:" > zero_mod.txt
cd
fi
# Get genome directory
genomedir=$(dirname $genome)
# create dict file using fasta genome file
count=`ls -1 $genomedir/*.dict 2>/dev/null | wc -l`
if [ $count == 0 ]; then
gatk CreateSequenceDictionary \
R=$genome
fi
dict=$(find $genomedir -maxdepth 1 -name "*.dict")
# create fai index file using fasta genome
count=`ls -1 $genomedir/*.fai 2>/dev/null | wc -l`
if [ $count == 0 ]; then
samtools faidx $genome
fi
# Check which mapping software, and check for index
if [[ "$tophat" = false ]]; then
# Check if indexed files already present for STAR
if [ -e "$out/ref/SAindex" ]; then
echo "STAR Genome Directory with indexed genome detected, skipping STAR indexing"
else
# Now, do the indexing step
# Define the SA index number argument
log_result=$(echo "scale=2; l($genomelength)/l(2)/2 - 1" | bc -l)
sain=$(echo "scale=0; if ($log_result < 14) $log_result else 14" | bc)
echo "Creating STAR genome index..."
# Create genome index
STAR \
--runThreadN $threads \
--runMode genomeGenerate \
--genomeDir $out/ref \
--genomeFastaFiles $genome \
--sjdbGTFfile $annotation \
--sjdbGTFtagExonParentTranscript Parent \
--sjdbOverhang $overhang \
--genomeSAindexNbases $sain
fi
else
# Check if bowtie index directory is already present
if [ -e "$out/btref" ]; then
echo "bowtie indexed directory detected, skipping generating bowtie index"
else
# If not, first check if ref folder is present, if not then make
if [ ! -d "$out/btref" ]; then mkdir "$out/btref"; echo "created path: $out/btref"; fi
echo "Creating Bowtie references..."
bowtie2-build $genome $out/btref
fi
fi
# Run a series of command checks to ensure fastq2hamr can run smoothly
if ! command -v mapfile > /dev/null; then
echo "Failed to call mapfile command. Please check your installation."
exit 1
fi
if ! command -v STAR > /dev/null; then
echo "Failed to call STAR command. Please check your installation."
exit 1
fi
if ! command -v samtools > /dev/null; then
echo "Failed to call samtools command. Please check your installation."
exit 1
fi
if ! command -v gatk > /dev/null; then
echo "Failed to call gatk command. Please check your installation."
exit 1
fi
if ! command -v python > /dev/null; then
echo "Failed to call python command. Please check your installation."
exit 1
fi
# Creates a folder for depth analysis
if [ ! -d "$out/pipeline/depth" ]; then mkdir $out/pipeline/depth; echo "created path: $out/pipeline/depth"; fi
#############fastq2hamr housekeeping ends#############
#############fastq2hamr main begins###############
# Pipes each fastq down the hamr pipeline, and stores out put in ~/hamr_out
# Note there's also a hamr_out in ~/pipeline/SRRNUMBER_temp/, but that one's for temp files
i=0
ttop=$(($threads/2))
for smp in $hamrin/*.$suf
do ((i=i%$ttop)); ((i++==0)) && wait
fastq2hamr &
done
wait
# Check whether any hamr.mod.text is present, if not, halt the program here
if [ -z "$(ls -A $out/hamr_out)" ]; then
echo "No HAMR predicted mod found for any sequencing data in this project, please see log for verification"
exit 1
fi
# If program didn't exit, at least 1 mod file, move zero mod record outside so it doesn't get read as a modtbl next
mv $out/hamr_out/zero_mod.txt $out
# Produce consensus bam files based on filename (per extracted from name.csv) and store in ~/consensus
if [ ! -d "$out/consensus" ]; then mkdir $out/consensus; echo "created path: $out/consensus"; fi
# Run a series of command checks to ensure findConsensus can run smoothly
if ! command -v Rscript > /dev/null; then
echo "Failed to call Rscript command. Please check your installation."
exit 1
fi
echo ""
echo "################ Finished HAMR analysis. Producing consensus mod table and depth analysis. ######################"
echo ""
#############fastq2hamr main ends###############
echo "Producing consensus file across biological replicates..."
# Find consensus accross all reps of a given sample group
Rscript $curdir/findConsensus.R \
$out/hamr_out \
$out/consensus
wait
echo "done"
# The case where no consensus file is found, prevents *.bed from being created
if [ -z "$(ls -A $out/consensus)" ]; then
echo "No consensus mods found within any sequencing group. Please see check individual rep for analysis. "
exit 1
fi
# Add depth columns with info from each rep alignment, mutate in place
for f in $out/consensus/*.bed
do
t=$(basename $f)
d=$(dirname $f)
n=${t%.*}
echo "starting depth analysis on $n"
for ff in $out/pipeline/depth/*.bam
do
if echo "$ff" | grep -q "$n"
then
tt=$(basename $ff)
nn=${tt%.*}
echo "[$n] extracting depth information from $nn"
for i in $(seq 1 $(wc -l < $f))
do
chr=$(sed "${i}q;d" $f | sed 's/\t/\n/g' | sed '1q;d')
pos=$(sed "${i}q;d" $f | sed 's/\t/\n/g' | sed '2q;d')
dph=$(samtools coverage \
-r $chr:$pos-$pos \
$ff \
| awk 'NR==2' | awk -F'\t' '{print $7}')
awk -v "i=$i" 'NR==i {print $0"\t"var; next} 1' var="$dph" $f > $d/${nn}_new.bed && mv $d/${nn}_new.bed $f
done
echo "[$n] finished $nn"
fi
done &
done
wait
for f in $out/consensus/*.bed
do
if [ -s $f ]; then
# The file is not-empty.
t=$(basename $f)
n=${t%.*}
echo "computing depth across reps for $n"
Rscript $curdir/depth_helper_average.R $f
fi
done
wait
# Produce overlap bam files with the provided annotation library folders and store in ~/lap
if [ ! -d "$out/lap" ]; then mkdir $out/lap; echo "created path: $out/lap"; fi
# Run a series of command checks to ensure consensusOverlap can run smoothly
if ! command -v intersectBed > /dev/null; then
echo "Failed to call intersectBed command. Please check your installation."
exit 1
fi
# checks if genomedir is populated with generated annotation files, if not, hamrbox can't run anymore, exit
count=`ls -1 $genomedir/*.bed 2>/dev/null | wc -l`
if [ $count == 0 ]; then
if [[ ! -z "$generator" ]]; then
echo "generating annotations for overlap..."
Rscript $generator $annotation
else
echo "#########NOTICE###########"
echo "##########No annotation generator or annotation files found, please check your supplied arguments##########"
echo "##########As a result, HAMRbox will stop here. Please provide the above files in the next run############"
exit 1
fi
else
echo "generated annotation detected, proceeding to overlapping"
fi
# Overlap with provided libraries for each sample group
for smp in $out/consensus/*
do consensusOverlap
done
echo ""
echo "###############SMACK portion completed, entering EXTRACT################"
echo ""
#######################################begins EXTRACT######################################
dir=$out
echo "generating long modification table..."
# collapse all overlapped data into longdf
Rscript $curdir/allLapPrep.R \
$dir/lap \
$dir
echo "done"
echo ""
echo "plotting modification abundance..."
# overview of modification proportion
Rscript $curdir/abundByLap.R \
$dir/mod_long.csv \
$genomedir \
$dir
echo "done"
echo ""
echo "performing modification cluster analysis..."
# analyze hamr-mediated/true clustering across project
Rscript $curdir/clusterAnalysis.R \
$dir/mod_long.csv \
$dir
echo "done"
echo ""
# if [ ! -z "${4+x}" ]; then
# echo "known modification landscape provided, performing relative positional analysis to known mod..."
# # The csv (in modtbl format) of the known mod you want analyzed in distToKnownMod
# antcsv=$4
# # analyze hamr-mediated/true clustering across project
# Rscript $curdir/distToKnownMod.R \
# $dir/mod_long.csv \
# $antcsv
# echo "done"
# echo ""
# else
# echo "known modification file not detected, skipping relative positional analysis"
# echo ""
# fi
echo "classifying modified RNA subtype..."
# looking at RNA subtype for mods
Rscript $curdir/RNAtype.R \
$dir/mod_long.csv
echo "done"
echo ""
if [ ! -d "$dir/go" ]; then mkdir $dir/go; echo "created path: $dir/go"; fi
if [ ! -d "$dir/go/genelists" ]; then mkdir $dir/go/genelists; echo "created path: $dir/go/genelists"; fi
if [ ! -d "$dir/go/pantherout" ]; then mkdir $dir/go/pantherout; echo "created path: $dir/go/pantherout"; fi
if [ ! -n "/pantherapi-pyclient" ]; then
echo "panther installation not found, skipping go analysis"
else
echo "generating genelist from mod table..."
# produce gene lists for all GMUCT (for now) groups
Rscript $curdir/produceGenelist.R \
$dir/mod_long.csv \
$dir/go/genelists
# proceed if genelists directory is not empty
if [ ! -z "$(ls $dir/go/genelists)" ]; then
echo "sending each gene list to panther for overrepresentation analysis..."
# Send each gene list into panther API and generate a overrepresentation result file in another folter
for f in $dir/go/genelists/*.txt
do
n=$(basename $f)