-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathabc-average.sh
More file actions
executable file
·50 lines (40 loc) · 1.01 KB
/
abc-average.sh
File metadata and controls
executable file
·50 lines (40 loc) · 1.01 KB
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
#!/bin/bash
# This script expects on STDIN a stream with format
# LABEL whitespace LABEL whitespace NUMBER
# it will order each label pair lexicographically and then sort the stream so that labels
# x y
# y x
# are grouped together.
# After this it will take the average of numbers associated with a pair of labels and output
# this again in the same three-column format.
# This script can be used to compute the average edge weight for a network if edges
# are specified in two directions.
set -euo pipefail
perl -ane '$i=$F[0]le$F[1]?0:1;;print "@F[$i,1-$i,2]\n"' | sort | perl <(cat <<'EOF'
use strict;
use warnings;
sub emit_ave {
my ($e, $ar) = @_;
die "! $e [@$ar]\n" unless @$ar;
my $s = 0.0;
$s += $_ for @{$ar};
$s /= @{$ar};
print "$e\t$s\n";
}
my $pe = "";
my @W = ();
while (<>) {
chomp;
my @F = split;
my $i = $F[0] le $F[1] ? 0 : 1;
my $e = "$F[$i]\t$F[1-$i]";
if ($pe && $pe ne $e) {
emit_ave($pe, \@W);
@W = ();
}
push @W, $F[2];
$pe = $e;
}
emit_ave($pe, \@W);
EOF
)