-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcondetri.pl
664 lines (608 loc) · 16.3 KB
/
condetri.pl
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
#!/usr/bin/perl
# condetri_v2.3.pl
# September 2010, mod Oct 2011, Mar, Jun & Oct 2012 & Mar 2015
# Author: Linnéa Smeds ([email protected])
use strict;
use warnings;
use Getopt::Long;
my $usage = "# condetri_v2.3.pl
# September 2010
# Version 2.3, October 2012
# Author: Linnéa Smeds (linnea.smeds\@ebc.uu.se)
# This version uses \"zcat\" instead of Zlib which means no library installation
# is required. (Works in linux, windows users should continue using v2.2).
# Also added two parameters \"-cutlast\" and \"-notrim\" so that ConDeTri can be
# used for trimming a fixed number of base pairs and stop before the quality based
# trimming starts.
# ---------------------------------------------------------------------------------
# Description: Trim reads from the 3'-end and extract reads (or read pairs) of
# good quality. If the reads are paired, the filtering is done pairwise, and
# if one read in a pair has low quality, the remaning read is saved as single end.
# Usage: perl condetri.pl -fastq1=file1 [-fastq2=file2 -prefix=s -cutfirst=i
# -rmN -hq=i -lq=i -frac=i -lfrac=i -minlen=i -mh=i -ml=i -sc=i -pb=s]
-fastq1=file \t Fastq(.gz) file. If a second file is given, the files are trimmed
-fastq2=file \t as a pair. The reads must have the same order in both files.
-prefix=string \t Prefix for the output file(s). The filtered fastq file(s) will
\t\t be named prefix_trim1.fastq (and prefix_trim2.fastq if present). For pairs,
\t\t a third file will be given with unpaired reads (reads from pairs where one
\t\t low quality read has been removed).
-cutfirst=i \t Remove i first bases from the 5'end before any trimming [0].
-cutlast=i \t Remove i bases from the 3'end before any trimming [0].
-rmN \t\t Remove non-ATCG bases from 5'end before any trimming [no].
-notrim \t Stop script after removing 5' or 3' bases (skips the actual
\t\t trimming and filtering step) [no].
-hq=i \t\t Hiqh quality threshold [25].
-lq=i \t\t Low quality threshold [10].
-frac=[0,1]\t Fraction of read that must exceed hq [0.8].
-lfrac=[0,1]\t Maximum fraction of bases with qual<lq [0].
-minlen=i \t Min allowed read length [50].
-mh=i \t\t When this no of sequential hq bases is reached, the trimming stops [5].
-ml=i \t\t Max no of lq bases allowed after a stretch of hq bases from 3'-end [1].
-sc=i\t\t Scoring table, Score=ASCII-sc, 64 for Solexa and Illumina 1.3+ and
\t\t Illumina 1.5+, 33 for Sanger and newer Illumina data (from 1.8+). [64].
-pb=fa|fq\t Print the removed low quality reads to a fasta file or a fastq
\t\t file (for investigation/evaluation purposes) [no].
-q \t\t Print Solexa/Illumina scoring table (64 offset).
-q33 \t\t Print Sanger/new Illumina scoring table (33 offset).
-h \t\t Print this help message.\n";
# Starting time
my $time = time;
# Input parameters
my ($read1,$read2,$prefix,$cutfirst,$cutlast,$removeN,$notrim,$HQlim,$lowlim,$minfrac,$lfrac,$minReadLen,
$maxNoHQ,$maxNoLQ,$scoring,$printBad,$tbl,$tbl33,$help);
GetOptions(
"fastq1=s" => \$read1,
"fastq2=s" => \$read2,
"prefix=s" => \$prefix,
"cutfirst=i" => \$cutfirst,
"cutlast=i" => \$cutlast,
"rmN" => \$removeN,
"notrim" => \$notrim,
"hq=i" => \$HQlim,
"lq=i" => \$lowlim,
"frac=s" => \$minfrac,
"lfrac=s" => \$lfrac,
"minlen=i" => \$minReadLen,
"mh=i" => \$maxNoHQ,
"ml=i" => \$maxNoLQ,
"sc=i" => \$scoring,
"pb=s" => \$printBad,
"q" => \$tbl,
"q33" => \$tbl33,
"h" => \$help);
#--------------------------------------------------------------------------------
#Checking input, set default if not given
if($tbl) {
&table64();
exit;
}
if($tbl33) {
&table33();
exit;
}
unless($read1) {
die $usage . "\n";
}
if($help) {
die $usage . "\n";
}
unless($prefix) {
$prefix = $read1;
$prefix =~ s/\.\w+//;
}
unless($cutfirst) {
$cutfirst=0;
}
unless($cutlast) {
$cutlast=0;
}
unless($HQlim) {
$HQlim=25;
}
unless($lowlim) {
$lowlim=10;
}
if($minfrac) {
if($minfrac<0 || $minfrac>1) {
die "Error: frac must be between 0 and 1.\n";
}
}
else{
$minfrac=0.8;
}
if($lfrac) {
if($lfrac<0 || $lfrac>1) {
die "Error: lfrac must be between 0 and 1.\n";
}
}
else{
$lfrac=0;
}
unless($maxNoHQ) {
$maxNoHQ=5;
}
unless($maxNoLQ) {
$maxNoLQ=1;
}
unless($minReadLen) {
$minReadLen=50;
}
unless($scoring) {
$scoring=64;
}
if($printBad) {
unless($printBad eq "fa" || $printBad eq "fq") {
die "Error: pb must be either \"fa\" or \"fq\".\n";
}
}
else {
$printBad="";
}
unless(-e $read1) {
die "Error: File $read1 doesn't exist!\n";
}
print "\ncondetri_v2.3.pl started " . localtime() . "\n";
print "------------------------------------------------------------------\n";
print "Settings: ";
if($cutfirst>0) {
print "Cutfirst=$cutfirst ";
}
if($cutlast>0) {
print "Cutlast=$cutlast ";
}
print "HQ=$HQlim LQ=$lowlim Frac=$minfrac Lfrac=$lfrac MH=$maxNoHQ ML=$maxNoLQ Minlen=$minReadLen Scoring=$scoring\n";
if($removeN) {
print "Remove non-ATCG characters from 5'-end before trimming\n";
}
if($printBad ne "") {
print "Print removed reads to a $printBad file.\n";
}
my ($totNoReads, $pairReads, $unpairedReads, $badReads) = (0,0,0,0);
my ($totNoBases, $pairBases, $unpairedBases, $firstbases, $lastbases) = (0,0,0,0,0);
#--------------------------------------------------------------------------------
# Trimming paired files
if($read2) {
unless(-e $read2) {
die "Error: File $read2 doesn't exist!\n";
}
if ($read1 =~ /\.gz$/) {
open(IN1, "zcat $read1 |");
}
else {
open(IN1, $read1);
}
if ($read2 =~ /\.gz$/) {
open(IN2, "zcat $read2 |");
}
else {
open(IN2, $read2);
}
my $out1 = $prefix . "_trim1.fastq";
my $out2 = $prefix . "_trim2.fastq";
my $out3 = $prefix . "_trim_unpaired.fastq";
my $out4 = $prefix . "_badreads.".$printBad;
open(OUT1, ">$out1");
open(OUT2, ">$out2");
unless($notrim) {
open(OUT3, ">$out3");
}
if($printBad ne "") {
open(OUT4, ">$out4");
}
my($head1,$seq1,$plus1,$qual1,$head2,$seq2,$plus2,$qual2);
print "Processing...\n";
while(my $line = <IN1>) {
$head1 = $line;
chomp($seq1 = <IN1>);
$plus1 = <IN1>;
chomp($qual1 = <IN1>);
$head2 = <IN2>;
chomp($seq2 = <IN2>);
$plus2 = <IN2>;
chomp($qual2 = <IN2>);
my($newseq1, $newscore1, $newseq2, $newscore2)=($seq1,$qual1,$seq2,$qual2);
# Cut from 5'-end
if($cutfirst>0) {
$newseq1 = substr($seq1, $cutfirst, length($seq1)-$cutfirst);
$newscore1 = substr($qual1, $cutfirst, length($qual1)-$cutfirst);
$newseq2 = substr($seq2, $cutfirst, length($seq2)-$cutfirst);
$newscore2 = substr($qual2, $cutfirst, length($qual2)-$cutfirst);
}
# Remove non-ATCG characters from 5'end
if($removeN) {
my ($tmp1, $tmp2);
($newseq1, $newscore1, $tmp1) = &removeNs($newseq1, $newscore1);
($newseq2, $newscore2, $tmp2) = &removeNs($newseq2, $newscore2);
$firstbases=$firstbases+$tmp1+$tmp2;
}
#Cut from 3'-end
if($cutlast>0) {
$newseq1 = substr($newseq1, 0, length($newseq1)-$cutlast);
$newscore1 = substr($newscore1, 0, length($newscore1)-$cutlast);
$newseq2 = substr($newseq2, 0, length($newseq2)-$cutlast);
$newscore2 = substr($newscore2, 0, length($newscore2)-$cutlast);
}
# If -notrim flag is used, the reads are not trimmed, just printed
if($notrim) {
print OUT1 $head1 . $newseq1 ."\n" . $plus1 . $newscore1 . "\n";
print OUT2 $head2 . $newseq2 ."\n" . $plus2 . $newscore2 . "\n";
$pairReads+=2;
$pairBases+=length($newseq1)+length($newseq2);
}
else {
# Trim both reads
($newseq1, $newscore1) = &trimEnd($newseq1,$newscore1, $HQlim, $lowlim, $maxNoHQ, $maxNoLQ, $minReadLen, $scoring);
($newseq2, $newscore2) = &trimEnd($newseq2,$newscore2, $HQlim, $lowlim, $maxNoHQ, $maxNoLQ, $minReadLen, $scoring);
# Check if reads are ok, print good reads
if(readOK($newscore1, $HQlim, $lowlim, $minfrac, $lfrac, $scoring, $minReadLen)) {
if(&readOK($newscore2, $HQlim, $lowlim, $minfrac, $lfrac, $scoring, $minReadLen)) {
print OUT1 $head1 . $newseq1 ."\n" . $plus1 . $newscore1 . "\n";
print OUT2 $head2 . $newseq2 ."\n" . $plus2 . $newscore2 . "\n";
$pairReads+=2;
$pairBases+=length($newseq1)+length($newseq2);
}
else {
print OUT3 $head1 . $newseq1 ."\n" . $plus1 . $newscore1 . "\n";
$unpairedReads++;
$unpairedBases+=length($newseq1);
if($printBad eq "fa") {
print OUT4 ">".$head2.$seq2."\n";
$badReads++;
}
elsif($printBad eq "fq") {
print OUT4 $head2.$seq2."\n".$plus2.$qual2."\n";
$badReads++;
}
}
}
else {
if(&readOK($newscore2, $HQlim, $lowlim, $minfrac, $lfrac, $scoring, $minReadLen)) {
print OUT3 $head2 . $newseq2 ."\n" . $plus2 . $newscore2 . "\n";
$unpairedReads++;
$unpairedBases+=length($newseq2);
if($printBad eq "fa") {
print OUT4 ">".$head1.$seq1."\n";
$badReads++;
}
elsif($printBad eq "fq") {
print OUT4 $head1.$seq1."\n".$plus1.$qual1."\n";
$badReads++;
}
}
else {
if($printBad eq "fa" ) {
print OUT4 ">".$head1.$seq1."\n>".$head2.$seq2."\n";
$badReads+=2;
}
elsif($printBad eq "fq") {
print OUT4 $head1.$seq1."\n".$plus1.$qual1."\n".
$head2.$seq2."\n".$plus2.$qual2."\n";
$badReads+=2;
}
}
}
}
$totNoBases+=length($seq1)+length($seq2);
$totNoReads+=2;
if ($totNoReads%100000==0) {
print "$totNoReads reads processed\r";
}
}
}
# Trimming single end files
else {
my $out1 = $prefix . "_trim.fastq";
my $out4 = $prefix . "_badreads.".$printBad;
if ($read1 =~ /\.gz$/) {
tie *IN1,'IO::Zlib',$read1,"rb";
}
else {
open(IN1, $read1);
}
open(OUT1, ">$out1");
if($printBad ne "") {
open(OUT4, ">$out4");
}
print "Processing...\n";
my($head1,$seq1,$plus1,$qual1);
while(my $line = <IN1>) {
$head1 = $line;
chomp($seq1 = <IN1>);
$plus1 = <IN1>;
chomp($qual1 = <IN1>);
my($newseq1, $newscore1) = ($seq1,$qual1);
# Cut from 5'-end
if($cutfirst>0) {
$newseq1 = substr($seq1, $cutfirst, length($seq1)-$cutfirst);
$newscore1 = substr($qual1, $cutfirst, length($qual1)-$cutfirst);
}
# Remove non-ATCG characters from 5'end
if($removeN) {
my $tmp1;
($newseq1, $newscore1, $tmp1) = &removeNs($newseq1, $newscore1);
$firstbases+=$tmp1;
}
#Cut from 3'-end
if($cutlast>0) {
$newseq1 = substr($newseq1, 0, length($newseq1)-$cutlast);
$newscore1 = substr($newscore1, 0, length($newscore1)-$cutlast);
}
# If -notrim flag is used, the reads are not trimmed, just printed
if($notrim) {
print OUT1 $head1 . $newseq1 ."\n" . $plus1 . $newscore1 . "\n";
$unpairedReads++;
$unpairedBases+=length($newseq1);
}
else {
# Trim both reads
($newseq1, $newscore1) = &trimEnd($newseq1, $newscore1, $HQlim, $lowlim, $maxNoHQ, $maxNoLQ, $minReadLen, $scoring);
if(readOK($newscore1, $HQlim, $lowlim, $minfrac, $lfrac, $scoring, $minReadLen)) {
print OUT1 $head1 . $newseq1 ."\n" . $plus1 . $newscore1 . "\n";
$unpairedReads++;
$unpairedBases+=length($newseq1);
}
else {
if($printBad eq "fa") {
print OUT4 ">".$head1.$seq1."\n";
$badReads++;
}
elsif($printBad eq "fq") {
print OUT4 $head1.$seq1."\n".$plus1.$qual1."\n";
$badReads++;
}
}
}
$totNoBases+=length($seq1);
$totNoReads+=1;
if ($totNoReads%100000==0) {
print "$totNoReads reads processed\r";
}
}
}
$firstbases+=$cutfirst*$totNoReads;
$lastbases+=$cutlast*$totNoReads;
my $totCutBases = $firstbases+$lastbases;
#--------------------------------------------------------------------------------
# Print statistics to table
open(STATS, ">$prefix".".stats");
print STATS $prefix."\t".$totNoReads."\t".$totNoBases."\t".$pairReads."\t".$pairBases.
"\t".$unpairedReads."\t".$unpairedBases."\n";
print "\nDone!\n";
print "------------------------------------------------------------------\n";
print "$totNoReads reads with $totNoBases bases in input files\n";
if($cutfirst>0) {
print "$cutfirst bases was removed from the 5'-end\n";
}
if($cutlast>0) {
print "$cutlast bases was removed from the 3'-end\n";
}
if($totCutBases>0) {
print "In total, $totCutBases was removed before quality trimming\n";
}
if($notrim) {
print "Use of flag \"notrim\" means no further quality trimming was done\n";
}
if($read2) {
my $percent = 100*($pairReads/$totNoReads);
$percent = sprintf "%.2f", $percent;
print "$pairReads ($percent%) reads with $pairBases bases saved in pair files\n";
unless($notrim) {
$percent = 100*($unpairedReads/$totNoReads);
$percent = sprintf "%.2f", $percent;
print "$unpairedReads ($percent%) reads with $unpairedBases bases saved in unpaired file\n";
print " due to low quality of the other read in the pair\n";
}
}
else {
my $percent = 100*($unpairedReads/$totNoReads);
$percent = sprintf "%.2f", $percent;
print "$unpairedReads ($percent%) reads with $unpairedBases bases saved\n";
}
if($printBad ne "") {
print "$badReads removed reads was printed to a $printBad file\n";
}
print "------------------------------------------------------------------\n";
$time = time-$time;
if($time<60) {
print "Total time elapsed: $time sec.\n";
}
else {
$time = int($time/60 + 0.5);
print "Total time elapsed: $time min.\n";
}
#--------------------------------------------------------------------------------
# Subroutines
sub removeNs {
my $seq = shift;
my $qual = shift;
# print "seq är $seq\n";
my @s = split("", $seq);
my @t = split("", $qual);
my $cnt = 0;
while(scalar(@s)>0 && $s[0] !~ m/[ATCG]/i) {
shift(@s);
shift(@t);
$cnt++;
}
$seq = join("", @s);
$qual = join("", @t);
return ($seq, $qual, $cnt);
}
sub trimEnd {
my $seq = shift;
my $qual = shift;
my $HQ = shift;
my $LQ = shift;
my $maxHQ = shift;
my $maxLQ = shift;
my $len = shift;
my $sc = shift;
if($seq ne "") {
my $LQ_flag = 0;
my $HQ_warn = 0;
my $LQinHQ_flag = "no";
my ($qual_end, $seq_end) = ("","");
my @t = split("", $qual);
my @s = split("", $seq);
while(scalar(@t)>$len && $HQ_warn<=$maxHQ) {
if (ord($t[scalar(@t)-1])-$sc < $HQ) {
if ($HQ_warn > 0 && $LQ_flag <= $maxLQ && ord($t[scalar(@t)-1])-$sc > $LQ) {
$qual_end = pop(@t).$qual_end;
$seq_end = pop(@s).$seq_end;
$LQinHQ_flag = "yes";
$LQ_flag++;
}
else {
pop(@t);
pop(@s);
($qual_end, $seq_end) = ("","");
$HQ_warn = 0;
}
}
else {
$qual_end = pop(@t).$qual_end;
$seq_end = pop(@s).$seq_end;
if($LQinHQ_flag eq "yes") {
$HQ_warn = 1;
$LQinHQ_flag = "no";
}
else {
$HQ_warn++;
}
}
}
$seq = join("", @s);
$qual = join("", @t);
$seq .= $seq_end;
$qual .= $qual_end;
}
return ($seq, $qual);
}
sub readOK {
my $qual = shift;
my $HQ = shift;
my $LQ = shift;
my $frac = shift;
my $lfrac = shift;
my $sc = shift;
my $minlen = shift;
if(length($qual)<$minlen) {
return 0;
}
my @t = split("", $qual);
my $score_cnt=0;
my $bad_cnt=0;
for (my $i=0; $i<scalar(@t)-1; $i++) {
if(ord($t[$i])-$sc >= $HQ) {
$score_cnt++;
}
if(ord($t[$i])-$sc < $lowlim) {
if($lfrac==0) {
return 0;
}
$bad_cnt++;
}
}
if($score_cnt/scalar(@t) >= $frac && $bad_cnt/scalar(@t)<=$lfrac) {
return 1;
}
else {
return 0;
}
}
sub table64 {
print "Char ASCII Char-64 P(error)
; 59 -5 0.7597
< 60 -4 0.7153
= 61 -3 0.6661
> 62 -2 0.6131
? 63 -1 0.5573
@ 64 0 0.5000
A 65 1 0.4427
B 66 2 0.3869
C 67 3 0.3339
D 68 4 0.2847
E 69 5 0.2403
F 70 6 0.2008
G 71 7 0.1663
H 72 8 0.1368
I 73 9 0.1118
J 74 10 0.0909
K 75 11 0.0736
L 76 12 0.0594
M 77 13 0.0477
N 78 14 0.0383
O 79 15 0.0307
P 80 16 0.0245
Q 81 17 0.0196
R 82 18 0.0156
S 83 19 0.0124
T 84 20 0.0099
U 85 21 0.0079
V 86 22 0.0063
W 87 23 0.0050
X 88 24 0.0040
Y 89 25 0.0032
Z 90 26 0.0025
[ 91 27 0.0020
\\ 92 28 0.0016
] 93 29 0.0013
^ 94 30 0.0010
_ 95 31 0.0008
` 96 32 0.0006
a 97 33 0.0005
b 98 34 0.0004
c 99 35 0.0003
d 100 36 0.0003
e 101 37 0.0002
f 102 38 0.0002
g 103 39 0.0001
h 104 40 0.0001
";
}
sub table33 {
print "Char ASCII Char-33 P(error)
! 33 0 0.5000
\" 34 1 0.4427
# 35 2 0.3869
\$ 36 3 0.3339
% 37 4 0.2847
& 38 5 0.2403
' 39 6 0.2008
( 40 7 0.1663
) 41 8 0.1368
* 42 9 0.1118
+ 43 10 0.0909
, 44 11 0.0736
- 45 12 0.0594
. 46 13 0.0477
/ 47 14 0.0383
0 48 15 0.0307
1 49 16 0.0245
2 50 17 0.0196
3 51 18 0.0156
4 52 19 0.0124
5 53 20 0.0099
6 54 21 0.0079
7 55 22 0.0063
8 56 23 0.0050
9 57 24 0.0040
: 58 25 0.0032
; 59 26 0.0025
< 60 27 0.0020
= 61 28 0.0016
> 62 29 0.0013
? 63 30 0.0010
@ 64 31 0.0008
A 65 32 0.0006
B 66 33 0.0005
C 67 34 0.0004
D 68 35 0.0003
E 69 36 0.0003
F 70 37 0.0002
G 71 38 0.0002
H 72 39 0.0001
I 73 40 0.0001
J 74 41 0.0001
";
}