R/BisqueRNA.R

Defines functions Bisquerna

Documented in Bisquerna

#' 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.
#' @importFrom Seurat CreateSeuratObject NormalizeData FindVariableFeatures ScaleData
#' @importFrom BisqueRNA SeuratToExpressionSet
#' @importFrom Biobase ExpressionSet
#' @importFrom dplyr top_n group_by
#' @importFrom  vctrs data_frame
#' @import BisqueRNA
#' @return   A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#'
#'
#'
#' Bulk <- Bulk_GSE60424
#' SC <- SC_GSE60424
#' Label <- Label_GSE60424$Label
#' res <- Bisquerna(bulkdata = Bulk,
#'                  scdata = SC,
#'                  label = Label)
#'

Bisquerna <- 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)

  factor_label <- as.factor(label)

  names(factor_label) <- colnames(scdata)

  scRNA@active.ident <- factor_label

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

  markers <- Seurat::FindAllMarkers(scRNA,
                                    only.pos = TRUE,
                                    min.pct = 0.25,
                                    test.use = "LR",
                                    logfc.threshold = 0.25)



  FC_Marker10 <- dplyr::top_n(dplyr::group_by(markers,cluster),n = 50, wt = avg_log2FC)

  BIS_markers <-  vctrs::data_frame("gene" = FC_Marker10$gene,
                                    "cluster" = FC_Marker10$cluster,
                                    "avg_logFC" = FC_Marker10$avg_log2FC)


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

  BisqueRNA_res <- BisqueRNA::ReferenceBasedDecomposition(bulk.eset,
                                                          sc.eset,
                                                          markers=BIS_markers,
                                                          use.overlap = FALSE,
                                                          verbose = TRUE)

  res_Bisquerna <- t(as.matrix(BisqueRNA_res$bulk.props))


  return(res_Bisquerna)

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