R/Enigma.R

Defines functions Enigma

Documented in Enigma

#' Title
#' @param scdata single data with genes in rows and cells in columns.
#' @param bulkdata A matrix with genes in rows and samples in columns.
#' @param label The cell type label of single data.
#' @param reftype Determine the reference type. Should be either "single_cell" or "sort".
#' @importFrom ENIGMA create_ENIGMA batch_correct
#' @importFrom Seurat CreateSeuratObject NormalizeData FindVariableFeatures ScaleData
#' @importFrom BisqueRNA SeuratToExpressionSet
#' @importFrom Biobase AnnotatedDataFrame
#' @return   A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#'
#'
#' # Bulk <- Bulk_GSE60424
#' # SC <- SC_GSE60424
#' # Label <- Label_GSE60424$Label
#' # res <- Enigma(bulkdata = Bulk,
#' #               scdata = SC,
#' #               label = Label)
#'

Enigma <- function(scdata, bulkdata,label,reftype = "single_cell") {

  gene <- intersect(rownames(scdata),rownames(bulkdata))

  scdata <- scdata[gene,]

  bulkdata <- bulkdata[gene,]

  scRNA <- Seurat::CreateSeuratObject(counts = scdata, project = "SC")

  scRNA <- Seurat::NormalizeData(scRNA, verbose = FALSE)

  scRNA <- Seurat::FindVariableFeatures(scRNA, selection.method = "vst",nfeatures = 2000)

  all.genes <- rownames(scRNA)

  scRNA <- Seurat::ScaleData(scRNA, features = all.genes)

  scRNA@meta.data$cell_type <- label

  sc.eset <- BisqueRNA::SeuratToExpressionSet(scRNA,
                                              delimiter="[.]",
                                              position=1,
                                              version="v3")

  sc.eset@phenoData <- Biobase::AnnotatedDataFrame(scRNA@meta.data)


  egm = ENIGMA::create_ENIGMA(bulk = as.matrix(bulkdata),
                              ref = exprs(sc.eset),
                              ref_type = reftype,
                              meta_ref = pData(sc.eset))



  ### mode batch effect correction
  egm = ENIGMA::batch_correct(egm,
                              varname_cell_type ="cell_type",
                              n_pseudo_bulk=100)

  egm <-  ENIGMA::get_cell_proportion(egm)

  res_Enigma <- egm@result_cell_proportion

  return(res_Enigma)
}
libcell/deconvBench documentation built on Sept. 24, 2022, 12:36 p.m.