forked from FelixKrueger/TrimGalore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim_galore
executable file
·2939 lines (2387 loc) · 112 KB
/
trim_galore
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use IPC::Open3;
use File::Spec;
use File::Basename;
use Cwd;
## This program is Copyright (C) 2012-19, Felix Krueger (felix.krueger@babraham.ac.uk)
## Edited by Frankie James (github.com/fjames003) for multi-core support
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## This script is taking in FastQ sequences and trims them using Cutadapt
## last modified on 20 Mar 2019
my $DOWARN = 1; # print on screen warning and text by default
BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } };
my $trimmer_version = '0.6.1_dev';
my $cutadapt_version;
my $python_version;
my ($compression_path,$cores,$cutoff,$adapter,$stringency,$rrbs,$length_cutoff,$keep,$fastqc,$non_directional,$phred_encoding,$fastqc_args,$trim,$gzip,$validate,$retain,$length_read_1,$length_read_2,$a2,$error_rate,$output_dir,$no_report_file,$dont_gzip,$clip_r1,$clip_r2,$three_prime_clip_r1,$three_prime_clip_r2,$nextera,$small_rna,$path_to_cutadapt,$illumina,$max_length,$maxn,$trim_n,$hardtrim5,$clock,$polyA,$hardtrim3,$nextseq,$basename) = process_commandline();
my @filenames = @ARGV;
die "\nPlease provide the filename(s) of one or more FastQ file(s) to launch Trim Galore!\n
USAGE: 'trim_galore [options] <filename(s)>' or 'trim_galore --help' for more options\n\n" unless (@filenames);
file_sanity_check($filenames[0]);
if (defined $hardtrim5){
warn "Hard-trimming from the 3'-end selected. File(s) will be trimmed to leave the leftmost $hardtrim5 bp on the 5'-end, and Trim Galore will then exit.\n\n";
foreach my $file(@filenames){
hardtrim_to_5prime_end($file);
}
exit;
}
if (defined $hardtrim3){
warn "Hard-trimming from 5'-end selected. File(s) will be trimmed to leave the rightmost $hardtrim3 bp on the 3'-end, and Trim Galore will then exit.\n\n";
foreach my $file(@filenames){
hardtrim_to_3prime_end($file);
}
exit;
}
if ($clock){
warn "\nIT'S TIME FOR CLOCK PROCESSING!!!\t\t\t\t\t\t\t\t\t[pun intended]\n\n";
while (@ARGV){
my $in1 = shift @ARGV;
my $in2 = shift @ARGV;
clockwork($in1,$in2);
}
warn "\nPre-processing finished...\n\nPlease run Trim Galore again to remove adapters, poor quality bases as well as UMI/fixed sequences from the 3'-end of the reads.\nA sample command for this is:\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ntrim_galore --paired --three_prime_clip_R1 15 --three_prime_clip_R2 15 *.clock_UMI.R1.fq.gz *.clock_UMI.R2.fq.gz\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTrim Galore Epigenetic Clock processing complete.\n\n";
exit;
}
sub clockwork{
### FILEHANDLES
my ($in1_fh,$in2_fh); # input filehandles
my ($read1_fh,$read2_fh); # output filehandles
### INPUT FILES
my ($in1,$in2) = @_;
if ($in1 =~ /\.gz$/){
open ($in1_fh,"$compression_path -d -c $in1 |") or die "Failed to read from file $in1: $!";
}
else{
open ($in1_fh,$in1) or die "Failed to read from file $in1: $!";
}
if ($in2 =~ /\.gz$/){
open ($in2_fh,"$compression_path -d -c $in2 |") or die "Failed to read from file $in2: $!";
}
else{
open ($in2_fh,$in2) or die "Failed to read from file $in2: $!";
}
# warn " Input file name 1: $in1\n";
# warn " Input file name 2: $in2\n";
### OUTPUT FILES
my $out1 = (split (/\//,$in1))[-1];
my $out2 = (split (/\//,$in2))[-1];
$out1 =~ s/(\.fastq$|\.fastq\.gz$)//;
$out1 =~ s/(\.fq$|\.fq\.gz$)//;
$out1 .= '.clock_UMI.R1.fq'; # appending to the end
$out2 =~ s/(\.fastq$|\.fastq\.gz$)//;
$out2 =~ s/(\.fq$|\.fq\.gz$)//;
$out2 .= '.clock_UMI.R2.fq'; # appending to the end
### READ 1
if ($gzip or $in1 =~ /\.gz$/){
if ($dont_gzip){
open ($read1_fh,'>',$output_dir.$out1) or die "Can't open '$out1': $!\n";
}
else{
$out1 .= '.gz';
open ($read1_fh,"| $compression_path -c - > ${output_dir}${out1}") or die "Can't write to '$out1': $!\n";
}
}
else{
open ($read1_fh,'>',$output_dir.$out1) or die "Can't open '$out1': $!\n";
}
warn "Writing dual trimmed version of the input file '$in1' to '$out1'\n";
### READ 2
if ($gzip or $in2 =~ /\.gz$/){
if ($dont_gzip){
open ($read2_fh,'>',$output_dir.$out2) or die "Can't open '$out2': $!\n";
}
else{
$out2 .= '.gz';
open ($read2_fh,"| $compression_path -c - > ${output_dir}${out2}") or die "Can't write to '$out2': $!\n";
}
}
else{
open ($read2_fh,'>',$output_dir.$out2) or die "Can't open '$out2': $!\n";
}
warn "Writing dual trimmed version of the input file '$in2' to '$out2'\n ---\n";
# warn "Output file name 1: $out1\n";
# warn "Output file name 2: $out2\n";
my %freqs;
my $umi_1;
my $umi_2;
# open ($out2,"| $compression_path -c - > $out1") or die $!;
# open (OUT2,"| $compression_path -c - > $out2") or die $!;
# print "Processing files $in1 and $in2\n";
my %r1; # storing the barcodes for R1
my %r2; # storing the barcodes for R2
my %fix1; # storing the fixed sequence (CAGT + A from A-tailing) of R1
my %fix2; # storing the fixed sequence (CAGT + A from A-tailing) of R2
my $count = 0;
my $filtered_count = 0;
my $r1_contains_rc = 0;
ORANGE: while (1){
my $one1 = <$in1_fh>;
my $one2 = <$in1_fh>;
my $one3 = <$in1_fh>;
my $one4 = <$in1_fh>;
my $two1 = <$in2_fh>;
my $two2 = <$in2_fh>;
my $two3 = <$in2_fh>;
my $two4 = <$in2_fh>;
last unless ($one4 and $two4);
chomp $one2; # sequence
chomp $two2; # sequence
chomp $one1; # read ID, need to append UMIs to the read ID
chomp $two1; # read ID, need to append UMIs to the read ID
chomp $one4; # quality
chomp $two4; # quality
++$count; # sequence count
if ($count % 1000000 ==0){
warn "Processed $count sequences so far...\n";
}
my $r1_barcode;
my $r2_barcode;
my $r1_fix;
my $r2_fix;
$r1_barcode = substr($one2,0,8);
$r2_barcode = substr($two2,0,8);
$r1_fix = substr($one2,8,4);
$r2_fix = substr($two2,8,4);
# warn "$one2\n$two2\n$r1_barcode\t$r1_fix\n$r2_barcode\t$r2_fix\n\n";sleep(1);
# this part of code simply counts the different types of sequence that occurred at the constant region where we expected
# to read CAGT in both reads. Both R1 and R2 are taken into account at the same time
unless ($r1_fix eq 'CAGT' and $r2_fix eq 'CAGT'){
$freqs{$r1_fix}++;
$freqs{$r2_fix}++;
$filtered_count++;
}
### BOTH READ1 AND READ2 should look like this:
# 0 8 12 13 INDEX position
# UUUUUUUU CAGT A FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF SEQUENCE
# where:
# U = UMI base
# CAGT = fixed sequence
# A = A-tail
# FFFF = RRBS-fragment to be aligned
### Capturing the sequence after the A from A-tailing (FFFFFFFFFFFFFF...)
my $seq1 = substr($one2,13); # truncated sequence without UMIs or fixed sequence
my $seq2 = substr($two2,13);
my $qual1 = substr($one4,13); # truncated quality string without barcode or fixed sequence
my $qual2 = substr($two4,13);
# warn "$one1\n";
$one1 .= ":R1:${r1_barcode}:R2:${r2_barcode}:F1:${r1_fix}:F2:${r2_fix}";
#warn "$one1\n";
#warn "$two1\n";
$two1 .= ":R1:${r1_barcode}:R2:${r2_barcode}:F1:${r1_fix}:F2:${r2_fix}";
#warn "$two1\n";
# warn "$one2\n $seq1\n$one4\n $qual1\n~~\n$two2\n $seq2\n$two4\n $qual2\n\n";sleep(1);
print ${read1_fh} "$one1\n";
print ${read1_fh} "$seq1\n";
print ${read1_fh} "+\n"; # replacing this with a + for space and format reasons
print ${read1_fh} "$qual1\n";
print ${read2_fh} "$two1\n";
print ${read2_fh} "$seq2\n";
print ${read2_fh} "+\n"; # replacing this with a + for space and format reasons
print ${read2_fh} "$qual2\n";
# sleep(1);
$r1{$r1_barcode}++;
$r2{$r2_barcode}++;
$fix1{$r1_fix}++;
$fix2{$r2_fix}++;
}
my $perc;
if ($count){
$perc = sprintf("%.2f",$filtered_count/$count * 100);
}
else{
$perc = 'N/A';
}
warn "Sequences processed in total: $count\nthereof had fixed sequence CAGT in both R1 and R2:\t $filtered_count ($perc%)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
}
sub rc{
my $string = shift;
$string = reverse($string);
$string =~ tr/GATC/CTAG/;
return $string;
}
########################################################################
my $path_to_fastqc = 'fastqc';
########################################################################
### SETTING DEFAULTS UNLESS THEY WERE SPECIFIED
unless (defined $cutoff){
unless ($nextseq){
$cutoff = 20;
}
}
my $phred_score_cutoff = $cutoff; # only relevant for report
my $adapter_name = '';
unless (defined $adapter){
if ($nextera){
$adapter = 'CTGTCTCTTATA';
$adapter_name = 'Nextera Transposase sequence; user defined';
}
elsif($small_rna){
$adapter = 'TGGAATTCTCGG';
$adapter_name = 'Illumina small RNA adapter; user defined';
}
elsif($illumina){
$adapter = 'AGATCGGAAGAGC';
$adapter_name = 'Illumina TruSeq, Sanger iPCR; user defined';
}
else{ # default
### If other -a and/or -a2 were given
if ($polyA){ # specialised PolyA trimming
($adapter,$adapter_name) = autodetect_polyA_type();
if ($validate){ # we need to select -a2 as the reverse complement to -a
if ($adapter =~ /A+/){
$adapter = extend_adapter_sequence("A",20); # defaulting to 15
$a2 = extend_adapter_sequence("T",150); # defaulting to 150 bp
}
elsif($adapter =~ /T+/){
$adapter = extend_adapter_sequence("T",20); # defaulting to 15
$a2 = extend_adapter_sequence("A",150);
}
else{
die "Something unexpected happened with the Poly-A autodetection, please check\n";
}
# warn "Paired-end Poly-A detection, set the following parameters:\n";
# warn " -a: $adapter\n-a2: $a2\n ~~~~~~~~~~ \n\n";sleep(1);
}
else{
# Single end
if ($adapter =~ /A+/){
$adapter = extend_adapter_sequence("A",20); # defaulting to 15
}
elsif($adapter =~ /T+/){
$adapter = extend_adapter_sequence("T",20); # defaulting to 15
}
else{
die "Something unexpected happened with the Poly-A autodetection, please check\n";
}
}
}
else{ # ADAPTER TRIMMING; DEFAULT
($adapter,$adapter_name) = autodetect_adapter_type();
}
}
}
else{
$adapter_name = 'user defined';
}
### For smallRNA adapters we are reducing the sequence length cutoff before a sequences gets thrown out entirely to 18bp. We are doing this because some 20-23bp long smallRNAs
### may be removed if T, TG, or TGG etc gets trimmed off the end (changed b ack up from 16 to 18bp to remove noise from alignment files, 18 11 2015)
if ($adapter eq 'TGGAATTCTCGG'){
unless (defined $length_cutoff){ # user defined length cutoff wins over auto-detection
$length_cutoff = 18;
warn "Reducing length cutoff to 18bp for small RNA-Seq reads because a cutoff of 20bp may remove some short species of small RNAs if they had been trimmed by 1,2 or 3bp\n";
}
### If the file is a smallRNA library and paired-end we set the Illumina 5' adapter as the $a2 sequence
if ($validate){
unless (defined $a2){
$a2 = 'GATCGTCGGACT';
warn "Setting the Illumina smallRNA 5' adapter as adapter 2: 'GATCGTCGGACT'\n";
}
}
}
unless (defined $length_cutoff){
$length_cutoff = 20; # non small RNA length cutoff
}
unless (defined $a2){ # optional adapter for the second read in a pair. Only works for --paired trimming
$a2 = '';
}
unless (defined $stringency){
$stringency = 1;
}
if ($phred_encoding == 64){
$cutoff += 31;
}
my $file_1;
my $file_2;
foreach my $filename (@ARGV){
trim ($filename);
}
sub trim{
my $filename = shift;
my $output_filename = (split (/\//,$filename))[-1];
my $report = $output_filename;
$report =~ s/$/_trimming_report.txt/;
if ($no_report_file) {
$report = File::Spec->devnull;
open (REPORT,'>',$report) or die "Failed to write to file '$report': $!\n";
# warn "Redirecting report output to /dev/null\n";
}
else{
open (REPORT,'>',$output_dir.$report) or die "Failed to write to file '$report': $!\n";
warn "Writing report to '$output_dir$report'\n";
}
warn "\nSUMMARISING RUN PARAMETERS\n==========================\nInput filename: $filename\n";
print REPORT "\nSUMMARISING RUN PARAMETERS\n==========================\nInput filename: $filename\n";
if ($validate){ # paired-end mode
warn "Trimming mode: paired-end\n";
print REPORT "Trimming mode: paired-end\n";
}
else{
warn "Trimming mode: single-end\n";
print REPORT "Trimming mode: single-end\n";
}
warn "Trim Galore version: $trimmer_version\n";
print REPORT "Trim Galore version: $trimmer_version\n";
warn "Cutadapt version: $cutadapt_version\n";
print REPORT "Cutadapt version: $cutadapt_version\n";
if (defined $python_version){
warn "Python version: $python_version\n";
print REPORT "Python version: $python_version\n";
}
if (defined $cores){
my $temp = $cores;
$temp =~ s/-j //;
warn "Number of cores used for trimming: $temp\n";
print REPORT "Number of cores used for trimming: $temp\n";
}
if (defined $phred_score_cutoff){
warn "Quality Phred score cutoff: $phred_score_cutoff\n";
print REPORT "Quality Phred score cutoff: $phred_score_cutoff\n";
}
warn "Quality encoding type selected: ASCII+$phred_encoding\n";
print REPORT "Quality encoding type selected: ASCII+$phred_encoding\n";
warn "Adapter sequence: '$adapter' ($adapter_name)\n";
print REPORT "Adapter sequence: '$adapter' ($adapter_name)\n";
if ($error_rate == 0.1){
warn "Maximum trimming error rate: $error_rate (default)\n";
}
else{
warn "Maximum trimming error rate: $error_rate\n";
}
if (defined $maxn){
warn "Maximum number of tolerated Ns: $maxn\n";
}
if ($nextseq){
warn "2-colour high quality G-trimming enabled, with quality cutoff: $nextseq\n";
print REPORT "2-colour high quality G-trimming enabled, with quality cutoff: $nextseq\n";
}
print REPORT "Maximum trimming error rate: $error_rate";
if ($error_rate == 0.1){
print REPORT " (default)\n";
}
else{
print REPORT "\n";
}
if ($a2){
warn "Optional adapter 2 sequence (only used for read 2 of paired-end files): '$a2'\n";
print REPORT "Optional adapter 2 sequence (only used for read 2 of paired-end files): '$a2'\n";
}
warn "Minimum required adapter overlap (stringency): $stringency bp\n";
print REPORT "Minimum required adapter overlap (stringency): $stringency bp\n";
if ($validate){
warn "Minimum required sequence length for both reads before a sequence pair gets removed: $length_cutoff bp\n";
print REPORT "Minimum required sequence length for both reads before a sequence pair gets removed: $length_cutoff bp\n";
}
else{
warn "Minimum required sequence length before a sequence gets removed: $length_cutoff bp\n";
print REPORT "Minimum required sequence length before a sequence gets removed: $length_cutoff bp\n";
}
if ($max_length){
warn "Maxiumum tolerated read length after trimming (for smallRNA trimming): $max_length bp\n";
print REPORT "Maxiumum tolerated read length after trimming (for smallRNA trimming): $max_length bp\n";
}
if ($trim_n){
warn "Removing Ns from the start and end of reads\n";
print REPORT "Removing Ns from the start and end of reads\n";
}
if ($validate){ # only for paired-end files
if ($retain){ # keeping single-end reads if only one end is long enough
if ($length_read_1 == 35){
warn "Length cut-off for read 1: $length_read_1 bp (default)\n";
print REPORT "Length cut-off for read 1: $length_read_1 bp (default)\n";
}
else{
warn "Length cut-off for read 1: $length_read_1 bp\n";
print REPORT "Length cut-off for read 1: $length_read_1 bp\n";
}
if ($length_read_2 == 35){
warn "Length cut-off for read 2: $length_read_2 bb (default)\n";
print REPORT "Length cut-off for read 2: $length_read_2 bp (default)\n";
}
else{
warn "Length cut-off for read 2: $length_read_2 bp\n";
print REPORT "Length cut-off for read 2: $length_read_2 bp\n";
}
}
}
if ($rrbs){
warn "File was specified to be an MspI-digested RRBS sample. Read 1 sequences with adapter contamination will be trimmed a further 2 bp from their 3' end, and Read 2 sequences will be trimmed by 2 bp from their 5' end to remove potential methylation-biased bases from the end-repair reaction\n";
print REPORT "File was specified to be an MspI-digested RRBS sample. Read 1 sequences with adapter contamination will be trimmed a further 2 bp from their 3' end, and Read 2 sequences will be trimmed by 2 bp from their 5' end to remove potential methylation-biased bases from the end-repair reaction\n";
}
if ($non_directional){
warn "File was specified to be a non-directional MspI-digested RRBS sample. Sequences starting with either 'CAA' or 'CGA' will have the first 2 bp trimmed off to remove potential methylation-biased bases from the end-repair reaction\n";
print REPORT "File was specified to be a non-directional MspI-digested RRBS sample. Sequences starting with either 'CAA' or 'CGA' will have the first 2 bp trimmed off to remove potential methylation-biased bases from the end-repair reaction\n";
}
if ($trim){
warn "All sequences will be trimmed by 1 bp on their 3' end to avoid problems with invalid paired-end alignments with Bowtie 1\n";
print REPORT "All sequences will be trimmed by 1 bp on their 3' end to avoid problems with invalid paired-end alignments with Bowtie 1\n";
}
if ($clip_r1){
warn "All Read 1 sequences will be trimmed by $clip_r1 bp from their 5' end to avoid poor qualities or biases\n";
print REPORT "All Read 1 sequences will be trimmed by $clip_r1 bp from their 5' end to avoid poor qualities or biases\n";
}
if ($clip_r2){
warn "All Read 2 sequences will be trimmed by $clip_r2 bp from their 5' end to avoid poor qualities or biases (e.g. M-bias for BS-Seq applications)\n";
print REPORT "All Read 2 sequences will be trimmed by $clip_r2 bp from their 5' end to avoid poor qualities or biases (e.g. M-bias for BS-Seq applications)\n";
}
if ($three_prime_clip_r1){
warn "All Read 1 sequences will be trimmed by $three_prime_clip_r1 bp from their 3' end to avoid poor qualities or biases\n";
print REPORT "All Read 1 sequences will be trimmed by $three_prime_clip_r1 bp from their 3' end to avoid poor qualities or biases\n";
}
if ($three_prime_clip_r2){
warn "All Read 2 sequences will be trimmed by $three_prime_clip_r2 bp from their 3' end to avoid poor qualities or biases\n";
print REPORT "All Read 2 sequences will be trimmed by $three_prime_clip_r2 bp from their 3' end to avoid poor qualities or biases\n";
}
if ($fastqc){
warn "Running FastQC on the data once trimming has completed\n";
print REPORT "Running FastQC on the data once trimming has completed\n";
if ($fastqc_args){
warn "Running FastQC with the following extra arguments: '$fastqc_args'\n";
print REPORT "Running FastQC with the following extra arguments: $fastqc_args\n";
}
}
if ($keep and $rrbs){
warn "Keeping quality trimmed (but not yet adapter trimmed) intermediate FastQ file\n";
print REPORT "Keeping quality trimmed (but not yet adapter trimmed) intermediate FastQ file\n";
}
if ($gzip or $filename =~ /\.gz$/){
$gzip = 1;
unless ($dont_gzip){
warn "Output file(s) will be GZIP compressed\n";
print REPORT "Output file will be GZIP compressed\n";
}
}
warn "\n";
print REPORT "\n";
# sleep (3);
my $temp;
### We need to make sure that Cutadapt still runs if users use a fairly old version of Cutadapt, as multi-core handling is only supported sincce
### Cutadapt version 1.15. Edited on 08 03 2019
if ($cutadapt_version < 1.15){
if ($cores > 1){
die "I'm sorry but your version of Cutadapt is too old to support multi-core trimming. Please update Cutadapt, and try again\n\n";
}
else{ #default single-core prcessing with old version of Cutadapt
warn "Your version of Cutadapt is fairly old (detected v$cutadapt_version), please consider updating Cutadapt!\n";
$cores = ""; # need to delete -j entirely as Cutadapt
}
}
else{
unless ($cores =~ /^-j \d+$/){ # if it was set before, don't change it again
$cores = "-j $cores";
}
}
### Proceeding differently for RRBS and other type of libraries
if ($rrbs){
### Skipping quality filtering for RRBS libraries if a quality cutoff of 0 was specified
if ($cutoff == 0){
warn "Quality cutoff selected was 0 - Skipping quality trimming altogether\n\n";
# sleep (3);
}
else{
$temp = $filename;
$temp =~ s/^.*\///; # replacing optional file path information
$temp =~ s/$/_qual_trimmed.fastq/;
open (TEMP,'>',$output_dir.$temp) or die "Can't write to '$temp': $!";
warn " >>> Now performing adaptive quality trimming with a Phred-score cutoff of: $cutoff <<<\n\n";
# sleep (1);
open (QUAL,"$path_to_cutadapt $cores -e $error_rate -q $cutoff -a X $filename |") or die "Can't open pipe to Cutadapt: $!";
my $qual_count = 0;
while (1){
my $l1 = <QUAL>;
my $seq = <QUAL>;
my $l3 = <QUAL>;
my $qual = <QUAL>;
last unless (defined $qual);
$qual_count++;
if ($qual_count%10000000 == 0){
warn "$qual_count sequences processed\n";
}
print TEMP "$l1$seq$l3$qual";
}
warn "\n >>> Quality trimming completed <<<\n$qual_count sequences processed in total\n\n";
close QUAL or die "Unable to close QUAL filehandle: $!\n";
}
}
if ($output_filename =~ /\.fastq$/){
$output_filename =~ s/\.fastq$/_trimmed.fq/;
}
elsif ($output_filename =~ /\.fastq\.gz$/){
$output_filename =~ s/\.fastq\.gz$/_trimmed.fq/;
}
elsif ($output_filename =~ /\.fq$/){
$output_filename =~ s/\.fq$/_trimmed.fq/;
}
elsif ($output_filename =~ /\.fq\.gz$/){
$output_filename =~ s/\.fq\.gz$/_trimmed.fq/;
}
else{
$output_filename =~ s/$/_trimmed.fq/;
}
if ($gzip or $filename =~ /\.gz$/){
if ($dont_gzip){
open (OUT,'>',$output_dir.$output_filename) or die "Can't open '$output_filename': $!\n"; # don't need to gzip intermediate file
}
else{
### 6 Jan 2014: had a request to also gzip intermediate files to save disk space
# if ($validate){
# open (OUT,'>',$output_dir.$output_filename) or die "Can't open '$output_filename': $!\n"; # don't need to gzip intermediate file
# }
$output_filename .= '.gz';
open (OUT,"| $compression_path -c - > ${output_dir}${output_filename}") or die "Can't write to '$output_filename': $!\n";
}
}
else{
open (OUT,'>',$output_dir.$output_filename) or die "Can't open '$output_filename': $!\n";
}
warn "Writing final adapter and quality trimmed output to $output_filename\n\n";
my $count = 0;
my $too_short = 0;
my $too_long = 0;
my $too_many_n = 0;
my $quality_trimmed = 0;
my $rrbs_trimmed = 0;
my $rrbs_trimmed_start = 0;
my $CAA = 0;
my $CGA = 0;
my $pid;
if ($rrbs and $cutoff != 0){
### optionally using 2 different adapters for read 1 and read 2
if ($validate and $a2){
### Figure out whether current file counts as read 1 or read 2 of paired-end files
if ( scalar(@filenames)%2 == 0){ # this is read 1 of a pair
warn "\n >>> Now performing adapter trimming for the adapter sequence: '$adapter' from file $temp <<< \n";
#sleep (1);
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -a $adapter $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
else{ # this is read 2 of a pair
warn "\n >>> Now performing adapter trimming for the adapter sequence: '$a2' from file $temp <<< \n";
#sleep (1);
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -a $a2 $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
}
### Using the same adapter for both read 1 and read 2
else{
warn "\n >>> Now performing adapter trimming for the adapter sequence: '$adapter' from file $temp <<< \n";
# sleep (3);
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -a $adapter $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
close WRITER or die $!; # not needed
open (QUAL,"$output_dir$temp") or die $!; # quality trimmed file
if ($filename =~ /\.gz$/){
open (IN,"gunzip -c $filename |") or die $!; # original, untrimmed file
}
else{
open (IN,$filename) or die $!; # original, untrimmed file
}
while (1){
# we can process the output from Cutadapt and the original input 1 by 1 to decide if the adapter has been removed or not
my $l1 = <TRIM>;
my $seq = <TRIM>; # adapter trimmed sequence
my $l3 = <TRIM>;
my $qual = <TRIM>;
$_ = <IN>; # irrelevant
my $original_seq = <IN>;
$_ = <IN>; # irrelevant
$_ = <IN>; # irrelevant
$_ = <QUAL>; # irrelevant
my $qual_trimmed_seq = <QUAL>;
$_ = <QUAL>; # irrelevant
my $qual_trimmed_qual = <QUAL>;
last unless (defined $qual and defined $qual_trimmed_qual); # could be empty strings
$count++;
if ($count%10000000 == 0){
warn "$count sequences processed\n";
}
chomp $seq;
chomp $qual;
chomp $qual_trimmed_seq;
chomp $original_seq;
my $quality_trimmed_seq_length = length $qual_trimmed_seq;
if (length $original_seq > length $qual_trimmed_seq){
++$quality_trimmed;
}
my $nd = 0;
### NON-DIRECTIONAL RRBS
if ($non_directional){
if (length$seq > 2){
if ($seq =~ /^CAA/){
++$CAA;
$seq = substr ($seq,2,length($seq)-2);
$qual = substr ($qual,2,length($qual)-2);
++$rrbs_trimmed_start;
$nd = 1;
}
elsif ($seq =~ /^CGA/){
$seq = substr ($seq,2,length($seq)-2);
$qual = substr ($qual,2,length($qual)-2);
++$CGA;
++$rrbs_trimmed_start;
$nd = 1;
}
}
}
### directional read
unless ($nd == 1){
# only trimming Read 1 of a pair for a further 2bp from their 3' end
if ($validate){ # paired end
if ( scalar(@filenames)%2 == 0){ # this is read 1 of a pair
if (length $seq >= 2 and length$seq < $quality_trimmed_seq_length){
$seq = substr ($seq,0,length($seq)-2);
$qual = substr ($qual,0,length($qual)-2);
++$rrbs_trimmed;
}
}
else{
# this is read 2 of a pair. We do not trim further from the 3' end but rather trim R2 from the 5' end later
}
}
else{ # single-end reads will be trimmed from their 3' end
if (length $seq >= 2 and length$seq < $quality_trimmed_seq_length){
$seq = substr ($seq,0,length($seq)-2);
$qual = substr ($qual,0,length($qual)-2);
++$rrbs_trimmed;
}
}
}
### Shortening all sequences by 1 bp on the 3' end
# 28 02 2019: This was really only required for Bowtie 1 paired-end alignments, maybe we should drop this option in soon
if ($trim){
$seq = substr($seq,0,length($seq)-1);
$qual = substr($qual,0,length($qual)-1);
}
### PRINTING (POTENTIALLY TRIMMED) SEQUENCE
if ($validate){ # printing the sequence without performing a length check (this is performed for the read pair separately later)
print OUT "$l1$seq\n$l3$qual\n";
}
else{ # single end
if ($clip_r1){
if (length $seq > $clip_r1){ # sequences that are already too short won't be clipped again
$seq = substr($seq,$clip_r1); # starting after the sequences to be trimmed until the end of the sequence
$qual = substr($qual,$clip_r1);
}
}
if ($three_prime_clip_r1){
if (length $seq > $three_prime_clip_r1){ # sequences that are already too short won't be clipped again
# warn "seq/qual before/after trimming:\n$seq\n$qual\n";
$seq = substr($seq,0,(length($seq) - $three_prime_clip_r1)); # starting after the sequences to be trimmed until the end of the sequence
$qual = substr($qual,0,(length($qual) - $three_prime_clip_r1 ));
# warn "$seq\n$qual\n";
}
}
if (defined $maxn){
my $n_count = Ncounter($seq);
# warn "Checking for Ns: Found $n_count\n";
if ($n_count > $maxn){
++$too_many_n;
next;
}
}
if (length $seq < $length_cutoff){
++$too_short;
next;
}
elsif($max_length and length$seq > $max_length){
++$too_long;
next; # sequence is too long
}
else{
print OUT "$l1$seq\n$l3$qual\n";
}
}
}
print REPORT "\n";
while (<ERROR>){
warn $_;
print REPORT $_;
}
close IN or die "Unable to close IN filehandle: $!";
close QUAL or die "Unable to close QUAL filehandle: $!";
close TRIM or die "Unable to close TRIM filehandle: $!";
close OUT or die "Unable to close OUT filehandle: $!";
}
############################################################################################################
elsif($polyA){ # PolyA trimming
warn "POLY-A TRIMMING MODE; EXPERIMENTAL!!\n";
my $isR2 = 0;
# For the moment we set the temp file name back to $filename
$temp = $filename;
### optionally using 2 different adapters for read 1 and read 2
if ($validate and $a2){
### Figure out whether current file counts as read 1 or read 2 of paired-end files
if ( scalar(@filenames)%2 == 0){ # this is read 1 of a pair
warn "\n >>> Now performing Poly-A trimming for the adapter sequence: '$adapter' from file $temp <<< \n";
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -a $adapter $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
else{ # this is read 2 of a pair
$isR2 = 1;
warn "\n >>> Now performing Poly-A trimming for the adapter sequence: '$a2' from file $temp <<< \n";
# For Read 2 we need to trim the PolyT (or PolyA) from the 5' end instead! Hence -g $a2 and not -a!
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -g $a2 $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
}
### Using the same adapter for both read 1 and read 2 - Single end will use the same adapters are Read 1s for paired-end files
else{
warn "\n >>> Now performing single-end Poly-A trimming for with the sequence: '$adapter' from file $temp <<< \n";
# sleep (3);
$pid = open3 (\*WRITER, \*TRIM, \*ERROR,"$path_to_cutadapt $cores -e $error_rate -O $stringency -a $adapter $output_dir$temp") or die "Failed to launch Cutadapt: $!\n";
}
close WRITER or die $!; # not needed
# This is the Illumina adapter trimmed file
if ($temp =~ /\.gz$/){
open (QUAL,"gunzip -c $output_dir$temp |") or die $!; # quality trimmed file
}
else{
open (QUAL,"$output_dir$temp") or die $!; # quality trimmed file
}
while(1){
# we can process the output from Cutadapt and the original input 1 by 1 to decide if the adapter has been removed or not
my $l1 = <TRIM>;
my $seq = <TRIM>; # adapter trimmed sequence
my $l3 = <TRIM>;
my $qual = <TRIM>;
# $_ = <IN>; # irrelevant
# my $original_seq = <IN>;
# $_ = <IN>; # irrelevant
# $_ = <IN>; # irrelevant
$_ = <QUAL>; # irrelevant
my $qual_trimmed_seq = <QUAL>;
$_ = <QUAL>; # irrelevant
my $qual_trimmed_qual = <QUAL>;
last unless (defined $qual and defined $qual_trimmed_qual); # could be empty strings
$count++;
if ($count%10000000 == 0){
warn "$count sequences processed\n";
}
chomp $l1;
chomp $seq;
chomp $qual;
chomp $qual_trimmed_seq;
# chomp $original_seq;
my $quality_trimmed_seq_length = length $qual_trimmed_seq;
# if (length $original_seq > length $qual_trimmed_seq){
# ++$quality_trimmed;
# }
my $diff = length($qual_trimmed_seq) - length($seq);
### CHANGING THE readID to remove white spaces and adding PolyA trimming information
$l1 =~ s/\s+/_/g; # removing white spaces from readID
if ($diff > 0){ # only adding the PolyA removal tag if some A's were really removed, so we can filter these out later if desired
$l1 .= "_PolyA:$diff";
}
### Shortening all sequences by 1 bp on the 3' end - This is probably no longer needed since we have stopped using Bowtie (1)
if ($trim){
$seq = substr($seq,0,length($seq)-1);
$qual = substr($qual,0,length($qual)-1);
}
### PRINTING (POTENTIALLY TRIMMED) SEQUENCE
if ($validate){ # printing the sequence without performing a length check (this is performed for the read pair separately later)
if ($isR2){
#print "$l1\n$qual_trimmed_seq\n$seq\n$l3$qual_trimmed_qual\n$qual\n~~~~~\n";
# print "length original: ",length($qual_trimmed_seq) , "\nlength trimmed: ", length($seq) , "\nDifference: $diff bp\n ~~~\n\n"; sleep(1);
}
print OUT "$l1\n$seq\n$l3$qual\n";
}
else{ # single end
if ($clip_r1){
if (length $seq > $clip_r1){ # sequences that are already too short won't be clipped again
$seq = substr($seq,$clip_r1); # starting after the sequences to be trimmed until the end of the sequence
$qual = substr($qual,$clip_r1);
}
}
if ($three_prime_clip_r1){
if (length $seq > $three_prime_clip_r1){ # sequences that are already too short won't be clipped again
# warn "seq/qual before/after trimming:\n$seq\n$qual\n";
$seq = substr($seq,0,(length($seq) - $three_prime_clip_r1)); # starting after the sequences to be trimmed until the end of the sequence
$qual = substr($qual,0,(length($qual) - $three_prime_clip_r1 ));
# warn "$seq\n$qual\n";
}
}
if (length $seq < $length_cutoff){
++$too_short;
next;
}
elsif($max_length and length$seq > $max_length){
++$too_long;
next; # sequence is too long
}