Source

JGI_iTagger / lib / iTagger / FastqDb / Fastq.pm

Full commit
  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
=pod

=head1 NAME

iTagger::Fastq - Simple object for Fastq sequence

=head1 SYNOPSIS

    my $rec=new Fastq( $hdr, $id, $base, $barcode, $pair, $seq, $qual, $qc_params, $barcodes, $barcode_variants);
    print $rec->output;

=head1 DESCRIPTION

Object for a single read sequence, with methods for basic manipulation and quality control.

=head1 METHODS

=over 5

=cut

package iTagger::FastqDb::Fastq;

use warnings;
use strict;
use constant {
	CHARACTERS_PER_LINE    => 80,    # for formatting Fasta/Qual output only
	DEFAULT_MINLEN         => 20,
	DEFAULT_MEANQ          => 20,
	DEFAULT_WINSIZE        => 5,
	DEFAULT_LOW_COMPLEXITY => 0.8,
	DEFAULT_MAXN           => 3};
use iTagger::Stats qw(oddMedian round);

our $VERSION = 1.0;

=item new $hdr $seq $qual $qc_params $barcodes $variants

Initialize new sequence object. If the quality scores use Illumina scaling, the $qual_to_sanger flag *must* be set as the object
assumes and requires sanger-scaling.  Quality encoding method is determined by FastqDb class instead.

=cut

sub new
{
	my ($class, $hdr, $seq, $qual, $qc_params, $barcodes, $barcode_variants) = @_;
	die("Missing hdr\n")           unless defined($hdr);
	die("Missing seq for $hdr\n")  unless defined($seq);
	die("Missing qual for $hdr\n") unless defined($qual);
	if (length($seq) != length($qual))
	{
		warn("Seq and qual don't match for: $hdr\n");
		return undef;
	}
	# INIT
	my $this = {
		seq      => $seq,     # complete sequence without newlines
		qual     => $qual,    # complete quality string without newlines
		filtered => undef,    # reason why filtered
        qc       => $qc_params
	};
	bless $this, $class;
	$this->header($hdr);      # populates id, base, pair, barcode values
	$this->convert_qual_to_sanger if $this->{qc}->{qual_to_sanger};
	$this->trim_roche_mid($this->{qc}->{roche_mid_len}) if defined($this->{qc}->{roche_mid_len});
	$this->check_barcode($barcodes, $barcode_variants) if defined($barcodes);
	$this->qc;
	return $this;
}

=item convert_qual_to_sanger

Convert the quality string from Illumina to Sanger scaling.

=cut

