/
importGeneSets.R
606 lines (564 loc) · 27.9 KB
/
importGeneSets.R
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#' @title Imports gene sets from a GMT file
#' @description Converts a list of gene sets stored in a GMT file into a
#' \linkS4class{GeneSetCollection} and stores it in the metadata of the
#' \linkS4class{SingleCellExperiment} object. These gene sets can be used in
#' downstream quality control and analysis functions in \link{singleCellTK}.
#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
#' @param file Character. Path to GMT file. See \link{getGmt} for
#' more information on reading GMT files.
#' @param collectionName Character. Name of collection to add gene sets to.
#' If this collection already exists in \code{inSCE}, then these gene sets will
#' be added to that collection. Any gene sets within the collection with the
#' same name will be overwritten. Default \code{GeneSetCollection}.
#' @param by Character, character vector, or NULL. Describes the
#' location within \code{inSCE} where the gene identifiers in
#' \code{geneSetList} should be mapped. If set to \code{"rownames"} then the
#' features will be searched for among \code{rownames(inSCE)}. This can also be
#' set to one of the column names of \code{rowData(inSCE)} in which case the
#' gene identifies will be mapped to that column in the \code{rowData}
#' of \code{inSCE}. \code{by} can be a vector the same length as
#' the number of gene sets in the GMT file and the elements of the vector
#' can point to different locations within \code{inSCE}. Finally, \code{by}
#' can be \code{NULL}. In this case, the location of the gene identifiers
#' in \code{inSCE} should be saved in the description (2nd column)
#' of the GMT file. See \link{featureIndex} for more information.
#' Default \code{"rownames"}.
#' @param sep Character. Delimiter of the GMT file. Default \code{"\t"}.
#' @param noMatchError Boolean. Show an error if a collection does not have
#' any matching features. Default \code{TRUE}.
#' @details The gene identifiers in gene sets in the GMT file will be
#' mapped to the rownames of \code{inSCE} using the \code{by} parameter and
#' stored in a \linkS4class{GeneSetCollection} object from package
#' \link{GSEABase}. This object is stored in
#' \code{metadata(inSCE)$sctk$genesets}, which can be accessed in downstream
#' analysis functions such as \link[singleCellTK]{runCellQC}.
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromList} for importing from lists,
#' \link{importGeneSetsFromCollection} for importing from
#' \linkS4class{GeneSetCollection} objects, and
#' \link{importGeneSetsFromMSigDB} for importing MSigDB gene sets.
#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object
#' with gene set from \code{collectionName} output stored to the
#' \link{metadata} slot.
#' @examples
#' data(scExample)
#'
#' # GMT file containing gene symbols for a subset of human mitochondrial genes
#' gmt <- system.file("extdata/mito_subset.gmt", package = "singleCellTK")
#'
#' # "feature_name" is the second column in the GMT file, so the ids will
#' # be mapped using this column in the 'rowData' of 'sce'. This
#' # could also be accomplished by setting by = "feature_name" in the
#' # function call.
#' sce <- importGeneSetsFromGMT(inSCE = sce, file = gmt, by = NULL)
#' @export
importGeneSetsFromGMT <- function(inSCE, file,
collectionName = "GeneSetCollection",
by = "rownames",
sep = "\t",
noMatchError = TRUE) {
# Read gmt file into GeneSetCollection
gsc <- GSEABase::getGmt(con = file, sep = sep)
inSCE <- importGeneSetsFromCollection(inSCE = inSCE,
geneSetCollection = gsc,
collectionName = collectionName,
by = by,
noMatchError = noMatchError)
return(inSCE)
}
#' @title Imports gene sets from a list
#' @description Converts a list of gene sets into a
#' \linkS4class{GeneSetCollection} and stores it in the metadata of the
#' \linkS4class{SingleCellExperiment} object. These gene sets can be used in
#' downstream quality control and analysis functions in \link{singleCellTK}.
#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
#' @param geneSetList Named List. A list containing one or more gene sets.
#' Each element of the list should be a character vector of gene identifiers.
#' The names of the list will be become the gene set names in the
#' \linkS4class{GeneSetCollection} object.
#' @param collectionName Character. Name of collection to add gene sets to.
#' If this collection already exists in \code{inSCE}, then these gene sets will
#' be added to that collection. Any gene sets within the collection with the
#' same name will be overwritten. Default \code{GeneSetCollection}.
#' @param by Character or character vector. Describes the
#' location within \code{inSCE} where the gene identifiers in
#' \code{geneSetList} should be mapped. If set to \code{"rownames"} then the
#' features will be searched for among \code{rownames(inSCE)}. This can also be
#' set to one of the column names of \code{rowData(inSCE)} in which case the
#' gene identifies will be mapped to that column in the \code{rowData}
#' of \code{inSCE}. Finally, \code{by} can be a vector the same length as
#' the number of gene sets in \code{geneSetList} and the elements of the vector
#' can point to different locations within \code{inSCE}. See
#' \link{featureIndex} for more information.
#' Default \code{"rownames"}.
#' @param noMatchError Boolean. Show an error if a collection does not have
#' any matching features. Default \code{TRUE}.
#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object
#' with gene set from \code{collectionName} output stored to the
#' \link{metadata} slot.
#' @details The gene identifiers in gene sets in \code{geneSetList} will be
#' mapped to the rownames of \code{inSCE} using the \code{by} parameter and
#' stored in a \linkS4class{GeneSetCollection} object from package
#' \link{GSEABase}. This object is stored in
#' \code{metadata(inSCE)$sctk$genesets}, which can be accessed in downstream
#' analysis functions such as \link[singleCellTK]{runCellQC}.
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromCollection} for importing from
#' \linkS4class{GeneSetCollection} objects,
#' \link{importGeneSetsFromGMT} for importing from GMT files, and
#' \link{importGeneSetsFromMSigDB} for importing MSigDB gene sets.
#' @examples
#' data(scExample)
#'
#' # Generate gene sets from 'rownames'
#' gs1 <- rownames(sce)[seq(10)]
#' gs2 <- rownames(sce)[seq(11,20)]
#' gs <- list("geneset1" = gs1, "geneset2" = gs2)
#' sce <- importGeneSetsFromList(inSCE = sce,
#' geneSetList = gs,
#' by = "rownames")
#'
#' # Generate a gene set for mitochondrial genes using
#' # Gene Symbols stored in 'rowData'
#' mito.ix <- grep("^MT-", rowData(sce)$feature_name)
#' mito <- list(mito = rowData(sce)$feature_name[mito.ix])
#' sce <- importGeneSetsFromList(inSCE = sce,
#' geneSetList = mito,
#' by = "feature_name")
#' @export
#' @importFrom SummarizedExperiment rowData
importGeneSetsFromList <- function(inSCE, geneSetList,
collectionName = "GeneSetCollection",
by = "rownames",
noMatchError = TRUE) {
# Check to ensure 'by' is correct
if(!all(by %in% c("rownames", colnames(rowData(inSCE))))) {
stop("The entries in 'by' must be 'rownames' or one of the column names ",
"in the 'rowData(inSCE)': ", paste(colnames(rowData(inSCE)),
collapse = ", "))
}
if(length(by) != 1 & length(by) != length(geneSetList)) {
stop("'by' needs to be a character of length 1 describing the location ",
"of all the gene sets in inSCE or a character vector the same ",
"length as 'geneSetList' where each entry describes the location of ",
"that particular gene set in 'geneSetList'.")
}
if(is.null(names(geneSetList))) {
stop("'geneSetList' needs to be a named list.")
}
if(any(is.na(names(geneSetList)))) {
stop("No names in 'geneSetList' can be 'NA'.")
}
# Convert to GeneSetCollection
gs <- list()
for(i in seq_along(geneSetList)) {
gs[[i]] <- GSEABase::GeneSet(setName = names(geneSetList)[i],
geneIds = geneSetList[[i]])
}
gsc <- GSEABase::GeneSetCollection(gs)
inSCE <- importGeneSetsFromCollection(inSCE = inSCE,
geneSetCollection = gsc,
collectionName = collectionName,
by = by,
noMatchError = noMatchError)
return(inSCE)
}
#' @title Imports gene sets from a GeneSetCollection object
#' @description Converts a list of gene sets stored in a
#' \linkS4class{GeneSetCollection} object and stores it in the metadata of the
#' \linkS4class{SingleCellExperiment} object. These gene sets can be used in
#' downstream quality control and analysis functions in \link{singleCellTK}.
#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
#' @param geneSetCollection A \linkS4class{GeneSetCollection} object. See
#' \link{GeneSetCollection} for more details.
#' @param collectionName Character. Name of collection to add gene sets to.
#' If this collection already exists in \code{inSCE}, then these gene sets will
#' be added to that collection. Any gene sets within the collection with the
#' same name will be overwritten. Default \code{GeneSetCollection}.
#' @param by Character, character vector, or NULL. Describes the
#' location within \code{inSCE} where the gene identifiers in
#' \code{geneSetCollection} should be mapped. If set to \code{"rownames"} then the
#' features will be searched for among \code{rownames(inSCE)}. This can also be
#' set to one of the column names of \code{rowData(inSCE)} in which case the
#' gene identifies will be mapped to that column in the \code{rowData}
#' of \code{inSCE}. \code{by} can be a vector the same length as
#' the number of gene sets in the \code{GeneSetCollection} and the elements of the vector
#' can point to different locations within \code{inSCE}. Finally, \code{by}
#' can be \code{NULL}. In this case, the location of the gene identifiers
#' in \code{inSCE} should be saved in the description slot for each gene set
#' in the \code{GeneSetCollection}.
#' See \link{featureIndex} for more information.
#' Default \code{"rownames"}.
#' @param noMatchError Boolean. Show an error if a collection does not have
#' any matching features. Default \code{TRUE}.
#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object
#' with gene set from \code{collectionName} output stored to the
#' \link{metadata} slot.
#' @details The gene identifiers in gene sets in the
#' \code{GeneSetCollection} will be mapped to the rownames of
#' \code{inSCE} using the \code{by} parameter and
#' stored in a \linkS4class{GeneSetCollection} object from package
#' \link{GSEABase}. This object is stored in
#' \code{metadata(inSCE)$sctk$genesets}, which can be accessed in downstream
#' analysis functions such as \link[singleCellTK]{runCellQC}.
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromList} for importing from lists,
#' \link{importGeneSetsFromGMT} for importing from GMT files, and
#' \link{importGeneSetsFromMSigDB} for importing MSigDB gene sets.
#' @examples
#' data(scExample)
#' gs1 <- GSEABase::GeneSet(setName = "geneset1",
#' geneIds = rownames(sce)[seq(10)])
#' gs2 <- GSEABase::GeneSet(setName = "geneset2",
#' geneIds = rownames(sce)[seq(11,20)])
#' gsc <- GSEABase::GeneSetCollection(list(gs1, gs2))
#' sce <- importGeneSetsFromCollection(inSCE = sce,
#' geneSetCollection = gsc,
#' by = "rownames")
#' @export
importGeneSetsFromCollection <- function(inSCE, geneSetCollection,
collectionName = "GeneSetCollection",
by = "rownames",
noMatchError = TRUE) {
# If 'by' is NULL, then the location will be derived from the description
if(is.null(by)) {
location <- unlist(lapply(seq_along(geneSetCollection),
function(i) GSEABase::description(geneSetCollection[[i]])))
} else {
if(length(by) != 1 & length(by) != length(geneSetCollection)) {
stop("If not NULL, then 'by' needs to be a character of ",
"length 1 describing the location ",
"of all the gene sets in inSCE or a character vector the same ",
"length as 'geneSetList' where each entry describes the location of ",
"that particular gene set in 'geneSetList'.")
}
if(length(by) == 1) {
location <- rep(by, length(geneSetCollection))
} else {
location <- by
}
}
# Check to ensure 'by' matches annotation in SCE
if(!all(location %in% c("rownames", colnames(rowData(inSCE))))) {
stop("The entries in 'by' must be 'rownames' or one of the column names ",
"in the 'rowData(inSCE)': ", paste(colnames(rowData(inSCE)),
collapse = ", "))
}
gs <- list()
for(i in seq_along(geneSetCollection)) {
temp.gs <- featureIndex(
features = GSEABase::geneIds(geneSetCollection[[i]]),
inSCE = inSCE,
by = location[i],
exactMatch = TRUE,
removeNA = TRUE,
errorOnNoMatch = FALSE,
warningOnPartialMatch = FALSE)
if(length(temp.gs) > 0) {
gs.new <- geneSetCollection[[i]]
GSEABase::geneIds(gs.new) <- rownames(inSCE)[temp.gs]
gs <- c(gs, gs.new)
}
}
if(length(gs) == 0 & isTRUE(noMatchError)) {
stop("No gene sets could be imported. This is either because the no
overlapping genes were identified between the gene sets and the input
data or different types of identifiers are used between the two.
Please check if the gene identifiers of both gene sets and input
data belong to same type.")
} else if(length(gs) > 0) {
# Add GeneSetCollection back to metadata
new.gsc <- GSEABase::GeneSetCollection(gs)
old.gsc <- NULL
if(!is.null(S4Vectors::metadata(inSCE)$sctk$genesets)) {
if(collectionName %in% names(S4Vectors::metadata(inSCE)$sctk$genesets)) {
old.gsc <- S4Vectors::metadata(inSCE)$sctk$genesets[[collectionName]]
}
}
if(!is.null(old.gsc)) {
# Remove any old gene sets with same name as new gene sets
old.gsc <- old.gsc[!(names(old.gsc) %in% names(new.gsc))]
# Combine old and new GeneSetCollections and save back to metadata
S4Vectors::metadata(inSCE)$sctk$genesets[[collectionName]] <-
GSEABase::GeneSetCollection(c(old.gsc, new.gsc))
} else {
S4Vectors::metadata(inSCE)$sctk$genesets[[collectionName]] <- new.gsc
}
}
return(inSCE)
}
#' @title Imports gene sets from MSigDB
#' @description Gets a list of MSigDB gene sets stores it in the metadata of the
#' \linkS4class{SingleCellExperiment} object. These gene sets can be used in
#' downstream quality control and analysis functions in \link{singleCellTK}.
#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
#' @param categoryIDs Character vector containing the MSigDB gene set ids.
#' The column \code{ID} in the table returned by \code{getMSigDBTable()} shows
#' the list of possible gene set IDs that can be obtained.
#' @param species Character. Species available can be found using the function
#' \code{\link[msigdbr]{msigdbr_show_species}}. Default \code{"Homo sapiens"}.
#' @param mapping Character. One of "gene_symbol", "human_gene_symbol", or
#' "entrez_gene". Gene identifiers to be used for MSigDB gene sets. IDs
#' denoted by the \code{by} parameter must be either in gene symbol or
#' Entrez gene id format to match IDs from MSigDB.
#' @param by Character. Describes the
#' location within \code{inSCE} where the gene identifiers in
#' the MSigDB gene sets should be mapped. If set to \code{"rownames"} then the
#' features will be searched for among \code{rownames(inSCE)}. This can also be
#' set to one of the column names of \code{rowData(inSCE)} in which case the
#' gene identifies will be mapped to that column in the \code{rowData}
#' of \code{inSCE}. See \link{featureIndex} for more information.
#' Default \code{"rownames"}.
#' @param verbose Boolean. Whether to display progress. Default \code{TRUE}.
#' @param noMatchError Boolean. Show an error if a collection does not have
#' any matching features. Default \code{TRUE}.
#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object
#' with gene set from \code{collectionName} output stored to the
#' \link{metadata} slot.
#' @details The gene identifiers in gene sets from MSigDB will be retrieved
#' using the \code{\link{msigdbr}} package. They will be mapped to the IDs in
#' \code{inSCE} using the \code{by} parameter and
#' stored in a \linkS4class{GeneSetCollection} object from package
#' \link{GSEABase}. This object is stored in
#' \code{metadata(inSCE)$sctk$genesets}, which can be accessed in downstream
#' analysis functions such as \link[singleCellTK]{runCellQC}.
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromList} for importing from lists,
#' \link{importGeneSetsFromGMT} for importing from GMT files, and
# \link{importGeneSetsFromCollection} for importing from
#' \linkS4class{GeneSetCollection} objects.
#' @examples
#' data(scExample)
#' sce <- importGeneSetsFromMSigDB(inSCE = sce,
#' categoryIDs = "H",
#' species = "Homo sapiens",
#' mapping = "gene_symbol",
#' by = "feature_name")
#' @export
#' @importFrom SummarizedExperiment rowData
importGeneSetsFromMSigDB <- function(inSCE, categoryIDs,
species = "Homo sapiens",
mapping = c("gene_symbol",
"human_gene_symbol",
"entrez_gene"),
by = "rownames",
verbose = TRUE,
noMatchError = TRUE) {
mapping <- match.arg(mapping)
# Check species
all.species <- msigdbr::msigdbr_species()$species_name
if(!(species %in% all.species)) {
stop("'species' needs to be one of the following: ",
paste(all.species, collapse = ", "))
}
# Check to ensure 'by' matches annotation in SCE
if(is.null(by) || length(by) > 1) {
stop("'by' must a character of length 1.")
}
if(!(by %in% c("rownames", colnames(rowData(inSCE))))) {
stop("'by' must be 'rownames' or one of the column names ",
"in the 'rowData(inSCE)': ", paste(colnames(rowData(inSCE)),
collapse = ", "))
}
msigdb_table <- getMSigDBTable()
if(!all(categoryIDs %in% msigdb_table$ID)) {
diff <- setdiff(categoryIDs, msigdb_table$ID)
stop("Category IDs not found in MSigDB: ", paste(diff, collapse = ", "),
". Valid IDs can be found in the table returned by the function ",
"getMSigDBTable().")
}
rownames(msigdb_table) <- msigdb_table$ID
gs.list <- list()
for(i in seq_along(categoryIDs)) {
category <- msigdb_table[categoryIDs[i],"Category"]
subcat <- msigdb_table[categoryIDs[i],"Subcategory"]
if(subcat == "N/A") {
subcat <- NA
}
# Retrieve lists from msigdbr
gs <- as.data.frame(msigdbr::msigdbr(species = species,
category = category,
subcategory = subcat))
# Parse data.frame into list of GeneSets
gs.names <- unique(gs$gs_name)
num.gs.names <- length(gs.names)
if(isTRUE(verbose)){
p <- paste0(date(), " .. Importing '", categoryIDs[i],
"' gene sets (n = ", num.gs.names, ")")
message(p)
}
for(j in seq_along(gs.names)) {
gs.sub <- gs[gs$gs_name == gs.names[j],mapping]
gs.list[[j]] <- GSEABase::GeneSet(setName = gs.names[j],
geneIds = unique(gs.sub),
collectionType = GSEABase::BroadCollection(
category = tolower(category),
subCategory = subcat))
if(j %% 1000 == 0) {
if(isTRUE(verbose)) {
p <- paste0(date(), " .... Completed ", j, " out of ",
num.gs.names, " gene sets")
message(p)
}
}
}
if(isTRUE(verbose)) {
p <- paste0(date(), " .... Completed ", num.gs.names, " gene sets ",
"for ", i)
message(p)
}
# Add to SCE
if(isTRUE(verbose)) {
p <- paste0(date(), " .. Matching gene sets to '", by, "'")
message(p)
}
gsc <- GSEABase::GeneSetCollection(gs.list)
inSCE <- importGeneSetsFromCollection(inSCE = inSCE,
geneSetCollection = gsc,
collectionName = categoryIDs[i],
by = by,
noMatchError = noMatchError)
}
return(inSCE)
}
#' @title Import mitochondrial gene sets
#' @description Imports mitochondrial gene sets and stores it in the metadata of the
#' \linkS4class{SingleCellExperiment} object. These gene sets can be used in
#' downstream quality control and analysis functions in \link{singleCellTK}.
#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
#' @param reference Character. Species available are "human" and "mouse".
#' @param by Character. Describes the location within \code{inSCE} where the gene
#' identifiers in the mitochondrial gene sets should be mapped.
#' If set to \code{"rownames"} then the features will
#' be searched for among \code{rownames(inSCE)}. This can also be
#' set to one of the column names of \code{rowData(inSCE)} in which case the
#' gene identifies will be mapped to that column in the \code{rowData}
#' of \code{inSCE}. See \link{featureIndex} for more information.
#' Default \code{"rownames"}.
#' @param id Types of gene id. Now it supports "symbol", "entrez", "ensembl"
#' and "ensemblTranscriptID".
#' @param collectionName Character. Name of collection to add gene sets to.
#' If this collection already exists in \code{inSCE}, then these gene sets will
#' be added to that collection. Any gene sets within the collection with the
#' same name will be overwritten. Default \code{"mito"}.
#' @param noMatchError Boolean. Show an error if a collection does not have
#' any matching features. Default \code{TRUE}.
#' @return A \link[SingleCellExperiment]{SingleCellExperiment} object
#' with gene set from \code{collectionName} output stored to the
#' \link{metadata} slot.
#' @details The gene identifiers of mitochondrial genes will be loaded with
#' "data(AllMito)". Currently, it supports human and mouse references.
#' Also, it supports entrez ID, gene symbol, ensemble ID and ensemble transcript ID.
#' They will be mapped to the IDs in \code{inSCE} using the \code{by} parameter and
#' stored in a \linkS4class{GeneSetCollection} object from package
#' \link{GSEABase}. This object is stored in
#' \code{metadata(inSCE)$sctk$genesets}, which can be accessed in downstream
#' analysis functions such as \link[singleCellTK]{runCellQC}.
#' @author Rui Hong
#' @seealso \link{importGeneSetsFromList} for importing from lists,
#' \link{importGeneSetsFromGMT} for importing from GMT files, and
# \link{importGeneSetsFromCollection} for importing from
#' \linkS4class{GeneSetCollection} objects.
#' @examples
#' data(scExample)
#' sce <- importMitoGeneSet(inSCE = sce,
#' reference = "human",
#' id = "ensembl",
#' collectionName = "human_mito",
#' by = "rownames")
#' @export
importMitoGeneSet <- function(inSCE, reference = "human", id = "ensembl",
by = "rownames", collectionName = "mito",
noMatchError = TRUE) {
if (!id %in% c("symbol", "entrez", "ensembl", "ensemblTranscriptID")) {
stop("`id` should be one of 'symbol', 'entrez', 'ensembl' or 'ensemblTranscriptID'")
}
reference <- tolower(reference)
if (!reference %in% c("human", "mouse")) {
stop("Now it only supports 'human' and 'mouse' reference.")
}
MitoGenes <- NULL
utils::data(MitoGenes, envir = environment())
target <- paste(reference, id, sep="_")
if (!target %in% names(MitoGenes)) {
stop(target, " is not supported now. Please use function 'importGeneSetsFromGMT' or 'importGeneSetsFromList' to load mitochondrial gene sets.")
}
mitogenes <- MitoGenes[[target]]
names(mitogenes) <- collectionName
inSCE <- importGeneSetsFromList(inSCE = inSCE, geneSetList = mitogenes,
collectionName = collectionName,
by = by,
noMatchError = noMatchError)
return(inSCE)
}
#' @title Lists imported GeneSetCollections
#' @description Returns a vector of GeneSetCollections that have been
#' imported and stored in \code{metadata(inSCE)$sctk$genesets}.
#' @param inSCE A \link[SingleCellExperiment]{SingleCellExperiment} object.
#' @return Character vector.
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromList} for importing from lists,
#' \link{importGeneSetsFromGMT} for importing from GMT files,
# \link{importGeneSetsFromCollection} for importing from
#' \linkS4class{GeneSetCollection} objects, and \link{importGeneSetsFromMSigDB}
#' for importing MSigDB gene sets.
#' @examples
#' data(scExample)
#' gs1 <- GSEABase::GeneSet(setName = "geneset1",
#' geneIds = rownames(sce)[seq(10)])
#' gs2 <- GSEABase::GeneSet(setName = "geneset2",
#' geneIds = rownames(sce)[seq(11,20)])
#' gsc1 <- GSEABase::GeneSetCollection(gs1)
#' gsc2 <- GSEABase::GeneSetCollection(gs2)
#' sce <- importGeneSetsFromCollection(inSCE = sce,
#' geneSetCollection = gsc1,
#' by = "rownames",
#' collectionName = "Collection1")
#' sce <- importGeneSetsFromCollection(inSCE = sce,
#' geneSetCollection = gsc2,
#' by = "rownames",
#' collectionName = "Collection2")
#' collections <- sctkListGeneSetCollections(sce)
#' @export
sctkListGeneSetCollections <- function(inSCE) {
if(!is.null(S4Vectors::metadata(inSCE)$sctk$genesets)) {
res <- names(S4Vectors::metadata(inSCE)$sctk$genesets)
} else {
res <- character()
}
return(res)
}
#' @title Shows MSigDB categories
#' @description Returns a data.frame that shows MSigDB categories and
#' subcategories as well as descriptions for each. The entries in the ID
#' column in this table can be used as input for \link{importGeneSetsFromMSigDB}.
#' @return data.frame, containing MSigDB categories
#' @author Joshua D. Campbell
#' @seealso \link{importGeneSetsFromMSigDB} for importing MSigDB gene sets.
#' @export
#' @examples
#' getMSigDBTable()
getMSigDBTable <- function() {
# Issues for getting this to pass R CMD Check:
# https://support.bioconductor.org/p/24756/#24768
# https://groups.google.com/forum/#!topic/cambridge-r-user-group/c7vf8o3QwDo
msigdb_table <- NULL
rm(msigdb_table)
utils::data(msigdb_table)
return(msigdb_table)
}
.retrieveGeneSetCollection <- function(inSCE, collectionName) {
if(!is.null(S4Vectors::metadata(inSCE)$sctk$genesets)) {
if(collectionName %in% names(S4Vectors::metadata(inSCE)$sctk$genesets)) {
gsc <- S4Vectors::metadata(inSCE)$sctk$genesets[[collectionName]]
} else {
stop("No GeneSetCollection called '", collectionName, "' was found. ",
"The GeneSetColletions that are available include: ",
paste(names(S4Vectors::metadata(inSCE)$sctk$genesets),
collapse = ", "))
}
} else {
stop("No GeneSetCollections have been imported.")
}
return(gsc)
}