R/set_obs.R

Defines functions set_obs

Documented in set_obs

#' Set unstructured data
#'
#' Set the sample observations layer (i.e. cell metadata)
#'  in any single-cell object that has one.
#' @param obs Observation metadata
#' as a \link[base]{data.frame} with samples as row names.
#' @inheritParams converters
#' @returns Single-cell object.
#'
#' @export
#' @examples
#' obj <- example_obj("ad")
#' obs <- get_obs(obj)
#' obs$new_col <- c(1,2)
#' obj2 <- set_obs(obj = obj,
#'                 obs = obs)
set_obs <- function(obj,
                    obs,
                    verbose=TRUE){

  messager("Setting cell metadata (obs) in obj.",v=verbose)
  obs <- check_metadata_rownames(d = obs,
                                 verbose = verbose)
  #### anndata ####
  if(is_class(obj,"anndata")){
    obj$obs <- obs[obj$obs_names,]
  #### seurat ####
  } else if(is_class(obj,"seurat")){
    obj@meta.data <- obs[colnames(obj),]
  }
  return(obj)
}
bschilder/scKirby documentation built on Oct. 2, 2024, 10:16 p.m.