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