R/Bioconductor: Applying Limma Functions To A Matrix.
0
0
Entering edit mode
11.2 years ago
pld 5.1k

I am new to R (Python and C background). I have been trying to write a script for benchmarking the performance of combinations of background correction and normalization methods for expression analysis. My idea is to populate a matrix with copies of the array data from read.maimages() and then apply the background correction methods across the rows and the normalization methods across the columns of the matrix.

The problem I am encountering is that the data seems to disappear once it reaches the functions I made for applying. All I get is an error stating "Execution Halted".

library(vsn)
library(limma)
set.seed(0xabcd) #from the vsn manual

#constants
A_SUB__ <- 1
A_NXP__ <- 2
A_MIN__ <- 3
A_VSN__ <- 1
A_QNT__ <- 2
A_LWS__ <- 3
M_SUB__ <- "subtract"
M_NXP__ <- "normexp"
M_MIN__ <- "minimum"
M_LWS__ <- "cyclicloess"
M_QNT__ <- "quantile"
COL__   <- list(G="Raw intensity (tr.mean) {A}",Gb="Background (tr.mean) {A}")
ANO__   <- c("Position", "Name", "ID", "Description", "GeneName")
N_BGM__ <- 3 #number of background correction methods
N_NRM__ <- 3 #number of normalization methods
A_BGM__ <- c(A_SUB__, A_NXP__, A_MIN__)
A_NRM__ <- c(A_VSN__, A_QNT__, A_LWS__)


#utility methods
bg_subtract <- function(x){
    return(backgroundCorrect(x, method = M_SUB__))
}

bg_normexp <- function(x){
    return(backgroundCorrect(x, method = M_NXP__))
}

bg_minimum <- function(x){
    return(backgroundCorrect(x, method = M_MIN__))
}

nb_vsn <- function(x){
    return(normalizeVSN(x))
}

nb_lowess <- function(x){
    return(normalizeBetweenArrays(x, method = M_LWS__))
}

nb_quantile <- function(x){
    return(normalizeBetweenArrays(x, method = M_QNT__))
}


###############################################################################
# Function to parse array data into limma objects
# Args:
#     tfile:   target file (see limma documentation)
#     tsep:    limma defaults TSV, have to set manually for CSV and so on
#     galfile: GAL file for printer/layout
# Returns data object with targets, genes and printer layout
###############################################################################
parse <- function(tfile, tsep, galfile){
    t                <- readTargets(tfile, sep=tsep)
    data             <- read.maimages(t,columns=COL__,
                                    annotation=ANO__,
                                    green.only=TRUE)
    data$raw         <- c(data$E,data$Eb)
    data$targets     <- t
    data$genes       <- readGAL(galfile)
    data$printer     <- getLayout(data$genes)
    data$normalized  <- FALSE
    data$bgcorrected <- FALSE
    data$processed   <- matrix(NA,9,
                               nrow = N_BGM__,
                               ncol = N_NRM__)
    data$processed   <- sapply(data$processed, function(x){x <- data$raw})
    return(data)
}

###############################################################################
# Function to perform three types of background correction
# Args:
#    data: array data object
# Adds $bg to data containing subtraction, normexp and minimum corrected
###############################################################################
background <- function(x){
    x$processed <- lapply(x$processed[A_SUB__,], bg_subtract)
    x$processed <- sapply(x$processed[A_NXP__,], bg_normexp)
    x$processed <- sapply(x$processed[A_MIN__,], bg_minimum)
    x$bgcorrected <- TRUE
}


###############################################################################
# Function to normalize array data with three types of normalization across
# all types of background corrected data
# Args:
#    data: array data object
# Adds $norm to data containing VSN, Cyclic Lowess and Quantile normalized data
###############################################################################
normalize <- function(x){
    x$processed <- sapply(x$processed[,A_VSN__], nb_vsn)
    x$processed <- sapply(x$processed[,A_LWS__], nb_lowess)
    x$processed <- sapply(x$processed[,A_QNT__], nb_quantile)
    x$normalized <- TRUE
}


###############################################################################
# Function to generate a set of plots for a given array data object
# Args:
#     data: array data object
#     name: the name of the data
###############################################################################
plt <- function(data, name){
   jpeg(paste(c(name, "-meansd.jpeg"), collapse=""))
   meanSdPlot(data$E)
   dev.off()
   jpeg(paste(c(name, "-ma.jpeg"), collapse=""))
   plotMA(data$E)
   dev.off()
   jpeg(paste(c(name, "-density.jpeg"), collapse=""))
   plotDensities(c(data), col="green")
   dev.off()
}

#read command line arguments
args <- commandArgs(trailingOnly = TRUE)

#begin processing data
a <- parse(args[1], args[2], args[3])
summary(a)
summary(a$processed)
background(a)
normalize(a)
summary(a)

In background I have tried to use:

function(x){backgroundCorrect(x, method = M_SUB__)}

in place of

bg_subtract

There I get the error message:

Error in backgroundCorrect(x, method = M_SUB__) :
  subtractcorrection requires background intensities
Calls: background -> lapply -> lapply -> FUN -> backgroundCorrect
Execution halted

Which makes me think that my approach in general is flawed. My goal is to use something like lapply or sapply so I can easily parallelize these operations with Snow later on.

I made a similar approach using only numbers and it seems to work, I can't tell if this is an issue with understanding how R works or how limma works.

My question is: How can I populate a matrix with array data from limma and use one of R's apply functions to map methods across rows and columns of the matrix?

r bioconductor limma • 4.3k views
ADD COMMENT

Login before adding your answer.

Traffic: 2635 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6