Debian Codegraph @

pytools/tools/patterN_fastq.pl

raw
1 #!/usr/bin/env perl
2 # counts N bases occuring in same spot on a read to identify stutters in illumina runs
3 # by packing bases into a bitstring and using as hash key.
4 # TODO: these have been observed occuring tilewise most often but also across lanes so
5 # counting by tile rather than by all input is probably useful.
6
7
8 use strict;
9 use warnings;
10 use Getopt::Long;
11
12 my $min_count = 1;
13 my $min_pct = 1 / 120; # based on 120 tiles to a lane
14 my $length = 0;
15 my $i = 0;
16 my %hash = ();
17 my $tile_parse = 0;
18 my $analog = 0;
19 my ( $in_file, $out_file, $help );
20
21 GetOptions(
22 'count=i' => \$min_count,
23 'pct=f' => \$min_pct,
24 'in=s' => \$in_file,
25 'help' => \$help,
26 'tile' => \$tile_parse,
27 'analog' => \$analog,
28 'out=s' => \$out_file,
29 ) or die $!;
30
31 if( $help || ! defined $in_file ) {
32 print STDERR "$0 -count MIN_OCCURENCES -pct MIN_PCT_READS_WITH_PATTERN -in s_1_1_sequence.fastq [ -out out.stats ][ -tile ][ -analog ]\n";
33 print STDERR "\n";
34 print STDERR " -count INT min. occurences of a pattern before it's reported\t[ $min_count ]\n";
35 print STDERR " -pct FLOAT min. % of reads with a particular pattern before it's reported\t[ 1 / 120 ]\n";
36 print STDERR " -tile evaluate pct per tile\n";
37 print STDERR " -analog output like -N--- instead of the hipper 01000\n";
38 print STDERR "\n";
39 exit;
40 }
41
42 open( my $fh, $in_file ) or die "ERROR: failed to open $in_file for reading $!";
43
44 my $out_fh;
45 if ( $out_file ) {
46 open( $out_fh, '>', $out_file ) or die $!;
47 }
48 else {
49 $out_fh = *STDOUT;
50 }
51
52 my %tile_counts = ();
53
54 while ( <$fh> ) { # readname
55 my $tile = $tile_parse ? ( split /:/,$_,4 )[2] : '';
56 $i++;
57 if( $tile_parse ) { $tile_counts{ $tile }++; }
58 $_=<$fh>; # sequence
59 if ( /N/ ) {
60 chomp;
61 tr/ACGTN/00001/;
62 if ( ! $length ) {
63 $length = length;
64 }
65 if ( $tile_parse ) {
66 $hash{ $tile }{ pack( "b$length", $_ ) }++;
67 }
68 else {
69 $hash{ pack( "b$length", $_ ) }++;
70 }
71 }
72 <$fh>; # seperator of seq/qual
73 <$fh>; # quality
74 }
75 my $sum_of_bad_past_threshold_pct = 0;
76 my $sum_of_bad_past_threshold = 0;
77 my %tile_sums = ();
78
79 if ( $tile_parse ) { #key is a tile, then maps as sub keys.
80 for my $key ( sort { $a <=> $b } keys %hash ) {
81 for my $pattern ( sort { $hash{ $key }{ $a } <=> $hash{ $key }{ $b } } keys %{ $hash{ $key } } ) {
82 if ( $hash{ $key }->{$pattern} >= $min_count && $hash{ $key }->{$pattern} / $tile_counts{ $key } * 100 >= $min_pct ) {
83 my $print_pattern = unpack( "b$length", $pattern );
84 if ( $analog ) { $print_pattern =~ tr/01/-N/; }
85 print $out_fh "$key\t$hash{ $key }{ $pattern }\t$print_pattern\n";
86 $sum_of_bad_past_threshold_pct += $hash{ $key }->{$pattern} / $i * 100;
87 $sum_of_bad_past_threshold += $hash{ $key }->{$pattern};
88 $tile_sums{ $key } += $hash{ $key }->{$pattern} / $tile_counts{ $key } * 100;
89 }
90 }
91 }
92 }
93 else {
94 for my $key ( keys %hash ) {
95 if ( $hash{ $key } >= $min_count && $hash{ $key } / $i * 100 >= $min_pct ) {
96 my $print_pattern = unpack( "b$length", $key );
97 if ( $analog ) { $print_pattern =~ tr/01/-N/; }
98 print $out_fh $hash{$key} , "\t$print_pattern\n";
99 $sum_of_bad_past_threshold_pct += $hash{ $key } / $i * 100;
100 $sum_of_bad_past_threshold += $hash{ $key };
101 }
102 }
103 }
104
105 # summary stuff
106
107 if ( $sum_of_bad_past_threshold_pct ) {
108 print $out_fh "sum pct patterNs past $min_pct == $sum_of_bad_past_threshold_pct ( $sum_of_bad_past_threshold / $i * 100 )\n";
109 }
110
111 if ( $tile_parse && $sum_of_bad_past_threshold_pct ) {
112 for my $tile ( sort { $tile_sums{ $a } <=> $tile_sums{ $b } } keys %tile_sums ) {
113 print $out_fh "tile $tile pct patterNs past $min_pct == $tile_sums{ $tile }\n";
114 }
115 }