-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_two_tables_by_column_value.pl
335 lines (293 loc) · 10.4 KB
/
merge_two_tables_by_column_value.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env perl
# Merges (takes union of) two tables by the values in the specified columns.
# Usage:
# perl merge_two_tables_by_column_value.pl [table1 file path]
# [table1 column number (0-indexed)] [table2 file path] [table2 column number (0-indexed)]
# Prints to console. To print to file, use
# perl merge_two_tables_by_column_value.pl [table1 file path]
# [table1 column number (0-indexed)] [table2 file path] [table2 column number (0-indexed)]
# > [output table path]
use strict;
use warnings;
my $table_1 = $ARGV[0]; # file path of tab-separated table
my $table_1_column_to_merge_by = $ARGV[1]; # table 1 column number to merge by (0-indexed)
my $table_2 = $ARGV[2]; # file path of tab-separated table
my $table_2_column_to_merge_by = $ARGV[3]; # table 2 column number to merge by (0-indexed)
my $NEWLINE = "\n";
my $DELIMITER = "\t";
my $NO_DATA = "";
# verifies that input tables exist and are non-empty
if(!$table_1 or !$table_2)
{
print STDERR "Error: two input tables not provided. Exiting.\n";
die;
}
if(!-e $table_1)
{
print STDERR "Error: input table file does not exist:\n\t".$table_1."\nExiting.\n";
die;
}
if(!-e $table_2)
{
print STDERR "Error: input table file does not exist:\n\t".$table_2."\nExiting.\n";
die;
}
if(-z $table_1)
{
print STDERR "Error: input table file is empty:\n\t".$table_1."\nExiting.\n";
die;
}
if(-z $table_2)
{
print STDERR "Error: input table file is empty:\n\t".$table_2."\nExiting.\n";
die;
}
# verifies that column numbers are non-negative value
if($table_1_column_to_merge_by < 0 or $table_2_column_to_merge_by < 0)
{
print STDERR "Error: negative column number. Exiting.\n";
die;
}
# verifies that column numbers are not strings
# (needs to be redone)
$table_1_column_to_merge_by/5;
$table_2_column_to_merge_by/5;
# reads in table 1
my $table_1_column_to_merge_by_title = "";
my $table_1_column_titles = "";
my %column_to_merge_by_values = (); # key: value in column to merge by in either table -> value: 1
my $table_1_number_columns = 0; # number columns in table 1
my %column_to_merge_by_value_to_table_1_line = (); # key: value in column to merge by in table 1 -> value: corresponding line in table 1
my $first_line = 1;
open TABLE_1, "<$table_1" || die "Could not open $table_1 to read; terminating =(\n";
while(<TABLE_1>) # for each row in the file
{
chomp;
if($_ =~ /\S/) # if row not empty
{
my $line = $_;
if($line =~ /^(.*[^\t])\t+$/) # strips any trailing tabs
{
$line = $1;
}
my @items_in_line = split($DELIMITER, $line, -1);
if($first_line) # column titles
{
$table_1_number_columns = scalar @items_in_line;
if($table_1_column_to_merge_by >= $table_1_number_columns)
{
print STDERR "Error: table 1 does not contain enough columns to retrieve column "
.$table_1_column_to_merge_by.":\n\t".$table_1."\nExiting.\n";
die;
}
$table_1_column_titles = $line;
$table_1_column_to_merge_by_title = $items_in_line[$table_1_column_to_merge_by];
$first_line = 0; # next line is not column titles
}
else # column values
{
my $column_to_merge_by_value = $items_in_line[$table_1_column_to_merge_by];
if(defined $column_to_merge_by_value)
{
if($column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value})
{
print STDERR "Warning: value ".$column_to_merge_by_value." appears more than "
."once in table 1. Merging rows.\n";
if($column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value} ne $line)
{
print STDERR "(Corresponding rows are NOT identical.)\n";
}
}
$column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value}
= merge_values_to_print($column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value}, $line);
$column_to_merge_by_values{$column_to_merge_by_value} = 1;
}
}
}
}
close TABLE_1;
# reads in table 2
my $table_2_column_to_merge_by_title = "";
my $table_2_column_titles = "";
my $table_2_number_columns = 0; # number columns in table 2
my %column_to_merge_by_value_to_table_2_line = (); # key: value in column to merge by in table 1 -> value: corresponding line in table 2
$first_line = 1;
open TABLE_2, "<$table_2" || die "Could not open $table_2 to read; terminating =(\n";
while(<TABLE_2>) # for each row in the file
{
chomp;
if($_ =~ /\S/) # if row not empty
{
my $line = $_;
if($line =~ /^(.*[^\t])\t+$/) # strips any trailing tabs
{
$line = $1;
}
my @items_in_line = split($DELIMITER, $line, -1);
if($first_line) # column titles
{
$table_2_number_columns = scalar @items_in_line;
if($table_2_column_to_merge_by >= $table_2_number_columns)
{
print STDERR "Error: table 2 does not contain enough columns to retrieve column "
.$table_2_column_to_merge_by.":\n\t".$table_2."\nExiting.\n";
die;
}
$table_2_column_titles = $line;
$table_2_column_to_merge_by_title = $items_in_line[$table_2_column_to_merge_by];
$first_line = 0; # next line is not column titles
}
else # column values
{
my $column_to_merge_by_value = $items_in_line[$table_2_column_to_merge_by];
if($column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value})
{
print STDERR "Warning: value ".$column_to_merge_by_value." appears more than "
."once in table 2. Merging rows.\n";
if($column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value} ne $line)
{
print STDERR "(Corresponding rows are NOT identical.)\n";
}
}
$column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value}
= merge_values_to_print($column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value}, $line);
$column_to_merge_by_values{$column_to_merge_by_value} = 1;
}
}
}
close TABLE_2;
# prints column titles
print $table_1_column_to_merge_by_title."/".$table_2_column_to_merge_by_title.$DELIMITER; # new column with column to merge by
print $table_1_column_titles.$DELIMITER; # all table1 values
print $table_2_column_titles.$NEWLINE; # all table2 values
# prints merged table with columns from both tables
my $no_data_to_print = $NO_DATA.$DELIMITER;
foreach my $column_to_merge_by_value(sort keys %column_to_merge_by_values)
{
# prints value merged by
print $column_to_merge_by_value;
print $DELIMITER;
# prints table 1 values
my $number_columns_printed = 0;
if($column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value})
{
print $column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value};
print $DELIMITER;
# prepares to print any additional spacing needed
my @items_in_line = split($DELIMITER, $column_to_merge_by_value_to_table_1_line{$column_to_merge_by_value});
$number_columns_printed = scalar @items_in_line;
}
# prints any additional spacing needed
if($number_columns_printed < $table_1_number_columns)
{
print $no_data_to_print x ($table_1_number_columns - $number_columns_printed);
}
elsif($number_columns_printed > $table_1_number_columns)
{
print STDERR "Error: too many columns printed from table 1 for value "
.$column_to_merge_by_value."; ".$number_columns_printed." > ".$table_1_number_columns."\n";
}
# prints table 2 values
$number_columns_printed = 0;
if($column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value})
{
print $column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value};
# prepares to print any additional spacing needed
my @items_in_line = split($DELIMITER, $column_to_merge_by_value_to_table_2_line{$column_to_merge_by_value});
$number_columns_printed = scalar @items_in_line;
}
# prints any additional spacing needed
if($number_columns_printed < $table_2_number_columns)
{
print $no_data_to_print x ($table_2_number_columns - $number_columns_printed);
}
elsif($number_columns_printed > $table_2_number_columns)
{
print STDERR "Error: too many columns printed from table 2 for value "
.$column_to_merge_by_value."; ".$number_columns_printed." > ".$table_2_number_columns."\n";
}
print $NEWLINE;
}
# merges two rows into one row
# for each column, adds both values if they are different, present value if one is absent,
# or nothing if neither has a value
sub merge_values_to_print
{
my $to_print_1 = $_[0];
my $to_print_2 = $_[1];
# if both empty, returns
if(!defined $to_print_1 and !defined $to_print_2)
{
return "";
}
# defines empty strings
if(!defined $to_print_1)
{
$to_print_1 = "";
}
if(!defined $to_print_2)
{
$to_print_2 = "";
}
# splits values to print into their component parts
my @to_print_1_items = split($DELIMITER, $to_print_1, -1);
my @to_print_2_items = split($DELIMITER, $to_print_2, -1);
# if(scalar @to_print_1_items != scalar @to_print_2_items)
# {
# print STDERR "Error: output row chunks with duplicate values to merge by contain "
# ."unequal numbers of columns (".(scalar @to_print_1_items)." and "
# .(scalar @to_print_2_items)."). Cannot merge properly:\n"
# .$to_print_1."\n".$to_print_2."\n";
# }
# merges values
my @to_print = ();
for my $index(0..max($#to_print_1_items, $#to_print_2_items))
{
my $to_print_1_item = $to_print_1_items[$index];
my $to_print_2_item = $to_print_2_items[$index];
# adds merged value
if(!length($to_print_1_item) and !length($to_print_2_item)) # both items absent
{
push(@to_print, "");
}
elsif(length($to_print_1_item) and !length($to_print_2_item)) # item 1 is present, item 2 is empty string
{
push(@to_print, $to_print_1_item);
}
elsif(length($to_print_2_item) and !length($to_print_1_item)) # item 2 is present, item 1 is empty string
{
push(@to_print, $to_print_2_item);
}
elsif(length($to_print_1_item) and length($to_print_2_item) and $to_print_1_item eq $to_print_2_item) # both items present and same value in both items
{
push(@to_print, $to_print_1_item);
}
elsif(length($to_print_1_item) and length($to_print_2_item) and $to_print_1_item ne $to_print_2_item) # both items present and different
{
push(@to_print, $to_print_1_item.", ".$to_print_2_item);
}
else # something else?
{
print STDERR "Error: unexpected possibility reached. Please check and fix code.\n";
push(@to_print, $to_print_1_item.", ".$to_print_2_item);
}
}
return join($DELIMITER, @to_print);
}
# returns the maximum of two values
sub max
{
my $value_1 = $_[0];
my $value_2 = $_[1];
if($value_1 >= $value_2)
{
return $value_1;
}
if($value_2 > $value_1)
{
return $value_2;
}
print STDERR "Error: unexpected possibility reached. Please check and fix code.\n";
return $value_2;
}
# August 4, 2021