« blib/lib/Algorithm/TrunkClassifier/Classification.pm
↑ Summary
blib/lib/Algorithm/TrunkClassifier/CommandProcessor.pm »
| 1 | package Algorithm::TrunkClassifier::DataWrapper; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use POSIX; | ||
| 7 | |||
| 8 | our $VERSION = "v1.0.1"; | ||
| 9 | |||
| 10 | my $NULL_CLASS = "#NA"; | ||
| 11 | my $PROSPECT_SAMPLES = "samples"; | ||
| 12 | my $PROSPECT_PROBES = "probes"; | ||
| 13 | my $PROSPECT_CLASSES = "classes"; | ||
| 14 | |||
| 15 | #Description: TrunkClassifier::DataWrapper constructor | ||
| 16 | #Parameters: (1) TrunkClassifier::DataWrapper, (2) classification variable name, (3) prospect flag, | ||
| 17 | # (4) supplementary file name, (5) input data file name, (6) verbose flag, (7) dataset type | ||
| 18 | #Return value: TrunkClassifier::DataWrapper object | ||
| 19 | sub new{ | ||
| 20 | my ($class, $className, $prospect, $suppFileName, $dataFileName, $VERBOSE, $datasetType) = @_; | ||
| 21 | my $self = { | ||
| 22 | "colnames" => "", | ||
| 23 | "rownames" => "", | ||
| 24 | "data_matrix" => "", | ||
| 25 | "class_vector" => "", | ||
| 26 | "class_one" => "", | ||
| 27 | "class_two" => "" | ||
| 28 | }; | ||
| 29 | bless($self, $class); | ||
| 30 | if(scalar(@_) == 1){ | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | #If supplementary file is given, write new input data file with meta data | ||
| 35 | if($suppFileName){ | ||
| 36 | $dataFileName = readSuppFile($suppFileName, $dataFileName, $VERBOSE, $datasetType); | ||
| 37 | } | ||
| 38 | |||
| 39 | #Read input data file | ||
| 40 | readExpData($self, $className, $prospect, $dataFileName, $datasetType); | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | #Description: Reads the supplementary file and writes new input data file with meta data | ||
| 45 | #Parameters: (1) supplementary file name, (2) input data file name, (3) dataset type | ||
| 46 | #Return value: New input data file name | ||
| 47 | sub readSuppFile($ $ $ $){ | ||
| 48 | my ($suppFileName, $dataFileName, $VERBOSE, $datasetType) = @_; | ||
| 49 | |||
| 50 | #Read supplementary file | ||
| 51 | open(SUPP_FILE, $suppFileName) or die "Error: Unable to open supplementary file '$suppFileName'\n"; | ||
| 52 | my @suppFile = <SUPP_FILE>; | ||
| 53 | my $content = join("", @suppFile); | ||
| 54 | $content =~ s/\r|\n\r|\r\n/\n/g; | ||
| 55 | @suppFile = split(/\n+/, $content); | ||
| 56 | close(SUPP_FILE); | ||
| 57 | |||
| 58 | #Extract classification variable names | ||
| 59 | my @classNames = split(/\t/, shift(@suppFile)); | ||
| 60 | my $numCols = scalar(@classNames); | ||
| 61 | shift(@classNames); | ||
| 62 | if($numCols < 2){ | ||
| 63 | warn "Warning: No classification variable names found in supplementary file\n"; | ||
| 64 | return $dataFileName; | ||
| 65 | } | ||
| 66 | foreach my $className (@classNames){ | ||
| 67 | $className = uc($className); | ||
| 68 | } | ||
| 69 | my %classes; | ||
| 70 | foreach my $classVar (@classNames){ | ||
| 71 | $classes{$classVar} = {}; | ||
| 72 | } | ||
| 73 | |||
| 74 | #Determine classes of each classification variable and assign classes to samples | ||
| 75 | my %sampleClasses; | ||
| 76 | for(my $lineIndex = 0; $lineIndex < scalar(@suppFile); $lineIndex++){ | ||
| 77 | if($suppFile[$lineIndex] =~ /^\s*$/){ | ||
| 78 | next; | ||
| 79 | } | ||
| 80 | my @cols = split(/\t/, $suppFile[$lineIndex]); | ||
| 81 | if(scalar(@cols) != $numCols){ | ||
| 82 | $lineIndex++; | ||
| 83 | die "Error: Wrong number of columns in supplmentary file at line $lineIndex\n"; | ||
| 84 | } | ||
| 85 | my $sampleName = uc(shift(@cols)); | ||
| 86 | $sampleName =~ s/\s+//g; | ||
| 87 | if(!$sampleName){ | ||
| 88 | $lineIndex++; | ||
| 89 | die "Error: Missing sample name in supplmentary file at line $lineIndex\n"; | ||
| 90 | } | ||
| 91 | for(my $classVarIndex = 0; $classVarIndex < scalar(@classNames); $classVarIndex++){ | ||
| 92 | my $class = uc($cols[$classVarIndex]); | ||
| 93 | $class =~ s/\s+//g; | ||
| 94 | if(!$class){ | ||
| 95 | my $lineWarn = $classVarIndex + 1; | ||
| 96 | warn "Warning: Missing class in supplmentary file at line $lineWarn, replacing with #NA\n"; | ||
| 97 | $class = $NULL_CLASS; | ||
| 98 | } | ||
| 99 | $sampleClasses{$sampleName}[$classVarIndex] = $class; | ||
| 100 | if($class ne $NULL_CLASS && !$classes{$classNames[$classVarIndex]}{$class}){ | ||
| 101 | $classes{$classNames[$classVarIndex]}{$class} = 1; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | foreach my $classVar (keys(%classes)){ | ||
| 106 | if(scalar(keys(%{$classes{$classVar}})) != 2){ | ||
| 107 | die "Error: Class variable $classVar in supplementary file does not have two classes\n"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | if(!%sampleClasses){ | ||
| 111 | warn "Warning: No sample classes found in supplementary file\n"; | ||
| 112 | return $dataFileName; | ||
| 113 | } | ||
| 114 | |||
| 115 | #Read input data file and write new data file with classification info | ||
| 116 | open(DATA_FILE, $dataFileName) or die "Unable to open $datasetType '$dataFileName'\n"; | ||
| 117 | my @dataFile = <DATA_FILE>; | ||
| 118 | close(DATA_FILE); | ||
| 119 | my @header = split(/\t/, $dataFile[0]); | ||
| 120 | shift(@header); | ||
| 121 | chomp(@header); | ||
| 122 | foreach my $sampleName (@header){ | ||
| 123 | $sampleName = uc($sampleName); | ||
| 124 | $sampleName =~ s/\n|\r//g; | ||
| 125 | } | ||
| 126 | $dataFileName =~ s/\.[^.]+$/_wmeta.txt/; | ||
| 127 | if($VERBOSE){ | ||
| 128 | print("Trunk classifier: Supplementary file supplied, writing new $datasetType with meta data\n"); | ||
| 129 | } | ||
| 130 | open(DATA_FILE, ">$dataFileName") or die "Unable to create new data file '$dataFileName'\n"; | ||
| 131 | my $meta = ""; | ||
| 132 | for(my $classVarIndex = 0; $classVarIndex < scalar(@classNames); $classVarIndex++){ | ||
| 133 | my $className = $classNames[$classVarIndex]; | ||
| 134 | my @classKeys = keys(%{$classes{$className}}); | ||
| 135 | $meta .= "#CLASSVAR $className @classKeys\n"; | ||
| 136 | $meta .= "#CLASSMEM $className"; | ||
| 137 | foreach my $sampleName (@header){ | ||
| 138 | if(!$sampleClasses{$sampleName}[$classVarIndex]){ | ||
| 139 | warn "Warning: Sample '$sampleName' has no '$className' class in supplementary file\n"; | ||
| 140 | $meta .= " " . "#NA"; | ||
| 141 | } | ||
| 142 | else{ | ||
| 143 | $meta .= " " . $sampleClasses{$sampleName}[$classVarIndex]; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | $meta .= "\n"; | ||
| 147 | } | ||
| 148 | print(DATA_FILE $meta . join("", @dataFile)); | ||
| 149 | close(DATA_FILE); | ||
| 150 | return $dataFileName; | ||
| 151 | } | ||
| 152 | |||
| 153 | #Description: Reads input data file with expression values and meta data | ||
| 154 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) classification variable name | ||
| 155 | # (3) prospect flag, (4) input data file name, (5) dataset type | ||
| 156 | #Return value: None | ||
| 157 | sub readExpData($ $ $ $ $){ | ||
| 158 | my ($self, $className, $prospect, $dataFileName, $datasetType) = @_; | ||
| 159 | $className = uc($className); | ||
| 160 | |||
| 161 | #Read input data file | ||
| 162 | if(!open(DATA_FILE, $dataFileName)){ | ||
| 163 | die "Error: Unable to open $datasetType '$dataFileName'\n"; | ||
| 164 | } | ||
| 165 | my @dataFile = <DATA_FILE>; | ||
| 166 | close(DATA_FILE); | ||
| 167 | my $content = join("", @dataFile); | ||
| 168 | $content =~ s/\r|\n\r|\r\n/\n/g; | ||
| 169 | @dataFile = split(/\n+/, $content); | ||
| 170 | |||
| 171 | foreach my $row (@dataFile){ | ||
| 172 | $row =~ s/\n//g; | ||
| 173 | } | ||
| 174 | |||
| 175 | #Extract meta data rows | ||
| 176 | my @metarows; | ||
| 177 | my $rowcounter = 0; | ||
| 178 | while($rowcounter < scalar(@dataFile)){ | ||
| 179 | $dataFile[$rowcounter] =~ s/^\s+//; | ||
| 180 | if($dataFile[$rowcounter] =~ /^\s*$/){ | ||
| 181 | shift(@dataFile); | ||
| 182 | } | ||
| 183 | elsif($dataFile[$rowcounter] =~ /^#/){ | ||
| 184 | push(@metarows, shift(@dataFile)); | ||
| 185 | } | ||
| 186 | else{ | ||
| 187 | $rowcounter++; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | #Extract samples | ||
| 192 | my @samples = split(/\t/, shift(@dataFile)); | ||
| 193 | shift(@samples); | ||
| 194 | my $totNumSamples = scalar(@samples); | ||
| 195 | if(!$totNumSamples){ | ||
| 196 | die "Error: No samples in $datasetType\n"; | ||
| 197 | } | ||
| 198 | |||
| 199 | #Check that class variable exists and that all samples have valid class membership | ||
| 200 | my %classes; | ||
| 201 | my %membership; | ||
| 202 | foreach my $row (@metarows){ | ||
| 203 | if($row =~ /^#CLASSVAR/){ | ||
| 204 | my @cols = split(/\s+/, $row); | ||
| 205 | shift(@cols); | ||
| 206 | if(!$cols[0]){ | ||
| 207 | warn "Warning: CLASSVAR name missing in meta data of $datasetType\n"; | ||
| 208 | next; | ||
| 209 | } | ||
| 210 | if(!$cols[1] || !$cols[2]){ | ||
| 211 | warn "Warning: CLASSVAR class labels for '$cols[0]' missing in meta data of $datasetType\n"; | ||
| 212 | next; | ||
| 213 | } | ||
| 214 | if($cols[1] eq $NULL_CLASS || $cols[2] eq $NULL_CLASS){ | ||
| 215 | die "Error: CLASSVAR class label equals NULL CLASS in $datasetType\n"; | ||
| 216 | } | ||
| 217 | my $classVarName = uc($cols[0]); | ||
| 218 | my $class1 = uc($cols[1]); | ||
| 219 | my $class2 = uc($cols[2]); | ||
| 220 | $classes{$classVarName} = {$class1 => 1, $class2 => 1}; | ||
| 221 | } | ||
| 222 | if($row =~ /^#CLASSMEM/){ | ||
| 223 | my @cols = split(/\s+/, $row); | ||
| 224 | shift(@cols); | ||
| 225 | if(!$cols[0]){ | ||
| 226 | warn "Warning: CLASSMEM name missing in meta data of $datasetType\n"; | ||
| 227 | next; | ||
| 228 | } | ||
| 229 | my $classVarName = uc(shift(@cols)); | ||
| 230 | foreach my $class (@cols){ | ||
| 231 | $class = uc($class); | ||
| 232 | } | ||
| 233 | $membership{$classVarName} = \@cols; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | if(!$classes{$className} || !$membership{$className}){ | ||
| 237 | die "Error: Missing meta data for classification variable '$className' in $datasetType\n"; | ||
| 238 | } | ||
| 239 | if(scalar(@{$membership{$className}}) != $totNumSamples){ | ||
| 240 | die "Error: CLASSMEM vector for '$className' and sample vector have different lengths in $datasetType\n"; | ||
| 241 | } | ||
| 242 | foreach my $class (@{$membership{$className}}){ | ||
| 243 | if($class ne $NULL_CLASS && !$classes{$className}{$class}){ | ||
| 244 | die "Error: Invalid class label in '$className' CLASSMEM vector in $datasetType\n"; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | my @classVector = @{$membership{$className}}; | ||
| 248 | my @classBuffer = sort(keys(%{$classes{$className}})); | ||
| 249 | my $classOne = $classBuffer[0]; | ||
| 250 | my $classTwo = $classBuffer[1]; | ||
| 251 | |||
| 252 | #Determine what sample indexes to include | ||
| 253 | my @includedInd; | ||
| 254 | my $classOneCount = 0; | ||
| 255 | my $classTwoCount = 0; | ||
| 256 | for(my $sampleIndex = 0; $sampleIndex < $totNumSamples; $sampleIndex++){ | ||
| 257 | if($classVector[$sampleIndex] eq $classOne){ | ||
| 258 | $classOneCount++; | ||
| 259 | push(@includedInd, $sampleIndex); | ||
| 260 | } | ||
| 261 | elsif($classVector[$sampleIndex] eq $classTwo){ | ||
| 262 | $classTwoCount++; | ||
| 263 | push(@includedInd, $sampleIndex); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | if(!$classOneCount){ | ||
| 267 | die "Error: Class '$classOne' for classification variable '$className' has zero members in $datasetType\n"; | ||
| 268 | } | ||
| 269 | if(!$classTwoCount){ | ||
| 270 | die "Error: Class '$classTwo' for classification variable '$className' has zero members in $datasetType\n"; | ||
| 271 | } | ||
| 272 | my $numIncInd = scalar(@includedInd); | ||
| 273 | |||
| 274 | #Check for sample duplicates | ||
| 275 | for(my $outer = 0; $outer < $totNumSamples - 1; $outer++){ | ||
| 276 | for(my $inner = $outer + 1; $inner < $totNumSamples; $inner++){ | ||
| 277 | if($samples[$outer] eq $samples[$inner]){ | ||
| 278 | warn "Warning: Duplicate sample name '$samples[$outer]' at positions ", $outer + 1, " and ", $inner + 1, " in $datasetType\n"; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | #Initialise Algorithm::TrunkClassifier::DataWrapper object | ||
| 284 | my @incSampleNames; | ||
| 285 | my @incClassVector; | ||
| 286 | my @probeNames; | ||
| 287 | my @dataMatrix; | ||
| 288 | foreach my $index (@includedInd){ | ||
| 289 | push(@incSampleNames, $samples[$index]); | ||
| 290 | push(@incClassVector, $classVector[$index]); | ||
| 291 | } | ||
| 292 | for(my $rowIndex = 0; $rowIndex < scalar(@dataFile); $rowIndex++){ | ||
| 293 | $dataFile[$rowIndex] =~ s/,/./g; | ||
| 294 | my @cols = split(/\t/, $dataFile[$rowIndex]); | ||
| 295 | if(scalar(@cols) != $totNumSamples + 1){ | ||
| 296 | die "Error: Wrong number of columns in $datasetType at probe ", $rowIndex + 1, "\n"; | ||
| 297 | } | ||
| 298 | my $probe = "$rowIndex:" . shift(@cols); | ||
| 299 | push(@probeNames, $probe); | ||
| 300 | my @includedCols; | ||
| 301 | foreach my $index (@includedInd){ | ||
| 302 | $cols[$index] =~ s/\s+//g; | ||
| 303 | if($cols[$index] !~ /^-?[0-9]+(\.[0-9]+)?([Ee][+\-]?[0-9]+)?$/){ | ||
| 304 | warn "Warning: Missing/invalid value '$cols[$index]' in $datasetType at probe ", $rowIndex + 1, "\n"; | ||
| 305 | $cols[$index] =~ s/[^0-9]+//g; | ||
| 306 | } | ||
| 307 | if($cols[$index] !~ /\./){ | ||
| 308 | $cols[$index] .= ".0"; | ||
| 309 | } | ||
| 310 | push(@includedCols, $cols[$index] + 0); | ||
| 311 | } | ||
| 312 | push(@dataMatrix, \@includedCols); | ||
| 313 | } | ||
| 314 | |||
| 315 | #Check prospect flag | ||
| 316 | if($prospect){ | ||
| 317 | if($prospect eq $PROSPECT_SAMPLES){ | ||
| 318 | die "Number of samples with $className class\n$classOne: $classOneCount\n$classTwo: $classTwoCount\n"; | ||
| 319 | } | ||
| 320 | elsif($prospect eq $PROSPECT_PROBES){ | ||
| 321 | my $numProbes = scalar(@dataMatrix); | ||
| 322 | die "Number of probes in dataset: $numProbes\n"; | ||
| 323 | } | ||
| 324 | elsif($prospect eq $PROSPECT_CLASSES){ | ||
| 325 | my @classKeys = keys(%classes); | ||
| 326 | die "Classes in the dataset: @classKeys\n"; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | $self->{"colnames"} = \@incSampleNames; | ||
| 330 | $self->{"rownames"} = \@probeNames; | ||
| 331 | $self->{"data_matrix"} = \@dataMatrix; | ||
| 332 | $self->{"class_vector"} = \@incClassVector; | ||
| 333 | $self->{"class_one"} = $classOne; | ||
| 334 | $self->{"class_two"} = $classTwo; | ||
| 335 | } | ||
| 336 | |||
| 337 | #Description: Returns the number of samples in the dataset | ||
| 338 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 339 | #Return value: Number of elements in "colnames" attribute | ||
| 340 | sub getNumSamples($){ | ||
| 341 | my $self = shift(@_); | ||
| 342 | return scalar(@{$self->{"colnames"}}); | ||
| 343 | } | ||
| 344 | |||
| 345 | #Description: Returns the number of probes in the dataset | ||
| 346 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 347 | #Return value: Number of rows in "rownames" array | ||
| 348 | sub getNumProbes($){ | ||
| 349 | my $self = shift(@_); | ||
| 350 | return scalar(@{$self->{"rownames"}}); | ||
| 351 | } | ||
| 352 | |||
| 353 | #Description: Returns the row names of the DataWrapper object | ||
| 354 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 355 | #Return value: Array of row names | ||
| 356 | sub getProbeList($){ | ||
| 357 | my $self = shift(@_); | ||
| 358 | return @{$self->{"rownames"}}; | ||
| 359 | } | ||
| 360 | |||
| 361 | #Description: Returns a reference to the data matrix | ||
| 362 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 363 | #Return value: Array reference | ||
| 364 | sub getDataMatrix($){ | ||
| 365 | my $self = shift(@_); | ||
| 366 | return $self->{"data_matrix"}; | ||
| 367 | } | ||
| 368 | |||
| 369 | #Description: Returns a reference to the class vector | ||
| 370 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 371 | #Return value: Array reference | ||
| 372 | sub getClassVector($){ | ||
| 373 | my $self = shift(@_); | ||
| 374 | return $self->{"class_vector"}; | ||
| 375 | } | ||
| 376 | |||
| 377 | #Description: Returns the name of class one | ||
| 378 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 379 | #Return value: Class name | ||
| 380 | sub getClassOneName($){ | ||
| 381 | my $self = shift(@_); | ||
| 382 | return $self->{"class_one"}; | ||
| 383 | } | ||
| 384 | |||
| 385 | #Description: Returns the name of class two | ||
| 386 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 387 | #Return value: Class name | ||
| 388 | sub getClassTwoName($){ | ||
| 389 | my $self = shift(@_); | ||
| 390 | return $self->{"class_two"}; | ||
| 391 | } | ||
| 392 | |||
| 393 | #Description: Returns a copy of a TrunkClassifier::DataWrapper object | ||
| 394 | #Parameters: (1) TrunkClassifier::DataWrapper object | ||
| 395 | #Return value: New TrunkClassifier::DataWrapper object | ||
| 396 | sub copy($){ | ||
| 397 | my $self = shift(@_); | ||
| 398 | my $newWrapper = Algorithm::TrunkClassifier::DataWrapper->new(); | ||
| 399 | my @colnames = @{$self->{"colnames"}}; | ||
| 400 | my @rownames = @{$self->{"rownames"}}; | ||
| 401 | my @classVector = @{$self->{"class_vector"}}; | ||
| 402 | my @dataMatrix; | ||
| 403 | foreach my $arrayRef (@{$self->{"data_matrix"}}){ | ||
| 404 | my @arrayCopy = @{$arrayRef}; | ||
| 405 | push(@dataMatrix, \@arrayCopy); | ||
| 406 | } | ||
| 407 | $newWrapper->{"colnames"} = \@colnames; | ||
| 408 | $newWrapper->{"rownames"} = \@rownames; | ||
| 409 | $newWrapper->{"data_matrix"} = \@dataMatrix; | ||
| 410 | $newWrapper->{"class_vector"} = \@classVector; | ||
| 411 | $newWrapper->{"class_one"} = $self->{"class_one"}; | ||
| 412 | $newWrapper->{"class_two"} = $self->{"class_two"}; | ||
| 413 | return $newWrapper; | ||
| 414 | } | ||
| 415 | |||
| 416 | #Description: Removes one sample from a TrunkClassifier::DataWrapper object | ||
| 417 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) index of sample to remove | ||
| 418 | #Return value: TrunkClassifier::DataWrapper object containing the removed sample | ||
| 419 | sub leaveOneOut($ $){ | ||
| 420 | my ($self, $index) = @_; | ||
| 421 | my @colnames = ($self->{"colnames"}[$index]); | ||
| 422 | my @rownames = @{$self->{"rownames"}}; | ||
| 423 | my @classVector = ($self->{"class_vector"}[$index]); | ||
| 424 | my @matrixCol; | ||
| 425 | for(my $row = 0; $row < scalar(@rownames); $row++){ | ||
| 426 | my @colArray = splice(@{$self->{"data_matrix"}[$row]}, $index, 1); | ||
| 427 | push(@matrixCol, \@colArray); | ||
| 428 | } | ||
| 429 | splice(@{$self->{"colnames"}}, $index, 1); | ||
| 430 | splice(@{$self->{"class_vector"}}, $index, 1); | ||
| 431 | my $newWrapper = Algorithm::TrunkClassifier::DataWrapper->new(); | ||
| 432 | $newWrapper->{"colnames"} = \@colnames; | ||
| 433 | $newWrapper->{"rownames"} = \@rownames; | ||
| 434 | $newWrapper->{"data_matrix"} = \@matrixCol; | ||
| 435 | $newWrapper->{"class_vector"} = \@classVector; | ||
| 436 | $newWrapper->{"class_one"} = $self->{"class_one"}; | ||
| 437 | $newWrapper->{"class_two"} = $self->{"class_two"}; | ||
| 438 | return $newWrapper; | ||
| 439 | } | ||
| 440 | |||
| 441 | #Description: Removes a percentage of samples from a TrunkClassifier::DataWrapper object | ||
| 442 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) split percent | ||
| 443 | #Return value: TrunkClassifier::DataWrapper object containing the removed samples | ||
| 444 | sub splitSamples($ $){ | ||
| 445 | my ($self, $split) = @_; | ||
| 446 | my $totNumSamples = $self->getNumSamples(); | ||
| 447 | my $testSetSize = floor(($split / 100) * $totNumSamples); | ||
| 448 | my @colnames; | ||
| 449 | my @rownames = $self->getProbeList(); | ||
| 450 | my @classVector; | ||
| 451 | my @matrix; | ||
| 452 | for(my $row = 0; $row < $self->getNumProbes(); $row++){ | ||
| 453 | my @array; | ||
| 454 | push(@matrix, \@array); | ||
| 455 | } | ||
| 456 | for(my $testIndex = 0; $testIndex < $testSetSize; $testIndex++){ | ||
| 457 | my $randIndex = int(rand($self->getNumSamples())); | ||
| 458 | my $colname = splice(@{$self->{"colnames"}}, $randIndex, 1); | ||
| 459 | push(@colnames, $colname); | ||
| 460 | my $class = splice(@{$self->{"class_vector"}}, $randIndex, 1); | ||
| 461 | push(@classVector, $class); | ||
| 462 | for(my $row = 0; $row < $self->getNumProbes(); $row++){ | ||
| 463 | my $value = splice(@{$self->{"data_matrix"}[$row]}, $randIndex, 1); | ||
| 464 | push(@{$matrix[$row]}, $value); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | my $testSet = Algorithm::TrunkClassifier::DataWrapper->new(); | ||
| 468 | $testSet->{"colnames"} = \@colnames; | ||
| 469 | $testSet->{"rownames"} = \@rownames; | ||
| 470 | $testSet->{"data_matrix"} = \@matrix; | ||
| 471 | $testSet->{"class_vector"} = \@classVector; | ||
| 472 | $testSet->{"class_one"} = $self->{"class_one"}; | ||
| 473 | $testSet->{"class_two"} = $self->{"class_two"}; | ||
| 474 | return $testSet; | ||
| 475 | } | ||
| 476 | |||
| 477 | #Description: Returns the number of samples in the specified class | ||
| 478 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) class | ||
| 479 | #Return value: Array with column indexes | ||
| 480 | sub getClassSize($ $){ | ||
| 481 | my ($self, $class) = @_; | ||
| 482 | my $classSize = 0; | ||
| 483 | foreach my $sampleClass (@{$self->{"class_vector"}}){ | ||
| 484 | if($sampleClass eq $class){ | ||
| 485 | $classSize++; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | return $classSize; | ||
| 489 | } | ||
| 490 | |||
| 491 | #Description: Returns the probe name of the probe row index given as argument | ||
| 492 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) probe row index | ||
| 493 | #Return value: Probe name | ||
| 494 | sub getProbeName($ $){ | ||
| 495 | my ($self, $probeIndex) = @_; | ||
| 496 | return ${$self->{"rownames"}}[$probeIndex]; | ||
| 497 | } | ||
| 498 | |||
| 499 | #Description: Returns the probe row index of the probe name given as argument | ||
| 500 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) probe name | ||
| 501 | #Return value: Probe row index | ||
| 502 | sub getProbeIndex($ $){ | ||
| 503 | my ($self, $probeName) = @_; | ||
| 504 | for(my $probeIndex = 0; $probeIndex < $self->getNumProbes(); $probeIndex++){ | ||
| 505 | if($self->{"rownames"}[$probeIndex] eq $probeName){ | ||
| 506 | return $probeIndex; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | return undef; | ||
| 510 | } | ||
| 511 | |||
| 512 | #Description: Returns the data matrix row corresponding to the argument index | ||
| 513 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) row index | ||
| 514 | #Return value: Array | ||
| 515 | sub getMatrixRow($ $){ | ||
| 516 | my ($self, $rowIndex) = @_; | ||
| 517 | return @{$self->{"data_matrix"}[$rowIndex]}; | ||
| 518 | } | ||
| 519 | |||
| 520 | #Description: Returns the sample name corresponding to the sample index given | ||
| 521 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) sample index | ||
| 522 | #Return value: Array reference | ||
| 523 | sub getSampleName($ $){ | ||
| 524 | my ($self, $sampleIndex) = @_; | ||
| 525 | return $self->{"colnames"}[$sampleIndex]; | ||
| 526 | } | ||
| 527 | |||
| 528 | #Description: Removes a probe name from row names and its row from the data matrix | ||
| 529 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) probe index | ||
| 530 | #Return value: None | ||
| 531 | sub removeProbe($ $){ | ||
| 532 | my ($self, $probeIndex) = @_; | ||
| 533 | splice(@{$self->{"rownames"}}, $probeIndex, 1); | ||
| 534 | splice(@{$self->{"data_matrix"}}, $probeIndex, 1); | ||
| 535 | } | ||
| 536 | |||
| 537 | #Description: Removes a sample name from col names, its class from class vector, and its column from the data matrix | ||
| 538 | #Parameters: (1) TrunkClassifier::DataWrapper object, (2) sample index | ||
| 539 | #Return value: None | ||
| 540 | sub removeSample($ $){ | ||
| 541 | my ($self, $sampleIndex) = @_; | ||
| 542 | splice(@{$self->{"colnames"}}, $sampleIndex, 1); | ||
| 543 | splice(@{$self->{"class_vector"}}, $sampleIndex, 1); | ||
| 544 | foreach my $rowref (@{$self->{"data_matrix"}}){ | ||
| 545 | splice(@{$rowref}, $sampleIndex, 1); | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | return 1; |