39
39
# ' @param reduction.save Keyword to save Harmony reduction. Useful if you want
40
40
# ' to try Harmony with multiple parameters and save them as e.g.
41
41
# ' 'harmony_theta0', 'harmony_theta1', 'harmony_theta2'
42
- # ' @param assay.use (Seurat V3 only) Which assay to Harmonize with (Default Assay as default).
42
+ # ' @param assay.use (Seurat V3 only) Which assay to run PCA on if no PCA present?
43
43
# ' @param ... other parameters
44
44
# '
45
45
# '
@@ -81,32 +81,30 @@ RunHarmony.Seurat <- function(
81
81
project.dim = TRUE ,
82
82
...
83
83
) {
84
- assay.use <- assay.use % || % DefaultAssay( object )
85
- if ( reduction == " pca " ) {
86
- tryCatch(
87
- embedding <- Seurat :: Embeddings (object , reduction = " pca " ),
88
- error = function ( e ) {
89
- if (verbose ) {
90
- message(" Harmony needs PCA. Trying to run PCA now." )
91
- }
92
- tryCatch(
93
- object <- Seurat :: RunPCA(
94
- object ,
95
- assay = assay.use , verbose = verbose
96
- ) ,
97
- error = function ( e ) {
98
- stop( " Harmony needs PCA. Tried to run PCA and failed. " )
99
- }
100
- )
84
+ if ( ! requireNamespace( ' Seurat ' , quietly = TRUE )) {
85
+ stop( " Running Harmony on a Seurat object requires Seurat " )
86
+ }
87
+ assay.use <- assay.use % || % Seurat :: DefaultAssay (object )
88
+ if ( reduction == " pca " && ! reduction %in% Seurat :: Reductions( object = object ) ) {
89
+ if (isTRUE( x = verbose ) ) {
90
+ message(" Harmony needs PCA. Trying to run PCA now." )
91
+ }
92
+ object <- tryCatch(
93
+ expr = Seurat :: RunPCA(
94
+ object = object ,
95
+ assay = assay.use ,
96
+ verbose = verbose ,
97
+ reduction.name = reduction
98
+ ),
99
+ error = function ( ... ) {
100
+ stop( " Harmony needs PCA. Tried to run PCA and failed. " )
101
101
}
102
102
)
103
- } else {
104
- available.dimreduc <- names(methods :: slot(object = object , name = " reductions" ))
105
- if (! (reduction %in% available.dimreduc )) {
106
- stop(" Requested dimension reduction is not present in the Seurat object" )
107
- }
108
- embedding <- Seurat :: Embeddings(object , reduction = reduction )
109
103
}
104
+ if (! reduction %in% Seurat :: Reductions(object = object )) {
105
+ stop(" Requested dimension reduction is not present in the Seurat object" )
106
+ }
107
+ embedding <- Seurat :: Embeddings(object , reduction = reduction )
110
108
if (is.null(dims.use )) {
111
109
dims.use <- seq_len(ncol(embedding ))
112
110
}
@@ -118,7 +116,11 @@ RunHarmony.Seurat <- function(
118
116
if (length(dims.use ) == 1 ) {
119
117
stop(" only specified one dimension in dims.use" )
120
118
}
121
- metavars_df <- Seurat :: FetchData(object , group.by.vars )
119
+ metavars_df <- Seurat :: FetchData(
120
+ object ,
121
+ group.by.vars ,
122
+ cells = Seurat :: Cells(x = object [[reduction ]])
123
+ )
122
124
123
125
harmonyEmbed <- HarmonyMatrix(
124
126
embedding ,
@@ -142,19 +144,16 @@ RunHarmony.Seurat <- function(
142
144
reference_values
143
145
)
144
146
145
- rownames(harmonyEmbed ) <- row.names(embedding )
146
- colnames(harmonyEmbed ) <- paste0(reduction.save , " _" , seq_len(ncol(harmonyEmbed )))
147
+ reduction.key <- Seurat :: Key(reduction.save , quiet = TRUE )
148
+ rownames(harmonyEmbed ) <- rownames(embedding )
149
+ colnames(harmonyEmbed ) <- paste0(reduction.key , seq_len(ncol(harmonyEmbed )))
147
150
148
- suppressWarnings({
149
- harmonydata <- Seurat :: CreateDimReducObject(
150
- embeddings = harmonyEmbed ,
151
- stdev = as.numeric(apply(harmonyEmbed , 2 , stats :: sd )),
152
- assay = assay.use ,
153
- key = reduction.save
154
- )
155
- })
156
-
157
- object [[reduction.save ]] <- harmonydata
151
+ object [[reduction.save ]] <- Seurat :: CreateDimReducObject(
152
+ embeddings = harmonyEmbed ,
153
+ stdev = as.numeric(apply(harmonyEmbed , 2 , stats :: sd )),
154
+ assay = Seurat :: DefaultAssay(object = object [[reduction ]]),
155
+ key = reduction.key
156
+ )
158
157
if (project.dim ) {
159
158
object <- Seurat :: ProjectDim(
160
159
object ,
0 commit comments