Algorithm-TrunkClassifier

 view release on metacpan or  search on metacpan

lib/Algorithm/TrunkClassifier/DataWrapper.pm  view on Meta::CPAN

package Algorithm::TrunkClassifier::DataWrapper;

use warnings;
use strict;

use POSIX;

our $VERSION = "v1.0.1";

my $NULL_CLASS = "#NA";
my $PROSPECT_SAMPLES = "samples";
my $PROSPECT_PROBES = "probes";
my $PROSPECT_CLASSES = "classes";

#Description: TrunkClassifier::DataWrapper constructor
#Parameters: (1) TrunkClassifier::DataWrapper, (2) classification variable name, (3) prospect flag,
#            (4) supplementary file name, (5) input data file name, (6) verbose flag, (7) dataset type
#Return value: TrunkClassifier::DataWrapper object
sub new{
	my ($class, $className, $prospect, $suppFileName, $dataFileName, $VERBOSE, $datasetType) = @_;
	my $self = {
		"colnames" => "",
		"rownames" => "",
		"data_matrix" => "",
		"class_vector" => "",
		"class_one" => "",
		"class_two" => ""
	};
	bless($self, $class);
	if(scalar(@_) == 1){
		return $self;
	}
	
	#If supplementary file is given, write new input data file with meta data
	if($suppFileName){
		$dataFileName = readSuppFile($suppFileName, $dataFileName, $VERBOSE, $datasetType);
	}
	
	#Read input data file
	readExpData($self, $className, $prospect, $dataFileName, $datasetType);
	return $self;
}

#Description: Reads the supplementary file and writes new input data file with meta data
#Parameters: (1) supplementary file name, (2) input data file name, (3) dataset type
#Return value: New input data file name
sub readSuppFile($ $ $ $){
	my ($suppFileName, $dataFileName, $VERBOSE, $datasetType) = @_;

	#Read supplementary file
	open(SUPP_FILE, $suppFileName) or die "Error: Unable to open supplementary file '$suppFileName'\n";
	my @suppFile = <SUPP_FILE>;
	my $content = join("", @suppFile);
	$content =~ s/\r|\n\r|\r\n/\n/g;
	@suppFile = split(/\n+/, $content);
	close(SUPP_FILE);
	
	#Extract classification variable names
	my @classNames = split(/\t/, shift(@suppFile));
	my $numCols = scalar(@classNames);
	shift(@classNames);
	if($numCols < 2){
		warn "Warning: No classification variable names found in supplementary file\n";
		return $dataFileName;
	}
	foreach my $className (@classNames){
		$className = uc($className);
	}
	my %classes;
	foreach my $classVar (@classNames){
		$classes{$classVar} = {};
	}
	
	#Determine classes of each classification variable and assign classes to samples
	my %sampleClasses;
	for(my $lineIndex = 0; $lineIndex < scalar(@suppFile); $lineIndex++){
		if($suppFile[$lineIndex] =~ /^\s*$/){
			next;
		}
		my @cols = split(/\t/, $suppFile[$lineIndex]);
		if(scalar(@cols) != $numCols){
			$lineIndex++;
			die "Error: Wrong number of columns in supplmentary file at line $lineIndex\n";
		}
		my $sampleName = uc(shift(@cols));
		$sampleName =~ s/\s+//g;
		if(!$sampleName){
			$lineIndex++;
			die "Error: Missing sample name in supplmentary file at line $lineIndex\n";



( run in 0.441 second using v1.01-cache-2.11-cpan-0b5f733616e )