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