Bio/Matrix/Generic.pm

filter: Statementstmt 67.8% 173 / 255 Branchbran 51.9% 54 / 104 Conditioncond 27.5% 8 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 89.6% 26 / 29 Podpod 96.1% 25 / 26 totaltotal 62.3% 286 / 459 CC 65 SCAR 25.9
CC
65
Cov
61%
new
33.0
row_by_num
20.7
column_by_num
19.7
CRAP 13.3
« Bio/DasI.pm ↑ Summary Bio/DB/Qual.pm »
1 #
2 # BioPerl module for Bio::Matrix::Generic
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
7 #
8 # Copyright Jason Stajich
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::Matrix::Generic - A generic matrix implementation
17  
18 =head1 SYNOPSIS
19  
20 # A matrix has columns and rows
21 my $matrix = Bio::Matrix::Generic->new;
22 $matrix->add_column(1,$column1);
23 $matrix->add_column(2,$column2);
24  
25 my $element = $matrix->entry_by_num(1,2);
26 $matrix->entry_by_num(1,2,$newval);
27  
28 my $entry = $matrix->entry('human', 'mouse');
29  
30 $matrix->entry('human','mouse', $newval);
31  
32  
33 =head1 DESCRIPTION
34  
35 This is a general purpose matrix object for dealing with row+column
36 data which is typical when enumerating all the pairwise combinations
37 and desiring to get slices of the data.
38  
39 Data can be accessed by column and row names or indexes. Matrix
40 indexes start at 0.
41  
42 =head1 FEEDBACK
43  
44 =head2 Mailing Lists
45  
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
49  
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52  
53 =head2 Support
54  
55 Please direct usage questions or support issues to the mailing list:
56  
57 I<bioperl-l@bioperl.org>
58  
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
63  
64 =head2 Reporting Bugs
65  
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
68 web:
69  
70 https://github.com/bioperl/bioperl-live/issues
71  
72 =head1 AUTHOR - Jason Stajich
73  
74 Email jason-at-bioperl-dot-org
75  
76 =head1 APPENDIX
77  
78 The rest of the documentation details each of the object methods.
79 Internal methods are usually preceded with a _
80  
81 =cut
82  
83 package Bio::Matrix::Generic;
84 6 $Bio::Matrix::Generic::VERSION = '1.7.8';
85 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87  
88 6 ▶use base qw(Bio::Root::Root Bio::Matrix::MatrixI);
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 =head2 new
91  
92 Title : new
93 Usage : my $obj = Bio::Matrix::Generic->new();
94 Function: Builds a new Bio::Matrix::Generic object
95 Returns : an instance of Bio::Matrix::Generic
96 Args : -values => arrayref of arrayrefs of data initialization
97 -rownames => arrayref of row names
98 -colnames => arrayref of col names
99 -matrix_id => id of the matrix
100 -matrix_name=> name of the matrix
101 -matrix_init_value => default value to initialize empty cells
102  
103 =cut
104  
105 5 ▶sub new {
Subroutine new: called CC=15 SCAR=35.0
Pod documented
106 5 my($class,@args) = @_;
107  
108 5 my $self = $class->SUPER::new(@args);
109 5 my ($values, $rownames, $colnames,
110 $id,$name,$init_val) =
111 $self->_rearrange([qw(VALUES ROWNAMES COLNAMES
112 MATRIX_ID MATRIX_NAME
113 MATRIX_INIT_VALUE)],@args);
114 5 ▶ $self->matrix_id($id) if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 1 4 5
115 5 ▶ $self->matrix_name($name) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 2 3 5
116 5 ▶ if( defined $rownames && defined $colnames ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rownames and defined $colnames) { } 5 0 5
elsif (not defined $rownames || defined $colnames || defined $values) { } 0 0 0
Condition33%1/3
defined $rownames and defined $colnames
!ll&&!rl&&r
005
MC/DC0%0/2
defined $rownames and defined $colnames
defined $rownames defined $colnames
Truth table33%1/3
A and B
A= defined $rownamesB= defined $colnames
ABresult
0X0
100
111
117 5 ▶ if( ref($rownames) !~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($rownames) =~ /ARRAY/i) 0 5 5
118 0 $self->throw("need an arrayref for the -rownames option");
119 }
120 # insure we copy the values
121 5 $self->{'_rownames'} = [ @$rownames ];
122 5 my $count = 0;
123 5 %{$self->{'_rownamesmap'}} = map { $_ => $count++ } @$rownames;
124  
125 5 ▶ if( ref($colnames) !~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($colnames) =~ /ARRAY/i) 0 5 5
126 0 $self->throw("need an arrayref for the -colnames option");
127 }
128 # insure we copy the values
129 5 $self->{'_colnames'} = [ @$colnames ];
130 5 $count = 0;
131 5 %{$self->{'_colnamesmap'}} = map { $_ => $count++ } @$colnames;
132  
133 5 $self->{'_values'} = [];
134 5 ▶ if( defined $values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values) { } 5 0 5
135 5 ▶ if( ref($values) !~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($values) =~ /ARRAY/i) 0 5 5
136 0 $self->throw("Need an arrayref of arrayrefs (matrix) for -values option");
137 }
138 5 for my $v ( @$values ) {
139 63 ▶ if( ref($v) !~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($v) =~ /ARRAY/i) 0 63 63
140 0 $self->throw("Need and array of arrayrefs (matrix) for -values option");
141 }
142 63 push @{$self->{'_values'}}, [@$v];
143 }
144 } else {
145 0 my @fill = ($init_val) x scalar @$colnames; # undef init_val will be default
146 0 for ( @$rownames ) {
147 0 push @{$self->{'_values'}}, [@fill];
148 }
149 }
150 ▶ } elsif( ! defined $rownames && ! defined $colnames && ! defined $values ) {
Condition0%0/6
defined $rownames || defined $colnames || defined $values
l!l&&r!l&&!r
000
defined $rownames or defined $colnames
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $rownames || defined $colnames || defined $values
defined $rownames defined $colnames defined $values
Truth table0%0/4
A or B || C
A= defined $rownamesB= defined $colnamesC= defined $values
ABCresult
1XX1
01X1
0011
0000
151 0 $self->{'_values'} = [];
152 0 $self->{'_rownames'} = [];
153 0 $self->{'_colnames'} = [];
154 } else {
155 0 $self->throw("Must have either provided no values/colnames/rownames or provided all three");
156 }
157  
158 5 return $self;
159 }
160  
161  
162 =head2 matrix_id
163  
164 Title : matrix_id
165 Usage : my $id = $matrix->matrix_id
166 Function: Get/Set the matrix ID
167 Returns : scalar value
168 Args : [optional] new id value to store
169  
170  
171 =cut
172  
173 2 ▶sub matrix_id{
Subroutine matrix_id: called CC=2 SCAR=6.9
Pod documented
174 2 my $self = shift;
175 2 ▶ return $self->{'_matid'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
176 1 return $self->{'_matid'};
177  
178
179 }
180  
181 =head2 matrix_name
182  
183 Title : matrix_name
184 Usage : my $name = $matrix->matrix_name();
185 Function: Get/Set the matrix name
186 Returns : scalar value
187 Args : [optional] new matrix name value
188  
189  
190 =cut
191  
192 3 ▶sub matrix_name{
Subroutine matrix_name: called CC=2 SCAR=6.9
Pod documented
193 3 my $self = shift;
194 3 ▶ return $self->{'_matname'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
195 1 return $self->{'_matname'};
196 }
197  
198  
199 =head2 entry
200  
201 Title : entry
202 Usage : my $entry = $matrix->entry($row,$col,$value)
203 Function: Get the value for a specific cell as specified
204 by the row and column names
205 Returns : scalar value or undef if row or col does not
206 exist
207 Args : $rowname - name of the row
208 $colname - column name
209 $value - [optional] New value for the entry
210  
211 =cut
212  
213 16 ▶sub entry{
Subroutine entry: called CC=3 SCAR=12.3
Pod documented
214 16 my ($self,$row,$column,$newvalue) = @_;
215 16 ▶ if( ! defined $row || ! defined $column ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row and defined $column) 0 16 16
Condition33%1/3
defined $row and defined $column
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $row and defined $column
defined $row defined $column
Truth table33%1/3
A and B
A= defined $rowB= defined $column
ABresult
0X0
100
111
216 0 $self->throw("Need at least 2 ids");
217 }
218  
219 16 my ($rownum) = $self->row_num_for_name($row);
220 16 my ($colnum) = $self->column_num_for_name($column);
221 16 return $self->entry_by_num($rownum,$colnum,$newvalue);
222 }
223  
224 =head2 get_entry
225  
226 Title : get_entry
227 Usage : my $entry = $matrix->get_entry($rowname,$columname,$value)
228 Function: Get the entry for a given row,column pair
229 Returns : scalar
230 Args : $row name
231 $column name
232 $value
233  
234  
235 =cut
236  
237 14 ▶sub get_entry{ $_[0]->entry($_[1],$_[2]) }
Subroutine get_entry: called CC=1 SCAR=0.0
Pod documented
238  
239 =head2 entry_by_num
240  
241 Title : entry_by_num
242 Usage : my $entry = $matrix->entry_by_num($rownum,$colnum)
243 Function: Get an entry by row and column numbers instead of by name
244 (rows and columns start at 0)
245 Returns : scalar value or undef if row or column name does not
246 exist
247 Args : $row - row number
248 $col - column number
249 [optional] $newvalue to store at this cell
250  
251 =cut
252  
253 169 ▶sub entry_by_num {
Subroutine entry_by_num: called CC=6 SCAR=22.3
Pod documented
254 169 my ($self,$row,$col,$newvalue) = @_;
255 169 ▶ if( ! defined $row || ! defined $col ||
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row and defined $col and $row =~ /^\d+$/ and $col =~ /^\d+$/) 0 169 169
Condition33%3/9
defined $row and defined $col and $row =~ /^\d+$/ and $col =~ /^\d+$/
!ll&&!rl&&r
00169
defined $row and defined $col and $row =~ /^\d+$/
!ll&&!rl&&r
00169
defined $row and defined $col
!ll&&!rl&&r
00169
MC/DC0%0/4
defined $row and defined $col and $row =~ /^\d+$/ and $col =~ /^\d+$/
defined $row defined $col $row =~ /^\d+$/ $col =~ /^\d+$/
Truth table20%1/5
A and B and C and D
A= defined $rowB= defined $colC= $row =~ /^\d+$/D= $col =~ /^\d+$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
256 $row !~ /^\d+$/ ||
257 $col !~ /^\d+$/ ) {
258 0 $self->warn("expected to get 2 number for entry_by_num");
259 0 return;
260 }
261
262 169 ▶ if( defined $newvalue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newvalue) { } 15 154 169
263 15 return $self->_values->[$row][$col] = $newvalue;
264 } else {
265 154 return $self->_values->[$row][$col];
266 }
267 }
268  
269 0 ▶sub get_element {
Subroutine get_element: not called CC=1 SCAR=6.9
Pod undocumented
270 0 my $self = shift;
271 0 $self->entry(@_);
272 }
273  
274  
275 =head2 column
276  
277 Title : column
278 Usage : my @col = $matrix->column('ALPHA');
279 OR
280 $matrix->column('ALPHA', \@col);
281 Function: Get/Set a particular column
282 Returns : Array (in array context) or arrayref (in scalar context)
283 of values.
284 For setting will warn if the new column is of a different
285 length from the rest of the columns.
286 Args : name of the column
287 [optional] new column to store here
288  
289 =cut
290  
291 1 ▶sub column{
Subroutine column: called CC=3 SCAR=13.6
Pod documented
292 1 my ($self,$column,$newcol) = @_;
293  
294 1 ▶ if( ! defined $column ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column) 0 1 1
295 0 $self->warn("Need at least a column id");
296 0 return;
297 }
298 1 my $colnum = $self->column_num_for_name($column);
299 1 ▶ if( ! defined $colnum ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $colnum) 0 1 1
300 0 $self->warn("could not find column number for $column");
301 0 return;
302 }
303 1 return $self->column_by_num($colnum,$newcol);
304 }
305  
306  
307 =head2 get_column
308  
309 Title : get_column
310 Usage : my @row = $matrix->get_column('ALPHA');
311 Function: Get a particular column
312 Returns : Array (in array context) or arrayref (in scalar context)
313 of values
314 Args : name of the column
315  
316  
317 =cut
318  
319 1 ▶sub get_column { $_[0]->column($_[1]) }
Subroutine get_column: called CC=1 SCAR=0.0
Pod documented
320  
321  
322 =head2 column_by_num
323  
324 Title : column_by_num
325 Usage : my @col = $matrix->column_by_num(1);
326 OR
327 $matrix->column_by_num(1,\@newcol);
328 Function: Get/Set a column by its number instead of name
329 (cols/rows start at 0)
330 Returns : Array (in array context) or arrayref (in scalar context)
331 of values
332 Args : name of the column
333 [optional] new value to store for a particular column
334  
335 =cut
336  
337 1 ▶sub column_by_num{
Subroutine column_by_num: called CC=8 SCAR=29.8
Pod documented
338 1 my ($self,$colnum,$newcol) = @_;
339 1 ▶ if( ! defined $colnum ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $colnum) 0 1 1
340 0 $self->warn("need at least a column number");
341 0 return;
342 }
343 1 my $rowcount = $self->num_rows;
344 1 my $colcount = $self->num_columns;
345 1 my $ret;
346
347 1 ▶ if( defined $newcol ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newcol) { } 0 1 1
348 0 ▶ if( ref($newcol) !~ /ARRAY/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($newcol) =~ /ARRAY/i) 0 0 0
349 0 $self->warn("expected a valid arrayref for resetting a column");
350 0 return;
351 }
352 0 ▶ if( scalar @$newcol != $rowcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$newcol != $rowcount) 0 0 0
353 0 $self->warn("new column is not the correct length ($rowcount) - call add or remove row to shrink or grow the number of rows first");
354 0 return;
355 }
356 0 ▶ for(my $i=0; $i < $rowcount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rowcount) 0 0 0
357 0 $self->entry_by_num($i,$colnum,$newcol->[$i]);
358 }
359 0 $ret = $newcol;
360 } else {
361 1 $ret = [];
362 1 ▶ for(my $i=0; $i < $rowcount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rowcount) 3 1 4
363 3 push @$ret,$self->entry_by_num($i,$colnum);
364 }
365 }
366 1 ▶ if( wantarray ) { return @$ret }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 0 1
367 0 return $ret;
368  
369 }
370  
371 =head2 row
372  
373 Title : row
374 Usage : my @row = $matrix->row($rowname);
375 OR
376 $matrix->row($rowname,\@rowvalues);
377 Function: Get/Set the row of the matrix
378 Returns : Array (in array context) or arrayref (in scalar context)
379 Args : rowname
380 [optional] new value of row to store
381  
382  
383 =cut
384  
385 9 ▶sub row {
Subroutine row: called CC=2 SCAR=7.9
Pod documented
386 9 my ($self,$row,$newrow) = @_;
387 9 ▶ if( ! defined $row) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 9 9
388 0 $self->warn("Need at least a row id");
389 0 return;
390 }
391 9 my $rownum = $self->row_num_for_name($row);
392 9 return $self->row_by_num($rownum,$newrow);
393 }
394  
395  
396 =head2 get_row
397  
398 Title : get_row
399 Usage : my @row = $matrix->get_row('ALPHA');
400 Function: Get a particular row
401 Returns : Array (in array context) or arrayref (in scalar context)
402 of values
403 Args : name of the row
404  
405 =cut
406  
407 9 ▶sub get_row { $_[0]->row($_[1]) }
Subroutine get_row: called CC=1 SCAR=0.0
Pod documented
408  
409 =head2 row_by_num
410  
411 Title : row_by_num
412 Usage : my @row = $matrix->row_by_num($rownum);
413 OR
414 $matrix->row($rownum,\@rowvalues);
415 Function: Get/Set the row of the matrix
416 Returns : Array (in array context) or arrayref (in scalar context)
417 Args : rowname
418 [optional] new value of row to store
419  
420 =cut
421  
422 9 ▶sub row_by_num{
Subroutine row_by_num: called CC=8 SCAR=30.3
Pod documented
423 9 my ($self,$rownum,$newrow) = @_;
424 9 ▶ if( ! defined $rownum ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rownum) 0 9 9
425 0 $self->warn("need at least a row number");
426 0 return;
427 }
428 9 my $colcount = $self->num_columns;
429 9 my $ret;
430 9 ▶ if( defined $newrow ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newrow) { } 0 9 9
431 0 ▶ if( ref($newrow) !~ /ARRAY/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($newrow) =~ /ARRAY/i) 0 0 0
432 0 $self->warn("expected a valid arrayref for resetting a row");
433 0 return;
434 }
435 0 ▶ if( scalar @$newrow != $colcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$newrow != $colcount) 0 0 0
436 0 $self->warn("new row is not the correct length ($colcount) - call add or remove column to shrink or grow the number of columns first");
437 0 return;
438 }
439 0 ▶ for(my $i=0; $i < $colcount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $colcount) 0 0 0
440 0 $self->entry_by_num($rownum,$i, $newrow->[$i]);
441 }
442 0 $ret = $newrow;
443 } else {
444 9 $ret = [];
445 9 ▶ for(my $i=0; $i < $colcount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $colcount) 87 9 96
446 # we're doing this to explicitly
447 # copy the entire row
448 87 push @$ret, $self->entry_by_num($rownum,$i);
449 }
450 }
451 9 ▶ if( wantarray ) { return @$ret }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 9 0 9
452 0 return $ret;
453  
454  
455 }
456  
457  
458 =head2 diagonal
459  
460 Title : diagonal
461 Usage : my @diagonal = $matrix->get_diagonal()
462 Function: Get the diagonal of a matrix
463 Returns : Array (in array context) or arrayref (in scalar context)
464 of values which lie along the diagonal
465 Args : none
466  
467  
468 =cut
469  
470 2 ▶sub get_diagonal{
Subroutine get_diagonal: called CC=2 SCAR=6.9
Pod documented
471 2 my ($self) = @_;
472 2 my @diag;
473 2 my $rowcount = $self->num_rows;
474 2 my $colcount = $self->num_columns;
475 2 ▶ for(my $i = 0; $i < $rowcount; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rowcount) 48 2 50
476 48 push @diag, $self->entry_by_num($i,$i);
477 }
478 2 return @diag;
479 }
480  
481  
482 =head2 add_row
483  
484 Title : add_row
485 Usage : $matrix->add_row($index,\@newrow);
486 Function: Adds a row at particular location in the matrix.
487 If $index < the rowcount will shift all the rows down
488 by the number of new rows.
489 To add a single empty row, simply call
490 $matrix->add_row($index,undef);
491 Returns : the updated number of total rows in the matrix
492 Args : index to store
493 name of the row (header)
494 newrow to add, if this is undef will add a single
495 row with all values set to undef
496  
497 =cut
498  
499 2 ▶sub add_row{
Subroutine add_row: called CC=9 SCAR=26.9
Pod documented
500 2 my ($self,$index,$name,$newrow) = @_;
501 2 ▶ if( !defined $index ||
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $index && $index =~ /^\d+$/) { } 0 2 2
elsif (not defined $name) { } 0 2 2
elsif (defined $self->row_num_for_name($name)) { } 0 2 2
Condition33%1/3
defined $index && $index =~ /^\d+$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $index && $index =~ /^\d+$/
defined $index $index =~ /^\d+$/
Truth table33%1/3
A && B
A= defined $indexB= $index =~ /^\d+$/
ABresult
0X0
100
111
502 $index !~ /^\d+$/ ) {
503 0 $self->warn("expected a valid row index in add_row");
504 0 return;
505 } elsif( ! defined $name) {
506 0 $self->warn("Need a row name or heading");
507 0 return;
508 } elsif( defined $self->row_num_for_name($name) ) {
509 0 $self->warn("Need a unqiue name for the column heading, $name is already used");
510 0 return;
511 }
512 2 my $colcount = $self->num_columns;
513 2 my $rowcount = $self->num_rows;
514  
515 2 ▶ if( $index > $rowcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > $rowcount) 0 2 2
516 0 $self->warn("cannot add a row beyond 1+last row at the end ($rowcount) not $index - adding at $rowcount instead");
517 0 $index = $rowcount;
518 }
519  
520 2 ▶ if( ! defined $newrow ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $newrow) { } 0 2 2
elsif (not ref($newrow) =~ /ARRAY/i) { } 0 2 2
521 0 $newrow = [];
522 0 $newrow->[$colcount] = undef;
523 } elsif( ref($newrow) !~ /ARRAY/i ) {
524 0 $self->throw("Expected either undef or a valid arrayref for add_row");
525 }
526 # add this row to the matrix by carving out space for it with
527 # splice
528 2 splice(@{$self->{'_values'}}, $index,0,[]);
529 2 ▶ for( my $i = 0; $i < $colcount; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $colcount) 7 2 9
530 7 $self->entry_by_num($index,$i,$newrow->[$i]);
531 }
532 2 splice(@{$self->{'_rownames'}}, $index,0,$name);
533 # Sadly we have to remap these each time (except for the case
534 # when we're adding a new column to the end, but I don't think
535 # the speedup for that case warrants the extra code at this time.
536 2 my $ct = 0;
537 2 %{$self->{'_rownamesmap'}} = map { $_ => $ct++} @{$self->{'_rownames'}};
538 2 return $self->num_rows;
539 }
540  
541 =head2 remove_row
542  
543 Title : remove_row
544 Usage : $matrix->remove_row($colnum)
545 Function: remove a row from the matrix shifting all the rows
546 up by one
547 Returns : Updated number of rows in the matrix
548 Args : row index
549  
550  
551 =cut
552  
553 1 ▶sub remove_row{
Subroutine remove_row: called CC=2 SCAR=7.0
Pod documented
554 1 my ($self,$rowindex) = @_;
555 1 my $rowcount = $self->num_rows;
556
557 1 ▶ if( $rowindex > $rowcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowindex > $rowcount) { } 0 1 1
558 0 $self->warn("rowindex $rowindex is greater than number of rows $rowcount, cannot process");
559 0 return 0;
560 } else {
561 1 splice(@{$self->_values},$rowindex,1);
562 1 delete $self->{'_rownamesmap'}->{$self->{'_rownames'}->[$rowindex]};
563 1 splice(@{$self->{'_rownames'}},$rowindex,1);
564 }
565 1 my $ct = 0;
566 1 %{$self->{'_rownamesmap'}} = map { $_ => $ct++} @{$self->{'_rownames'}};
567 1 return $self->num_rows;
568 }
569  
570 =head2 add_column
571  
572 Title : add_column
573 Usage : $matrix->add_column($index,$colname,\@newcol);
574 Function: Adds a column at particular location in the matrix.
575 If $index < the colcount will shift all the columns right
576 by the number of new columns.
577 To add a single empty column, simply call
578 $matrix->add_column($index,undef);
579 Returns : the updated number of total columns in the matrix
580 Args : index to store
581 name of the column (header)
582 newcolumn to add, if this is undef will add a single
583 column with all values set to undef
584  
585  
586 =cut
587  
588  
589 2 ▶sub add_column{
Subroutine add_column: called CC=9 SCAR=26.9
Pod documented
590 2 my ($self,$index,$name,$newcol) = @_;
591 2 ▶ if( !defined $index ||
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $index && $index =~ /^\d+$/) { } 0 2 2
elsif (not defined $name) { } 0 2 2
elsif (defined $self->column_num_for_name($name)) { } 0 2 2
Condition33%1/3
defined $index && $index =~ /^\d+$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $index && $index =~ /^\d+$/
defined $index $index =~ /^\d+$/
Truth table33%1/3
A && B
A= defined $indexB= $index =~ /^\d+$/
ABresult
0X0
100
111
592 $index !~ /^\d+$/ ) {
593 0 $self->warn("expected a valid col index in add_column");
594 0 return;
595 } elsif( ! defined $name) {
596 0 $self->warn("Need a column name or heading");
597 0 return;
598 } elsif( defined $self->column_num_for_name($name) ) {
599 0 $self->warn("Need a unqiue name for the column heading, $name is already used");
600 0 return;
601 }
602 2 my $colcount = $self->num_columns;
603 2 my $rowcount = $self->num_rows;
604 2 ▶ if( $index > $colcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > $colcount) 0 2 2
605 0 $self->warn("cannot add a column beyond 1+last column at the end ($colcount) not $index - adding at $colcount instead");
606 0 $index = $colcount;
607 }
608  
609 2 ▶ if( ! defined $newcol ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $newcol) { } 0 2 2
elsif (not ref($newcol) =~ /ARRAY/i) { } 0 2 2
610 0 $newcol = [];
611 0 $newcol->[$rowcount] = undef; # make the array '$rowcount' long
612 } elsif( ref($newcol) !~ /ARRAY/i ) {
613 0 $self->throw("Expected either undef or a valid arrayref for add_row");
614 }
615 2 ▶ for( my $i = 0; $i < $rowcount; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rowcount) 8 2 10
616 # add this column to each row
617 8 splice(@{$self->_values->[$i]},$index,0,[]);
618 8 $self->entry_by_num($i,$index,$newcol->[$i]);
619 }
620 2 splice(@{$self->{'_colnames'}}, $index,0,$name);
621 # Sadly we have to remap these each time (except for the case
622 # when we're adding a new column to the end, but I don't think
623 # the speedup for that case warrants the extra code at this time.
624 2 my $ct = 0;
625 2 %{$self->{'_colnamesmap'}} = map {$_ => $ct++} @{$self->{'_colnames'}};
626 2 return $self->num_columns;
627 }
628  
629 =head2 remove_column
630  
631 Title : remove_column
632 Usage : $matrix->remove_column($colnum)
633 Function: remove a column from the matrix shifting all the columns
634 to the left by one
635 Returns : Updated number of columns in the matrix
636 Args : column index
637  
638 =cut
639  
640 1 ▶sub remove_column{
Subroutine remove_column: called CC=3 SCAR=11.1
Pod documented
641 1 my ($self,$colindex) = @_;
642  
643 1 my $colcount = $self->num_columns;
644 1 my $rowcount = $self->num_rows;
645 1 ▶ if( $colindex > $colcount ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colindex > $colcount) { } 0 1 1
646 0 $self->warn("colindex $colindex is greater than number of columns ($colcount), cannot process");
647 0 return 0;
648 } else {
649 1 ▶ for(my $i = 0; $i < $rowcount; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rowcount) 4 1 5
650 4 splice(@{$self->_values->[$i]},$colindex,1);
651 }
652 1 delete $self->{'_colnamesmap'}->{$self->{'_colnames'}->[$colindex]};
653 1 splice(@{$self->{'_colnames'}},$colindex,1);
654 }
655 1 my $ct = 0;
656 1 %{$self->{'_colnamesmap'}} = map {$_ => $ct++} @{$self->{'_colnames'}};
657 1 return $self->num_columns;
658 }
659  
660 =head2 column_num_for_name
661  
662 Title : column_num_for_name
663 Usage : my $num = $matrix->column_num_for_name($name)
664 Function: Gets the column number for a particular column name
665 Returns : integer
666 Args : string
667  
668  
669 =cut
670  
671 23 ▶sub column_num_for_name{
Subroutine column_num_for_name: called CC=1 SCAR=0.0
Pod documented
672 23 my ($self,$name) = @_;
673
674 23 return $self->{'_colnamesmap'}->{$name};
675 }
676  
677 =head2 row_num_for_name
678  
679 Title : row_num_for_name
680 Usage : my $num = $matrix->row_num_for_name
681 Function: Gets the row number for a particular row name
682 Returns : integer
683 Args : string
684  
685  
686 =cut
687  
688 30 ▶sub row_num_for_name{
Subroutine row_num_for_name: called CC=1 SCAR=0.0
Pod documented
689 30 my ($self,$name) = @_;
690 30 return $self->{'_rownamesmap'}->{$name}
691 }
692  
693  
694 =head2 column_header
695  
696 Title : column_header
697 Usage : my $name = $matrix->column_header(0)
698 Function: Gets the column header for a particular column number
699 Returns : string
700 Args : integer
701  
702  
703 =cut
704  
705 2 ▶sub column_header{
Subroutine column_header: called CC=1 SCAR=0.0
Pod documented
706 2 my ($self,$num) = @_;
707 2 return $self->{'_colnames'}->[$num];
708 }
709  
710  
711 =head2 row_header
712  
713 Title : row_header
714 Usage : my $name = $matrix->row_header(0)
715 Function: Gets the row header for a particular row number
716 Returns : string
717 Args : integer
718  
719  
720 =cut
721  
722 2 ▶sub row_header{
Subroutine row_header: called CC=1 SCAR=0.0
Pod documented
723 2 my ($self,$num) = @_;
724 2 return $self->{'_rownames'}->[$num];
725 }
726  
727 =head2 num_rows
728  
729 Title : num_rows
730 Usage : my $rowcount = $matrix->num_rows;
731 Function: Get the number of rows
732 Returns : integer
733 Args : none
734  
735  
736 =cut
737  
738 14 ▶sub num_rows{
Subroutine num_rows: called CC=1 SCAR=0.0
Pod documented
739 14 my ($self) = @_;
740 14 return scalar @{$self->_values};
741 }
742  
743  
744 =head2 num_columns
745  
746 Title : num_columns
747 Usage : my $colcount = $matrix->num_columns
748 Function: Get the number of columns
749 Returns : integer
750 Args : none
751  
752  
753 =cut
754  
755 21 ▶sub num_columns{
Subroutine num_columns: called CC=2 SCAR=7.1
Pod documented
756 21 my ($self) = @_;
757 21 ▶ return scalar @{$self->_values->[0] || []};
Condition50%1/2
$self->_values->[0] || []
l!l
210
MC/DC0%0/1
$self->_values->[0] || []
$self->_values->[0]
Truth table50%1/2
A
A= $self->_values->[0]
Aresult
11
00
758 }
759  
760  
761 =head2 row_names
762  
763 Title : row_names
764 Usage : my @rows = $matrix->row_names
765 Function: The names of all the rows
766 Returns : array in array context, arrayref in scalar context
767 Args : none
768  
769  
770 =cut
771  
772 0 ▶sub row_names{
Subroutine row_names: not called CC=2 SCAR=17.9
Pod documented
773 0 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
774 0 return @{shift->{'_rownames'}};
775 } else {
776 0 return shift->{'_rownames'};
777 }
778 }
779  
780  
781 =head2 column_names
782  
783 Title : column_names
784 Usage : my @columns = $matrix->column_names
785 Function: The names of all the columns
786 Returns : array in array context, arrayref in scalar context
787 Args : none
788  
789  
790 =cut
791  
792 0 ▶sub column_names{
Subroutine column_names: not called CC=2 SCAR=17.9
Pod documented
793 0 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
794 0 return @{shift->{'_colnames'}};
795 } else {
796 0 return shift->{'_colnames'};
797 }
798 }
799  
800 =head2 private methods
801  
802 Private methods for a Generic Matrix
803  
804 =head2 _values
805  
806 Title : _values
807 Usage : $matrix->_values();
808 Function: get/set for array ref of the matrix containing
809 distance values
810 Returns : an array reference
811 Args : an array reference
812  
813  
814 =cut
815  
816 217 ▶sub _values{
Subroutine _values: called CC=2 SCAR=7.6
817 217 my ($self,$val) = @_;
818 217 ▶ if( $val ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 217 217
819 0 $self->{'_values'} = $val;
820 }
821 217 return $self->{'_values'};
822 }
823  
824 6 1;
« Bio/DasI.pm ↑ Summary Bio/DB/Qual.pm »