~ubuntu-branches/ubuntu/hoary/bioperl/hoary

« back to all changes in this revision

Viewing changes to Bio/Matrix/Generic.pm

  • Committer: Bazaar Package Importer
  • Author(s): Matt Hope
  • Date: 2004-04-18 14:24:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040418142411-gr92uexquw4w8liq
Tags: 1.4-1
* New upstream release
* Examples and working code are installed by default to usr/bin,
  this has been moved to usr/share/doc/bioperl/bin

Show diffs side-by-side

added added

removed removed

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