1 前言¶
本次分析所用数据均是来自于vdjMatrixPB-流程说明的结果目录。
2 R包载入¶
suppressPackageStartupMessages({
library(ggplot2)
library(semla)
library(Seurat)
library(paletteer)
library(viridis)
library(tibble)
library(patchwork)
library(immunarch)
library(ggforce)
library(scales)
library(tidyr)
library(ggvenn)
library(ggrepel)
library(circlize)
})
3 数据读取¶
##读取100um分辨率下IG和TR的结果用以演示
IG <- readRDS("Result/05.Web_Report/result/IG/rds/L13.rds")
TR <- readRDS("Result/05.Web_Report/result/TR/rds/L13.rds")
##查看IG和TR的结果
IG;TR
An object of class Seurat 4178 features across 1846 samples within 1 assay Active assay: Spatial (4178 features, 0 variable features) 2 layers present: counts, data 1 image present: sample1
An object of class Seurat 1062 features across 1723 samples within 1 assay Active assay: Spatial (1062 features, 0 variable features) 2 layers present: counts, data 1 image present: sample1
4 颜色配置¶
##color
col_scale_genes <- paletteer_c("grDevices::Oslo", 30)
col_scale_clones <- "blue"
col_scale_genes
<colors> #FCFCFCFF #F0F2F8FF #E4E9F4FF #D8DFEFFF #CCD6EBFF #C0CDE7FF #B4C4E3FF #A7BADEFF #9BB1DAFF #8EA8D6FF #82A0D2FF #7497CDFF #668EC9FF #5786C5FF #467DC2FF #3975B9FF #356CACFF #31649FFF #2C5B92FF #285386FF #244B7AFF #20436DFF #1C3C61FF #183456FF #142D4AFF #10253FFF #0D1E34FF #0A1829FF #07101CFF #040404FF
5 数据分析¶
5.1 semla对象转换¶
将Seurat对象转换为适配semla的对象
##semla 格式转换
IG_semla <- UpdateSeuratForSemla(IG,verbose = FALSE)
IG_semla <- LoadImages(IG_semla,verbose = FALSE)
TR_semla <- UpdateSeuratForSemla(TR,verbose = FALSE)
TR_semla <- LoadImages(TR_semla,verbose = FALSE)
ℹ Found VisiumV1 object(s). ── Collecting data from @images slot ℹ Found VisiumV1 object(s). ── Collecting data from @images slot
##查看HE图图像
options(repr.plot.width = 7, repr.plot.height = 7)
ImagePlot(IG_semla)
5.2 克隆型整体空间展示¶
##案例1
options(repr.plot.width = 20, repr.plot.height = 15)
MapFeaturesSummary(IG_semla,
pt_size = 3.5,
pt_alpha = 1,
max.cutoff = "q95",
min_cutoff = 0.05,
colors = col_scale_genes,
override_plot_dims = TRUE,
features = "nFeature_Spatial",
image_use = "raw",
subplot_type = "violin") -> IGspatial
MapFeaturesSummary(TR_semla,
pt_size = 3.5,
pt_alpha = 1,
max.cutoff = "q95",
min_cutoff = 0.05,
colors = col_scale_genes,
override_plot_dims = TRUE,
image_use = "raw",
features = "nFeature_Spatial",
subplot_type = "violin") -> TRspatial
IGspatial|TRspatial
##案例2
options(repr.plot.width = 20, repr.plot.height = 15)
suppressMessages(
SpatialFeaturePlot(IG,crop = F,features = "nFeature_Spatial",pt.size.factor = 15,image.alpha = 0.8) +
labs(title = "100um level") +
scale_fill_viridis(option = "D",name = "IG_Clonotype_Spatial") +
theme(plot.margin = margin(t = 0.5,r = 0.5,b = 0.5,l = 0.5,unit = "cm"),
legend.position = "right") -> IGspatial
)
suppressMessages(
SpatialFeaturePlot(TR,crop = F,features = "nFeature_Spatial",pt.size.factor = 15,image.alpha = 0.8) +
labs(title = "100um level") +
scale_fill_viridis(option = "D",name = "TR_Clonotype_Spatial") +
theme(plot.margin = margin(t = 0.5,r = 0.5,b = 0.5,l = 0.5,unit = "cm"),
legend.position = "right") -> TRspatial
)
IGspatial|TRspatial
5.3 特定克隆型展示¶
##查看克隆型
rownames(IG)[1:10]
rownames(TR)[1:10]
- 'IGH-CARVPYNSGWDVYWYFDLW'
- 'IGH-CRGRAYSGYDYGVDTFDIW'
- 'IGL-CSSYSGSNNRVF'
- 'IGH-CAASGFGYGDHDWFAPW'
- 'IGH-CARDLDTSSWFEPGYW'
- 'IGH-CARDRPGDYW'
- 'IGH-CARVGLSGTYFNFFDYW'
- 'IGH-CARVHGNGWWDW'
- 'IGH-CVKDTSVIPTVPGLFSSW'
- 'IGH-CVKGGRGTPIDSW'
- 'TRA-CAAKTGGGADGLTF'
- 'TRA-CIVRSLGSSNTGKLIF'
- 'TRB-CASSEATDAANTGELFF'
- 'TRA-CAASGGGTASKLTF'
- 'TRA-CAFMNPNNAGNMLTF'
- 'TRA-CAMREGRGGSYIPTF'
- 'TRB-CATWTGGGGQPQHF'
- 'TRB-CASSLGRIEQFF'
- 'TRB-CASSRDRNSQPQHF'
- 'TRB-CASVGLLLEQYF'
##IG和TR各随机选取5个克隆型展示
IGclono <- c("IGH-CARALESSGWHGWWFDLW","IGH-CTADTPEGVLMPDHW","IGK-CTQAAQFPWTF","IGL-CNCRDSSGHHLVF","IGL-CQAWDSSTWVF")
TRclono <- c("TRA-CAVSGGYQKVTF","TRA-CIVRSLGSSNTGKLIF","TRB-CASIPGGSYEQYF","TRB-CASSLDRYLFF","TRB-CASSPDKNTGELFF")
options(repr.plot.width = 30, repr.plot.height = 10,warn = -1)
IGlist <- NULL
k <- 1
for (i in IGclono){
IGsub <- IG[rownames(IG) %in% i,]
df <- FetchData(IGsub,vars = i) %>% rownames_to_column(var = "ID")
IGsub2 <- IGsub[,colnames(IGsub) %in% df[df[2] > 0,][,"ID"]]
suppressMessages(SpatialFeaturePlot(IGsub2,crop = F,features = i,pt.size.factor = 30,image.alpha = 0.2) + scale_fill_gradient(low = "steelblue",high = "steelblue") -> p)
IGlist[[k]] <- p
k <- k + 1
}
wrap_plots(IGlist,nrow = 1,widths = TRUE)
options(repr.plot.width = 30, repr.plot.height = 10,warn = -1)
TRlist <- NULL
k <- 1
for (i in TRclono){
TRsub <- TR[rownames(TR) %in% i,]
df <- FetchData(TRsub,vars = i) %>% rownames_to_column(var = "ID")
TRsub2 <- TRsub[,colnames(TRsub) %in% df[df[2] > 0,][,"ID"]]
suppressMessages(SpatialFeaturePlot(TRsub2,crop = F,features = i,pt.size.factor = 30,image.alpha = 0.2) + scale_fill_gradient(low = "steelblue",high = "steelblue") -> p)
TRlist[[k]] <- p
k <- k + 1
}
wrap_plots(TRlist,nrow = 1,widths = TRUE)
组织展示IG/TR克隆型数量的空间分布,可以看出,IG呈现成簇分布,TR呈现分散分布
5.4 克隆型数量分布统计¶
##BCR的轻重链
IGHnum <- length(grep(pattern = "^IGH",x = rownames(IG)))
IGKnum <- length(grep(pattern = "^IGK",x = rownames(IG)))
IGLnum <- length(grep(pattern = "^IGL",x = rownames(IG)))
##TCR的轻重链
TRAnum <- length(grep(pattern = "^TRA",x = rownames(TR)))
TRBnum <- length(grep(pattern = "^TRB",x = rownames(TR)))
##整理结果
IGTRnum <- data.frame(type = c("IGH","IGK","IGL","TRA","TRB"),values = c(IGHnum,IGKnum,IGLnum,TRAnum,TRBnum))
IGTRnum
| type | values |
|---|---|
| <chr> | <int> |
| IGH | 2852 |
| IGK | 611 |
| IGL | 715 |
| TRA | 289 |
| TRB | 772 |
##绘图展示
options(repr.plot.width = 8, repr.plot.height = 8)
ggplot(data = IGTRnum,aes(x = type,y = values)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme_bw(base_rect_size = 2) +
theme(axis.title.x = element_text(face = 2,size = 20,vjust = -4),
axis.title.y = element_text(face = 2,size = 20,vjust = 4),
axis.text = element_text(face = 2,size = 14),
panel.grid = element_blank(),
plot.margin = margin(40,40,40,40)) +
labs(x = "LR Spatial VDJ",y = "log10(No. of unique clones)") +
scale_y_log10()
5.5 Isotype 分布统计¶
同种型(isotype)指的是免疫球蛋白重链(Ig heavy chain)的恒定区域,包括IGHA、IGHD、IGHE、IGHG和IGHM。这些恒定区域可以被交换,以赋予抗体多样化的效应功能,同时保持对抗原的特异性。
Isotype 反映了免疫球蛋白的不同类别,即IgA、IgD、IgE、IgG和IgM。每种类别的免疫球蛋白都有其独特的生物学功能,这些功能由重链的恒定区域决定,因此也决定了免疫球蛋白的效应功能。
##数据读取
Isotype <- read.table("Result/03.Matrix/YF-XG.IG/L13/YF-XG.L13.Productive.addCgene.xls",header = FALSE)
head(Isotype)
| V1 | V2 | V3 | V4 | V5 | V6 | |
|---|---|---|---|---|---|---|
| <chr> | <chr> | <chr> | <int> | <chr> | <chr> | |
| 1 | L13_1000 | CAGGATAGTGGC | IGLC | 1 | CSSYSGSNNRVF | TGCAGCTCATATTCAGGCAGCAACAATCGGGTGTTC |
| 2 | L13_1000 | GTTTTCAGAAGG | IGHG | 1 | CARVPYNSGWDVYWYFDLW | TGTGCGAGAGTTCCATATAACAGTGGCTGGGACGTCTACTGGTACTTCGATCTCTGG |
| 3 | L13_1000 | TCGAACAGTCAG | IGHM | 1 | CRGRAYSGYDYGVDTFDIW | TGCCGTGGGAGGGCCTATAGTGGCTACGATTATGGGGTTGATACTTTTGATATCTGG |
| 4 | L13_1002 | AATGTTGACAAA | IGHG | 1 | CAASGFGYGDHDWFAPW | TGTGCGGCTTCCGGATTTGGATACGGTGACCACGACTGGTTCGCCCCCTGG |
| 5 | L13_1002 | ACATTCACAGGT | IGHM | 1 | CVRDDDTSSHYSVFNYW | TGTGTGAGAGATGATGATACTAGTAGTCATTACTCGGTTTTTAACTACTGG |
| 6 | L13_1002 | ACCAGACTAGGC | IGHG | 1 | CARVGLSGTYFNFFDYW | TGTGCGAGAGTCGGATTGAGTGGGACATATTTCAACTTCTTTGACTACTGG |
##数量统计整理
Isotypenum <- data.frame(table(Isotype$V3)[1:4])
Isotypenum
| Var1 | Freq |
|---|---|
| <fct> | <int> |
| IGHA | 9003 |
| IGHD | 9 |
| IGHG | 33370 |
| IGHM | 1702 |
##绘图展示
options(repr.plot.width = 8, repr.plot.height = 8)
ggplot(data = Isotypenum,aes(x = Var1,y = Freq)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme_bw(base_rect_size = 2) +
theme(axis.title.x = element_text(face = 2,size = 20,vjust = -4),
axis.title.y = element_text(face = 2,size = 20,vjust = 4),
axis.text = element_text(face = 2,size = 14),
panel.grid = element_blank(),
plot.margin = margin(40,40,40,40)) +
labs(x = "LR Spatial VDJ",y = "log10(No. of unique clones)") +
scale_y_log10()
在这里也可以展示同种型(isotype)的空间分布情况
##提取isotype所在的细胞
IGHA <- Isotype[Isotype$V3 == "IGHA",][,c("V1")]
IGHD <- Isotype[Isotype$V3 == "IGHD",][,c("V1")]
IGHG <- Isotype[Isotype$V3 == "IGHG",][,c("V1")]
IGHM <- Isotype[Isotype$V3 == "IGHM",][,c("V1")]
##提取子集
IGHAobj <- SubsetSTData(IG_semla,spots = unique(IGHA))
IGHDobj <- SubsetSTData(IG_semla,spots = unique(IGHD))
IGHGobj <- SubsetSTData(IG_semla,spots = unique(IGHG))
IGHMobj <- SubsetSTData(IG_semla,spots = unique(IGHM))
options(repr.plot.width = 30, repr.plot.height = 10)
MapFeatures(IGHAobj, pt_size = 3.5,pt_alpha = 1,colors = col_scale_genes,features = "nFeature_Spatial",image_use = "raw") -> IGHAobjp
MapFeatures(IGHDobj, pt_size = 3.5,pt_alpha = 1,colors = col_scale_genes,features = "nFeature_Spatial",image_use = "raw") -> IGHDobjp
MapFeatures(IGHGobj, pt_size = 3.5,pt_alpha = 1,colors = col_scale_genes,features = "nFeature_Spatial",image_use = "raw") -> IGHGobjp
MapFeatures(IGHMobj, pt_size = 3.5,pt_alpha = 1,colors = col_scale_genes,features = "nFeature_Spatial",image_use = "raw") -> IGHMobjp
IGHAobjp|IGHDobjp|IGHGobjp|IGHMobjp
5.6 克隆型长度分布统计¶
##数据读取
IGclonotype <- read.table("Result/05.Web_Report/result/IG/clonotype/L13.clonotype.xls",header = TRUE)
TRclonotype <- read.table("Result/05.Web_Report/result/TR/clonotype/L13.clonotype.xls",header = TRUE)
##长度统计
IGclonotype$len <- nchar(IGclonotype$CDR3aa)
TRclonotype$len <- nchar(TRclonotype$CDR3aa)
##轻重链区分
IGclonotype$chain <- substr(x = IGclonotype$V,start = 1,stop = 3)
TRclonotype$chain <- substr(x = TRclonotype$V,start = 1,stop = 3)
不区分轻重链和区分轻重链展示
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
##CDR3aa长度分布
suppressMessages(
ggplot(data = IGclonotype,aes(x = len)) +
geom_histogram(fill = "steelblue",color = "grey40",bins = 30) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_rect_size = 1.5) +
theme(panel.grid = element_blank(),
axis.title.x = element_text(face = 2,size = 15,vjust = -4),
axis.title.y = element_text(face = 2,size = 15,vjust = 4),
axis.text = element_text(face = 2,size = 12),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "CDR3aa length, bp",
y = "Frequency",
title = "IG Clonotype CDR3aa Distribution") +
xlim(0,40) -> IGcdr3aalen1
)
suppressMessages(
ggplot(data = IGclonotype,aes(x = len,fill = chain)) +
geom_histogram(color = "grey40",position = "identity",binwidth = 0.9,alpha = 0.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_rect_size = 1.5) +
theme(panel.grid = element_blank(),
axis.title.x = element_text(face = 2,size = 15,vjust = -4),
axis.title.y = element_text(face = 2,size = 15,vjust = 4),
axis.text = element_text(face = 2,size = 12),
legend.text = element_text(face = 2,size = 15),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "CDR3aa length, bp",
y = "Frequency",
title = "IG Clonotype CDR3aa Distribution") +
scale_fill_manual(values=c('red', 'blue', 'green')) +
xlim(0,40) -> IGcdr3aalen2
)
IGcdr3aalen1|IGcdr3aalen2
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
##CDR3aa长度分布
suppressMessages(
ggplot(data = TRclonotype,aes(x = len)) +
geom_histogram(fill = "steelblue",color = "grey40",bins = 30) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_rect_size = 1.5) +
theme(panel.grid = element_blank(),
axis.title.x = element_text(face = 2,size = 15,vjust = -4),
axis.title.y = element_text(face = 2,size = 15,vjust = 4),
axis.title = element_text(face = 2,size = 15),
axis.text = element_text(face = 2,size = 12),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "CDR3aa length, bp",
y = "Frequency",
title = "TR Clonotype CDR3aa Distribution") +
xlim(0,40) -> TRcdr3aalen1
)
suppressMessages(
ggplot(data = TRclonotype,aes(x = len,fill = chain)) +
geom_histogram(color = "grey40",position = "identity",binwidth = 0.9,alpha = 0.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_bw(base_rect_size = 1.5) +
theme(panel.grid = element_blank(),
axis.title.x = element_text(face = 2,size = 15,vjust = -4),
axis.title.y = element_text(face = 2,size = 15,vjust = 4),
axis.title = element_text(face = 2,size = 15),
axis.text = element_text(face = 2,size = 12),
legend.text = element_text(face = 2,size = 15),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "CDR3aa length, bp",
y = "Frequency",
title = "TR Clonotype CDR3aa Distribution") +
scale_fill_manual(values=c('red', 'blue','green','purple')) +
xlim(0,40) -> TRcdr3aalen2
)
TRcdr3aalen1|TRcdr3aalen2
6 immunarch分析¶
R语言包immunarch兼容的数据输入格式: ImmunoSEQ, IMGT, MiTCR, MiXCR, MiGEC, MigMap, VDJtools, tcR, AIRR, 10XGenomics。在这里将克隆型数据整理成vdjtools格式方便快速读取。
本次分析主要参考:immunarch教程文档
6.1 数据格式转换¶
##数据读取并新增列
IGclonotype <- read.table("Result/05.Web_Report/result/IG/clonotype/L13.clonotype.xls", header = TRUE) %>%
mutate(VEnd = -1, DStart = -1, DEnd = -1, JStart = -1)
TRclonotype <- read.table("Result/05.Web_Report/result/TR/clonotype/L13.clonotype.xls", header = TRUE) %>%
mutate(VEnd = -1, DStart = -1, DEnd = -1, JStart = -1)
##导出数据用于repLoad读取
write.table(x = IGclonotype,file = "IG_L13.clonotype.xls",quote = F,sep = "\t",row.names = F)
write.table(x = TRclonotype,file = "TR_L13.clonotype.xls",quote = F,sep = "\t",row.names = F)
6.2 数据读取¶
##数据导入
IGimmdata <- repLoad("IG_L13.clonotype.xls")
TRimmdata <- repLoad("TR_L13.clonotype.xls")
== Step 1/3: loading repertoire files... == Processing "<initial>" ... -- [1/1] Parsing "IG_L13.clonotype.xls" -- vdjtools == Step 2/3: checking metadata files and merging files... == Processing "<initial>" ... -- Metadata file not found; creating a dummy metadata... == Step 3/3: processing paired chain data... == Done! == Step 1/3: loading repertoire files... == Processing "<initial>" ... -- [1/1] Parsing "TR_L13.clonotype.xls" -- vdjtools == Step 2/3: checking metadata files and merging files... == Processing "<initial>" ... -- Metadata file not found; creating a dummy metadata... == Step 3/3: processing paired chain data... == Done!
6.3 克隆型丰度图¶
##克隆型的相对丰度统计
ig_cnt <- repExplore(IGimmdata$data, .method = "count")
tr_cnt <- repExplore(TRimmdata$data, .method = "count")
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
p1 <- vis(ig_cnt)
p2 <- vis(tr_cnt)
p1|p2
6.4 克隆型的克隆性评估¶
##克隆比例(clonal.prop)方法
ig_pr <- repClonality(IGimmdata$data, .method = "clonal.prop")
tr_pr <- repClonality(TRimmdata$data, .method = "clonal.prop")
ig_pr;tr_pr
| Clones | Percentage | Clonal.count.prop | |
|---|---|---|---|
| IG_L13.clonotype.xls | 2 | 12.1 | 0.0004792715 |
| Clones | Percentage | Clonal.count.prop | |
|---|---|---|---|
| TR_L13.clonotype.xls | 1 | 23.7 | 0.0009425071 |
##稀有方法(Rare method)
ig_rare <- repClonality(IGimmdata$data, .method = "rare")
tr_rare <- repClonality(TRimmdata$data, .method = "rare")
ig_rare;tr_rare
| 1 | 3 | 10 | 30 | 100 | MAX | |
|---|---|---|---|---|---|---|
| IG_L13.clonotype.xls | 0.04465872 | 0.07406169 | 0.1120515 | 0.1903539 | 0.3708187 | 1 |
| 1 | 3 | 10 | 30 | 100 | MAX | |
|---|---|---|---|---|---|---|
| TR_L13.clonotype.xls | 0.05770485 | 0.1162791 | 0.2414692 | 0.3674201 | 0.5357531 | 1 |
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
p1 <- vis(ig_rare) +
theme(axis.text.x = element_text(angle = 0,hjust = 0.5),
axis.title.x = element_blank())
p2 <- vis(tr_rare, .meta = immdata$meta) +
theme(axis.text.x = element_text(angle = 0,hjust = 0.5),
axis.title.x = element_blank())
p1|p2
Using Sample as id variables Using Sample as id variables
稀有方法(Rare method)是一种评估克隆性的方法,它专注于样本中最不丰富的克隆型。通过分析这些最不丰富的克隆型,可以了解样本中克隆型的多样性和稀有性,从而评估样本的克隆性。
##Homeo方法评估克隆空间内稳态
ig_hom <- repClonality(IGimmdata$data,.method = "homeo",.clone.types = c(Small = .0001, Medium = .001, Large = .01, Hyperexpanded = 1))
tr_hom <- repClonality(TRimmdata$data,.method = "homeo",.clone.types = c(Small = .0001, Medium = .001, Large = .01, Hyperexpanded = 1))
ig_hom;tr_hom
| Small (0 < X <= 1e-04) | Medium (1e-04 < X <= 0.001) | Large (0.001 < X <= 0.01) | Hyperexpanded (0.01 < X <= 1) | |
|---|---|---|---|---|
| IG_L13.clonotype.xls | 0.08859601 | 0.1710862 | 0.457444 | 0.2828738 |
| Small (0 < X <= 1e-04) | Medium (1e-04 < X <= 0.001) | Large (0.001 < X <= 0.01) | Hyperexpanded (0.01 < X <= 1) | |
|---|---|---|---|---|
| TR_L13.clonotype.xls | 0 | 0.2284286 | 0.3073245 | 0.4642469 |
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
p1 <- vis(ig_hom) +
theme(axis.text.x = element_text(angle = 0,hjust = 0.5),
axis.title.x = element_blank())
p2 <- vis(tr_hom) +
theme(axis.text.x = element_text(angle = 0,hjust = 0.5),
axis.title.x = element_blank())
p1|p2
Homeo方法评估克隆空间内稳态,即特定大小的克隆群体在总库中所占的比例。这有助于了解样本中克隆型的分布情况,特别是不同丰度克隆型的相对比例。
- Small:小克隆型,频率较低。
- Medium:中等克隆型,频率中等。
- Large:大克隆型,频率较高。
- Hyperexpanded:过度扩增克隆型,频率非常高。
6.5 基因使用计算¶
immunarch包含一个基因片段数据表,其中列出了遵循IMGT命名法的多个物种的已知基因片段。为了获取当前基因的统计数据,可以调用gene_stats() 函数。
gene_stats()
| alias | species | ighd | ighj | ighv | igij | igkj | igkv | iglj | iglv | traj | trav | trbd | trbj | trbv | trdd | trdj | trdv | trgj | trgv |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <chr> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> |
| bt | BosTaurus | 21 | 4 | 25 | 0 | 1 | 6 | 5 | 26 | 46 | 0 | 0 | 0 | 0 | 5 | 3 | 0 | 6 | 15 |
| cd | CamelusDromedarius | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 2 | 2 |
| clf | CanisLupusFamiliaris | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 8 | 19 | 0 | 0 | 0 | 7 | 8 |
| dr | DanioRerio | 7 | 7 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| hs | HomoSapiens | 30 | 13 | 248 | 0 | 5 | 64 | 7 | 69 | 57 | 60 | 3 | 14 | 64 | 3 | 4 | 6 | 4 | 10 |
| macmul | MacacaMulatta | 24 | 7 | 19 | 0 | 4 | 83 | 5 | 0 | 0 | 0 | 2 | 15 | 58 | 0 | 0 | 0 | 0 | 0 |
| mmc | MusMusculusCastaneus | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| mmd | MusMusculusDomesticus | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| musmus | MusMusculus | 32 | 8 | 225 | 0 | 8 | 109 | 3 | 5 | 42 | 145 | 2 | 14 | 23 | 2 | 3 | 7 | 0 | 11 |
| oa | OrnithorhynchusAnatinus | 3 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| oc | OryctolagusCuniculus | 10 | 11 | 39 | 0 | 8 | 26 | 2 | 20 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| om | OncorhynchusMykiss | 9 | 7 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 9 | 0 | 0 | 0 | 0 | 0 | 0 |
| rn | RattusNorvegicus | 30 | 4 | 113 | 0 | 6 | 132 | 2 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| smth | MusMusculusMolossinus | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| smth | MusMusculusMusculus | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| smth | MusSpretus | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| ss | SusScrofa | 5 | 5 | 15 | 0 | 8 | 19 | 4 | 14 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
##这里演示以TR中的TRB为例
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
TR_gucount <- geneUsage(TRimmdata$data,.gene = "HomoSapiens.TRBV",.ambig = "exc")
TR_gunorm <- geneUsage(TRimmdata$data, .gene = "HomoSapiens.TRBV",.norm = T, .ambig = "exc")
p1 <- vis(TR_gucount,.grid = T)
p2 <- vis(TR_gunorm,.grid = T)
p1/p2
Using Names as id variables Using Names as id variables
Spectratype 是一种用于表示每个序列长度的基因分布的有用方法。immunarch 包中的 spectratype 函数可以实现这一功能。该函数接收一个免疫库或多个免疫库的列表作为输入,并通过 .quant 和 .col 参数控制计算比例的量和选择的列。
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
p1 <- vis(spectratype(IGimmdata$data[[1]], .quant = "count", .col = "aa+v"))
p2 <- vis(spectratype(TRimmdata$data[[1]], .quant = "count", .col = "aa+j"))
p1 + p2
6.6 克隆型多样性估计¶
三种公认的生物物种多样性假说。它们包括:(1)异质性假说,(2)竞争假说,(3)捕食假说。
衡量多样性有三个主要原因:
- 衡量稳定性,以确定一个环境是否在退化,
- 比较两个或更多的环境
- 消除对广泛列表的需要(形成数据概览,即用一个多样性指标,说明了需要列举很多概念才能说清楚的东西)。多样性指数提供了一个群落组成的重要信息。这些指数不仅衡量了物种的丰富度,还考虑了物种的相对丰富度或均匀度。在测量物种多样性时,物种丰富度和均匀度必须同时考虑。此外,指数还提供了物种稀有度和共性的重要信息。
repDiversity 函数实现了几种估计免疫库多样性的方法。.method 参数类似于上述函数,用于设置多样性的估计方法。可以选择以下方法:
- chao1:Chao1 是一种非参数渐近估计器,用于估计物种丰富度(一个群体中的物种数量)。适用于估计免疫库中不同克隆型的数量,特别是在样本量较小的情况下。
- hill:Hill 数字是一个数学上统一的多样性指数家族,通过不同的指数 q 来区分。
- div:真实多样性,或有效类型数,指的是在数据集中观察到的平均比例丰富度所需的同等丰富的类型数。用于评估免疫库中不同克隆型的实际多样性,考虑了克隆型的丰度差异。
- gini.simp:Gini-Simpson 指数是异种相遇的概率,即两个实体代表不同类型的概率。用于评估免疫库中克隆型的多样性,特别是在克隆型分布不均匀的情况下。
- inv.simp:逆 Simpson 指数是通过加权算术平均数来量化数据集中类型的比例丰富度时得到的有效类型数。用于评估免疫库中克隆型的多样性,特别适用于克隆型分布较为集中时。
- gini:Gini 系数衡量频率分布(例如收入水平)中的不平等程度。Gini 系数为零表示完全平等,所有值都相同。Gini 系数为一(或100%)表示值之间的最大不平等。用于评估免疫库中克隆型分布的不平等程度,帮助理解克隆型的主导情况。
- raref:稀释法是一种通过外推采样结果来评估物种丰富度的技术。用于评估免疫库中克隆型的丰富度,特别是在样本量较小或采样不完全时。
- dXX 是一个与 d50 指数类似的指标,其中 XX 对应于总测序读数的期望百分比。这个指标用于评估免疫库的多样性,特别是在测序数据中不同克隆型的分布情况。
- d50 指数是指在总测序读数中,累积读数达到 50% 时所需的克隆型数量。这个指标反映了免疫库中克隆型的分布情况,数值越小,表示少数克隆型占据了大部分读数,多样性较低;数值越大,表示克隆型分布较为均匀,多样性较高。
##IG分析
# Compute statistics and visualise them
# Chao1 diversity measure
div_chao_ig <- repDiversity(IGimmdata$data, "chao1")
# Hill numbers
div_hill_ig <- repDiversity(IGimmdata$data, "hill")
# Ecological diversity measure
div_div_ig <- repDiversity(IGimmdata$data, "div")
# D50
div_d50_ig <- repDiversity(IGimmdata$data, "d50")
# gini.simp
div_gini.simp_ig <- repDiversity(IGimmdata$data, "gini.simp")
# inv.simp
div_inv.simp_ig <- repDiversity(IGimmdata$data, "inv.simp")
# gini
div_gini_ig <- repDiversity(IGimmdata$data, "gini")
# dxx
div_dxx_ig <- repDiversity(IGimmdata$data, "dxx")
# raref
div_raref_ig <- repDiversity(IGimmdata$data, "raref")
##绘图展示
options(repr.plot.width = 30, repr.plot.height = 15,warn = -1)
p1 <- vis(div_chao_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p2 <- vis(div_hill_ig)
p3 <- vis(div_div_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p4 <- vis(div_d50_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p5 <- vis(div_gini.simp_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p6 <- vis(div_inv.simp_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p7 <- vis(div_dxx_ig) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p8 <- vis(div_raref_ig)
wrap_plots(plotlist = list(p1,p2,p3,p4,p5,p6,p7,p8),ncol = 4)
|======================================================================| 100%
##TR分析
# Compute statistics and visualise them
# Chao1 diversity measure
div_chao_tr <- repDiversity(TRimmdata$data, "chao1")
# Hill numbers
div_hill_tr <- repDiversity(TRimmdata$data, "hill")
# Ecological diversity measure
div_div_tr <- repDiversity(TRimmdata$data, "div")
# D50
div_d50_tr <- repDiversity(TRimmdata$data, "d50")
# gini.simp
div_gini.simp_tr <- repDiversity(TRimmdata$data, "gini.simp")
# inv.simp
div_inv.simp_tr <- repDiversity(TRimmdata$data, "inv.simp")
# gini
div_gini_tr <- repDiversity(TRimmdata$data, "gini")
# dxx
div_dxx_tr <- repDiversity(TRimmdata$data, "dxx")
# raref
div_raref_tr <- repDiversity(TRimmdata$data, "raref")
##绘图展示
options(repr.plot.width = 30, repr.plot.height = 15,warn = -1)
p1 <- vis(div_chao_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p2 <- vis(div_hill_tr)
p3 <- vis(div_div_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p4 <- vis(div_d50_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p5 <- vis(div_gini.simp_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p6 <- vis(div_inv.simp_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p7 <- vis(div_dxx_tr) + theme(axis.title.x = element_blank(),axis.text.x = element_text(angle = 0,hjust = 0.5))
p8 <- vis(div_raref_tr)
wrap_plots(plotlist = list(p1,p2,p3,p4,p5,p6,p7,p8),ncol = 4)
|======================================================================| 100%
6.7 kmer分析¶
##kmer分析, 默认k = 3
IGkmers <- getKmers(IGimmdata$data[[1]], 3)
TRkmers <- getKmers(TRimmdata$data[[1]], 3)
##绘图
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
p1 <- vis(IGkmers) + labs(title = "IG Kmers distribution")
p2 <- vis(TRkmers) + labs(title = "TR Kmers distribution")
p1/p2
6.8 序列基序分析¶
- 位置频率矩阵(PFM),每个位置上每种氨基酸出现的次数的矩阵。PFM 用于统计每个位置上每种氨基酸的出现频率,是基序分析的基础。
- 位置概率矩阵(PPM),每个位置上每种氨基酸出现的概率的矩阵。PPM 通过将 PFM 中的频率转换为概率,提供了更直观的基序表示,便于进一步分析。
- 位置权重矩阵(PWM),PPM 元素的对数似然比的矩阵。PWM 通过计算对数似然比,强调了某些氨基酸在特定位置上的重要性,常用于基序搜索和匹配。
- 自信息矩阵,PWM 中每个元素的自信息的矩阵。自信息矩阵用于评估每个位置上每种氨基酸的信息含量,帮助识别关键的基序位置。
##IG分析
##四种分析方法
IGkmers <- getKmers(IGimmdata$data[[1]], 5)
IG1 <- kmer_profile(IGkmers,.method = "freq")
IG2 <- kmer_profile(IGkmers,.method = "prob")
IG3 <- kmer_profile(IGkmers,.method = "wei")
IG4 <- kmer_profile(IGkmers,.method = "self")
##其中一种结果
IG1
| A | 4581 | 4606 | 2498 | 2461 | 2242 |
|---|---|---|---|---|---|
| C | 4402 | 585 | 479 | 512 | 495 |
| D | 2884 | 2954 | 4958 | 5005 | 4222 |
| E | 967 | 1004 | 1124 | 1099 | 826 |
| F | 1082 | 2391 | 2495 | 2589 | 3717 |
| G | 5414 | 5584 | 5805 | 5688 | 5190 |
| H | 1163 | 1280 | 1340 | 1417 | 1221 |
| I | 909 | 1062 | 1086 | 1387 | 1365 |
| K | 876 | 883 | 893 | 558 | 495 |
| L | 1808 | 2216 | 2343 | 2478 | 2384 |
| M | 480 | 730 | 629 | 672 | 665 |
| N | 1556 | 1639 | 1698 | 1696 | 1669 |
| P | 1708 | 2274 | 2351 | 2614 | 2503 |
| Q | 1869 | 1898 | 1400 | 890 | 796 |
| R | 4106 | 4077 | 4134 | 2378 | 2313 |
| S | 5102 | 5299 | 5005 | 4716 | 4537 |
| T | 2710 | 2918 | 2733 | 3033 | 2963 |
| V | 2320 | 2476 | 2363 | 3207 | 2988 |
| W | 1511 | 1547 | 1865 | 1843 | 4194 |
| Y | 4156 | 4181 | 4405 | 5361 | 4819 |
##绘图
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
p1 <- vis(IG1)
p2 <- vis(IG1, .plot = "seq")
p1|p2
##TR分析
##四种分析方法
TRkmers <- getKmers(TRimmdata$data[[1]], 5)
TR1 <- kmer_profile(TRkmers,.method = "freq")
TR2 <- kmer_profile(TRkmers,.method = "prob")
TR3 <- kmer_profile(TRkmers,.method = "wei")
TR4 <- kmer_profile(TRkmers,.method = "self")
##其中一种结果
TR1
| A | 1383 | 1402 | 626 | 551 | 531 |
|---|---|---|---|---|---|
| C | 1017 | 33 | 34 | 31 | 32 |
| D | 414 | 422 | 426 | 429 | 416 |
| E | 293 | 642 | 637 | 626 | 616 |
| F | 124 | 125 | 152 | 432 | 1399 |
| G | 1676 | 1791 | 1789 | 1748 | 1696 |
| H | 77 | 77 | 77 | 218 | 216 |
| I | 140 | 169 | 187 | 250 | 232 |
| K | 109 | 233 | 231 | 228 | 199 |
| L | 457 | 466 | 752 | 742 | 744 |
| M | 89 | 106 | 114 | 80 | 65 |
| N | 532 | 551 | 553 | 544 | 520 |
| P | 368 | 512 | 551 | 545 | 532 |
| Q | 385 | 418 | 794 | 798 | 784 |
| R | 558 | 552 | 555 | 559 | 430 |
| S | 1835 | 1833 | 1782 | 1193 | 630 |
| T | 682 | 806 | 795 | 927 | 900 |
| V | 326 | 341 | 327 | 259 | 247 |
| W | 135 | 132 | 133 | 131 | 105 |
| Y | 398 | 387 | 483 | 707 | 704 |
##绘图
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
p1 <- vis(TR1)
p2 <- vis(TR1, .plot = "seq")
p1|p2
6.9 VJ 桑基图¶
##IG分析
options(repr.plot.width = 15, repr.plot.height = 40,warn = -1)
IGimmdata$data$IG_L13.clonotype.xls %>%
gather_set_data(c(6,7,5)) %>%
ggplot(aes(x, id = id, split = y, value = 1)) +
geom_parallel_sets(aes(fill = J.name), show.legend = FALSE, alpha = 0.5) +
geom_parallel_sets_axes(axis.width = 0.1, color = "lightgrey", fill = "white") +
geom_parallel_sets_labels(angle = 0) +
theme_no_axes()
##整理IG的VJ组合,用于绘制圆形桑基图
IGimmdata$data$IG_L13.clonotype.xls %>%
select(V.name,J.name,Clones) %>%
group_by(V.name, J.name) %>%
summarise(Clones = sum(Clones)) -> IG_vj_circle
`summarise()` has grouped output by 'V.name'. You can override using the `.groups` argument.
options(repr.plot.width = 15, repr.plot.height = 15,warn = -1)
chordDiagram(IG_vj_circle,
preAllocateTracks = list(track.height = 0.2),
transparency = 0.5,
annotationTrack = "grid",
reduce = 0)
circos.trackPlotRegion(track.index = 1,
bg.border = NA,
panel.fun = function(x, y) {
sector.name = get.cell.meta.data("sector.index")
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
circos.text(mean(xlim), ylim[1], cex = 0.6, sector.name, facing = "clockwise", adj = c(0, 0.5))
})
##TR分析
options(repr.plot.width = 15, repr.plot.height = 40,warn = -1)
TRimmdata$data$TR_L13.clonotype.xls %>%
gather_set_data(c(6,7,5)) %>%
ggplot(aes(x, id = id, split = y, value = 1)) +
geom_parallel_sets(aes(fill = J.name), show.legend = FALSE, alpha = 0.5) +
geom_parallel_sets_axes(axis.width = 0.1, color = "lightgrey", fill = "white") +
geom_parallel_sets_labels(angle = 0) +
theme_no_axes()
##整理TR的VJ组合,用于绘制圆形桑基图
TRimmdata$data$TR_L13.clonotype.xls %>%
select(V.name,J.name,Clones) %>%
group_by(V.name, J.name) %>%
summarise(Clones = sum(Clones)) -> TR_vj_circle
`summarise()` has grouped output by 'V.name'. You can override using the `.groups` argument.
options(repr.plot.width = 15, repr.plot.height = 15,warn = -1)
chordDiagram(TR_vj_circle,
preAllocateTracks = list(track.height = 0.2),
transparency = 0.5,
annotationTrack = "grid",
reduce = 0)
circos.trackPlotRegion(track.index = 1,
bg.border = NA,
panel.fun = function(x, y) {
sector.name = get.cell.meta.data("sector.index")
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
circos.text(mean(xlim), ylim[1], cex = 0.6, sector.name, facing = "clockwise", adj = c(0, 0.5))
})
7 细胞水平分析¶
7.1 颜色配置¶
##color(色系配置)
options(repr.plot.width = 8, repr.plot.height = 8,warn = -1)
col_scale_genes <- viridis_pal(option = "D")(12)
show_col(col_scale_genes)
7.2 整体展示¶
##细胞水平IG和TR读取
IG <- readRDS("Result/05.Web_Report/result/IG/rds/Cell.rds")
TR <- readRDS("Result/05.Web_Report/result/TR/rds/Cell.rds")
##semla 格式转换
IG_semla <- UpdateSeuratForSemla(IG,verbose = FALSE)
IG_semla <- LoadImages(IG_semla,verbose = FALSE)
TR_semla <- UpdateSeuratForSemla(TR,verbose = FALSE)
TR_semla <- LoadImages(TR_semla,verbose = FALSE)
ℹ Found VisiumV1 object(s). ── Collecting data from @images slot ℹ Found VisiumV1 object(s). ── Collecting data from @images slot
##细胞水平全局展示
options(repr.plot.width = 20, repr.plot.height = 15)
MapFeatures(IG_semla,
pt_size = 1.5,
pt_alpha = 1,
max.cutoff = "q75",
min_cutoff = 0.05,
colors = col_scale_genes,
override_plot_dims = TRUE,
features = "nFeature_Spatial",
#image_use = "raw",
subplot_type = "violin") -> IGspatial
MapFeatures(TR_semla,
pt_size = 1.5,
pt_alpha = 1,
max.cutoff = "q95",
min_cutoff = 0.05,
colors = col_scale_genes,
override_plot_dims = TRUE,
#image_use = "raw",
features = "nFeature_Spatial",
subplot_type = "violin") -> TRspatial
IGspatial|TRspatial
7.3 VDJ基因数量统计¶
IMGT ,关于人类免疫组库,功能基因其中IG有V基因229个,D基因37个,J基因18个,C基因,14个;TR有V基因127个,D基因5个,J基因84个,C基因3个。
##读取IgBlast分析初步处理结果
IGgene <- read.table("Result/05.Web_Report/result/IG/YF-XG.IG.read.result.xls",header = T)
TRgene <- read.table("Result/05.Web_Report/result/TR/YF-XG.TR.read.result.xls",header = T)
##统计VDJC的数量
#IG
options(warn = -1)
IGgene %>% separate(V, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> IGVnum
IGgene %>% separate(D, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> IGDnum
IGgene %>% separate(J, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> IGJnum
IGgene %>% separate(C, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> IGCnum
#TR
TRgene %>% separate(V, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> TRVnum
TRgene %>% separate(D, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> TRDnum
TRgene %>% separate(J, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> TRJnum
TRgene %>% separate(C, into = c('First', 'Last'), sep = '[*]') %>% filter(First != "-") %>% select(First) %>% distinct() %>% nrow() -> TRCnum
##构建数据框
IGnum <- data.frame(Type = c("V","D","J","C"),IMGT = c(229,37,18,14),IgBlast = c(IGVnum,IGDnum,IGJnum,IGCnum))
TRnum <- data.frame(Type = c("V","D","J","C"),IMGT = c(127,5,84,3), IgBlast = c(TRVnum,TRDnum,TRJnum,TRCnum))
##绘图展示
options(repr.plot.width = 15, repr.plot.height = 8)
ggplot(data = IGnum,aes(x = Type,y = IgBlast)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme_bw(base_rect_size = 2) +
theme(axis.title.x = element_text(face = 2,size = 20,vjust = -4),
axis.title.y = element_text(face = 2,size = 20,vjust = 4),
axis.text = element_text(face = 2,size = 14),
panel.grid = element_blank(),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "genes",y = "count",title = "BCR align gene") +
geom_text(aes(label = IgBlast),nudge_y = 5,size = 6) +
scale_x_discrete(limits = c("V","D","J","C"),
labels = c("V","D","J","C")) -> p1
ggplot(data = TRnum,aes(x = Type,y = IgBlast)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme_bw(base_rect_size = 2) +
theme(axis.title.x = element_text(face = 2,size = 20,vjust = -4),
axis.title.y = element_text(face = 2,size = 20,vjust = 4),
axis.text = element_text(face = 2,size = 14),
panel.grid = element_blank(),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
labs(x = "genes",y = "count",title = "TCR align gene") +
geom_text(aes(label = IgBlast),nudge_y = 5,size = 6) +
scale_x_discrete(limits = c("V","D","J","C"),
labels = c("V","D","J","C")) -> p2
p1|p2
7.4 Pair统计¶
##细胞水平IG和TR读取
IGprod <- read.table("Result/05.Web_Report/result/IG/YF-XG.IG.Cell.Productive.xls",header = F)
TRprod <- read.table("Result/05.Web_Report/result/TR/YF-XG.TR.Cell.Productive.xls",header = F)
names(IGprod) <- c("Cell","UMI","Chain","Read","CDR3aa","CDR3nt")
names(TRprod) <- c("Cell","UMI","Chain","Read","CDR3aa","CDR3nt")
##统计
##IG
IGprod[,c("Cell","Chain")] %>% distinct() %>% filter(Chain == 'IGH') %>% select(Cell) -> IGHcells
IGprod[,c("Cell","Chain")] %>% distinct() %>% filter(Chain == 'IGK' | Chain == "IGL") %>% select(Cell) -> IGKLcells
IGList <- list(IgH = IGHcells[,1],`IgK/L` = IGKLcells[,1])
##TR
TRprod[,c("Cell","Chain")] %>% distinct() %>% filter(Chain == 'TRA') %>% select(Cell) -> TRAcells
TRprod[,c("Cell","Chain")] %>% distinct() %>% filter(Chain == 'TRB') %>% select(Cell) -> TRBcells
TRList <- list(TCRα = TRAcells[,1],TCRβ = TRBcells[,1])
##韦恩图
options(repr.plot.width = 12, repr.plot.height = 8)
ggvenn(data = IGList,
columns = c("IgH", "IgK/L"),
auto_scale = FALSE,
fill_color = c("red","green")) -> venn1
ggvenn(data = TRList,
columns = c("TCRα", "TCRβ"),
auto_scale = FALSE,
fill_color = c("red","green")) -> venn2
venn1|venn2
##pair对筛选
TRprod[,c("Cell","Chain","CDR3aa")] %>%
arrange(Cell,Chain) %>%
mutate(Type = paste(CDR3aa,Chain,sep = "@")) %>%
select(Cell,Type) %>%
group_by(Cell) %>%
summarise(Chain = paste(Type,collapse = ","),Pair = n()) %>%
filter(grepl("@TRA", Chain) & grepl("@TRB", Chain)) %>%
filter(Pair == 2) -> TRpair
##整理数据
TRpairdf <- data.frame(sort(table(TRpair$Chain),decreasing = TRUE))
TRpairdf$rank <- 1:nrow(TRpairdf)
names(TRpairdf) <- c("pair","count","rank")
head(TRpairdf)
| pair | count | rank | |
|---|---|---|---|
| <fct> | <int> | <int> | |
| 1 | CAMREGRGGSYIPTF@TRA,CATWTGGGGQPQHF@TRB | 37 | 1 |
| 2 | CAAKTGGGADGLTF@TRA,CATWTGGGGQPQHF@TRB | 19 | 2 |
| 3 | CAFMNPNNAGNMLTF@TRA,CATWTGGGGQPQHF@TRB | 10 | 3 |
| 4 | CAMREGRGGSYIPTF@TRA,CASSLGGIEQFF@TRB | 9 | 4 |
| 5 | CAMREGRGGSYIPTF@TRA,CASSPRGRVGDTEAFF@TRB | 8 | 5 |
| 6 | CAAKTGGGADGLTF@TRA,CASSPRGRVGDTEAFF@TRB | 5 | 6 |
##默认展示前5个pair
TRpairdftopN <- TRpairdf[1:5,]
##绘图展示
options(repr.plot.width = 8, repr.plot.height = 7)
ggplot(data = TRpairdf,aes(x = rank,y = count,size = count)) +
geom_text_repel(data = TRpairdftopN,aes(x = rank,y = count,label = pair),nudge_x = 20,nudge_y = 2,size = 4) +
geom_point(color = "darkgreen") +
theme(panel.grid = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size = 1.2),
axis.title = element_text(face = 2,size = 15),
axis.text = element_text(face = 2,size = 12),
legend.title = element_blank())
##统计top10的TRA和TRB扩展细胞数目
TRprod %>% filter(Chain == 'TRA') %>% select(Cell,CDR3aa) -> TRA_num
TRprod %>% filter(Chain == 'TRB') %>% select(Cell,CDR3aa) -> TRB_num
TRA_num_df <- data.frame(sort(table(TRA_num$CDR3aa),decreasing = TRUE)[1:10])
TRB_num_df <- data.frame(sort(table(TRB_num$CDR3aa),decreasing = TRUE)[1:10])
names(TRA_num_df) <- c("TRA","Count")
names(TRB_num_df) <- c("TRB","Count")
##绘图展示
options(repr.plot.width = 20, repr.plot.height = 6)
ggplot(data = TRA_num_df,aes(x = TRA,y = Count)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme(axis.title = element_text(face = 2,size = 20),
axis.text = element_text(face = 2,size = 14),
axis.line = element_line(size = 1.2),
panel.grid = element_blank(),
panel.background = element_blank(),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
coord_flip() +
scale_y_continuous(expand = c(0.01,0.01)) +
scale_x_discrete(expand = c(0.01,0.01),breaks = rev(TRA_num_df$TRA),labels = rev(TRA_num_df$TRA),limits = rev(TRA_num_df$TRA)) +
labs(x = '',y = 'Number of Cells',title = "Top 10 TCRα CDR3 by Cell Count") -> p1
ggplot(data = TRB_num_df,aes(x = TRB,y = Count)) +
geom_bar(stat = "identity",fill = "steelblue",color = "grey40") +
theme(axis.title = element_text(face = 2,size = 20),
axis.text = element_text(face = 2,size = 14),
axis.line = element_line(size = 1.2),
panel.grid = element_blank(),
panel.background = element_blank(),
title = element_text(face = 2,size = 16),
plot.margin = margin(40,40,40,40)) +
coord_flip() +
scale_y_continuous(expand = c(0.01,0.01)) +
scale_x_discrete(expand = c(0.01,0.01),breaks = rev(TRB_num_df$TRB),labels = rev(TRB_num_df$TRB),limits = rev(TRB_num_df$TRB)) +
labs(x = '',y = 'Number of Cells',title = "Top 10 TCRβ CDR3 by Cell Count") -> p2
p1|p2
7.5 不同链克隆分布¶
##子集提取
IGprod %>% filter(Chain == "IGH") %>% select(Cell) -> IGHcell
IGprod %>% filter(Chain == "IGK") %>% select(Cell) -> IGKcell
IGprod %>% filter(Chain == "IGL") %>% select(Cell) -> IGLcell
IGHobj <- SubsetSTData(IG_semla,spots = unique(IGHcell[,1]))
IGKobj <- SubsetSTData(IG_semla,spots = unique(IGKcell[,1]))
IGLobj <- SubsetSTData(IG_semla,spots = unique(IGLcell[,1]))
##绘图
options(repr.plot.width = 20, repr.plot.height = 5)
MapFeatures(IGHobj, pt_size = 1.5,pt_alpha = 1,colors = c("yellowgreen","yellowgreen"),override_plot_dims = TRUE,features = "nFeature_Spatial") + NoLegend() + labs(title = "IgH Clone") -> p1
MapFeatures(IGKobj, pt_size = 1.5,pt_alpha = 1,colors = c("yellowgreen","yellowgreen"),override_plot_dims = TRUE,features = "nFeature_Spatial") + NoLegend() + labs(title = "IgK Clone") -> p2
MapFeatures(IGLobj, pt_size = 1.5,pt_alpha = 1,colors = c("yellowgreen","yellowgreen"),override_plot_dims = TRUE,features = "nFeature_Spatial") + NoLegend() + labs(title = "IgL Clone") -> p3
p1|p2|p3
##子集提取
TRprod %>% filter(Chain == "TRA") %>% select(Cell) -> TRAcell
TRprod %>% filter(Chain == "TRB") %>% select(Cell) -> TRBcell
TRAobj <- SubsetSTData(TR_semla,spots = unique(TRAcell[,1]))
TRBobj <- SubsetSTData(TR_semla,spots = unique(TRBcell[,1]))
##绘图
options(repr.plot.width = 20, repr.plot.height = 5)
MapFeatures(TRAobj, pt_size = 1.5,pt_alpha = 1,colors = c("yellowgreen","yellowgreen"),override_plot_dims = TRUE,features = "nFeature_Spatial") + NoLegend() + labs(title = "TCRα Clone") -> p1
MapFeatures(TRBobj, pt_size = 1.5,pt_alpha = 1,colors = c("yellowgreen","yellowgreen"),override_plot_dims = TRUE,features = "nFeature_Spatial") + NoLegend() + labs(title = "TCRβ Clone") -> p2
p1|p2