R/Distinguisher.R

Defines functions Distinguisher

Documented in Distinguisher

#' Title
#'
#' @param bulkdata A matrix with genes in rows and samples in columns.
#' @param celltypenum Specify the range of underlying subpopulation number.
#' @importFrom CellDistinguisher gecd_CellDistinguisher gecd_DeconvolutionByDistinguishers
#' @return  A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#'
#'  Bulk <- Bulk_GSE60424
#'  res <- Distinguisher(bulkdata = Bulk,
#'                       celltypenum = 5)
#'






Distinguisher <- function(bulkdata,celltypenum) {


  exprLinear <- bulkdata[rowSums(bulkdata==0) != ncol(bulkdata), ]

  exprLinear <- as.matrix(exprLinear)

  distinguishers <- CellDistinguisher::gecd_CellDistinguisher(exprLinear,
                                                              genesymb=NULL,
                                                              expressionQuantileForScale = 0.75,
                                                              expressionQuantileForFilter = 1,
                                                              expressionConcentrationRatio = 0.333,
                                                              probesWithGenesOnly = FALSE,
                                                              numCellClasses=celltypenum,
                                                              minDistinguisherAlternatives=1,
                                                              maxDistinguisherAlternatives=5,
                                                              verbose=0)


  deconvolution <- CellDistinguisher::gecd_DeconvolutionByDistinguishers(exprLinear,
                                                                         distinguishers$bestDistinguishers,
                                                                         nonNegativeOnly = TRUE,
                                                                         convexSolution = TRUE,
                                                                         verbose = 0)

  marker_ann <- distinguishers[["bestDistinguishers"]]

  marker_ann <- as.data.frame(na.omit(marker_ann))

  anncell <- NULL


  probpath <- system.file("extdata", "PanglaoDB_markers_27_Mar_2020.tsv", package = "deconvBench")

  pl <- read.table(probpath,sep = "\t",header = T)

  Plhuman <- pl[pl$species!="Mm",]


  for (i in 1:ncol(marker_ann)) {

    marker <- marker_ann[,i]

    celltype <- NULL

    for (m in marker ) {

       ct <- Plhuman[which(Plhuman$official.gene.symbol==m),3]

      celltype <-c(celltype,ct)
    }

    uniquecell <- unique(celltype)

    times_cell <- tabulate(match(celltype, uniquecell))

    names(times_cell) <- uniquecell

    cell <- names(sort(times_cell,decreasing = T))[1]

    anncell <- c(anncell,cell)
  }

  cellDistinguisher_res <- t(deconvolution$sampleCompositions)

  colnames(cellDistinguisher_res) <-  paste(anncell,1:ncol(marker_ann),sep = "_")

  res_Celldistinguisher <- cellDistinguisher_res

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