esApply               package:Biobase               R Documentation

_A_n _a_p_p_l_y-_l_i_k_e _f_u_n_c_t_i_o_n _f_o_r _E_x_p_r_e_s_s_i_o_n_S_e_t _a_n_d _r_e_l_a_t_e_d _s_t_r_u_c_t_u_r_e_s.

_D_e_s_c_r_i_p_t_i_o_n:

     'esApply' is a wrapper to 'apply' for use with 'ExpressionSet's.
     The application of a function to rows of an expression array
     usually involves variables in 'pData'. 'esApply' uses a special
     evaluation paradigm to make this easy. The function 'FUN' may
     reference any data in 'pData' by name.

_U_s_a_g_e:

     esApply(X, MARGIN, FUN, ...)

_A_r_g_u_m_e_n_t_s:

       X: An instance of class 'ExpressionSet'.

  MARGIN: The margin to apply to, either 1 for rows (samples) or 2 for
          columns (features).

     FUN: Any function 

     ...: Additional parameters for 'FUN'.

_D_e_t_a_i_l_s:

     The 'pData' from 'X' is installed in an environment. This
     environment is installed as the environment of 'FUN'. This will
     then provide bindings for any symbols in 'FUN' that are the same
     as the names of the 'pData' of 'X'. If 'FUN' has an environment
     already it is retained but placed after the newly created
     environment. Some variable shadowing could occur under these
     circumstances.

_V_a_l_u_e:

     The result of 'with(pData(x), apply(exprs(X), MARGIN, FUN, ...))'.

_A_u_t_h_o_r(_s):

     V.J. Carey <stvjc@channing.harvard.edu>, R. Gentleman

_S_e_e _A_l_s_o:

     'apply', 'ExpressionSet'

_E_x_a_m_p_l_e_s:

     data(sample.ExpressionSet)
     ## sum columns of exprs
     res <- esApply(sample.ExpressionSet, 1, sum)

     ## t-test, spliting samples by 'sex'
     f <- function(x) {
         xx <- split(x, sex)
         t.test(xx[[1]], xx[[2]])$p.value
     }
     res <- esApply(sample.ExpressionSet, 1, f)

     ## same, but using a variable passed in the function call

     f <- function(x, s) {
         xx <- split(x, s)
         mean(xx[[1]]) - mean(xx[[2]])
     }
     sex <- sample.ExpressionSet[["sex"]]
     res <- esApply(sample.ExpressionSet, 1, f, s = sex)

     # obtain the p-value of the t-test for sex difference
     mytt.demo <- function(y) {
      ys <- split(y, sex)
      t.test(ys[[1]], ys[[2]])$p.value
     }
     sexPValue <- esApply(sample.ExpressionSet, 1, mytt.demo)

     # obtain the p-value of the slope associated with score, adjusting for sex
     # (if we were concerned with sign we could save the z statistic instead at coef[3,3]
     myreg.demo <- function(y) {
        summary(lm(y ~ sex + score))$coef[3,4]
     }
     scorePValue <- esApply(sample.ExpressionSet, 1, myreg.demo)

     # a resampling method
     resamp <- function(ESET) {
      ntiss <- ncol(exprs(ESET))
      newind <- sample(1:ntiss, size = ntiss, replace = TRUE)
      ESET[newind,]
     }

     # a filter
     q3g100filt <- function(eset) {
      apply(exprs(eset), 1, function(x) quantile(x,.75) > 100)
     }

     # filter after resampling and then apply
     set.seed(123)
     rest <- esApply({bool <- q3g100filt(resamp(sample.ExpressionSet)); sample.ExpressionSet[bool,]},
                     1, mytt.demo)

