-
Notifications
You must be signed in to change notification settings - Fork 1
/
HAMRLNC.sh
executable file
·2342 lines (2030 loc) · 79.6 KB
/
HAMRLNC.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 & Chosen Obih, University of Arizona
# man page
usage () {
echo ""
echo "Usage : sh $0"
echo ""
cat <<'EOF'
######################################### COMMAND LINE OPTIONS #############################
REQUIRED:
-o <project directory>
-c <filenames with key and name>
-g <reference genome.fa>
-i <reference genome annotation.gff3>
OPTIONAL:
-d [input a directory of fastq]
-l <read length>
-t [trim input fastq files, default=false]
-D [input a directory of bam]
-b [sort input bam files, default=false]
-I [input genome annotation folder for STAR]
-h [help message]
-n number of threads (default=4)
-O [Panther: organism taxon ID, default=3702]
-A [Panther: annotation dataset, default="GO:0008150"]
-Y [Panther: test type, default="FISHER"]
-R [Panther: correction type, default="FDR"]
-y [keep intermediate bam files, default=false]
-q [halt program upon completion of checkpoint 2, default=false]
-G [attribute used for featurecount, default="gene_id"]
-k [activate modification annotation workflow, default=false]
-p [activate lncRNA annotation workflow, default=false]
-u [activate featurecount workflow, default=false]
-H [SERVER alt path for panther]
-U [SERVER alt path for HAMRLINC]
-W [SERVER alt path for GATK]
-S [SERVER alt path for HAMR]
-J [SERVER alt path for CPC2]
-M [SERVER alt path for Rfam]
-f [HAMR: filter]
-m [HAMR: model]
-Q [HAMR: minimum quality score, default=30]
-E [HAMR: sequencing error, default=0.01]
-P [HAMR: maximum p-value, default=1]
-F [HAMR: maximum fdr, default=0.05]
-C [HAMR: minimum coverage, default=10]
################################################# END ########################################
EOF
exit 0
}
############################################# Define Default Variables #####################################
# hamr related defaults
quality=30
coverage=10
err=0.01
pvalue=1
fdr=0.05
path_to_HAMR="/HAMR"
path_to_rfam="/"
path_to_STARref=""
# hamr downstream
execpthr="/pantherapi-pyclient/pthr_go_annots.py"
execcpc="/CPC2_standalone-1.0.1/bin/CPC2.py"
# subprogram activation boolean
run_lnc=false
run_mod=false
run_featurecount=false
mod_partial=false
# other initialization
length=0
threads=4
attribute_fc="gene_id"
# I hope this is an ok initialization
lnc_max_intron_len=""
clean=true
clean_fastq_raw=true
#curdir=$(dirname "$0")
hsref=""
fastq_in=""
bam_in=""
bam_sorted=true
fastq_trimmed=true
do_fastqc=false
porg=""
pterm=""
ptest=""
pcorrect=""
# might change this later
hamrlinc_dir=""
gatk_dir=""
######################################################### Grab Arguments #########################################
while getopts ":o:c:g:i:l:d:D:btI:hn:O:A:Y:R:yzqrG:x:kupH:U:W:S:M:J:f:m:Q:E:P:F:C:" opt; do
case $opt in
o)
out=$OPTARG # project output directory root
;;
c)
csv=$OPTARG # SRR to filename table
;;
g)
genome=$OPTARG # reference genome
;;
i)
annotation=$OPTARG # reference genome annotation
;;
l)
length=$OPTARG # read length
;;
f)
filter=$OPTARG
;;
m)
model=$OPTARG
;;
n)
threads=$OPTARG
;;
p)
run_lnc=true
;;
k)
run_mod=true
;;
u)
run_featurecount=true
;;
q)
mod_partial=true
;;
y)
clean=false
;;
z)
clean_fastq_raw=false
;;
r)
do_fastqc=true
;;
Q)
quality=$OPTARG
;;
O)
porg=$OPTARG
;;
G)
attribute_fc=$OPTARG
;;
x)
lnc_max_intron_len=$OPTARG
;;
A)
pterm=$OPTARG
;;
Y)
ptest=$OPTARG
;;
R)
pcorrect=$OPTARG
;;
d)
fastq_in=$OPTARG
;;
D)
bam_in=$OPTARG
;;
I)
path_to_STARref=$OPTARG
;;
b)
bam_sorted=false
;;
t)
fastq_trimmed=false
;;
C)
coverage=$OPTARG
;;
E)
err=$OPTARG
;;
P)
pvalue=$OPTARG
;;
S)
path_to_HAMR="$OPTARG"
;;
M)
path_to_rfam="$OPTARG"
;;
H)
execpthr="$OPTARG"
;;
J)
execcpc="$OPTARG"
;;
F)
fdr=$OPTARG
;;
U)
hamrlinc_dir=$OPTARG
;;
W)
gatk_dir=$OPTARG
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
############################################### Derive Other Variables #########################################
# reassign sample input files, genome and annotation files name and include file paths
user_dir=$(pwd)
genome="$user_dir"/"$genome"
annotation="$user_dir"/"$annotation"
out="$user_dir"/"$out"
csv="$user_dir"/"$csv"
# assigning additional variables
dumpout=$out/datasets
ttop=$((threads/2))
genomedir=$(dirname "$genome")
last_checkpoint=""
# HAMR components path assignment
exechamrpy="$path_to_HAMR"/"hamr.py"
execignoreends="$path_to_HAMR"/"ignoreBamReadEnds.py"
# this might not work?
export util="$hamrlinc_dir"/"util"
export scripts="$hamrlinc_dir"/"scripts"
export PATH="$gatk_dir/:$PATH"
filter="$util"/filter_SAM_number_hits.pl
model="$util"/euk_trna_mods.Rdata
json="$util"/panther_params.json
generator="$scripts"/annotationGenerateUnified.R
################################################ Subprogram Definitions #########################################
# announces reached checkpoint and updates checkpoint file, or creates txt if it didn't exist
checkpoint () {
echo "Checkpoint reached: $1"
echo "$1" > "$out"/checkpoint.txt
}
# is repeated for each accession code found in csv, performs fasterq-dump, fastqc, and trimming; automatic paired-end recognition
fastqGrabSRA () {
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
if [[ "$do_fastqc" == true ]]; then
echo "[$line] performing fastqc on raw file..."
fastqc "$dumpout"/raw/"$line"."$suf" -o "$dumpout"/fastqc_results &
fi
if [[ "$fastq_trimmed" == false ]]; then
echo "[$line] trimming..."
trim_galore -o "$dumpout"/trimmed "$dumpout"/raw/"$line"."$suf"
if [[ "$do_fastqc" == true ]]; then
echo "[$line] trimming complete, performing fastqc..."
fastqc "$dumpout"/trimmed/"$line""_trimmed.fq" -o "$dumpout"/fastqc_results
fi
else
echo "[$line] is already trimmed, skipping trimming step..."
cp "$dumpout"/raw/"$line"."$suf" "$dumpout"/trimmed/"$line""_trimmed.fq"
fi
# remove unneeded raw
if [[ "$clean_fastq_raw" == true ]]; then
rm "$dumpout"/raw/"$line"."$suf"
fi
else
if [[ "$do_fastqc" == true ]]; then
echo "[$line] performing fastqc on raw file..."
fastqc -o "$dumpout"/fastqc_results "$dumpout"/raw/"$line""_1.$suf"
fastqc -o "$dumpout"/fastqc_results "$dumpout"/raw/"$line""_2.$suf"
fi
if [[ "$fastq_trimmed" == false ]]; then
echo "[$line] trimming..."
trim_galore --paired \
-o "$dumpout"/trimmed \
"$dumpout"/raw/"$line""_1.$suf" "$dumpout"/raw/"$line""_2.$suf"
# dealing with trim galore naming convention under pe
mv "$dumpout"/trimmed/"$tt""_1_val_1.fq" "$dumpout"/trimmed/"$tt""_1_trimmed.fq"
mv "$dumpout"/trimmed/"$tt""_2_val_2.fq" "$dumpout"/trimmed/"$tt""_2_trimmed.fq"
else
echo "[$line] is already trimmed, skipping trimming step..."
cp "$dumpout"/raw/"$line""_1.$suf" "$dumpout"/trimmed/"$line""_1_trimmed.fq"
cp "$dumpout"/raw/"$line""_2.$suf" "$dumpout"/trimmed/"$line""_2_trimmed.fq"
fi
if [[ "$fastq_trimmed" == false ]]; then
if [[ "$do_fastqc" == true ]]; then
echo "[$line] trimming complete, performing fastqc..."
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$line""_1_trimmed.fq"
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$line""_2_trimmed.fq"
fi
fi
# remove unneeded raw
if [[ "$clean_fastq_raw" == true ]]; then
rm "$dumpout"/raw/"$line""_1.$suf"
rm "$dumpout"/raw/"$line""_2.$suf"
fi
fi
echo "[$(date '+%d/%m/%Y %H:%M:%S')] finished processing $line"
echo ""
}
# is repeated for each local file provided, performs fastqc and trimming; automatic paired-end recognition
fastqGrabLocal () {
sname=$(basename "$fq")
t=$(echo "$sname" | cut -d '.' -f1)
tt=$(echo "$t" | cut -d '_' -f1)
# automatically detects the suffix
if [[ -f $fastq_in/$tt"_1.fastq" ]]; then
suf="fastq"
PE=true
echo "$tt is a paired-end file ending in .fastq"
elif [[ -f $fastq_in/$tt"_1.fq" ]]; then
suf="fq"
PE=true
echo "$tt is a paired-end file ending in .fq"
elif [[ -f $fastq_in/$tt".fastq" ]]; then
suf="fastq"
PE=false
echo "$tt is a single-end file ending in .fastq"
elif [[ -f $fastq_in/$tt".fq" ]]; then
suf="fq"
PE=false
echo "$tt is a single-end file ending in .fq"
elif [[ -f $fastq_in/$tt"_1.fastq.gz" ]]; then
suf="fastq.gz"
PE=true
echo "$tt is a paired-end file ending in .fastq"
elif [[ -f $fastq_in/$tt"_1.fq.gz" ]]; then
suf="fq.gz"
PE=true
echo "$tt is a paired-end file ending in .fq"
elif [[ -f $fastq_in/$tt".fastq.gz" ]]; then
suf="fastq.gz"
PE=false
echo "$tt is a single-end file ending in .fastq"
elif [[ -f $fastq_in/$tt".fq.gz" ]]; then
suf="fq.gz"
PE=false
echo "$tt is a single-end file ending in .fq"
else
echo "suffix not recognized, please check your datasets"
exit 1
fi
if [[ "$PE" = false ]]; then
if [[ "$fastq_trimmed" == true ]]; then
echo "[$sname] is already trimmed, skipping trimming step..."
cp "$fq" "$dumpout"/trimmed/"$tt""_trimmed.fq"
if [[ "$do_fastqc" == true ]]; then
echo "[$sname] performing fastqc on raw file..."
fastqc "$fq" -o "$dumpout"/fastqc_results
fi
else
if [[ "$do_fastqc" == true ]]; then
fastqc "$fq" -o "$dumpout"/fastqc_results &
fi
echo "[$sname] trimming..."
trim_galore -o "$dumpout"/trimmed "$fq" --dont_gzip
if [[ "$do_fastqc" == true ]]; then
echo "[$sname] trimming complete, performing fastqc..."
fastqc "$dumpout"/trimmed/"$tt""_trimmed.fq" -o "$dumpout"/fastqc_results
fi
fi
else
if [[ "$fastq_trimmed" == true ]]; then
echo "[$sname] is already trimmed, skipping trimming step..."
cp "$fastq_in"/"$tt""_1.$suf" "$dumpout"/trimmed/"$tt""_1_trimmed.fq"
cp "$fastq_in"/"$tt""_2.$suf" "$dumpout"/trimmed/"$tt""_2_trimmed.fq"
if [[ "$do_fastqc" == true ]]; then
echo "[$sname] performing fastqc on raw file..."
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$tt""_1_trimmed.fq"
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$tt""_2_trimmed.fq"
fi
else
if [[ "$do_fastqc" == true ]]; then
echo "[$sname] performing fastqc on raw file..."
fastqc -o "$dumpout"/fastqc_results "$fastq_in"/"$tt""_1.$suf"
fastqc -o "$dumpout"/fastqc_results "$fastq_in"/"$tt""_2.$suf"
fi
echo "[$sname] trimming..."
trim_galore --paired \
-o "$dumpout"/trimmed \
"$fastq_in"/"$tt""_1.$suf" "$fastq_in"/"$tt""_2.$suf" \
--dont_gzip
# dealing with trim galore naming convention under pe
mv "$dumpout"/trimmed/"$tt""_1_val_1.fq" "$dumpout"/trimmed/"$tt""_1_trimmed.fq"
mv "$dumpout"/trimmed/"$tt""_2_val_2.fq" "$dumpout"/trimmed/"$tt""_2_trimmed.fq"
if [[ "$do_fastqc" == true ]]; then
echo "[$sname] trimming complete, performing fastqc..."
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$tt""_1_trimmed.fq"
fastqc -o "$dumpout"/fastqc_results "$dumpout"/trimmed/"$tt""_2_trimmed.fq"
fi
fi
fi
# choosing not to remove user provided raw fastq
}
# called upon completion of each sorted BAM files, takes the file through pre-processing, and performs hamr
hamrBranch () {
if [[ $currProg_mod == "2" ]]; then
#adds read groups using picard, note the RG arguments are disregarded here
echo "[$smpkey] adding/replacing read groups..."
gatk AddOrReplaceReadGroups \
I="$smpout"/sort_accepted.bam \
O="$smpout"/sorted_RG.bam \
RGPU=HWI-ST1395:97:d29b4acxx:8 \
RGID=1 \
RGSM=xxx \
RGLB=xxx \
RGPL=illumina
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished adding/replacing read groups (MOD 1/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 1/7, pipeline halted"
exit 1
fi
# RG finished without exiting
echo "3" > "$smpout"/progress_mod.txt
currProg_mod="3"
fi
wait
if [[ $currProg_mod == "3" ]]; then
#filter the accepted hits by uniqueness
echo "[$smpkey] filtering uniquely mapped reads..."
samtools view \
-h "$smpout"/sorted_RG.bam \
| perl "$filter" 1 \
| samtools view -bS - \
| samtools sort \
-o "$smpout"/sorted_RG_unique.bam
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished filtering (MOD 2/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 2/7, pipeline halted"
exit 1
fi
# filtering unique completed without erroring out if this is reached
echo "4" > "$smpout"/progress_mod.txt
currProg_mod="4"
fi
wait
if [[ $currProg_mod == "4" ]]; then
echo "[$smpkey] excluding read ends..."
# first index the input bam file with samtools
samtools index \
"$smpout"/sorted_RG_unique.bam \
-o "$smpout"/sorted_RG_unique.bai
wait
# ignore read ends
#######!!!!!!!!!debugging decision, change back later!!!!!!!!##########
# mv "$smpout"/sorted_RG_unique.bam "$smpout"/sorted_RG_unique_endsIGN.bam
python $execignoreends \
-5p 1 -3p 1 \
"$smpout"/sorted_RG_unique.bam \
"$smpout"/sorted_RG_unique_endsIGN.bam
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished excluding (MOD 3/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 3/7, pipeline halted"
exit 1
fi
####################################################################
# removing ends finished without exiting
echo "5" > "$smpout"/progress_mod.txt
currProg_mod="5"
fi
wait
if [[ $currProg_mod == "5" ]]; then
#reorder the reads using picard
# 6/26 adding allow discordance, remove ends might have messed with the length?
echo "[$smpkey] reordering..."
gatk --java-options "-Xmx2g -Djava.io.tmpdir=$smpout/tmp" ReorderSam \
I="$smpout"/sorted_RG_unique_endsIGN.bam \
O="$smpout"/sorted_RG_unique_endsIGN_reordered.bam \
R="$genome" \
CREATE_INDEX=TRUE \
SEQUENCE_DICTIONARY="$dict" \
TMP_DIR="$smpout"/tmp \
ALLOW_CONTIG_LENGTH_DISCORDANCE=true
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished reordering (MOD 4/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 4/7, pipeline halted"
exit 1
fi
# ordering finished without exiting
echo "6" > "$smpout"/progress_mod.txt
currProg_mod="6"
fi
wait
if [[ $currProg_mod == "6" ]]; then
#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"/sorted_RG_unique_endsIGN_reordered.bam \
-O "$smpout"/sorted_RG_unique_endsIGN_reordered_SNC.bam
# apparently this is now outdated
# -U ALLOW_N_CIGAR_READS
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished splitting N cigarring (MOD 5/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 5/7, pipeline halted"
exit 1
fi
# cigaring and spliting finished without exiting
echo "7" > "$smpout"/progress_mod.txt
currProg_mod="7"
fi
wait
if [[ $currProg_mod == "7" ]]; then
#final resorting using picard
echo "[$smpkey] resorting..."
gatk --java-options "-Xmx2g -Djava.io.tmpdir=$smpout/tmp" SortSam \
I="$smpout"/sorted_RG_unique_endsIGN_reordered_SNC.bam \
O="$smpout"/sorted_RG_unique_endsIGN_reordered_SNC_resorted.bam \
SORT_ORDER=coordinate
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished resorting (MOD 6/7)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at MOD 6/7, pipeline halted"
exit 1
fi
# cigaring and spliting finished without exiting
echo "8" > "$smpout"/progress_mod.txt
currProg_mod="8"
fi
wait
if [[ $currProg_mod == "8" ]]; then
#hamr step, can take ~1hr
echo "[$smpkey] hamr..."
#hamr_path=$(which hamr.py)
python $exechamrpy \
-fe "$smpout"/sorted_RG_unique_endsIGN_reordered_SNC_resorted.bam "$genome" "$model" "$smpout" $smpname $quality $coverage $err H4 $pvalue $fdr .05
status=$?
if [[ "$status" -eq 0 ]]; then
if [ ! -e "$smpout/${smpname}.mods.txt" ]; then
cd "$hamrout" || exit
printf '%s \n' "$smpname" >> zero_mod.txt
cd || exit
else
# HAMR needs separate folders to store temp for each sample, so we move at the end
cp "$smpout"/"${smpname}".mods.txt "$hamrout"
fi
echo "[$smpkey] finished hamr (MOD 7/7)"
else
echo "[$smpkey] returned non-zero exit status at MOD 7/7, pipeline halted"
exit 1
fi
echo "9" > "$smpout"/progress_mod.txt
currProg_mod="9"
fi
wait
# intermediate file clean up
if [[ $currProg_mod == "9" ]]; then
echo "[$smpkey] moving files around for depth analysis..."
# Move the unique_RG_ordered.bam and unique_RG_ordered.bai to a folder for read depth analysis
cp "$smpout"/sorted_RG_unique_endsIGN_reordered.bam "$out"/pipeline/depth/"$smpname".bam
cp "$smpout"/sorted_RG_unique_endsIGN_reordered.bai "$out"/pipeline/depth/"$smpname".bai
echo "[$smpkey] done"
# only delete intermediates if user didn't disable it
if [[ "$clean" == true ]]; then
# delete more intermediate files
echo "[$smpkey] removing large intermediate files..."
rm "$smpout"/sorted_RG.bam
rm "$smpout"/sorted_RG_unique.bam
rm "$smpout"/sorted_RG_unique_endsIGN.bam
rm "$smpout"/sorted_RG_unique_endsIGN_reordered.bam
rm "$smpout"/sorted_RG_unique_endsIGN_reordered_SNC.bam
rm "$smpout"/sorted_RG_unique_endsIGN_reordered_SNC_resorted.bam
fi
echo "[$smpkey] finished cleaning (MOD 7/7)"
echo "10" > "$smpout"/progress_mod.txt
fi
}
# called upon completion of each sorted BAM files, takes the sorted BAM through lncRNA prediction pipeline
lncCallBranch () {
cd $smpout
if [[ $currProg_lnc == "2" ]]; then
echo "[$smpkey] running stringtie for gtf conversion..."
# turn bam into gtf
# depends on if user supplied bam, if so, that's used and is called sort_accepted
if [[ -z "$bam_in" ]]; then
stringtie \
sort_accepted_lnc.bam \
-G $annotation -o stringtie_out.gtf \
-f 0.05 -j 9 -c 7 -s 20
else
stringtie \
sort_accepted.bam \
-G $annotation -o stringtie_out.gtf \
-f 0.05 -j 9 -c 7 -s 20
fi
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished converting bam to gtf (LNC 1/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 1/15, pipeline halted"
exit 1
fi
echo "3" > "$smpout"/progress_lnc.txt
currProg_lnc="3"
fi
if [[ $currProg_lnc == "3" ]]; then
echo "[$smpkey] merging sample gtf with reference gtf..."
# merge gtf from bam with ref gtf
stringtie --merge -G $annotation \
-o stringtie_merge_out.gtf \
stringtie_out.gtf
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished merging (LNC 2/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 2/15, pipeline halted"
exit 1
fi
echo "4" > "$smpout"/progress_lnc.txt
currProg_lnc="4"
fi
if [[ $currProg_lnc == "4" ]]; then
echo "[$smpkey] running gffcompare on merged gtf..."
# gffcmp merged gtf
gffcompare -r $annotation stringtie_merge_out.gtf
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished gffcompare (LNC 3/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 3/15, pipeline halted"
exit 1
fi
echo "5" > "$smpout"/progress_lnc.txt
currProg_lnc="5"
fi
if [[ $currProg_lnc == "5" ]]; then
echo "[$smpkey] filtering..."
# filter with awk
awk '$7 != "." {print}' gffcmp.annotated.gtf > filtered_gffcmp_annotated.gtf
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished filtering (LNC 4/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 4/15, pipeline halted"
exit 1
fi
echo "6" > "$smpout"/progress_lnc.txt
currProg_lnc="6"
fi
if [[ $currProg_lnc == "6" ]]; then
echo "[$smpkey] filtering for UX..."
# filter with grep for UX class codes
grep -E 'class_code "u";|class_code "x";' filtered_gffcmp_annotated.gtf > UXfiltered_gffcmp_annotated.gtf
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished filtering (LNC 5/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 5/15, pipeline halted"
exit 1
fi
echo "7" > "$smpout"/progress_lnc.txt
currProg_lnc="7"
fi
if [[ $currProg_lnc == "7" ]] && [[ -s UXfiltered_gffcmp_annotated.gtf ]]; then
echo "[$smpkey] creating index file..."
# I could copy over the already made fai but... eh
samtools faidx $genome
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished indexing (LNC 6/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 6/15, pipeline halted"
exit 1
fi
echo "8" > "$smpout"/progress_lnc.txt
currProg_lnc="8"
fi
if [[ $currProg_lnc == "8" ]]; then
echo "[$smpkey] converting filtered gtf to gff3..."
# covnvert to gff3
gffread UXfiltered_gffcmp_annotated.gtf -T -o UXfiltered_gffcmp_annotated.gff3
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished conversion (LNC 7/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 7/15, pipeline halted"
exit 1
fi
echo "9" > "$smpout"/progress_lnc.txt
currProg_lnc="9"
fi
if [[ $currProg_lnc == "9" ]]; then
echo "[$smpkey] writing fa file from filtered gtf..."
# write gtf to fasta
gffread -w transcripts.fa -g $genome UXfiltered_gffcmp_annotated.gtf
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished writing (LNC 8/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 8/15, pipeline halted"
exit 1
fi
echo "10" > "$smpout"/progress_lnc.txt
currProg_lnc="10"
fi
if [[ $currProg_lnc == "10" ]]; then
echo "[$smpkey] analyzing for transcript coding probability with cpc2..."
# use cpc2 to analyze for coding probablity
python $execcpc -i transcripts.fa -o cpc2_output
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished analysis (LNC 9/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 9/15, pipeline halted"
exit 1
fi
echo "11" > "$smpout"/progress_lnc.txt
currProg_lnc="11"
fi
if [[ $currProg_lnc == "11" ]]; then
echo "[$smpkey] extracting transcripts with probability less than 0.5..."
# use awk to extract result
awk '$7 < 0.5' cpc2_output.txt > filtered_transcripts.txt
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished extraction (LNC 10/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 10/15, pipeline halted"
exit 1
fi
echo "12" > "$smpout"/progress_lnc.txt
currProg_lnc="12"
fi
if [[ $currProg_lnc == "12" ]]; then
echo "[$smpkey] using cpc2 results to filter gtf..."
# loop to fetch entries from gtf
inputFile="$smpout/filtered_transcripts.txt"
gtfFile="$smpout/UXfiltered_gffcmp_annotated.gtf"
outputFile="$smpout/cpc_filtered_transcripts.txt"
while IFS= read -r line; do
pattern=$(echo "$line" | cut -f1)
grep "$pattern" "$gtfFile" >> "$outputFile"
done < "$inputFile"
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished filtering (LNC 11/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 11/15, pipeline halted"
exit 1
fi
echo "13" > "$smpout"/progress_lnc.txt
currProg_lnc="13"
fi
if [[ $currProg_lnc == "13" ]]; then
echo "[$smpkey] creating fa file from cpc2 filtered gtf..."
# create fa from cpc filtered gtf
gffread cpc_filtered_transcripts.txt -g $genome -w rfam_in.fa
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished writing (LNC 12/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 12/15, pipeline halted"
exit 1
fi
echo "14" > "$smpout"/progress_lnc.txt
currProg_lnc="14"
fi
if [[ $currProg_lnc == "14" ]]; then
echo "[$smpkey] performing cmscan..."
# create fa from cpc filtered gtf
cmscan --nohmmonly \
--rfam --cut_ga --fmt 2 --oclan --oskip \
--clanin "$path_to_rfam"/Rfam.clanin -o "$smpout"/my.cmscan.out \
--tblout "$smpout"/my.cmscan.tblout "$path_to_rfam"/Rfam.cm "$smpout"/rfam_in.fa
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished (LNC 13/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 13/15, pipeline halted"
exit 1
fi
echo "15" > "$smpout"/progress_lnc.txt
currProg_lnc="15"
fi
if [[ $currProg_lnc == "15" ]]; then
echo "[$smpkey] sacnning against rfam and finishing lncRNA annotation..."
# tblout info extraction
inputFile="$smpout/my.cmscan.tblout"
gtfFile="$smpout/cpc_filtered_transcripts.txt"
outputFile="$smpout/rfam_filtered_transcripts.txt"
tail -n +3 "$inputFile" >> parsed_rfam_out.tblout
# created a python script to deal with infernal's space delimited file
cp "cpc_filtered_transcripts.txt" "rfam_filtered_transcripts.txt"
while IFS= read -r line; do
if [[ $line =~ ^#$ ]]; then
break
else
pattern=$(python "$scripts"/parser.py "$line")
sed -i "/$pattern/d" "$outputFile"
fi
done < "$smpout"/parsed_rfam_out.tblout
status=$?
if [[ "$status" -eq 0 ]]; then
echo "[$smpkey] finished lncRNA annotation (LNC 14/15)"
echo ""
else
echo "[$smpkey] returned non-zero exit status at LNC 14/15, pipeline halted"
exit 1
fi
echo "16" > "$smpout"/progress_lnc.txt
currProg_lnc="16"
fi
if [[ $currProg_lnc == "16" ]]; then
echo "[$smpkey] processing identified lncRNA into GTF..."
cat $annotation "$smpout"/rfam_filtered_transcripts.txt > "$smpout"/final_combined_unsorted.gtf
sort "$smpout"/final_combined_unsorted.gtf -o "$smpout"/final_combined.gtf
status=$?
if [[ "$status" -eq 0 ]]; then