]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/merge_records
fixed seq qual length check
[biopieces.git] / bp_bin / merge_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 # Merge records in the stream based on identifier values to specific keys.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::Filesys;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $record, $tmp_dir, $file1, $file2, $fh1, $fh2,
40      $key1, $key2, @keys1, @keys2, @vals1, @vals2, $num1, $num2, $num, $cmp, $i );
41
42 $options = Maasha::Biopieces::parse_options(
43     [
44         { long => 'keys',  short => 'k', type => 'list',   mandatory => 'yes', default => undef,   allowed => undef,                         disallowed => undef },
45         { long => 'merge', short => 'm', type => 'string', mandatory => 'no',  default => 'AandB', allowed => 'AandB,AorB,BorA,AnotB,BnotA', disallowed => undef },
46     ]   
47 );
48
49 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
51
52 $tmp_dir = Maasha::Biopieces::get_tmpdir();
53
54 $file1 = "$tmp_dir/merge_records1.tmp";
55 $file2 = "$tmp_dir/merge_records2.tmp";
56
57 $fh1   = Maasha::Filesys::file_write_open( $file1 );
58 $fh2   = Maasha::Filesys::file_write_open( $file2 );
59
60 $key1  = $options->{ "keys" }->[ 0 ];
61 $key2  = $options->{ "keys" }->[ 1 ];
62
63 $num   = $key2 =~ s/n$//;
64 $num1  = 0;
65 $num2  = 0;
66
67 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
68 {
69     if ( exists $record->{ $key1 } )
70     {
71         @keys1 = $key1;
72         @vals1 = $record->{ $key1 };
73
74         delete $record->{ $key1 };
75
76         map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
77
78         print $fh1 join( "\t", @vals1 ), "\n";
79
80         $num1++;
81     }
82     elsif ( exists $record->{ $key2 } )
83     {
84         @keys2 = $key2;
85         @vals2 = $record->{ $key2 };
86
87         delete $record->{ $key2 };
88
89         map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
90
91         print $fh2 join( "\t", @vals2 ), "\n";
92
93         $num2++;
94     }
95 }
96
97 close $fh1;
98 close $fh2;
99
100 if ( $num )
101 {
102     Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
103     Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
104 }
105 else
106 {
107     Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
108     Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
109 }
110
111 $fh1 = Maasha::Filesys::file_read_open( $file1 );
112 $fh2 = Maasha::Filesys::file_read_open( $file2 );
113
114 @vals1 = Maasha::Common::get_fields( $fh1 );
115 @vals2 = Maasha::Common::get_fields( $fh2 );
116
117 while ( $num1 > 0 and $num2 > 0 )
118 {
119     undef $record;
120
121     if ( $num ) {
122         $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
123     } else {
124         $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
125     }
126
127     if ( $cmp < 0 )
128     {
129         if ( $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
130         {
131             for ( $i = 0; $i < @keys1; $i++ ) {
132                 $record->{ $keys1[ $i ] } = $vals1[ $i ];
133             }
134
135             Maasha::Biopieces::put_record( $record, $out );
136         }
137
138         @vals1 = Maasha::Common::get_fields( $fh1 );
139         $num1--;
140     }
141     elsif ( $cmp > 0 )
142     {
143         if ( $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
144         {
145             for ( $i = 0; $i < @keys2; $i++ ) {
146                 $record->{ $keys2[ $i ] } = $vals2[ $i ];
147             }
148
149             Maasha::Biopieces::put_record( $record, $out );
150         }
151
152         @vals2 = Maasha::Common::get_fields( $fh2 );
153         $num2--;
154     }
155     else
156     {
157         if ( $options->{ 'merge' } =~ /^(AandB|AorB|BorA)$/ )
158         {
159             for ( $i = 0; $i < @keys1; $i++ ) {
160                 $record->{ $keys1[ $i ] } = $vals1[ $i ];
161             }
162
163             for ( $i = 1; $i < @keys2; $i++ ) {
164                 $record->{ $keys2[ $i ] } = $vals2[ $i ];
165             }
166         
167             Maasha::Biopieces::put_record( $record, $out );
168         }
169
170         @vals1 = Maasha::Common::get_fields( $fh1 );
171         @vals2 = Maasha::Common::get_fields( $fh2 );
172         $num1--;
173         $num2--;
174     }
175 }
176
177 close $fh1;
178 close $fh2;
179
180 unlink $file1;
181 unlink $file2;
182
183 if ( $num1 > 0 and $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
184 {
185     undef $record;
186
187     for ( $i = 0; $i < @keys1; $i++ ) {
188         $record->{ $keys1[ $i ] } = $vals1[ $i ];
189     }
190
191     Maasha::Biopieces::put_record( $record, $out );
192 }
193
194 if ( $num2 > 0 and $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
195 {
196     undef $record;
197
198     for ( $i = 0; $i < @keys2; $i++ ) {
199         $record->{ $keys2[ $i ] } = $vals2[ $i ];
200     }
201
202     Maasha::Biopieces::put_record( $record, $out );
203 }
204
205 Maasha::Biopieces::close_stream( $in );
206 Maasha::Biopieces::close_stream( $out );
207
208
209 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
210
211
212 BEGIN
213 {
214     Maasha::Biopieces::status_set();
215 }
216
217
218 END
219 {
220     Maasha::Biopieces::status_log();
221 }
222
223
224 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
225
226
227 __END__