MicroArrayData.r

### Data structure for storing named expression matrices at different stages of normalisation
#

## Global settings
ArrayData.write.digits=7; # Number of digits to use in ArrayData.write

## Include code using the following line:
#source("D:\\Projects\\_Appl_\\_R_\\ArrayData.r")

# Implement generic functions
print.ArrayData=function(arraydata) {
 if (is.null(arraydata$label)) log.message("ArrayData object.")
 else log.message("ArrayData object labeled ",arraydata$label);
 ArrayData.message(arraydata,"Present matrix is ");
}

# Make new ArrayData object
ArrayData.new=function(name=NULL,m=NULL,label='probe*array=expression') {
	arraydata=new.env();
	class(arraydata)="ArrayData";
	arraydata$label=label;
	arraydata$last=NULL;
	arraydata$arraylist=c();
	
	if (!is.null(m)) {
		ArrayData.add(arraydata,name,m);
	}
	invisible(arraydata);
}

# Check if an object is and ArrayData object
ArrayData.check=function(arraydata) return(class(arraydata)=="ArrayData");
ArrayData.checkOrStop=function(arraydata) {
 if (!ArrayData.check(arraydata)) stop("Data should be of type ArrayData.");
}

# Get last matrix
ArrayData.last=function(arraydata) {
 if (is.null(arraydata$last)) stop("No available matrix.");
 invisible(arraydata[[arraydata$last]]);
}

# Get name or last named matrix if name not specified (NULL)
ArrayData.getName=function(arraydata,name=NULL) {
 if (is.null(name)) return(arraydata$last)
 else return(name);
}

# Get matrix by name (or last named matrix if not specified)
ArrayData.get=function(arraydata,name=NULL) {
 name=ArrayData.getName(arraydata,name=name);
 invisible(arraydata[[name]])
}

# Set last matrix
ArrayData.set=function(arraydata,name) {
 arraydata$last=name;
 ArrayData.message(arraydata,"Matrix set to ",name=name);
 invisible(ArrayData.last(arraydata));
}

# Add new matrix
ArrayData.add=function(arraydata,name,m) {
 arraydata[[name]]=m;
 arraydata$arraylist=c(arraydata$arraylist,name);
 attr(arraydata[[name]],"name")=name;
 arraydata$last=name;
 ArrayData.message(arraydata,"Present matrix is ",name=name);
 invisible(m);
}

# Get attribute of matrix
ArrayData.getAttr=function(arraydata,name=NULL,key=NULL) {
	name=ArrayData.getName(arraydata,name=name);
	return(attr(arraydata[[name]],key));
}

# Set attribute of matrix
ArrayData.setAttr=function(arraydata,name=NULL,key=NULL,value=NULL) {
	name=ArrayData.getName(arraydata,name=name);
	attr(arraydata[[name]],key)=value;
}

# Print message concerning matrix
ArrayData.message=function(arraydata,prefix="Matrix ",suffix=".",name=NULL) {
 name=ArrayData.getName(arraydata,name=name);
 x=ArrayData.get(arraydata,name=name);
 log.message(paste(prefix,name," (",nrow(x),"x",ncol(x),")",suffix,sep=""));
}

#-NonmissSample: get indexes of given number of non-missing values of m
NonmissSample=function(m,size) sort(sample(which(!is.na(m)),size));
ArrayData.randomMissing=function(arraydata,n,name=NULL,setname="missmat") {
 m=ArrayData.get(arraydata,name=name);
 misslist=NonmissSample(m,n);
 arraydata[[paste(setname,"setmiss",sep=".")]]=misslist;
 m[misslist]=NA;
 ArrayData.add(arraydata,setname,m);
 invisible(m);
}

#
ArrayData.getMissStat=function(arraydata,name=NULL) {
 name=ArrayData.getName(arraydata,name);
 missList=which(is.na(arraydata[[name]]));
 m=list();
 m$missList=which(is.na(arraydata[[name]]));
 m$rowNmiss=apply(is.na(arraydata[[name]]),1,sum);
 m$colNmiss=apply(is.na(arraydata[[name]]),2,sum);
 m$missRows=which(m$rowNmiss>0);
 arraydata[[paste(name,"missList",sep=".")]]=m$missList;
 arraydata[[paste(name,"rowNmiss",sep=".")]]=m$rowNmiss;
 arraydata[[paste(name,"colNmiss",sep=".")]]=m$colNmiss;
 arraydata[[paste(name,"missRows",sep=".")]]=m$missRows;
 return(m);
}

# Read matrix from file with row names, column names, and row name header
ArrayData.read=function(file,sep="\t",name="raw") {
 arraydata=ArrayData.new();
 arraydata$label=scan(file,sep=sep,what="character",n=1,quiet=TRUE);
 m=as.matrix(read.table(file,sep=sep,na.strings="",comment.char="",quote="",header=TRUE,row.names=1,check.names=FALSE));
 log.message(paste("Read data matrix with",nrow(m),"rows and",ncol(m),"columns."));
 ArrayData.add(arraydata,name,m);
 invisible(arraydata);
}

# Write matrix to file with row names, column names, and row name header
ArrayData.write=function(arraydata,file=NULL,label=NULL,sep="\t",name=NULL) {
 if (ArrayData.check(arraydata)) {
  name=ArrayData.getName(arraydata,name=name);
  attr(arraydata[[name]],'filepath')=file;
  x=arraydata[[name]];
  if (is.null(label)) label=arraydata$label;
  log.message(paste("Writing matrix ",attr(x,"name")," to ",file,".",sep=""));
 } else {
  x=arraydata;
 }
 if (!class(x)=="matrix") stop(paste("Method requires matrix, not ",class(x),"."))
 outfile=file(file,"w");
 writeLines(paste(c(label,colnames(x)),collapse=sep),outfile);
 # ??? .Internal(write.table(x,outfile,nrow(x),ncol(x),rownames(x),sep,"\n","",".",integer(0),FALSE));
 write.table(round(x,ArrayData.write.digits),outfile,col.names=FALSE,quote=FALSE,sep=sep,na="");
 close(outfile);
 log.message(paste("Matrix with",nrow(x),"rows and",ncol(x),"columns written."));
 invisible(x);
}
Last modified March 06, 2014.