This demo will walk you through a complete dataset projection and analysis using ProjecTILs. We will use as a query set the Tetramer+ CD8+ T cells from the study by Miller et al., Nature Immunol (2019)

First, install ProjecTILs. For detailed installation instructions refer to the ProjecTILs Github page

remotes::install_github("carmonalab/ProjecTILs")
library(ProjecTILs)

Load reference atlas and query data

First, load the default reference TIL atlas. If no reference map file is provided, the function load.reference.map() will automatically download it from https://doi.org/10.6084/m9.figshare.12478571

ref <- load.reference.map()
## [1] "Loading Default Reference Atlas..."
## [1] "/Users/mass/Documents/Projects/Cell_clustering/ProjecTILs.demo/ref_TILAtlas_mouse_v1.rds"
## [1] "Loaded Reference map ref_TILAtlas_mouse_v1"

Let’s explore the reference atlas

refCols <- c("#edbe2a", "#A58AFF", "#53B400", "#F8766D", "#00B6EB", "#d1cfcc", "#FF0000", "#87f6a5", "#e812dd")
DimPlot(ref,label = T, cols = refCols)

See expression of important marker genes across reference subtypes

markers <- c("Cd4","Cd8a","Ccr7","Tcf7","Pdcd1","Havcr2","Tox","Izumo1r","Cxcr6","Xcl1","Gzmb","Gzmk","Ifng","Foxp3")
VlnPlot(ref,features=markers,stack = T,flip = T,assay = "RNA")

Now let’s load a query dataset - Miller et al., Nature Immunol (2019)

#A sample data set is provided with the ProjecTILs package
querydata <- ProjecTILs::query_example_seurat

More generally, it is possible to load a query matrix with gene names and barcodes (e.g. 10X format or raw counts)

##10X format
fname2 <- "./sample_data"
querydata2 <- read.sc.query(fname2, type="10x")

##Raw count matrix from GEO
library(GEOquery)
geo_acc <- "GSE86028"
getGEOSuppFiles(geo_acc)

fname3 <- sprintf("%s/GSE86028_TILs_sc_wt_mtko.tpm.log2.txt.gz", geo_acc)
querydata3 <- read.sc.query(fname3, type = "raw.log2")

Run Projection algorithm

query.projected <- make.projection(querydata, ref=ref)
## [1] "Using assay RNA for query"
## celltype.pred
##          CD4T          CD8T     Non-Tcell Tcell_unknown          Treg 
##             1          1288           183            17            11 
##       unknown 
##             1 
## CD8Tstate
##          Naive EffectorMemory     MemoryLike      Exhausted        unknown 
##              5             15             62           1201              5 
## [1] "184 out of 1501 ( 12 % ) non-pure T cells removed.  Use filter.cells=FALSE to avoid pre-filtering (NOT RECOMMENDED)"
## [1] "Aligning query to reference map for batch-correction..."
## 
## Projecting corrected query onto Reference PCA space
## [1] "Projecting corrected query onto Reference UMAP space"

Plot projection of new data over the reference in UMAP space. The contour lines display the density of projected query cells onto the reference map.

plot.projection(ref, query.projected)

Predict cell states

Predict the cell states in the query set using a nearest-neighbor algorithm

query.projected <- cellstate.predict(ref=ref, query=query.projected)
table(query.projected$functional.cluster)
## 
##     CD8_EarlyActiv CD8_EffectorMemory      CD8_NaiveLike            CD8_Tex 
##                  1                 68                 14               1173 
##           CD8_Tpex               Treg 
##                 54                  7

Plot the predicted composition of the query in terms of reference T cell subtypes

plot.statepred.composition(ref, query.projected,metric = "Percent")

How do the gene expression levels compare between reference and query for the different cell states?

plot.states.radar(ref, query=query.projected, min.cells = 20)

Compare states across conditions

If we have multiple conditions (e.g. control vs. treatment, or samples from different tissues), we can search for discriminant genes between conditions (otherwise, by default this analysis is performed against the reference subtype as the ‘control’)

#Simulate a condition which e.g. increases Gzmb expression compared to control
query.control <- subset(query.projected, subset=`Gzmb` < 1.5)
query.perturb <- subset(query.projected, subset=`Gzmb` >= 1.5)

plot.states.radar(ref, query=list("Control" = query.control, "Query" = query.perturb))

In this toy example, where we simulated a condition that increases Gzmb expression compared to control, we expect cytotoxicity genes to drive differences.

