R/Demixt.R

Defines functions Demixt

Documented in Demixt

#' 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.
#' @import DeMixT
#' @import parallel
#' @return   A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#' #Bulk <- Bulk_GSE106898
#' #SC <- SC_GSE107011
#' #Label <- Label_GSE107011$Label
#' #res <- Demixt(bulkdata = Bulk,
#' #              scdata = SC,
#' #              label = Label)

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


  all_cell_type <- unique(as.character(label))

  pure_samples <- lapply(1:length(all_cell_type), function(i) {

    which(as.character(label) == all_cell_type[i])
  })

  names(pure_samples) = all_cell_type

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

  Demixt_res <- NULL

  for (i in 1:length(all_cell_type)) {

     big_BC <- cbind(bulkdata[gene,],scdata[gene,pure_samples[[i]]])

     data <- big_BC

     star <- length(colnames(bulkdata))+1

     end <- dim(data)[2]

     opsation <- c(star:end)

     try( res.GS <-  DeMixT::DeMixT(data.Y = as.matrix(data[, colnames(bulkdata)]),
                               data.comp1 = as.matrix(data[,opsation ]),
                               niter = 10,
                               nbin = 50,
                               if.filter = TRUE,
                               mean.diff.in.CM = 0.25,
                               ngene.selected.for.pi = 250,
                               tol = 10^(-5)))



       if("try-error" %in% class(res.GS)){
         next()
       }else{
         Demixt_temp <- res.GS$pi
         rownames(Demixt_temp) <- all_cell_type[i]
         Demixt_res <- rbind(Demixt_res,Demixt_temp)
       }


  }


  res_Demixt <- t(Demixt_res)


  return(res_Demixt)

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