line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Module for Bio::PhyloNetwork::muVector |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Please direct questions and support issues to |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Cared for by Gabriel Cardona |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# Copyright Gabriel Cardona |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# You may distribute this module under the same terms as perl itself |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# POD documentation - main docs before the code |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Bio::PhyloNetwork::muVector - Module to compute with vectors of arbitrary |
17
|
|
|
|
|
|
|
dimension |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use strict; |
22
|
|
|
|
|
|
|
use warnings; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Bio::PhyloNetwork::muVector; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $vec1=Bio::PhyloNetwork::muVector->new(4); |
27
|
|
|
|
|
|
|
my $vec2=Bio::PhyloNetwork::muVector->new([1,2,3,4]); |
28
|
|
|
|
|
|
|
my $vec3=Bio::PhyloNetwork::muVector->new([10,20,30,40]); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $vec4=$vec3-10*$vec2; |
31
|
|
|
|
|
|
|
if (($vec4 cmp $vec1) == 0) { |
32
|
|
|
|
|
|
|
print "$vec4 is zero\n"; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $vec5=Bio::PhyloNetwork::muVector->new([8,2,2,4]); |
36
|
|
|
|
|
|
|
my $vec6=Bio::PhyloNetwork::muVector->new([1,2,3,4]); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
print "Test poset $vec5 > $vec6: ".$vec5->geq_poset($vec6)."\n"; |
39
|
|
|
|
|
|
|
print "Test lex $vec5 > $vec6: ".($vec5 cmp $vec6)."\n"; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
This is a module to work with vectors. It creates |
44
|
|
|
|
|
|
|
vectors of arbitrary length, defines its basic arithmetic operations, |
45
|
|
|
|
|
|
|
its lexicographic ordering and the natural structure of poset. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 AUTHOR |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 APPENDIX |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
The rest of the documentation details each of the object methods. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
package Bio::PhyloNetwork::muVector; |
58
|
|
|
|
|
|
|
|
59
|
6
|
|
|
6
|
|
642
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
190
|
|
60
|
6
|
|
|
6
|
|
29
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
181
|
|
61
|
|
|
|
|
|
|
|
62
|
6
|
|
|
6
|
|
30
|
use base qw(Bio::Root::Root); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
1680
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 new |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Title : new |
67
|
|
|
|
|
|
|
Usage : my $mu = new Bio::PhyloNetwork::muVector(); |
68
|
|
|
|
|
|
|
Function: Creates a new Bio::PhyloNetwork::muVector object |
69
|
|
|
|
|
|
|
Returns : Bio::PhyloNetwork::muVector |
70
|
|
|
|
|
|
|
Args : integer or (reference to) an array |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
If given an integer as argument, returns a Bio::PhyloNetwork::muVector |
73
|
|
|
|
|
|
|
object with dimension the integer given and initialized to zero. |
74
|
|
|
|
|
|
|
If it is an anonimous array, then the vector is initialized with the values |
75
|
|
|
|
|
|
|
in the array and with the corresponding dimension. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub new { |
80
|
19805
|
|
|
19805
|
1
|
29461
|
my ($pkg,$cont)=@_; |
81
|
19805
|
|
|
|
|
36206
|
my $self=$pkg->SUPER::new(); |
82
|
19805
|
|
|
|
|
22992
|
my @arr=(); |
83
|
19805
|
100
|
|
|
|
29943
|
if (!ref($cont)) { |
84
|
|
|
|
|
|
|
#$cont is a number; initialize to a zero-vector |
85
|
9975
|
|
|
|
|
17271
|
for (my $i=0; $i < $cont; $i++) { |
86
|
30235
|
|
|
|
|
47284
|
$arr[$i]=0; |
87
|
|
|
|
|
|
|
} |
88
|
9975
|
|
|
|
|
21001
|
$self->{arr}=\@arr; |
89
|
|
|
|
|
|
|
} else { |
90
|
|
|
|
|
|
|
#$cont points to an array |
91
|
9830
|
|
|
|
|
11034
|
@arr=@{$cont}; |
|
9830
|
|
|
|
|
17506
|
|
92
|
|
|
|
|
|
|
} |
93
|
19805
|
|
|
|
|
31701
|
$self->{dim}=scalar @arr; |
94
|
19805
|
|
|
|
|
26227
|
$self->{arr}=\@arr; |
95
|
19805
|
|
|
|
|
23178
|
bless($self,$pkg); |
96
|
19805
|
|
|
|
|
30709
|
return $self; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub dim { |
100
|
|
|
|
|
|
|
return shift->{dim} |
101
|
0
|
|
|
0
|
0
|
0
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
use overload |
104
|
6
|
|
|
|
|
97
|
"+" => \&add, |
105
|
|
|
|
|
|
|
"-" => \&substract, |
106
|
|
|
|
|
|
|
"*" => \&scalarproduct, |
107
|
|
|
|
|
|
|
"<=>" => \&comparelex, |
108
|
|
|
|
|
|
|
"cmp" => \&comparelex, |
109
|
|
|
|
|
|
|
'""' => \&display, |
110
|
6
|
|
|
6
|
|
51
|
'@{}' => \&as_array; |
|
6
|
|
|
|
|
13
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub as_array { |
113
|
65385
|
|
|
65385
|
0
|
127466
|
return shift->{arr}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 display |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Title : display |
119
|
|
|
|
|
|
|
Usage : my $str=$mu->display() |
120
|
|
|
|
|
|
|
Function: returns an string displaying its contents |
121
|
|
|
|
|
|
|
Returns : string |
122
|
|
|
|
|
|
|
Args : none |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This function is also overloaded to the "" operator. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub display { |
129
|
3812
|
|
|
3812
|
1
|
5120
|
my ($self)=@_; |
130
|
3812
|
|
|
|
|
4049
|
my @arr=@{$self->{arr}}; |
|
3812
|
|
|
|
|
6254
|
|
131
|
3812
|
|
|
|
|
14844
|
return "(@arr)"; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 add |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Title : add |
137
|
|
|
|
|
|
|
Usage : $mu->add($mu2) |
138
|
|
|
|
|
|
|
Function: returns the sum of $mu and $mu2 |
139
|
|
|
|
|
|
|
Returns : Bio::PhyloNetwork::muVector |
140
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This function is also overloaded to the + operator. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub add { |
147
|
9808
|
|
|
9808
|
1
|
16270
|
my ($v1,$v2)=@_; |
148
|
|
|
|
|
|
|
|
149
|
9808
|
50
|
|
|
|
20634
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
150
|
9808
|
|
|
|
|
12596
|
my $dim=$v1->{dim}; |
151
|
9808
|
|
|
|
|
11899
|
my @sum=(); |
152
|
9808
|
|
|
|
|
18038
|
for (my $i=0; $i<$dim; $i++) { |
153
|
29718
|
|
|
|
|
47346
|
$sum[$i]=$v1->[$i]+$v2->[$i]; |
154
|
|
|
|
|
|
|
} |
155
|
9808
|
|
|
|
|
18766
|
my $result=Bio::PhyloNetwork::muVector->new(\@sum); |
156
|
9808
|
|
|
|
|
26179
|
return $result; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 substract |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Title : substract |
162
|
|
|
|
|
|
|
Usage : $mu->substract($mu2) |
163
|
|
|
|
|
|
|
Function: returns the difference of $mu and $mu2 |
164
|
|
|
|
|
|
|
Returns : Bio::PhyloNetwork::muVector |
165
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This function is also overloaded to the - operator. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub substract { |
172
|
15
|
|
|
15
|
1
|
45
|
my ($v1,$v2)=@_; |
173
|
|
|
|
|
|
|
|
174
|
15
|
50
|
|
|
|
55
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
175
|
15
|
|
|
|
|
38
|
my $dim=$v1->{dim}; |
176
|
15
|
|
|
|
|
34
|
my @sum=(); |
177
|
15
|
|
|
|
|
43
|
for (my $i=0; $i<$dim; $i++) { |
178
|
60
|
|
|
|
|
203
|
$sum[$i]=$v1->{arr}->[$i]-$v2->{arr}->[$i]; |
179
|
|
|
|
|
|
|
} |
180
|
15
|
|
|
|
|
59
|
my $result=Bio::PhyloNetwork::muVector->new(\@sum); |
181
|
15
|
|
|
|
|
72
|
return $result; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 scalarproduct |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Title : scalarproduct |
187
|
|
|
|
|
|
|
Usage : $mu->scalarproduct($ct) |
188
|
|
|
|
|
|
|
Function: returns the scalar product of $ct and $mu |
189
|
|
|
|
|
|
|
Returns : Bio::PhyloNetwork::muVector |
190
|
|
|
|
|
|
|
Args : scalar |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This function is also overloaded to the * operator. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub scalarproduct { |
197
|
1
|
|
|
1
|
1
|
2134
|
my ($v1,$num,$swapped)=@_; |
198
|
|
|
|
|
|
|
|
199
|
1
|
|
|
|
|
5
|
my $dim=$v1->{dim}; |
200
|
1
|
|
|
|
|
3
|
my @sum=(); |
201
|
1
|
|
|
|
|
8
|
for (my $i=0; $i<$dim; $i++) { |
202
|
4
|
|
|
|
|
16
|
$sum[$i]=$num*$v1->{arr}->[$i]; |
203
|
|
|
|
|
|
|
} |
204
|
1
|
|
|
|
|
9
|
my $result=Bio::PhyloNetwork::muVector->new(\@sum); |
205
|
1
|
|
|
|
|
7
|
return $result; |
206
|
0
|
|
|
|
|
0
|
return $result; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 comparelex |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Title : comparelex |
212
|
|
|
|
|
|
|
Usage : $mu1->comparelex($mu2) |
213
|
|
|
|
|
|
|
Function: compares $mu and $mu2 w.r.t. the lexicographic ordering |
214
|
|
|
|
|
|
|
Returns : scalar (-1 if $mu1<$mu2, 0 if $mu1=$mu2, 1 if $mu1>$mu2) |
215
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This function is also overloaded to the E=E and cmp operator. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub comparelex { |
222
|
170278
|
|
|
170278
|
1
|
220873
|
my ($v1,$v2)=@_; |
223
|
|
|
|
|
|
|
|
224
|
170278
|
50
|
|
|
|
261537
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
225
|
170278
|
|
|
|
|
176509
|
my $dim=$v1->{dim}; |
226
|
170278
|
|
|
|
|
233586
|
for (my $i=0; $i<$dim; $i++) { |
227
|
280628
|
100
|
|
|
|
565573
|
return -1 if $v1->{arr}->[$i] < $v2->{arr}->[$i]; |
228
|
199470
|
100
|
|
|
|
455329
|
return 1 if $v1->{arr}->[$i] > $v2->{arr}->[$i]; |
229
|
|
|
|
|
|
|
} |
230
|
21416
|
|
|
|
|
50008
|
return 0; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 geq_poset |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Title : geq_poset |
236
|
|
|
|
|
|
|
Usage : $mu1->geq_poset($mu2) |
237
|
|
|
|
|
|
|
Function: compares $mu and $mu2 w.r.t. the natural partial ordering |
238
|
|
|
|
|
|
|
Returns : boolean (1 if $mu >= $mu2, 0 otherwise) |
239
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub geq_poset { |
244
|
80
|
|
|
80
|
1
|
198
|
my ($v1,$v2)=@_; |
245
|
|
|
|
|
|
|
|
246
|
80
|
50
|
|
|
|
267
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
247
|
80
|
|
|
|
|
183
|
my $dim=$v1->{dim}; |
248
|
80
|
|
|
|
|
218
|
for (my $i=0; $i<$dim; $i++) { |
249
|
219
|
100
|
|
|
|
616
|
return 0 unless $v1->[$i] >= $v2->[$i]; |
250
|
|
|
|
|
|
|
} |
251
|
16
|
|
|
|
|
70
|
return 1; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head2 is_positive |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Title : is_positive |
257
|
|
|
|
|
|
|
Usage : $mu->is_positive() |
258
|
|
|
|
|
|
|
Function: tests if all components of $mu are positive (or zero) |
259
|
|
|
|
|
|
|
Returns : boolean |
260
|
|
|
|
|
|
|
Args : none |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=cut |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
sub is_positive { |
265
|
92
|
|
|
92
|
1
|
212
|
my ($v1)=@_; |
266
|
|
|
|
|
|
|
|
267
|
92
|
|
|
|
|
214
|
my $dim=$v1->{dim}; |
268
|
92
|
|
|
|
|
264
|
for (my $i=0; $i<$dim; $i++) { |
269
|
368
|
50
|
|
|
|
1026
|
return 0 unless $v1->[$i] >= 0; |
270
|
|
|
|
|
|
|
} |
271
|
92
|
|
|
|
|
550
|
return 1; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head2 hamming |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
Title : hamming |
277
|
|
|
|
|
|
|
Usage : $mu1->hamming($mu2) |
278
|
|
|
|
|
|
|
Function: returns the Hamming distance between $mu1 and $mu2 |
279
|
|
|
|
|
|
|
Returns : scalar |
280
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=cut |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub hamming { |
285
|
45
|
|
|
45
|
1
|
69
|
my ($v1,$v2)=@_; |
286
|
|
|
|
|
|
|
|
287
|
45
|
50
|
|
|
|
122
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
288
|
45
|
|
|
|
|
65
|
my $dim=$v1->{dim}; |
289
|
45
|
|
|
|
|
52
|
my $w=0; |
290
|
45
|
|
|
|
|
82
|
for (my $i=0; $i<$dim; $i++) { |
291
|
180
|
100
|
|
|
|
333
|
$w++ unless $v1->[$i] == $v2->[$i]; |
292
|
|
|
|
|
|
|
} |
293
|
45
|
|
|
|
|
97
|
return $w; |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head2 manhattan |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
Title : manhattan |
299
|
|
|
|
|
|
|
Usage : $mu1->manhattan($mu2) |
300
|
|
|
|
|
|
|
Function: returns the Manhattan distance between $mu1 and $mu2 |
301
|
|
|
|
|
|
|
Returns : scalar |
302
|
|
|
|
|
|
|
Args : Bio::PhyloNetwork::muVector |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=cut |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
sub manhattan { |
307
|
45
|
|
|
45
|
1
|
53
|
my ($v1,$v2)=@_; |
308
|
|
|
|
|
|
|
|
309
|
45
|
50
|
|
|
|
90
|
$v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim}); |
310
|
45
|
|
|
|
|
49
|
my $dim=$v1->{dim}; |
311
|
45
|
|
|
|
|
44
|
my $w=0; |
312
|
45
|
|
|
|
|
57
|
for (my $i=0; $i<$dim; $i++) { |
313
|
180
|
|
|
|
|
260
|
$w+= abs($v1->[$i] - $v2->[$i]); |
314
|
|
|
|
|
|
|
} |
315
|
45
|
|
|
|
|
71
|
return $w; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
1; |