web123456

[AUCell] Scoring of single-cell gene sets

Scoring gene sets at the single-cell level can be done in addition to enrichment as with analyses such as GSVA by cellmarker gene set does subpopulation annotation (need to pay attention to the problem of cell ratio, the number of small groups does not work, can be used as an aid). The previously written scMetabolism package scores metabolic pathways, same idea.

AUCell is ranking-based, so it doesn't matter exactly which normalization method is used, as long as it doesn't affect the ordering of gene expression within each cell. In general, it is divided into three steps:

1,build the rankings

2,calculate the AUC

3,set the assignment threshold

Installation of R packages

  1. if (!requireNamespace("BiocManager", quietly=TRUE))
  2. ("BiocManager")
  3. Biocmanager::install("AUCell")
  4. # To support paralell execution:
  5. BiocManager::install(c("doMC", "doRNG","doSNOW"))
  6. # For the main example:
  7. BiocManager::install(c("mixtools", "GEOquery", "SummarizedExperiment"))
  8. # For the examples in the follow-up section of the tutorial:
  9. BiocManager::install(c("DT", "plotly", "NMF", "d3heatmap", "shiny", "rbokeh",
  10. "dynamicTreeCut","R2HTML","Rtsne", "zoo"))

  • load the scRNA expression matrix and gene set
  1. # . Reading from a text file
  2. #exprMatrix <- ("")
  3. #exprMatrix <- (exprMatrix)
  4. # or using an expression set
  5. #exprMatrix <- exprs(myExpressionSet)
  6. # (This may take a few minutes)
  7. library(GEOquery)
  8. attemptsLeft <- 20
  9. while(attemptsLeft>0)
  10. {
  11. geoFile <- tryCatch(getGEOSuppFiles("GSE60361", makeDirectory=FALSE), error=identity)
  12. if(methods::is(geoFile,"error"))
  13. {
  14. attemptsLeft <- attemptsLeft-1
  15. (5)
  16. }
  17. else
  18. attemptsLeft <- 0
  19. }
  20. gzFile <- grep(".", basename(rownames(geoFile)), fixed=TRUE, value=TRUE)
  21. message(gzFile)
  22. txtFile <- gsub(".gz", "", gzFile, fixed=TRUE)
  23. message(txtFile)
  24. gunzip(filename=gzFile, destname=txtFile, remove=TRUE)
  25. library()
  26. geoData <- fread(txtFile, sep="\t")
  27. geneNames <- unname(unlist(geoData[,1, with=FALSE]))
  28. exprMatrix <- (geoData[,-1, with=FALSE])
  29. rm(geoData)
  30. dim(exprMatrix)
  31. rownames(exprMatrix) <- geneNames
  32. exprMatrix[1:5,1:4]
  33. # Remove file
  34. (txtFile)
  35. # Save for future use
  36. mouseBrainExprMatrix <- exprMatrix
  37. save(mouseBrainExprMatrix, file="exprMatrix_AUCellVignette_MouseBrain.RData")

pick random 5000 genes to release computational burden

  1. # load("exprMatrix_AUCellVignette_MouseBrain.RData")
  2. (333)
  3. exprMatrix <- mouseBrainExprMatrix[sample(rownames(mouseBrainExprMatrix), 5000),]
  4. dim(exprMatrix)

the next thing we need to prepare is the gene set. use ?AUCell_calcAUC to check accepted forms.

  1. library(GSEABase)
  2. #gene list version
  3. genes <- c("gene1", "gene2", "gene3")
  4. geneSets <- GeneSet(genes, setName="geneSet1")
  5. geneSets
  6. #gmt version
  7. library(AUCell)
  8. library(GSEABase)
  9. gmtFile <- paste((('examples', package='AUCell')), "", sep="/")
  10. geneSets <- getGmt(gmtFile)
  11. #check how many of these genes are in the expression matrix
  12. geneSets <- subsetGeneSets(geneSets, rownames(exprMatrix))
  13. cbind(nGenes(geneSets))
  14. #add the gene set size to its name
  15. geneSets <- setGeneSetNames(geneSets, newNames=paste(names(geneSets), " (", nGenes(geneSets) ,"g)", sep=""))
  16. #let’s also add a few sets of random genes and 100 genes expressed in many cells (. housekeeping-like):
  17. # Random
  18. (321)
  19. extraGeneSets <- c(
  20. GeneSet(sample(rownames(exprMatrix), 50), setName="Random (50g)"),
  21. GeneSet(sample(rownames(exprMatrix), 500), setName="Random (500g)"))
  22. countsPerGene <- apply(exprMatrix, 1, function(x) sum(x>0))
  23. # Housekeeping-like
  24. extraGeneSets <- c(extraGeneSets,
  25. GeneSet(sample(names(countsPerGene)[which(countsPerGene>quantile(countsPerGene, probs=.95))], 100), setName="HK-like (100g)"))
  26. geneSets <- GeneSetCollection(c(geneSets,extraGeneSets))
  27. names(geneSets)

  • build gene expression rankings for each cell
  1. cells_rankings <- AUCell_buildRankings(exprMatrix, nCores=1, plotStats=TRUE)
  2. cells_rankings

 

  1. #The “rankings” can be seen as a new representation of the original dataset. Once #they are calculated, they can be saved for future analyses.
  2. save(cells_rankings, file="cells_rankings.RData")
  • calculate enrichment for the signature (AUC)

