File Coverage

Bio/PhyloNetwork/muVector.pm
Criterion Covered Total %
statement 85 87 97.7
branch 17 24 70.8
condition n/a
subroutine 15 16 93.7
pod 10 12 83.3
total 127 139 91.3


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   417 use strict;
  6         6  
  6         134  
60 6     6   18 use warnings;
  6         6  
  6         126  
61              
62 6     6   19 use base qw(Bio::Root::Root);
  6         7  
  6         1232  
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 20143     20143 1 16281 my ($pkg,$cont)=@_;
81 20143         24495 my $self=$pkg->SUPER::new();
82 20143         15469 my @arr=();
83 20143 100       21409 if (!ref($cont)) {
84             #$cont is a number; initialize to a zero-vector
85 10148         13440 for (my $i=0; $i < $cont; $i++) {
86 30754         40287 $arr[$i]=0;
87             }
88 10148         13906 $self->{arr}=\@arr;
89             } else {
90             #$cont points to an array
91 9995         5992 @arr=@{$cont};
  9995         12427  
92             }
93 20143         20338 $self->{dim}=scalar @arr;
94 20143         16204 $self->{arr}=\@arr;
95 20143         16458 bless($self,$pkg);
96 20143         20700 return $self;
97             }
98              
99             sub dim {
100             return shift->{dim}
101 0     0 0 0 }
102              
103             use overload
104 6         45 "+" => \&add,
105             "-" => \&substract,
106             "*" => \&scalarproduct,
107             "<=>" => \&comparelex,
108             "cmp" => \&comparelex,
109             '""' => \&display,
110 6     6   25 '@{}' => \&as_array;
  6         7  
111              
112             sub as_array {
113 66456     66456 0 94459 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 3866     3866 1 2681 my ($self)=@_;
130 3866         2404 my @arr=@{$self->{arr}};
  3866         5090  
131 3866         10798 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 9973     9973 1 8396 my ($v1,$v2)=@_;
148              
149 9973 50       15066 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
150 9973         8274 my $dim=$v1->{dim};
151 9973         7897 my @sum=();
152 9973         13560 for (my $i=0; $i<$dim; $i++) {
153 30213         28649 $sum[$i]=$v1->[$i]+$v2->[$i];
154             }
155 9973         12286 my $result=Bio::PhyloNetwork::muVector->new(\@sum);
156 9973         18046 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 11 my ($v1,$v2)=@_;
173              
174 15 50       24 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
175 15         15 my $dim=$v1->{dim};
176 15         15 my @sum=();
177 15         21 for (my $i=0; $i<$dim; $i++) {
178 60         91 $sum[$i]=$v1->{arr}->[$i]-$v2->{arr}->[$i];
179             }
180 15         18 my $result=Bio::PhyloNetwork::muVector->new(\@sum);
181 15         22 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 515 my ($v1,$num,$swapped)=@_;
198              
199 1         2 my $dim=$v1->{dim};
200 1         1 my @sum=();
201 1         5 for (my $i=0; $i<$dim; $i++) {
202 4         8 $sum[$i]=$num*$v1->{arr}->[$i];
203             }
204 1         3 my $result=Bio::PhyloNetwork::muVector->new(\@sum);
205 1         3 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 169196     169196 1 116381 my ($v1,$v2)=@_;
223              
224 169196 50       224458 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
225 169196         113143 my $dim=$v1->{dim};
226 169196         199965 for (my $i=0; $i<$dim; $i++) {
227 279208 100       513833 return -1 if $v1->{arr}->[$i] < $v2->{arr}->[$i];
228 198291 100       447800 return 1 if $v1->{arr}->[$i] > $v2->{arr}->[$i];
229             }
230 21417         50807 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 68 my ($v1,$v2)=@_;
245              
246 80 50       110 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
247 80         58 my $dim=$v1->{dim};
248 80         99 for (my $i=0; $i<$dim; $i++) {
249 219 100       180 return 0 unless $v1->[$i] >= $v2->[$i];
250             }
251 16         26 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 60 my ($v1)=@_;
266              
267 92         62 my $dim=$v1->{dim};
268 92         118 for (my $i=0; $i<$dim; $i++) {
269 368 50       300 return 0 unless $v1->[$i] >= 0;
270             }
271 92         277 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 38 my ($v1,$v2)=@_;
286              
287 45 50       79 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
288 45         32 my $dim=$v1->{dim};
289 45         27 my $w=0;
290 45         71 for (my $i=0; $i<$dim; $i++) {
291 180 100       174 $w++ unless $v1->[$i] == $v2->[$i];
292             }
293 45         55 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 38 my ($v1,$v2)=@_;
308              
309 45 50       74 $v1->throw("Vectors not the same size") unless ($v1->{dim} == $v2->{dim});
310 45         33 my $dim=$v1->{dim};
311 45         32 my $w=0;
312 45         64 for (my $i=0; $i<$dim; $i++) {
313 180         168 $w+= abs($v1->[$i] - $v2->[$i]);
314             }
315 45         51 return $w;
316             }
317              
318             1;