File: Bioc-layer.R

package info (click to toggle)
r-cran-nmf 0.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,344 kB
  • sloc: cpp: 680; ansic: 7; makefile: 2
file content (325 lines) | stat: -rw-r--r-- 9,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Layer for Bioconductor
# 
# - define methods with signature for use within Bioconductor
# - define alias methods for use in the context of microarray analysis (metagenes, metaprofiles, ...)
# 
# Author: Renaud Gaujoux \email{renaud@@cbio.uct.ac.za}
###############################################################################

#' @include NMF-class.R
#' @include transforms.R
NULL

#' Specific NMF Layer for Bioconductor 
#' 
#' The package NMF provides an optional layer for working with common objects
#' and functions defined in the Bioconductor platform.
#' 
#' It provides:
#' \itemize{
#' \item computation functions that support \code{ExpressionSet} objects as
#' inputs.
#' \item aliases and methods for generic functions defined and widely used by
#' Bioconductor base packages.
#' \item specialised visualisation methods that adapt the titles and legend
#' using bioinformatics terminology.
#' \item functions to link the results with annotations, etc...
#' }
#' 
#' @rdname bioc
#' @name bioc-NMF
#' 
#' @aliases nmf,ExpressionSet,ANY,ANY-method
#' @aliases nmf,matrix,ExpressionSet,ANY-method
#' 
#' @aliases seed,ExpressionSet,ANY,ANY-method
#' 
#' @aliases run,NMFStrategy,ExpressionSet,ANY-method
#' 
#' @aliases nmfModel,ExpressionSet,ANY-method
#' @aliases nmfModel,ANY,ExpressionSet-method
#' 
#' @aliases rnmf,ANY,ExpressionSet-method
#' 
#' @aliases nneg,ExpressionSet-method
#' @aliases rposneg,ExpressionSet-method
#' 
#' @aliases .atrack,ExpressionSet-method
#' 
#' @aliases sampleNames,NMF-method
#' @aliases sampleNames<-,NMF,ANY-method
#' @aliases sampleNames,NMFfitX-method
#' @aliases featureNames,NMF-method
#' @aliases featureNames<-,NMF-method
#' @aliases featureNames,NMFfitX-method
#' 
#' @aliases nmeta
#' @aliases metagenes metagenes<-
#' @aliases metaprofiles metaprofiles<-
#' 
#' @exportPattern ^featureNames
#' @exportPattern ^sampleNames
#' @exportPattern ^metagenes
#' @exportPattern ^metaprofiles
#' @exportPattern ^nmeta
NULL

# add extra package Biobase
#setPackageExtra('install.packages', 'Biobase', pkgs='Biobase')
if(!requireNamespace("Biobase")) BiocManager::install("Biobase")

