line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bio::ConnectDots::Util; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Utility functions for ConnectDots |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
49157
|
use Exporter(); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
104
|
|
6
|
4
|
|
|
4
|
|
23
|
use Scalar::Util qw(blessed); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
14409
|
|
7
|
|
|
|
|
|
|
@ISA=qw(Exporter); |
8
|
|
|
|
|
|
|
@EXPORT=qw(&blessed |
9
|
|
|
|
|
|
|
&joindef &value_as_string &is_number &is_alpha |
10
|
|
|
|
|
|
|
&min &max &minmax &mina &maxa &minmaxa &minb &maxb &minmaxb |
11
|
|
|
|
|
|
|
&avg &mean &sum &eq_list &uniq); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub joindef { |
14
|
1
|
|
|
1
|
0
|
10
|
my $join=shift @_; |
15
|
1
|
|
|
|
|
2
|
join($join,grep {defined $_} @_); |
|
4
|
|
|
|
|
10
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub value_as_string { |
19
|
9
|
|
|
9
|
0
|
1935
|
my($value)=@_; |
20
|
9
|
|
|
|
|
8
|
my $result; |
21
|
9
|
100
|
|
|
|
21
|
if (!ref $value) { |
|
|
100
|
|
|
|
|
|
22
|
7
|
|
|
|
|
8
|
$result=$value; |
23
|
|
|
|
|
|
|
} elsif ('ARRAY' eq ref $value) { |
24
|
1
|
|
|
|
|
2
|
$result='['.join(', ',map {value_as_string($_)} @$value).']'; |
|
4
|
|
|
|
|
10
|
|
25
|
|
|
|
|
|
|
} else { |
26
|
1
|
|
|
|
|
2
|
my @result; |
27
|
1
|
|
|
|
|
6
|
while(my($key,$val)=each %$value) { |
28
|
2
|
|
|
|
|
7
|
push(@result,"$key=>".value_as_string($val)); |
29
|
|
|
|
|
|
|
} |
30
|
1
|
|
|
|
|
4
|
$result='{'.join(', ',@result).'}'; |
31
|
|
|
|
|
|
|
} |
32
|
9
|
|
|
|
|
24
|
$result; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# pattern copied from Regexp::Common by Damian Conway |
36
|
|
|
|
|
|
|
# change to looks_like_number from Scalar::Util |
37
|
|
|
|
|
|
|
my $pattern='(?:(?:[+-]?)(?:\d+))|(?:(?i)(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))'; |
38
|
|
|
|
|
|
|
sub is_number { |
39
|
2
|
|
|
2
|
0
|
1611
|
my($value)=@_; |
40
|
2
|
|
|
|
|
110
|
return $value=~/$pattern/; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
sub is_alpha { |
43
|
20
|
|
|
20
|
0
|
1127
|
my($value)=@_; |
44
|
20
|
|
|
|
|
213
|
return $value!~/$pattern/; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# can change these to use List::Util |
48
|
|
|
|
|
|
|
# the following do numeric comparisons |
49
|
|
|
|
|
|
|
sub min { |
50
|
1
|
50
|
|
1
|
0
|
547
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
11
|
|
|
1
|
|
|
|
|
5
|
|
51
|
1
|
|
|
|
|
2
|
@_=grep {defined $_} @_; |
|
4
|
|
|
|
|
9
|
|
52
|
1
|
50
|
|
|
|
5
|
return undef unless @_; |
53
|
1
|
0
|
|
|
|
5
|
if ($#_==1) {my($x,$y)=@_; return ($x<=$y?$x:$y);} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
54
|
1
|
|
|
|
|
3
|
my $min=shift @_; |
55
|
1
|
100
|
|
|
|
3
|
map {$min=$_ if $_<$min} @_; |
|
3
|
|
|
|
|
14
|
|
56
|
1
|
|
|
|
|
4
|
$min; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
sub max { |
59
|
1
|
50
|
|
1
|
0
|
1287
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
6
|
|
|
1
|
|
|
|
|
5
|
|
60
|
1
|
50
|
|
|
|
5
|
return undef unless @_; |
61
|
1
|
0
|
|
|
|
5
|
if ($#_==1) {my($x,$y)=@_; return ($x>=$y?$x:$y);} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
62
|
1
|
|
|
|
|
3
|
my $max=shift @_; |
63
|
1
|
100
|
|
|
|
3
|
map {$max=$_ if $_>$max} @_; |
|
3
|
|
|
|
|
14
|
|
64
|
1
|
|
|
|
|
4
|
$max; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
sub minmax { |
67
|
1
|
50
|
|
1
|
0
|
644
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
5
|
|
|
1
|
|
|
|
|
4
|
|
68
|
1
|
50
|
|
|
|
4
|
return undef unless @_; |
69
|
1
|
0
|
|
|
|
8
|
if ($#_==1) {my($x,$y)=@_; return ($x<=$y?($x,$y):($y,$x));} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
70
|
1
|
|
|
|
|
2
|
my $min=shift @_; |
71
|
1
|
|
|
|
|
3
|
my $max=$min; |
72
|
1
|
100
|
|
|
|
2
|
map {if ($_<$min) {$min=$_;} elsif ($_>$max) {$max=$_;}} @_; |
|
3
|
100
|
|
|
|
15
|
|
|
1
|
|
|
|
|
24
|
|
|
1
|
|
|
|
|
4
|
|
73
|
1
|
|
|
|
|
5
|
($min,$max); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
# the following use alpha comparisons |
76
|
|
|
|
|
|
|
sub mina { |
77
|
2
|
0
|
|
2
|
0
|
541
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
78
|
2
|
|
|
|
|
5
|
@_=grep {defined $_} @_; |
|
9
|
|
|
|
|
16
|
|
79
|
2
|
50
|
|
|
|
6
|
return undef unless @_; |
80
|
2
|
0
|
|
|
|
8
|
if ($#_==1) {my($x,$y)=@_; return ($x le $y?$x:$y);} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
81
|
2
|
|
|
|
|
3
|
my $min=shift @_; |
82
|
2
|
100
|
|
|
|
3
|
map {$min=$_ if $_ lt $min} @_; |
|
7
|
|
|
|
|
24
|
|
83
|
2
|
|
|
|
|
8
|
$min; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
sub maxa { |
86
|
2
|
50
|
|
2
|
0
|
514
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
100
|
|
|
|
5
|
|
|
1
|
|
|
|
|
4
|
|
87
|
2
|
50
|
|
|
|
6
|
return undef unless @_; |
88
|
2
|
0
|
|
|
|
6
|
if ($#_==1) {my($x,$y)=@_; return ($x ge $y?$x:$y);} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
89
|
2
|
|
|
|
|
5
|
my $max=shift @_; |
90
|
2
|
100
|
|
|
|
4
|
map {$max=$_ if $_ gt $max} @_; |
|
7
|
|
|
|
|
20
|
|
91
|
2
|
|
|
|
|
8
|
$max; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
sub minmaxa { |
94
|
2
|
0
|
|
2
|
0
|
542
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
95
|
2
|
50
|
|
|
|
7
|
return undef unless @_; |
96
|
2
|
0
|
|
|
|
5
|
if ($#_==1) {my($x,$y)=@_; return ($x le $y?($x,$y):($y,$x));} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
97
|
2
|
|
|
|
|
4
|
my $min=shift @_; |
98
|
2
|
|
|
|
|
5
|
my $max=$min; |
99
|
2
|
100
|
|
|
|
4
|
map {if ($_ lt $min) {$min=$_;} elsif ($_ gt $max) {$max=$_;}} @_; |
|
7
|
100
|
|
|
|
30
|
|
|
2
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
6
|
|
100
|
2
|
|
|
|
|
11
|
($min,$max); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
# the following use numeric or alpha comparisons as appropriate |
103
|
|
|
|
|
|
|
sub maxb { |
104
|
1
|
50
|
|
1
|
0
|
425
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
5
|
|
|
1
|
|
|
|
|
5
|
|
105
|
1
|
50
|
|
|
|
3
|
return maxa(@_) if grep {is_alpha($_)} @_; |
|
6
|
|
|
|
|
9
|
|
106
|
0
|
|
|
|
|
0
|
return max(@_); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
sub minb { |
109
|
1
|
50
|
|
1
|
0
|
415
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
4
|
|
|
1
|
|
|
|
|
5
|
|
110
|
1
|
50
|
|
|
|
2
|
return mina(@_) if grep {is_alpha($_)} @_; |
|
6
|
|
|
|
|
10
|
|
111
|
0
|
|
|
|
|
0
|
return min(@_); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
sub minmaxb { |
114
|
1
|
50
|
|
1
|
0
|
487
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
1
|
50
|
|
|
|
6
|
|
|
1
|
|
|
|
|
6
|
|
115
|
1
|
50
|
|
|
|
3
|
return minmaxa(@_) if grep {is_alpha($_)} @_; |
|
6
|
|
|
|
|
12
|
|
116
|
0
|
|
|
|
|
0
|
return minmax(@_); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub avg { |
120
|
2
|
0
|
|
2
|
0
|
575
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
121
|
2
|
50
|
|
|
|
6
|
return undef unless @_; |
122
|
2
|
50
|
|
|
|
5
|
if ($#_==1) {my($x,$y)=@_; return ($x+$y)/2;} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
123
|
2
|
|
|
|
|
4
|
my $sum; |
124
|
2
|
|
|
|
|
3
|
map {$sum+=$_} @_; |
|
10
|
|
|
|
|
14
|
|
125
|
2
|
|
|
|
|
7
|
$sum/(@_+0); |
126
|
|
|
|
|
|
|
} |
127
|
1
|
|
|
1
|
0
|
566
|
sub mean {avg @_;} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub sum { |
130
|
1
|
0
|
|
1
|
0
|
506
|
if ($#_==0) {@_=@{$_[0]} if 'ARRAY' eq ref $_[0];} |
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
131
|
1
|
50
|
|
|
|
5
|
return undef unless @_; |
132
|
1
|
50
|
|
|
|
4
|
if ($#_==1) {my($x,$y)=@_; return $x+$y;} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
133
|
1
|
|
|
|
|
1
|
my $sum; |
134
|
1
|
|
|
|
|
4
|
map {$sum+=$_} @_; |
|
5
|
|
|
|
|
7
|
|
135
|
1
|
|
|
|
|
3
|
$sum; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# test equality of two lists |
139
|
|
|
|
|
|
|
sub eq_list { |
140
|
3
|
|
|
3
|
0
|
1509
|
my($a,$b)=@_; |
141
|
3
|
50
|
33
|
|
|
20
|
return undef unless 'ARRAY' eq ref $a && 'ARRAY' eq ref $b; |
142
|
3
|
100
|
|
|
|
10
|
return undef unless @$a==@$b; |
143
|
2
|
|
|
|
|
7
|
for(my $i=0;$i<@$a;$i++) { |
144
|
10
|
100
|
|
|
|
37
|
return undef unless $a->[$i] eq $b->[$i]; |
145
|
|
|
|
|
|
|
} |
146
|
1
|
|
|
|
|
2
|
return 1; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# uniquify a list, ie, eliminate duplicates) |
150
|
|
|
|
|
|
|
sub uniq { |
151
|
1
|
|
|
1
|
0
|
408
|
my %hash; |
152
|
1
|
|
|
|
|
2
|
my $output=[]; |
153
|
1
|
50
|
|
|
|
8
|
if ('ARRAY' eq ref $_[0]) { |
154
|
0
|
|
|
|
|
0
|
my($input)=@_; |
155
|
0
|
|
|
|
|
0
|
@hash{@$input}=@$input; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
else { |
158
|
1
|
|
|
|
|
11
|
@hash{@_}=@_; |
159
|
|
|
|
|
|
|
} |
160
|
1
|
|
|
|
|
5
|
@$output=values(%hash); |
161
|
1
|
50
|
|
|
|
6
|
wantarray? @$output: $output; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |