lib/Microarray/DataMatrix.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 40 totaltotal 0.0% 0 / 288 CC 17 SCAR 19.4
CC
17
Cov
0%
_rowAverage
30.0
_average
20.0
_setAutoDump
6.0
CRAP 7.0
1 package Microarray::DataMatrix;
2  
3 # License information (the MIT license)
4  
5 # Copyright (c) 2003 Christian Rees, Janos Demeter, John Matese, Gavin
6 # Sherlock; Stanford University
7  
8 # Permission is hereby granted, free of charge, to any person
9 # obtaining a copy of this software and associated documentation files
10 # (the "Software"), to deal in the Software without restriction,
11 # including without limitation the rights to use, copy, modify, merge,
12 # publish, distribute, sublicense, and/or sell copies of the Software,
13 # and to permit persons to whom the Software is furnished to do so,
14 # subject to the following conditions:
15  
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18  
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27  
28  
29 # Author : Gavin Sherlock
30 # Date : 11th June 2002
31  
32 # This package provides a base class for dealing with the contents
33 # of a dataMatrix, providing a structure whereby tracking of whether
34 # rows or columns in the matrix are deemed valid, and some primitives
35 # are provided for manipulating the data.
36  
37 use strict;
38  
39 ####################################################################
40 #
41 # CLASS GLOBALS
42 #
43 ####################################################################
44  
45 # The following globals are used so we only ever access hash entries
46 # (ie object attributes) using variables, to prevent typos, or
47 # accidental clobberring.
48  
49 # These values are assigned here and then are READ-ONLY.
50  
51 my $PACKAGE = ' Microarray::DataMatrix'; # will use to prefix all hash entries
52 # which will prevent collision with
53 # any attributes in super or sub
54 # classes.
55  
56 my $kValidColumns = $PACKAGE.'::__validColumns'; # the currently valid columns
57 my $kValidRows = $PACKAGE.'::__validRows'; # the currently valid rows
58 my $kErrstr = $PACKAGE.'::__errstr'; # to hold the contents of the last error
59 my $kNumColumnsToReport = $PACKAGE.'::__numColumnsToReport'; # for when someone wants verbose reporting
60 my $kNumRowsToReport = $PACKAGE.'::__numRowsToReport'; # for when someone wants verbose reporting
61 my $kAutoDump = $PACKAGE.'::__autoDump'; # whether to autodump
62  
63 my $kNumberFormat = "%.3f"; # precision for average, etc.
64  
65 my %kLineEndings = (text=>"\n",
66 html=>"\n<br>");
67  
68 my %kAllowedCenteringMethods = ('mean' => undef,
69 'median' => undef);
70  
71 # The following hash contains the names of all the operators, and
72 # their aliases, that a client can use when filtering rows based on
73 # the actual data. The values are the names of methods that are used to
74 # do the comparison. Each method simply takes two values, one being the
75 # left hand value, and one the right hand value of the comparison.
76  
77 my %kAllowedOperators = ("absolute value >" => "_absgt",
78 "absolute value >=" => "_absgteq",
79 "absolute value =" => "_abseq",
80 "absolute value <" => "_abslt",
81 "absolute value <=" => "_abslteq",
82 ">" => "_gt",
83 ">=" => "_gteq",
84 "=" => "_eq",
85 "<=" => "_lteq",
86 "<" => "_lt",
87 "not equal" => "_ne",
88 "absgt" => "_absgt",
89 "absgteq" => "_absgteq",
90 "abseq" => "_abseq",
91 "abslt" => "_abslt",
92 "abslteq" => "_abslteq",
93 "gt" => "_gt",
94 "gteq" => "_gteq",
95 "eq" => "_eq",
96 "lteq" => "_lteq",
97 "lt" => "_lt",
98 "ne" => "_ne",
99 "|>|" => "_absgt",
100 "|>=|" => "_absgteq",
101 "|=|" => "_abseq",
102 "|<|" => "_abslt",
103 "|<=|" => "_abslteq",
104 "==" => "_eq",
105 "!=" => "_ne");
106  
107 #####################################################################
108 #
109 # PROTECTED SETTER METHODS - should only be used by subclasses
110 #
111 #####################################################################
112  
113 #####################################################################
114 sub _setAutoDump{
115 #####################################################################
116 # This method is used to set the autodump flag, which can be either 1
117 # or 0. This should only be utilized by subclasses, not clients.
118 #
119 # Usage: $self->_setAutoDump(1);
120  
121 my ($self, $value) = @_;
122  
123 if ($value != 0 && $value != 1){
124  
125 $self->_usage("setAutoDump requires an argument of either 0 or 1!\nYou provided : \"$value\"\n");
126  
127 }
128  
129 $self->{$kAutoDump} = $value;
130  
131 }
132  
133 #####################################################################
134 sub _setValidColumns{
135 #####################################################################
136 # This protected setter method receives a reference to a hash, which
137 # has as its keys the index of the columns which are valid. This
138 # method is expected to only be used when the matrix has been first
139 # read, to set up all the columns which are initially valid. The
140 # values of the hash will usually be undef, to simply save space.
141 # There is no expectation for them to be otherwise.
142 #
143 # Usage: $self->_setValidColumns(\%validColumns);
144 #
145  
146 my ($self, $validColumnsHashRef) = @_;
147
148 $self->{$kValidColumns} = $validColumnsHashRef;
149
150 }
151
152 #####################################################################
153 sub _setValidRows{
154 #####################################################################
155 # This protected setter method receives a reference to a hash, which
156 # has as its keys the index of the rows which are valid. This
157 # method is expected to only be used when the matrix has been first
158 # read, to set up all the rows which are initially valid. The
159 # values of the hash will usually be undef, to simply save space.
160 # There is no expectation for them to be otherwise.
161 #
162 # Usage: $self->_setValidRows(\%validRows);
163 #
164  
165 my ($self, $validRowsHashRef) = @_;
166
167 $self->{$kValidRows} = $validRowsHashRef;
168
169 }
170  
171 #####################################################################
172 sub _setErrstr{
173 #####################################################################
174 # This protected setter method accepts a scalar, that will correspond
175 # to an error that has occurred, and will store it within the object.
176 #
177 # Usage: $self->_setErrstr($error);
178  
179 my ($self, $errstr) = @_;
180  
181 $self->{$kErrstr} = $errstr;
182  
183 }
184  
185 ######################################################################
186 sub _invalidateMatrixRow{
187 ######################################################################
188 # This protected mutator method makes a row invalid. This method is not
189 # undoable, because the invalidation also deletes the data for the row.
190 #
191 # Usage : $self->_invalidateMatrixRow($row);
192  
193 my ($self, $row) = @_;
194  
195 delete $self->{$kValidRows}->{$row}; # make invalid
196  
197 }
198  
199 ######################################################################
200 sub _invalidateMatrixColumn{
201 ######################################################################
202 # This protected mutator method makes a column invalid.
203 #
204 # Usage : $self->_invalidateMatrixColumn($column);
205  
206 my ($self, $column) = @_;
207
208 delete $self->{$kValidColumns}->{$column}; # make invalid
209  
210 }
211  
212 #####################################################################
213 #
214 # PROTECTED GETTER/ACCESSOR METHODS - should only be used by this
215 # class, and subclasses
216 #
217 #####################################################################
218  
219 #####################################################################
220 sub _autoDump{
221 #####################################################################
222 # This protected method returns a boolean to indicate whether
223 # autodumping is enabled.
224 #
225 # Usage : if ($self->_autoDump){ # blah };
226  
227 return $_[0]->{$kAutoDump};
228  
229 }
230  
231 ######################################################################
232 sub _validRowsArrayRef{
233 ######################################################################
234 # This protected accessor returns a reference to an array that contains
235 # the indexes of all the valid rows
236 #
237 # Usage:
238 #
239 # foreach my $row (@{$self->_validRowsArrayRef}){
240 #
241 # # do something useful
242 #
243 # }
244  
245 return [keys %{$_[0]->{$kValidRows}}];
246  
247 }
248  
249 ######################################################################
250 sub _validColumnsArrayRef{
251 ######################################################################
252 # This protected accessor returns a reference to an array that contains
253 # the indexes of all the valid columns.
254 #
255 # Usage:
256 #
257 # foreach my $column (@{$self->_validColumnsArrayRef}){
258 #
259 # # do something useful
260 #
261 # }
262  
263 return [keys %{$_[0]->{$kValidColumns}}];
264  
265 }
266  
267 ######################################################################
268 sub _matrixRowIsValid{
269 ######################################################################
270 # This protected accessor returns a boolean to indicate whether a
271 # given row in the data matrix is still valid (ie has not been
272 # filtered out)
273 #
274 # Usage : if ($self->_matrixRowIsValid($row)){ # blah }
275  
276 return exists $_[0]->{$kValidRows}->{$_[1]};
277  
278 }
279  
280 ######################################################################
281 sub _matrixColumnIsValid{
282 ######################################################################
283 # This protected accessor returns a boolean to indicate whether a
284 # given column in the data matrix is still valid (ie has not been
285 # filtered out)
286 #
287 # Usage : if ($self->_matrixColumnIsValid($column)){ # blah }
288  
289 return exists $_[0]->{$kValidColumns}->{$_[1]};
290  
291 }
292  
293 #####################################################################
294 sub _numColumnsToReport{
295 #####################################################################
296 # This protected method returns the number of columns to process after which
297 # reporting should be done, if verbose reporting has been indicated.
298 # If no value has been set, then the default of 50 is returned.
299 #
300 # Usage :
301 #
302 # my $numColumnsToReport = $self->_numColumnsToReport;
303 #
304  
305 my ($self) = @_;
306  
307 if (!exists $self->{$kNumColumnsToReport}){
308  
309 $self->setNumColumnsToReport(50); # set to default
310  
311 }
312  
313 return $self->{$kNumColumnsToReport};
314  
315 }
316  
317 #####################################################################
318 sub _numRowsToReport{
319 #####################################################################
320 # This protected method returns the number of rows to process after which
321 # reporting should be done, if verbose reporting has been indicated.
322 # If no value has been set, then the default of 5000 is returned.
323 #
324 # Usage :
325 #
326 # my $numRowsToReport = $self->_numRowsToReport;
327 #
328  
329 my ($self) = @_;
330  
331 if (!exists $self->{$kNumRowsToReport}){
332  
333 $self->setNumRowsToReport(5000);
334  
335 }
336  
337 return $self->{$kNumRowsToReport};
338  
339 }
340  
341 #####################################################################
342 sub _lineEnding{
343 #####################################################################
344 # This protected method returns the appropriate line ending, for text
345 # or html reporting. It expects a string, either 'html' or 'text' and
346 # will return the appropriate line ending.
347 #
348 # Usage:
349 #
350 # my $lineEnding = $self->_lineEnding("text");
351  
352 my ($self, $reportingType) = @_;
353  
354 if (exists $kLineEndings{lc($reportingType)}){
355  
356 return $kLineEndings{lc($reportingType)};
357  
358 }else{
359  
360 die "The reporting type '$reportingType' is not recognized.";
361  
362 }
363  
364 }
365  
366 #####################################################################
367 sub _centeringMethodIsAllowed{
368 #####################################################################
369 # This protected method returns a boolean to indicate whether a
370 # centering method is allowed. Allowed methods are 'mean' and
371 # 'median'.
372 #
373 # Usage : if ($self->_centeringMethodIsAllowed($method)){ # blah }
374  
375 my ($self, $method) = @_;
376  
377 return exists $kAllowedCenteringMethods{$method};
378  
379 }
380  
381 #####################################################################
382 sub _operatorIsAllowed{
383 #####################################################################
384 # This protected method returns a boolean to indicate whether a
385 # particular operator is allowed. For each operator, there exists a
386 # corresponding method that uses that operator.
387  
388 #
389 # Usage : if ($self->_operatorIsAllowed($operator)){ # blah }
390  
391 my ($self, $operator) = @_;
392  
393 return exists $kAllowedOperators{$operator};
394  
395 }
396  
397 ######################################################################
398 sub _methodForOperator{
399 ######################################################################
400 # This protected method returns the name of the method that is used to compare
401 # two values, based on the operator that was passed in.
402 #
403 # Usage : my $method = $self->_methodForOperator($operator);
404 #
405  
406 my ($self, $operator) = @_;
407  
408 return $kAllowedOperators{$operator};
409  
410 }
411  
412 # The following set of protected methods are used for comparing two
413 # values to each other. All of them return a boolean to indicate if
414 # the comparison failed or succeeded
415  
416 ######################################################################
417 sub _absgt{
418 ######################################################################
419 # This method returns a boolean to indicate if the first argument is
420 # absolutely greater than the second argument.
421 #
422 # Usage: if ($self->_absgt($x, $y)){ # blah }
423  
424 my ($self, $lhs, $rhs) = @_;
425  
426 return (abs($lhs) > abs($rhs));
427  
428 }
429  
430 ######################################################################
431 sub _absgteq{
432 ######################################################################
433 # This method returns a boolean to indicate if the first argument is
434 # absolutely greater than or equal to the second argument.
435 #
436 # Usage: if ($self->_absgteq($x, $y)){ # blah }
437  
438 my ($self, $lhs, $rhs) = @_;
439  
440 return (abs($lhs) >= abs($rhs));
441  
442 }
443  
444 ######################################################################
445 sub _abseq{
446 ######################################################################
447 # This method returns a boolean to indicate if the first argument is
448 # absolutely equal to the second argument.
449 #
450 # Usage: if ($self->_abseq($x, $y)){ # blah }
451  
452 my ($self, $lhs, $rhs) = @_;
453
454 return (abs($lhs) == abs($rhs));
455  
456 }
457  
458 ######################################################################
459 sub _abslt{
460 ######################################################################
461 # This method returns a boolean to indicate if the first argument is
462 # absolutely less than the second argument.
463 #
464 # Usage: if ($self->_abslt($x, $y)){ # blah }
465  
466 my ($self, $lhs, $rhs) = @_;
467  
468 return (abs($lhs) < abs($rhs));
469  
470 }
471  
472 ######################################################################
473 sub _abslteq{
474 ######################################################################
475 # This method returns a boolean to indicate if the first argument is
476 # absolutely less than or equal to the second argument.
477 #
478 # Usage: if ($self->_abslteq($x, $y)){ # blah }
479  
480 my ($self, $lhs, $rhs) = @_;
481
482 return (abs($lhs) <= abs($rhs));
483  
484 }
485  
486 ######################################################################
487 sub _gt{
488 ######################################################################
489 # This method returns a boolean to indicate if the first argument is
490 # greater than the second argument.
491 #
492 # Usage: if ($self->_gt($x, $y)){ # blah }
493  
494 my ($self, $lhs, $rhs) = @_;
495  
496 return ($lhs > $rhs);
497  
498 }
499  
500 ######################################################################
501 sub _gteq{
502 ######################################################################
503 # This method returns a boolean to indicate if the first argument is
504 # greater than or equal to the second argument.
505 #
506 # Usage: if ($self->_gteq($x, $y)){ # blah }
507  
508 my ($self, $lhs, $rhs) = @_;
509  
510 return ($lhs >= $rhs);
511  
512 }
513  
514 ######################################################################
515 sub _eq{
516 ######################################################################
517 # This method returns a boolean to indicate if the first argument is
518 # equal to the second argument.
519 #
520 # Usage: if ($self->_eq($x, $y)){ # blah }
521  
522 my ($self, $lhs, $rhs) = @_;
523
524 return ($lhs == $rhs)
525  
526 }
527  
528 ######################################################################
529 sub _lteq{
530 ######################################################################
531 # This method returns a boolean to indicate if the first argument is
532 # less than or equal to the second argument.
533 #
534 # Usage: if ($self->_lteq($x, $y)){ # blah }
535  
536 my ($self, $lhs, $rhs) = @_;
537  
538 return ($lhs <= $rhs);
539  
540 }
541  
542 ######################################################################
543 sub _lt{
544 ######################################################################
545 # This method returns a boolean to indicate if the first argument is
546 # less than the second argument.
547 #
548 # Usage: if ($self->_lt($x, $y)){ # blah }
549  
550 my ($self, $lhs, $rhs) = @_;
551  
552 return ($lhs < $rhs);
553  
554 }
555  
556 ######################################################################
557 sub _ne{
558 ######################################################################
559 # This method returns a boolean to indicate if the first argument is
560 # not equal to the second argument.
561 #
562 # Usage: if ($self->_ne($x, $y)){ # blah }
563  
564 my ($self, $lhs, $rhs) = @_;
565
566 return ($lhs != $rhs);
567  
568 }
569  
570 # Utility methods
571  
572 ######################################################################
573 sub _rowAverage{
574 ######################################################################
575 # This method returns the average of the valid entries in the row,
576 # using either the mean or the median, depending on the requested
577 # method. The row is passed in a reference to an array containing the
578 # values for the row. If no mean/median could be calculated, then the
579 # method returns undef.
580  
581 my ($self, $rowRef, $method) = @_;
582
583 my $total = 0;
584 my @data;
585 my $numDatapoints = 0;
586
587 foreach my $column (@{$self->_validColumnsArrayRef}){
588
589 next if ($rowRef->[$column] eq "");
590
591 $total += $rowRef->[$column] if ($method eq "mean");
592 push (@data, $rowRef->[$column]) if ($method eq "median");
593 $numDatapoints++;
594
595 }
596
597 return undef if ($numDatapoints == 0); # could just be a spacer row in the matrix
598
599 return $self->_average($method, \@data, $total, $numDatapoints);
600
601 }
602  
603 ######################################################################
604 sub _average{
605 ######################################################################
606 # This method calculates the average of a set of data, by receiving
607 # the total number of datapoints, and either an array by reference of
608 # all the datavalues, or the sum total of all the datapoints. The
609 # former is required to calculate the median (and is not assumed to be
610 # sorted), the latter to calculate the mean. The method must also be
611 # passed in. The number of datapoints must be non-zero
612  
613 my ($self, $method, $dataRef, $total, $numDatapoints) = @_;
614  
615 my $average;
616  
617 if ($method eq "mean"){
618
619 $average = $total/$numDatapoints;
620
621 }elsif ($method eq "median"){
622
623 $numDatapoints = scalar @{$dataRef};
624  
625 @{$dataRef} = sort {$a <=> $b} @{$dataRef};
626
627 if (!($numDatapoints%2)){ # even number of entries
628 $average = ($dataRef->[$numDatapoints/2] + $dataRef->[($numDatapoints/2)-1]) / 2;
629 }else{ # odd number of entries
630 $average = $dataRef->[int($numDatapoints/2)];
631 }
632
633 }
634  
635 return sprintf($kNumberFormat, $average);
636  
637 }
638  
639 ######################################################################
640 sub _centerRow{
641 ######################################################################
642 # This protected method takes an array reference to a row, and the
643 # average (either mean or median, depending on what was requested),
644 # and subtracts that value from every valid value in the row
645  
646 my ($self, $rowRef, $average) = @_;
647  
648 foreach my $column (@{$self->_validColumnsArrayRef}){
649
650 next if ($rowRef->[$column] eq "");
651  
652 $rowRef->[$column] -= $average;
653  
654 }
655  
656 }
657  
658 ######################################################################
659 sub _calculateMeansAndStdDeviations{
660 ######################################################################
661 # This method expects to receive hashes of the sums of X, the sums of
662 # X squared and the number of datapoints, where the keys for each hash
663 # are the unique identifiers for a series of numbers, whose mean and
664 # standard deviations are to be calculated. It returns references to
665 # hashes that hash the same id's to the means and standard deviations.
666 # It uses the n-1 version of standard deviation. If no standard deviation
667 # can be calculated, then it will be designated as undef.
668
669 my ($self, $sumOfXHashRef, $sumX2HashRef, $numDataPointsHashRef) = @_;
670
671 my (%means, %deviations, $n);
672
673 foreach my $column (keys %{$numDataPointsHashRef}){
674
675 $n = $numDataPointsHashRef->{$column};
676
677 $means{$column} = $sumOfXHashRef->{$column}/$n;
678  
679 if ($n >= 2){
680  
681 $deviations{$column} = sqrt(($n * $sumX2HashRef->{$column} - $sumOfXHashRef->{$column}**2)/($n*($n-1)));
682
683 }else{
684  
685 $deviations{$column} = undef;
686  
687 }
688  
689 }
690
691 return (\%deviations, \%means);
692
693 }
694  
695 ######################################################################
696 sub _calculateBounds{
697 ######################################################################
698 # This method receives two hashes by reference. One is a hash of
699 # means, the other a hash of std deviations. It also receives a
700 # multiplier. It then calculates, and returns as hash references, the
701 # upper and lower bounds for the mean plus or minus that number of
702 # deviations
703  
704 my ($self, $stddevHashRef, $meansHashRef, $deviations, $lineEnding) = @_;
705  
706 my (%upper, %lower, $val);
707  
708 defined $lineEnding && print("Calculating upper and lower bounds for filter.", $lineEnding);
709  
710 foreach my $column (keys %{$meansHashRef}){
711  
712 die "A standard deviation for column $column could not be calculated.\n" if (!defined($stddevHashRef->{$column}));
713  
714 $val = $stddevHashRef->{$column} * $deviations;
715  
716 $upper{$column} = $meansHashRef->{$column} + $val;
717 $lower{$column} = $meansHashRef->{$column} - $val;
718  
719 }
720  
721 return (\%upper, \%lower);
722  
723 }
724  
725 #####################################################################
726 sub _giveOverrideMessage{
727 #####################################################################
728 # This protected utility method can be used by any subclass that
729 # expects its own subclasses to implement certain methods. It can
730 # have stub methods, that simply call this method, which will give a
731 # standard error message saying that the class 'X' must override
732 # method Y.
733  
734 my ($self) = shift;
735  
736 my $class = ref $self;
737  
738 my $caller = (caller(1))[3];
739  
740 my $message = "The method, $caller, was invoked on a $class object.\n";
741 $message .= "That method must be implemented in the $class package.\n";
742 $message .= "\n\nA full stack trace is below:\n\n";
743  
744 confess ($message);
745  
746 }
747  
748 #####################################################################
749 #
750 # PUBLIC SETTER METHODS - can be used by anyone
751 #
752 #####################################################################
753  
754 #####################################################################
755 sub setNumColumnsToReport{
756 #####################################################################
757 # This methods accepts an integer, that indicates the number of
758 # columns after which progress should be indicated, during a
759 # transformation that involves column processing.
760 #
761 # Usage : $matrix->setNumColumnsToReport(50);
762 #
763  
764 my ($self, $num) = @_;
765  
766 if ($num < 1 || $num != int($num)){
767  
768 die "setNumColumnsToReport must be given an integer greater than or equal to 1.";
769  
770 }
771  
772 $self->{$kNumColumnsToReport} = $num;
773  
774 }
775  
776 #####################################################################
777 sub setNumRowsToReport{
778 #####################################################################
779 # This methods accepts an integer, that indicates the number of
780 # rows after which progress should be indicated, during a
781 # transformation that involves row processing.
782 #
783 # Usage : $matrix->setNumRowsToReport(50);
784 #
785  
786 my ($self, $num) = @_;
787  
788 if ($num < 1 || $num != int($num)){
789  
790 die "setNumRowsToReport must be given an integer greater than or equal to 1.";
791  
792 }
793  
794 $self->{$kNumRowsToReport} = $num;
795  
796 }
797  
798 #####################################################################
799 #
800 # PUBLIC GETTER/ACCESSOR METHODS - can be used by anyone
801 #
802 #####################################################################
803  
804 ######################################################################
805 sub numRows{
806 ######################################################################
807 # This public method returns the number of valid rows that are in the
808 # dataMatrix. Subclasses MUST determine the number of rows in the matrix
809 # prior to allowing methods to be called on the object.
810 #
811 # Usage : my $numRows = $dataMatrix->numRows;
812  
813 return scalar @{$_[0]->_validRowsArrayRef};
814  
815 }
816  
817 #######################################################################
818 sub numColumns{
819 #######################################################################
820 # This public method returns the number of valid columns that are in the
821 # dataMatrix. Concrete subclasses must determine the number of columns
822 # in the matrix prior to allowing clients to call methods on the object.
823  
824 return scalar @{$_[0]->_validColumnsArrayRef};
825  
826 }
827  
828 ######################################################################
829 sub errstr{
830 #######################################################################
831 # This method returns the last error string that resulted from a method
832 # call. Calling the method deletes any previously held error string.
833 #
834 # Usage example:
835 #
836 # print "An error occured : ", $matrix->errstr, "\n";
837 #
838  
839 my ($self) = @_;
840  
841 my $error = $self->{$kErrstr};
842  
843 delete $self->{$kErrstr};
844  
845 return $error;
846  
847 }
848  
849 #######################################################################
850 sub allowedOperators{
851 #######################################################################
852 # This public method returns an array of all the allowed operators that
853 # may be used by methods (in subclasses) that employ the operators for
854 # whatever reason.
855 #
856 # Usage : my @operators = $matrix->allowedOperators;
857 #
858  
859 my ($self) = @_;
860  
861 return (sort keys %kAllowedOperators);
862  
863 }
864  
865 =pod
866  
867 =head1 Name
868  
869 Microarray::DataMatrix - abstraction to matrices of microarray data
870  
871 =head1 Abstract and Overall Logic
872  
873 Note : This documentation is for Developers only. Clients of concrete
874 subclasses of this package should have no need to consult this
875 documentation, as the API for those subclasses should be fully
876 documented as part of those subclasses.
877  
878 dataMatrix provides an abstract superclass for a collection of
879 abstract classes pertaining to dealing with matrices. Only in the
880 context of the those other classes is baseDataMatrix useful and
881 meaningful. baseDataMatrix itself provides protected methods for
882 certain primitive operations that can be used by its subclasses, and
883 public methods for which it is required that its immediate subclasses
884 have the same underlying structure to deal with their dataMatrix, such
885 as which rows and columns have not yet been filtered out.
886  
887 The collection of classes are structured like this:
888  
889  
890 dataMatrix
891  
892 /\
893 / \
894 / \
895 ISA / \ ISA
896 / \
897 / \
898 / \
899 smallDataMatrix bigDataMatrix
900 \ /
901 \ /
902 \ /
903 \ /
904 CanBeA \ / CanBeA
905 \ /
906 \ /
907 \ /
908 anySizeDataMatrix
909  
910 |
911 | ISA
912 |
913 ------------------ - - - - - -
914 | | |
915 | | |
916 | | |
917 concreteClassA concreteClassB concreteClassX
918  
919  
920 anySizeDataMatrix provides an abstraction to a dataMatrix whose
921 contents may or may not fit into memory. An object will inherit
922 dynamically, at construction time, from either small- or
923 bigDataMatrix, which know how to deal with a matrix of a particular
924 size. anySizeDataMatrix itself is an abstract class, and will be
925 subclassed by concrete classes dealing with a particular file type of
926 data, which they know how to parse, for example a pclFile. Because
927 development of dataMatrix, smallDataMatrix, bigDataMatrix and
928 anySizeDataMatrix was done as a collection of classes, they are
929 somewhat more intimate with each other than say a concrete subclass of
930 anySizeDataMatrix would be with anySizeDataMatrix itself. While the
931 subclasses do stick to the API, and respect the privacy of attributes
932 and methods, the API was developed simultaneously with the subclasses
933 that were using it. Thus it may not be the cleanest API in the
934 world.....
935  
936 This collection of classes tries to follow the rules that all
937 attributes are preceded by the "$PACKAGE::", in the objects hash.
938 Private attribute names and private methods are preceded by two
939 underscores, protected attributes and protected methods (which can be
940 accessed by subclasses, as well as in $PACKAGE itself) are preceded by
941 a single underscore. Public attributes and methods (which can be
942 accessed anywhere) have no preceding underscores. In actuality, all
943 object attributes are (and should be private). If there is a need for
944 either subclasses or clients to manipulate or access them, then there
945 are provided protected and public methods respectively, for setting or
946 getting the values of the attributes. Disobey this interface at your
947 peril!!!!
948  
949 =head1 Protected Setter/Mutator Methods
950  
951 =head2 _setAutoDump
952  
953 This protected method is used to set the autodump flag, which can be
954 either 1 or 0. This should only be utilized by subclasses, not
955 clients.
956  
957 Usage:
958  
959 $self->_setAutoDump(1);
960  
961 =head2 _setValidColumns
962  
963 This protected setter method receives a reference to a hash, which has
964 as its keys the indexes of the columns in the matrix which are valid.
965 This method MUST be used when the matrix has been first read, to set
966 up all the columns which are initially valid (this call will actually
967 occur in the _init methods (or methods called by them) of big- and
968 smallDataMatrix). The values of the hash will usually be undef, to
969 simply save space. There is no expectation for them to be otherwise.
970  
971 Usage:
972  
973 $self->_setValidColumns(\%validColumns);
974  
975 =head2 _setValidRows
976  
977 This protected setter method receives a reference to a hash, which has
978 as its keys the indexes of the rows in the matrix which are valid.
979 This method is expected to only be used when the matrix has been first
980 read, to set up all the rows which are initially valid (this call will
981 actually occur in the _init methods (or methods called by them) of
982 big- and smallDataMatrix). The values of the hash will usually be
983 undef, to simply save space. There is no expectation for them to be
984 otherwise.
985  
986 Usage:
987  
988 $self->_setValidRows(\%validRows);
989  
990 =head2 _setErrstr
991  
992 This protected setter method accepts a scalar, that will correspond to
993 an error that has occurred, and will store it within the object.
994  
995 Usage:
996  
997 $self->_setErrstr($error);
998  
999 =head2 _invalidateMatrixRow
1000  
1001 This protected mutator method makes a row invalid. This method is not
1002 undoable, because the invalidation also deletes the data for the row.
1003 Note that the row index MUST correspond to the index of that row in
1004 the original file, not whatever row it may currently be (ie if rows 1
1005 and 2 were filtered out, row 3 should still be called row 3 when being
1006 invalidated, not row 1).
1007  
1008 Usage :
1009  
1010 $self->_invalidateMatrixRow($row);
1011  
1012 =head2 _invalidateMatrixColumn
1013  
1014 This protected mutator method makes a column invalid. Note that the
1015 column index MUST correspond to the index of that column in the original
1016 file, not whatever column it may currently be (ie if columns 1 and 2 were
1017 filtered out, column 3 should still be called column 3 when being
1018 invalidated, not column 1).
1019  
1020 Usage :
1021  
1022 $self->_invalidateMatrixColumn($column);
1023  
1024 =head1 PROTECTED GETTER/ACCESSOR METHODS
1025  
1026 =head2 _autoDump
1027  
1028 This protected method returns a boolean to indicate whether
1029 autodumping is enabled.
1030  
1031 Usage:
1032  
1033 if ($self->_autoDump){
1034  
1035 # blah
1036  
1037 }
1038  
1039 =head2 _validRowsArrayRef
1040  
1041 This protected accessor returns a reference to an array that contains
1042 the indexes of all the valid rows
1043  
1044 Usage:
1045  
1046 foreach my $row (@{$self->_validRowsArrayRef}){
1047  
1048 # do something useful
1049  
1050 }
1051  
1052 =head2 _validColumnArrayRef
1053  
1054 This protected accessor returns a reference to an array that contains
1055 the indexes of all the valid columns.
1056  
1057 Usage:
1058  
1059 foreach my $column (@{$self->_validColumnsArrayRef}){
1060  
1061 # do something useful
1062  
1063 }
1064  
1065 =head2 _matrixRowIsValid
1066  
1067 This protected accessor returns a boolean to indicate whether a given
1068 row in the data matrix is still valid (ie has not been filtered out).
1069 The row index is with respect to its index in the original file that
1070 was used to construct the object.
1071  
1072 Usage :
1073  
1074 if ($self->_matrixRowIsValid($row)){ # blah }
1075  
1076 =head2 _matrixColumnIsValid
1077  
1078 This protected accessor returns a boolean to indicate whether a given
1079 column in the data matrix is still valid (ie has not been filtered
1080 out). The column index is with respect to its index in the original
1081 file that was used to construct the object.
1082  
1083 Usage :
1084  
1085 if ($self->_matrixColumnIsValid($column)){ # blah }
1086  
1087 =head2 _numColumnsToReport
1088  
1089 This protected method returns the number of columns to process after
1090 which reporting should be done, if verbose reporting has been
1091 indicated. If no value has been set, then the default of 50 is
1092 returned.
1093  
1094 Usage :
1095  
1096 my $numColumnsToReport = $self->_numColumnsToReport;
1097  
1098 =head2 _numRowsToReport
1099  
1100 This protected method returns the number of rows to process after
1101 which reporting should be done, if verbose reporting has been
1102 indicated. If no value has been set, then the default of 5000 is
1103 returned.
1104  
1105 Usage :
1106  
1107 my $numRowsToReport = $self->_numRowsToReport;
1108  
1109 =head2 _lineEnding
1110  
1111 This protected method returns the appropriate line ending, for text or
1112 html reporting. It expects a string, either 'html' or 'text' and will
1113 return the appropriate line ending.
1114  
1115 Usage:
1116  
1117 my $lineEnding = $self->_lineEnding("text");
1118  
1119 =head2 _centeringMethodIsAllowed
1120  
1121 This protected method returns a boolean to indicate whether a
1122 centering method is allowed. Allowed methods are 'mean' and 'median'.
1123  
1124 Usage :
1125  
1126 if ($self->_centeringMethodIsAllowed($method)){ # blah }
1127  
1128 =head2 _operatorIsAllowed
1129  
1130 This protected method returns a boolean to indicate whether a
1131 particular operator is allowed. For each operator, there exists a
1132 corresponding method that uses that operator. Such operators are used
1133 when filtering rows by there values, eg >, or < etc.
1134  
1135  
1136 Usage :
1137  
1138 if ($self->_operatorIsAllowed($operator)){ # blah }
1139  
1140 =head2 _methodForOperator
1141  
1142 This protected method returns the name of the method that is used to
1143 compare two values, based on the operator that was passed in.
1144  
1145 Usage :
1146  
1147 my $method = $self->_methodForOperator($operator);
1148  
1149 =head1 PROTECTED UTILITY METHODS
1150  
1151 =head2 _rowAverage
1152  
1153 This method returns the average of the valid entries in a row, using
1154 either the mean or the median, depending on the requested method. The
1155 row is passed in as a reference to an array containing the values for
1156 the row. If no mean/median could be calculated, then the method
1157 returns undef. Only values at validRowIndexes within the passed in
1158 array are used in the calculation.
1159  
1160 Usage:
1161  
1162 my $average = $self->_rowAverage(\@row, "mean");
1163  
1164 =head2 _average
1165  
1166 This method calculates either the mean or median of a set of data, by
1167 receiving the total number of datapoints, an array by reference of all
1168 the datavalues, and the sum total of all the datapoints. The former
1169 is required to calculate the median (and is not assumed to be sorted),
1170 the latter to calculate the mean. The method must also be passed in.
1171 The number of datapoints must be non-zero.
1172  
1173 Usage:
1174  
1175 my $average = $self->_average("mean", \@data, $total, $numDatapoints);
1176  
1177 =head2 _centerRow
1178  
1179 This protected method takes an array reference to a row, and the
1180 average (either mean or median, depending on what was requested), and
1181 subtracts that value from every valid value (ie for the valid column
1182 indexes) in the row.
1183  
1184 Usage:
1185  
1186 $self->_centerRow(\@row, $average);
1187  
1188 =head2 _calculateMeansAndStdDeviations
1189  
1190 This method expects to receive hashes of the sums of X, the sums of X
1191 squared and the number of datapoints, where the keys for each hash are
1192 the unique identifiers for a series of numbers, whose mean and
1193 standard deviations are to be calculated. It returns references to
1194 hashes that hash the same ids to the means and standard deviations.
1195 It uses the n-1 version of standard deviation. If a standard
1196 deviation cannot be calculated, it will be stored as undef.
1197  
1198 Usage:
1199  
1200 my ($stddevHashRef, $meansHashRef) = $self->_calculateMeansAndStdDeviations(\%sumOfX, \%sumX2, \%numDataPoints);
1201  
1202 =head2 _calculateBounds
1203  
1204 This method receives two hashes by reference. One is a hash of means,
1205 the other a hash of std deviations. It also receives a multiplier.
1206 It then calculates, and returns as hash references, the upper and
1207 lower bounds for the mean plus or minus that number of deviations. It
1208 also receives what line ending it should be using, if being verbose in
1209 its reporting.
1210  
1211 Usage:
1212  
1213 my ($upperHashRef, $lowerHashRef) = $self->_calculateBounds($stddevHashRef, $meansHashRef, $deviations, $lineEnding);
1214  
1215 =head2 _giveOverrideMessage
1216  
1217 This protected utility method can be used by any subclass that expects
1218 its own subclasses to implement certain methods. It can have stub
1219 methods, that simply call this method, which will give a standard
1220 error message saying that the class 'X' must override method Y.
1221  
1222 Usage:
1223  
1224 $self->_giveOverrideMessage();
1225  
1226 =head1 PUBLIC ACCESSOR METHODS
1227  
1228 =head2 allowedOperators
1229  
1230 This public method returns a sorted array of all the allowed operators that
1231 may be used by methods (in subclasses) that employ the operators for
1232 whatever reason (their interface should indicate that they employ such
1233 operators).
1234  
1235 Usage :
1236  
1237 my @operators = $matrix->allowedOperators;
1238  
1239 =head1 PUBLIC SETTER METHODS
1240  
1241 =head2 setNumColumnsToReport
1242  
1243 This method accepts a positive integer, that indicates the number of
1244 columns that have been processed during a filtering/transformation
1245 method that is carried out on a column basis, after which progress
1246 should be indicated. If a client has not set this value, then it
1247 defaults to 50.
1248  
1249 Usage :
1250  
1251 $matrix->setNumColumnsToReport(50);
1252  
1253 =head2 setNumRowsToReport
1254  
1255 This method accepts a positive integer, that indicates the number of
1256 rows that have been processed during a filtering/transformation method
1257 that is carried out on a row basis, after which progress should be
1258 indicated. If a client has not set this value, then it defaults to
1259 5000.
1260  
1261 Usage :
1262  
1263 $matrix->setNumRowsToReport(5000);
1264  
1265 =head1 AUTHOR
1266  
1267 Gavin Sherlock
1268  
1269 sherlock@genome.stanford.edu
1270  
1271 =cut