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