MicroArrayAggregate.r

### Procedures for aggregating micro array data
#

ArrayData.aggregate=function(arraydata,names=NULL
			,map=NULL,from=NULL,to=NULL,label=NULL
			,filename=NULL,path=Array.normalize.default.path
			,except.names=c())
{
	if (is.null(names)) names=setdiff(arraydata$arraylist,except.names);
	if (is.null(label)) {if (is.character(to)) label=sub("^\\w+",to,arraydata$label) else label="";}
	if (!is.null(map)) {
		if (is.null(from)) fromlist=rownames(map) else if (is.character(from)) fromlist=map[[from]] else fromlist=from;
		if (is.null(to)) tolist=as.vector(map) else if (is.character(to)) tolist=map[[to]] else tolist=to;
	}
	aggrdata=ArrayData.new(label=label);
	for (name in names) {
		x=ArrayData.get(arraydata,name=name);
		if (sum(is.na(x))>0) {
			log.message("Skipping ",name,": matrix has missing values");
			next;
		}
		log.message("Aggregating ",name,": ",label);
		tags=attr(x,'tags');
		message('Tags: ',paste(c(tags),sep='+',collapse='+'));
		group=tolist[match(rownames(x),fromlist)];
		x=matrix.row.mean(x,group);
		ArrayData.add(aggrdata,name,x);
		ArrayData.setAttr(aggrdata,name=name,key='tags',value=tags);
		if (!is.null(filename)) {
			filepath=file.tag.path(filename,tags,'log2.dat',dir=path);
			ArrayData.write(aggrdata,name=name,label=label,file=filepath);
		}
		log.message("Done with ",name,".");
	}
	if (arraydata$last %in% aggrdata$arraylist) ArrayData.set(aggrdata,arraydata$last);
	invisible(aggrdata);
}
Last modified March 06, 2014.