Browse code

Modified documentation, more examples and exported getters, added links between SconeExperiment and methods, and apply getters to setValidate

mbcole authored on 14/12/2016 00:54:28
Showing 1 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 #' @rdname get_params
2 2
 #'   
3
-#' @param x an object of class \code{\link{sconeExperiment}}.
3
+#' @param x an object of class \code{\link{SconeExperiment}}.
4 4
 #'   
5 5
 #' @return A data.frame containing workflow parameters for each scone workflow.
6 6
 #'   
... ...
@@ -15,7 +15,7 @@ setMethod(
15 15
 
16 16
 #' @rdname get_scores
17 17
 #'   
18
-#' @param x an object of class \code{\link{sconeExperiment}}.
18
+#' @param x an object of class \code{\link{SconeExperiment}}.
19 19
 #'   
20 20
 #' @return \code{get_scores} returns a matrix with all (non-missing) scone 
21 21
 #'   scores, ordered by average score rank.
... ...
@@ -46,12 +46,15 @@ setMethod(
46 46
 
47 47
 #' @rdname get_negconruv
48 48
 #'   
49
-#' @param x an object of class \code{\link{sconeExperiment}}.
49
+#' @param x an object of class \code{\link{SconeExperiment}}.
50 50
 #'   
51 51
 #' @return NULL or a logical vector.
52 52
 #'   
53 53
 #' @return For \code{get_negconruv} the returned vector indicates which genes
54 54
 #'   are negative controls to be used for RUV.
55
+#'   
56
+#' @export
57
+#' 
55 58
 setMethod(
56 59
   f = "get_negconruv",
57 60
   signature = signature(x = "SconeExperiment"),
... ...
@@ -68,6 +71,9 @@ setMethod(
68 71
 #'   
69 72
 #' @return For \code{get_negconeval} the returned vector indicates which genes
70 73
 #'   are negative controls to be used for evaluation.
74
+#'
75
+#' @export
76
+#' 
71 77
 setMethod(
72 78
   f = "get_negconeval",
73 79
   signature = signature(x = "SconeExperiment"),
... ...
@@ -84,6 +90,9 @@ setMethod(
84 90
 #'   
85 91
 #' @return For \code{get_poscon} the returned vector indicates which genes are 
86 92
 #'   positive controls to be used for evaluation.
93
+#'
94
+#' @export
95
+#'
87 96
 setMethod(
88 97
   f = "get_poscon",
89 98
   signature = signature(x = "SconeExperiment"),
... ...
@@ -98,9 +107,12 @@ setMethod(
98 107
 
99 108
 #' @rdname get_qc
100 109
 #'   
101
-#' @param x an object of class \code{\link{sconeExperiment}}.
110
+#' @param x an object of class \code{\link{SconeExperiment}}.
102 111
 #'   
103 112
 #' @return NULL or the quality control (QC) metric matrix.
113
+#'
114
+#' @export
115
+#'
104 116
 setMethod(
105 117
   f = "get_qc",
106 118
   signature = signature(x = "SconeExperiment"),
... ...
@@ -116,9 +128,12 @@ setMethod(
116 128
 
117 129
 #' @rdname get_bio
118 130
 #'   
119
-#' @param x an object of class \code{\link{sconeExperiment}}.
131
+#' @param x an object of class \code{\link{SconeExperiment}}.
120 132
 #'   
121 133
 #' @return NULL or a factor containing bio or batch covariate.
134
+#'
135
+#' @export
136
+#'
122 137
 setMethod(
123 138
   f = "get_bio",
124 139
   signature = signature(x = "SconeExperiment"),
... ...
@@ -132,6 +147,9 @@ setMethod(
132 147
 )
133 148
 
134 149
 #' @rdname get_bio
150
+#'
151
+#' @export
152
+#'
135 153
 setMethod(
136 154
   f = "get_batch",
137 155
   signature = signature(x = "SconeExperiment"),
Browse code

Reformat lines and fixed bugs in easy wrapper

mbcole authored on 22/11/2016 22:48:59
Showing 1 changed files
... ...
@@ -1,11 +1,11 @@
1 1
 #' @rdname get_params
2
-#'
2
+#'   
3 3
 #' @param x an object of class \code{\link{sconeExperiment}}.
4
-#'
4
+#'   
5 5
 #' @return A data.frame containing workflow parameters for each scone workflow.
6
-#'
6
+#'   
7 7
 #' @export
8
-#'
8
+#' 
9 9
 setMethod(
10 10
   f = "get_params",
11 11
   signature = signature(x = "SconeExperiment"),
... ...
@@ -14,13 +14,14 @@ setMethod(
14 14
   })
15 15
 
16 16
 #' @rdname get_scores
17
-#'
17
+#'   
18 18
 #' @param x an object of class \code{\link{sconeExperiment}}.
19
-#'
20
-#' @return \code{get_scores} returns a matrix with all (non-missing) scone scores, ordered by average score rank.
21
-#'
19
+#'   
20
+#' @return \code{get_scores} returns a matrix with all (non-missing) scone 
21
+#'   scores, ordered by average score rank.
22
+#'   
22 23
 #' @export
23
-#'
24
+#' 
24 25
 setMethod(
25 26
   f = "get_scores",
26 27
   signature = signature(x = "SconeExperiment"),
... ...
@@ -30,11 +31,11 @@ setMethod(
30 31
 })
31 32
 
32 33
 #' @rdname get_scores
33
-#' 
34
+#'   
34 35
 #' @return \code{get_score_ranks} returns a vector of average score ranks.
35
-#'
36
+#'   
36 37
 #' @export
37
-#'
38
+#' 
38 39
 setMethod(
39 40
   f = "get_score_ranks",
40 41
   signature = signature(x = "SconeExperiment"),
... ...
@@ -44,13 +45,13 @@ setMethod(
44 45
 
45 46
 
46 47
 #' @rdname get_negconruv
47
-#'
48
+#'   
48 49
 #' @param x an object of class \code{\link{sconeExperiment}}.
49
-#'
50
+#'   
50 51
 #' @return NULL or a logical vector.
51
-#'
52
-#' @return For \code{get_negconruv} the returned vector indicates which genes are negative controls
53
-#'  to be used for RUV.
52
+#'   
53
+#' @return For \code{get_negconruv} the returned vector indicates which genes
54
+#'   are negative controls to be used for RUV.
54 55
 setMethod(
55 56
   f = "get_negconruv",
56 57
   signature = signature(x = "SconeExperiment"),
... ...
@@ -64,9 +65,9 @@ setMethod(
64 65
 )
65 66
 
66 67
 #' @rdname get_negconruv
67
-#'
68
-#' @return For \code{get_negconeval} the returned vector indicates which genes are negative controls
69
-#'  to be used for evaluation.
68
+#'   
69
+#' @return For \code{get_negconeval} the returned vector indicates which genes
70
+#'   are negative controls to be used for evaluation.
70 71
 setMethod(
71 72
   f = "get_negconeval",
72 73
   signature = signature(x = "SconeExperiment"),
... ...
@@ -80,9 +81,9 @@ setMethod(
80 81
 )
81 82
 
82 83
 #' @rdname get_negconruv
83
-#'
84
-#' @return For \code{get_poscon} the returned vector indicates which genes are positive controls
85
-#'  to be used for evaluation.
84
+#'   
85
+#' @return For \code{get_poscon} the returned vector indicates which genes are 
86
+#'   positive controls to be used for evaluation.
86 87
 setMethod(
87 88
   f = "get_poscon",
88 89
   signature = signature(x = "SconeExperiment"),
... ...
@@ -96,9 +97,9 @@ setMethod(
96 97
 )
97 98
 
98 99
 #' @rdname get_qc
99
-#'
100
+#'   
100 101
 #' @param x an object of class \code{\link{sconeExperiment}}.
101
-#'
102
+#'   
102 103
 #' @return NULL or the quality control (QC) metric matrix.
103 104
 setMethod(
104 105
   f = "get_qc",
... ...
@@ -114,9 +115,9 @@ setMethod(
114 115
 )
115 116
 
116 117
 #' @rdname get_bio
117
-#'
118
+#'   
118 119
 #' @param x an object of class \code{\link{sconeExperiment}}.
119
-#'
120
+#'   
120 121
 #' @return NULL or a factor containing bio or batch covariate.
121 122
 setMethod(
122 123
   f = "get_bio",
... ...
@@ -144,19 +145,22 @@ setMethod(
144 145
 )
145 146
 
146 147
 #' Parse rows
147
-#'
148
-#' This function is used internally in scone to parse the variables used to generate the design matrices.
149
-#'
150
-#' @param pars character. A vector of parameters corresponding to a row of workflow parameters.
148
+#' 
149
+#' This function is used internally in scone to parse the variables used to
150
+#' generate the design matrices.
151
+#' 
152
+#' @param pars character. A vector of parameters corresponding to a row of
153
+#'   workflow parameters.
151 154
 #' @param bio factor. The biological covariate.
152 155
 #' @param batch factor. The batch covariate.
153
-#' @param ruv_factors list. A list containing the factors of unwanted variation (RUVg) for all upstream workflows.
156
+#' @param ruv_factors list. A list containing the factors of unwanted variation
157
+#'   (RUVg) for all upstream workflows.
154 158
 #' @param qc matrix. The principal components of the QC metric matrix.
155
-#'
159
+#'   
156 160
 #' @return A list with the variables to be passed to make_design.
157
-#' 
161
+#'   
158 162
 #' @keywords internal
159
-#' 
163
+#'   
160 164
 .parse_row <- function(pars, bio, batch, ruv_factors, qc) {
161 165
   
162 166
   # Define upstream workflow: imputation x scaling
... ...
@@ -185,22 +189,33 @@ setMethod(
185 189
 }
186 190
 
187 191
 #' Make a Design Matrix
188
-#'
189
-#' This function builds a design matrix for the Adjustment Normalization Step, in which 
190
-#' covariates are two (possibly nested) categorical factors and one or more continuous variables.
191
-#'
192
-#' @details If nested=TRUE a nested design is used, i.e. the batch variable is assumed to be nested within
193
-#' the bio variable. Here, nested means that each batch is composed of samples from only *one* level of bio,
194
-#' while each level of bio may contain multiple batches.
195
-#'
192
+#' 
193
+#' This function builds a design matrix for the Adjustment Normalization Step,
194
+#' in which covariates are two (possibly nested) categorical factors and one or
195
+#' more continuous variables.
196
+#' 
197
+#' @details If nested=TRUE a nested design is used, i.e. the batch variable is
198
+#'   assumed to be nested within the bio variable. Here, nested means that each
199
+#'   batch is composed of samples from only *one* level of bio, while each
200
+#'   level of bio may contain multiple batches.
201
+#'   
196 202
 #' @export
197
-#'
203
+#' 
198 204
 #' @param bio factor. The biological covariate.
199 205
 #' @param batch factor. The batch covariate.
200
-#' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUVg factors).
201
-#' @param nested logical. Whether or not to consider a nested design (see details).
202
-#'
206
+#' @param W numeric. Either a vector or matrix containing one or more
207
+#'   continuous covariates (e.g. RUVg factors).
208
+#' @param nested logical. Whether or not to consider a nested design
209
+#'   (see details).
210
+#'   
203 211
 #' @return The design matrix.
212
+#'   
213
+#' @examples 
214
+#' 
215
+#' bio = as.factor(rep(c(1,2),each = 2))
216
+#' batch = as.factor(rep(c(1,2),2))
217
+#' design_mat = make_design(bio,batch, W = NULL)
218
+#' 
204 219
 make_design <- function(bio, batch, W, nested=FALSE) {
205 220
   
206 221
   if(nested & (is.null(bio) | is.null(batch))) {
... ...
@@ -251,29 +266,48 @@ make_design <- function(bio, batch, W, nested=FALSE) {
251 266
       }
252 267
     }
253 268
 
254
-    return(model.matrix(as.formula(f), contrasts=list(bio=contr.sum, batch=mat)))
269
+    return(model.matrix(as.formula(f), 
270
+                        contrasts=list(bio=contr.sum, batch=mat)))
255 271
   } else {
256 272
     return(model.matrix(as.formula(f)))
257 273
   }
258 274
 }
259 275
 
260 276
 #' Linear Adjustment Normalization
261
-#'
262
-#' Given a matrix with log expression values and a design matrix, this function fits a linear model
263
-#' and removes the effects of the batch factor as well as of the linear variables encoded in W.
264
-#'
265
-#' @details The function assumes that the columns of the design matrix corresponding to the variable
266
-#' for which expression needs to be adjusted, start with either the word "batch" or the letter "W" (case sensitive).
267
-#' Any other covariate (including the intercept) is kept.
268
-#'
277
+#' 
278
+#' Given a matrix with log expression values and a design matrix, this function
279
+#' fits a linear model and removes the effects of the batch factor
280
+#' as well as of the linear variables encoded in W.
281
+#' 
282
+#' @details The function assumes that the columns of the design matrix
283
+#'   corresponding to the variable for which expression needs to be adjusted,
284
+#'   start with either the word "batch" or the letter "W" (case sensitive). Any
285
+#'   other covariate (including the intercept) is kept.
286
+#'   
269 287
 #' @importFrom limma lmFit
270 288
 #' @export
271
-#'
272
-#' @param log_expr matrix. The log gene expression (genes in row, samples in columns).
273
-#' @param design_mat matrix. The design matrix (usually the result of make_design).
274
-#' @param batch factor. A factor with the batch information, identifying batch effect to be removed.
289
+#' 
290
+#' @param log_expr matrix. The log gene expression (genes in row, samples in
291
+#'   columns).
292
+#' @param design_mat matrix. The design matrix (usually the result of
293
+#'   make_design).
294
+#' @param batch factor. A factor with the batch information, identifying batch
295
+#'   effect to be removed.
275 296
 #' @param weights matrix. A matrix of weights.
276 297
 #' @return The corrected log gene expression.
298
+#'   
299
+#' @examples
300
+#' 
301
+#' set.seed(141)
302
+#' bio = as.factor(rep(c(1,2),each = 2))
303
+#' batch = as.factor(rep(c(1,2),2))
304
+#' design_mat = make_design(bio,batch, W = NULL)
305
+#' 
306
+#' log_expr = matrix(rnorm(20),ncol = 4)
307
+#' adjusted_log_expr = lm_adjust(log_expr = log_expr,
308
+#'   design_mat = design_mat,
309
+#'   batch = batch)
310
+#' 
277 311
 lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
278 312
   lm_object <- lmFit(log_expr, design = design_mat, weights = weights)
279 313
 
Browse code

Updated doumentations and S4 interface fixes

mbcole authored on 14/11/2016 22:31:36
Showing 1 changed files
... ...
@@ -2,8 +2,7 @@
2 2
 #'
3 3
 #' @param x an object of class \code{\link{sconeExperiment}}.
4 4
 #'
5
-#' @return A data.frame with the information of the normalization schemes that
6
-#'   scone has run.
5
+#' @return A data.frame containing workflow parameters for each scone workflow.
7 6
 #'
8 7
 #' @export
9 8
 #'
... ...
@@ -18,8 +17,7 @@ setMethod(
18 17
 #'
19 18
 #' @param x an object of class \code{\link{sconeExperiment}}.
20 19
 #'
21
-#' @return \code{get_scores} returns a matrix with the non-missing scone scores,
22
-#'   ordered by average score.
20
+#' @return \code{get_scores} returns a matrix with all (non-missing) scone scores, ordered by average score rank.
23 21
 #'
24 22
 #' @export
25 23
 #'
... ...
@@ -32,9 +30,8 @@ setMethod(
32 30
 })
33 31
 
34 32
 #' @rdname get_scores
35
-#'
36
-#' @return \code{get_score_ranks} returns a vector with the ranks of the average
37
-#'   score.
33
+#' 
34
+#' @return \code{get_score_ranks} returns a vector of average score ranks.
38 35
 #'
39 36
 #' @export
40 37
 #'
... ...
@@ -52,8 +49,8 @@ setMethod(
52 49
 #'
53 50
 #' @return NULL or a logical vector.
54 51
 #'
55
-#' @return For \code{get_negconruv} it indicates which genes are negative controls
56
-#'   to be used for RUV.
52
+#' @return For \code{get_negconruv} the returned vector indicates which genes are negative controls
53
+#'  to be used for RUV.
57 54
 setMethod(
58 55
   f = "get_negconruv",
59 56
   signature = signature(x = "SconeExperiment"),
... ...
@@ -68,8 +65,8 @@ setMethod(
68 65
 
69 66
 #' @rdname get_negconruv
70 67
 #'
71
-#' @return For \code{get_negconeval} it indicates which genes are negative controls
72
-#'   to be used for evaluation.
68
+#' @return For \code{get_negconeval} the returned vector indicates which genes are negative controls
69
+#'  to be used for evaluation.
73 70
 setMethod(
74 71
   f = "get_negconeval",
75 72
   signature = signature(x = "SconeExperiment"),
... ...
@@ -84,7 +81,8 @@ setMethod(
84 81
 
85 82
 #' @rdname get_negconruv
86 83
 #'
87
-#' @return For \code{get_poscon} it indicates which genes are positive controls.
84
+#' @return For \code{get_poscon} the returned vector indicates which genes are positive controls
85
+#'  to be used for evaluation.
88 86
 setMethod(
89 87
   f = "get_poscon",
90 88
   signature = signature(x = "SconeExperiment"),
... ...
@@ -101,7 +99,7 @@ setMethod(
101 99
 #'
102 100
 #' @param x an object of class \code{\link{sconeExperiment}}.
103 101
 #'
104
-#' @return NULL or the QC matrix.
102
+#' @return NULL or the quality control (QC) metric matrix.
105 103
 setMethod(
106 104
   f = "get_qc",
107 105
   signature = signature(x = "SconeExperiment"),
... ...
@@ -119,7 +117,7 @@ setMethod(
119 117
 #'
120 118
 #' @param x an object of class \code{\link{sconeExperiment}}.
121 119
 #'
122
-#' @return NULL or a factor containing the bio or batch information.
120
+#' @return NULL or a factor containing bio or batch covariate.
123 121
 setMethod(
124 122
   f = "get_bio",
125 123
   signature = signature(x = "SconeExperiment"),
... ...
@@ -149,22 +147,27 @@ setMethod(
149 147
 #'
150 148
 #' This function is used internally in scone to parse the variables used to generate the design matrices.
151 149
 #'
152
-#' @param pars character. A vector of parameters corresponding to a row of params.
153
-#' @param bio factor. The biological factor of interest.
154
-#' @param batch factor. The known batch effects.
155
-#' @param ruv_factors list. A list containing the factors of unwanted variation.
156
-#' @param qc matrix. The principal components of the QC metrics.
150
+#' @param pars character. A vector of parameters corresponding to a row of workflow parameters.
151
+#' @param bio factor. The biological covariate.
152
+#' @param batch factor. The batch covariate.
153
+#' @param ruv_factors list. A list containing the factors of unwanted variation (RUVg) for all upstream workflows.
154
+#' @param qc matrix. The principal components of the QC metric matrix.
157 155
 #'
158 156
 #' @return A list with the variables to be passed to make_design.
159
-parse_row <- function(pars, bio, batch, ruv_factors, qc) {
157
+#' 
158
+#' @keywords internal
159
+#' 
160
+.parse_row <- function(pars, bio, batch, ruv_factors, qc) {
161
+  
162
+  # Define upstream workflow: imputation x scaling
160 163
   sc_name <- paste(pars[1:2], collapse="_")
161 164
 
162 165
   W <- out_bio <- out_batch <- NULL
163
-
166
+  
164 167
   if(pars[3]!="no_uv") {
165 168
     parsed <- strsplit(as.character(pars[3]), "=")[[1]]
166 169
     if(grepl("ruv", parsed[1])) {
167
-      W <- ruv_factors[[sc_name]][,1:as.numeric(parsed[2])]
170
+      W <- ruv_factors[[sc_name]][,seq_len(as.numeric(parsed[2]))]
168 171
     } else {
169 172
       W <- qc[,seq_len(as.numeric(parsed[2]))]
170 173
     }
... ...
@@ -183,30 +186,33 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
183 186
 
184 187
 #' Make a Design Matrix
185 188
 #'
186
-#' This function is useful to create a design matrix, when the covariates are two (possibly nested) factors
187
-#' and one or more continuous variables.
189
+#' This function builds a design matrix for the Adjustment Normalization Step, in which 
190
+#' covariates are two (possibly nested) categorical factors and one or more continuous variables.
188 191
 #'
189
-#' @details If nested=TRUE a nested design is used, i.e., the batch variable is assumed to be nested within
190
-#' the bio variable. Here, nested means that each batch is made of observations from only one level of bio,
192
+#' @details If nested=TRUE a nested design is used, i.e. the batch variable is assumed to be nested within
193
+#' the bio variable. Here, nested means that each batch is composed of samples from only *one* level of bio,
191 194
 #' while each level of bio may contain multiple batches.
192 195
 #'
193 196
 #' @export
194 197
 #'
195
-#' @param bio factor. The biological factor of interest.
196
-#' @param batch factor. The known batch effects.
197
-#' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUV factors).
198
+#' @param bio factor. The biological covariate.
199
+#' @param batch factor. The batch covariate.
200
+#' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUVg factors).
198 201
 #' @param nested logical. Whether or not to consider a nested design (see details).
199 202
 #'
200 203
 #' @return The design matrix.
201 204
 make_design <- function(bio, batch, W, nested=FALSE) {
205
+  
202 206
   if(nested & (is.null(bio) | is.null(batch))) {
203 207
     stop("Nested design can be used only if both batch and bio are specified.")
204 208
   }
209
+  
205 210
   if(!is.null(bio)) {
206 211
     if(class(bio)!="factor") {
207 212
       stop("bio must be a factor.")
208 213
     }
209 214
   }
215
+  
210 216
   if(!is.null(batch)){
211 217
     if(class(batch)!="factor") {
212 218
       stop("batch must be a factor.")
... ...
@@ -227,6 +233,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
227 233
   if(is.null(bio) & is.null(batch) & is.null(W)) {
228 234
     return(NULL)
229 235
   } else if (!is.null(bio) & !is.null(batch) & nested) {
236
+    
230 237
     n_vec <- tapply(batch, bio, function(x) nlevels(droplevels(x)))
231 238
 
232 239
     mat = matrix(0,nrow = sum(n_vec),ncol = sum(n_vec - 1))
... ...
@@ -250,7 +257,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
250 257
   }
251 258
 }
252 259
 
253
-#' Linear Batch Effect Correction
260
+#' Linear Adjustment Normalization
254 261
 #'
255 262
 #' Given a matrix with log expression values and a design matrix, this function fits a linear model
256 263
 #' and removes the effects of the batch factor as well as of the linear variables encoded in W.
... ...
@@ -264,7 +271,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
264 271
 #'
265 272
 #' @param log_expr matrix. The log gene expression (genes in row, samples in columns).
266 273
 #' @param design_mat matrix. The design matrix (usually the result of make_design).
267
-#' @param batch factor. A factor with the batch information.
274
+#' @param batch factor. A factor with the batch information, identifying batch effect to be removed.
268 275
 #' @param weights matrix. A matrix of weights.
269 276
 #' @return The corrected log gene expression.
270 277
 lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
Browse code

Method for extracting params get_params()

Davide Risso authored on 07/10/2016 23:18:53
Showing 1 changed files
... ...
@@ -1,3 +1,19 @@
1
+#' @rdname get_params
2
+#'
3
+#' @param x an object of class \code{\link{sconeExperiment}}.
4
+#'
5
+#' @return A data.frame with the information of the normalization schemes that
6
+#'   scone has run.
7
+#'
8
+#' @export
9
+#'
10
+setMethod(
11
+  f = "get_params",
12
+  signature = signature(x = "SconeExperiment"),
13
+  definition = function(x) {
14
+    return(x@scone_params)
15
+  })
16
+
1 17
 #' @rdname get_scores
2 18
 #'
3 19
 #' @param x an object of class \code{\link{sconeExperiment}}.
Browse code

get_scores() method

Davide Risso authored on 07/10/2016 23:11:54
Showing 1 changed files
... ...
@@ -1,3 +1,35 @@
1
+#' @rdname get_scores
2
+#'
3
+#' @param x an object of class \code{\link{sconeExperiment}}.
4
+#'
5
+#' @return \code{get_scores} returns a matrix with the non-missing scone scores,
6
+#'   ordered by average score.
7
+#'
8
+#' @export
9
+#'
10
+setMethod(
11
+  f = "get_scores",
12
+  signature = signature(x = "SconeExperiment"),
13
+  definition = function(x) {
14
+      scores <- t(na.omit(t(x@scone_scores[,-NCOL(x@scone_scores)])))
15
+      return(scores)
16
+})
17
+
18
+#' @rdname get_scores
19
+#'
20
+#' @return \code{get_score_ranks} returns a vector with the ranks of the average
21
+#'   score.
22
+#'
23
+#' @export
24
+#'
25
+setMethod(
26
+  f = "get_score_ranks",
27
+  signature = signature(x = "SconeExperiment"),
28
+  definition = function(x) {
29
+    return(x@scone_scores[,NCOL(x@scone_scores)])
30
+  })
31
+
32
+
1 33
 #' @rdname get_negconruv
2 34
 #'
3 35
 #' @param x an object of class \code{\link{sconeExperiment}}.
Browse code

Added tests for the get_normalized() wrapper.

This exposed a few bugs and issues which are fixed with this
commit. I think it is now possible to close issues #36 and #49.

Davide Risso authored on 06/10/2016 19:54:58
Showing 1 changed files
... ...
@@ -118,7 +118,7 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
118 118
     if(grepl("ruv", parsed[1])) {
119 119
       W <- ruv_factors[[sc_name]][,1:as.numeric(parsed[2])]
120 120
     } else {
121
-      W <- qc[,1:as.numeric(parsed[2])]
121
+      W <- qc[,seq_len(as.numeric(parsed[2]))]
122 122
     }
123 123
   }
124 124
 
Browse code

Helper methods to retrieve content of scone slots

Davide Risso authored on 30/09/2016 19:41:51
Showing 1 changed files
... ...
@@ -1,4 +1,103 @@
1
-#' Internal Function
1
+#' @rdname get_negconruv
2
+#'
3
+#' @param x an object of class \code{\link{sconeExperiment}}.
4
+#'
5
+#' @return NULL or a logical vector.
6
+#'
7
+#' @return For \code{get_negconruv} it indicates which genes are negative controls
8
+#'   to be used for RUV.
9
+setMethod(
10
+  f = "get_negconruv",
11
+  signature = signature(x = "SconeExperiment"),
12
+  definition = function(x) {
13
+    if(length(x@which_negconruv) == 0) {
14
+      return(NULL)
15
+    } else {
16
+      return(rowData(x)[,x@which_negconruv])
17
+    }
18
+  }
19
+)
20
+
21
+#' @rdname get_negconruv
22
+#'
23
+#' @return For \code{get_negconeval} it indicates which genes are negative controls
24
+#'   to be used for evaluation.
25
+setMethod(
26
+  f = "get_negconeval",
27
+  signature = signature(x = "SconeExperiment"),
28
+  definition = function(x) {
29
+    if(length(x@which_negconeval) == 0) {
30
+      return(NULL)
31
+    } else {
32
+      return(rowData(x)[,x@which_negconeval])
33
+    }
34
+  }
35
+)
36
+
37
+#' @rdname get_negconruv
38
+#'
39
+#' @return For \code{get_poscon} it indicates which genes are positive controls.
40
+setMethod(
41
+  f = "get_poscon",
42
+  signature = signature(x = "SconeExperiment"),
43
+  definition = function(x) {
44
+    if(length(x@which_poscon) == 0) {
45
+      return(NULL)
46
+    } else {
47
+      return(rowData(x)[,x@which_poscon])
48
+    }
49
+  }
50
+)
51
+
52
+#' @rdname get_qc
53
+#'
54
+#' @param x an object of class \code{\link{sconeExperiment}}.
55
+#'
56
+#' @return NULL or the QC matrix.
57
+setMethod(
58
+  f = "get_qc",
59
+  signature = signature(x = "SconeExperiment"),
60
+  definition = function(x) {
61
+    if(length(x@which_qc) == 0) {
62
+      return(NULL)
63
+    } else {
64
+      retval <- as.matrix(colData(x)[, x@which_qc, drop=FALSE])
65
+      return(retval)
66
+    }
67
+  }
68
+)
69
+
70
+#' @rdname get_bio
71
+#'
72
+#' @param x an object of class \code{\link{sconeExperiment}}.
73
+#'
74
+#' @return NULL or a factor containing the bio or batch information.
75
+setMethod(
76
+  f = "get_bio",
77
+  signature = signature(x = "SconeExperiment"),
78
+  definition = function(x) {
79
+    if(length(x@which_bio) == 0) {
80
+      return(NULL)
81
+    } else {
82
+      return(colData(x)[,x@which_bio])
83
+    }
84
+  }
85
+)
86
+
87
+#' @rdname get_bio
88
+setMethod(
89
+  f = "get_batch",
90
+  signature = signature(x = "SconeExperiment"),
91
+  definition = function(x) {
92
+    if(length(x@which_batch) == 0) {
93
+      return(NULL)
94
+    } else {
95
+      return(colData(x)[,x@which_batch])
96
+    }
97
+  }
98
+)
99
+
100
+#' Parse rows
2 101
 #'
3 102
 #' This function is used internally in scone to parse the variables used to generate the design matrices.
4 103
 #'
... ...
@@ -34,7 +133,7 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
34 133
   return(list(sc_name=sc_name, W=W, bio=out_bio, batch=out_batch))
35 134
 }
36 135
 
37
-#' Function to make a design matrix
136
+#' Make a Design Matrix
38 137
 #'
39 138
 #' This function is useful to create a design matrix, when the covariates are two (possibly nested) factors
40 139
 #' and one or more continuous variables.
... ...
@@ -103,7 +202,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
103 202
   }
104 203
 }
105 204
 
106
-#' Function to perform linear batch effect correction
205
+#' Linear Batch Effect Correction
107 206
 #'
108 207
 #' Given a matrix with log expression values and a design matrix, this function fits a linear model
109 208
 #' and removes the effects of the batch factor as well as of the linear variables encoded in W.
Browse code

Fixes #13

Davide Risso authored on 28/04/2016 00:40:05
Showing 1 changed files
... ...
@@ -1,19 +1,19 @@
1 1
 #' Internal Function
2
-#' 
2
+#'
3 3
 #' This function is used internally in scone to parse the variables used to generate the design matrices.
4
-#' 
4
+#'
5 5
 #' @param pars character. A vector of parameters corresponding to a row of params.
6 6
 #' @param bio factor. The biological factor of interest.
7 7
 #' @param batch factor. The known batch effects.
8 8
 #' @param ruv_factors list. A list containing the factors of unwanted variation.
9 9
 #' @param qc matrix. The principal components of the QC metrics.
10
-#' 
10
+#'
11 11
 #' @return A list with the variables to be passed to make_design.
12 12
 parse_row <- function(pars, bio, batch, ruv_factors, qc) {
13 13
   sc_name <- paste(pars[1:2], collapse="_")
14
-  
14
+
15 15
   W <- out_bio <- out_batch <- NULL
16
-  
16
+
17 17
   if(pars[3]!="no_uv") {
18 18
     parsed <- strsplit(as.character(pars[3]), "=")[[1]]
19 19
     if(grepl("ruv", parsed[1])) {
... ...
@@ -22,34 +22,34 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
22 22
       W <- qc[,1:as.numeric(parsed[2])]
23 23
     }
24 24
   }
25
-  
25
+
26 26
   if(pars[4]=="bio") {
27 27
     out_bio <- bio
28 28
   }
29
-  
29
+
30 30
   if(pars[5]=="batch") {
31 31
     out_batch <- batch
32 32
   }
33
-  
33
+
34 34
   return(list(sc_name=sc_name, W=W, bio=out_bio, batch=out_batch))
35 35
 }
36 36
 
37 37
 #' Function to make a design matrix
38
-#' 
38
+#'
39 39
 #' This function is useful to create a design matrix, when the covariates are two (possibly nested) factors
40 40
 #' and one or more continuous variables.
41
-#' 
41
+#'
42 42
 #' @details If nested=TRUE a nested design is used, i.e., the batch variable is assumed to be nested within
43 43
 #' the bio variable. Here, nested means that each batch is made of observations from only one level of bio,
44 44
 #' while each level of bio may contain multiple batches.
45
-#'  
45
+#'
46 46
 #' @export
47
-#'  
47
+#'
48 48
 #' @param bio factor. The biological factor of interest.
49 49
 #' @param batch factor. The known batch effects.
50 50
 #' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUV factors).
51 51
 #' @param nested logical. Whether or not to consider a nested design (see details).
52
-#' 
52
+#'
53 53
 #' @return The design matrix.
54 54
 make_design <- function(bio, batch, W, nested=FALSE) {
55 55
   if(nested & (is.null(bio) | is.null(batch))) {
... ...
@@ -65,7 +65,7 @@ make_design <- function(bio, batch, W, nested=FALSE) {
65 65
       stop("batch must be a factor.")
66 66
     }
67 67
   }
68
-  
68
+
69 69
   f <- "~ 1"
70 70
   if(!is.null(bio)) {
71 71
     f <- paste(f, "bio", sep="+")
... ...
@@ -76,12 +76,12 @@ make_design <- function(bio, batch, W, nested=FALSE) {
76 76
   if(!is.null(W)) {
77 77
     f <- paste(f, "W", sep="+")
78 78
   }
79
-  
79
+
80 80
   if(is.null(bio) & is.null(batch) & is.null(W)) {
81 81
     return(NULL)
82 82
   } else if (!is.null(bio) & !is.null(batch) & nested) {
83 83
     n_vec <- tapply(batch, bio, function(x) nlevels(droplevels(x)))
84
-    
84
+
85 85
     mat = matrix(0,nrow = sum(n_vec),ncol = sum(n_vec - 1))
86 86
     xi = 1
87 87
     yi = 1
... ...
@@ -96,25 +96,25 @@ make_design <- function(bio, batch, W, nested=FALSE) {
96 96
         xi = xi + 1
97 97
       }
98 98
     }
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)))
101 101
   } else {
102
-    return(model.matrix(as.formula(f)))    
102
+    return(model.matrix(as.formula(f)))
103 103
   }
104 104
 }
105 105
 
106 106
 #' Function to perform linear batch effect correction
107
-#' 
107
+#'
108 108
 #' Given a matrix with log expression values and a design matrix, this function fits a linear model
109 109
 #' and removes the effects of the batch factor as well as of the linear variables encoded in W.
110
-#' 
110
+#'
111 111
 #' @details The function assumes that the columns of the design matrix corresponding to the variable
112 112
 #' for which expression needs to be adjusted, start with either the word "batch" or the letter "W" (case sensitive).
113 113
 #' Any other covariate (including the intercept) is kept.
114
-#'  
114
+#'
115 115
 #' @importFrom limma lmFit
116 116
 #' @export
117
-#' 
117
+#'
118 118
 #' @param log_expr matrix. The log gene expression (genes in row, samples in columns).
119 119
 #' @param design_mat matrix. The design matrix (usually the result of make_design).
120 120
 #' @param batch factor. A factor with the batch information.
... ...
@@ -125,13 +125,13 @@ lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
125 125
 
126 126
   uvind <- grep("^W", colnames(design_mat))
127 127
   bind <- grep("^batch", colnames(design_mat))
128
-  
128
+
129 129
   if(length(uvind)) {
130 130
     uv_term <- t(design_mat[,uvind] %*% t(lm_object$coefficients[,uvind]))
131 131
   } else {
132 132
     uv_term <- 0
133 133
   }
134
-  
134
+
135 135
   if(length(bind)) {
136 136
     if(is.character(attr(design_mat,"contrasts")$batch)) {
137 137
       contr <- get(attr(design_mat,"contrasts")$batch)(nlevels(batch))
... ...
@@ -142,6 +142,6 @@ lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
142 142
   } else {
143 143
     batch_term <- 0
144 144
   }
145
-  
146
-  log_norm <- log_expr - batch_term  - uv_term
145
+
146
+  return(log_expr - batch_term  - uv_term)
147 147
 }
Browse code

Moved files for package building

Davide Risso authored on 07/02/2016 06:34:07
Showing 1 changed files
... ...
@@ -2,7 +2,6 @@
2 2
 #' 
3 3
 #' This function is used internally in scone to parse the variables used to generate the design matrices.
4 4
 #' 
5
-#'  
6 5
 #' @param pars character. A vector of parameters corresponding to a row of params.
7 6
 #' @param bio factor. The biological factor of interest.
8 7
 #' @param batch factor. The known batch effects.
... ...
@@ -44,6 +43,8 @@ parse_row <- function(pars, bio, batch, ruv_factors, qc) {
44 43
 #' the bio variable. Here, nested means that each batch is made of observations from only one level of bio,
45 44
 #' while each level of bio may contain multiple batches.
46 45
 #'  
46
+#' @export
47
+#'  
47 48
 #' @param bio factor. The biological factor of interest.
48 49
 #' @param batch factor. The known batch effects.
49 50
 #' @param W numeric. Either a vector or matrix containing one or more continuous covariates (e.g. RUV factors).
... ...
@@ -111,11 +112,15 @@ make_design <- function(bio, batch, W, nested=FALSE) {
111 112
 #' for which expression needs to be adjusted, start with either the word "batch" or the letter "W" (case sensitive).
112 113
 #' Any other covariate (including the intercept) is kept.
113 114
 #'  
115
+#' @importFrom limma lmFit
116
+#' @export
117
+#' 
114 118
 #' @param log_expr matrix. The log gene expression (genes in row, samples in columns).
115 119
 #' @param design_mat matrix. The design matrix (usually the result of make_design).
116
-#' 
120
+#' @param batch factor. A factor with the batch information.
121
+#' @param weights matrix. A matrix of weights.
117 122
 #' @return The corrected log gene expression.
118
-lm_adjust <- function(log_expr, design_mat, weights=NULL) {
123
+lm_adjust <- function(log_expr, design_mat, batch=NULL, weights=NULL) {
119 124
   lm_object <- lmFit(log_expr, design = design_mat, weights = weights)
120 125
 
121 126
   uvind <- grep("^W", colnames(design_mat))
Browse code

cp from YosefCode

Michael Cole authored on 05/02/2016 02:05:20
Showing 1 changed files