MicroArrayNormalize.r

### Micma normalization

# Include
#source("D:\\Projects\\_Appl_\\_R_\\ArrayData.r")
library(limma);
library(pcaMethods);

# Centering (requires complete data)
centerCols=function(m) scale(m,scale=FALSE);
centerRows=function(m) t(centerCols(t(m)));
centerBoth=function(m) centerRows(centerCols(m));

### Define normalization function

Array.normalize.default.path=NULL;

Array.normalize=function(filename=NULL,arraydata=NULL,
				path=Array.normalize.default.path,subdir=NULL,
				inpath=NULL,outpath=NULL,
				k=20)
{

	if (!is.null(path)) {
		if (!is.null(subdir)) path=file.path(path,subdir);
		if (is.null(inpath)) inpath=file.path(path);
		if (is.null(outpath)) outpath=file.path(path);
	}

	# Read data
	if (is.null(arraydata)) {
		tags=c('raw');
		infile=file.tag.path(c(filename,tags,"log2.dat"),dir=inpath);
		log.message("Reading data: ",infile);
		arraydata=ArrayData.read(infile);
		ArrayData.setAttr(arraydata,key='tags',value=tags);
		ArrayData.setAttr(arraydata,key='filepath',value=infile);
	}
	arraydata.nmiss=sum(is.na(ArrayData.last(arraydata)))
	log.message("Number of missing values: ",arraydata.nmiss);

	# Quantile normalization
	tags=c('qnorm');
	log.message("Running quantile normalization...");
	x=normalizeBetweenArrays(ArrayData.last(arraydata),method="quantile");
	ArrayData.add(arraydata,"norm",x);
	ArrayData.setAttr(arraydata,key='tags',value=tags);
	if (!is.null(outpath)) {
		outfile=file.tag.path(c(filename,tags,"log2.dat"));
		log.message("Writing quantile normalization to ",outfile);
		ArrayData.write(arraydata,file.path(outpath,outfile));
	}

	# LLS missing value imputation (will not run on complete data matrices)
	if (arraydata.nmiss>0) {
		log.message("Running LLS missing value imputation...");
		tags=c(tags,'missimp');
		x=llsImpute(t(ArrayData.last(arraydata)),k=k);
		ArrayData.add(arraydata,"missimp",t(x@completeObs));
		ArrayData.setAttr(arraydata,key='tags',value=tags);
		rm(x);
		if (!is.null(outpath)) {
			outfile=file.tag.path(c(filename,tags,"log2.dat"));
			log.message("Writing LLS imputed data set to ",outfile,".");
			ArrayData.write(arraydata,file.path(outpath,outfile))
		}
	}

	# Centered data
	log.message("Centering data...");
	tags=c(tags,'cent');
	x=centerBoth(ArrayData.last(arraydata))
	ArrayData.add(arraydata,"cent",x)
	ArrayData.setAttr(arraydata,key='tags',value=tags);
	if (!is.null(outpath)) {
		outfile=file.tag.path(c(filename,tags,"log2.dat"));
		log.message("Writing centered data to ",outfile,".");
		ArrayData.write(arraydata,file.path(outpath,outfile))
	}

	log.message("DONE!");

	invisible(arraydata);

}
### End Array.normalize

Last modified March 06, 2014.