@@ -76,3 +76,59 @@ runSingleR <- function(obj.seurat,
76
76
77
77
return (list (obj.seurat , singleR.out ))
78
78
}
79
+
80
+ # ' Convert cell ontology IDs to names in SingleR output
81
+ # '
82
+ # ' Convert bare cell ontology IDs used in SingleR to human-readable names
83
+ # '
84
+ # ' @param metadata data.frame with labels, pruned.labels and delta.next columns. Typically either:
85
+ # ' - Seurat object metadata (after running SingleR), or
86
+ # ' - SingleR output object
87
+ # ' @param cl Cell ontology database
88
+ # ' - Can be btained via: `cl <- ontoProc::getOnto('cellOnto')`
89
+ # ' @param old.prefix The prefix used for the original runSingleR call (e.g. "ImmGen.ont")
90
+ # ' @param new.prefix The new prefix to use for the human readable column (e.g. "ImmGen.ont.name")
91
+ # '
92
+ # ' @returns data.frame with three new columns: `{new.prefix}.labels`, `{new.prefix}.pruned.labels`, `{new.prefix}.delta.next`.
93
+ # '
94
+ # '@examples
95
+ # ' \dontrun{
96
+ # ' ## Run on bare ontology IDs, then convert to human readable cell type labels
97
+ # ' # Get cell ontology names and other info
98
+ # ' cl <- ontoProc::getOnto('cellOnto')
99
+ # '
100
+ # ' # ImmGen.ont
101
+ # ' c(obj.seurat, SingleR.ImmGen.ont) %<-% obj.seurat %>%
102
+ # ' Run.SingleR(obj.seurat=.,
103
+ # ' ref=ref.ImmGen,
104
+ # ' labels=ref.ImmGen$label.ont,
105
+ # ' de.method="classic",
106
+ # ' meta.prefix = "ImmGen.ont")
107
+ # '
108
+ # ' # These labels aren't very informative:
109
+ # ' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont, show.pruned = TRUE, fontsize=4)
110
+ # '
111
+ # ' # Convert bare cell ontology IDs to informative names in seurat object
112
+ # ' [email protected] <- convertClID2Name(metadata = [email protected] , cl = cl,
113
+ # ' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name")
114
+ # '
115
+ # ' # Convert bare cell ontology IDs to informative names in SingleR output and visualize
116
+ # ' SingleR.ImmGen.ont.names <- convertClID2Name(metadata = SingleR.ImmGen.ont, cl = cl,
117
+ # ' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name")
118
+ # '
119
+ # ' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont.names, show.pruned = TRUE, fontsize=4)
120
+ # ' }
121
+ # '
122
+ # ' @export
123
+ # Run SingleR
124
+ # Convert bare cell ontology IDs to informative names
125
+ convertClID2Name <- function (metadata , cl , old.prefix = " ImmGen.ont" , new.prefix = " ImmGen.ont.name" ) {
126
+ op <- old.prefix
127
+ np <- new.prefix
128
+ md <- metadata
129
+ md [[paste0(np , " .labels" )]] <- unname(cl $ name [unname(md [[paste0(op , " .labels" )]])])
130
+ md [[paste0(np , " .pruned.labels" )]] <- unname(cl $ name [unname(md [[paste0(op , " .pruned.labels" )]])])
131
+ md [[paste0(np , " .delta.next" )]] <- md [[paste0(op , " .delta.next" )]]
132
+ metadata <- md
133
+ return (metadata )
134
+ }
0 commit comments