.onLoad.nmf.bioc <- function(){
	
if( pkgmaker::require.quiet('Biobase') ){

	# load Biobase package
	requireNamespace('Biobase')
	#library(Biobase)

	#' Performs NMF on an ExpressionSet: the target matrix is the expression matrix \code{exprs(x)}.
	#' @rdname bioc
	setMethod('nmf', signature(x='ExpressionSet', rank='ANY', method='ANY'), 
		function(x, rank, method, ...)
		{
			# replace missing values by NULL values for correct dispatch
			if( missing(method) ) method <- NULL
			if( missing(rank) ) rank <- NULL
			
			# apply NMF to the gene expression matrix			
			nmf(Biobase::exprs(x), rank, method, ...)
		}
	)
	
	#' Fits an NMF model partially seeding the computation with a given 
	#' ExpressionSet object passed in \code{rank}.
	#' 
	#' This method provides a shortcut for \code{nmf(x, exprs(rank), method, ...)}. 
	#' 
	#' @examples
	#' # partially seed with an ExpressionSet (requires package Biobase)
	#' \dontrun{
	#' if(requireNamespace("Biobase")) BiocManager::install("Biobase"){
	#' data(esGolub)
	#' nmf(esGolub, esGolub[,1:3])
	#' }
	#' }
	#' 
	setMethod('nmf', signature(x='matrix', rank='ExpressionSet', method='ANY'),
		function(x, rank, method, ...){
			# replace missing values by NULL values for correct dispatch
			if( missing(method) ) method <- NULL
			
			nmf(x, Biobase::exprs(rank), method, ...)
		}
	)
	
	
	#' Seeds an NMF model directly on an ExpressionSet object.
	#' This method provides a shortcut for \code{seed(exprs(x), model, method, ...)}. 
	#' 
	#' @examples
	#' # run on an ExpressionSet (requires package Biobase)
	#' \dontrun{
	#' if(requireNamespace("Biobase")) BiocManager::install("Biobase"){
	#' data(esGolub)
	#' nmf(esGolub, 3)
	#' }
	#' }
	#' 
	setMethod('seed', signature(x='ExpressionSet', model='ANY', method='ANY'), 
		function(x, model, method, ...)
		{
			# replace missing values by NULL values for correct dispatch
			if( missing(method) ) method <- NULL
			if( missing(model) ) model <- NULL
			
			# apply NMF to the gene expression matrix			
			seed(Biobase::exprs(x), model, method, ...)
		}
	)
	
	#' Runs an NMF algorithm on the expression matrix of an \code{ExpressionSet} object.
	setMethod('run', signature(object='NMFStrategy', y='ExpressionSet', x='ANY'),
		function(object, y, x, ...){
			
			run(object, Biobase::exprs(y), x, ...)
			
		}
	)
		
	###% Method 'nmfModel' for 'ExpressionSet' target objects: 
	###% -> use the expression matrix of 'target' as the target matrix
	setMethod('nmfModel', signature(rank='ANY', target='ExpressionSet'),
			function(rank, target, ...){
				if( missing(rank) ) rank <- NULL
				# call nmfModel on the expression matrix
				nmfModel(rank, Biobase::exprs(target), ...)
			}	
	)
	setMethod('nmfModel', signature(rank='ExpressionSet', target='ANY'),
			function(rank, target, ...){
				if( missing(target) ) target <- NULL
				# call nmfModel on the expression matrix
				nmfModel(Biobase::exprs(rank), target, ...)
			}	
	)	
	
	###% Method 'rnmf' for 'ExpressionSet' target objects: 
	###% -> use the expression matrix of 'target' as the target matrix
	###% 
	setMethod('rnmf', signature(x='ANY', target='ExpressionSet'), 
		function(x, target, ...){
			rnmf(x, Biobase::exprs(target), ...)
		}
	)
	
	###% The method for an \code{ExpressionSet} object returns the data.frame that 
	###% contains the phenotypic data (i.e. \code{pData(object)})
	setMethod('.atrack', 'ExpressionSet', 
		function(object, data=NULL, ...){
			if( is.null(data) ) data <- t(Biobase::exprs(object))
			.atrack(Biobase::pData(object), data=data, ...)	
		}
	)
	
	#' Apply \code{nneg} to the expression matrix of an \code{\link{ExpressionSet}} 
	#' object (i.e. \code{exprs(object)}). 
	#' All extra arguments in \code{...} are passed to the method \code{nneg,matrix}.
	#' 
	#' @examples
	#' 
	#' E <- Biobase::ExpressionSet(x)
	#' nnE <- nneg(e)
	#' exprs(nnE)
	#' 
	setMethod('nneg', 'ExpressionSet'
			, function(object, ...){
				Biobase::exprs(object) <- nneg(Biobase::exprs(object), ...)
				object
			}
	)
	
	#' Apply \code{rposneg} to the expression matrix of an \code{\link{ExpressionSet}} 
	#' object (i.e. \code{exprs(object)}). 
	#' 
	#' @examples
	#' 
	#' E <- Biobase::ExpressionSet(x)
	#' nnE <- posneg(E)
	#' E2 <- rposneg(nnE)
	#' all.equal(E, E2)
	#' 
	setMethod('rposneg', 'ExpressionSet'
			, function(object, ...){
				Biobase::exprs(object) <- rposneg(Biobase::exprs(object), ...)
				object
			}
	)
	
	###% Annotate the genes specific to each cluster.
	###%
	###% This function uses the \code{annaffy} package to generate an HTML table from the probe identifiers.
#	setGeneric('annotate', function(x, annotation, ...) standardGeneric('annotate') )
#	setMethod('annotate', signature(x='factor', annotation='character'), 
#		function(x, annotation, filename='NMF genes', outdir='.', name='Cluster specific genes', ...)
#		{
#			library(annaffy)
#			anncols<-aaf.handler()[c(1:3, 6:13)]			
#			
#			# add html suffix to filename if necessary
#			if( length(grep("\\.html$", filename)) == 0 ) filename <- paste(filename, 'html', sep='.')
#			
#			# for each cluster annotate the genes set		
#			print(head(x))		
#			by(names(x), x, function(g){	
#						print(head(g))
#						if( length(g) == 0 ) return()
#						g <- as.character(g)
#						anntable <- aafTableAnn(g, annotation, anncols)
#						# generate HTML output
#						saveHTML(anntable, file.path(outdir,filename), title=paste(name, '[top', nrow(anntable),']'))
#					}, simplify=FALSE)
#			
#			# return nothing
#			invisible()
#		}
#	)
#	
#	setMethod('annotate', signature(x='NMF', annotation='character'), 
#		function(x, annotation, ...)
#		{
#			s <- extractFeatures(x)
#			class <- .predict.nmf(t(s))
#			annotate(class, annotation=annotation, ...)
#		}
#	)

	## Assign BioConductor aliases
	###% number of metagenes
	nmeta <- nbasis
	###% get/set methods of basis matrix
	metagenes <- basis
	`metagenes<-` <- `basis<-`
	###% get/set methods of mixture coefficients matrix
	metaprofiles <- coef
	`metaprofiles<-` <- `coef<-`
	
	###% Get/Set methods for rows/columns names of the basis and mixture matrices
	# using the Biobase definition standard generics
	setGeneric('featureNames', package='Biobase')
	setGeneric('featureNames<-', package='Biobase')	
	setMethod('featureNames', 'NMF',
		function(object){
			rownames(object)
		}
	)
	setReplaceMethod('featureNames', 'NMF',
		function(object, value){
			rownames(object) <- value
			object
		}
	)
	###% For NMFfitX objects: returns the featureNames of the best fit 
	###% There is no replace method for NMFfitX objects
	setMethod('featureNames', 'NMFfitX',
		function(object){
			rownames(fit(object))
		}
	)
	
	setGeneric('sampleNames', package='Biobase')
	setGeneric('sampleNames<-', package='Biobase')	
	setMethod('sampleNames', 'NMF',
		function(object){
			colnames(object)
		}
	)
	setReplaceMethod('sampleNames', 'NMF',
		function(object, value){
			colnames(object) <- value
			object
		}
	)
	###% For NMFfitX objects: returns the sampleNames of the best fit 
	###% There is no replace method for NMFfitX objects
	setMethod('sampleNames', 'NMFfitX',
		function(object){
			colnames(fit(object))
		}
	)

#	# Export layer-specific methods [only if one is loading a namespace]
#	# NB: Only for R < 3.0.0
#	if( pkgmaker::testRversion("2.15.3", -1L) ){
#		ns <- pkgmaker::addNamespaceExport(c("nmeta"
#							,"featureNames", "featureNames<-"
#							,"sampleNames", "sampleNames<-"
#							,"metagenes", "metagenes<-"
#							,"metaprofiles", "metaprofiles<-"))
#	}
	
	# return TRUE
	TRUE
}

}