@@ -85,12 +85,17 @@ scVIIntegration <- function(
8585 }
8686
8787 # build a meta.data-style data.frame indicating the batch for each cell
88- batches <- .FindBatches(object , layers = layers )
8988 # scVI expects a single counts matrix so we'll join our layers together
9089 # it also expects the raw counts matrix
9190 # TODO: avoid hardcoding this - users can rename their layers arbitrarily
9291 # so there's no gauruntee that the usual naming conventions will be followed
93- object <- JoinLayers(object = object , layers = " counts" )
92+ if (inherits(object , what = " SCTAssay" )) {
93+ batches <- .FindSCTBatches(object , layers = layers )
94+ } else {
95+ batches <- .FindBatches(object , layers = layers )
96+ object <- JoinLayers(object = object , layers = " counts" )
97+ }
98+
9499 # setup an `AnnData` python instance
95100 adata <- sc $ AnnData(
96101 X = scipy $ sparse $ csr_matrix(
@@ -140,8 +145,7 @@ attr(x = scVIIntegration, which = "Seurat.method") <- "integration"
140145
141146
142147# ' Builds a data.frame with batch identifiers to use when integrating
143- # ' \code{object}. For \code{SCTAssay}s, batches are split using their
144- # ' model identifiers. For \code{StdAssays}, batches are split by layer.
148+ # ' \code{object}. For \code{StdAssays}, batches are split by layer.
145149# '
146150# ' Internal - essentially the same as \code{Seurat:::CreateIntegrationGroups}
147151# ' except that it does not take in a `scale.layer` param.
@@ -154,32 +158,50 @@ attr(x = scVIIntegration, which = "Seurat.method") <- "integration"
154158# ' @return A dataframe indexed on the cell identifiers from \code{object} -
155159# ' the dataframe contains a single column, "batch", indicating the ...
156160.FindBatches <- function (object , layers ) {
157- # if an `SCTAssay` is passed in it's expected that the transformation
158- # was run on each batch individually and then merged so we can use
159- # the model identifier to split our batches
160- if (inherits(object , what = " SCTAssay" )) {
161- # build an empty data.frame indexed
162- # on the cell identifiers from `object`
163- batch.df <- SeuratObject :: EmptyDF(n = ncol(object ))
164- row.names(batch.df ) <- Cells(object )
165- # for each
166- for (sct.model in levels(object )) {
167- cell.identifiers <- Cells(object , layer = sct.model )
168- batch.df [cell.identifiers , " batch" ] <- sct.model
169- }
170- # otherwise batches can be split using `object`'s layers
171- } else {
172161 # build a LogMap indicating which layer each cell is from
173162 layer.masks <- slot(object , name = " cells" )[, layers ]
174163 # get a named vector mapping each cell to its respective layer
175164 layer.map <- labels(
176- layer.masks ,
177- values = Cells(object , layer = layers )
165+ layer.masks ,
166+ values = Cells(object , layer = layers )
178167 )
179168 # wrap the vector up in a data.frame
180169 batch.df <- as.data.frame(layer.map )
181170 names(batch.df ) <- " batch"
182- }
171+
172+ return (batch.df )
173+ }
174+
183175
184- return (batch.df )
176+ # ' Builds a data.frame with batch identifiers to use when integrating
177+ # ' \code{object}. For \code{SCTAssay}s, batches are split using their
178+ # ' model identifiers.
179+ # '
180+ # ' Internal - essentially the same as \code{Seurat:::CreateIntegrationGroups}
181+ # ' except that it does not take in a `scale.layer` param.
182+ # '
183+ # ' @noRd
184+ # '
185+ # ' @param object A \code{SCTAssay} or \code{StdAssays} instance.
186+ # ' @param layers Layers in \code{object} to integrate.
187+ # '
188+ # ' @return A dataframe indexed on the cell identifiers from \code{object} -
189+ # ' the dataframe contains a single column, "batch", indicating the ...
190+ .FindSCTBatches <- function (object , layers ) {
191+ # build an empty data.frame indexed
192+ # on the cell identifiers from `object`
193+ batch.df <- SeuratObject :: EmptyDF(n = ncol(object ))
194+ row.names(batch.df ) <- Cells(object )
195+ # for each
196+ for (sct.model in levels(object )) {
197+ cell.identifiers <- Cells(object , layer = sct.model )
198+ batch.df [cell.identifiers , " batch" ] <- sct.model
199+ }
200+ return (batch.df )
185201}
202+
203+
204+
205+
206+
207+
0 commit comments