| 1 | package Microarray::DataMatrix::PclFile; | ||
| 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 : 3rd April 2002 | ||
| 31 | |||
| 32 | # This package is intended to provide an abstraction layer to a | ||
| 33 | # pclFile, whose format is detailed at: | ||
| 34 | # | ||
| 35 | # http://genome-www5.Stanford.EDU/MicroArray/help/formats.shtml#pcl | ||
| 36 | # | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use vars qw (@ISA); | ||
| 40 | |||
| 41 | use Microarray::DataMatrix::TabDelimitedDataMatrix; | ||
| 42 | @ISA = qw (Microarray::DataMatrix::TabDelimitedDataMatrix); | ||
| 43 | |||
| 44 | use Microarray::Config; | ||
| 45 | |||
| 46 | # CLASS GLOBALS | ||
| 47 | |||
| 48 | # use the following so we only ever access hash entries | ||
| 49 | # using variables, to prevent typos, or accidental cloberring. | ||
| 50 | |||
| 51 | # These values are assigned here and then are READ-ONLY. | ||
| 52 | |||
| 53 | my $PACKAGE = "Microarray::DataMatrix::PclFile"; | ||
| 54 | |||
| 55 | my $kRowNames = $PACKAGE.'::__rowNames'; # array of row names | ||
| 56 | my $kRowDescs = $PACKAGE.'::__rowDescs'; # array of row descriptions | ||
| 57 | my $keWeights = $PACKAGE.'::__eweights'; # array of eweights in the file | ||
| 58 | my $kgWeights = $PACKAGE.'::__gWeights'; # array of gweights in the file | ||
| 59 | my $kIdName = $PACKAGE.'::__idName'; # name of the first column | ||
| 60 | my $kDescName = $PACKAGE.'::__descName'; # name of the second column | ||
| 61 | |||
| 62 | ###################################################################### | ||
| 63 | sub new{ | ||
| 64 | ###################################################################### | ||
| 65 | # This is the constructor. It requires that the filename of the pcl | ||
| 66 | # file that is being used to construct the object is passed in. In | ||
| 67 | # addition you can set a variable, autodump, to indicate whether data | ||
| 68 | # should be automatically dumped out after any method call that | ||
| 69 | # transforms or filters the data. If this is not set, then you must | ||
| 70 | # manually call the dumpData method. The default is for autodumping | ||
| 71 | # to be off. This is useful, for instance, because you may run | ||
| 72 | # several filters/transformations over the data, and only want to dump | ||
| 73 | # the data at the end, which is more optimal that dumping it at every | ||
| 74 | # step. | ||
| 75 | # | ||
| 76 | # Usage : | ||
| 77 | # | ||
| 78 | # my $pclFile = pclFile->new(file=>$file, | ||
| 79 | # autodump=>1); | ||
| 80 | # | ||
| 81 | # The constructor simply sets up the object attributes. It does | ||
| 82 | # not read in any data. This will be done on demand, if a method | ||
| 83 | # is called that requires the data to be read in. | ||
| 84 | |||
| 85 | my $self = {}; | ||
| 86 | |||
| 87 | my ($class, %args) = @_; | ||
| 88 | |||
| 89 | bless $self, $class; | ||
| 90 | |||
| 91 | $self->_setFile($args{'file'}); | ||
| 92 | $self->{$kRowNames} = []; | ||
| 93 | $self->{$kRowDescs} = []; | ||
| 94 | $self->{$kgWeights} = []; | ||
| 95 | |||
| 96 | $self->SUPER::_init($args{'tmpDir'}); # required by the anySizeMatrixClass | ||
| 97 | |||
| 98 | $self->_setAutoDump($args{'autodump'}) if (exists($args{'autodump'})); | ||
| 99 | |||
| 100 | return $self; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | ##################################################################### | ||
| 105 | sub __usage{ | ||
| 106 | ##################################################################### | ||
| 107 | # This private method prints out a usage message for the constructor, | ||
| 108 | # and then dies with a specific error message. | ||
| 109 | # | ||
| 110 | # Usage : $self->__usage("$file does not exist"); | ||
| 111 | |||
| 112 | my ($self, $error) = @_; | ||
| 113 | |||
| 114 | print STDERR ' | ||
| 115 | Usage: | ||
| 116 | |||
| 117 | my $dataMatrix = pclFile->new(file=>$file, | ||
| 118 | autoDump=>1, | ||
| 119 | tmpDir=>$dir); | ||
| 120 | |||
| 121 | where the file is a pcl file, formatted according to : | ||
| 122 | |||
| 123 | http://genome-www5.Stanford.EDU/MicroArray/help/formats.shtml#pcl | ||
| 124 | |||
| 125 | '; | ||
| 126 | |||
| 127 | |||
| 128 | die "$error.\n"; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | ###################################################################### | ||
| 133 | sub _numLeadingMetaLines{ | ||
| 134 | ###################################################################### | ||
| 135 | # This method returns the number of leading meta lines that exist in | ||
| 136 | # the pcl file, which is 2. | ||
| 137 | |||
| 138 | return (2); | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | ###################################################################### | ||
| 143 | sub _parseFileHeaders{ | ||
| 144 | ###################################################################### | ||
| 145 | # This private method removes the file headers from a pcl file. It | ||
| 146 | # accepts a file handle as input, and assumes that the file handle is | ||
| 147 | # pointing to the very beginning of the file. | ||
| 148 | |||
| 149 | my ($self, $fh) = @_; | ||
| 150 | |||
| 151 | my $line = <$fh>; | ||
| 152 | |||
| 153 | if (!exists($self->{$kIdName})){ # if we've not read this line before | ||
| 154 | |||
| 155 | my $lineRef = $self->_arrayRefForLine(\$line); | ||
| 156 | |||
| 157 | $self->__parseFirstLine($lineRef); | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | $line = <$fh>; | ||
| 162 | |||
| 163 | if (!exists $self->{$keWeights}){ # the second line has the EWEIGHTs | ||
| 164 | |||
| 165 | my $lineRef = $self->_arrayRefForLine(\$line); | ||
| 166 | |||
| 167 | $self->__parseSecondLine($lineRef); | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | # now advance the data line row count to zero, as the next line | ||
| 172 | # to be read in will be the first data line | ||
| 173 | |||
| 174 | $self->_setCurrFileDataRow(0); | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | ###################################################################### | ||
| 179 | sub __parseFirstLine{ | ||
| 180 | ###################################################################### | ||
| 181 | # This method deals with recording data from the first line of a pcl | ||
| 182 | # file. It accepts as input a reference to an array that contains | ||
| 183 | # the fields on the first line split on tabs. | ||
| 184 | |||
| 185 | my ($self, $lineRef) = @_; | ||
| 186 | |||
| 187 | $self->idName (shift @{$lineRef}); | ||
| 188 | $self->descName(shift @{$lineRef}); | ||
| 189 | |||
| 190 | if (shift @{$lineRef} ne "GWEIGHT"){ | ||
| 191 | |||
| 192 | $self->__usage("Your file, ".$self->file." has no GWEIGHT column header"); | ||
| 193 | |||
| 194 | } | ||
| 195 | |||
| 196 | $self->columnNamesArrayRef($lineRef); | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | ###################################################################### | ||
| 201 | sub __parseSecondLine{ | ||
| 202 | ###################################################################### | ||
| 203 | # This method deals with recording data from the second line of a pcl | ||
| 204 | # file. It accepts as input a reference to an array that contains | ||
| 205 | # the fields on the second line split on tabs. | ||
| 206 | |||
| 207 | my ($self, $lineRef) = @_; | ||
| 208 | |||
| 209 | # simply record the EWEIGHTS | ||
| 210 | |||
| 211 | $self->eweightsArrayRef([@{$lineRef}[3..$#$lineRef]]); # anonymous array reference | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | ###################################################################### | ||
| 216 | sub _removeMetaDataFromLine{ | ||
| 217 | ###################################################################### | ||
| 218 | # This method removes and stores meta data from a data line in a pcl file. | ||
| 219 | # Because the array of data is passed in by reference, the array to which | ||
| 220 | # this reference refers is directly manipulated, such that the caller's | ||
| 221 | # array will have meta data removed from it | ||
| 222 | |||
| 223 | my ($self, $currFileDataRow, $lineRef) = @_; | ||
| 224 | |||
| 225 | if ($self->_metaDataRecorded($currFileDataRow)){ | ||
| 226 | |||
| 227 | # if we've already recorded the meta data for this line number, | ||
| 228 | # simply remove the data from the line | ||
| 229 | |||
| 230 | shift @{$lineRef}; shift @{$lineRef}; shift @{$lineRef}; | ||
| 231 | |||
| 232 | }else{ | ||
| 233 | |||
| 234 | # otherwise record the data, and then remember that we've recorded it | ||
| 235 | |||
| 236 | $self->rowName($currFileDataRow, shift @{$lineRef}); | ||
| 237 | $self->rowDesc($currFileDataRow, shift @{$lineRef}); | ||
| 238 | $self->gWeight($currFileDataRow, shift @{$lineRef}); | ||
| 239 | |||
| 240 | $self->_indicateMetaDataRecorded($currFileDataRow); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | ###################################################################### | ||
| 247 | sub rowName{ | ||
| 248 | ###################################################################### | ||
| 249 | # This polymorphic setter/getter method returns the row name for a given | ||
| 250 | # row in the pcl file. If a new name is provided, it will update that | ||
| 251 | # name to the new value. Note the row number that is passed in is based | ||
| 252 | # on the row number in the file used for object construction. | ||
| 253 | # | ||
| 254 | # Usage: | ||
| 255 | # | ||
| 256 | # $self->rowName($rowNum, $newName); | ||
| 257 | # | ||
| 258 | # my $rowName = $self->rowName($rowNum); | ||
| 259 | |||
| 260 | my ($self, $rowNum, $data) = @_; | ||
| 261 | |||
| 262 | defined ($data) && ($self->{$kRowNames}->[$rowNum] = $data); | ||
| 263 | |||
| 264 | return $self->{$kRowNames}->[$rowNum]; | ||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | ###################################################################### | ||
| 269 | sub rowDesc{ | ||
| 270 | ###################################################################### | ||
| 271 | # This polymorphic setter/getter method returns the row description | ||
| 272 | # for a given row in the pcl file. If a new description is provided, | ||
| 273 | # it will update that description to the new value. Note the row | ||
| 274 | # number that is passed in is based on the row number in the file used | ||
| 275 | # for object construction. | ||
| 276 | # | ||
| 277 | # Usage: | ||
| 278 | # | ||
| 279 | # $self->rowDesc($rowNum, $newDesc); | ||
| 280 | # | ||
| 281 | # my $rowDesc = $self->rowDesc($rowNum); | ||
| 282 | |||
| 283 | my ($self, $rowNum, $data) = @_; | ||
| 284 | |||
| 285 | defined ($data) && ($self->{$kRowDescs}->[$rowNum] = $data); | ||
| 286 | |||
| 287 | return $self->{$kRowDescs}->[$rowNum]; | ||
| 288 | |||
| 289 | } | ||
| 290 | |||
| 291 | ###################################################################### | ||
| 292 | sub gWeight{ | ||
| 293 | ###################################################################### | ||
| 294 | # This polymorphic setter/getter method returns the gWeight for a | ||
| 295 | # given row in the pcl file. If a new gWeight is provided, it will | ||
| 296 | # update that gWeight to the new value. Note the row number that is | ||
| 297 | # passed in is based on the row number in the file used for object | ||
| 298 | # construction. | ||
| 299 | # | ||
| 300 | # Usage: | ||
| 301 | # | ||
| 302 | # $self->gWeight($rowNum, $newGWeight); | ||
| 303 | # | ||
| 304 | # my $gWeight = $self->gWeight($rowNum); | ||
| 305 | |||
| 306 | my ($self, $rowNum, $data) = @_; | ||
| 307 | |||
| 308 | defined ($data) && ($self->{$kgWeights}->[$rowNum] = $data); | ||
| 309 | |||
| 310 | return $self->{$kgWeights}->[$rowNum]; | ||
| 311 | |||
| 312 | } | ||
| 313 | |||
| 314 | ###################################################################### | ||
| 315 | sub eweightsArrayRef{ | ||
| 316 | ###################################################################### | ||
| 317 | # This polymorphic setter/getter method returns a reference to an | ||
| 318 | # array of the eweights that existed in the original file, in the | ||
| 319 | # order that they appeared. If a new array reference is provided, it | ||
| 320 | # will give the experiments new eweights. | ||
| 321 | # | ||
| 322 | # Usage: | ||
| 323 | # | ||
| 324 | # my $eweightsArrayRef = $self->eweightsArrayRef; | ||
| 325 | # $self->eweightsArrayRef(\@eweights); | ||
| 326 | |||
| 327 | my ($self, $data) = @_; | ||
| 328 | |||
| 329 | defined ($data) && ($self->{$keWeights} = $data); | ||
| 330 | |||
| 331 | return $self->{$keWeights}; | ||
| 332 | |||
| 333 | } | ||
| 334 | |||
| 335 | ###################################################################### | ||
| 336 | sub idName{ | ||
| 337 | ###################################################################### | ||
| 338 | # This polymorphic setter/getter method returns the name of the id | ||
| 339 | # column that was used in the original file. If a new value is passed | ||
| 340 | # in, it will instead use that value as the id column name. | ||
| 341 | # | ||
| 342 | # Usage: | ||
| 343 | # | ||
| 344 | # print $self->idName; | ||
| 345 | # $self->idName($idName); | ||
| 346 | |||
| 347 | my ($self, $data) = @_; | ||
| 348 | |||
| 349 | defined ($data) && ($self->{$kIdName} = $data); | ||
| 350 | |||
| 351 | return $self->{$kIdName}; | ||
| 352 | |||
| 353 | } | ||
| 354 | |||
| 355 | ###################################################################### | ||
| 356 | sub descName{ | ||
| 357 | ###################################################################### | ||
| 358 | # This method returns the name of the description column that was used | ||
| 359 | # in the original file | ||
| 360 | |||
| 361 | my ($self, $data) = @_; | ||
| 362 | |||
| 363 | defined ($data) && ($self->{$kDescName} = $data); | ||
| 364 | |||
| 365 | return $_[0]->{$kDescName}; | ||
| 366 | |||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | ###################################################################### | ||
| 371 | sub _printLeadingMeta{ | ||
| 372 | ###################################################################### | ||
| 373 | # This method prints the first two lines of a pcl file out. | ||
| 374 | # | ||
| 375 | # Usage : $self->_printLeadingMeta($fh, $validColumnsArrayRef, $hasExtraInfo, $extraInfoName); | ||
| 376 | # | ||
| 377 | # This method prints to the passed in file handle. It will only print | ||
| 378 | # meta information for those valid columns. If the $extraInfo | ||
| 379 | # variable is true (ie a non-zero value) it will leave a print a | ||
| 380 | # column called $extraInfoName in between each column of meta data. | ||
| 381 | |||
| 382 | my ($self, $fh, $validColumnsArrayRef, $hasExtraInfo, $extraInfoName) = @_; | ||
| 383 | |||
| 384 | # print the experiment names line | ||
| 385 | |||
| 386 | print $fh $self->idName, "\t", $self->descName, "\tGWEIGHT"; | ||
| 387 | |||
| 388 | my $columnNamesArrayRef = $self->columnNamesArrayRef; | ||
| 389 | |||
| 390 | foreach my $column (sort {$a <=> $b } @{$validColumnsArrayRef}){ | ||
| 391 | |||
| 392 | print $fh "\t", $columnNamesArrayRef->[$column]; | ||
| 393 | |||
| 394 | $hasExtraInfo && print($fh "\t$extraInfoName"); | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | print $fh "\n"; | ||
| 399 | |||
| 400 | # now print the eweights line | ||
| 401 | |||
| 402 | print $fh "EWEIGHT\t\t"; | ||
| 403 | |||
| 404 | my $eweightsArrayRef = $self->eweightsArrayRef; | ||
| 405 | |||
| 406 | foreach my $column (sort {$a <=> $b } @{$validColumnsArrayRef}){ | ||
| 407 | |||
| 408 | print $fh "\t", $eweightsArrayRef->[$column]; | ||
| 409 | |||
| 410 | $hasExtraInfo && print($fh "\t"); | ||
| 411 | |||
| 412 | } | ||
| 413 | |||
| 414 | print $fh "\n"; | ||
| 415 | |||
| 416 | } | ||
| 417 | |||
| 418 | ###################################################################### | ||
| 419 | sub _printRowMetaData{ | ||
| 420 | ###################################################################### | ||
| 421 | # This method (required by the tabDelimitedDataMatrix) prints out the | ||
| 422 | # meta data for a single row, to a passed in file handle | ||
| 423 | # | ||
| 424 | # Usage : $self->_printRowMetaData($fh, $row); | ||
| 425 | |||
| 426 | my ($self, $fh, $row) = @_; | ||
| 427 | |||
| 428 | print $fh $self->rowName($row), "\t", $self->rowDesc($row), "\t", $self->gWeight($row); | ||
| 429 | |||
| 430 | } | ||
| 431 | |||
| 432 | sub _printTrailingMeta{ | ||
| 433 | |||
| 434 | # nothing needs to be done | ||
| 435 | |||
| 436 | } | ||
| 437 | |||
| 438 | ###################################################################### | ||
| 439 | sub createCorrelationsFile{ | ||
| 440 | ###################################################################### | ||
| 441 | # This method creates a 'correlations' file from a pcl file | ||
| 442 | # | ||
| 443 | # Usage: The following arguments are allowed: | ||
| 444 | # | ||
| 445 | # | ||
| 446 | # corr <uncentered|centered> whether you want an uncentered or a centered metric. | ||
| 447 | # uncentered is the default | ||
| 448 | # | ||
| 449 | # cutoff Allows you to specify a cutoff, correlations above which will be stored | ||
| 450 | # | ||
| 451 | # num Allows you to specify the number of correlations, up to 50, which you would like to store | ||
| 452 | # 50 is the default | ||
| 453 | # | ||
| 454 | # file Allows you to specify the fully qualified file name of the output file, to which the correlations will be written. | ||
| 455 | # A .stdCor extension will be added. | ||
| 456 | # default is to use the name of the input file, but with a .stdCor extension, if no file is | ||
| 457 | # supplied | ||
| 458 | |||
| 459 | my ($self, %args) = @_; | ||
| 460 | |||
| 461 | my %corr = ("uncentered" => 1, | ||
| 462 | "centered" => 2); | ||
| 463 | |||
| 464 | my @args; | ||
| 465 | |||
| 466 | if (exists($args{'corr'})){ | ||
| 467 | |||
| 468 | if (exists($corr{$args{'corr'}})){ | ||
| 469 | |||
| 470 | push (@args, ("-corr", $corr{$args{'corr'}})); | ||
| 471 | |||
| 472 | }else{ | ||
| 473 | |||
| 474 | die "$args{'corr'} is not a valid choice for the 'corr' argument.\n". | ||
| 475 | "Only :\n\n".join("\n", keys %corr)."\n\n are valid"; | ||
| 476 | |||
| 477 | } | ||
| 478 | |||
| 479 | } | ||
| 480 | |||
| 481 | if (exists($args{'cutoff'})){ | ||
| 482 | |||
| 483 | if ($args{'cutoff'} < 1 && $args{'cutoff'} >= -1){ | ||
| 484 | |||
| 485 | push (@args, ("-cutoff", $args{'cutoff'})); | ||
| 486 | |||
| 487 | }else{ | ||
| 488 | |||
| 489 | die "$args{'cutoff'} is not a valid value for the 'cutoff' argument.\n". | ||
| 490 | "The value must be less than 1, and greater than or equal to zero."; | ||
| 491 | |||
| 492 | } | ||
| 493 | |||
| 494 | } | ||
| 495 | |||
| 496 | if (exists($args{'num'})){ | ||
| 497 | |||
| 498 | if ($args{'num'} >=1 && $args{'num'} <=50 && int($args{'num'}) == $args{'num'}){ | ||
| 499 | |||
| 500 | push (@args, ("-num", $args{'num'})); | ||
| 501 | |||
| 502 | }else{ | ||
| 503 | |||
| 504 | die "$args{'num'} is not a valid choice for the num argument.\n". | ||
| 505 | "The value must be an integer between 1 and 50 inclusive.\n"; | ||
| 506 | |||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | # now run the correlations maker - we assume that the binary | ||
| 511 | # 'correlations' program is in our path | ||
| 512 | |||
| 513 | push (@args, ("-f", $self->file)); | ||
| 514 | |||
| 515 | my $command = 'correlations '.join(' ', @args); | ||
| 516 | |||
| 517 | my $error = system("$command 2>&1"); | ||
| 518 | |||
| 519 | if ($error){ | ||
| 520 | |||
| 521 | die "An error occured running the correlations program : system call returns $error.\n"; | ||
| 522 | |||
| 523 | }else{ | ||
| 524 | |||
| 525 | # something here? | ||
| 526 | |||
| 527 | } | ||
| 528 | |||
| 529 | } | ||
| 530 | |||
| 531 | 1; # to keep perl happy | ||
| 532 | |||
| 533 | =pod | ||
| 534 | |||
| 535 | =head1 NAME | ||
| 536 | |||
| 537 | Microarray::DataMatrix::PclFile - abstraction to pcl file | ||
| 538 | |||
| 539 | =head1 Abstract | ||
| 540 | |||
| 541 | pclFile.pm provides an abstraction layer to a pcl file, in that | ||
| 542 | it provides methods for manipulating or querying the contents of a | ||
| 543 | pcl file. | ||
| 544 | |||
| 545 | =head1 Overall Logic | ||
| 546 | |||
| 547 | This is for programmers only - do not rely on any of these details | ||
| 548 | when programming clients of the object, as the underlying implementation | ||
| 549 | is subject to change at any time without notice. | ||
| 550 | |||
| 551 | The pclFile does little more than providing the parsing ability for | ||
| 552 | pcl files. All filtering/transformations are taken care of by | ||
| 553 | anyDataMatrix, from which it inherits. | ||
| 554 | |||
| 555 | =head1 Public Methods | ||
| 556 | |||
| 557 | =head2 new | ||
| 558 | |||
| 559 | Constructor - will take a fully qualified filename that correspond to | ||
| 560 | a pcl file as a name argument - see - | ||
| 561 | http://genome-www5.stanford.edu/MicroArray/help/formats.shtml. In | ||
| 562 | addition you can set a variable, autodump, to indicate whether data | ||
| 563 | should be automatically dumped out after any method call that | ||
| 564 | transforms or filters the data. If this is not set, then you must | ||
| 565 | manually call the dumpData method. The default is for autodumping to | ||
| 566 | be off. This is useful, for instance, because you may run several | ||
| 567 | filters/transformations over the data, and only want to dump the data | ||
| 568 | at the end, which is more optimal that dumping it at every step. Note | ||
| 569 | that if autodumping is on, then the data are only dumped if a method | ||
| 570 | completes successfully. Currently, if a method fails, the matrix may | ||
| 571 | be left in an uncertain state, and should not be used further. | ||
| 572 | |||
| 573 | Construction requires that a directory be provided where temp files | ||
| 574 | may be written, which may be generated during matrix filtering and | ||
| 575 | transformation. | ||
| 576 | |||
| 577 | Usage: | ||
| 578 | |||
| 579 | my $pclFile = pclFile->new(file=>$file, | ||
| 580 | autodump=>1, | ||
| 581 | tmpDir=>$tmpDir); | ||
| 582 | |||
| 583 | returns : a pclFile object. | ||
| 584 | |||
| 585 | =head2 dumpData | ||
| 586 | |||
| 587 | This method dumps the current contents of the pclFile object to a | ||
| 588 | file, either whose name was provided as a single argument, or to a | ||
| 589 | file whose name was used to construct the object. If the data have | ||
| 590 | been filtered based on columnPercentiles, and these were elected to be | ||
| 591 | shown, then these will be dumped out too (see below). | ||
| 592 | |||
| 593 | Usage: | ||
| 594 | |||
| 595 | $pclFile->dumpData($file); | ||
| 596 | |||
| 597 | or: | ||
| 598 | |||
| 599 | $pclFile->dumpData; | ||
| 600 | |||
| 601 | =head1 Tranformation and Filtering Methods | ||
| 602 | |||
| 603 | General note on methods that transform the data : If autodumping is | ||
| 604 | on, then by default, they will overwrite the file that was used to | ||
| 605 | create the pclFile object, unless a new filename is passed in. If | ||
| 606 | a new filename is passed in (as an argument named 'file'), and | ||
| 607 | autodumping is on, then further operations on the pclFile of | ||
| 608 | filtered data will require instantiation of a pclFile object with | ||
| 609 | that file. Note, this also means that the program MUST have | ||
| 610 | permissions to overwrite the file, and also to write to the same | ||
| 611 | directory as the file (for temp file purposes). | ||
| 612 | |||
| 613 | All of the transformation and filtering methods return 1 upon success. | ||
| 614 | If an error was encountered, then the method will return 0, and the | ||
| 615 | error message associated with the problem can be retrieved using the | ||
| 616 | errstr() method, eg: | ||
| 617 | |||
| 618 | $pclFile->methodX(%args) || die "An error occured ".$pclFile->errstr."\n"; | ||
| 619 | |||
| 620 | All of the transformation and filtering methods allow a verbose | ||
| 621 | argument to be passed in, with valid values for the verbose argument | ||
| 622 | being either 'text' or 'html'. For text, \n will be used as an end of | ||
| 623 | line character after every line of reporting is printed. For html, | ||
| 624 | \n<br> will be used, eg: | ||
| 625 | |||
| 626 | $pclFile->center(rows=>'mean', | ||
| 627 | verbose=>'html') || die $pclFile->errstr; | ||
| 628 | |||
| 629 | =head2 center | ||
| 630 | |||
| 631 | This method allows either rows or columns of the pclFile to be | ||
| 632 | centered using either means or medians (centering is when the average | ||
| 633 | - mean or median - is set to zero, by subtracting the average from | ||
| 634 | every value for that row/column). If centering both rows and columns, | ||
| 635 | centering will be done iteratively, until no datapoint changes by more | ||
| 636 | than 0.01. Alternatively, the maxNumIterations can be specified, or | ||
| 637 | the maxAllowableChange can be specified. If used in combination, the | ||
| 638 | first one that is met will terminate centering. The defaults are: | ||
| 639 | |||
| 640 | maxAllowableChange 0.01 | ||
| 641 | maxNumIterations 10 | ||
| 642 | |||
| 643 | Usage: eg: | ||
| 644 | |||
| 645 | $pclFile->center(rows=>'mean', | ||
| 646 | columns=>'median') || die $pclFile->errstr; | ||
| 647 | |||
| 648 | |||
| 649 | returns : 1 upon success, or 0 otherwise | ||
| 650 | |||
| 651 | =head2 filterByPercentPresentData | ||
| 652 | |||
| 653 | This method allows for filtering out of rows or columns which do not | ||
| 654 | have greater than the specified percentage of data available. Note, | ||
| 655 | if filtering by both rows and columns, filtering will be done | ||
| 656 | sequentially, firstly by rows. To overide this, make two seperate | ||
| 657 | calls to the method, in the opposite order. There is no fancy | ||
| 658 | algorithm to maximize the amount of retained data (eg consider filter | ||
| 659 | by rows, then by columns, that removal of a column means that some | ||
| 660 | rows thrown out in the first step may have greater than 80% good data | ||
| 661 | for the remaining columns - this method does not consider this). | ||
| 662 | |||
| 663 | $pclFile->filterByPercentPresentData(rows=>80, | ||
| 664 | columns=>80); | ||
| 665 | |||
| 666 | $pclFile->filterByPercentPresentData(rows=>90, | ||
| 667 | file=>$filename); | ||
| 668 | |||
| 669 | returns: 1 upon success, or 0 otherwise | ||
| 670 | |||
| 671 | =head2 filterRowsOnColumnPercentile | ||
| 672 | |||
| 673 | This method will filter out rows whose values do not have a percentile | ||
| 674 | rank for their particular column above a specified percentile rank, in | ||
| 675 | at least numColumns columns. In addition, this method will accept a | ||
| 676 | 'showPercentile' argument, which if set to a non-zero value, will | ||
| 677 | result in the percentiles of the datapoints being dumped out with the | ||
| 678 | data, when the data are aubsequently dumped to a file. Columns of | ||
| 679 | percentiles are interleaved with the data columns, so the resulting | ||
| 680 | file can not be clustered. | ||
| 681 | |||
| 682 | Usage: | ||
| 683 | |||
| 684 | $pclFile->filterRowsOnColumnPercentile(percentile=>95, | ||
| 685 | numColumns=>1, | ||
| 686 | showPercentiles=>1); | ||
| 687 | |||
| 688 | |||
| 689 | returns : 1 upon success, or 0 otherwise | ||
| 690 | |||
| 691 | =head2 filterRowsOnColumnDeviation | ||
| 692 | |||
| 693 | This method will filter out rows whose values do not deviate from the | ||
| 694 | column mean by a specified number of standard deviations, in at least | ||
| 695 | numColumns columns. | ||
| 696 | |||
| 697 | Usage: | ||
| 698 | |||
| 699 | $pclFile->filterRowsOnColumnDeviation(deviations=>2, | ||
| 700 | numColumns=>1); | ||
| 701 | |||
| 702 | returns : 1 upon success, or 0 otherwise | ||
| 703 | |||
| 704 | =head2 filterRowsOnValues | ||
| 705 | |||
| 706 | This method filters out rows whose values do not pass a specified | ||
| 707 | criterion, in at least numColumns columns. To specify the criterion, | ||
| 708 | a value, and an operator must be specified. The valid operators are: | ||
| 709 | |||
| 710 | "absolute value >" also aliased by "absgt" and "|>|" | ||
| 711 | "absolute value >=" also aliased by "absgteq" and "|>=|" | ||
| 712 | "absolute value =" also aliased by "abseq" and "|=|" | ||
| 713 | "absolute value <" also aliased by "abslt" and "|<|" | ||
| 714 | "absolute value <=" also aliased by "abslteq" and "|<=|" | ||
| 715 | ">" also aliased by "gt" | ||
| 716 | ">=" also aliased by "gteq" | ||
| 717 | "=" also aliased by "eq" and "==" | ||
| 718 | "<=" also aliased by "lteq" | ||
| 719 | "<" also aliased by "lt" | ||
| 720 | "not equal" also aliased by "ne" and "!=" | ||
| 721 | |||
| 722 | |||
| 723 | Usage: | ||
| 724 | |||
| 725 | $pclFile->filterRowsOnValues(operator=>"absolute value >", | ||
| 726 | value=>2, | ||
| 727 | numColumns=>1); | ||
| 728 | |||
| 729 | |||
| 730 | returns : 1 upon success, or 0 otherwise | ||
| 731 | |||
| 732 | =head2 filterRowsOnVectorLength | ||
| 733 | |||
| 734 | This method filters out rows based on whether the vector that their | ||
| 735 | values define has a length of greater than the specified length. | ||
| 736 | |||
| 737 | Usage: | ||
| 738 | |||
| 739 | $pclFile->filterRowsOnVectorLength(length=>2); | ||
| 740 | |||
| 741 | returns : 1 upon success, or 0 otherwise | ||
| 742 | |||
| 743 | =head2 logTransformData | ||
| 744 | |||
| 745 | This method log transforms the contents of the data matrix, using the | ||
| 746 | specified base. If any values less than or equal to zero are | ||
| 747 | encountered, then the transformation will fail. The matrix will be | ||
| 748 | returned to its state prior to log transformation if the operation | ||
| 749 | fails. | ||
| 750 | |||
| 751 | Usage : | ||
| 752 | |||
| 753 | $pclFile->logTransformData(base=>2); | ||
| 754 | |||
| 755 | returns : 1 upon success, or 0 otherwise | ||
| 756 | |||
| 757 | =head2 scaleColumnData | ||
| 758 | |||
| 759 | This method scales the data for particular columns as specified by the | ||
| 760 | client, by dividing the values by specified factors. It could, for | ||
| 761 | instance, be used to renormalize the data. Note it is only | ||
| 762 | appropriate to normalize ratio data, not log transformed data. | ||
| 763 | |||
| 764 | The client passes in a hash, by reference, of the column numbers | ||
| 765 | (starting from zero) as the keys, and the scaling factors as the | ||
| 766 | values. | ||
| 767 | |||
| 768 | If a column number which is invalid is specified, then a warning to | ||
| 769 | STDERR will be printed. Also, if a scaling factor of zero (or undef) | ||
| 770 | is supplied for a column, a warning will also be printed to STDERR, | ||
| 771 | and the column data for that column will not be scaled. | ||
| 772 | |||
| 773 | Usage: | ||
| 774 | |||
| 775 | $pclFile->scaleColumnData(columns=>{0=>1.2, | ||
| 776 | 2=>0.8}); | ||
| 777 | |||
| 778 | returns : 1 upon success, or 0 otherwise | ||
| 779 | |||
| 780 | =head1 Public Accessor Methods | ||
| 781 | |||
| 782 | =head2 numRows | ||
| 783 | |||
| 784 | This method returns the number of rows that are currently valid in the | ||
| 785 | data matrix. | ||
| 786 | |||
| 787 | Usage: | ||
| 788 | |||
| 789 | my $numRows = $pclFile->numRows; | ||
| 790 | |||
| 791 | returns: a scalar | ||
| 792 | |||
| 793 | =head2 numColumns | ||
| 794 | |||
| 795 | This method returns the number of columns that are currently valid in | ||
| 796 | the data matrix. | ||
| 797 | |||
| 798 | Usage: | ||
| 799 | |||
| 800 | my $numColumns = $pclFile->numColumns; | ||
| 801 | |||
| 802 | returns: a scalar | ||
| 803 | |||
| 804 | =head2 errstr | ||
| 805 | |||
| 806 | This method returns an error string that is associated with the last | ||
| 807 | failed call to a data transformation/filtering method. Calling this | ||
| 808 | method will clear the contents of the error string. | ||
| 809 | |||
| 810 | =head1 Public Setter Methods | ||
| 811 | |||
| 812 | =head2 setAutoDump | ||
| 813 | |||
| 814 | This method can be used to turn autodumping on or off. | ||
| 815 | |||
| 816 | Usage: | ||
| 817 | |||
| 818 | $pclFile->setAutoDump($n); # where $n can be 0 or 1 | ||
| 819 | |||
| 820 | =head1 Public setter/getter methods | ||
| 821 | |||
| 822 | =head2 rowName | ||
| 823 | |||
| 824 | This polymorphic setter/getter method returns the row name for a given | ||
| 825 | row in the pcl file. If a new name is provided, it will update that | ||
| 826 | name to the new value. Note the row number that is passed in is based | ||
| 827 | on the row number in the file used for object construction. | ||
| 828 | |||
| 829 | Usage: | ||
| 830 | |||
| 831 | $self->rowName($rowNum, $newName); | ||
| 832 | |||
| 833 | my $rowName = $self->rowName($rowNum); | ||
| 834 | |||
| 835 | =head2 rowDesc | ||
| 836 | |||
| 837 | This polymorphic setter/getter method returns the row description for | ||
| 838 | a given row in the pcl file. If a new description is provided, it | ||
| 839 | will update that description to the new value. Note the row number | ||
| 840 | that is passed in is based on the row number in the file used for | ||
| 841 | object construction. | ||
| 842 | |||
| 843 | Usage: | ||
| 844 | |||
| 845 | $self->rowDesc($rowNum, $newDesc); | ||
| 846 | |||
| 847 | my $rowDesc = $self->rowDesc($rowNum); | ||
| 848 | |||
| 849 | =head2 gWeight | ||
| 850 | |||
| 851 | This polymorphic setter/getter method returns the gWeight for a given | ||
| 852 | row in the pcl file. If a new gWeight is provided, it will update | ||
| 853 | that gWeight to the new value. Note the row number that is passed in | ||
| 854 | is based on the row number in the file used for object construction. | ||
| 855 | |||
| 856 | Usage: | ||
| 857 | |||
| 858 | $self->gWeight($rowNum, $newGWeight); | ||
| 859 | |||
| 860 | my $gWeight = $self->gWeight($rowNum); | ||
| 861 | |||
| 862 | =head2 eweightsArrayRef | ||
| 863 | |||
| 864 | This polymorphic setter/getter method returns a reference to an array | ||
| 865 | of the eweights that existed in the original file, in the order that | ||
| 866 | they appeared. If a new array reference is provided, it will give the | ||
| 867 | experiments new eweights. | ||
| 868 | |||
| 869 | Usage: | ||
| 870 | |||
| 871 | my $eweightsArrayRef = $self->eweightsArrayRef; | ||
| 872 | $self->eweightsArrayRef(\@eweights); | ||
| 873 | |||
| 874 | =head2 idName | ||
| 875 | |||
| 876 | This polymorphic setter/getter method returns the name of the id | ||
| 877 | column that was used in the original file. If a new value is passed | ||
| 878 | in, it will instead use that value as the id column name. | ||
| 879 | |||
| 880 | Usage: | ||
| 881 | |||
| 882 | print $self->idName; | ||
| 883 | $self->idName($idName); | ||
| 884 | |||
| 885 | =head2 descName | ||
| 886 | |||
| 887 | This polymorphic setter/getter method either returns the name of the | ||
| 888 | description column that was used in the original file, or allows it to | ||
| 889 | be set to a new value. | ||
| 890 | |||
| 891 | Usage: | ||
| 892 | |||
| 893 | print $self->descName; | ||
| 894 | $self->descName($descName); | ||
| 895 | |||
| 896 | =head1 Protected methods | ||
| 897 | |||
| 898 | =head2 _numDataRows | ||
| 899 | |||
| 900 | This method returns the total number of data rows in the pcl file. | ||
| 901 | The superclass requires that it be implemented, such that the base it | ||
| 902 | can determine the number of rows, even if the full data have not been | ||
| 903 | read in. | ||
| 904 | |||
| 905 | Usage: | ||
| 906 | |||
| 907 | my $numRows = $self->_numDataRows; | ||
| 908 | |||
| 909 | =head2 _numDataColumns | ||
| 910 | |||
| 911 | This method returns the total number of data columns in the pcl file. | ||
| 912 | The superclass requires that it be implemented, such that it can | ||
| 913 | determine the number of columns, even if the full data have not been | ||
| 914 | read in. | ||
| 915 | |||
| 916 | Usage: | ||
| 917 | |||
| 918 | my $numColumns = $self->_numDataColumns; | ||
| 919 | |||
| 920 | =head2 _dataLine | ||
| 921 | |||
| 922 | This method reads through the associated pcl file, and then returns a | ||
| 923 | reference to an array of data that corresponds to the requested data | ||
| 924 | line. A requestedRowNum of zero means the first data line. If no | ||
| 925 | data line corresponds to the requested row number, it will return | ||
| 926 | undef. | ||
| 927 | |||
| 928 | Usage: | ||
| 929 | |||
| 930 | my $lineRef = $self->_dataLine($num); | ||
| 931 | |||
| 932 | =head2 _printLeadingMeta | ||
| 933 | |||
| 934 | This method prints the first two lines of a pcl file out. It prints | ||
| 935 | to the passed in file handle. It will only print meta information for | ||
| 936 | those valid columns. If the $extraInfo variable is true (ie a | ||
| 937 | non-zero value) it will leave a print a column called $extraInfoName | ||
| 938 | in between each column of meta data. This method is implemented as | ||
| 939 | required by its superclass, anySizeDataMatrix. | ||
| 940 | |||
| 941 | Usage: | ||
| 942 | |||
| 943 | $self->_printLeadingMeta($fh, $validColumnsArrayRef, $hasExtraInfo, $extraInfoName); | ||
| 944 | |||
| 945 | =head2 _printRow | ||
| 946 | |||
| 947 | This method is used to print out a row of data to a file. It prints | ||
| 948 | to the passed in file handle. It will only print information for | ||
| 949 | those valid columns. If the $extraInfo variable is true (ie a | ||
| 950 | non-zero value) it will print the extra info interleaved between each | ||
| 951 | column of data. The extra info comes from the 2D hash whose reference | ||
| 952 | is passed in. This method is implemented as required by its | ||
| 953 | superclass, anySizeDataMatrix. | ||
| 954 | |||
| 955 | Usage : | ||
| 956 | |||
| 957 | $self->_printRow($fh, $row, $dataRef, $validColumnsArrayRef, $hasExtraInfo, $extraInfoHashRef); | ||
| 958 | |||
| 959 | =head2 _printTrailingMeta | ||
| 960 | |||
| 961 | This method is implemented as required by its superclass, | ||
| 962 | anySizeDataMatrix. For a pclFile, it down;t actually need to do | ||
| 963 | anything. | ||
| 964 | |||
| 965 | Usage: | ||
| 966 | |||
| 967 | $self->_printTrailingMeta($fh, $self->_validColumnsArrayRef); | ||
| 968 | |||
| 969 | =head1 Private Methods | ||
| 970 | |||
| 971 | =head2 __usage | ||
| 972 | |||
| 973 | This private method prints out a usage message for the constructor, | ||
| 974 | and then dies with a specific error message. | ||
| 975 | |||
| 976 | Usage : | ||
| 977 | |||
| 978 | $self->__usage("$file does not exist"); | ||
| 979 | |||
| 980 | =head2 _parseFileHeaders | ||
| 981 | |||
| 982 | This protected method removes the file headers from a pcl file. It | ||
| 983 | accepts a file handle as input, and assumes that the file handle is | ||
| 984 | pointing to the very beginning of the file. | ||
| 985 | |||
| 986 | Usage: | ||
| 987 | |||
| 988 | $self->_parseFileHeaders($fh); | ||
| 989 | |||
| 990 | =head2 __parseFirstLine | ||
| 991 | |||
| 992 | This method deals with recording data from the first line of a pcl | ||
| 993 | file. It accepts as input a reference to an array that contains the | ||
| 994 | fields on the first line split on tabs. | ||
| 995 | |||
| 996 | Usage: | ||
| 997 | |||
| 998 | $self->__parseFirstLine($lineRef); | ||
| 999 | |||
| 1000 | =head2 __parseSecondLine | ||
| 1001 | |||
| 1002 | This method deals with recording data from the second line of a pcl | ||
| 1003 | file. It accepts as input a reference to an array that contains the | ||
| 1004 | fields on the first second split on tabs. | ||
| 1005 | |||
| 1006 | Usage: | ||
| 1007 | |||
| 1008 | $self->__parseSecondLine($lineRef); | ||
| 1009 | |||
| 1010 | =head2 _removeMetaDataFromLine | ||
| 1011 | |||
| 1012 | This method removes and stores meta data from a data line in a pcl | ||
| 1013 | file. Because the array of data is passed in by reference, the array | ||
| 1014 | to which this reference refers is directly manipulated, such that the | ||
| 1015 | callers array will have meta data removed from it. | ||
| 1016 | |||
| 1017 | Usage: | ||
| 1018 | |||
| 1019 | $self->_removeMetaDataFromLine($currFileDataRow, $lineRef); | ||
| 1020 | |||
| 1021 | =head1 AUTHOR | ||
| 1022 | |||
| 1023 | Gavin Sherlock | ||
| 1024 | |||
| 1025 | sherlock@genome.stanford.edu | ||
| 1026 | |||
| 1027 | =cut | ||
| 1028 |