R/Scdc.R

Defines functions Scdc

Documented in Scdc

#' SCDC
#'
#' @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.
#' @import Seurat
#' @importFrom BiocGenerics colnames
#' @importFrom methods new
#' @importFrom Biobase ExpressionSet
#' @importFrom SCDC SCDC_prop SCDC_prop_ONE
#' @return Mixed cell composition estimation ratio data frame.
#' @export
#'
#' @examples
#'
#' Bulk <- Bulk_GSE60424
#' SC <- SC_GSE60424
#' Label <- Label_GSE60424$Label
#' res <- Scdc(bulkdata = Bulk,
#'             scdata = SC,
#'             label = Label)
#'


Scdc <- function(scdata, bulkdata,label) {




  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 <- Seurat::RunPCA(scRNA,features = VariableFeatures(object = scRNA))

  scRNA <- Seurat::FindNeighbors(scRNA, dims = 1:15)

  scRNA <- Seurat::FindClusters(scRNA, resolution = 0.5)

  scRNA  <- Seurat::RunTSNE(scRNA , dims = 1:15, check_duplicates = FALSE, perplexity=20)

  factor_label <- as.factor(label)

  names(factor_label) <- BiocGenerics::colnames(scdata)

  scRNA@active.ident <- factor_label

  scRNA@meta.data$cell_type <- label


  phenoData <- methods::new("AnnotatedDataFrame", data=scRNA@meta.data)

  sc.eset <- Biobase::ExpressionSet(assayData = as.matrix(scdata),phenoData = phenoData)

  bulk.eset <- Biobase::ExpressionSet(assayData = as.matrix(bulkdata))

  if( length(levels(sc.eset@phenoData@data$orig.ident))==1){

    SCDC_res <- SCDC::SCDC_prop_ONE(bulk.eset,
                                    sc.eset,
                                    ct.varname = "cell_type",
                                    sample = "orig.ident",
                                    ct.sub = names(table(sc.eset@phenoData@data$cell_type)))


  }else{
    SCDC_res <- SCDC::SCDC_prop(bulk.eset,
                                sc.eset,
                                ct.varname = "cell_type",
                                sample = "orig.ident",
                                ct.sub = names(table(sc.eset@phenoData@data$cell_type)))
     }






  res_Scdc <- SCDC_res$prop.est.mvw

  return(res_Scdc)

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