discriminantGenes <- find.discriminant.genes(ref=ref, query=query.perturb, query.control=query.control, state="CD8_Tex")
head(discriminantGenes,n=10)
##                  p_val avg_log2FC pct.1 pct.2    p_val_adj
## Gzmb      5.725952e-84  2.8687747 1.000 0.836 1.565533e-79
## Gzmc      3.503195e-32  2.9141749 0.803 0.370 9.578087e-28
## Lgals1    1.615188e-23  0.4724190 1.000 1.000 4.416087e-19
## AA467197  5.722795e-20  1.2066027 0.850 0.541 1.564669e-15
## Ccl9      9.238058e-16  1.6495391 0.490 0.130 2.525778e-11
## Mt1       9.872509e-16  1.1729060 0.775 0.466 2.699243e-11
## Serpinb6b 2.637569e-15  0.8289507 0.863 0.575 7.211377e-11
## Prf1      1.533166e-12  0.5716789 0.511 0.178 4.191829e-08
## Gzme      4.762643e-12  2.0662138 0.465 0.178 1.302154e-07
## Mt2       1.798971e-11  0.8955465 0.603 0.295 4.918565e-07

We can use a volcano plot to display differentially expressed genes:

library(EnhancedVolcano)
## Loading required package: ggrepel
## Registered S3 methods overwritten by 'ggalt':
##   method                  from   
##   grid.draw.absoluteGrob  ggplot2
##   grobHeight.absoluteGrob ggplot2
##   grobWidth.absoluteGrob  ggplot2
##   grobX.absoluteGrob      ggplot2
##   grobY.absoluteGrob      ggplot2
EnhancedVolcano(discriminantGenes, lab = rownames(discriminantGenes), x = "avg_log2FC", y = "p_val", pCutoff = 1e-09, 
    FCcutoff = 0.5, labSize = 5, legendPosition = "none", drawConnectors = F, title = "Gzmb_high vs. Gzmb_low (Tex)")

Using a random subsetting, p-values should not be significant:

rand.list <- ProjecTILs:::randomSplit(query.projected, n=2, seed=1)
discriminantGenes <- find.discriminant.genes(ref=ref, query=rand.list[[1]], query.control=rand.list[[2]], state="CD8_Tex")
EnhancedVolcano(discriminantGenes, lab = rownames(discriminantGenes), x = "avg_log2FC", y = "p_val", pCutoff = 1e-09, 
    FCcutoff = 0.5, labSize = 5, legendPosition = "none", drawConnectors = F, title = "Random split (Tex)")

Find discriminant dimensions

The dimensions in UMAP space summarize the main axes of variability of the reference map. What if the query data contains novel states? We can search for additional, maximally discriminant dimensions (either in ICA or PCA space) that explain new variability in the query set.

As before, simulate a condition which increases Gzmb expression compared to control

#
query.control <- subset(query.projected, subset=`Gzmb` < 1.5)
query.perturb <- subset(query.projected, subset=`Gzmb` >= 1.5)

plot.states.radar(ref, query=list("Control" = query.control, "Query" = query.perturb))

In this toy example, we expect some gene module associated with granzymes to drive the discriminant analysis:

top.ica.wcontrol <- find.discriminant.dimensions(ref=ref, query=query.perturb, query.control=query.control)
head(top.ica.wcontrol)
##              stat  stat_abs        p_val
## ICA_26  0.4433848 0.4433848 0.000000e+00
## ICA_24 -0.3075389 0.3075389 3.153405e-09
## ICA_42  0.2895386 0.2895386 4.921312e-08
## ICA_19  0.2442144 0.2442144 2.388393e-05
## ICA_28 -0.2368249 0.2368249 5.926225e-05
## ICA_32  0.2156367 0.2156367 6.873906e-04
VizDimLoadings(ref, reduction = "ica", nfeatures = 10, dims=c(26,24,42), ncol=3)

Now we can plot the ICA dimension that captured the genetic changes associated to the perturbation of increasing Gzmb

plot3d <- plot.discriminant.3d(ref, query=query.perturb, query.control=query.control, extra.dim="ICA_26")
plot3d

We can plot other metadata in the z-axis of the UMAP, e.g. the cycling score calculated by the TILPRED cycling signature

plot3d <- plot.discriminant.3d(ref, query.projected, extra.dim="cycling.score")
plot3d

Focus the plot only on a specific state

plot3d <- plot.discriminant.3d(ref, query.projected, extra.dim="cycling.score", query.state="CD8_Tex")
plot3d

Using a random subsetting, p-values should not be significant:

rand.list <- ProjecTILs:::randomSplit(query.projected, n=2, seed=1)
top.ica.ks.rand <- find.discriminant.dimensions(ref=ref, query=rand.list[[1]], query.control=rand.list[[2]], reduction="ica")
top.ica.ttest.rand <- find.discriminant.dimensions(ref=ref, query=rand.list[[1]], query.control=rand.list[[2]], reduction="ica", test = "t-test")

Further information

ProjecTILs repository

ProjecTILs case studies - INDEX - Repository

Publication: pre-print