sub convert_qual_to_sanger
{
	my $this = shift;
	return $this->{qual} = join('', map { chr(ord($_) - 31) } split(//, $this->{qual}));
}

=item trim_roche_mid

Trim the first x bases which are the 454 MID.

=cut

# TRIM THE ROCHE MOLECULAR ID FROM THE 5' END OF THE SEQUENCE
sub trim_roche_mid
{
	my ($this, $len) = @_;
	return unless defined($len);
	die("Invalid MID length, $len\n") unless $len > 0;
	if (length($this->{seq}) <= $len)
	{
		$this->{seq} = $this->{qual} = undef;
		return;
	}
	$this->{barcode} = uc(substr($this->{seq}, 0, $len));
	$this->{seq}  = substr($this->{seq},  $len);
	$this->{qual} = substr($this->{qual}, $len);
}

=item check_barcode

Check if barcode is valid, perform 1-base error correction, or filter read.

=cut

sub check_barcode
{
	my ($this, $barcodes, $barcode_variants) = @_;
	return unless defined($barcodes) and defined($barcode_variants);
	my $barcode = $this->barcode;
	return unless $barcode;
	return if exists($barcodes->{$barcode});
	if (exists($barcode_variants->{$barcode}))
	{
		$this->barcode($barcode_variants->{$barcode});
	} else
	{
		$this->del('invalid_barcode');
	}
}

=item header ($new_hdr)

Parse a header and populate object's ID-related variables.  Doesn't return anything.

=cut

sub header
{
	my ($this, $hdr) = @_;
	unless ( defined($hdr) and $hdr )
    {
        return '@'.$this->id;
    }

	$hdr = '@' . $hdr unless $hdr =~ /^@/;
	$this->{base}    = undef;    # base ID only (e.g. "A"); always defined
	$this->{pair}    = undef;    # pair ID only (e.g. "1"); only defined if paired
	$this->{barcode} = undef;    # barcode sequence (always upper-case); only defined it barcoded
    $this->{control} = 0;

	if ($hdr =~ /^@(\w+_\d+_\d+_\w+_\w+_\w+\/\d+)\/\w+$/)
	{
        # PacBio as of feb 2014
		$this->{base}    = $1;
	} elsif ($hdr =~ /^@(\S+) (\d?):([YN]):(\d+):([aAtTcCgGnN]*)/)
	{
        # current Illumina format (Casava 1.8.2)
		$this->{base}    = $1;
		$this->{pair}    = $2 if $2;
        $this->{filtered} = $3 eq 'Y' ? "casava" : undef;
        $this->{control} = $4;
		$this->{barcode} = uc($5);
	} elsif ($hdr =~ /^@(\S+)#([aAtTcCgGnN]+)\/([12])/)
	{
        # old Illumina format, JGI variant
		$this->{base}    = $1;
		$this->{barcode} = uc($2);
		$this->{pair}    = $3;
	} elsif ($hdr =~ /^@(\S+)\/([12])#([aAtTcCgGnN]+)/)
	{
        # old Illumina format
		$this->{base}    = $1;
		$this->{pair}    = $2;
		$this->{barcode} = uc($3);
	} elsif ($hdr =~ /^@(\S+)\/([12])/)
	{
        # old Illumina format
		$this->{base} = $1;
		$this->{pair} = $2;
	} elsif ($hdr =~ /^@(\S+)#([aAtTcCgGnN]+)/)
	{
        # old Illumina format
		$this->{base}    = $1;
		$this->{barcode} = uc($2);
	} elsif ($hdr =~ /^@(\S+)/)
	{
        # old Illumina format
		$this->{base} = $1;
	} elsif ($hdr =~ /^@(\S+) length=\d+ xy=\d+_\d+ region=\d run=\S/)
	{
		# Roche/454
		$this->{base} = $1;
	} elsif ($hdr =~ /^@(\S+)/)
	{
		# other/unknown (pairing, barcodes not parsable)
		$this->{base} = $1;
	} else
	{
		die("Unable to parse sequence header: $hdr\n");
	}
}

=item id

Returns the object's ID, which is the sequence's unique identifier without comments which may be present in the header.
It cannot be changed directly; set header, base, barcode, or pair instead.

=cut

sub id
{
	my $this = shift;
    my $base = $this->{base};
    my $pair = defined($this->{pair}) ? $this->{pair} : '';
    my $filtered = defined($this->{filtered}) ? 'Y' : 'N';
    my $control = defined($this->{control}) ? $this->{control} : 0;
    my $barcode = defined($this->{barcode}) ? $this->{barcode} : '';
    return "$base $pair:$filtered:$control:$barcode";
}

=item base

Returns the base of the ID; both paired reads will have the same base.
Optionally set base of the ID.

=cut

sub base
{
    my ($this, $base) = @_;
    $this->{base} = $base if $base;
    return $this->{base};
}

=item barcode

If the read contains an Illumina molecular barcode ID, it will be returned; otherwise returns undef.
Supplying an optional argument will set the barcode; passing an empty string will clear the barcode.

=cut

sub barcode
{
	my ($this, $barcode) = @_;
	if (defined($barcode))
	{
		$this->{barcode} = $barcode ? $barcode : undef;
	}
	return $this->{barcode};
}

=item pair

Returns the read's ord in the pair; undef otherwise.

=cut

sub pair
{
    my ($this, $pair) = @_;
    if (defined($pair) and ($pair eq '' or $pair == 1 or $pair == 2) )
    {
        $this->{pair} = $pair;
    }
    return $this->{pair};
}

=item unpair

Method to clear pairing of a read (e.g. singleton).

=cut

sub unpair { shift->{pair} = undef }

=item seq

Returns the read's complete sequence, without newlines.

=cut

sub seq
{
	my $this = shift;
	return $this->{filtered} ? '' : $this->{seq};
}

=item len

Returns the length of the sequence (returns 0 if filtered).

=cut

sub len
{
	my $this = shift;
	return $this->{filtered} ? 0 : length($this->{seq});
}

=item revcomp

Reverse-complements a sequence and quality scores.

=cut

sub revcomp
{
	my $this = shift;
	return unless $this->{seq};
	$this->{seq} =~ tr/ATCGatcg/TAGCtagc/;
	my @seq = reverse split(//, $this->{seq});
	$this->{seq} = join('', @seq);
	my @qual = reverse split(//, $this->{qual});
	$this->{qual} = join('', @qual);
}

=item qual ($new_qual)

Returns the read's quality string, without newlines.

=cut

sub qual
{
	my $this = shift;
	return $this->{filtered} ? '' : $this->{qual};
}

=item qual_arrayref

Returns an arrayref of sanger-scaled quality scores.

=cut

sub qual_arrayref
{
	my $this = shift;
	return [] unless $this->{qual};
	return [] if $this->{filtered};
	my @qual = map { ord($_) - 33 } split(//, $this->{qual});
	return \@qual;
}

=item qual_stats

Returns statistics on quality scores.

=cut

sub qual_stats
{
    my $this = shift;
    return undef if $this->{filtered};
    return undef unless $this->{qual};
	my @qual = map { ord($_) - 33 } split(//, $this->{qual});
    my $n = scalar(@qual);
    # odd median
    my $odd_median = odd_median(\@qual);
    # min,max,sum
    my $min = 32767;
    my $max = -1;
    my $sum = 0;
    my %count = ();
    foreach my $q (@qual)
    {
        $min = $q if $q < $min;
        $max = $q if $q > $max;
        $sum += $q;
        $count{$q}++;
    }
    # mean
    my $mean = $sum/$n;
    # std dev
    my $d = 0;
    foreach my $q (@qual)
    {
        $d += ( $q - $mean ) **2;
    }
    my $stdev = $d/$n;
    # mode
    my @result = ();
    foreach (sort { $count{$b} <=> $count{$a} } keys %count)
    {
        last if @result && $count{$_} != $count{$result[0]};
        push(@result, $_);
    }
    my $mode = oddMedian(\@result);
    return ($n,$min,$max,$odd_median,$mode,round($mean),round($stdev),\%count);

}

=item del

Mark this record as filtered.  Always returns 0.

=cut

sub del
{
	my ($this, $reason) = @_;
	die("Missing filter reason\n") unless $reason;
    $this->{seq} = $this->{qual} = '';
	$this->{filtered} = $reason;
    return 0;
}

=item filtered

Returns the reason why the read was filtered; undef otherwise.

=cut

sub filtered { return shift->{filtered} }

=item pass

Returns 1 if read passes QC, 0 otherwise.

=cut

sub pass { return shift->{filtered} ? 0 : 1 }

=item output

Returns a multiline string of the sequence in Fastq format.  Filtered unpaired reads return an empty string,
while filtered paired reads return an empty record, so pairing won't be broken.

=cut

sub output
{
	my $this = shift;
    return '' if $this->filtered;
    return '@' . $this->id . "\n" . $this->{seq} . "\n+\n" . $this->{qual} . "\n";
}

=item output_fasta

Returns a multiline string of the sequence in Fasta format if read passed QC filters.
in the header.

=cut

sub output_fasta
{
	my $this = shift;
    return '' if $this->{filtered} or !$this->len;
    my $fasta = '>'.$this->id."\n";
    my $seq = $this->{seq};
    while (length($seq) > CHARACTERS_PER_LINE)
    {
        $fasta .= substr($seq, 0, CHARACTERS_PER_LINE) . "\n";
        $seq = substr($seq, CHARACTERS_PER_LINE);
    }
    $fasta .= $seq . "\n";
    return $fasta;
}

=item output_qual

Returns a multiline string of the sequence's quality scores in phred format only if read passed QC filters.

=cut

sub output_qual
{
	my $this = shift;
    return '' if $this->filtered or !$this->len;
    my @qual   = map { ord($_) - 33 } split(//, $this->{qual});
    my $output = ">" . $this->id . "\n";
    my $i      = CHARACTERS_PER_LINE - 1;
    while (scalar(@qual) > CHARACTERS_PER_LINE)
    {
        $output .= join(' ', @qual[ 0 .. $i ]) . "\n";
        @qual = @qual[ CHARACTERS_PER_LINE .. $#qual ];
    }
    $output .= join(' ', @qual) . "\n";
    return $output;
}

###############################################################################
## QC METHODS

=back

=head2 QC Methods

Each function returns 1 if read passes filter, 0 otherwise.

=over 5

=item qc

To perform QC steps defined in %qc_params.

=cut

sub qc
{
	my ($this) = @_;
    return 1 if $this->{qc}->{no_qc};
    return 0 unless $this->min_len($this->{qc}->{min_len});
    return 0 unless $this->trim3($this->{qc}->{trim3});
    return 0 unless $this->trim5_bp($this->{qc}->{trim5_bp});
    return 0 unless $this->trim3_bp($this->{qc}->{trim3_bp});
    return 0 unless $this->trim_to($this->{qc}->{trim_to});
    return 0 unless $this->trim3_exp_err($this->{qc}->{trim3_exp_err});
    return 0 unless $this->max_n($this->{qc}->{max_n});
    return 0 unless $this->low_complexity($this->{qc}->{low_complexity});
    return 0 unless $this->low_q($this->{qc}->{low_q});
    return 0 unless $this->exp_err($this->{qc}->{exp_err});
    return 1;
}

=item trim3

Trim uncalled bases from 3' end of read (i.e. bp=N or qual=2).

=cut

sub trim3
{
	my ($this) = @_;
    my $seq = $this->seq;
    my $qual = $this->qual;
    my $len = length($seq);
    if ( $seq =~ s/N+$//g )
    {
        $qual = substr($qual, 0, length($seq));
    }
    if ( $qual =~ s/#+$//g )
    {
        $seq = substr($seq, 0, length($qual));
    }
    $this->{seq} = $seq;
    $this->{qual} = $qual;
    return $this->min_len($this->{qc}->{min_len});
}

=item trim5_bp

Remove the specified number of bases from the 5' end.

=cut

sub trim5_bp
{
    my ($this, $trim5_bp) = @_;
    return 1 unless defined($trim5_bp);
    my $len = $this->len - $trim5_bp;
    if ( $len > 0 )
    {
        $this->{seq} = substr($this->seq, $trim5_bp);
        $this->{qual} = substr($this->qual, $trim5_bp);
    } else
    {
        $this->{seq} = $this->{qual} = '';
    }
    return $this->min_len($this->{qc}->{min_len});
}

=item trim3_bp

Remove the specified number of bases from the 3' end.

=cut

sub trim3_bp
{
    my ($this, $trim3_bp) = @_;
    return 1 unless $trim3_bp;
    my $len = $this->len - $trim3_bp;
    if ( $len > 0 )
    {
        $this->{seq} = substr($this->seq, 0, $len);
        $this->{qual} = substr($this->qual, 0, $len);
    } else
    {
        $this->{seq} = $this->{qual} = '';
    }
    return $this->min_len($this->{qc}->{min_len});
}

=item min_len

If the sequence is shorter than the minimum length, the record is filtered.

=cut

sub min_len
{
    my ($this, $min_len) = @_;
    return 1 unless $min_len;
	return $this->del('too_short') if $this->len < $min_len;
    return 1;
}

=item max_n

If the sequence contains more than the allowed number of Ns, the record is filtered.

=cut

sub max_n
{
    my ($this, $max_n) = @_;
    return 1 unless defined($max_n);
	my $n = $this->{seq} =~ s/N/N/gi;
	return $this->del('too_many_N') if $n > $max_n;
    return $this->min_len($this->{qc}->{min_len});
}

=item low_complexity $pct_len

If the sequence is >= $pct_len mono- or di-nucleotide repeats, the record is filtered.

=cut

sub low_complexity
{
    my ($this, $pct_len) = @_;
    return 1 unless defined($pct_len);
	my $seq = $this->{seq};
	my $len = length($seq);
	return 0 unless $len;
	foreach my $nn (qw/AA TT CC GG CA GT CT GA AT CG/)
	{
		my $n = $seq =~ s/$nn/$nn/gi;
		return $this->del('low_complexity') if ($n*2/$len) >= $pct_len;
	}
    return 1;
}

=item low_q

Filter record if there are too many low-quality bases. Default minq=10.

=cut

sub low_q
{
    my ($this, $max ) = @_;
    return 1 unless $max;
	my $minq = $this->{qc}->{low_q};
    $minq = 10 unless $minq;
	my $n = 0;
	foreach my $q (@{$this->qual_arrayref}) { ++$n if $q < $minq }
	return $this->del('too_many_low_qual') if $n > $max;
    return 1;
}

=item trim3_exp_err

Trim 3' end of read once total expected error of 5' bases reaches max (default = 0.5).  Fail if read has less than minimum specified number of bp (default = 20).

=cut

sub trim3_exp_err
{
    my ($this, $max_exp_err) = @_;
    return 1 unless defined($max_exp_err);
    my $exp_err = 0;
    my $len = 0;
    foreach (split(//, $this->{qual}))
    {
        $exp_err += 10 ** (((ord($_) - 33) * -1)/10);
        last if $exp_err > $max_exp_err;
        ++$len;
    }
    $this->{seq} = substr($this->seq,0,$len);
    $this->{qual} = substr($this->qual,0,$len);
    return $this->min_len($this->{qc}->{min_len});
}

=item exp_err

Fail read if expected number of errors exceeds threshold.

=cut

sub exp_err
{
    my ($this, $max_exp_err) = @_;
    return 1 unless $max_exp_err;
    my $exp_err = 0;
    my @qs = split(//, $this->{qual});
    foreach my $s (@qs)
    {
        my $q = ord($s) - 33;
        my $ee = 10 ** (($q * -1)/10);
        $exp_err += $ee;
        return $this->del('expected_error') if $exp_err > $max_exp_err;
    }
    return 1;
}

=item trim_to

Trim read to exactly this many bp by discarding 3' bases.

=cut

sub trim_to
{
    my ($this, $len) = @_;
    return 1 unless $len;
    return 0 unless $this->len >= $len;
    $this->{seq} = substr($this->seq,0,$len);
    $this->{qual} = substr($this->qual,0,$len);
    return 1;
}

=back

=head1 BUGS AND LIMITATIONS

Reads must be named in accordance with Illumina naming conventions.

=head1 COPYRIGHT

Copyright (c) 2010 U.S. Department of Energy Joint Genome Institute

All right reserved. This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=head1 AUTHOR

Edward Kirton <ESKirton@LBL.gov>

=cut

1;
__END__