R/Dtangle.R

Defines functions Dtangle

Documented in Dtangle

#'Dtangle
#'
#' @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 limma normalizeBetweenArrays
#' @importFrom dtangle find_markers dtangle
#' @return   A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#'
#' Bulk <- Bulk_GSE60424
#' SC <- SC_GSE60424
#' Label <- Label_GSE60424$Label
#' res <- Dtangle(bulkdata = Bulk,
#'                scdata = SC,
#'                label = Label)
#'



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

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

  bulkdata.match <- bulkdata[pmatch(gene, rownames(bulkdata)), ]

  scdata.match <- scdata[pmatch(gene, rownames(scdata)), ]

  y <- cbind(bulkdata.match, scdata.match)

  y <- limma::normalizeBetweenArrays(y)

  y <- t(y)

  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

  marker_list = dtangle::find_markers(y,pure_samples=pure_samples, data_type="rna-seq",marker_method='ratio')

  quantiles = lapply(marker_list$V,function(x)quantile(x,0.5))

  K = length(pure_samples)

  n_markers = sapply(1:K,function(i){max(which(marker_list$V[[i]] > quantiles[[i]]))})

  marks = marker_list$L

  dc <- dtangle::dtangle(y,pure_samples=pure_samples,n_markers=n_markers,data_type="rna-seq", markers = marks)

  final_est <- dc$estimates[1:dim(bulkdata.match)[2],]

  colnames(final_est) <- all_cell_type

  res_Dtangle <- as.data.frame(final_est)

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