R/quietTCRgenes.R

Defines functions quietTCRgenes

Documented in quietTCRgenes

#' Remove TCR genes from variable gene results
#'
#'Most single-cell workflows use highly-expressed and highly-variable
#'genes for the initial calculation of PCA and subsequent dimensional
#'reduction. This function will remove the TCR genes from the variable
#'features in the Seurat object or from a vector generated by
#'the Bioconductor scran workflow. 
#'
#' @examples
#' trex_example <- quietTCRgenes(trex_example)
#' 
#' @param sc Single-cell object in Seurat format or vector of variable genes to use in reduction
#' @param assay The Seurat assay slot to use to remove TCR genes from, NULL value will default to
#' the default assay
#' @importFrom SeuratObject DefaultAssay VariableFeatures
#' @importFrom utils getFromNamespace
#' @export
#' @return Seurat object or vector list with TCR genes removed.
#' @author Nicky de Vrij Nikolaj Pagh Nick Borcherding
quietTCRgenes <- function(sc, 
                          assay = NULL) {
  'VariableFeatures<-'<-utils::getFromNamespace("VariableFeatures<-", "SeuratObject")
  unwanted_genes <- "^TR[ABDG][VDJ][^D]"
  if (inherits(x=sc, what ="Seurat")) {
    if (is.null(assay)) {
      assay <- DefaultAssay(sc)
    }
    unwanted_genes <- grep(pattern = unwanted_genes, x = VariableFeatures(sc, assay = assay), value = TRUE)
    VariableFeatures(sc, assay = assay) <- VariableFeatures(sc, assay = assay)[VariableFeatures(sc, assay = assay) %!in% unwanted_genes]
  } else {
    #Bioconductor scran pipelines uses vector of variable genes for DR
    unwanted_genes <- grep(pattern = unwanted_genes, x = sc, value = TRUE)
    sc <- sc[sc %!in% unwanted_genes]
  }
  return(sc)
}
ncborcherding/Trex documentation built on Nov. 4, 2024, 10:31 p.m.