From 2f3997c56e099691d089dd3628f03410f1a262cb Mon Sep 17 00:00:00 2001 From: Yichen Wang Date: Fri, 15 Mar 2024 14:55:38 -0400 Subject: [PATCH] dimReds updates --- NAMESPACE | 4 ++++ R/classConversion.R | 21 +++------------------ R/generics.R | 14 ++++++++++++++ R/liger-methods.R | 20 ++++++++++++++++++++ R/util.R | 2 +- man/liger-class.Rd | 12 ++++++++++++ 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index bcf528b0..1c3a6532 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -54,6 +54,7 @@ export("datasets<-") export("defaultCluster<-") export("defaultDimRed<-") export("dimRed<-") +export("dimReds<-") export("featureMeta<-") export("h5fileInfo<-") export("normData<-") @@ -84,6 +85,7 @@ export(datasets) export(defaultCluster) export(defaultDimRed) export(dimRed) +export(dimReds) export(downsample) export(exportInteractTrack) export(featureMeta) @@ -210,6 +212,7 @@ exportMethods("datasets<-") exportMethods("defaultCluster<-") exportMethods("defaultDimRed<-") exportMethods("dimRed<-") +exportMethods("dimReds<-") exportMethods("dimnames<-") exportMethods("featureMeta<-") exportMethods("h5fileInfo<-") @@ -230,6 +233,7 @@ exportMethods(defaultCluster) exportMethods(defaultDimRed) exportMethods(dim) exportMethods(dimRed) +exportMethods(dimReds) exportMethods(dimnames) exportMethods(featureMeta) exportMethods(getH5File) diff --git a/R/classConversion.R b/R/classConversion.R index 9eeaed76..dadc84ab 100644 --- a/R/classConversion.R +++ b/R/classConversion.R @@ -137,7 +137,7 @@ as.liger.Seurat <- function( for (rd in SeuratObject::Reductions(object)) { mat <- object[[rd]][[]] colnames(mat) <- seq_len(ncol(mat)) - cellMeta(lig, rd) <- mat + dimRed(lig, rd) <- mat } return(lig) } @@ -341,29 +341,14 @@ ligerToSeurat <- function( } # Split normal data.frame compatible info and dimReds - metadata <- data.frame(row.names = colnames(object)) - dimReds <- list() - for (i in seq_along(cellMeta(object))) { - varname <- names(cellMeta(object))[i] - var <- cellMeta(object)[[i]] - if (is.null(dim(var))) metadata[[varname]] <- var - else dimReds[[varname]] <- var - } + metadata <- .DataFrame.as.data.frame(cellMeta(object)) + dimReds <- dimReds(object) srt <- Seurat::CreateSeuratObject(counts = Assay, assay = assay, meta.data = metadata) srt$orig.ident <- orig.ident Seurat::Idents(srt) <- idents - # if (!is.null(data)) { - # srt <- .setSeuratData(srt, layer = "ligerNormData", slot = "data", - # value = data, assay = assay, denseIfNeeded = FALSE) - # } - # if (!is.null(scale.data)) { - # srt <- .setSeuratData(srt, layer = "ligerScaleData", slot = "scale.data", - # value = scale.data, assay = assay, - # denseIfNeeded = TRUE) - # } # Attempt to get H.norm primarily. If it is NULL, then turn to H h <- getMatrix(object, "H.norm") %||% getMatrix(object, "H", returnList = TRUE) diff --git a/R/generics.R b/R/generics.R index 86922609..9d34e3ed 100644 --- a/R/generics.R +++ b/R/generics.R @@ -276,6 +276,20 @@ setGeneric( #' default cluster labeling, we also constructed the feature of default dimRed. #' It can be set with \code{defaultDimRed(obj) <- "existingMatLikeVar"} and the #' matrix can be retrieved with \code{defaultDimRed(obj)}. +setGeneric( + "dimReds", + function(x) standardGeneric("dimReds") +) + +#' @export +#' @rdname liger-class +setGeneric( + "dimReds<-", + function(x, value) standardGeneric("dimReds<-") +) + +#' @export +#' @rdname liger-class setGeneric( "dimRed", function(x, name = NULL, useDatasets = NULL, cellIdx = NULL, ...) { diff --git a/R/liger-methods.R b/R/liger-methods.R index 82dbfabb..ece44f81 100644 --- a/R/liger-methods.R +++ b/R/liger-methods.R @@ -1125,6 +1125,26 @@ setReplaceMethod( } ) +#' @export +#' @rdname liger-class +setMethod( + "dimReds", + signature = c(x = "liger"), + function(x) x@dimReds +) + +#' @export +#' @rdname liger-class +setReplaceMethod( + "dimReds", + signature(x = "liger", value = "list"), + function(x, value) { + x@dimReds <- value + validObject(x) + return(x) + } +) + #' @export #' @rdname liger-class setMethod( diff --git a/R/util.R b/R/util.R index 618200d9..1c52aa5e 100644 --- a/R/util.R +++ b/R/util.R @@ -685,7 +685,7 @@ searchH <- function(object, useRaw = NULL) { # use when you have objects created when this package was named by `rliger2` # now we rename the package back to `rliger` and you'll need to have the objects # compatible with "new" package name. -rliger2_to_rliger_namespace <- function(obj, dimredName = NULL) { +rliger2_to_rliger_namespace <- function(obj, dimredName) { cm <- obj@cellMeta drList <- list() for (i in dimredName) { diff --git a/man/liger-class.Rd b/man/liger-class.Rd index 4aa930b5..ba7eff03 100644 --- a/man/liger-class.Rd +++ b/man/liger-class.Rd @@ -13,6 +13,8 @@ \alias{cellMeta<-} \alias{defaultCluster} \alias{defaultCluster<-} +\alias{dimReds} +\alias{dimReds<-} \alias{dimRed} \alias{dimRed<-} \alias{defaultDimRed} @@ -68,6 +70,8 @@ \alias{defaultCluster<-,liger,ANY,ANY,character-method} \alias{defaultCluster<-,liger,ANY,ANY,factor-method} \alias{defaultCluster<-,liger,ANY,ANY,NULL-method} +\alias{dimReds,liger-method} +\alias{dimReds<-,liger,list-method} \alias{dimRed,liger,missing_OR_NULL-method} \alias{dimRed,liger,index-method} \alias{dimRed<-,liger,index,ANY,ANY,NULL-method} @@ -115,6 +119,10 @@ defaultCluster(x, useDatasets = NULL, ...) defaultCluster(x, name = NULL, useDatasets = NULL, ...) <- value +dimReds(x) + +dimReds(x) <- value + dimRed(x, name = NULL, useDatasets = NULL, cellIdx = NULL, ...) dimRed(x, name = NULL, useDatasets = NULL, cellIdx = NULL, ...) <- value @@ -259,6 +267,10 @@ commands(x, funcName = NULL, arg = NULL) \S4method{defaultCluster}{liger,ANY,ANY,NULL}(x, name = NULL, useDatasets = NULL, ...) <- value +\S4method{dimReds}{liger}(x) + +\S4method{dimReds}{liger,list}(x) <- value + \S4method{dimRed}{liger,missing_OR_NULL}(x, name = NULL, useDatasets = NULL, cellIdx = NULL, ...) \S4method{dimRed}{liger,index}(x, name = NULL, useDatasets = NULL, cellIdx = NULL, ...)