Cell Line L2 Analysis

1. load libraries

2. Load Seurat Object

#Load Seurat Object L2
load("../Documents/1-SS-STeps/4-Analysis_and_Robj_Marie/analyse juillet 2023/ObjetsR/L2.Robj")

L2
## An object of class Seurat 
## 36629 features across 5935 samples within 2 assays 
## Active assay: RNA (36601 features, 0 variable features)
##  2 layers present: counts, data
##  1 other assay present: ADT

3. QC

# Set identity classes to an existing column in meta data
Idents(object = L2) <- "cell_line"

L2[["percent.rb"]] <- PercentageFeatureSet(L2, pattern = "^RP[SL]")

VlnPlot(L2, features = c("nFeature_RNA", 
                                         "nCount_RNA", 
                                         "percent.mito",
                                         "percent.rb"), 
                            ncol = 4, pt.size = 0.1) & 
              theme(plot.title = element_text(size=10))

FeatureScatter(L2, feature1 = "percent.mito", 
                                  feature2 = "percent.rb")

VlnPlot(L2, features = c("nFeature_RNA", 
                                    "nCount_RNA", 
                                    "percent.mito"), 
                                      ncol = 3)

FeatureScatter(L2, 
               feature1 = "percent.mito", 
               feature2 = "percent.rb") +
        geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

FeatureScatter(L2, 
               feature1 = "nCount_RNA", 
               feature2 = "nFeature_RNA") +
        geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

##FeatureScatter is typically used to visualize feature-feature relationships ##for anything calculated by the object, ##i.e. columns in object metadata, PC scores etc.

FeatureScatter(L2, 
               feature1 = "nCount_RNA", 
               feature2 = "percent.mito")+
  geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

FeatureScatter(L2, 
               feature1 = "nCount_RNA", 
               feature2 = "nFeature_RNA")+
  geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

## Assign Cell-Cycle Scores

## Running SCTransform on assay: RNA
## vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
## Calculating cell attributes from input UMI matrix: log_umi
## Variance stabilizing transformation of count matrix of size 18709 by 5935
## Model formula is y ~ log_umi
## Get Negative Binomial regression parameters per gene
## Using 2000 genes, 5000 cells
## Found 111 outliers - those will be ignored in fitting/regularization step
## Second step: Get residuals using fitted parameters for 18709 genes
## Computing corrected count matrix for 18709 genes
## Calculating gene attributes
## Wall clock passed: Time difference of 23.52639 secs
## Determine variable features
## Place corrected count matrix in counts slot
## Set default assay to SCT
## Warning: The following features are not present in the object: MLF1IP, not
## searching for symbol synonyms
## Warning: The following features are not present in the object: FAM64A, HN1, not
## searching for symbol synonyms

4. Normalize data

# Apply SCTransform
L2 <- SCTransform(L2, vars.to.regress = c("percent.rb","percent.mito", "CC.Difference"), 
                  do.scale=TRUE, 
                  do.center=TRUE, 
                  verbose = TRUE)
## Running SCTransform on assay: RNA
## vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
## Calculating cell attributes from input UMI matrix: log_umi
## Variance stabilizing transformation of count matrix of size 18709 by 5935
## Model formula is y ~ log_umi
## Get Negative Binomial regression parameters per gene
## Using 2000 genes, 5000 cells
## Found 111 outliers - those will be ignored in fitting/regularization step
## Second step: Get residuals using fitted parameters for 18709 genes
## Computing corrected count matrix for 18709 genes
## Calculating gene attributes
## Wall clock passed: Time difference of 19.68775 secs
## Determine variable features
## Regressing out percent.rb, percent.mito, CC.Difference
## Centering and scaling data matrix
## Place corrected count matrix in counts slot
## Set default assay to SCT

5. Perform PCA

Variables_genes <- L2@assays$SCT@var.features

# Exclude genes starting with "HLA-" AND "Xist" AND "TRBV, TRAV"
Variables_genes_after_exclusion <- Variables_genes[!grepl("^HLA-|^XIST|^TRBV|^TRAV", Variables_genes)]


