Loading required package: SeuratObject
Loading required package: sp
Attaching package: 'SeuratObject'
The following objects are masked from 'package:base':
intersect, t
── Installed datasets ──────────────────────────────── SeuratData v0.2.2.9001 ──
✔ pbmcref 1.0.0 ✔ pbmcsca 3.0.0
────────────────────────────────────── Key ─────────────────────────────────────
✔ Dataset loaded successfully
❯ Dataset built with a newer version of Seurat than installed
❓ Unknown version of Seurat installed
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ readr 2.1.5
✔ ggplot2 3.5.1 ✔ stringr 1.5.1
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Attaching package: 'magrittr'
The following object is masked from 'package:purrr':
set_names
The following object is masked from 'package:tidyr':
extract
Attaching package: 'dbplyr'
The following objects are masked from 'package:dplyr':
ident, sql
Registered S3 method overwritten by 'SeuratDisk':
method from
as.sparse.H5Group Seurat
Attaching shinyBS
Loading required package: ggraph
Attaching package: 'ggraph'
The following object is masked from 'package:sp':
geometry
#Load Seurat Object merged from cell lines and a control(PBMC) after filtration
load("../../../0-IMP-OBJECTS/All_Samples_Merged_with_10x_Azitmuth_Annotated.robj")
All_samples_Merged
An object of class Seurat
36752 features across 59355 samples within 5 assays
Active assay: RNA (36601 features, 0 variable features)
2 layers present: data, counts
4 other assays present: ADT, prediction.score.celltype.l1, prediction.score.celltype.l2, prediction.score.celltype.l3
2 dimensional reductions calculated: integrated_dr, ref.umap
# Load necessary libraries
library(Seurat)
# Display basic metadata summary
head(All_samples_Merged@meta.data)
# Check if columns such as `orig.ident`, `nCount_RNA`, `nFeature_RNA`, `nUMI`, `ngene`, and any other necessary columns exist
required_columns <- c("orig.ident", "nCount_RNA", "nFeature_RNA", "nUMI", "ngene")
missing_columns <- setdiff(required_columns, colnames(All_samples_Merged@meta.data))
if (length(missing_columns) > 0) {
cat("Missing columns:", paste(missing_columns, collapse = ", "), "\n")
} else {
cat("All required columns are present.\n")
}
All required columns are present.
# Check cell counts and features
cat("Number of cells:", ncol(All_samples_Merged), "\n")
Number of cells: 59355
cat("Number of features:", nrow(All_samples_Merged), "\n")
Number of features: 36601
# Verify that each `orig.ident` label has the correct number of cells
cat("Cell counts per group:\n")
Cell counts per group:
print(table(All_samples_Merged$orig.ident))
L1 L2 L3 L4 L5 L6 L7 PBMC PBMC10x
5825 5935 6428 6150 6022 5148 5331 8354 10162
# Check that the cell IDs are unique (which ensures no issues from merging)
if (any(duplicated(colnames(All_samples_Merged)))) {
cat("Warning: There are duplicated cell IDs.\n")
} else {
cat("Cell IDs are unique.\n")
}
Cell IDs are unique.
# Check the assay consistency for RNA
DefaultAssay(All_samples_Merged) <- "RNA"
# Check dimensions of the RNA counts layer using the new method
cat("Dimensions of the RNA counts layer:", dim(GetAssayData(All_samples_Merged, layer = "counts")), "\n")
Dimensions of the RNA counts layer: 36601 59355
cat("Dimensions of the RNA data layer:", dim(GetAssayData(All_samples_Merged, layer = "data")), "\n")
Dimensions of the RNA data layer: 36601 59355
# Check the ADT assay (optional)
if ("ADT" %in% names(All_samples_Merged@assays)) {
cat("ADT assay is present.\n")
cat("Dimensions of the ADT counts layer:", dim(GetAssayData(All_samples_Merged, assay = "ADT", layer = "counts")), "\n")
} else {
cat("ADT assay is not present.\n")
}
ADT assay is present.
Dimensions of the ADT counts layer: 56 59355
# InstallData("pbmcref")
#
# # The RunAzimuth function can take a Seurat object as input
# All_samples_Merged <- RunAzimuth(All_samples_Merged, reference = "pbmcref")
# Remove the percent.mito column
All_samples_Merged$percent.mito <- NULL
# Set identity classes to an existing column in meta data
Idents(object = All_samples_Merged) <- "cell_line"
All_samples_Merged[["percent.rb"]] <- PercentageFeatureSet(All_samples_Merged,
pattern = "^RP[SL]")
# Convert 'percent.mt' to numeric, replacing "NaN" with 0
All_samples_Merged$percent.rb <- replace(as.numeric(All_samples_Merged$percent.rb), is.na(All_samples_Merged$percent.rb), 0)
# The [[ operator can add columns to object metadata. This is a great place to stash QC stats
All_samples_Merged[["percent.mt"]] <- PercentageFeatureSet(All_samples_Merged, pattern = "^MT-")
# Convert 'percent.mt' to numeric, replacing "NaN" with 0
All_samples_Merged$percent.mt <- replace(as.numeric(All_samples_Merged$percent.mt), is.na(All_samples_Merged$percent.mt), 0)
VlnPlot(All_samples_Merged, features = c("nFeature_RNA",
"nCount_RNA",
"percent.mt",
"percent.rb"),
ncol = 4, pt.size = 0.1) &
theme(plot.title = element_text(size=10))
FeatureScatter(All_samples_Merged, feature1 = "percent.mt",
feature2 = "percent.rb")
VlnPlot(All_samples_Merged, features = c("nFeature_RNA",
"nCount_RNA",
"percent.mt"),
ncol = 3)
FeatureScatter(All_samples_Merged,
feature1 = "percent.mt",
feature2 = "percent.rb") +
geom_smooth(method = 'lm')
`geom_smooth()` using formula = 'y ~ x'
FeatureScatter(All_samples_Merged,
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(All_samples_Merged,
feature1 = "nCount_RNA",
feature2 = "percent.mt")+
geom_smooth(method = 'lm')
`geom_smooth()` using formula = 'y ~ x'
FeatureScatter(All_samples_Merged,
feature1 = "nCount_RNA",
feature2 = "nFeature_RNA")+
geom_smooth(method = 'lm')
`geom_smooth()` using formula = 'y ~ x'
Running SCTransform on assay: RNA
Running SCTransform on layer: counts
vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
Variance stabilizing transformation of count matrix of size 27417 by 59355
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 5000 cells
Found 453 outliers - those will be ignored in fitting/regularization step
Second step: Get residuals using fitted parameters for 27417 genes
Computing corrected count matrix for 27417 genes
Calculating gene attributes
Wall clock passed: Time difference of 10.1647 mins
Determine variable features
Getting residuals for block 1(of 12) for counts dataset
Getting residuals for block 2(of 12) for counts dataset
Getting residuals for block 3(of 12) for counts dataset
Getting residuals for block 4(of 12) for counts dataset
Getting residuals for block 5(of 12) for counts dataset
Getting residuals for block 6(of 12) for counts dataset
Getting residuals for block 7(of 12) for counts dataset
Getting residuals for block 8(of 12) for counts dataset
Getting residuals for block 9(of 12) for counts dataset
Getting residuals for block 10(of 12) for counts dataset
Getting residuals for block 11(of 12) for counts dataset
Getting residuals for block 12(of 12) for counts dataset
Finished calculating residuals for counts
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
# Apply SCTransform
All_samples_Merged <- SCTransform(All_samples_Merged,
vars.to.regress = c("percent.rb","percent.mt", "CC.Difference"),
do.scale=TRUE,
do.center=TRUE,
verbose = TRUE)
Running SCTransform on assay: RNA
Running SCTransform on layer: counts
vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
Variance stabilizing transformation of count matrix of size 27417 by 59355
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 5000 cells
Found 453 outliers - those will be ignored in fitting/regularization step
Second step: Get residuals using fitted parameters for 27417 genes
Computing corrected count matrix for 27417 genes
Calculating gene attributes
Wall clock passed: Time difference of 8.202876 mins
Determine variable features
Regressing out percent.rb, percent.mt, CC.Difference
Centering and scaling data matrix
Getting residuals for block 1(of 12) for counts dataset
Getting residuals for block 2(of 12) for counts dataset
Getting residuals for block 3(of 12) for counts dataset
Getting residuals for block 4(of 12) for counts dataset
Getting residuals for block 5(of 12) for counts dataset
Getting residuals for block 6(of 12) for counts dataset
Getting residuals for block 7(of 12) for counts dataset
Getting residuals for block 8(of 12) for counts dataset
Getting residuals for block 9(of 12) for counts dataset
Getting residuals for block 10(of 12) for counts dataset
Getting residuals for block 11(of 12) for counts dataset
Getting residuals for block 12(of 12) for counts dataset
Regressing out percent.rb, percent.mt, CC.Difference
Centering and scaling data matrix
Finished calculating residuals for counts
Set default assay to SCT
Variables_genes <- All_samples_Merged@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
All_samples_Merged <- RunPCA(All_samples_Merged,
features = Variables_genes_after_exclusion,
do.print = TRUE,
pcs.print = 1:5,
genes.print = 15,
npcs = 50)
PC_ 1
Positive: PPIA, RAN, TUBA1B, H2AFZ, NPM1, PRDX1, TPI1, HSPD1, MIF, ATP5F1B
COX5A, GAPDH, HSP90AB1, NME2, PRELID1, UBE2S, VDAC1, TUBB, ATP5MC3, RANBP1
NME1, HMGA1, CHCHD2, CYC1, ENO1, SLC25A5, HSPE1, JPT1, SNRPD1, RPS2
Negative: TYROBP, S100A9, S100A8, LYZ, CTSS, DPYD, VCAN, CYBB, PSAP, SPI1
CST3, FCN1, ZEB2, FOS, GABARAP, AIF1, FCER1G, RNF130, PLXDC2, PTPRE
ARHGAP26, HCK, AKAP13, FGR, LYN, LRMDA, PFDN5, MNDA, RPL39, SYK
PC_ 2
Positive: FCN1, LYZ, S100A9, CST3, VCAN, S100A8, NME2, MNDA, PLXDC2, RPS2
FOS, IFI30, LRMDA, CTSS, CD36, GRN, CSF3R, RPS4X, TSPO, AIF1
CEBPD, SERPINA1, TYROBP, MS4A6A, LRP1, S100A12, RPLP0, CSTA, FPR1, LST1
Negative: B2M, SARAF, MALAT1, RPS27, TCF7, EVL, RPS29, LINC00861, TRBC2, ETS1
BTG1, RPS4Y1, PCED1B-AS1, LBH, CD3E, ITK, IL7R, PIK3IP1, ABLIM1, STK17A
TLE5, CD247, CLEC2D, LEF1, FCMR, GIMAP5, SELL, RPL30, PTPRC, RPL34
PC_ 3
Positive: SEC11C, HDGFL3, YBX3, IL2RA, FAM107B, KRT7, CTSH, RPL30, MTHFD2, MINDY3
PPDPF, RAD21, EGFL6, HINT2, HTATIP2, BATF3, NCF4, ATP8B4, ELL2, TNFRSF4
EPB41L2, MIIP, CD74, TNFRSF11A, SPATS2L, RDH10, SYT4, RBM17, CD58, TIGIT
Negative: KIR3DL1, KIR3DL2, EPCAM, KIR2DL3, TRGV2, CST7, DAD1, XCL1, MATK, RAB25
RPL27A, CD7, ESYT2, KLRC1, KIR2DL4, GZMM, CXCR3, PFN1, C1QBP, RCBTB2
KRT86, XCL2, TRGV4, SRRT, EIF4A1, MYO1E, ZBTB16, RPS15, KRT81, TRGC2
PC_ 4
Positive: C12orf75, HACD1, EGFL6, TNFRSF4, LY6E, TIGIT, BACE2, SYT4, PTP4A3, GGH
ARPC2, NET1, CCL17, PARK7, PXYLP1, ATP5MC1, CYBA, GRIA4, ADGRB3, UBE2D2
GYPC, MAP1B, PLEKHH2, PLPP1, ONECUT2, ENO1, FAM216A, ACTN1, DBN1, ACAA2
Negative: PAGE5, LMNA, RPL35A, ANXA5, RBPMS, CDKN2A, NDUFV2, RPL22L1, TENM3, GPX4
MSC-AS1, KIF2A, CD74, PLD1, TALDO1, ANXA2, SLC7A11, NEURL1, FAM241A, MT2A
STAT1, PPP2R2B, FAM50B, LGALS3, PSMB9, SPOCK1, IQCG, PPBP, RPS3A, EEF2
PC_ 5
Positive: CXCL8, SOD2, C15orf48, DOCK4, KYNU, THBS1, EREG, SLC7A11, MMP9, AC025580.2
SDC2, MMP14, CXCL5, FTH1, CXCL3, GLIS3, CXCL1, SERPINB2, IL1B, CXCL16
CTSL, VMO1, RAB13, CYP27A1, MARCKS, NRP1, ABCA1, CXCL2, NINJ1, EPB41L3
Negative: PAGE5, RPL35A, MNDA, FCN1, NDUFV2, TSPO, RPS14, RBPMS, FOS, CSF3R
C1orf162, CST3, LYST, STMN1, CD36, JAML, MS4A6A, TMSB4X, KIF2A, AC007952.4
RPL11, CD302, CDKN2A, PSMB2, BLVRB, PSMB9, TENM3, RPS3A, PRAM1, VCAN
# determine dimensionality of the data
ElbowPlot(All_samples_Merged, ndims = 50)
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 All_samples_Merged$cell_line is a factor or character vector containing cell line names
data <- as.data.frame(table(All_samples_Merged$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 <- All_samples_Merged[["pca"]]@stdev / sum(All_samples_Merged[["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] 22
# TEST-2
# get significant PCs
stdv <- All_samples_Merged[["pca"]]@stdev
sum.stdv <- sum(All_samples_Merged[["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] 22
# 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()
All_samples_Merged <- FindNeighbors(All_samples_Merged,
dims = 1:22,
verbose = FALSE)
# understanding resolution
All_samples_Merged <- FindClusters(All_samples_Merged,
resolution = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,0.8, 0.9, 1,1.2,1.5,2))
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9889
Number of communities: 14
Elapsed time: 31 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9804
Number of communities: 18
Elapsed time: 30 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9723
Number of communities: 19
Elapsed time: 26 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9643
Number of communities: 20
Elapsed time: 27 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9568
Number of communities: 23
Elapsed time: 23 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9497
Number of communities: 25
Elapsed time: 27 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9425
Number of communities: 25
Elapsed time: 39 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9360
Number of communities: 29
Elapsed time: 29 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9298
Number of communities: 30
Elapsed time: 25 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9236
Number of communities: 32
Elapsed time: 28 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9125
Number of communities: 37
Elapsed time: 24 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9003
Number of communities: 39
Elapsed time: 33 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1966749
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8815
Number of communities: 44
Elapsed time: 35 seconds
# non-linear dimensionality reduction --------------
All_samples_Merged <- RunUMAP(All_samples_Merged,
dims = 1:22,
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(All_samples_Merged,group.by = "cell_line",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,group.by = "predicted.celltype.l2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
Warning: ggrepel: 3 unlabeled data points (too many overlaps). Consider
increasing max.overlaps
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.1",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.3",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.4",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.5",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.6",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.7",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.8",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.0.9",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.1",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.1.2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.1.5",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged,
group.by = "SCT_snn_res.2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
# Set identity classes to an existing column in meta data
Idents(object = All_samples_Merged) <- "SCT_snn_res.0.7"
cluster_table <- table(Idents(All_samples_Merged))
barplot(cluster_table, main = "Number of Cells in Each Cluster",
xlab = "Cluster",
ylab = "Number of Cells",
col = rainbow(length(cluster_table)))
print(cluster_table)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
6391 5922 5827 5578 5518 4951 4629 4057 3272 3232 2030 1767 1373 945 925 821
16 17 18 19 20 21 22 23 24
429 423 395 308 253 123 90 61 35
table(All_samples_Merged$predicted.celltype.l2, All_samples_Merged$SCT_snn_res.0.2)
0 1 2 3 4 5 6 7 8 9 10 11
ASDC 0 0 0 0 0 0 0 0 0 0 0 0
B intermediate 0 1 0 0 0 0 3 0 1 0 0 490
B memory 9 0 2 0 0 117 88 1 31 0 3 198
B naive 0 1 0 0 0 0 0 0 0 2 0 502
CD14 Mono 0 3 2 0 0 7 0 0 4 3025 0 7
CD16 Mono 0 0 0 0 0 0 0 0 0 123 0 1
CD4 CTL 0 16 0 0 0 0 0 0 0 1 0 0
CD4 Naive 0 522 0 0 0 0 0 1480 0 1 0 7
CD4 Proliferating 5449 0 5391 2849 2462 4173 4044 5 3270 0 1362 3
CD4 TCM 870 4487 523 266 3322 486 607 1843 115 20 38 95
CD4 TEM 0 68 0 0 1 0 0 25 0 0 0 0
CD8 Naive 0 351 0 0 0 0 0 1003 0 2 0 2
CD8 Proliferating 0 0 0 0 0 1 1 0 0 0 0 0
CD8 TCM 0 283 0 16 1 0 0 173 0 0 0 2
CD8 TEM 0 179 0 6 1 1 3 184 2 1 0 1
cDC1 0 0 0 0 0 0 6 0 2 13 0 21
cDC2 0 0 2 0 0 36 4 0 9 124 0 53
dnT 0 32 1 0 1 3 4 16 3 1 0 6
gdT 0 27 0 0 0 0 0 66 0 0 0 0
HSPC 55 0 1 0 0 493 215 0 677 0 354 39
ILC 0 1 0 0 0 0 1 3 0 0 0 2
MAIT 0 15 0 0 0 0 0 223 0 2 0 0
NK 0 91 0 0 0 0 0 17 0 9 0 0
NK Proliferating 6 0 23 2785 38 38 262 1 10 0 1 1
NK_CD56bright 0 1 0 0 0 0 0 6 0 0 0 0
pDC 0 0 0 0 0 0 0 0 0 0 0 0
Plasmablast 0 0 0 0 0 0 0 0 0 0 0 18
Platelet 0 0 0 0 0 0 0 0 0 0 0 0
Treg 2 206 2 0 1 0 25 92 0 1 0 15
12 13 14 15 16 17
ASDC 0 0 0 0 3 0
B intermediate 184 17 0 0 0 0
B memory 70 4 0 0 0 0
B naive 687 0 0 0 0 0
CD14 Mono 11 758 0 0 2 4
CD16 Mono 0 2 0 0 0 0
CD4 CTL 0 0 0 0 0 0
CD4 Naive 0 0 0 33 0 0
CD4 Proliferating 0 0 0 3 0 0
CD4 TCM 2 35 0 169 0 0
CD4 TEM 0 0 0 0 0 0
CD8 Naive 0 1 0 14 0 0
CD8 Proliferating 0 0 0 0 0 0
CD8 TCM 0 0 0 2 0 0
CD8 TEM 0 0 10 3 0 0
cDC1 0 0 0 0 0 0
cDC2 0 1 0 0 0 0
dnT 0 2 0 13 0 0
gdT 0 0 0 0 0 0
HSPC 0 0 0 0 0 0
ILC 0 0 0 0 0 0
MAIT 0 0 0 2 0 0
NK 2 0 413 2 0 0
NK Proliferating 0 0 2 0 0 0
NK_CD56bright 0 0 7 2 0 0
pDC 0 0 0 0 56 0
Plasmablast 1 0 0 0 0 0
Platelet 0 1 0 0 0 31
Treg 0 0 0 9 0 0
clustree(All_samples_Merged, prefix = "SCT_snn_res.")
# InstallData("pbmcref")
#
# # The RunAzimuth function can take a Seurat object as input
# All_samples_Merged <- RunAzimuth(All_samples_Merged, reference = "pbmcref")
DimPlot(All_samples_Merged, group.by = "predicted.celltype.l1",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
DimPlot(All_samples_Merged, group.by = "predicted.celltype.l1",
reduction = "umap",
label.size = 3,
repel = T,
label = F)
DimPlot(All_samples_Merged, group.by = "predicted.celltype.l2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
Warning: ggrepel: 3 unlabeled data points (too many overlaps). Consider
increasing max.overlaps
DimPlot(All_samples_Merged, group.by = "predicted.celltype.l2",
reduction = "umap",
label.size = 3,
repel = T,
label = F)
DimPlot(All_samples_Merged, group.by = "predicted.celltype.l2",
reduction = "umap",
label.size = 3,
repel = T,
label = T, label.box = T)
Warning: ggrepel: 3 unlabeled data points (too many overlaps). Consider
increasing max.overlaps
table(All_samples_Merged$predicted.celltype.l2, All_samples_Merged$SCT_snn_res.0.2)
0 1 2 3 4 5 6 7 8 9 10 11
ASDC 0 0 0 0 0 0 0 0 0 0 0 0
B intermediate 0 1 0 0 0 0 3 0 1 0 0 490
B memory 9 0 2 0 0 117 88 1 31 0 3 198
B naive 0 1 0 0 0 0 0 0 0 2 0 502
CD14 Mono 0 3 2 0 0 7 0 0 4 3025 0 7
CD16 Mono 0 0 0 0 0 0 0 0 0 123 0 1
CD4 CTL 0 16 0 0 0 0 0 0 0 1 0 0
CD4 Naive 0 522 0 0 0 0 0 1480 0 1 0 7
CD4 Proliferating 5449 0 5391 2849 2462 4173 4044 5 3270 0 1362 3
CD4 TCM 870 4487 523 266 3322 486 607 1843 115 20 38 95
CD4 TEM 0 68 0 0 1 0 0 25 0 0 0 0
CD8 Naive 0 351 0 0 0 0 0 1003 0 2 0 2
CD8 Proliferating 0 0 0 0 0 1 1 0 0 0 0 0
CD8 TCM 0 283 0 16 1 0 0 173 0 0 0 2
CD8 TEM 0 179 0 6 1 1 3 184 2 1 0 1
cDC1 0 0 0 0 0 0 6 0 2 13 0 21
cDC2 0 0 2 0 0 36 4 0 9 124 0 53
dnT 0 32 1 0 1 3 4 16 3 1 0 6
gdT 0 27 0 0 0 0 0 66 0 0 0 0
HSPC 55 0 1 0 0 493 215 0 677 0 354 39
ILC 0 1 0 0 0 0 1 3 0 0 0 2
MAIT 0 15 0 0 0 0 0 223 0 2 0 0
NK 0 91 0 0 0 0 0 17 0 9 0 0
NK Proliferating 6 0 23 2785 38 38 262 1 10 0 1 1
NK_CD56bright 0 1 0 0 0 0 0 6 0 0 0 0
pDC 0 0 0 0 0 0 0 0 0 0 0 0
Plasmablast 0 0 0 0 0 0 0 0 0 0 0 18
Platelet 0 0 0 0 0 0 0 0 0 0 0 0
Treg 2 206 2 0 1 0 25 92 0 1 0 15
12 13 14 15 16 17
ASDC 0 0 0 0 3 0
B intermediate 184 17 0 0 0 0
B memory 70 4 0 0 0 0
B naive 687 0 0 0 0 0
CD14 Mono 11 758 0 0 2 4
CD16 Mono 0 2 0 0 0 0
CD4 CTL 0 0 0 0 0 0
CD4 Naive 0 0 0 33 0 0
CD4 Proliferating 0 0 0 3 0 0
CD4 TCM 2 35 0 169 0 0
CD4 TEM 0 0 0 0 0 0
CD8 Naive 0 1 0 14 0 0
CD8 Proliferating 0 0 0 0 0 0
CD8 TCM 0 0 0 2 0 0
CD8 TEM 0 0 10 3 0 0
cDC1 0 0 0 0 0 0
cDC2 0 1 0 0 0 0
dnT 0 2 0 13 0 0
gdT 0 0 0 0 0 0
HSPC 0 0 0 0 0 0
ILC 0 0 0 0 0 0
MAIT 0 0 0 2 0 0
NK 2 0 413 2 0 0
NK Proliferating 0 0 2 0 0 0
NK_CD56bright 0 0 7 2 0 0
pDC 0 0 0 0 56 0
Plasmablast 1 0 0 0 0 0
Platelet 0 1 0 0 0 31
Treg 0 0 0 9 0 0
# Load required libraries
library(Seurat)
library(harmony)
Loading required package: Rcpp
library(ggplot2)
# Run Harmony, adjusting for batch effect using "cell_line" or another grouping variable
All_samples_Merged <- RunHarmony(
object = All_samples_Merged,
group.by.vars = "cell_line", # Replace with the metadata column specifying batch or cell line
dims.use = 1:22 # Use the same dimensions as PCA
)
Transposing data matrix
Initializing state using k-means centroids initialization
Harmony 1/10
Harmony 2/10
Harmony 3/10
Harmony converged after 3 iterations
# Check results in harmony embeddings
harmony_embeddings <- Embeddings(All_samples_Merged, reduction = "harmony")
head(harmony_embeddings)
harmony_1 harmony_2 harmony_3 harmony_4 harmony_5
L1_AAACCTGAGGGCTTCC-1 -9.1829758 1.963191 -1.918213 -2.6439503 5.2090589
L1_AAACCTGGTGCAGGTA-1 -2.0181314 -1.959992 -6.630212 -0.6095339 2.4711615
L1_AAACCTGGTTAAAGTG-1 2.9577096 -3.100137 -7.965495 1.3714850 -1.8113084
L1_AAACCTGTCAGGTAAA-1 5.5391691 2.079810 -3.930100 -2.6739791 2.9035729
L1_AAACCTGTCCCTGACT-1 -5.9452052 -2.490022 -1.093192 -2.3334026 5.3740374
L1_AAACCTGTCCTTCAAT-1 0.6440283 -3.066060 -6.719512 1.0522677 0.5062576
harmony_6 harmony_7 harmony_8 harmony_9 harmony_10
L1_AAACCTGAGGGCTTCC-1 1.942551 7.5160215 1.590007728 -1.6905939 2.25899415
L1_AAACCTGGTGCAGGTA-1 2.082405 9.2696845 0.170642540 -0.3150174 5.06578933
L1_AAACCTGGTTAAAGTG-1 2.089400 5.7529230 1.740593023 -2.0960111 0.60816445
L1_AAACCTGTCAGGTAAA-1 1.087311 0.5767162 5.328467887 0.8787689 -0.03566803
L1_AAACCTGTCCCTGACT-1 3.290703 2.9443345 1.647116518 -0.2437256 1.83756606
L1_AAACCTGTCCTTCAAT-1 1.894603 5.1840153 -0.004641828 -2.8198796 1.83086508
harmony_11 harmony_12 harmony_13 harmony_14 harmony_15
L1_AAACCTGAGGGCTTCC-1 -0.6705618 -15.821885 0.01758632 -1.0042047 -3.1817639
L1_AAACCTGGTGCAGGTA-1 0.6516908 -9.727728 -1.79718451 1.4884099 -0.3023961
L1_AAACCTGGTTAAAGTG-1 -0.2784375 -4.331778 -2.35365317 -1.8970786 4.4020402
L1_AAACCTGTCAGGTAAA-1 2.4349602 -7.389665 -1.43931257 0.1163356 -0.1042223
L1_AAACCTGTCCCTGACT-1 1.0582357 -16.896482 -0.33692269 -0.3218589 -3.5518742
L1_AAACCTGTCCTTCAAT-1 -0.7462167 -8.431887 -1.57728399 0.1007814 0.6195831
harmony_16 harmony_17 harmony_18 harmony_19 harmony_20
L1_AAACCTGAGGGCTTCC-1 6.1639263 -0.8775208 -1.0145176 0.5309521 -0.6053158
L1_AAACCTGGTGCAGGTA-1 2.7239952 0.5881924 -1.4079552 -1.3660352 0.5972989
L1_AAACCTGGTTAAAGTG-1 -0.3734147 4.2548894 2.6650620 1.4432665 -2.7793401
L1_AAACCTGTCAGGTAAA-1 0.9545364 0.8569197 1.0572318 -0.8463755 -0.7375956
L1_AAACCTGTCCCTGACT-1 4.6672031 -0.3968514 0.3944571 0.9812218 -1.3131569
L1_AAACCTGTCCTTCAAT-1 1.6501552 0.9518280 -0.1556663 0.5601416 -0.3584055
harmony_21 harmony_22
L1_AAACCTGAGGGCTTCC-1 -1.13059949 -1.275543
L1_AAACCTGGTGCAGGTA-1 -1.42605510 -1.701459
L1_AAACCTGGTTAAAGTG-1 3.66049873 -1.524969
L1_AAACCTGTCAGGTAAA-1 -0.06630109 -1.217809
L1_AAACCTGTCCCTGACT-1 -1.13165681 -1.400442
L1_AAACCTGTCCTTCAAT-1 -1.77846391 -1.537383
# Run UMAP on Harmony embeddings
All_samples_Merged <- RunUMAP(All_samples_Merged, reduction = "harmony", dims = 1:22)
23:01:44 UMAP embedding parameters a = 0.9922 b = 1.112
23:01:44 Read 59355 rows and found 22 numeric columns
23:01:44 Using Annoy for neighbor search, n_neighbors = 30
23:01:44 Building Annoy index with metric = cosine, n_trees = 50
0% 10 20 30 40 50 60 70 80 90 100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
23:01:50 Writing NN index file to temp file /tmp/RtmpZhrHJH/file17f7f1648c784
23:01:50 Searching Annoy index using 1 thread, search_k = 3000
23:02:11 Annoy recall = 100%
23:02:13 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
23:02:18 Initializing from normalized Laplacian + noise (using RSpectra)
23:02:25 Commencing optimization for 200 epochs, with 2552746 positive edges
23:03:40 Optimization finished
# Optionally, find neighbors and clusters (if you plan to do clustering analysis)
All_samples_Merged <- FindNeighbors(All_samples_Merged, reduction = "harmony", dims = 1:22)
Computing nearest neighbor graph
Computing SNN
All_samples_Merged <- FindClusters(All_samples_Merged, resolution = 0.5) # Adjust resolution as needed
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 59355
Number of edges: 1783585
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8996
Number of communities: 18
Elapsed time: 28 seconds
# Visualize UMAP
DimPlot(All_samples_Merged, reduction = "umap", group.by = "cell_line", label = TRUE, pt.size = 0.5) +
ggtitle("UMAP of Harmony-Integrated Data")
# Visualize UMAP with batch/cell line information
DimPlot(All_samples_Merged, reduction = "umap", group.by = "cell_line", label = TRUE, pt.size = 0.5) +
ggtitle("UMAP - Colored by Cell Line (After Harmony Integration)")
# Visualize UMAP with clusters
DimPlot(All_samples_Merged, reduction = "umap", group.by = "seurat_clusters", label = TRUE, pt.size = 0.5) +
ggtitle("UMAP - Clustered Data (After Harmony Integration)")
# Visualize specific cell types or other metadata
DimPlot(All_samples_Merged, reduction = "umap", group.by = "predicted.celltype.l2", label = TRUE, pt.size = 0.5) +
ggtitle("UMAP - Cell Types After Harmony Integration")
save(All_samples_Merged, file = "../../../0-IMP-OBJECTS/All_Samples_Merged_with_10x_Azitmuth_Annotated_SCT_HPC.robj")