]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/read_454
added solexa_str_mean to read_454
[biopieces.git] / bp_bin / read_454
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2010 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Read 454 entries from fasta and quality file.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Biopieces;
33 use Maasha::Filesys;
34 use Maasha::Fasta;
35 use Maasha::Fastq;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $in, $out, $record, $data_in, $qual_in, $num, $fasta, $qual, @seqs, @scores, $i );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'data_in',     short => 'i', type => 'file!',  mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
46         { long => 'qual_in',     short => 'q', type => 'file!',  mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
47         { long => 'num',         short => 'n', type => 'uint',   mandatory => 'no',  default => undef, allowed => undef, disallowed => '0' },
48         { long => 'convert2dec', short => 'c', type => 'flag',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
49         { long => 'cutoff',      short => 'C', type => 'int',    mandatory => 'no',  default => 20,    allowed => undef, disallowed => undef },
50         { long => 'soft_mask',   short => 's', type => 'flag',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
51         { long => 'mean',        short => 'm', type => 'flag',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
52     ]   
53 );
54
55 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
56 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
57
58 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
59     Maasha::Biopieces::put_record( $record, $out );
60 }
61
62 if ( $options->{ 'data_in' } )
63 {
64     $data_in = Maasha::Filesys::file_read_open( $options->{ 'data_in' } );
65     $qual_in = Maasha::Filesys::file_read_open( $options->{ 'qual_in' } );
66
67     $num = 1;
68
69     while ( $fasta = Maasha::Fasta::get_entry( $data_in ) )
70     {
71         $qual = get_qual( $qual_in );
72         
73         check_names( $fasta, $qual );
74
75         $record = {
76             SEQ_NAME    => $fasta->[ 0 ],
77             SEQ         => $fasta->[ 1 ],
78             SCORES      => $qual->[ 1 ],
79         };
80
81         $record->{ 'SCORES_MEAN' } = sprintf "%.2f", Maasha::Fastq::solexa_str_mean( $qual->[ 1 ] ) if $options->{ 'mean' };
82         
83         Maasha::Fastq::softmask_solexa_str( $record->{ 'SEQ' }, $record->{ 'SCORES' }, $options->{ 'cutoff' } ) if $options->{ 'soft_mask' };
84         $record->{ 'SCORES' } = Maasha::Fastq::solexa_str2dec_str( $record->{ 'SCORES' } ) if $options->{ 'convert2dec' };
85
86         Maasha::Biopieces::put_record( $record, $out );
87
88         last if $options->{ "num" } and $num == $options->{ "num" };
89
90         $num++;
91     }
92
93     close $data_in;
94     close $qual_in;
95 }
96
97 Maasha::Biopieces::close_stream( $in );
98 Maasha::Biopieces::close_stream( $out );
99
100
101 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
102
103
104 sub get_qual
105 {
106     # Martin A. Hansen, May 2010.
107     
108     # Get the next 454 quality entry from an open file.
109
110     my ( $fh,   # file handle
111        ) = @_;
112
113     # Returns list.
114
115     my ( $block, $name, $qual, $scores, $entry );
116
117     local $/ = "\n>";
118
119     while ( $block = <$fh> )
120     {
121         chomp $block;
122
123         last if $block !~ /^\s+$/;
124     }
125
126     return if not defined $block;
127
128     $block =~ /^>?(.+)\n/m;
129     $name = $1;
130     $qual = $';
131
132     local $/ = "\n";
133
134     chomp $qual;
135
136     $name =~ tr/\r//d;
137     $qual =~ tr/ \n\r/;;;/;
138     $qual =~ s/;;/;/g;
139
140     $scores = Maasha::Fastq::dec_str2solexa_str( $qual );
141
142     $entry = [ $name, $scores ];
143
144     return wantarray ? @{ $entry } : $entry;
145 }
146
147
148 sub check_names
149 {
150     # Martin A. Hansen, May 2010.
151
152     # Check if the names of the fasta and qual entries match
153     # and raise an error if they don't.
154
155     my ( $fasta,    # fasta entry
156          $qual,     # qual entry
157        ) = @_;
158
159     # Returns nothing.
160
161     my ( $f_name, $q_name );
162
163     if ( $fasta->[ 0 ] =~ /^([^ ]+)/ ) {
164         $f_name = $1;
165     }
166
167     if ( $qual->[ 0 ] =~ /^([^ ]+)/ ) {
168         $q_name = $1;
169     }
170
171     Maasha::Common::error( qq(names don't match "$fasta->[ 0 ]" ne "$qual->[ 0 ]") ) if $f_name ne $q_name;
172 }
173
174
175 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
176
177
178 BEGIN
179 {
180     Maasha::Biopieces::status_set();
181 }
182
183
184 END
185 {
186     Maasha::Biopieces::status_log();
187 }
188
189
190 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
191
192
193 __END__