forked from rkitchen/exceRpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceRpt_longRNA
1110 lines (949 loc) · 102 KB
/
exceRpt_longRNA
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
#########################################################################################
## ##
## ____ _ __ ____ _ _____ ##
## _____ _____ ___| _ \ _ __ | |_ / /___ ____ ____ _/ __ \/ | / / | ##
## / _ \ \/ / __/ _ \ |_) | '_ \| __| / / __ \/ __ \/ __ `/ /_/ / |/ / /| | ##
## | __/> < (_| __/ _ <| |_) | |_ / / /_/ / / / / /_/ / _, _/ /| / ___ | ##
## \___/_/\_\___\___|_| \_\ .__/ \__| /_/\____/_/ /_/\__, /_/ |_/_/ |_/_/ |_| ##
## |_| /____/ ##
## ##
## ##
## ##
## The extra-cellular RNA processing toolkit (exceRpt) optimised for longRNA analysis ##
## ##
## This pipeline processes a single longRNA sequence file from a single sample ##
## ##
## Author: Rob Kitchen (r.r.kitchen@gmail.com) ##
## ##
## Learn more at github.com/rkitchen/exceRpt ##
## ##
## Version 4.6.2 (2017-01-24) ##
## ##
#########################################################################################
EXCERPT_VERSION := 4.6.2
##
## 1) On the command line, be sure to specify the following MANDATORY parameters
##
OUTPUT_DIR := NULL
INPUT_FILE_PATH_R1 := NULL
INPUT_FILE_PATH_R2 := NULL
SAMPLE_NAME := NULL
## small hack to use only the first read of each pair
INPUT_FILE_PATH := $(INPUT_FILE_PATH_R1)
##
## 2) Choose the main organism for longRNA / genome alignment (hsa + hg19, hsa + hg38, or mmu + mm10)
##
MAIN_ORGANISM_GENOME_ID := hg38
##
## 3) *OPTIONAL* parameters for calibrator library and random barcodes
##
ADAPTER_SEQ := none
MIN_ADAPTER_BASES_3p := 7
CALIBRATOR_LIBRARY := NULL
TRIM_N_BASES_5p := 0
TRIM_N_BASES_3p := 0
RANDOM_BARCODE_LENGTH := 0
RANDOM_BARCODE_LOCATION := -5p -3p
KEEP_RANDOM_BARCODE_STATS := false
ENDOGENOUS_LIB_PRIORITY := gencode,miRNA,tRNA,piRNA,circRNA
##
## 4) Select whether pipeline is run locally, should be 'true' unless this is the Genboree.org implementation!
##
LOCAL_EXECUTION := true
REMOVE_LARGE_INTERMEDIATE_FILES := false
##
## 5) READ QUALITY filtering parameters
##
QFILTER_MIN_READ_FRAC := 80
QFILTER_MIN_QUAL := 20
##
## 6) ENDOGENOUS alignment parameters
##
DOWNSAMPLE_RNA_READS := NULL
MIN_READ_LENGTH := 18
STAR_outFilterMatchNmin := $(MIN_READ_LENGTH)
STAR_outFilterMatchNminOverLread := 0.9
STAR_outFilterMismatchNmax := 1
STAR_outFilterMismatchNoverLmax := 0.3
STAR_alignEndsType := Local
##
## 7) EXOGENOUS alignment parameters
##
## Choose what kind of EXOGENOUS alignments to attempt:
## - off : none
## - miRNA : map only to exogenous miRNAs in miRbase
## - on : map to exogenous miRNAs in miRbase AND the genomes of all sequenced species in ensembl/NCBI
##
MAP_EXOGENOUS := off
## for exogenous alignments (can only be 0 or 1)
MAX_MISMATCHES_EXOGENOUS := 0
## For the taxonomic interpretation:
EXOGENOUS_RIBOSOMAL_TAXA_MINFRAC := 0.95
EXOGENOUS_RIBOSOMAL_TAXA_BATCHSIZE := 20000
EXOGENOUS_RIBOSOMAL_TAXA_MINREADPERCENT := 0.001
EXOGENOUS_RIBOSOMAL_TAXA_MINREADS := 3
EXOGENOUS_GENOMES_TAXA_MINFRAC := 0.95
EXOGENOUS_GENOMES_TAXA_BATCHSIZE := 500000
EXOGENOUS_GENOMES_TAXA_MINREADPERCENT := 0.001
EXOGENOUS_GENOMES_TAXA_MINREADS := 3
##
## 8) If this is a local installation of the pipeline, be sure to also modify the parameters in steps 4, 5, and 6 below...
##
ifeq ($(LOCAL_EXECUTION),true)
##
## 5) Modify installation-specific variables
##
N_THREADS := 4
JAVA_RAM := 10G
#MAX_RAM := 64000000000
MAX_RAM := 10000000000
BOWTIE_CHUNKMBS := 2000
SAMTOOLS_SORT_MEM := 8G
## NB: The 'EXE_DIR' MUST be an ABSOLUTE PATH or sRNABench will fail!
EXE_DIR := /gpfs/scratch/fas/gerstein/rrk24/bin/smallRNAPipeline
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
#current_dir_rel := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
EXE_DIR := $(dir $(mkfile_path))
##
## 6) Check that the paths to the required 3rd party executables work!
##
JAVA_EXE := /usr/bin/java
FASTX_CLIP_EXE := $(EXE_DIR)/fastx_0.0.14/bin/fastx_clipper
FASTX_FILTER_EXE := $(EXE_DIR)/fastx_0.0.14/bin/fastq_quality_filter
#VIENNA_PATH := $(EXE_DIR)/ViennaRNA_2.1.5/bin
BOWTIE2_EXE := $(EXE_DIR)/bowtie2-2.2.4/bowtie2
#SAMTOOLS_EXE := $(EXE_DIR)/samtools-1.1/samtools
SAMTOOLS_EXE := $(EXE_DIR)/samtools-1.3/samtools
FASTQC_EXE := $(JAVA_EXE) -classpath $(EXE_DIR)/FastQC_0.11.2:$(EXE_DIR)/FastQC_0.11.2/sam-1.103.jar:$(EXE_DIR)/FastQC_0.11.2/jbzip2-0.9.jar
SRATOOLS_EXE := $(EXE_DIR)/sratoolkit.2.5.1-centos_linux64/bin/fastq-dump
EXCERPT_TOOLS_EXE := $(EXE_DIR)/exceRpt_Tools.jar
DATABASE_PATH := $(EXE_DIR)/DATABASE
STAR_EXE := $(EXE_DIR)/STAR_2.4.2a/bin/Linux_x86_64/STAR
STAR_GENOMES_DIR := $(DATABASE_PATH)/Genomes_BacteriaFungiMammalPlantProtistVirus
STAR_PARAMS_FILE_PATH := $(STAR_GENOMES_DIR)/STAR_Parameters_Exogenous.in
EXPRESS_EXE := $(EXE_DIR)/express-1.5.1-linux_x86_64/express
##
## Use the input path to infer filetype and short name
##
INPUT_FILE_NAME := $(notdir $(INPUT_FILE_PATH))
INPUT_FILE_ID := $(basename $(INPUT_FILE_NAME))
else
##
## These parameters are for the Genboree installation only
##
EXE_DIR := $(SCRATCH_DIR)
N_THREADS := $(N_THREADS)
JAVA_RAM := 64G
MAX_RAM := 64000000000
BOWTIE_CHUNKMBS := 8000
SAMTOOLS_SORT_MEM := 2G
FASTX_CLIP_EXE := fastx_clipper
FASTX_FILTER_EXE := fastq_quality_filter
VIENNA_PATH := NULL
BOWTIE2_EXE := bowtie2
SAMTOOLS_EXE := samtools
FASTQC_EXE := $(JAVA_EXE) -classpath $(FASTQC_EXE_DIR):$(FASTQC_EXE_DIR)/sam-1.103.jar:$(FASTQC_EXE_DIR)/jbzip2-0.9.jar
SRATOOLS_EXE := fastq-dump
SRNABENCH_EXE := $(SRNABENCH_EXE)
EXCERPT_TOOLS_EXE := $(EXCERPT_TOOLS_EXE)
DATABASE_PATH := $(EXCERPT_DATABASE)
## Path to sRNABench libraries
SRNABENCH_LIBS := $(SRNABENCH_LIBS)
STAR_EXE := STAR
STAR_GENOMES_DIR := $(STAR_GENOMES_DIR)
STAR_PARAMS_FILE_PATH := $(STAR_GENOMES_DIR)/STAR_Parameters_Exogenous.in
INPUT_FILE_NAME := $(notdir $(INPUT_FILE_PATH))
INPUT_FILE_ID := $(INPUT_FILE_ID)
endif
## Define current time
ts := `/bin/date "+%Y-%m-%d--%H:%M:%S"`
ONE := 1
## Define tool name for logging
PIPELINE_NAME := exceRpt_longRNA
##
## Initialise longRNA alignment parameters
##
#BOWTIE_SEED_LENGTH := 19
BOWTIE_SEED_LENGTH := $(MIN_READ_LENGTH)
##
## For sample quality control (QC)
##
MIN_TRANSCRIPTOME_MAPPED := 100000
MIN_GENOME_TRANSCRIPTOME_RATIO := 0.5
USEAGE :=
ifeq ($(INPUT_FILE_ID),NULL)
#USEAGE := "make -f smallRNA_pipeline INPUT_FILE_PATH=[required: absolute/path/to/input/.fa|.fq|.sra] N_THREADS=[required: number of threads] OUTPUT_DIR=<required: absolute/path/to/output> INPUT_FILE_ID=[required: samplename] ADAPTER_SEQ=[optional: will guess sequence if not provided here; none, if already clipped input] MAIN_ORGANISM=[optional: defaults to 'hsa'] MAIN_ORGANISM_GENOME_ID=[optional: defaults to 'hg38'] CALIBRATOR_LIBRARY=[optional: path/to/bowtie/index/containing/calibrator/sequences] TRNA_MAPPING=[optional: TRUE|FALSE, default is TRUE] GENCODE_MAPPING=[optional: TRUE|FALSE, default is TRUE] PIRNA_MAPPING=[optional: TRUE|FALSE, default is TRUE] MAP_EXOGENOUS=[optional: off|miRNA|on, default is miRNA]"
USEAGE := "make -f longRNA_pipeline INPUT_FILE_PATH=[required: absolute/path/to/input/.fa|.fq|.sra] N_THREADS=[required: number of threads] OUTPUT_DIR=<required: absolute/path/to/output> INPUT_FILE_ID=[required: samplename] ADAPTER_SEQ=[optional: will guess sequence if not provided here; none, if already clipped input] MAIN_ORGANISM=[optional: defaults to 'hsa'] MAIN_ORGANISM_GENOME_ID=[optional: defaults to 'hg38'] CALIBRATOR_LIBRARY=[optional: path/to/bowtie/index/containing/calibrator/sequences] MAP_EXOGENOUS=[optional: off|miRNA|on, default is miRNA]"
endif
##
## STAR dynamic parameter string
##
STAR_ENDOGENOUS_DYNAMIC_PARAMS := --alignEndsType $(STAR_alignEndsType) --outFilterMatchNmin $(MIN_READ_LENGTH) --outFilterMatchNminOverLread $(STAR_outFilterMatchNminOverLread) --outFilterMismatchNmax $(STAR_outFilterMismatchNmax) --outFilterMismatchNoverLmax $(STAR_outFilterMismatchNoverLmax)
STAR_EXOGENOUS_DYNAMIC_PARAMS := --outSAMtype BAM Unsorted --outSAMattributes Standard --alignEndsType EndToEnd --outFilterMatchNmin $(MIN_READ_LENGTH) --outFilterMatchNminOverLread 1.0 --outFilterMismatchNmax $(MAX_MISMATCHES_EXOGENOUS) --outFilterMismatchNoverLmax $(STAR_outFilterMismatchNoverLmax)
##
## Map reads to plant and virus miRNAs
##
ifeq ($(MAP_EXOGENOUS),miRNA) ## ALIGNMENT TO ONLY EXOGENOUS miRNA and rRNA
PROCESS_SAMPLE_REQFILE := EXOGENOUS_rRNA/ExogenousRibosomalAlignments.result.taxaAnnotated.txt
JAVA_RAM := 90G
else ifeq ($(MAP_EXOGENOUS),on) ## COMPLETE EXOGENOUS GENOME ALIGNMENT
PROCESS_SAMPLE_REQFILE := EXOGENOUS_genomes/ExogenousGenomicAlignments.result.taxaAnnotated.txt
JAVA_RAM := 90G
else
PROCESS_SAMPLE_REQFILE := endogenousAlignments_Accepted.txt.gz
endif
##
## List of plant and virus species IDs to which to map reads that do not map to the genome of the primary organism
##
#EXOGENOUS_MIRNA_SPECIES := $(shell cat $(SRNABENCH_LIBS)/libs/mature.fa | grep ">" | awk -F '-' '{print $$1}' | sed 's/>//g'| sort | uniq | tr '\n' ':' | rev | cut -c 2- | rev)
## Parameters to use for the bowtie mapping of calibrator oligos and rRNAs
BOWTIE2_MAPPING_PARAMS_CALIBRATOR := -D 15 -R 2 -N $(STAR_outFilterMismatchNmax) -L $(BOWTIE_SEED_LENGTH) -i S,1,0
#BOWTIE2_MAPPING_PARAMS_RRNA := -D 15 -R 2 -N $(MAX_MISMATCHES_EXOGENOUS) -L $(BOWTIE_SEED_LENGTH) -i S,1,0
#################################################
##
## Generate unique ID from the input fastq filename and user's sample ID
##
SAMPLE_ID := $(INPUT_FILE_ID)
ifneq ($(SAMPLE_NAME),NULL)
SAMPLE_ID := $(SAMPLE_ID)_$(SAMPLE_NAME)
endif
##
## Detect filetype and extract from SRA format if necessary
##
COMMAND_CONVERT_SRA := cat $(INPUT_FILE_PATH)
ifeq ($(suffix $(INPUT_FILE_NAME)),.sra)
COMMAND_CONVERT_SRA := $(SRATOOLS_EXE) --stdout $(INPUT_FILE_PATH)
else ifeq ($(suffix $(INPUT_FILE_NAME)),.gz)
COMMAND_CONVERT_SRA := gunzip -c $(INPUT_FILE_PATH)
else ifeq ($(suffix $(INPUT_FILE_NAME)),.bz2)
COMMAND_CONVERT_SRA := bzip2 -dc $(INPUT_FILE_PATH)
endif
##
## Guess quality encoding
##
#COMMAND_FILTER_BY_QUALITY ?= gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz | $(FASTX_FILTER_EXE) -v -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -p $(QFILTER_MIN_READ_FRAC) -q $(QFILTER_MIN_QUAL) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Are we downsampling the transcriptome alignments?
##
ifeq ($(DOWNSAMPLE_RNA_READS),NULL)
COMMAND_DOWNSAMPLE :=
else
COMMAND_DOWNSAMPLE := mv $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.original.txt; \
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.original.txt | shuf -n $(DOWNSAMPLE_RNA_READS) --random-source=$(DATABASE_PATH)/randomBits.dat > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt; \
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.original.txt > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.original.txt.gz
endif
##
## Logic block to write the adapter sequence (whether or not one is provided by the user) to the .adapterSeq file
##
#ifeq ($(ADAPTER_SEQ),NULL)
# COMMAND_WRITE_ADAPTER_SEQ := $(COMMAND_CONVERT_SRA) 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | head -n 40000000 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | $(BOWTIE2_EXE) --no-head -p $(N_THREADS) --local -D 15 -R 2 -N 0 -L 20 -i S,1,0.75 -k 2 --upto 10000000 -x $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/$(MAIN_ORGANISM_GENOME_ID) -U - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '{if ($$5==255) print $$0}' > $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.unique.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log; \
# cat $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.unique.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{print $$6}' 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort -rnk 1 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | head -n 100 > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).cigarFreqs 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
# cat $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.unique.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{if ($$2==0) print $$3"\t"$$4"\t"$$6"\t"$$10}' 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | grep "[[:space:]]2[0-9]M[0-9][0-9]S" > $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
# cat $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{print $$3}' 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort -rnk 1 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | head -n 100 > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).okCigarFreqs 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
# cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).okCigarFreqs 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | head -n 1 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{print substr($$2,1,2)}' > $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
# cat $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.sam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | grep "[[:space:]]$$(<$(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.txt)" 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{getline len<"$(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.txt"; print substr($$4,len+1)}' 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sed 's/[A]*$$//' 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | sort -rnk 1 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | awk '{if ($$1 > 75) print $$0}' > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).potentialAdapters.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
# head -n 1 $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).potentialAdapters.txt | awk '{print $$2}' > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq; \
# rm $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp.*
# LOGENTRY_WRITE_ADAPTER := $(ts) $(PIPELINE_NAME): Identifying unknown 3' adapter sequence. Removing 3' adapter sequence using fastX:\n
#else ifeq ($(ADAPTER_SEQ),guessKnown)
ifeq ($(ADAPTER_SEQ),guessKnown)
COMMAND_WRITE_ADAPTER_SEQ := $(COMMAND_CONVERT_SRA) 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) FindAdapter -n 10000 -m 1000000 -s 4 -a $(DATABASE_PATH)/adapters/adapters.fa - > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
LOGENTRY_WRITE_ADAPTER := $(ts) $(PIPELINE_NAME): Identifying 3' adapter from list of known sequences. Removing 3' adapter sequence using fastX:\n
else ifeq ($(ADAPTER_SEQ),none)
COMMAND_WRITE_ADAPTER_SEQ := echo 'no adapter' > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq;
COMMAND_CLIP_ADAPTER := $(COMMAND_CONVERT_SRA) | gzip -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz | wc -l | awk '{print "input\t"$$0/4"\nsuccessfully_clipped\tNA"}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
LOGENTRY_WRITE_ADAPTER := Provided 3' adapter clipped input sequence file. No clipping necessary.\n
else
COMMAND_WRITE_ADAPTER_SEQ := echo $(ADAPTER_SEQ) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq
LOGENTRY_WRITE_ADAPTER := $(ts) $(PIPELINE_NAME): Provided 3' adapter sequence. Removing 3' adapter sequence using fastX:\n
endif
## If no adapter clipping command has been set- use this one:
COMMAND_CLIP_ADAPTER ?= $(COMMAND_CONVERT_SRA) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).preClipped.fastq.tmp; $(FASTX_CLIP_EXE) $(FASTX_CLIP_COMMANDS_FOR_RANDOM_BARCODE) -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -a $(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq) -l $(MIN_READ_LENGTH) -v -n -M $(MIN_ADAPTER_BASES_3p) -i $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).preClipped.fastq.tmp -z -o $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; rm $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).preClipped.fastq.tmp
##
## Logic block to handle random adapter removal
##
ifeq ($(KEEP_RANDOM_BARCODE_STATS),true)
BARCODE_STATS_COMMAND := -stats $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.barcodeStats
else
BARCODE_STATS_COMMAND :=
endif
ifeq ($(RANDOM_BARCODE_LENGTH),0)
COMMAND_REMOVE_RANDOM_BARCODE := mv $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz
ENDOGENOUS_QUANT_RANDOM_BARCODE_STATS :=
FASTX_CLIP_COMMANDS_FOR_RANDOM_BARCODE :=
else
#COMMAND_REMOVE_RANDOM_BARCODE := gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessFastqWithRandomBarcode -n $(RANDOM_BARCODE_LENGTH) $(RANDOM_BARCODE_LOCATION) -stats $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.barcodeStats - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | gzip -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz
#ENDOGENOUS_QUANT_RANDOM_BARCODE_STATS := -randombarcode $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.barcodeStats
COMMAND_REMOVE_RANDOM_BARCODE := gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.tmp.gz | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessFastqWithRandomBarcode -min $(MIN_READ_LENGTH) -n $(RANDOM_BARCODE_LENGTH) $(RANDOM_BARCODE_LOCATION) $(BARCODE_STATS_COMMAND) - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | gzip -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz
ENDOGENOUS_QUANT_RANDOM_BARCODE_STATS :=
#
# if we are using random barcodes, reads input to the pipeline MUST HAVE A 3' ADAPTER!
FASTX_CLIP_COMMANDS_FOR_RANDOM_BARCODE := -c
endif
##
## Logic block for removing rRNAs and [optionally] calibrator sequences that may have been spiked into the sample
##
ifeq ($(CALIBRATOR_LIBRARY),NULL)
LOGENTRY_MAP_CALIBRATOR_1 := No calibrator sequences\n
LOGENTRY_MAP_CALIBRATOR_2 := Moving on to UniVec and rRNA sequences\n
COMMAND_COUNT_CALIBRATOR := echo -e "calibrator\tNA" >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
COMMAND_MAP_CALIBRATOR :=
FILE_TO_INPUT_TO_UNIVEC_ALIGNMENT := $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz
else
COMMAND_COUNT_CALIBRATOR := cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.calibratormapped.counts | awk 'BEGIN{sum=0} {sum+=$$1} END{print "calibrator\t"sum}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
COMMAND_MAP_CALIBRATOR := $(BOWTIE2_EXE) -p $(N_THREADS) $(BOWTIE2_MAPPING_PARAMS_CALIBRATOR) --un-gz $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noCalibrator.fastq.gz -x $(CALIBRATOR_LIBRARY) -U $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '$$2 != 4 {print $$0}' | $(SAMTOOLS_EXE) view -Sb - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | tee $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.calibratormapped.bam | $(SAMTOOLS_EXE) view - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '{print $$3}' | sort -k 2 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq --count > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.calibratormapped.counts 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
LOGENTRY_MAP_CALIBRATOR_1 := $(ts) $(PIPELINE_NAME): Mapping reads to calibrator sequences using bowtie:\n
LOGENTRY_MAP_CALIBRATOR_2 := $(ts) $(PIPELINE_NAME): Finished mapping to the calibrators\n
FILE_TO_INPUT_TO_UNIVEC_ALIGNMENT := $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noCalibrator.fastq.gz
endif
##
## STAR command to align reads to the UniVec contaminant sequence database
##
COMMAND_MAP_UNIVEC ?= $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_UniVec_ --genomeDir $(DATABASE_PATH)/UniVec/STAR_INDEX_UniVec --readFilesIn $(FILE_TO_INPUT_TO_UNIVEC_ALIGNMENT) --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_UniVec_Aligned.out.bam | awk '{print $$3}' | sort -k 2,2 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq --count > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.uniVecContaminants.counts 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_UniVec_Aligned.out.bam | awk '{print $$1}' | sort 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c | wc -l > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.uniVecContaminants.readCount 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_UniVec_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noUniVecContaminants.fastq.gz; \
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_UniVec_Unmapped.out.mate1
##
## STAR command to align reads to the rRNA sequences
##
COMMAND_MAP_RRNAS ?= $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_ --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_rRNA --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noUniVecContaminants.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.bam | awk '{print $$3}' | sort -k 2,2 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.rRNA.counts 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.bam | awk '{print $$1}' | sort 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | uniq -c | wc -l > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.rRNA.readCount 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err; \
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz; \
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Unmapped.out.mate1
##
## Remove some potentially large intermediate pipeline output (can save as much as 50% total output size)
##
TIDYUP_COMMAND :=
ifeq ($(REMOVE_LARGE_INTERMEDIATE_FILES),true)
TIDYUP_COMMAND := rm $(OUTPUT_DIR)/$(SAMPLE_ID)/genome.parsed; rm $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped*.fastq.gz
endif
##
## Compress only the most vital output!
##
#ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/noGenome | awk '{print $$9}' | grep "sense.grouped\|stat" | awk '{print "$(SAMPLE_ID)/noGenome/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt;
COMPRESS_COMMAND := ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID) | awk '{print $$9}' | grep "readCounts_\|.readLengths.txt\|_fastqc.zip\|.counts\|.knownAdapterSeq\|.adapterSeq\|.qualityEncoding\|.CIGARstats.txt\|.coverage.txt" | awk '{print "$(SAMPLE_ID)/"$$1}' > $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
echo $(SAMPLE_ID).log >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
echo $(SAMPLE_ID).stats >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
echo $(SAMPLE_ID).qcResult >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID) | awk '{print $$9}' | grep "calibratormapped.counts" | awk '{print "$(SAMPLE_ID)/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA | awk '{print $$9}' | grep "readCounts_" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_miRNA/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_rRNA | awk '{print $$9}' | grep "ExogenousRibosomalAlignments.result.taxaAnnotated.txt" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_rRNA/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_genomes | awk '{print $$9}' | grep "ExogenousGenomicAlignments.result.taxaAnnotated.txt" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_genomes/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt
#ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_rRNA | awk '{print $$9}' | grep "readCounts_" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_rRNA/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt; \
#ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA | awk '{print $$9}' | grep "exogenous_miRBase_mapped" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_miRNA/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt
#ls -lh $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_rRNA | awk '{print $$9}' | grep "exogenous_miRBase_mapped" | awk '{print "$(SAMPLE_ID)/EXOGENOUS_rRNA/"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt
OUTDIR := $(OUTPUT_DIR)/$(SAMPLE_ID)
###########################################################
###########################################################
###########################################################
##
## Main make target
##
.PHONY: all
.DEFAULT: all
all: processSample
##
## Target to print help
##
help:
@echo -e ""
@echo -e "Help for the exceRpt longRNA pipeline (version v.$(EXCERPT_VERSION))"
@echo -e ""
@echo -e "Usage: make -f path/to/exceRpt_longRNA [OPTION1=value1 OPTION2=value2 ...]"
@echo -e ""
@echo -e "*Required* OPTIONs:"
@echo -e " INPUT_FILE_PATH_R1 | Path to the input fastq file corresponding to the FIRST read of each pair"
@echo -e " OUTPUT_DIR | Path to store the output results"
@echo -e ""
@echo -e "For paired-end RNA-seq reads:"
@echo -e " INPUT_FILE_PATH_R2 | Path to the input fastq file corresponding to the SECOND read of each pair"
@echo -e ""
@echo -e "Main analysis OPTIONs:"
@echo -e " DATABASE_PATH | <Path> | [default: '$(DATABASE_PATH)''] change the location of the exceRpt database [by default this is within the EXE_DIR specified above]"
@echo -e " ADAPTER_SEQ | 'guessKnown'/'none'/<String> | [default: '$(ADAPTER_SEQ)'] will attempt to guess the 3 adapter using known sequences. The actual adapter can be input here if known, or specify 'none' if the adapter is already removed"
@echo -e " SAMPLE_NAME | <String> | add an optional ID to the input file specified above"
@echo -e " MAIN_ORGANISM_GENOME_ID | 'hg38'/'hg19'/'mm10' | [default: '$(MAIN_ORGANISM_GENOME_ID)'] changes the organism/genome build used for alignment"
@echo -e " CALIBRATOR_LIBRARY | <Path> | path to a bowtie2 index of calibrator oligos used for QC or normalisation"
@echo -e " MAP_EXOGENOUS | 'off'/'miRNA'/'on' | [default: '$(MAP_EXOGENOUS)'] choose whether to also align to known exogenous miRNAs+rRNAs ['miRNA'] or also to the full genomes of exogenous species ['on']"
@echo -e " ENDOGENOUS_LIB_PRIORITY | <comma,separated,list,no,spaces> | [default: '$(ENDOGENOUS_LIB_PRIORITY)'] choose the priority of each library during read assignment and quantification"
@echo -e ""
@echo -e "Additional analysis OPTIONs:"
@echo -e " TRIM_N_BASES_5p | <int> | [default: '$(TRIM_N_BASES_5p)'] remove N bases from the 5' end of every read"
@echo -e " TRIM_N_BASES_3p | <int> | [default: '$(TRIM_N_BASES_3p)'] remove N bases from the 3' end of every read"
@echo -e " RANDOM_BARCODE_LENGTH | <int> | [default: $(RANDOM_BARCODE_LENGTH)] identify and remove random barcodes of this number of nucleotides. For a Bioo prep with a 4N random barcode on both the 3' and 5' adapter, this value should be '4'."
@echo -e " RANDOM_BARCODE_LOCATION | '-5p -3p'/'-5p'/'-3p' | [default: '$(RANDOM_BARCODE_LOCATION)'] specify where to look for the random barcode(s)"
@echo -e " KEEP_RANDOM_BARCODE_STATS | 'false'/'true' | [default: '$(KEEP_RANDOM_BARCODE_STATS)'] specify whether or not to calculate overrepresentation statistics using the random barcodes (this may be slow and memory intensive!)"
@echo -e " DOWNSAMPLE_RNA_READS | <int> | [default: $(DOWNSAMPLE_RNA_READS)] choose whether to downsample to this number of reads after assigning reads to the various transcriptome libraries (may be useful for normalising very different yields)"
@echo -e ""
@echo -e "Hardware-specific OPTIONs:"
@echo -e " N_THREADS | <int> | [default: $(N_THREADS)] change the number of threads used in the alignments performed by exceRpt"
@echo -e " JAVA_RAM | <String> | [default: '$(JAVA_RAM)'] change the amount of memory (RAM) available to Java. This may need to be higher if crashes occur during quantification or random barcode stats calculation"
@echo -e " REMOVE_LARGE_INTERMEDIATE_FILES | 'false'/'true' | [default: '$(REMOVE_LARGE_INTERMEDIATE_FILES)'] when exceRpt finishes, choose whether to remove the large alignment files that can take a lot of disk space"
@echo -e ""
@echo -e "Alignment/QC OPTIONs:"
@echo -e " MIN_READ_LENGTH | <int> | [default: $(MIN_READ_LENGTH)] minimum read-length to use after adapter (+ random barcode) removal"
@echo -e " QFILTER_MIN_QUAL | <int> | [default: $(QFILTER_MIN_QUAL)] minimum base-call quality of the read"
@echo -e " QFILTER_MIN_READ_FRAC | <double> | [default: $(QFILTER_MIN_READ_FRAC)] read must have base-calls higher than the value above for at least this fraction of its length"
@echo -e " STAR_alignEndsType | 'Local'/'EndToEnd' | [default: $(STAR_alignEndsType)] defines the alignment mode; local alignment is recommended to allow for isomiRs"
@echo -e " STAR_outFilterMatchNmin | <int> | [default: $(STAR_outFilterMatchNmin)] minimum number of bases to include in the alignment (should match the minimum read length defined above)"
@echo -e " STAR_outFilterMatchNminOverLread | <double> | [default: $(STAR_outFilterMatchNminOverLread)] minimum fraction of the read that *must* remain following soft-clipping in a local alignment"
@echo -e " STAR_outFilterMismatchNmax | <int> | [default: $(STAR_outFilterMismatchNmax)] maximum allowed mismatched bases in the aligned portion of the read"
@echo -e " MAX_MISMATCHES_EXOGENOUS | <int> | [default: $(MAX_MISMATCHES_EXOGENOUS)] maximum allowed mismatched bases in the *entire* read when aligning to exogenous sequences"
@echo -e ""
##
## Target to selectively compress only the most useful results for downstream processing
##
## - this will typically reduce the volume of data needing to be transferred by 100x
##
compressCoreResults:
$(COMPRESS_COMMAND)
tar -cvz -C $(OUTPUT_DIR) -T $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt -f $(OUTPUT_DIR)/$(SAMPLE_ID)_CORE_RESULTS_v$(EXCERPT_VERSION).tgz 2> /dev/null
rm $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt
##
## Delete sample results and logfiles
##
#clean:
# rm -r $(OUTPUT_DIR)/$(SAMPLE_ID)
####
#### Main sub-target
####
processSample: $(OUTDIR)/$(PROCESS_SAMPLE_REQFILE)
## Wrap up logging and stats files
@echo -e "$(ts) $(PIPELINE_NAME): END longRNA-seq Pipeline for sample $(SAMPLE_ID)\n======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): END\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "#END OF STATS from the exceRpt longRNA-seq pipeline. Run completed at $(ts)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#
## Adapter confidence
echo -e "known: " >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).knownAdapterSeq >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
echo -e "used: " >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp | tr '\n' ' ' | awk -F ' ' '{if($$2=="used:"){ if(NF==2){print "Adapter_confidence: LOW"}else{print "Adapter_confidence: WARN_unableToGuessAdapter_usingProvided("$$3")"}}else{if($$2==$$4){print "Adapter_confidence: HIGH"}else{print "Adapter_confidence: WARN_providedAdapter("$$4")DisagreesWithGuessed("$$2")"}}}' > $(OUTPUT_DIR)/$(SAMPLE_ID).qcResult
#
## Calculate QC result
cat $(OUTPUT_DIR)/$(SAMPLE_ID).stats | grep "^input" | head -n 1 | awk '{print $$2}' > $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID).stats | grep "^genome" | awk '{print $$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID).stats | grep "sense" | awk '{SUM+=$$2}END{print SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
cat $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp | tr '\n' '\t' | awk '{result="FAIL"; ratio=0; if($$2>0){ratio=$$3/$$2}; if(ratio>$(MIN_GENOME_TRANSCRIPTOME_RATIO) && $$3>$(MIN_TRANSCRIPTOME_MAPPED))result="PASS"}END{print "QC_result: "result"\nInputReads: "$$1"\nGenomeReads: "$$2"\nTranscriptomeReads: "$$3"\nTranscriptomeGenomeRatio: "ratio}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).qcResult
gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt.gz | wc -l > $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt.gz | awk '{print $$2}' | uniq | wc -l >> $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
#
cat $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp | tr '\n' '\t' | awk '{if($$1>0){print "TranscriptomeComplexity: "($$2/$$1)}else{print "TranscriptomeComplexity: 0"}}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).qcResult
rm $(OUTPUT_DIR)/$(SAMPLE_ID).qctmp
#
## Compress core results files automatically
$(COMPRESS_COMMAND)
#tar -cvz -C $(OUTPUT_DIR) -T $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt -f $(OUTPUT_DIR)/$(SAMPLE_ID)_results.tgz 2> /dev/null
tar -cvz -C $(OUTPUT_DIR) -T $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt -f $(OUTPUT_DIR)/$(SAMPLE_ID)_CORE_RESULTS_v$(EXCERPT_VERSION).tgz 2> /dev/null
rm $(OUTPUT_DIR)/$(SAMPLE_ID)_filesToCompress.txt
## END PIPELINE
##
###
#### BEGIN PIPELINE
####
#### vvv Sub-targets to do the read-preprocessing, calibrator mapping, rRNA mapping, en-exRNA mapping, and ex-exRNA mapping vvv
###
##
##
## Make results directory & Write adapter sequence
##
$(OUTDIR)/Progress_1_FoundAdapter.dat:
#$(EXPORT_CMD)
@echo -e "$(USEAGE)"
mkdir -p $(OUTPUT_DIR)/$(SAMPLE_ID)
@echo -e "$(ts) $(PIPELINE_NAME): BEGIN exceRpt longRNA-seq pipeline v.$(EXCERPT_VERSION) for sample $(SAMPLE_ID)\n======================\n" > $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): BEGIN \n" > $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Created results dir: $(OUTPUT_DIR)/$(SAMPLE_ID)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Processing adapter sequence:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_WRITE_ADAPTER_SEQ)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
$(COMMAND_WRITE_ADAPTER_SEQ)
@echo -e "$(ts) $(PIPELINE_NAME): Progress_1_FoundAdapter" > $(OUTPUT_DIR)/$(SAMPLE_ID)/Progress_1_FoundAdapter.dat
#
@echo -e "#STATS from the exceRpt longRNA-seq pipeline v.$(EXCERPT_VERSION) for sample $(SAMPLE_ID). Run started at $(ts)" > $(OUTPUT_DIR)/$(SAMPLE_ID).stats
@echo -e "Stage\tReadCount" >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
##
## Guess Fastq quality encoding
##
$(OUTDIR)/$(SAMPLE_ID).qualityEncoding: $(OUTDIR)/Progress_1_FoundAdapter.dat
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Guessing encoding of fastq read-qualities:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## ASCII 84 is equal to Q20 (p<0.01) in Phred+64, so any file with max quals greater than this can reasonably assumed to be Phred+64
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_CONVERT_SRA) | head -n 40000 | awk '{if(NR%4==0) printf("%s",$$0);}' | od -A n -t u1 | grep -v "^\*" | awk 'BEGIN{min=100;max=0;}{for(i=1;i<=NF;i++) {if($$i>max) max=$$i; if($$i<min) min=$$i;}}END{if(max<84) print "33"; else print "64";}' > $@\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_CONVERT_SRA) | head -n 40000 | awk '{if(NR%4==0) printf("%s",$$0);}' | od -A n -t u1 | grep -v "^\*" | awk 'BEGIN{min=100;max=0;}{for(i=1;i<=NF;i++) {if($$i>max) max=$$i; if($$i<min) min=$$i;}}END{if(max<84) print "33"; else print "64";}' > $@
@echo -e "$(ts) $(PIPELINE_NAME): Finished guessing encoding of fastq read-qualities:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## CLIP 3' adapter sequence
##
$(OUTDIR)/$(SAMPLE_ID).clipped.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).qualityEncoding
## Run the SW alignment of known adapters regardless of user preference
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Checking adapter against known sequences: $(COMMAND_CONVERT_SRA) 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) FindAdapter -n 1000 -m 100000 -s 4 -a $(DATABASE_PATH)/adapters/adapters.fa - > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).knownAdapterSeq 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_CONVERT_SRA) 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) FindAdapter -n 1000 -m 100000 -s 4 -a $(DATABASE_PATH)/adapters/adapters.fa - > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).knownAdapterSeq 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#@echo -e "$(ts) $(PIPELINE_NAME): Known adapter sequence: $(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).knownAdapterSeq)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Carry on with the adapter provided / guessed
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Provided/guessed adapter sequence: $(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).adapterSeq)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(LOGENTRY_WRITE_ADAPTER)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_CLIP_ADAPTER)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
$(COMMAND_CLIP_ADAPTER)
@echo -e "$(ts) $(PIPELINE_NAME): Finished removing adapters\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Count reads input to adapter clipping
grep "Input: " $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '{print "input\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
## Count reads output following adapter clipping
grep "Output: " $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '{print "successfully_clipped\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
## Remove random barcodes if there are any
@echo -e "$(ts) $(PIPELINE_NAME): Removing $(RANDOM_BARCODE_LENGTH)N random barcode:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_REMOVE_RANDOM_BARCODE)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
$(COMMAND_REMOVE_RANDOM_BARCODE)
@echo -e "$(ts) $(PIPELINE_NAME): Finished removing random barcode\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Trim reads at the 5' and/or 3' end?
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).clipped.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Trimming $(TRIM_N_BASES_5p) bases from the 5' and $(TRIM_N_BASES_3p) bases from the 3' of all reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#@echo -e "$(ts) $(PIPELINE_NAME): gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz | $(FASTX_TRIMMER_EXE) -z -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -f $(shell expr $(TRIM_N_BASES_5p) + $(ONE) ) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz | $(FASTX_TRIMMER_EXE) -z -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -f $(shell expr $(TRIM_N_BASES_5p) + $(ONE) ) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) TrimFastq -5p $(TRIM_N_BASES_5p) -3p $(TRIM_N_BASES_3p) | gzip -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.fastq.gz | $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) TrimFastq -5p $(TRIM_N_BASES_5p) -3p $(TRIM_N_BASES_3p) | gzip -c > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished trimming bases from the 5' and 3' end of all reads\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## FILTER clipped reads that have poor overall base quality & Remove homopolymer repeats
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.fastq.gz $(OUTDIR)/$(SAMPLE_ID).qualityEncoding
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Filtering reads by base quality:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_FILTER_BY_QUALITY)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
#$(COMMAND_FILTER_BY_QUALITY)
@echo -e "$(ts) $(PIPELINE_NAME): gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz | $(FASTX_FILTER_EXE) -v -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -p $(QFILTER_MIN_READ_FRAC) -q $(QFILTER_MIN_QUAL) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.fastq.gz | $(FASTX_FILTER_EXE) -v -Q$(shell cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).qualityEncoding) -p $(QFILTER_MIN_READ_FRAC) -q $(QFILTER_MIN_QUAL) > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp 2>>$(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished filtering reads by base quality\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Count reads that failed the quality filter
grep "low-quality reads" $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk '{print "failed_quality_filter\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#
# Filter homopolymer reads (those that have too many single nt repeats)
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Filtering homopolymer repeat reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) RemoveHomopolymerRepeats -m 0.66 -i $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp -o $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) RemoveHomopolymerRepeats --verbose -m 0.66 -i $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp -o $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.REMOVEDRepeatReads.fastq
gzip $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.REMOVEDRepeatReads.fastq
gzip $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.tmp
@echo -e "$(ts) $(PIPELINE_NAME): Finished filtering homopolymer repeat reads\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Count homopolymer repeat reads that failed the quality filter
grep "Done. Sequences removed" $(OUTPUT_DIR)/$(SAMPLE_ID).log | awk -F "=" '{print "failed_homopolymer_filter\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
##
## Assess Read-lengths after clipping
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.readLengths.txt: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz
@echo -e "======================" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Calculating length distribution of clipped reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) GetSequenceLengths $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq > $@ 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
gunzip -c $< > $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) GetSequenceLengths $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq > $@ 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq
@echo -e "$(ts) $(PIPELINE_NAME): Finished calculating read-lengths\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Perform FastQC after adapter removal
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered_fastqc.zip: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz
@echo -e "======================" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Running FastQC on clipped reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(FASTQC_EXE) -Xmx$(JAVA_RAM) -Dfastqc.threads=$(N_THREADS) -Dfastqc.unzip=false -Dfastqc.output_dir=$(OUTPUT_DIR)/$(SAMPLE_ID)/ uk/ac/bbsrc/babraham/FastQC/FastQCApplication $< >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(FASTQC_EXE) -Xmx$(JAVA_RAM) -Dfastqc.threads=$(N_THREADS) -Dfastqc.unzip=false -Dfastqc.output_dir=$(OUTPUT_DIR)/$(SAMPLE_ID)/ uk/ac/babraham/FastQC/FastQCApplication $< >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished running FastQC\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## MAP to external bowtie (calibrator?) library and to UniVec sequences
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noUniVecContaminants.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.fastq.gz $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.readLengths.txt $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered_fastqc.zip
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(LOGENTRY_MAP_CALIBRATOR_1)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_MAP_CALIBRATOR)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_MAP_CALIBRATOR)
@echo -e "$(ts) $(PIPELINE_NAME): $(LOGENTRY_MAP_CALIBRATOR_2)" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Count calibrator oligo reads
$(COMMAND_COUNT_CALIBRATOR)
#
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to contaminant sequences in UniVec using STAR:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_MAP_UNIVEC)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_MAP_UNIVEC)
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to the UniVec contaminant DB\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Count UniVec contaminant reads
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.uniVecContaminants.readCount | awk '{print "UniVec_contaminants\t"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
##
## MAP to rRNA sequences
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noUniVecContaminants.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to ribosomal RNA sequences using STAR:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_MAP_RRNAS)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_MAP_RRNAS)
## Count rRNA reads
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.rRNA.readCount | awk ' {print "rRNA\t"$$1}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#
#$(SAMTOOLS_EXE) sort $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.rRNAmapped.bam $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.rRNAmapped.sorted
$(SAMTOOLS_EXE) sort -@ $(N_THREADS) -m 8G -O bam -T $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.sorted.bam
$(SAMTOOLS_EXE) index $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.sorted.bam
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/filteringAlignments_rRNA_Aligned.out.bam
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to the rRNAs\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Perform FastQC again after rRNA / UniVec removal
##
$(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA_fastqc.zip: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz
@echo -e "======================" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Running FastQC on cleaned reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(FASTQC_EXE) -Xmx$(JAVA_RAM) -Dfastqc.threads=$(N_THREADS) -Dfastqc.unzip=false -Dfastqc.output_dir=$(OUTPUT_DIR)/$(SAMPLE_ID)/ uk/ac/bbsrc/babraham/FastQC/FastQCApplication $< >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(FASTQC_EXE) -Xmx$(JAVA_RAM) -Dfastqc.threads=$(N_THREADS) -Dfastqc.unzip=false -Dfastqc.output_dir=$(OUTPUT_DIR)/$(SAMPLE_ID)/ uk/ac/babraham/FastQC/FastQCApplication $< >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished running FastQC\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Map reads to the endogenous genome and transcriptome
##
## map ALL READS to the GENOME (STAR ungapped)
$(OUTDIR)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz: $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz $(OUTDIR)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA_fastqc.zip
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to the genome of the primary organism:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_ --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_genome --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_ --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_genome --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/$(SAMPLE_ID).clipped.trimmed.filtered.noRiboRNA.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to the genome of the primary organism\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## sort the alignments by ReadID just in case these are paired end reads in a single file? -- no, better to flag that this is an invalid file (ToDo)
#
## v use this line when we start dealing with paired-end reads
#$(SAMTOOLS_EXE) fastq -1 $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate1 -2 $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate2 $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam
$(SAMTOOLS_EXE) fastq $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate1
#
## map ALL READS to the TRANSCRIPTOME (STAR ungapped)
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping genome-aligned reads to the transcriptome of the primary organism:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_ --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate1 --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_transcriptome --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) --readFilesCommand - >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_ --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate1 --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_transcriptome --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) --readFilesCommand - >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping genome-aligned reads to the transcriptome of the primary organism\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Unmapped.R1.fastq.gz
#
@echo -e "$(ts) $(PIPELINE_NAME): Mapping genome-unaligned reads to the transcriptome of the primary organism:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_ --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Unmapped.out.mate1 --outReadsUnmapped Fastx --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_transcriptome --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) --readFilesCommand - >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_ --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Unmapped.out.mate1 --outReadsUnmapped Fastx --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_transcriptome --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) --readFilesCommand - >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping genome-unaligned reads to the transcriptome of the primary organism\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz
#
## Count # mapped reads
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Log.final.out | grep "Number of input reads" | awk -F "|\t" '{print "reads_used_for_alignment\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#cat $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Log.final.out | grep "Uniquely mapped reads number\|Number of reads mapped to multiple loci" | awk -F "|\t" '{SUM+=$$2}END{print "reads_used_for_alignment\t"SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome*apped_transcriptome_Log.final.out | grep "Number of input reads\|Uniquely mapped reads number\|Number of reads mapped to multiple loci" | sed '2,4d' | awk -F "|\t" '{SUM+=$$2}END{print "genome\t"SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#
## Compress STAR logs
gzip $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Log.out
gzip $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Log.out
#
## Tidy up
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_SJ.out.tab
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_SJ.out.tab
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Mapped.out.mate1
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Unmapped.out.mate1
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Unmapped.out.mate1
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.out.mate1
## convert genomic alignments to wiggle file for display - CURRENTLY DISABLED - REQUIRES BAM TO BE SORTED!
$(OUTDIR)/endogenousAlignments_unspliced.wig: $(OUTDIR)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Converting genomic alignments to .wig format:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(SAMTOOLS_EXE) sort -@ $(N_THREADS) -m 8G -O bam -T $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam | $(SAMTOOLS_EXE) mpileup -t SP - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | perl -ne 'BEGIN{print "track type=wiggle_0 name=$(SAMPLE_ID) description=$(SAMPLE_ID)\n"};($$c, $$start, undef, $$depth) = split; if ($$c ne $$lastC) { print "variableStep chrom=$$c\n"; };$$lastC=$$c;next unless $$. % 10 ==0;print "$$start\t$$depth\n" unless $$depth<3;' > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.wig 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(SAMTOOLS_EXE) sort -@ $(N_THREADS) -m 8G -O bam -T $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam | $(SAMTOOLS_EXE) mpileup -t SP - 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | perl -ne 'BEGIN{print "track type=wiggle_0 name=$(SAMPLE_ID) description=$(SAMPLE_ID)\n"};($$c, $$start, undef, $$depth) = split; if ($$c ne $$lastC) { print "variableStep chrom=$$c\n"; };$$lastC=$$c;next unless $$. % 10 ==0;print "$$start\t$$depth\n" unless $$depth<3;' > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.wig 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished converting genomic alignments to .wig format\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Do QC on alignments
##
$(OUTDIR)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam.coverage.txt: $(OUTDIR)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
@echo -e "$(ts) $(PIPELINE_NAME): Performing CIGAR QC on endogenous genome alignments:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) CIGAR_2_PWM -f $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam.CIGARstats.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) CIGAR_2_PWM -f $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genome_Aligned.out.bam.CIGARstats.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished performing CIGAR QC on endogenous genome alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
@echo -e "$(ts) $(PIPELINE_NAME): Sorting endogenous transcriptome alignments:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(SAMTOOLS_EXE) sort -n -@ $(N_THREADS) -m $(SAMTOOLS_SORT_MEM) -O bam -T $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(SAMTOOLS_EXE) sort -n -@ $(N_THREADS) -m $(SAMTOOLS_SORT_MEM) -O bam -T $(OUTPUT_DIR)/$(SAMPLE_ID)/tmp $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.bam > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished sorting endogenous transcriptome alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
@echo -e "$(ts) $(PIPELINE_NAME): Performing read-coverage QC on endogenous transcriptome alignments:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ReadCoverage -exceRpt -a $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/gencodeAnnotation.gtf -f $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ReadCoverage -exceRpt -a $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/gencodeAnnotation.gtf -f $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished performing read-coverage QC on endogenous transcriptome alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam
## process alignments
$(OUTDIR)/endogenousAlignments_Accepted.txt.gz: $(OUTDIR)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz $(OUTDIR)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.sorted.bam.coverage.txt
#$(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt.gz: $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_unspliced.wig
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## Assign reads
@echo -e "$(ts) $(PIPELINE_NAME): Assigning and sorting reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessEndogenousAlignments --libPriority $(ENDOGENOUS_LIB_PRIORITY) --genomeMappedReads $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.bam --transcriptomeMappedReads $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Aligned.out.bam --hairpin2genome $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/miRNA_precursor2genome.sam --mature2hairpin $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/miRNA_mature2precursor.sam --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.dict 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | sort -k 2,2 -k 1,1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessEndogenousAlignments --libPriority $(ENDOGENOUS_LIB_PRIORITY) --genomeMappedReads $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeMapped_transcriptome_Aligned.out.bam --transcriptomeMappedReads $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Aligned.out.bam --hairpin2genome $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/miRNA_precursor2genome.sam --mature2hairpin $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/miRNA_mature2precursor.sam --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.dict 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | sort -k 2,2 -k 1,1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt
@echo -e "$(ts) $(PIPELINE_NAME): Finished assigning and sorting reads\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## Do we want to downsample?
@echo -e "$(ts) $(PIPELINE_NAME): If requested, downsampling to $(DOWNSAMPLE_RNA_READS) transcriptome alignments:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(COMMAND_DOWNSAMPLE)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(COMMAND_DOWNSAMPLE)
@echo -e "$(ts) $(PIPELINE_NAME): Finished downsampling transcriptome alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## Quantify all annotated RNAs
@echo -e "$(ts) $(PIPELINE_NAME): Quantifying:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) QuantifyEndogenousAlignments --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.dict --acceptedAlignments $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt --outputPath $(OUTPUT_DIR)/$(SAMPLE_ID)\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) QuantifyEndogenousAlignments --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.dict --acceptedAlignments $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt --outputPath $(OUTPUT_DIR)/$(SAMPLE_ID) 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## Summarise alignment statistics
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_miRNAmature_sense.txt | awk '{SUM+=$$4}END{printf "miRNA_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_miRNAmature_antisense.txt | awk '{SUM+=$$4}END{printf "miRNA_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_miRNAprecursor_sense.txt | awk '{SUM+=$$4}END{printf "miRNAprecursor_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_miRNAprecursor_antisense.txt | awk '{SUM+=$$4}END{printf "miRNAprecursor_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_tRNA_sense.txt | awk '{SUM+=$$4}END{printf "tRNA_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_tRNA_antisense.txt | awk '{SUM+=$$4}END{printf "tRNA_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_piRNA_sense.txt | awk '{SUM+=$$4}END{printf "piRNA_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_piRNA_antisense.txt | awk '{SUM+=$$4}END{printf "piRNA_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_gencode_sense.txt | awk '{SUM+=$$4}END{printf "gencode_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_gencode_antisense.txt | awk '{SUM+=$$4}END{printf "gencode_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_circRNA_sense.txt | awk '{SUM+=$$4}END{printf "circularRNA_sense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/readCounts_circRNA_antisense.txt | awk '{SUM+=$$4}END{printf "circularRNA_antisense\t%.0f\n",SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
## Count reads not mapping to the genome or to the libraries
gunzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz | wc -l | awk '{print "not_mapped_to_genome_or_libs\t"($$1/4)}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#
## Tidy up
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt.gz
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_Accepted.txt
##
## Align reads to repetitive element sequences, just in case repetitive reads have not been mapped to the genome
##
$(OUTDIR)/endogenousAlignments_repetitiveElements_Unmapped.R1.fastq.gz: $(OUTDIR)/endogenousAlignments_Accepted.txt.gz $(OUTDIR)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to repetitive elements in the host genome:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_ --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_repetitiveElements --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_ --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_repetitiveElements --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeUnmapped_transcriptome_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to repetitive elements in the host genome\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Assigned non-redundantly to annotated REs
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_Aligned.out.bam | grep -v "^@" | awk '{print $$1}' | sort | uniq | wc -l | awk '{print "repetitiveElements\t"$$0}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_Unmapped.R1.fastq.gz
##
## map REMAINING reads to the genome allowing gaps / novel splices
##
$(OUTDIR)/endogenousAlignments_genomeGapped_Unmapped.R1.fastq.gz: $(OUTDIR)/endogenousAlignments_repetitiveElements_Unmapped.R1.fastq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Aligning remaining reads to the genome allowing gaps \n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_ --alignIntronMax 0 --alignIntronMin 21 --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_genome --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_ --alignIntronMax 0 --alignIntronMin 21 --genomeDir $(DATABASE_PATH)/$(MAIN_ORGANISM_GENOME_ID)/STAR_INDEX_genome --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_repetitiveElements_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Endogenous_smallRNA.in $(STAR_ENDOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished aligning remaining reads to the genome allowing gaps\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## mapped to the genome with gaps
$(SAMTOOLS_EXE) view $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_Aligned.out.bam | grep -v "^@" | awk '{print $$1}' | sort | uniq | wc -l | awk '{print "endogenous_gapped\t"$$0}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_Unmapped.R1.fastq.gz
##
## Use the unmapped reads and search against all miRNAs in miRBase
##
$(OUTDIR)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.R1.fastq.gz: $(OUTDIR)/endogenousAlignments_genomeGapped_Unmapped.R1.fastq.gz
mkdir -p $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to all miRNAs in miRBase:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_ --genomeDir $(DATABASE_PATH)/miRBase/STAR_INDEX_miRBaseAll --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Exogenous.in $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_ --genomeDir $(DATABASE_PATH)/miRBase/STAR_INDEX_miRBaseAll --readFilesIn $(OUTPUT_DIR)/$(SAMPLE_ID)/endogenousAlignments_genomeGapped_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(DATABASE_PATH)/STAR_Parameters_Exogenous.in $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).err
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to all miRNAs in miRBase\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.out.mate1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.R1.fastq.gz
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.out.mate1
#
@echo -e "$(ts) $(PIPELINE_NAME): Assigning reads:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## quantify read alignments using a slight hack of the endogenous alignment engine
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessEndogenousAlignments --forceLib miRNA --transcriptomeMappedReads $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Aligned.out.bam --hairpin2genome $(DATABASE_PATH)/miRBase/miRNA_precursor2genome.sam --mature2hairpin $(DATABASE_PATH)/miRBase/miRNA_mature2precursor.sam --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.dict 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log | sort -k 2,2 -k 1,1 > $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.txt
#
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) QuantifyEndogenousAlignments --dict $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.dict --acceptedAlignments $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.txt --outputPath $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Finished assigning reads\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
## Tidy up:
gzip -c $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.txt > $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousMiRNAAlignments_Accepted.txt.gz
rm $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenousAlignments_Accepted.txt
#
## Stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Log.final.out | grep "Number of input reads" | awk -F "|\t" '{print "input_to_exogenous_miRNA\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
cat $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_miRNA/exogenous_miRBase_Log.final.out | grep "Uniquely mapped reads number\|Number of reads mapped to multiple loci" | awk -F "|\t" '{SUM+=$$2}END{print "exogenous_miRNA\t"SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
##
## Use the unmapped reads and search against all rRNAs in RDP (ribosome DB)
##
$(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz: $(OUTDIR)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.R1.fastq.gz
mkdir -p $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_rRNA
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to rRNA sequences in RDP (a.k.a. ribosome DB):\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): $(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_ --genomeDir $(DATABASE_PATH)/ribosomeDatabase/exogenous_rRNAs --readFilesIn $(OUTDIR)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_ --genomeDir $(DATABASE_PATH)/ribosomeDatabase/exogenous_rRNAs --readFilesIn $(OUTDIR)/EXOGENOUS_miRNA/exogenous_miRBase_Unmapped.R1.fastq.gz --outReadsUnmapped Fastx --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
## Input to exogenous rRNA alignment
grep "Number of input reads" $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Log.final.out | tr '[:blank:]' ' ' | awk -F " \\\| " '{print "input_to_exogenous_rRNA\t"$$2}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
## Assigned non-redundantly to annotated exogenous rRNAs
cat $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Log.final.out | grep "Uniquely mapped reads number\|Number of reads mapped to multiple loci" | awk -F "|\t" '{SUM+=$$2}END{print "exogenous_rRNA\t"SUM}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
#$(SAMTOOLS_EXE) view $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.out.bam | awk '{print $$1}' | sort | uniq | wc -l | awk '{print "exogenous_rRNA\t"$$0}' >> $(OUTPUT_DIR)/$(SAMPLE_ID).stats
## compress and tidy up
gzip -c $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Unmapped.out.mate1 > $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz
#$(SAMTOOLS_EXE) view -@ $(N_THREADS) -b $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.out.sam > $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.bam
rm $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Unmapped.out.mate1
#rm $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.out.sam
rm $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Log.out
@echo -e "$(ts) $(PIPELINE_NAME): Finished mapping to rRNA sequences in RDP\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Resolve the RDP rRNA alignments against the NCBI taxonomy
##
$(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.result.taxaAnnotated.txt: $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Resolving and quantifying rRNA alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "\n$(ts) $(PIPELINE_NAME): $(SAMTOOLS_EXE) view $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.out.bam | awk '{print $$1,$$3,$$4,$$6,$$10}' | sort -k 1,1 -k 2,2 | uniq > $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(SAMTOOLS_EXE) view $(OUTDIR)/EXOGENOUS_rRNA/exogenous_rRNA_Aligned.out.bam | awk '{print $$1,$$3,$$4,$$6,$$10}' | sort -k 1,1 -k 2,2 | uniq > $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.txt 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
@echo -e "\n$(ts) $(PIPELINE_NAME): $(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessExogenousAlignments -taxonomyPath $(DATABASE_PATH)/NCBI_taxonomy_taxdump -min $(EXOGENOUS_RIBOSOMAL_TAXA_MINREADPERCENT) -frac $(EXOGENOUS_RIBOSOMAL_TAXA_MINFRAC) --minReads $(EXOGENOUS_RIBOSOMAL_TAXA_MINREADS) -batchSize $(EXOGENOUS_RIBOSOMAL_TAXA_BATCHSIZE) -alignments $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.txt --rdp > $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.tmp 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(JAVA_EXE) -Xmx$(JAVA_RAM) -jar $(EXCERPT_TOOLS_EXE) ProcessExogenousAlignments -taxonomyPath $(DATABASE_PATH)/NCBI_taxonomy_taxdump -min $(EXOGENOUS_RIBOSOMAL_TAXA_MINREADPERCENT) -frac $(EXOGENOUS_RIBOSOMAL_TAXA_MINFRAC) --minReads $(EXOGENOUS_RIBOSOMAL_TAXA_MINREADS) -batchSize $(EXOGENOUS_RIBOSOMAL_TAXA_BATCHSIZE) -alignments $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.txt --rdp > $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.tmp 2>> $(OUTPUT_DIR)/$(SAMPLE_ID).log
#
# Tidy up
mv $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.tmp $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.result.taxaAnnotated.txt
rm $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.txt
@echo -e "$(ts) $(PIPELINE_NAME): Finished resolving and quantifying rRNA alignments\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
##
## Routines for aligning unmapped reads to exogenous sequences
##
## Bacteria
$(OUTDIR)/EXOGENOUS_genomes/Bacteria10_Aligned.out.bam: $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.result.taxaAnnotated.txt
mkdir -p $(OUTPUT_DIR)/$(SAMPLE_ID)/EXOGENOUS_genomes
@echo -e "======================\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to exogenous GENOMES of BACTERIA:\n" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
@echo -e "$(ts) $(PIPELINE_NAME): Bacteria1:" >> $(OUTPUT_DIR)/$(SAMPLE_ID).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria1_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA1 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria2:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria2_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA2 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria3:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria3_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA3 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria4:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria4_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA4 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria5:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria5_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA5 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria6:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria6_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA6 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria7:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria7_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA7 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria8:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria8_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA8 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria9:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria9_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA9 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Bacteria10:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Bacteria10_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_BACTERIA10 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Finished mapping to exogenous GENOMES of BACTERIA\n" >> $(OUTDIR).log
## Plants
$(OUTDIR)/EXOGENOUS_genomes/Plants5_Aligned.out.bam: $(OUTDIR)/EXOGENOUS_rRNA/ExogenousRibosomalAlignments.result.taxaAnnotated.txt
mkdir -p $(OUTDIR)/EXOGENOUS_genomes
@echo -e "======================\n" >> $(OUTDIR).log
@echo -e "$(ts) $(PIPELINE_NAME): Mapping reads to exogenous GENOMES of PLANTS:\n" >> $(OUTDIR).log
@echo -e "$(ts) $(PIPELINE_NAME): Plants1:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Plants1_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_PLANTS1 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Plants2:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Plants2_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_PLANTS2 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Plants3:" >> $(OUTDIR).log
$(STAR_EXE) --runThreadN $(N_THREADS) --outFileNamePrefix $(OUTDIR)/EXOGENOUS_genomes/Plants3_ --genomeDir $(STAR_GENOMES_DIR)/STAR_GENOME_PLANTS3 --readFilesIn $(OUTDIR)/EXOGENOUS_rRNA/unaligned.fq.gz --parametersFiles $(STAR_PARAMS_FILE_PATH) $(STAR_EXOGENOUS_DYNAMIC_PARAMS) >> $(OUTDIR).log
@echo -e "\n$(ts) $(PIPELINE_NAME): Plants4:" >> $(OUTDIR).log