]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/grab
fixed nasty bug in grab where -E and pattern 0 failed
[biopieces.git] / bp_bin / grab
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 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 # Grab records in stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Biopieces;
33 use Maasha::Common;
34 use Maasha::Patscan;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $record, $keys, $vals_only, $keys_only, $invert,
41      $patterns, $regex, %lookup_hash, $key, $op, $val, $found );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
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 },
55     ]   
56 );
57
58 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
59 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
60
61 $keys      = $options->{ 'keys' };
62 $vals_only = $options->{ 'vals_only' };
63 $keys_only = $options->{ 'keys_only' };
64 $invert    = $options->{ 'invert' };
65
66 if ( $options->{ 'patterns' } )
67 {
68     $patterns = $options->{ 'patterns' };
69 }
70 elsif ( defined $options->{ 'patterns_in' } and -f $options->{ 'patterns_in' } )
71 {
72     $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
73 }
74 elsif ( $options->{ 'regex' } )
75 {
76     if ( $options->{ 'case_insensitive' } ) {
77         $regex = qr/$options->{ 'regex' }/i;
78     } else {
79         $regex = qr/$options->{ 'regex' }/;
80     }
81 }
82 elsif ( defined $options->{ 'exact_in' } and -f $options->{ 'exact_in' } )
83 {
84     $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
85
86     map { $lookup_hash{ $_ } = 1 } @{ $patterns };
87
88     undef $patterns;
89 }
90 elsif ( $options->{ 'eval' } )
91 {
92     if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|==|=|!=|eq|ne)\s*(.+)$/ )
93     {
94         $key = $1;
95         $op  = $2;
96         $val = $3;
97     }
98     else
99     {
100         Maasha::Common::error( qq(Bad eval string: $options->{ 'eval' }) );
101     }
102
103
104 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
105 {
106     $found = 0;
107
108     if ( %lookup_hash ) {
109         $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
110     } elsif ( $patterns ) {
111         $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
112     } elsif ( $regex ) {
113         $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
114     } elsif ( $op ) {
115         $found = grab_eval( $key, $op, $val, $record );
116     }
117
118     if ( $found and not $invert ) {
119         Maasha::Biopieces::put_record( $record, $out );
120     } elsif ( not $found and $invert ) {
121         Maasha::Biopieces::put_record( $record, $out );
122     }
123 }
124
125 Maasha::Biopieces::close_stream( $in );
126 Maasha::Biopieces::close_stream( $out );
127
128
129 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
130
131
132 sub grab_lookup
133 {
134     # Martin A. Hansen, November 2009.
135
136     # Uses keys from a lookup hash to search records. Optionally, a list of
137     # keys can be given so the lookup is limited to these, also, flags
138     # can be given to limit lookup to keys or vals only. Returns 1 if lookup
139     # succeeded, else 0.
140
141     my ( $lookup_hash,   # hashref with patterns
142          $record,        # hashref
143          $keys,          # list of keys   - OPTIONAL
144          $vals_only,     # only vals flag - OPTIONAL
145          $keys_only,     # only keys flag - OPTIONAL
146        ) = @_;
147
148     # Returns boolean.
149
150     if ( $keys )
151     {
152         map { return 1 if defined $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
153     }
154     else
155     {
156         if ( not $vals_only ) {
157             map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
158         }
159
160         if ( not $keys_only ) {
161             map { return 1 if $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
162         }
163     }
164
165     return 0;
166 }
167
168
169 sub grab_patterns
170 {
171     # Martin A. Hansen, November 2009.
172
173     # Uses patterns to match records containing the pattern as a substring.
174     # Returns 1 if the record is matched, else 0.
175
176     my ( $patterns,   # list of patterns
177          $record,     # hashref
178          $keys,       # list of keys   - OPTIONAL
179          $vals_only,  # only vals flag - OPTIONAL
180          $keys_only,  # only keys flag - OPTIONAL
181        ) = @_;
182
183     # Returns boolean.
184
185     my ( $pattern, $key );
186
187     foreach $pattern ( @{ $patterns } )
188     {
189         if ( $keys )
190         {
191             foreach $key ( @{ $keys } )
192             {
193                 return 0 if not exists $record->{ $key };
194                 return 1 if index( $record->{ $key }, $pattern ) >= 0;
195             }
196         }
197         else
198         {
199             if ( not $vals_only ) {
200                 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
201             }
202
203             if ( not $keys_only ) {
204                 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
205             }
206         }
207     }
208
209     return 0;
210 }
211
212
213 sub grab_regex
214 {
215     # Martin A. Hansen, November 2009.
216
217     # Uses regex to match records.
218     # Returns 1 if the record is matched, else 0.
219
220     my ( $regex,      # regex to match
221          $record,     # hashref
222          $keys,       # list of keys   - OPTIONAL
223          $vals_only,  # only vals flag - OPTIONAL
224          $keys_only,  # only keys flag - OPTIONAL
225        ) = @_;
226
227     # Returns boolean.
228
229     if ( $keys )
230     {
231         map { return 1 if exists $record->{ $_ } and $record->{ $_ } =~ /$regex/ } @{ $keys };
232     }
233     else
234     {
235         if ( not $vals_only ) {
236             map { return 1 if $_ =~ /$regex/ } keys %{ $record };
237         }
238
239         if ( not $keys_only ) {
240             map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
241         }
242     }
243
244     return 0;
245 }
246
247
248 sub grab_eval
249 {
250     # Martin A. Hansen, November 2009.
251
252     # Test if the value of a given record key evaluates according
253     # to a given operator. Returns 1 if eval is OK, else 0.
254
255     my ( $key,     # record key
256          $op,      # operator
257          $val,     # value
258          $record,  # hashref
259        ) = @_;
260     
261     # Returns boolean.
262
263     if ( defined $record->{ $key } ) 
264     {
265         return 1 if ( $op eq "<"  and $record->{ $key } <  $val );
266         return 1 if ( $op eq ">"  and $record->{ $key } >  $val );
267         return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
268         return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
269         return 1 if ( $op eq "="  and $record->{ $key } == $val );
270         return 1 if ( $op eq "==" and $record->{ $key } == $val );
271         return 1 if ( $op eq "!=" and $record->{ $key } != $val );
272         return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
273         return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
274     }
275
276     return 0;
277 }
278
279
280 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
281
282
283 BEGIN
284 {
285     Maasha::Biopieces::status_set();
286 }
287
288
289 END
290 {
291     Maasha::Biopieces::status_log();
292 }
293
294
295 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
296
297
298 __END__