-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtblfromatrix
executable file
·57 lines (50 loc) · 1.09 KB
/
tblfromatrix
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
#!/usr/bin/env perl
use strict;
use warnings;
use locale;
use Getopt::Std;
# arguments
my %flags;
getopts("sth", \%flags);
my $SEP = ($ENV{'TBLSEP'} || "\t");
my ($file) = @ARGV;
if(!$file || defined($flags{'h'}))
{
print(STDERR qq{$0 [-sth] file:
Write a three-column associative table starting from a matrix.
You can change the column separator by setting the TBLSEP environment variable.
The following command line flags are supported:
-s: automatic symmetric elimination
-t: output transposed identifiers
-h: help summary
});
exit(2);
}
# settings
my $transpose = defined($flags{'t'});
my $symmetric = defined($flags{'s'});
# program
open(FD, $file) or die("cannot open $file\n");
my %matrix;
# headers
$_ = <FD>;
s/[\r\n]+$//;
my @names = split($SEP, $_, -1);
@names = splice(@names, 1);
# rows
my @rows;
my %data;
while(<FD>)
{
s/[\r\n]+$//;
my @line = split($SEP, $_, -1);
push(@rows, $line[0]);
$data{$line[0]} = [splice(@line, 1)];
}
# output
for my $i(@rows) {
for my $j(0 .. $#names) {
my $name = $names[$j];
print(join($SEP, ($i, $name, $data{$i}->[$j])) . "\n");
}
}