-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsv_join.pl
56 lines (47 loc) · 1.34 KB
/
tsv_join.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
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
my $tsv_root_file = shift @ARGV;
my (%table_root, $col_root);
open( my $TSV_ROOT, "<", $tsv_root_file );
while ( my $line = <$TSV_ROOT> ) {
chomp $line;
my @tbl = split /\s+/, $line;
my $name = $tbl[0];
$col_root = $#tbl;
$table_root{$name} = join( "\t", @tbl[ 1 .. $col_root ] );
}
close($TSV_ROOT);
foreach my $file (@ARGV) {
open( my $TSV_ADD, "<", $file );
my %table_add;
my $col_add;
while ( my $line = <$TSV_ADD> ) {
chomp $line;
my @tbl = split /\s+/, $line;
my $name = $tbl[0];
$col_add = $#tbl;
$table_add{$name} = join( "\t", @tbl[ 1 .. $col_add ] );
}
close($TSV_ADD);
my %count;
@count{ keys %table_root, keys %table_add } = ();
foreach my $key ( keys %count ) {
if ( exists $table_root{$key} && exists $table_add{$key} ) {
$table_root{$key} .= "\t$table_add{$key}";
}
elsif ( exists $table_add{$key} ) {
$table_root{$key} = "NA\t" x $col_root;
$table_root{$key} .= $table_add{$key};
}
elsif ( exists $table_root{$key} ) {
$table_root{$key} .= "\tNA" x $col_add;
}
}
$col_root += $col_add;
}
foreach my $key ( sort keys %table_root ) {
print("$key\t$table_root{$key}\n");
}
__END__