R/virtualArrayBuildfData.R

virtualArrayBuildfData <-
function(x,identifier="SYMBOL",collapse_fun=median) {
cat("\nNow preprocessing raw data of ",as.character(x),": Loading annotations...\n")
identifier <- identifier
z <- NULL
z <- eval(as.symbol(x))
annot <- annotation(eval(as.symbol((x))))
annot_identifier <- paste(annot,identifier,sep="")
annot_package <- sub(" ",".",paste(annot,"db"))
if(is.na((grep(pattern=as.character(annot_package),installed.packages())[1])))
	{
		cat("\nPackage",annot_package,"not available. Atempting to install it.\n")
		source("http://www.bioconductor.org/biocLite.R")
		# biocLite(pkgs=annot_package,suppressUpdates=TRUE,suppressAutoUpdates=TRUE,ask=FALSE,dependencies=c("Depends","Imports"))
        biocLite(pkgs=annot_package)
	}
require(package=annot_package,character.only=TRUE)
annot_identifier <- toTable(eval(as.symbol(annot_identifier)))
fData(z) <- annot_identifier
# annot_identifier <- toggleProbes(value="all",x=eval(as.symbol(annot_identifier)))
# fData(z) <- toTable(annot_identifier)

# subset z
match_fData_y <- match(rownames(exprs(z)), fData(z)[,1])
match_fData_y <- na.omit(match_fData_y)
z <- z[match_fData_y,]

exprs_z <- as.data.frame(exprs(z))
exprs_z <- cbind(identifier=fData(z)[,2],exprs_z,row.names=NULL)

cat("\nNow preprocessing raw data of ",as.character(x),": Collapsing expression values to their ",as.character(as.list(collapse_fun)[[4]])[2],"...\n")
exprs_z <- recast(data=exprs_z,formula=identifier~variable,fun.aggregate=collapse_fun)
cat("\nNow preprocessing raw data of ",as.character(x),": Annotating expression values with ",as.character(identifier),"...\n")

# match_fData_y2 <- match(exprs_z[,1],fData(z)[,2])
# match_fData_y2 <- na.omit(match_fData_y2)
# match_fData_y2.id <- as.character(exprs_z[match_fData_y2, 1])
# fData(z) <- fData(z)[match_fData_y2,]

fData(z) <- fData(z)[fData(z)[,2]%in%exprs_z$identifier, ]
fData(z) <- fData(z)[!duplicated(fData(z)[,2]), ]
rownames(fData(z)) <- fData(z)[,2]

rownames(exprs_z) <- exprs_z$identifier
exprs_z <- as.matrix(exprs_z[, -1])

z <- z[1:dim(exprs_z)[1],]
dimnames(z)[[1]] <- fData(z)[,2]
# featureNames(z) <- fData(z)[,1]
exprs(z) <- exprs_z
# exprs_z <- data.matrix(exprs_z, rownames.force = TRUE)
# creat a new object
# z2 <- ExpressionSet(assayData = exprs_z, phenoData = z@phenoData,
#                     featureData = z@featureData, experimentData = z@experimentData,
#                     annotation = z@annotation, protocolData = z@protocolData)
cat("\nSize of expression matrix of",as.character(x),":",dim(exprs(z))[1],"rows and",dim(exprs(z))[2],"columns\n")
x <- z
#x <- as.data.frame(exprs(x))
return(x)
}
ShixiangWang/arrayConnector documentation built on May 14, 2019, 6:02 a.m.