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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30 use Maasha::Biopieces;
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $tmp_dir, $file1, $file2, $fh1, $fh2,
39 $key1, $key2, @keys1, @keys2, @vals1, @vals2, $num1, $num2, $num, $cmp, $i );
41 $options = Maasha::Biopieces::parse_options(
43 { long => 'keys', short => 'k', type => 'list', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
44 { long => 'merge', short => 'm', type => 'string', mandatory => 'no', default => 'AandB', allowed => 'AandB,AorB,BorA,AnotB,BnotA', disallowed => undef },
48 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
49 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
51 $tmp_dir = Maasha::Biopieces::get_tmpdir();
53 $file1 = "$tmp_dir/merge_records1.tmp";
54 $file2 = "$tmp_dir/merge_records2.tmp";
56 $fh1 = Maasha::Filesys::file_write_open( $file1 );
57 $fh2 = Maasha::Filesys::file_write_open( $file2 );
59 $key1 = $options->{ "keys" }->[ 0 ];
60 $key2 = $options->{ "keys" }->[ 1 ];
62 $num = $key2 =~ s/n$//;
66 while ( $record = Maasha::Biopieces::get_record( $in ) )
68 if ( exists $record->{ $key1 } )
71 @vals1 = $record->{ $key1 };
73 delete $record->{ $key1 };
75 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
77 print $fh1 join( "\t", @vals1 ), "\n";
81 elsif ( exists $record->{ $key2 } )
84 @vals2 = $record->{ $key2 };
86 delete $record->{ $key2 };
88 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
90 print $fh2 join( "\t", @vals2 ), "\n";
101 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
102 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
106 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
107 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
110 $fh1 = Maasha::Filesys::file_read_open( $file1 );
111 $fh2 = Maasha::Filesys::file_read_open( $file2 );
113 @vals1 = Maasha::Common::get_fields( $fh1 );
114 @vals2 = Maasha::Common::get_fields( $fh2 );
116 while ( $num1 > 0 and $num2 > 0 )
121 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
123 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
128 if ( $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
130 for ( $i = 0; $i < @keys1; $i++ ) {
131 $record->{ $keys1[ $i ] } = $vals1[ $i ];
134 Maasha::Biopieces::put_record( $record, $out );
137 @vals1 = Maasha::Common::get_fields( $fh1 );
142 if ( $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
144 for ( $i = 0; $i < @keys2; $i++ ) {
145 $record->{ $keys2[ $i ] } = $vals2[ $i ];
148 Maasha::Biopieces::put_record( $record, $out );
151 @vals2 = Maasha::Common::get_fields( $fh2 );
156 if ( $options->{ 'merge' } =~ /^(AandB|AorB|BorA)$/ )
158 for ( $i = 0; $i < @keys1; $i++ ) {
159 $record->{ $keys1[ $i ] } = $vals1[ $i ];
162 for ( $i = 1; $i < @keys2; $i++ ) {
163 $record->{ $keys2[ $i ] } = $vals2[ $i ];
166 Maasha::Biopieces::put_record( $record, $out );
169 @vals1 = Maasha::Common::get_fields( $fh1 );
170 @vals2 = Maasha::Common::get_fields( $fh2 );
182 if ( $num1 > 0 and $options->{ 'merge' } =~ /^(AorB|AnotB)$/ )
186 for ( $i = 0; $i < @keys1; $i++ ) {
187 $record->{ $keys1[ $i ] } = $vals1[ $i ];
190 Maasha::Biopieces::put_record( $record, $out );
193 if ( $num2 > 0 and $options->{ 'merge' } =~ /^(BorA|BnotA)$/ )
197 for ( $i = 0; $i < @keys2; $i++ ) {
198 $record->{ $keys2[ $i ] } = $vals2[ $i ];
201 Maasha::Biopieces::put_record( $record, $out );
204 Maasha::Filesys::dir_remove( $tmp_dir );
207 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
212 $run_time_beg = Maasha::Biopieces::run_time();
214 Maasha::Biopieces::log_biopiece();
220 Maasha::Biopieces::close_stream( $in );
221 Maasha::Biopieces::close_stream( $out );
223 $run_time_end = Maasha::Biopieces::run_time();
225 Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
229 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<