11# ' Internal Function
2- # '
2+ # '
33# ' This function is used internally in scone to parse the variables used to generate the design matrices.
4- # '
4+ # '
55# ' @param pars character. A vector of parameters corresponding to a row of params.
66# ' @param bio factor. The biological factor of interest.
77# ' @param batch factor. The known batch effects.
88# ' @param ruv_factors list. A list containing the factors of unwanted variation.
99# ' @param qc matrix. The principal components of the QC metrics.
10- # '
10+ # '
1111# ' @return A list with the variables to be passed to make_design.
1212parse_row <- function (pars , bio , batch , ruv_factors , qc ) {
1313 sc_name <- paste(pars [1 : 2 ], collapse = " _" )
14-
14+
1515 W <- out_bio <- out_batch <- NULL
16-
16+
1717 if (pars [3 ]!= " no_uv" ) {
1818 parsed <- strsplit(as.character(pars [3 ]), " =" )[[1 ]]
1919 if (grepl(" ruv" , parsed [1 ])) {
@@ -22,34 +22,34 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
2222 W <- qc [,1 : as.numeric(parsed [2 ])]
2323 }
2424 }
25-
25+
2626 if (pars [4 ]== " bio" ) {
2727 out_bio <- bio
2828 }
29-
29+
3030 if (pars [5 ]== " batch" ) {
3131 out_batch <- batch
3232 }
33-
33+
3434 return (list (sc_name = sc_name , W = W , bio = out_bio , batch = out_batch ))
3535}
3636
3737# ' Function to make a design matrix
38- # '
38+ # '
3939# ' This function is useful to create a design matrix, when the covariates are two (possibly nested) factors
4040# ' and one or more continuous variables.
41- # '
41+ # '
4242# ' @details If nested=TRUE a nested design is used, i.e., the batch variable is assumed to be nested within
4343# ' the bio variable. Here, nested means that each batch is made of observations from only one level of bio,
4444# ' while each level of bio may contain multiple batches.
45- # '
45+ # '
4646# ' @export
47- # '
47+ # '
4848# ' @param bio factor. The biological factor of interest.
4949# ' @param batch factor. The known batch effects.
5050# ' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUV factors).
5151# ' @param nested logical. Whether or not to consider a nested design (see details).
52- # '
52+ # '
5353# ' @return The design matrix.
5454make_design <- function (bio , batch , W , nested = FALSE ) {
5555 if (nested & (is.null(bio ) | is.null(batch ))) {
@@ -65,7 +65,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
6565 stop(" batch must be a factor." )
6666 }
6767 }
68-
68+
6969 f <- " ~ 1"
7070 if (! is.null(bio )) {
7171 f <- paste(f , " bio" , sep = " +" )
@@ -76,12 +76,12 @@ make_design <- function(bio, batch, W, nested=FALSE) {
7676 if (! is.null(W )) {
7777 f <- paste(f , " W" , sep = " +" )
7878 }
79-
79+
8080 if (is.null(bio ) & is.null(batch ) & is.null(W )) {
8181 return (NULL )
8282 } else if (! is.null(bio ) & ! is.null(batch ) & nested ) {
8383 n_vec <- tapply(batch , bio , function (x ) nlevels(droplevels(x )))
84-
84+
8585 mat = matrix (0 ,nrow = sum(n_vec ),ncol = sum(n_vec - 1 ))
8686 xi = 1
8787 yi = 1
@@ -96,25 +96,25 @@ make_design <- function(bio, batch, W, nested=FALSE) {
9696 xi = xi + 1
9797 }
9898 }
99-
100- return (model.matrix(as.formula(f ), contrasts = list (bio = contr.sum , batch = mat )))
99+
100+ return (model.matrix(as.formula(f ), contrasts = list (bio = contr.sum , batch = mat )))
101101 } else {
102- return (model.matrix(as.formula(f )))
102+ return (model.matrix(as.formula(f )))
103103 }
104104}
105105
106106# ' Function to perform linear batch effect correction
107- # '
107+ # '
108108# ' Given a matrix with log expression values and a design matrix, this function fits a linear model
109109# ' and removes the effects of the batch factor as well as of the linear variables encoded in W.
110- # '
110+ # '
111111# ' @details The function assumes that the columns of the design matrix corresponding to the variable
112112# ' for which expression needs to be adjusted, start with either the word "batch" or the letter "W" (case sensitive).
113113# ' Any other covariate (including the intercept) is kept.
114- # '
114+ # '
115115# ' @importFrom limma lmFit
116116# ' @export
117- # '
117+ # '
118118# ' @param log_expr matrix. The log gene expression (genes in row, samples in columns).
119119# ' @param design_mat matrix. The design matrix (usually the result of make_design).
120120# ' @param batch factor. A factor with the batch information.
@@ -125,13 +125,13 @@ lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
125125
126126 uvind <- grep(" ^W" , colnames(design_mat ))
127127 bind <- grep(" ^batch" , colnames(design_mat ))
128-
128+
129129 if (length(uvind )) {
130130 uv_term <- t(design_mat [,uvind ] %*% t(lm_object $ coefficients [,uvind ]))
131131 } else {
132132 uv_term <- 0
133133 }
134-
134+
135135 if (length(bind )) {
136136 if (is.character(attr(design_mat ," contrasts" )$ batch )) {
137137 contr <- get(attr(design_mat ," contrasts" )$ batch )(nlevels(batch ))
@@ -142,6 +142,6 @@ lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
142142 } else {
143143 batch_term <- 0
144144 }
145-
146- log_norm <- log_expr - batch_term - uv_term
145+
146+ return ( log_expr - batch_term - uv_term )
147147}
0 commit comments