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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30 use Maasha::Biopieces;
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 my ( $options, $in, $out, $record, $keys, $vals_only, $keys_only, $invert,
39 $patterns, $regex, %lookup_hash, $key, $op, $val, $found );
41 $options = Maasha::Biopieces::parse_options(
43 { long => 'patterns', short => 'p', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
44 { long => 'patterns_in', short => 'P', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45 { long => 'regex', short => 'r', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46 { long => 'eval', short => 'e', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'exact_in', short => 'E', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'invert', short => 'i', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'case_insensitive', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
51 { long => 'keys_only', short => 'K', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
52 { long => 'vals_only', short => 'V', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
56 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
57 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59 $keys = $options->{ 'keys' };
60 $vals_only = $options->{ 'vals_only' };
61 $keys_only = $options->{ 'keys_only' };
62 $invert = $options->{ 'invert' };
64 if ( $options->{ 'patterns' } )
66 $patterns = $options->{ 'patterns' };
68 elsif ( defined $options->{ 'patterns_in' } and -f $options->{ 'patterns_in' } )
70 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
72 elsif ( $options->{ 'regex' } )
74 if ( $options->{ 'case_insensitive' } ) {
75 $regex = qr/$options->{ 'regex' }/i;
77 $regex = qr/$options->{ 'regex' }/;
80 elsif ( defined $options->{ 'exact_in' } and -f $options->{ 'exact_in' } )
82 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
84 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
88 elsif ( $options->{ 'eval' } )
90 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|==|=|!=|eq|ne)\s*(.+)$/ )
98 Maasha::Common::error( qq(Bad eval string: $options->{ 'eval' }) );
102 while ( $record = Maasha::Biopieces::get_record( $in ) )
106 if ( %lookup_hash ) {
107 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
108 } elsif ( $patterns ) {
109 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
111 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
113 $found = grab_eval( $key, $op, $val, $record );
116 if ( $found and not $invert ) {
117 Maasha::Biopieces::put_record( $record, $out );
118 } elsif ( not $found and $invert ) {
119 Maasha::Biopieces::put_record( $record, $out );
123 Maasha::Biopieces::close_stream( $in );
124 Maasha::Biopieces::close_stream( $out );
127 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
132 # Martin A. Hansen, November 2009.
134 # Uses keys from a lookup hash to search records. Optionally, a list of
135 # keys can be given so the lookup is limited to these, also, flags
136 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
139 my ( $lookup_hash, # hashref with patterns
141 $keys, # list of keys - OPTIONAL
142 $vals_only, # only vals flag - OPTIONAL
143 $keys_only, # only keys flag - OPTIONAL
150 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
154 if ( not $vals_only ) {
155 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
158 if ( not $keys_only ) {
159 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
169 # Martin A. Hansen, November 2009.
171 # Uses patterns to match records containing the pattern as a substring.
172 # Returns 1 if the record is matched, else 0.
174 my ( $patterns, # list of patterns
176 $keys, # list of keys - OPTIONAL
177 $vals_only, # only vals flag - OPTIONAL
178 $keys_only, # only keys flag - OPTIONAL
185 foreach $pattern ( @{ $patterns } )
189 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
193 if ( not $vals_only ) {
194 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
197 if ( not $keys_only ) {
198 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
209 # Martin A. Hansen, November 2009.
211 # Uses regex to match records.
212 # Returns 1 if the record is matched, else 0.
214 my ( $regex, # regex to match
216 $keys, # list of keys - OPTIONAL
217 $vals_only, # only vals flag - OPTIONAL
218 $keys_only, # only keys flag - OPTIONAL
225 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
229 if ( not $vals_only ) {
230 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
233 if ( not $keys_only ) {
234 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
244 # Martin A. Hansen, November 2009.
246 # Test if the value of a given record key evaluates according
247 # to a given operator. Returns 1 if eval is OK, else 0.
249 my ( $key, # record key
257 if ( defined $record->{ $key } )
259 return 1 if ( $op eq "<" and $record->{ $key } < $val );
260 return 1 if ( $op eq ">" and $record->{ $key } > $val );
261 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
262 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
263 return 1 if ( $op eq "=" and $record->{ $key } == $val );
264 return 1 if ( $op eq "==" and $record->{ $key } == $val );
265 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
266 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
267 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
274 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
279 Maasha::Biopieces::status_set();
285 Maasha::Biopieces::status_log();
289 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<