-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge2tabs.pl
318 lines (251 loc) · 9.38 KB
/
merge2tabs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/perl
=head1 NAME
merge2tabs.pl - Merge 2 tables by the columns with unique IDs
=head1 SYNOPSIS
perl -w merge2tabs.pl --table1=<path to table 1> --table2=<path to table 2> --output=<path to merged> [--colID_tab1=<column Nr. tab1> --colID_tab2=<column Nr. tab2> --gzip --help]
Required arguments:
--table1 | -t1 path to table 1
--table2 | -t2 path to table 2
--output | -out path to output merged table
Options:
--colID_tab1 | -c1 column Nr. with unique IDs, table 1 (default: 0)
--colID_tab2 | -c2 column Nr. with unique IDs, table 2 (default: 0)
--gzip compress resulting merged table with gzip (when found in the system path)
--help | h Help
Example usage:
perl -w merge2tabs.pl --table1=table1.txt.gz --table2=table2.txt.gz --output=merged.txt.gz --colID_tab1=1 --colID_tab2=2
OR
perl -w merge2tabs.pl -t1 table1.txt.gz -t2 table2.txt.gz -out merged.txt.gz -c1=1 -c2=2
=head1 DESCRIPTION
=head2 NucTools 1.0 package.
NucTools is a software package for analysis of chromatin feature occupancy profiles from high-throughput sequencing data
=head2 average_replicates.pl
extend_PE_reads.pl Merge 2 tables by the columns with unique IDs
=head1 AUTHORS
=over
=item
Yevhen Vainshtein <[email protected]>
=item
Vladimir Teif
=back
=head2 Last modified
18 October 2016
=head1 LICENSE
Copyright (C) 2012-2016 Yevhen Vainshtein, Vladimir Teif
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=cut
use strict "vars";
use Getopt::Long;
use Pod::Usage;
# optional gzip support if modules are installed
my ($ModuleGzipIsLoaded, $ModuleGunzipIsLoaded);
BEGIN { $ModuleGunzipIsLoaded = eval "require IO::Uncompress::Gunzip; 1"; }
BEGIN { $ModuleGzipIsLoaded = eval "require IO::Compress::Gzip; IO::Compress::Gzip->import( qw[gzip] );1"; }
my ($path_tab1, $path_tab2, $path2output);
my $colID_tab1 = 0;
my $colID_tab2 = 0;
my $needsHelp;
my $useGzip;
my $options_okay = &Getopt::Long::GetOptions(
'table1|t1=s' => \$path_tab1,
'table2|t2=s' => \$path_tab2,
'output|out=s' => \$path2output,
'colID_tab1|c1=s' => \$colID_tab1,
'colID_tab2|c2=s' => \$colID_tab2,
'gzip' => \$useGzip,
'help|h' => \$needsHelp
);
# Check to make sure options are specified correctly and files exist
&check_opts();
# check if GZIP is loaded
if ( ((!$ModuleGzipIsLoaded) or (!$ModuleGunzipIsLoaded)) and ($useGzip) ) {
print STDERR "Can't work with GZIP: IO::Compress::Gzip is not on PATH\n";
exit;
}
elsif ( (($ModuleGzipIsLoaded) and ($ModuleGunzipIsLoaded)) and ($useGzip) ) {
print STDERR "ZGIP support enabled\n";
}
else {
print STDERR "ZGIP support disabled\n";
if ( ( ($path_tab1 =~ (/.*\.gz$/)) or ($path_tab2 =~ (/.*\.gz$/)) ) and (!$useGzip) ) {
print STDERR "======================================\n";
print STDERR "WARNING! Input file probably compressed!\n";
print STDERR "Use --gzip parameter to enable support for file compression/decompression!";
print STDERR "======================================\n";
exit;
}
}
print STDERR "table 1:",$path_tab1, "\n";
print STDERR "table 2:",$path_tab2, "\n";
print STDERR "column with ID, table 1:",$colID_tab1, "\n";
print STDERR "column with ID, table 2:", $colID_tab2, "\n";
print STDERR "save merged table to a file: ",$path2output, "\n";
my (@array_tab1,@array_tab2);
# open occupancy file
my $inFH1;
if ( $path_tab1 =~ (/.*\.gz$/) ) {
$inFH1 = IO::Uncompress::Gunzip->new( $path_tab1 )
or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
}
else { open( $inFH1, "<", $path_tab1 ) or die "error: $path_tab1 cannot be opened:$!"; }
while (<$inFH1>) { for my $chank (split/\r\n/) { my $text = clean($chank); push(@array_tab1, $text); } }
close($inFH1);
my $inFH2;
if ( $path_tab2 =~ (/.*\.gz$/) ) {
$inFH2 = IO::Uncompress::Gunzip->new( $path_tab2 )
or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip failed::GunzipError\n";
}
else { open( $inFH2, "<", $path_tab2 ) or die "error: $path_tab2 cannot be opened:$!"; }
while (<$inFH2>) { for my $chank (split/\r\n/) { my $text = clean($chank); push(@array_tab2, $text); } }
close($inFH2);
my ($array_tab1_ref, $array_tab2_ref,$match_array1_ref,$match_array2_ref) = compare_2_arrays($colID_tab1, $colID_tab2, \@array_tab1, \@array_tab2);
my @joined_arrays = join_arrays($match_array1_ref,$match_array2_ref);
my $OUT_FHs;
if ($useGzip) {
# open pipe to Gzip or open text file for writing
my $out_file = $path2output;
$out_file =~ s/(.*)\.gz$/$1/;
my $gz_out_file = $out_file.".gz";
$OUT_FHs = new IO::Compress::Gzip ($gz_out_file) or open ">$out_file" or die "Can't open $out_file for writing: $!\n";
} else {
open ($OUT_FHs, ">$path2output") or die "Can't open $path2output for writing: $!\n";
}
print $OUT_FHs join("\n", @joined_arrays), "\n";
close($OUT_FHs);
print STDERR "\nfinished\n";
exit;
#-------------------------------------------------------------
#-------------- match 2 arrays--------------------------------
sub compare_2_arrays {
my ($selected_column1, $selected_column2, $array_ref1, $array_ref2) = @_;
my @array1 = @{$array_ref1};
my @array2 = @{$array_ref2};
my $nr_of_cols1 = () = $array1[0] =~ /\t/g;
my $nr_of_cols2 = () = $array2[0] =~ /\t/g;
my $empty_string_array1 = "\t"x$nr_of_cols1;
my $empty_string_array2 = "\t"x$nr_of_cols2;
my $counter1=1;
my $counter2;
my (@match_array1, @match_array2);
my @sel_column_array1 = Read_column($selected_column1, $array_ref1);
my @sel_column_array2 = Read_column($selected_column2, $array_ref2);
my $progress_step = int($#array1/20); my $progress_counter=0;
while ($counter1<=$#array1) {
$counter2=1;
my $ID_A = $sel_column_array1[$counter1]; #print STDERR join("\t",$counter1,$ID_A);
while ($counter2<=$#array2) {
my $ID_B = $sel_column_array2[$counter2];
#print STDERR "\t\t",join("\t",$counter2,$ID_B),"\n";
if ("$ID_A" eq "$ID_B") {
push(@match_array1, $array1[$counter1]);
push(@match_array2, $array2[$counter2]);
splice (@array1, $counter1, 1, $empty_string_array1);
splice (@array2, $counter2, 1, $empty_string_array2);
last;
}
$counter2 += 1;
}
#print STDERR "\n";
$counter1 += 1;
$progress_counter++;
if($progress_counter==$progress_step) {print STDERR "."; $progress_counter=0;}
}
my (@rest_array1,@rest_array2);
foreach my $element (@array1) {
if ($element) {push(@rest_array1,$element);}
}
foreach my $element (@array2) {
if ($element) {push(@rest_array2,$element);}
}
return (\@array1, \@array2,\@match_array1,\@match_array2,\@rest_array1,\@rest_array2);
}
#-------------------------------------------------------------
#------------------ join 2 arrays of the same size -----------
sub join_arrays {
my ($array_ref1, $array_ref2) = @_;
my @array1 = @{$array_ref1};
my @array2 = @{$array_ref2};
my @array;
for (my $i=0; $i <= $#array1; $i++) {
push (@array, "$array1[$i]\t\t$array2[$i]");
}
return(@array);
}
#-------------------------------------------------------------
#------------------ Read specified column --------------------
sub Read_column {
my ($column_number, $array_ref) = @_;
my @array = @{$array_ref};
my (@column, @string);
# read column of interest to the memory
for(my $j=0; $j <= $#array ; $j++ )
{
push (@string, split("\t",$array[$j]));
if ($column_number > $#string) {push (@column,undef);}
else {push (@column, $string[$column_number]);}
undef @string;
}
return (@column);
}
#-------------------------------------------------------------
#------------------ Read specified string --------------------
sub Read_string
{
my ($string_number, $array_ref) = @_;
my @array = @{$array_ref};
push (my @string, split ("\t",$array[$string_number]));
return (@string);
}
#-------------------------------------------------------------
#------------------ clean line endings -----------------------
sub clean {
my $text = shift;
$text =~ s/\r//g;
$text =~ s/\n//g;
return $text;
}
#--------------------------------------------------------------------------
# Check for problem with the options or if user requests help
sub check_opts {
if ($needsHelp) {
pod2usage( -verbose => 2 );
}
if ( !$options_okay ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Error specifying options."
);
}
if ( ! -e $path_tab1 ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Cannot find input file $path_tab1: $!\n"
);
}
if ( ! -e $path_tab2 ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Cannot find input file $path_tab2: $!\n"
);
}
if (!$path2output ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "please specify output file name\n"
);
}
}