]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/intersect_records
fixed seq qual length check
[biopieces.git] / bp_bin / intersect_records
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 # Intersect records in the stream based on overlapping intervals contained in values to specific keys.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Data::Dumper;
33
34
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36
37
38 my ( $options, $in, $out, $record, $opt_key, $opt_strand, $found, $pos, $lookup_hash );
39
40 $options = Maasha::Biopieces::parse_options(
41     [
42         { long => 'key',     short => 'k', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
43         { long => 'strand',  short => 's', type => 'flag',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
44         { long => 'inverse', short => 'i', type => 'flag',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
45     ]   
46 );
47
48 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
49 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
50
51 $opt_key    = $options->{ 'key' };
52 $opt_strand = $options->{ 'strand' };
53
54 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
55 {
56     if ( exists $record->{ 'S_ID' } and exists $record->{ 'S_BEG' } and exists $record->{ 'S_END' } )
57     {
58         $record->{ 'STRAND' } ||= '.';
59
60         if ( exists $record->{ $opt_key } )
61         {
62             map { $lookup_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->{ $_ } = 1 } ( $record->{ 'S_BEG' } .. $record->{ 'S_END' } );
63         }
64         else
65         {
66             $found = 0;
67
68             foreach $pos ( $record->{ 'S_BEG' } .. $record->{ 'S_END' } )
69             {
70                 if ( $opt_strand )
71                 {
72                     if ( exists $lookup_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->{ $pos } )
73                     {
74                         $found = 1;
75
76                         last;
77                     }
78                 }
79                 else
80                 {
81                     if ( exists $lookup_hash->{ $record->{ 'S_ID' } }->{ '+' }->{ $pos } or
82                          exists $lookup_hash->{ $record->{ 'S_ID' } }->{ '-' }->{ $pos }
83                     )
84                     {
85                         $found = 1;
86
87                         last;
88                     }
89                 }
90             }
91
92             if ( (     $found and not $options->{ 'inverse' } ) or
93                  ( not $found and     $options->{ 'inverse' } )
94             )
95             {
96                 Maasha::Biopieces::put_record( $record, $out );
97             }
98         }
99     }
100 }
101
102 # print Dumper( $lookup_hash );
103
104 Maasha::Biopieces::close_stream( $in );
105 Maasha::Biopieces::close_stream( $out );
106
107
108 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
109
110
111 BEGIN
112 {
113     Maasha::Biopieces::status_set();
114 }
115
116
117 END
118 {
119     Maasha::Biopieces::status_log();
120 }
121
122
123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
124
125
126 __END__