R/Autogenes.R

Defines functions Autogenes

Documented in Autogenes

#'#' Autogenes
#'
#' @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 nnls or nusvr.
#'
#' @importFrom reticulate source_python
#' @importFrom utils write.csv write.table
#' @importFrom SeuratDisk Convert SaveH5Seurat
#' @import Seurat
#' @return A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#' # Bulk <- Bulk_GSE106898
#' # SC <- SC_GSE60424
#' # Label <- Label_GSE60424$Label
#' # res <- Autogenes(bulkdata = Bulk,
#' #                  scdata = SC,
#' #                  label = Label,
#' #                  SubMethod = "nnls")
#'
#'
Autogenes <- function(scdata,bulkdata,label,SubMethod = "nnls") {

  bulkdata <- bulkdata[apply(bulkdata, 1, var)!=0,]

  scdata <- scdata[apply(scdata, 1, var)!=0,]

  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)

  scRNA <- Seurat::RunPCA(scRNA,features = Seurat::VariableFeatures(object = scRNA))

  scRNA <- Seurat::FindNeighbors(scRNA, dims = 1:15)

  scRNA <- Seurat::FindClusters(scRNA, resolution = 0.5)

  scRNA  <- Seurat::RunTSNE(scRNA , dims = 1:15, check_duplicates = FALSE, perplexity=20)

  scRNA@active.ident <- scRNA$orig.ident

  factor_label <- as.factor(label)

  names(factor_label) <- BiocGenerics::colnames(scdata)

  scRNA@active.ident <- factor_label

  scRNA@meta.data$cells <- label

  tmp_path <- tempdir()

  Autogenes_filepath <- paste0(tmp_path,"/result_Autogenes/")


  if(file.exists(Autogenes_filepath)){
    path <- Autogenes_filepath
  }else{
    dir.create(Autogenes_filepath)
    path <- Autogenes_filepath
  }

  allfile = dir(Autogenes_filepath,full.names = T)

  print(allfile)

  file.remove(allfile)

  SeuratDisk::SaveH5Seurat(scRNA,filename = paste0(Autogenes_filepath,"scRNA.h5Seurat"))

  SeuratDisk::Convert(paste0(Autogenes_filepath,"scRNA.h5Seurat"), dest = paste0(path,"scRNA.h5ad"))

  utils::write.csv(bulkdata,paste0(Autogenes_filepath,"Bulk_Data.csv"))

  probpath <- system.file(package = "deconvBench")

  Autogenes_probpath <- paste0(probpath,"/python/rpytools/")

  reticulate::source_python(paste0(Autogenes_probpath,"/Autogenes.py"))

  Autogenes_python(Autogenes_filepath)

  if(SubMethod == "nnls"){
    res_Autogenes <- read.csv(paste0(Autogenes_filepath,"/Autogenes_nnls.csv"),row.names = 1) #NNLS
  }

  if(SubMethod == "nusvr"){
    res_Autogenes <- read.csv(paste0(Autogenes_filepath,"/Autogenes_nusvr.csv"),row.names = 1) #NuSVR
  }

  return(res_Autogenes)

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