3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Grab records in stream.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $in, $out, $record, $keys, $vals_only, $keys_only, $invert,
41 $patterns, $regex, %lookup_hash, $key, $op, $val, $found, $total, $grabbed );
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'patterns', short => 'p', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46 { long => 'patterns_in', short => 'P', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'regex', short => 'r', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'eval', short => 'e', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'exact_in', short => 'E', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 { long => 'invert', short => 'i', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
51 { long => 'case_insensitive', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
52 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
53 { long => 'keys_only', short => 'K', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
54 { long => 'vals_only', short => 'V', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
58 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
59 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
61 $keys = $options->{ 'keys' };
62 $vals_only = $options->{ 'vals_only' };
63 $keys_only = $options->{ 'keys_only' };
64 $invert = $options->{ 'invert' };
66 if ( $options->{ 'patterns' } )
68 $patterns = $options->{ 'patterns' };
70 elsif ( defined $options->{ 'patterns_in' } and -f $options->{ 'patterns_in' } )
72 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
74 elsif ( $options->{ 'regex' } )
76 if ( $options->{ 'case_insensitive' } ) {
77 $regex = qr/$options->{ 'regex' }/i;
79 $regex = qr/$options->{ 'regex' }/;
82 elsif ( defined $options->{ 'exact_in' } and -f $options->{ 'exact_in' } )
84 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
86 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
90 elsif ( $options->{ 'eval' } )
92 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|==|=|!=|eq|ne)\s*(.+)$/ )
100 Maasha::Common::error( qq(Bad eval string: $options->{ 'eval' }) );
107 while ( $record = Maasha::Biopieces::get_record( $in ) )
111 if ( %lookup_hash ) {
112 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
113 } elsif ( $patterns ) {
114 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
116 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
118 $found = grab_eval( $key, $op, $val, $record );
121 if ( $found and not $invert )
123 Maasha::Biopieces::put_record( $record, $out );
126 elsif ( not $found and $invert )
128 Maasha::Biopieces::put_record( $record, $out );
135 Maasha::Biopieces::close_stream( $in );
136 Maasha::Biopieces::close_stream( $out );
138 if ( $options->{ 'verbose' } )
140 print STDERR "Records grabbed: $grabbed\n";
141 print STDERR "Records missed: " . ( $total - $grabbed ) . "\n";
142 print STDERR "Patterns used: " . ( scalar @{$patterns} ) . "\n" if defined $patterns;
143 print STDERR "Patterns used: " . ( scalar keys %lookup_hash ) . "\n" if %lookup_hash;
147 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
152 # Martin A. Hansen, November 2009.
154 # Uses keys from a lookup hash to search records. Optionally, a list of
155 # keys can be given so the lookup is limited to these, also, flags
156 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
159 my ( $lookup_hash, # hashref with patterns
161 $keys, # list of keys - OPTIONAL
162 $vals_only, # only vals flag - OPTIONAL
163 $keys_only, # only keys flag - OPTIONAL
170 map { return 1 if defined $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
174 if ( not $vals_only ) {
175 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
178 if ( not $keys_only ) {
179 map { return 1 if defined $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
189 # Martin A. Hansen, November 2009.
191 # Uses patterns to match records containing the pattern as a substring.
192 # Returns 1 if the record is matched, else 0.
194 my ( $patterns, # list of patterns
196 $keys, # list of keys - OPTIONAL
197 $vals_only, # only vals flag - OPTIONAL
198 $keys_only, # only keys flag - OPTIONAL
203 my ( $pattern, $key );
205 foreach $pattern ( @{ $patterns } )
209 foreach $key ( @{ $keys } )
211 return 0 if not exists $record->{ $key };
212 return 1 if index( $record->{ $key }, $pattern ) >= 0;
217 if ( not $vals_only ) {
218 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
221 if ( not $keys_only ) {
222 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
233 # Martin A. Hansen, November 2009.
235 # Uses regex to match records.
236 # Returns 1 if the record is matched, else 0.
238 my ( $regex, # regex to match
240 $keys, # list of keys - OPTIONAL
241 $vals_only, # only vals flag - OPTIONAL
242 $keys_only, # only keys flag - OPTIONAL
249 map { return 1 if exists $record->{ $_ } and $record->{ $_ } =~ /$regex/ } @{ $keys };
253 if ( not $vals_only ) {
254 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
257 if ( not $keys_only ) {
258 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
268 # Martin A. Hansen, November 2009.
270 # Test if the value of a given record key evaluates according
271 # to a given operator. Returns 1 if eval is OK, else 0.
273 my ( $key, # record key
281 if ( defined $record->{ $key } )
283 return 1 if ( $op eq "<" and $record->{ $key } < $val );
284 return 1 if ( $op eq ">" and $record->{ $key } > $val );
285 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
286 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
287 return 1 if ( $op eq "=" and $record->{ $key } == $val );
288 return 1 if ( $op eq "==" and $record->{ $key } == $val );
289 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
290 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
291 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
298 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
303 Maasha::Biopieces::status_set();
309 Maasha::Biopieces::status_log();
313 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<