# These are now standard steps in the Seurat workflow for visualization and clustering
L2 <- RunPCA(L2,
             features = Variables_genes_after_exclusion,
             do.print = TRUE, 
             pcs.print = 1:5, 
             genes.print = 15,
             npcs = 50)
## PC_ 1 
## Positive:  ESYT2, MALAT1, ARHGAP15, PDE3B, IKZF2, SYNE2, KCNQ5, NEAT1, CAMK4, DOCK10 
##     EXOC4, WWOX, MT-ND6, TOX, RCSD1, TOP2A, SOS1, PTPRC, RAD51B, LRBA 
##     ELMO1, OSBPL3, SPTBN1, ATXN1, MACF1, PRKCH, ADD3, RABGAP1L, UTRN, MBNL1 
## Negative:  ACTB, HSPE1, NME1, RAN, CLIC1, GAPDH, PRELID1, CYCS, RPL35, ENO1 
##     XCL1, UBE2S, ACTG1, HSPD1, LDHA, PPP1R14B, SRM, NPM1, MRPL41, SNRPG 
##     NDUFAB1, PFDN2, KRT86, TMSB4X, TUBA1B, CHCHD2, JPT1, KRT81, TPI1, SNRPD1 
## PC_ 2 
## Positive:  TMSB10, S100A10, S100A6, B2M, EMP3, CRIP1, S100A4, S100A11, KLF2, TLE5 
##     CD52, LGALS1, LTB, PFN1, CYBA, PLP2, ANXA5, AHNAK, GABARAP, ANXA2 
##     RAP1GAP2, CALM1, PIM1, CD9, PLSCR3, CNN2, IFITM1, RIPOR2, SIGIRR, SLC9A3R1 
## Negative:  XCL1, XCL2, CST7, CRIM1, KSR2, HSPD1, PRKCH, PDE7B, SRGN, DPF3 
##     HDAC9, ADAMTS17, GRK3, ASB2, NPM1, SRM, CD3D, PEBP1, AHI1, KCNQ5 
##     PLPP1, RERE, GFOD1, MT-ND2, CD96, UBE2F, NME1, SRRT, MIR181A1HG, GCSH 
## PC_ 3 
## Positive:  HIST1H4C, DUT, PCLAF, WDR34, HIST1H1A, HIST1H1D, HIST1H3D, STMN1, HIST1H1C, TYMS 
##     H2AFX, SIVA1, HIST1H1E, TK1, HIST1H1B, H2AFZ, HIST1H2AH, HIST2H2AC, RRM2, TUBB 
##     GAPDH, KLRC1, GABARAP, NUSAP1, GPX4, WWOX, HIST1H3B, GSTP1, COX5B, CARHSP1 
## Negative:  LTB, EIF4G2, CALR, HSPA8, CCND2, CDC20, HSP90AB1, EMP3, ARL6IP1, FLNA 
##     HNRNPU, CCT5, S100A6, CCNB1, TMSB10, YWHAZ, FLT3LG, B2M, MYH9, VIM 
##     HSPA5, EIF3B, HNRNPA2B1, HDGF, YWHAG, PLEC, HNRNPA3, HNRNPH1, CAPRIN1, RIPOR2 
## PC_ 4 
## Positive:  C1QBP, EIF5A, PSMB6, PFN1, YWHAE, RPL26, RNASEK, SNHG29, RPA1, RPL31 
##     GLOD4, UBB, PRPF8, EIF4A1, NAA38, GABARAP, UBE2G1, CAPG, TXNDC17, ACADVL 
##     GSTP1, TRAPPC1, EMC6, ITGAE, ARRB2, RABEP1, PITPNA, ACAP1, PAFAH1B1, RANGRF 
## Negative:  HIST1H4C, TUBB, HIST1H1E, RPS4X, HIST1H1B, HIST1H1C, RRM2, HIST1H1D, H2AFX, MKI67 
##     HIST1H2AH, HIST1H3D, HIST1H1A, RNF212, CXCR3, MAL, DUT, HIST1H3B, CORO1B, TYMS 
##     HIST2H2AC, MRPS5, HIST1H2AL, TUBA1B, UBE2C, UBC, TOP2A, CD74, HIST1H3C, ALYREF 
## PC_ 5 
## Positive:  ARL6IP1, RPS4X, PTTG1, SEPTIN6, CORO1B, IL2RG, MIF, SOD1, MT1E, SYT1 
##     CD3D, RNF212, PTMA, OAZ1, PDE7B, TXN, PPIA, CHCHD2, MT1G, ATP5MF 
##     DAD1, BTG1, IL32, ATP5MC3, FOXP2, LITAF, CDC20, HMGB2, SDCBP, MYL6 
## Negative:  TSR1, C1QBP, PFN1, HIST1H1E, PRPF8, MYBBP1A, HIST1H1B, RPL26, HNRNPU, NCL 
##     YWHAE, HIST1H1C, EIF4A1, RPL31, MYH9, EIF5A, HIST1H2AH, SMC1A, TXNDC17, ACAP1 
##     PELP1, CTDNEP1, EIF2S3, EMC6, HSPA9, RPA1, HIST1H4C, IPO7, COPS3, LRPPRC
# determine dimensionality of the data
ElbowPlot(L2, ndims =50)