To determine whether the gene set is enriched at the top of the gene-ranking for each cell, AUCell uses the “Area Under the Curve” (AUC) of the recovery curve.

 

  1. cells_AUC <- AUCell_calcAUC(geneSets, cells_rankings)
  2. save(cells_AUC, file="cells_AUC.RData")

col is the cell id, row is gene set, AUC in each cell.

  • determine the cells with given gene signatures or active gene sets

The AUC estimates the proportion of genes in the gene-set that are highly expressed in each cell. 

The ideal situation will be a bi-modal distribution, in which most cells in the dataset have a low “AUC” compared to a population of cells with a clearly higher value

Note that if the gene set is a cellular marker, the cellular subpopulation ratio must be taken into account. If the subpopulation ratio is very high, then the low peaks in the double peaks may be masked and behave more like house keeping genes (single peak or even constant expression); similarly, if the ratio is very small, the peaks may be so insignificant that they cannot be seen.

In addition, the number of genes in the gene set should not be too small, or there is a possibility that none of the genes in the gene set can match the top 5% of the matrix, at which time the AUC=0. Therefore, the length of the gene set is more appropriate at 100-2k.

To ease the exploration of the distributions, the function AUCell_exploreThresholds() automatically plots all the histograms and calculates several thresholds that could be used to consider a gene-set ‘active’ (returned in $aucThr). The distributions are plotted as dotted lines over the histogram and the corresponding thresholds as vertical bars in the matching color. The thicker vertical line indicates the threshold selected by default ($aucThr$selected): the highest value to reduce the false positives.

  1. (123)
  2. par(mfrow=c(3,3))
  3. cells_assignment <- AUCell_exploreThresholds(cells_AUC, plotHist=TRUE, assign=TRUE)
  1. # Look at the alerts stored in the results
  2. warningMsg <- sapply(cells_assignment, function(x) x$aucThr$comment)
  3. warningMsg[which(warningMsg!="")]
  4. ## Microglia_lavin (165g)
  5. ## "The AUC might follow a normal distribution (random gene-set?). "
  6. ## Random (500g)
  7. ## "The AUC might follow a normal distribution (random gene-set?). The global distribution overlaps the partial distributions. "

The thresholds calcuated for each gene set are stored in the $aucThr slot. For example, the thresholds suggested for the oligodendrocyte gene-set:

cells_assignment$Oligodendrocyte_Cahoy$aucThr$thresholds
  1. ## threshold nCells
  2. ## Global_k1 0.2419241 739
  3. ## L_k2 0.2437017 733
  4. ## R_k3 0.1766730 1008
  5. ## minimumDens 0.2417192 740

To obtain the threshold selected automatically for a given gene set(. Oligodendrocytes):

cells_assignment$Oligodendrocyte_Cahoy$aucThr$selected

Cells assigned at this threshold (AUC over the threshold)

  1. oligodencrocytesAssigned <- cells_assignment$Oligodendrocyte_Cahoy$assignment
  2. length(oligodencrocytesAssigned)
  3. head(oligodencrocytesAssigned)

Plotting the AUC histogram of a specific gene set, and setting a new threshold:

  1. geneSetName <- rownames(cells_AUC)[grep("Oligodendrocyte_Cahoy", rownames(cells_AUC))]
  2. AUCell_plotHist(cells_AUC[geneSetName,], aucThr=0.25)
  3. abline(v=0.25)

 

Assigning cells to this new threshold:

  1. newSelectedCells <- names(which(getAUC(cells_AUC)[geneSetName,]>0.08))
  2. length(newSelectedCells)
head(newSelectedCells)

Now, with the gene set scored in each cell, explore common visualization ideas, see the official documentation.

AUCell: Identifying cells with active gene sets

(1) Heat map (on-off binary or continuous values)

 

(2)feature plot