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_Robj/SS_CD4_Tcells_Azimuth_Annotated_PBMC10x_excluding_nonCD4_cells_from_Control.robj")
All_samples_Merged <- seurat_filtered
# 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: 49515
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 5171 3505
# 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 49515
cat("Dimensions of the RNA data layer:", dim(GetAssayData(All_samples_Merged, layer = "data")), "\n")
Dimensions of the RNA data layer: 36601 49515
# 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 49515
# 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 26195 by 49515
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 5000 cells
Found 526 outliers - those will be ignored in fitting/regularization step
Second step: Get residuals using fitted parameters for 26195 genes
Computing corrected count matrix for 26195 genes
Calculating gene attributes
Wall clock passed: Time difference of 7.363764 mins
Determine variable features
Getting residuals for block 1(of 10) for counts dataset
Getting residuals for block 2(of 10) for counts dataset
Getting residuals for block 3(of 10) for counts dataset
Getting residuals for block 4(of 10) for counts dataset
Getting residuals for block 5(of 10) for counts dataset
Getting residuals for block 6(of 10) for counts dataset
Getting residuals for block 7(of 10) for counts dataset
Getting residuals for block 8(of 10) for counts dataset
Getting residuals for block 9(of 10) for counts dataset
Getting residuals for block 10(of 10) 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 26195 by 49515
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 5000 cells
Found 526 outliers - those will be ignored in fitting/regularization step
Second step: Get residuals using fitted parameters for 26195 genes
Computing corrected count matrix for 26195 genes
Calculating gene attributes
Wall clock passed: Time difference of 7.497466 mins
Determine variable features
Regressing out percent.rb, percent.mt, CC.Difference
Centering and scaling data matrix
Getting residuals for block 1(of 10) for counts dataset
Getting residuals for block 2(of 10) for counts dataset
Getting residuals for block 3(of 10) for counts dataset
Getting residuals for block 4(of 10) for counts dataset
Getting residuals for block 5(of 10) for counts dataset
Getting residuals for block 6(of 10) for counts dataset
Getting residuals for block 7(of 10) for counts dataset
Getting residuals for block 8(of 10) for counts dataset
Getting residuals for block 9(of 10) for counts dataset
Getting residuals for block 10(of 10) 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: MALAT1, RPS27, PTPRC, SARAF, RPS4Y1, TCF7, RPL39, RPS29, RPL34, FYB1
ITM2B, GIMAP7, PNRC1, LEF1, BTG1, B2M, FOXP1, GIMAP5, IL7R, EVL
CD52, RIPOR2, LINC00861, SELL, YPEL3, CD3E, PIK3IP1, ITK, ZFP36L2, ANKRD12
Negative: PPIA, NPM1, RAN, NME2, GAPDH, PRDX1, PRELID1, RPS2, TUBA1B, HSP90AB1
HSPD1, RPLP0, TPI1, HMGA1, VDAC1, H2AFZ, SRM, TXN, ATP5F1B, CYC1
UBE2S, ENO1, PPP1R14B, NME1, RANBP1, CCT8, JPT1, SNRPD1, MIF, HNRNPAB
PC_ 2
Positive: KIR3DL1, CST7, EPCAM, TRGV2, KIR2DL3, RPL27A, MATK, DAD1, KIR3DL2, XCL1
C1QBP, KLRC1, KIR2DL4, PFN1, MYO1E, RAB25, GZMM, CXCR3, CD7, NDUFA4
EIF4A1, ESYT2, ZBTB16, KLRK1, XCL2, KRT86, RCBTB2, SPINT2, PTPN6, TRGV4
Negative: RPL30, FAM107B, SEC11C, ELL2, CD74, LRRFIP1, MTHFD2, YBX3, VIM, RAD21
MT-ND3, SMAP2, CFLAR, TAP1, CD2, ANXA1, TMEM173, HDGFL3, IL2RA, CD70
AHNAK, HTATIP2, BATF3, MTLN, EEF1A1, CD58, C3orf14, RPL35A, RPL39, CCR7
PC_ 3
Positive: TNFRSF4, C12orf75, HACD1, EGFL6, TIGIT, BACE2, ARPC2, SYT4, NET1, PXYLP1
LY6E, GRIA4, GGH, PARK7, MAP1B, UBE2D2, CCL17, KRT7, PON2, ADGRB3
PTP4A3, ACTN1, SCCPDH, MCTP2, CYBA, EEF1A2, PLEKHH2, SMIM3, MSC, HDGFL3
Negative: PAGE5, NDUFV2, RPL35A, RBPMS, CDKN2A, RPL22L1, CD74, STMN1, KIF2A, LMNA
TENM3, PSMB2, PSMB9, ANXA2, GPX4, FAM50B, PPP2R2B, GAPDH, ANXA5, IFI27L2
RPL11, VAMP5, FAM241A, NEURL1, PPBP, SH3KBP1, RPS3A, PLD1, HEBP2, TYMS
PC_ 4
Positive: RPS4X, EGLN3, GAS5, WFDC1, KRT1, LINC02752, TTC29, TBX4, RPLP1, IL32
IFNGR1, AC069410.1, PLCB1, S100A11, TNS4, SP5, RPL13, FAM9C, SEMA4A, S100A4
IL4, NKG7, S100A6, LINC00469, VIM, PTGIS, HSPB1, CEBPD, VIPR2, NPTX1
Negative: EIF5A, HSPE1, ATP5MC3, ODC1, CHCHD10, PPID, MT-ND3, CYCS, PPBP, CYC1
RPL34, GCSH, FCER2, RPS4Y1, DNAJC12, FKBP4, FAM162A, GSTP1, CD7, FKBP11
HSPD1, RPS29, MTDH, RPL39, HSP90B1, TCF7, SET, PSMB6, SLC7A11-AS1, C1QBP
PC_ 5
Positive: TMSB4X, TMSB10, LGALS1, S100A11, COTL1, S100A4, TP73, LSP1, IFITM2, S100A6
TMEM163, GPAT3, TAGLN2, HOXC9, LIME1, GPAT2, DUSP4, GAS2L1, EEF1A2, TNFRSF18
IFITM1, QPRT, MIIP, RBM38, LAPTM5, CEP135, PGAM1, CRIP1, PXYLP1, PRDX5
Negative: CCL17, MIR155HG, MAP4K4, MYO1D, RXFP1, LRBA, CA10, CFI, CA2, RUNX1
PRKCA, THY1, FRMD4A, AL590550.1, EZH2, IGHE, IMMP2L, HS3ST1, NFIB, LTA
SNTB1, RANBP17, MGST3, AKAP12, SLC35F3, AC100801.1, DOCK10, CCL5, EPB41L2, ONECUT2
# 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] 16
# 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] 16
# 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:16,
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: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9871
Number of communities: 12
Elapsed time: 21 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9767
Number of communities: 12
Elapsed time: 15 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9665
Number of communities: 13
Elapsed time: 17 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9568
Number of communities: 15
Elapsed time: 15 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9476
Number of communities: 17
Elapsed time: 14 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9384
Number of communities: 17
Elapsed time: 17 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9305
Number of communities: 19
Elapsed time: 25 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9225
Number of communities: 19
Elapsed time: 16 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9155
Number of communities: 21
Elapsed time: 16 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.9086
Number of communities: 24
Elapsed time: 15 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8978
Number of communities: 28
Elapsed time: 14 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8835
Number of communities: 31
Elapsed time: 15 seconds
Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
Number of nodes: 49515
Number of edges: 1638661
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8632
Number of communities: 37
Elapsed time: 19 seconds
# non-linear dimensionality reduction --------------
All_samples_Merged <- RunUMAP(All_samples_Merged,
dims = 1:16,
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)
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
6255 5929 5421 4932 4674 3798 3343 3338 3299 2021 1895 1796 880 520 512 478
16 17 18
231 121 72
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
B intermediate 0 0 0 0 0 3 3 1 0 0 53 0
B memory 9 2 0 0 118 86 1 30 0 3 36 0
B naive 0 0 0 0 0 0 0 0 0 0 41 0
CD14 Mono 0 2 0 0 6 0 0 5 0 0 13 0
CD4 CTL 0 0 0 0 0 0 11 0 0 0 2 0
CD4 Naive 0 0 0 0 0 0 523 0 1480 0 9 30
CD4 Proliferating 5451 5386 2852 2461 4124 4061 1 3225 5 1436 9 0
CD4 TCM 879 523 267 3317 478 590 4548 109 1838 48 142 91
CD4 TEM 0 0 0 1 0 0 62 0 21 0 0 0
CD8 Proliferating 0 0 0 0 1 1 0 0 0 0 0 0
CD8 TCM 0 0 16 1 0 0 0 0 0 0 0 0
CD8 TEM 0 0 8 1 1 3 0 2 0 0 0 0
cDC1 0 0 0 0 0 6 0 2 0 0 0 0
cDC2 0 2 0 0 35 3 0 10 0 1 2 0
dnT 0 1 1 1 3 4 1 2 0 0 2 0
HSPC 57 1 0 0 489 213 0 672 0 363 10 0
ILC 0 0 0 0 0 0 0 0 0 0 1 0
NK 0 0 0 0 0 0 0 0 0 0 1 0
NK Proliferating 7 23 2785 38 34 263 0 10 0 1 0 0
Treg 15 1 0 1 0 14 2 0 0 0 12 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)
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)
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
B intermediate 0 0 0 0 0 3 3 1 0 0 53 0
B memory 9 2 0 0 118 86 1 30 0 3 36 0
B naive 0 0 0 0 0 0 0 0 0 0 41 0
CD14 Mono 0 2 0 0 6 0 0 5 0 0 13 0
CD4 CTL 0 0 0 0 0 0 11 0 0 0 2 0
CD4 Naive 0 0 0 0 0 0 523 0 1480 0 9 30
CD4 Proliferating 5451 5386 2852 2461 4124 4061 1 3225 5 1436 9 0
CD4 TCM 879 523 267 3317 478 590 4548 109 1838 48 142 91
CD4 TEM 0 0 0 1 0 0 62 0 21 0 0 0
CD8 Proliferating 0 0 0 0 1 1 0 0 0 0 0 0
CD8 TCM 0 0 16 1 0 0 0 0 0 0 0 0
CD8 TEM 0 0 8 1 1 3 0 2 0 0 0 0
cDC1 0 0 0 0 0 6 0 2 0 0 0 0
cDC2 0 2 0 0 35 3 0 10 0 1 2 0
dnT 0 1 1 1 3 4 1 2 0 0 2 0
HSPC 57 1 0 0 489 213 0 672 0 363 10 0
ILC 0 0 0 0 0 0 0 0 0 0 1 0
NK 0 0 0 0 0 0 0 0 0 0 1 0
NK Proliferating 7 23 2785 38 34 263 0 10 0 1 0 0
Treg 15 1 0 1 0 14 2 0 0 0 12 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
)
Transposing data matrix
Initializing state using k-means centroids initialization
Harmony 1/10
Harmony 2/10
Harmony converged after 2 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 -4.320627 -2.317685 2.9430139 5.34050099 0.03130615
L1_AAACCTGGTGCAGGTA-1 8.343182 6.478844 2.6105995 9.39039981 -3.07004428
L1_AAACCTGGTTAAAGTG-1 3.603586 13.274618 0.3506875 5.78084998 0.89811646
L1_AAACCTGTCAGGTAAA-1 -5.976624 5.294591 0.9692598 -0.01281093 2.77149172
L1_AAACCTGTCCCTGACT-1 -1.853064 -4.962650 5.2762370 2.83288208 -1.51401130
L1_AAACCTGTCCTTCAAT-1 9.413513 9.068538 2.0306801 5.67023711 -1.72402761
harmony_6 harmony_7 harmony_8 harmony_9 harmony_10
L1_AAACCTGAGGGCTTCC-1 -1.0619370 -3.0401360 1.6617955 1.4779144 5.8455255
L1_AAACCTGGTGCAGGTA-1 -6.9019951 -0.1736879 -0.6861296 -1.4546126 -0.4235130
L1_AAACCTGGTTAAAGTG-1 -2.6929608 -2.5491623 3.1370845 -4.7034440 -3.2186842
L1_AAACCTGTCAGGTAAA-1 -0.2708270 2.4308115 0.3683388 -2.4538382 -2.8856956
L1_AAACCTGTCCCTGACT-1 -0.1350112 -1.8530740 0.8914700 0.8419062 2.6451692
L1_AAACCTGTCCTTCAAT-1 -3.7985693 -4.6277750 1.0286100 -2.3638671 -0.6485828
harmony_11 harmony_12 harmony_13 harmony_14 harmony_15
L1_AAACCTGAGGGCTTCC-1 1.0017413 -1.07240037 -0.09287815 -3.3461357 0.41327209
L1_AAACCTGGTGCAGGTA-1 -1.4560726 -0.50744218 -0.40539901 0.6890528 -0.29686610
L1_AAACCTGGTTAAAGTG-1 3.2061339 2.97949032 1.99578213 2.8535416 -4.17291661
L1_AAACCTGTCAGGTAAA-1 0.1240572 2.92741419 1.18206172 1.0893541 0.14280006
L1_AAACCTGTCCCTGACT-1 1.3883640 -0.09287043 0.22326928 -1.3124155 1.54353486
L1_AAACCTGTCCTTCAAT-1 1.0359249 -0.88996335 -0.38246480 0.6706358 -0.04157207
harmony_16 harmony_17 harmony_18 harmony_19 harmony_20
L1_AAACCTGAGGGCTTCC-1 -1.19337618 1.5215119 0.6279281 1.900453 -0.0883689
L1_AAACCTGGTGCAGGTA-1 1.45113148 0.7866671 0.7856182 -2.024082 -2.7355006
L1_AAACCTGGTTAAAGTG-1 0.42522022 -3.0031691 -0.1688935 -1.567725 -1.8483972
L1_AAACCTGTCAGGTAAA-1 0.33855377 -0.3991149 0.4769769 -1.389909 2.7205370
L1_AAACCTGTCCCTGACT-1 -4.35654249 -3.7322188 -1.3062391 2.082070 0.3145153
L1_AAACCTGTCCTTCAAT-1 0.01396619 -0.5907626 0.7888971 -1.779261 -0.3159930
harmony_21 harmony_22 harmony_23 harmony_24 harmony_25
L1_AAACCTGAGGGCTTCC-1 1.87568301 -5.111657 4.87214463 -0.08246990 -2.9138479
L1_AAACCTGGTGCAGGTA-1 0.58698305 -3.532982 0.09029822 -0.01787307 -0.9451554
L1_AAACCTGGTTAAAGTG-1 1.86667594 -3.074572 0.26092542 -1.67363661 -2.5888576
L1_AAACCTGTCAGGTAAA-1 -0.07876724 0.452983 -1.76033322 0.36133588 0.4935879
L1_AAACCTGTCCCTGACT-1 3.78124704 -4.791863 1.58294121 -0.60992989 -0.8401014
L1_AAACCTGTCCTTCAAT-1 -0.00866252 -4.251814 2.91232755 2.54476559 0.4380775
harmony_26 harmony_27 harmony_28 harmony_29 harmony_30
L1_AAACCTGAGGGCTTCC-1 0.49915898 1.1044102 -0.3714357 -0.7424044 0.48397303
L1_AAACCTGGTGCAGGTA-1 0.02498027 0.6448542 0.8595064 0.6553885 0.19538056
L1_AAACCTGGTTAAAGTG-1 0.92812115 0.4444257 -1.6033999 -2.6745387 0.63625048
L1_AAACCTGTCAGGTAAA-1 1.20343803 0.1554112 1.0715101 1.0798230 0.73986336
L1_AAACCTGTCCCTGACT-1 -0.74811611 -0.7705023 -1.0825354 -1.2346742 0.08136387
L1_AAACCTGTCCTTCAAT-1 -0.15308084 2.1517230 -0.2666182 1.3836759 0.63463441
harmony_31 harmony_32 harmony_33 harmony_34 harmony_35
L1_AAACCTGAGGGCTTCC-1 1.9925323 1.1201484 -1.3082249 -0.005557626 -2.2347211
L1_AAACCTGGTGCAGGTA-1 -1.7745965 0.4881785 2.6250817 1.325839660 1.7668570
L1_AAACCTGGTTAAAGTG-1 3.4189587 -1.1619459 -0.9131173 -0.738929237 -0.7944653
L1_AAACCTGTCAGGTAAA-1 -0.2905196 0.6616816 0.6616241 -0.417822466 -1.7207539
L1_AAACCTGTCCCTGACT-1 1.6030761 -0.6067572 -2.9550014 -1.073673601 -1.1409654
L1_AAACCTGTCCTTCAAT-1 0.8458875 1.0601501 2.1920442 0.453059896 -2.2320115
harmony_36 harmony_37 harmony_38 harmony_39 harmony_40
L1_AAACCTGAGGGCTTCC-1 0.58020487 -1.1289010 0.4495203 0.7372680 -0.3200434
L1_AAACCTGGTGCAGGTA-1 -0.69019882 0.3898066 -1.9040244 1.2530776 -2.1472602
L1_AAACCTGGTTAAAGTG-1 1.29037462 0.7052604 -0.3256223 -0.6045750 0.8934575
L1_AAACCTGTCAGGTAAA-1 -0.01755711 -0.6828708 -0.8218647 -0.5782875 1.0538171
L1_AAACCTGTCCCTGACT-1 -0.74725451 -0.8275788 2.0642014 -1.5769540 0.9883856
L1_AAACCTGTCCTTCAAT-1 -2.02453991 -0.7509340 -0.1231362 1.0223206 -0.9645708
harmony_41 harmony_42 harmony_43 harmony_44 harmony_45
L1_AAACCTGAGGGCTTCC-1 0.6400222 -0.5739799 0.4499493 -0.203359845 0.05030824
L1_AAACCTGGTGCAGGTA-1 0.7559842 0.9200416 1.7585739 -0.763331597 1.52800339
L1_AAACCTGGTTAAAGTG-1 -0.7195569 1.9110048 -0.9038872 0.007754539 -0.49006157
L1_AAACCTGTCAGGTAAA-1 0.7064127 -0.7832511 -1.2484201 0.917867543 1.81997842
L1_AAACCTGTCCCTGACT-1 -2.0747057 -0.4612828 -0.8220306 -1.367216381 0.10618932
L1_AAACCTGTCCTTCAAT-1 -1.1612844 -0.2023824 -3.0715510 -0.137506183 1.93822610
harmony_46 harmony_47 harmony_48 harmony_49 harmony_50
L1_AAACCTGAGGGCTTCC-1 1.586223770 -0.8058414 0.8400177 0.13174746 0.5703916
L1_AAACCTGGTGCAGGTA-1 0.004456556 0.8801568 -0.6638030 -0.73355571 0.7042793
L1_AAACCTGGTTAAAGTG-1 -1.649104678 -2.2764772 -2.1765942 0.79411393 -0.5709610
L1_AAACCTGTCAGGTAAA-1 -0.330346754 0.2369881 -1.2665325 0.39852531 -0.9456649
L1_AAACCTGTCCCTGACT-1 1.012225573 -0.5072353 1.0867109 -1.28707999 -0.1462727
L1_AAACCTGTCCTTCAAT-1 1.251540361 -1.4596204 -1.9589616 0.08050124 -0.4647281
# Run UMAP on Harmony embeddings
All_samples_Merged <- RunUMAP(All_samples_Merged, reduction = "harmony", dims = 1:16)
17:07:42 UMAP embedding parameters a = 0.9922 b = 1.112
17:07:42 Read 49515 rows and found 16 numeric columns
17:07:42 Using Annoy for neighbor search, n_neighbors = 30
17:07:42 Building Annoy index with metric = cosine, n_trees = 50
0% 10 20 30 40 50 60 70 80 90 100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
17:07:47 Writing NN index file to temp file /tmp/RtmpPISC9c/file217d72cb42c57
17:07:47 Searching Annoy index using 1 thread, search_k = 3000
17:08:05 Annoy recall = 100%
17:08:07 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
17:08:11 Initializing from normalized Laplacian + noise (using RSpectra)
17:08:14 Commencing optimization for 200 epochs, with 2100738 positive edges
17:09:16 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:16)
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: 49515
Number of edges: 1517154
Running Louvain algorithm...
Maximum modularity in 10 random starts: 0.8956
Number of communities: 14
Elapsed time: 18 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_without_harmony_integration.robj")