]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/read_454
polished read_454 and added tests
[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     ]   
49 );
50
51 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
52 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53
54 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
55     Maasha::Biopieces::put_record( $record, $out );
56 }
57
58 if ( $options->{ 'data_in' } )
59 {
60     $data_in = Maasha::Filesys::file_read_open( $options->{ 'data_in' } );
61     $qual_in = Maasha::Filesys::file_read_open( $options->{ 'qual_in' } );
62
63     $num = 1;
64
65     while ( $fasta = Maasha::Fasta::get_entry( $data_in ) )
66     {
67         $qual = get_qual( $qual_in );
68         
69         check_names( $fasta, $qual );
70
71         $record = {
72             SEQ_NAME    => $fasta->[ 0 ],
73             SEQ         => $fasta->[ 1 ],
74             SEQ_LEN     => length $fasta->[ 1 ],
75             SCORES      => $qual->[ 1 ],
76         };
77
78         Maasha::Biopieces::put_record( $record, $out );
79
80         last if $options->{ "num" } and $num == $options->{ "num" };
81
82         $num++;
83     }
84
85     close $data_in;
86     close $qual_in;
87 }
88
89 Maasha::Biopieces::close_stream( $in );
90 Maasha::Biopieces::close_stream( $out );
91
92
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
94
95
96 sub get_qual
97 {
98     # Martin A. Hansen, May 2010.
99     
100     # Get the next 454 quality entry from an open file.
101
102     my ( $fh,   # file handle
103        ) = @_;
104
105     # Returns list.
106
107     my ( $block, $name, $qual, $scores, $entry );
108
109     local $/ = "\n>";
110
111     while ( $block = <$fh> )
112     {
113         chomp $block;
114
115         last if $block !~ /^\s+$/;
116     }
117
118     return if not defined $block;
119
120     $block =~ /^>?(.+)\n/m;
121     $name = $1;
122     $qual = $';
123
124     local $/ = "\n";
125
126     chomp $qual;
127
128     $name =~ tr/\r//d;
129     $qual =~ tr/ \n\r/;;;/;
130     $qual =~ s/;;/;/g;
131
132     $scores = Maasha::Fastq::dec_str2solexa_str( $qual );
133
134     $entry = [ $name, $scores ];
135
136     return wantarray ? @{ $entry } : $entry;
137 }
138
139
140 sub check_names
141 {
142     # Martin A. Hansen, May 2010.
143
144     # Check if the names of the fasta and qual entries match
145     # and raise an error if they don't.
146
147     my ( $fasta,    # fasta entry
148          $qual,     # qual entry
149        ) = @_;
150
151     # Returns nothing.
152
153     my ( $f_name, $q_name );
154
155     if ( $fasta->[ 0 ] =~ /^([^ ]+)/ ) {
156         $f_name = $1;
157     }
158
159     if ( $qual->[ 0 ] =~ /^([^ ]+)/ ) {
160         $q_name = $1;
161     }
162
163     Maasha::Common::error( qq(names don't match "$fasta->[ 0 ]" ne "$qual->[ 0 ]") ) if $f_name ne $q_name;
164 }
165
166
167 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
168
169
170 BEGIN
171 {
172     Maasha::Biopieces::status_set();
173 }
174
175
176 END
177 {
178     Maasha::Biopieces::status_log();
179 }
180
181
182 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
183
184
185 __END__