# Perform PCA TEST

library(ggplot2)
library(RColorBrewer)  

# Assuming you have 10 different cell lines, generating a color palette with 10 colors
cell_line_colors <- brewer.pal(10, "Set3")

# Assuming L2$cell_line is a factor or character vector containing cell line names
data <- as.data.frame(table(L2$cell_line))
colnames(data) <- c("cell_line", "nUMI")  # Change column name to nUMI

ncells <- ggplot(data, aes(x = cell_line, y = nUMI, fill = cell_line)) + 
  geom_col() +
  theme_classic() +
  geom_text(aes(label = nUMI), 
            position = position_dodge(width = 0.9), 
            vjust = -0.25) +
  scale_fill_manual(values = cell_line_colors) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5)) +  # Adjust the title position
  ggtitle("Filtered cells per sample") +
  xlab("Cell lines") +  # Adjust x-axis label
  ylab("Frequency")    # Adjust y-axis label

print(ncells)

# TEST-1
# given that the output of RunPCA is "pca"
# replace "so" by the name of your seurat object

pct <- L2[["pca"]]@stdev / sum(L2[["pca"]]@stdev) * 100
cumu <- cumsum(pct) # Calculate cumulative percents for each PC
# Determine the difference between variation of PC and subsequent PC
co2 <- sort(which((pct[-length(pct)] - pct[-1]) > 0.1), decreasing = T)[1] + 1
# last point where change of % of variation is more than 0.1%. -> co2
co2
## [1] 12
# TEST-2
# get significant PCs
stdv <- L2[["pca"]]@stdev
sum.stdv <- sum(L2[["pca"]]@stdev)
percent.stdv <- (stdv / sum.stdv) * 100
cumulative <- cumsum(percent.stdv)
co1 <- which(cumulative > 90 & percent.stdv < 5)[1]
co2 <- sort(which((percent.stdv[1:length(percent.stdv) - 1] - 
                       percent.stdv[2:length(percent.stdv)]) > 0.1), 
              decreasing = T)[1] + 1
min.pc <- min(co1, co2)
min.pc
## [1] 12
# Create a dataframe with values
plot_df <- data.frame(pct = percent.stdv, 
           cumu = cumulative, 
           rank = 1:length(percent.stdv))

# Elbow plot to visualize 
  ggplot(plot_df, aes(cumulative, percent.stdv, label = rank, color = rank > min.pc)) + 
  geom_text() + 
  geom_vline(xintercept = 90, color = "grey") + 
  geom_hline(yintercept = min(percent.stdv[percent.stdv > 5]), color = "grey") +
  theme_bw()

6. Clustering

L2 <- FindNeighbors(L2, 
                    dims = 1:min.pc, 
                    verbose = FALSE)

# understanding resolution
L2 <- FindClusters(L2, 
                  resolution = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 
                                0.7,0.8, 0.9, 1, 1.1, 1.2))
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9000
## Number of communities: 1
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.8363
## Number of communities: 2
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7966
## Number of communities: 3
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7689
## Number of communities: 4
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7498
## Number of communities: 5
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7296
## Number of communities: 7
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7145
## Number of communities: 8
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.7002
## Number of communities: 8
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.6857
## Number of communities: 10
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.6744
## Number of communities: 11
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.6628
## Number of communities: 12
## Elapsed time: 0 seconds
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5935
## Number of edges: 180434
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.6522
## Number of communities: 12
## Elapsed time: 0 seconds
# non-linear dimensionality reduction --------------
L2 <- RunUMAP(L2, 
              dims = 1:min.pc,
              verbose = FALSE)
## Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the R-native UWOT using the cosine metric
## To use Python UMAP via reticulate, set umap.method to 'umap-learn' and metric to 'correlation'
## This message will be shown once per session
# note that you can set `label = TRUE` or use the Label Clusters function to help label
# individual clusters
DimPlot(L2,group.by = "cell_line", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.1", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.2", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.3", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.4", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.5", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.6", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.7", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.8", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.0.9", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.1", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.1.1", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2,
        group.by = "SCT_snn_res.1.2", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

7. Azimuth Annotation

InstallData("pbmcref")
## Warning: The following packages are already installed and will not be
## reinstalled: pbmcref
# The RunAzimuth function can take a Seurat object as input
L2 <- RunAzimuth(L2, reference = "pbmcref")
## Warning: Overwriting miscellanous data for model
## Warning: Adding a dimensional reduction (refUMAP) without the associated assay
## being present
## Warning: Adding a dimensional reduction (refUMAP) without the associated assay
## being present
## detected inputs from HUMAN with id type Gene.name
## reference rownames detected HUMAN with id type Gene.name
## Normalizing query using reference SCT model
## Warning: 113 features of the features specified were not present in both the reference query assays. 
## Continuing with remaining 4887 features.
## Projecting cell embeddings
## Finding query neighbors
## Finding neighborhoods
## Finding anchors
##  Found 383 anchors
## Finding integration vectors
## Finding integration vector weights
## Predicting cell labels
## Predicting cell labels
## Warning: Feature names cannot have underscores ('_'), replacing with dashes
## ('-')
## Predicting cell labels
## Warning: Feature names cannot have underscores ('_'), replacing with dashes
## ('-')
## 
## Integrating dataset 2 with reference dataset
## Finding integration vectors
## Integrating data
## Warning: Keys should be one or more alphanumeric characters followed by an
## underscore, setting key from integrated_dr_ to integrateddr_
## Computing nearest neighbors
## Running UMAP projection
## Warning in RunUMAP.default(object = neighborlist, reduction.model =
## reduction.model, : Number of neighbors between query and reference is not equal
## to the number of neighbors within reference
## 16:34:45 Read 5935 rows
## 16:34:45 Processing block 1 of 1
## 16:34:45 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 20
## 16:34:45 Initializing by weighted average of neighbor coordinates using 1 thread
## 16:34:45 Commencing optimization for 67 epochs, with 118700 positive edges
## 16:34:46 Finished
## Warning: No assay specified, setting assay as RNA by default.
## Projecting reference PCA onto query
## Finding integration vector weights
## Projecting back the query cells into original PCA space
## Finding integration vector weights
## Computing scores:
##     Finding neighbors of original query cells
##     Finding neighbors of transformed query cells
##     Computing query SNN
##     Determining bandwidth and computing transition probabilities
## Total elapsed time: 2.35190320014954
UMAPPlot(L2, group.by = "predicted.celltype.l2", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

UMAPPlot(L2, group.by = "predicted.celltype.l2", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T)

8. Cell type annotation using ProjectTils

#Load reference atlas and query data
ref <- readRDS(file = "CD4T_human_ref_v1.rds")

#Run Projection algorithm
query.projected <- Run.ProjecTILs(L2, ref = ref)
##   |                                                                              |                                                                      |   0%[1] "Using assay SCT for query"
## Pre-filtering cells with scGate...
## 
## ### Detected a total of 5466 pure 'Target' cells (92.10% of total)
## [1] "469 out of 5935 ( 8% ) non-pure cells removed. Use filter.cells=FALSE to avoid pre-filtering"
## [1] "Aligning query to reference map for batch-correction..."
## Warning: Layer counts isn't present in the assay object[[assay]]; returning
## NULL
## Preparing PCA embeddings for objects...
## Warning: Number of dimensions changing from 50 to 20
## 
## Projecting corrected query onto Reference PCA space
## 
## Projecting corrected query onto Reference UMAP space
## Warning: Not all features provided are in this Assay object, removing the
## following feature(s): CXCL13, GNLY, CD177, CCL22, CGA, SOX4, G0S2, IL17A,
## IGFL2, PMCH, TRDC, AREG, IL21, HLA-DQA1, IL1RL1, IL1R2, EBI3, DIRAS3, NPPC,
## RGS2, CAVIN3, CD200, MS4A6A, IL2, BEX3, BASP1, PVALB, MRC1, CTLA4, TASL, CPM,
## LHFPL6, H1-4, IL26, HLA-DPA1, HLA-DPB1, H2AZ1, TENT5A, MSC, SH2D1A, RCAN2,
## GPR25, TNS3, ELAPOR1, IGFBP3, CCL3L3, POLR1F, BTLA, PTPN13, FCRL3, FANK1, PLK2,
## CRLF2, F5, IL1R1, AHSP, CCR2, TSHZ2, CSF2RB, CDCA7L, HTRA1, PPARG, H1-2, H1-0,
## LMCD1, WARS1, H1-3, H2BC11, GPX1, STAC, H2AC6, IRAG2, CYP7B1, H1-10, MYO7A,
## PTGIR, H3C10, NAP1L2, FAS, IL22, CHGB, SLC28A3, PDLIM4, ECEL1, HES1, HSD11B1,
## CPE, CD40, TNFSF11, CD86, RNF144B, H4C3, NEBL, HS3ST1
##   |                                                                              |======================================================================| 100%
## Creating slots functional.cluster and functional.cluster.conf in query object
#reference atlas
DimPlot(ref, label = T)

#Visualize projection
plot.projection(ref, query.projected, linesize = 0.5, pointsize = 0.5)

#Plot the predicted composition of the query in terms of reference T cell subtypes
plot.statepred.composition(ref, query.projected, metric = "Percent")

L2 <- ProjecTILs.classifier(query = L2, ref = ref)
##   |                                                                              |                                                                      |   0%[1] "Using assay SCT for query"
## Pre-filtering cells with scGate...
## 
## ### Detected a total of 5466 pure 'Target' cells (92.10% of total)
## [1] "469 out of 5935 ( 8% ) non-pure cells removed. Use filter.cells=FALSE to avoid pre-filtering"
## [1] "Aligning query to reference map for batch-correction..."
## Warning: Layer counts isn't present in the assay object[[assay]]; returning
## NULL
## Preparing PCA embeddings for objects...
## Warning: Number of dimensions changing from 50 to 20
## 
## Projecting corrected query onto Reference PCA space
## 
## Projecting corrected query onto Reference UMAP space
## Warning: Not all features provided are in this Assay object, removing the
## following feature(s): CXCL13, GNLY, CD177, CCL22, CGA, SOX4, G0S2, IL17A,
## IGFL2, PMCH, TRDC, AREG, IL21, HLA-DQA1, IL1RL1, IL1R2, EBI3, DIRAS3, NPPC,
## RGS2, CAVIN3, CD200, MS4A6A, IL2, BEX3, BASP1, PVALB, MRC1, CTLA4, TASL, CPM,
## LHFPL6, H1-4, IL26, HLA-DPA1, HLA-DPB1, H2AZ1, TENT5A, MSC, SH2D1A, RCAN2,
## GPR25, TNS3, ELAPOR1, IGFBP3, CCL3L3, POLR1F, BTLA, PTPN13, FCRL3, FANK1, PLK2,
## CRLF2, F5, IL1R1, AHSP, CCR2, TSHZ2, CSF2RB, CDCA7L, HTRA1, PPARG, H1-2, H1-0,
## LMCD1, WARS1, H1-3, H2BC11, GPX1, STAC, H2AC6, IRAG2, CYP7B1, H1-10, MYO7A,
## PTGIR, H3C10, NAP1L2, FAS, IL22, CHGB, SLC28A3, PDLIM4, ECEL1, HES1, HSD11B1,
## CPE, CD40, TNFSF11, CD86, RNF144B, H4C3, NEBL, HS3ST1
##   |                                                                              |======================================================================| 100%
## Creating slots functional.cluster and functional.cluster.conf in query object
UMAPPlot(L2, group.by = "functional.cluster", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T)

9.Cell type annotation using SingleR

#get reference datasets from celldex package

monaco.ref <- celldex::MonacoImmuneData()
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
hpca.ref <- celldex::HumanPrimaryCellAtlasData()
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
dice.ref <- celldex::DatabaseImmuneCellExpressionData()
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
bpe.ref <- celldex::BlueprintEncodeData()
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
## see ?celldex and browseVignettes('celldex') for documentation
## loading from cache
#convert our Seurat object to single cell experiment (SCE)
sce <- as.SingleCellExperiment(DietSeurat(L2))

#using SingleR
monaco.main <- SingleR(test = sce,assay.type.test = 1,ref = monaco.ref,labels = monaco.ref$label.main)
monaco.fine <- SingleR(test = sce,assay.type.test = 1,ref = monaco.ref,labels = monaco.ref$label.fine)
hpca.main <- SingleR(test = sce,assay.type.test = 1,ref = hpca.ref,labels = hpca.ref$label.main)
hpca.fine <- SingleR(test = sce,assay.type.test = 1,ref = hpca.ref,labels = hpca.ref$label.fine)
dice.main <- SingleR(test = sce,assay.type.test = 1,ref = dice.ref,labels = dice.ref$label.main)
dice.fine <- SingleR(test = sce,assay.type.test = 1,ref = dice.ref,labels = dice.ref$label.fine)
bpe.main <- SingleR(test = sce,assay.type.test = 1,ref = bpe.ref,labels = bpe.ref$label.main)
bpe.fine <- SingleR(test = sce,assay.type.test = 1,ref = bpe.ref,labels = bpe.ref$label.fine)


#summary of general cell type annotations

#table(monaco.main$pruned.labels)
#table(hpca.main$pruned.labels)
#table(dice.main$pruned.labels)
#table(bpe.main$pruned.labels)

#The finer cell types annotations are you after, the harder they are to get reliably. 
#This is where comparing many databases, as well as using individual markers from literature, 
#would all be very valuable.

#table(monaco.fine$pruned.labels)
#table(hpca.fine$pruned.labels)
#table(dice.fine$pruned.labels)
#table(bpe.fine$pruned.labels)



#add the annotations to the Seurat object metadata
L2@meta.data$monaco.main <- monaco.main$pruned.labels
L2@meta.data$monaco.fine <- monaco.fine$pruned.labels
#
L2@meta.data$hpca.main   <- hpca.main$pruned.labels
L2@meta.data$hpca.fine   <- hpca.fine$pruned.labels
#  
L2@meta.data$dice.main   <- dice.main$pruned.labels
L2@meta.data$dice.fine   <- dice.fine$pruned.labels
# 
L2@meta.data$bpe.main   <- bpe.main$pruned.labels
L2@meta.data$bpe.fine   <- bpe.fine$pruned.labels

# Monaco Annotations
DimPlot(L2, group.by = "monaco.main", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

DimPlot(L2, group.by = "monaco.main", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2, group.by = "monaco.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

DimPlot(L2, group.by = "monaco.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)
## Warning: ggrepel: 7 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

# HPCA Annotations
DimPlot(L2, group.by = "hpca.main", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2, group.by = "hpca.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

DimPlot(L2, group.by = "hpca.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

# DICE Annotations
DimPlot(L2, group.by = "dice.main", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2, group.by = "dice.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

DimPlot(L2, group.by = "dice.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

# BPE Annotations
DimPlot(L2, group.by = "bpe.main", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

DimPlot(L2, group.by = "bpe.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = F)

DimPlot(L2, group.by = "bpe.fine", 
        reduction = "umap",
        label.size = 3,
        repel = T,
        label = T, label.box = T)

10. clusTree

library(clustree)
## Loading required package: ggraph
## 
## Attaching package: 'ggraph'
## The following object is masked from 'package:sp':
## 
##     geometry
clustree(L2, prefix = "SCT_snn_res.")

11.Save the Seurat object as an Robj file