3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Merge records in the stream based on identifier values to specific keys.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 my ( $options, $in, $out, $record, $tmp_dir, $file1, $file2, $fh1, $fh2,
40 $key1, $key2, @keys1, @keys2, @vals1, @vals2, $num1, $num2, $num, $cmp, $i );
42 $options = Maasha::Biopieces::parse_options(
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 },
49 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
52 $tmp_dir = Maasha::Biopieces::get_tmpdir();
54 $file1 = "$tmp_dir/merge_records1.tmp";
55 $file2 = "$tmp_dir/merge_records2.tmp";
57 $fh1 = Maasha::Filesys::file_write_open( $file1 );
58 $fh2 = Maasha::Filesys::file_write_open( $file2 );
60 $key1 = $options->{ "keys" }->[ 0 ];
61 $key2 = $options->{ "keys" }->[ 1 ];
63 $num = $key2 =~ s/n$//;
67 while ( $record = Maasha::Biopieces::get_record( $in ) )
69 if ( exists $record->{ $key1 } )
72 @vals1 = $record->{ $key1 };
74 delete $record->{ $key1 };
76 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
78 print $fh1 join( "\t", @vals1 ), "\n";
82 elsif ( exists $record->{ $key2 } )
85 @vals2 = $record->{ $key2 };
87 delete $record->{ $key2 };
89 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
91 print $fh2 join( "\t", @vals2 ), "\n";
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;
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;
111 $fh1 = Maasha::Filesys::file_read_open( $file1 );
112 $fh2 = Maasha::Filesys::file_read_open( $file2 );
114 @vals1 = Maasha::Common::get_fields( $fh1 );
115 @vals2 = Maasha::Common::get_fields( $fh2 );
117 while ( $num1 > 0 and $num2 > 0 )
122 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
124 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
129 if ( $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
131 for ( $i = 0; $i < @keys1; $i++ ) {
132 $record->{ $keys1[ $i ] } = $vals1[ $i ];
135 Maasha::Biopieces::put_record( $record, $out );
138 @vals1 = Maasha::Common::get_fields( $fh1 );
143 if ( $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
145 for ( $i = 0; $i < @keys2; $i++ ) {
146 $record->{ $keys2[ $i ] } = $vals2[ $i ];
149 Maasha::Biopieces::put_record( $record, $out );
152 @vals2 = Maasha::Common::get_fields( $fh2 );
157 if ( $options->{ 'merge' } =~ /^(AandB|AorB|BorA)$/ )
159 for ( $i = 0; $i < @keys1; $i++ ) {
160 $record->{ $keys1[ $i ] } = $vals1[ $i ];
163 for ( $i = 1; $i < @keys2; $i++ ) {
164 $record->{ $keys2[ $i ] } = $vals2[ $i ];
167 Maasha::Biopieces::put_record( $record, $out );
170 @vals1 = Maasha::Common::get_fields( $fh1 );
171 @vals2 = Maasha::Common::get_fields( $fh2 );
183 if ( $num1 > 0 and $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
187 for ( $i = 0; $i < @keys1; $i++ ) {
188 $record->{ $keys1[ $i ] } = $vals1[ $i ];
191 Maasha::Biopieces::put_record( $record, $out );
194 if ( $num2 > 0 and $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
198 for ( $i = 0; $i < @keys2; $i++ ) {
199 $record->{ $keys2[ $i ] } = $vals2[ $i ];
202 Maasha::Biopieces::put_record( $record, $out );
205 Maasha::Biopieces::close_stream( $in );
206 Maasha::Biopieces::close_stream( $out );
209 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
214 Maasha::Biopieces::status_set();
220 Maasha::Biopieces::status_log();
224 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<