R/Music.R

Defines functions Music

Documented in Music

#' 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 SubMethod the methods type "nnls","weighted".
#' @importFrom Seurat CreateSeuratObject NormalizeData FindVariableFeatures ScaleData
#' @importFrom BisqueRNA SeuratToExpressionSet
#' @importFrom MuSiC music_prop
#' @return  A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#' # Bulk <- Bulk_GSE60424
#' # SC <- SC_GSE60424
#' # Label <- Label_GSE60424$Label
#' # res <-  Music(bulkdata = Bulk,
#' #               scdata = SC,
#' #               label = Label,
#' #               SubMethod = "weighted")

Music <- function(scdata, bulkdata,label,SubMethod = "weighted") {


  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)

  factor_label <- as.factor(label)

  names(factor_label) <- colnames(scdata)

  scRNA@active.ident <- factor_label

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



  MuSiC_res <- MuSiC::music_prop(bulk.eset = as.matrix(bulkdata),
                                 sc.eset = sc.eset,
                                 clusters = 'cellType',
                                 samples = 'SubjectName',
                                 select.ct = names(BiocGenerics::table(sc.eset@phenoData@data$cellType)),
                                 verbose = F)



  if( SubMethod == "weighted"){
    MuSiC_res <- MuSiC_res$Est.prop.weighted # weighted
  }
  if(SubMethod == "nnls"){
    MuSiC_res  <- MuSiC_res$Est.prop.allgene #nnls
  }

  res_Music <- MuSiC_res

  return(res_Music)

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