1 前言¶

本次分析所用数据均是来自于vdjMatrixPB-流程说明的结果目录。

2 R包载入¶

In [44]:
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 数据读取¶

In [2]:
##读取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")
In [3]:
##查看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 颜色配置¶

In [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的对象

In [5]:
##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 

In [6]:
##查看HE图图像
options(repr.plot.width = 7, repr.plot.height = 7)
ImagePlot(IG_semla)
No description has been provided for this image

5.2 克隆型整体空间展示¶

In [8]:
##案例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
No description has been provided for this image
In [9]:
##案例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
No description has been provided for this image

5.3 特定克隆型展示¶

In [10]:
##查看克隆型
rownames(IG)[1:10]
rownames(TR)[1:10]
  1. 'IGH-CARVPYNSGWDVYWYFDLW'
  2. 'IGH-CRGRAYSGYDYGVDTFDIW'
  3. 'IGL-CSSYSGSNNRVF'
  4. 'IGH-CAASGFGYGDHDWFAPW'
  5. 'IGH-CARDLDTSSWFEPGYW'
  6. 'IGH-CARDRPGDYW'
  7. 'IGH-CARVGLSGTYFNFFDYW'
  8. 'IGH-CARVHGNGWWDW'
  9. 'IGH-CVKDTSVIPTVPGLFSSW'
  10. 'IGH-CVKGGRGTPIDSW'
  1. 'TRA-CAAKTGGGADGLTF'
  2. 'TRA-CIVRSLGSSNTGKLIF'
  3. 'TRB-CASSEATDAANTGELFF'
  4. 'TRA-CAASGGGTASKLTF'
  5. 'TRA-CAFMNPNNAGNMLTF'
  6. 'TRA-CAMREGRGGSYIPTF'
  7. 'TRB-CATWTGGGGQPQHF'
  8. 'TRB-CASSLGRIEQFF'
  9. 'TRB-CASSRDRNSQPQHF'
  10. 'TRB-CASVGLLLEQYF'
In [11]:
##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")
In [12]:
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)
No description has been provided for this image
In [13]:
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)
No description has been provided for this image

组织展示IG/TR克隆型数量的空间分布,可以看出,IG呈现成簇分布,TR呈现分散分布

5.4 克隆型数量分布统计¶

In [14]:
##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
A data.frame: 5 × 2
typevalues
<chr><int>
IGH2852
IGK 611
IGL 715
TRA 289
TRB 772
In [15]:
##绘图展示
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()
No description has been provided for this image

5.5 Isotype 分布统计¶

同种型(isotype)指的是免疫球蛋白重链(Ig heavy chain)的恒定区域,包括IGHA、IGHD、IGHE、IGHG和IGHM。这些恒定区域可以被交换,以赋予抗体多样化的效应功能,同时保持对抗原的特异性。 Isotype 反映了免疫球蛋白的不同类别,即IgA、IgD、IgE、IgG和IgM。每种类别的免疫球蛋白都有其独特的生物学功能,这些功能由重链的恒定区域决定,因此也决定了免疫球蛋白的效应功能。

In [16]:
##数据读取
Isotype <- read.table("Result/03.Matrix/YF-XG.IG/L13/YF-XG.L13.Productive.addCgene.xls",header = FALSE)
head(Isotype)
A data.frame: 6 × 6
V1V2V3V4V5V6
<chr><chr><chr><int><chr><chr>
1L13_1000CAGGATAGTGGCIGLC1CSSYSGSNNRVF TGCAGCTCATATTCAGGCAGCAACAATCGGGTGTTC
2L13_1000GTTTTCAGAAGGIGHG1CARVPYNSGWDVYWYFDLWTGTGCGAGAGTTCCATATAACAGTGGCTGGGACGTCTACTGGTACTTCGATCTCTGG
3L13_1000TCGAACAGTCAGIGHM1CRGRAYSGYDYGVDTFDIWTGCCGTGGGAGGGCCTATAGTGGCTACGATTATGGGGTTGATACTTTTGATATCTGG
4L13_1002AATGTTGACAAAIGHG1CAASGFGYGDHDWFAPW TGTGCGGCTTCCGGATTTGGATACGGTGACCACGACTGGTTCGCCCCCTGG
5L13_1002ACATTCACAGGTIGHM1CVRDDDTSSHYSVFNYW TGTGTGAGAGATGATGATACTAGTAGTCATTACTCGGTTTTTAACTACTGG
6L13_1002ACCAGACTAGGCIGHG1CARVGLSGTYFNFFDYW TGTGCGAGAGTCGGATTGAGTGGGACATATTTCAACTTCTTTGACTACTGG
In [17]:
##数量统计整理
Isotypenum <- data.frame(table(Isotype$V3)[1:4])
Isotypenum
A data.frame: 4 × 2
Var1Freq
<fct><int>
IGHA 9003
IGHD 9
IGHG33370
IGHM 1702
In [18]:
##绘图展示
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()
No description has been provided for this image

在这里也可以展示同种型(isotype)的空间分布情况

In [19]:
##提取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")]
In [20]:
##提取子集
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))
In [21]:
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
No description has been provided for this image

5.6 克隆型长度分布统计¶

In [22]:
##数据读取
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)

不区分轻重链和区分轻重链展示

In [23]:
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
No description has been provided for this image
In [24]:
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
No description has been provided for this image

6 immunarch分析¶

R语言包immunarch兼容的数据输入格式: ImmunoSEQ, IMGT, MiTCR, MiXCR, MiGEC, MigMap, VDJtools, tcR, AIRR, 10XGenomics。在这里将克隆型数据整理成vdjtools格式方便快速读取。

本次分析主要参考:immunarch教程文档

6.1 数据格式转换¶

In [4]:
##数据读取并新增列
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 数据读取¶

In [5]:
##数据导入
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 克隆型丰度图¶

In [27]:
##克隆型的相对丰度统计
ig_cnt <- repExplore(IGimmdata$data, .method = "count")
tr_cnt <- repExplore(TRimmdata$data, .method = "count")
In [28]:
options(repr.plot.width = 16, repr.plot.height = 8,warn = -1)
p1 <- vis(ig_cnt)
p2 <- vis(tr_cnt)
p1|p2
No description has been provided for this image

6.4 克隆型的克隆性评估¶

In [29]:
##克隆比例(clonal.prop)方法
ig_pr <- repClonality(IGimmdata$data, .method = "clonal.prop")
tr_pr <- repClonality(TRimmdata$data, .method = "clonal.prop")
ig_pr;tr_pr
A immunr_clonal_prop: 1 × 3 of type dbl
ClonesPercentageClonal.count.prop
IG_L13.clonotype.xls212.10.0004792715
A immunr_clonal_prop: 1 × 3 of type dbl
ClonesPercentageClonal.count.prop
TR_L13.clonotype.xls123.70.0009425071
In [30]:
##稀有方法(Rare method)
ig_rare <- repClonality(IGimmdata$data, .method = "rare")
tr_rare <- repClonality(TRimmdata$data, .method = "rare")
ig_rare;tr_rare
A immunr_rare_prop: 1 × 6 of type dbl
131030100MAX
IG_L13.clonotype.xls0.044658720.074061690.11205150.19035390.37081871
A immunr_rare_prop: 1 × 6 of type dbl
131030100MAX
TR_L13.clonotype.xls0.057704850.11627910.24146920.36742010.53575311
In [31]:
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

No description has been provided for this image

稀有方法(Rare method)是一种评估克隆性的方法,它专注于样本中最不丰富的克隆型。通过分析这些最不丰富的克隆型,可以了解样本中克隆型的多样性和稀有性,从而评估样本的克隆性。

In [32]:
##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
A immunr_homeo: 1 × 4 of type dbl
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.xls0.088596010.17108620.4574440.2828738
A immunr_homeo: 1 × 4 of type dbl
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.xls00.22842860.30732450.4642469
In [33]:
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
No description has been provided for this image

Homeo方法评估克隆空间内稳态,即特定大小的克隆群体在总库中所占的比例。这有助于了解样本中克隆型的分布情况,特别是不同丰度克隆型的相对比例。

  1. Small:小克隆型,频率较低。
  2. Medium:中等克隆型,频率中等。
  3. Large:大克隆型,频率较高。
  4. Hyperexpanded:过度扩增克隆型,频率非常高。

6.5 基因使用计算¶

immunarch包含一个基因片段数据表,其中列出了遵循IMGT命名法的多个物种的已知基因片段。为了获取当前基因的统计数据,可以调用gene_stats() 函数。

In [34]:
gene_stats()
A data.frame: 17 × 20
aliasspeciesighdighjighvigijigkjigkvigljiglvtrajtravtrbdtrbjtrbvtrddtrdjtrdvtrgjtrgv
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
bt BosTaurus 21 4 2501 652646 00 0 0530615
cd CamelusDromedarius 0 0 000 00 0 0 00 0 00072 2
clf CanisLupusFamiliaris 0 0 000 00 0 0 02 8190007 8
dr DanioRerio 7 7 030 00 0 0 00 0 00000 0
hs HomoSapiens 301324805 6476957 6031464346410
macmulMacacaMulatta 24 7 1904 835 0 0 0215580000 0
mmc MusMusculusCastaneus 0 0 000 40 0 0 00 0 00000 0
mmd MusMusculusDomesticus 0 0 000 20 0 0 00 0 00000 0
musmusMusMusculus 32 8225081093 54214521423237011
oa OrnithorhynchusAnatinus 310 000 00 0 0 00 0 00000 0
oc OryctolagusCuniculus 1011 3908 26220 0 00 0 00000 0
om OncorhynchusMykiss 9 7 600 00 0 0 01 9 00000 0
rn RattusNorvegicus 30 4113061322 8 0 00 0 00000 0
smth MusMusculusMolossinus 0 0 000 10 0 0 00 0 00000 0
smth MusMusculusMusculus 0 0 000 10 0 0 00 0 00000 0
smth MusSpretus 0 0 000 20 2 0 00 0 00000 0
ss SusScrofa 5 5 1508 19414 0 00 0 00000 0
In [35]:
##这里演示以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

No description has been provided for this image

Spectratype 是一种用于表示每个序列长度的基因分布的有用方法。immunarch 包中的 spectratype 函数可以实现这一功能。该函数接收一个免疫库或多个免疫库的列表作为输入,并通过 .quant 和 .col 参数控制计算比例的量和选择的列。

In [36]:
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
No description has been provided for this image

6.6 克隆型多样性估计¶

三种公认的生物物种多样性假说。它们包括:(1)异质性假说,(2)竞争假说,(3)捕食假说。
衡量多样性有三个主要原因:

  1. 衡量稳定性,以确定一个环境是否在退化,
  2. 比较两个或更多的环境
  3. 消除对广泛列表的需要(形成数据概览,即用一个多样性指标,说明了需要列举很多概念才能说清楚的东西)。多样性指数提供了一个群落组成的重要信息。这些指数不仅衡量了物种的丰富度,还考虑了物种的相对丰富度或均匀度。在测量物种多样性时,物种丰富度和均匀度必须同时考虑。此外,指数还提供了物种稀有度和共性的重要信息。

repDiversity 函数实现了几种估计免疫库多样性的方法。.method 参数类似于上述函数,用于设置多样性的估计方法。可以选择以下方法:

  1. chao1:Chao1 是一种非参数渐近估计器,用于估计物种丰富度(一个群体中的物种数量)。适用于估计免疫库中不同克隆型的数量,特别是在样本量较小的情况下。
  2. hill:Hill 数字是一个数学上统一的多样性指数家族,通过不同的指数 q 来区分。
  3. div:真实多样性,或有效类型数,指的是在数据集中观察到的平均比例丰富度所需的同等丰富的类型数。用于评估免疫库中不同克隆型的实际多样性,考虑了克隆型的丰度差异。
  4. gini.simp:Gini-Simpson 指数是异种相遇的概率,即两个实体代表不同类型的概率。用于评估免疫库中克隆型的多样性,特别是在克隆型分布不均匀的情况下。
  5. inv.simp:逆 Simpson 指数是通过加权算术平均数来量化数据集中类型的比例丰富度时得到的有效类型数。用于评估免疫库中克隆型的多样性,特别适用于克隆型分布较为集中时。
  6. gini:Gini 系数衡量频率分布(例如收入水平)中的不平等程度。Gini 系数为零表示完全平等,所有值都相同。Gini 系数为一(或100%)表示值之间的最大不平等。用于评估免疫库中克隆型分布的不平等程度,帮助理解克隆型的主导情况。
  7. raref:稀释法是一种通过外推采样结果来评估物种丰富度的技术。用于评估免疫库中克隆型的丰富度,特别是在样本量较小或采样不完全时。
  8. dXX 是一个与 d50 指数类似的指标,其中 XX 对应于总测序读数的期望百分比。这个指标用于评估免疫库的多样性,特别是在测序数据中不同克隆型的分布情况。
  9. d50 指数是指在总测序读数中,累积读数达到 50% 时所需的克隆型数量。这个指标反映了免疫库中克隆型的分布情况,数值越小,表示少数克隆型占据了大部分读数,多样性较低;数值越大,表示克隆型分布较为均匀,多样性较高。
In [37]:
##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%
No description has been provided for this image
In [38]:
##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%
No description has been provided for this image

6.7 kmer分析¶

In [39]:
##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
No description has been provided for this image

6.8 序列基序分析¶

  1. 位置频率矩阵(PFM),每个位置上每种氨基酸出现的次数的矩阵。PFM 用于统计每个位置上每种氨基酸的出现频率,是基序分析的基础。
  2. 位置概率矩阵(PPM),每个位置上每种氨基酸出现的概率的矩阵。PPM 通过将 PFM 中的频率转换为概率,提供了更直观的基序表示,便于进一步分析。
  3. 位置权重矩阵(PWM),PPM 元素的对数似然比的矩阵。PWM 通过计算对数似然比,强调了某些氨基酸在特定位置上的重要性,常用于基序搜索和匹配。
  4. 自信息矩阵,PWM 中每个元素的自信息的矩阵。自信息矩阵用于评估每个位置上每种氨基酸的信息含量,帮助识别关键的基序位置。
In [40]:
##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 immunr_kmer_profile_pfm: 20 × 5 of type dbl
A45814606249824612242
C4402 585 479 512 495
D28842954495850054222
E 967100411241099 826
F10822391249525893717
G54145584580556885190
H11631280134014171221
I 9091062108613871365
K 876 883 893 558 495
L18082216234324782384
M 480 730 629 672 665
N15561639169816961669
P17082274235126142503
Q186918981400 890 796
R41064077413423782313
S51025299500547164537
T27102918273330332963
V23202476236332072988
W15111547186518434194
Y41564181440553614819
In [41]:
##绘图
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
p1 <- vis(IG1)
p2 <- vis(IG1, .plot = "seq")
p1|p2
No description has been provided for this image
In [42]:
##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 immunr_kmer_profile_pfm: 20 × 5 of type dbl
A13831402 626 551 531
C1017 33 34 31 32
D 414 422 426 429 416
E 293 642 637 626 616
F 124 125 152 4321399
G16761791178917481696
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
S1835183317821193 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
In [43]:
##绘图
options(repr.plot.width = 20, repr.plot.height = 12,warn = -1)
p1 <- vis(TR1)
p2 <- vis(TR1, .plot = "seq")
p1|p2
No description has been provided for this image

6.9 VJ 桑基图¶

In [44]:
##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()
No description has been provided for this image
In [53]:
##整理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.
In [56]:
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))
                       })
No description has been provided for this image
In [59]:
##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()
No description has been provided for this image
In [57]:
##整理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.
In [58]:
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))
                       })
No description has been provided for this image

7 细胞水平分析¶

本次分析主要参考文章:Single cell resolved spatial immune repertoire unveils spatial heterogeneity of lymphoid aggregates in human immune disorders

7.1 颜色配置¶

In [47]:
##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)
No description has been provided for this image

7.2 整体展示¶

In [48]:
##细胞水平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 

In [49]:
##细胞水平全局展示
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
No description has been provided for this image

7.3 VDJ基因数量统计¶

IMGT ,关于人类免疫组库,功能基因其中IG有V基因229个,D基因37个,J基因18个,C基因,14个;TR有V基因127个,D基因5个,J基因84个,C基因3个。

In [50]:
##读取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)
In [51]:
##统计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
In [52]:
##构建数据框
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))
In [53]:
##绘图展示
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
No description has been provided for this image

7.4 Pair统计¶

In [54]:
##细胞水平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")
In [55]:
##统计
##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])
In [56]:
##韦恩图
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
No description has been provided for this image
In [57]:
##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)
A data.frame: 6 × 3
paircountrank
<fct><int><int>
1CAMREGRGGSYIPTF@TRA,CATWTGGGGQPQHF@TRB 371
2CAAKTGGGADGLTF@TRA,CATWTGGGGQPQHF@TRB 192
3CAFMNPNNAGNMLTF@TRA,CATWTGGGGQPQHF@TRB 103
4CAMREGRGGSYIPTF@TRA,CASSLGGIEQFF@TRB 94
5CAMREGRGGSYIPTF@TRA,CASSPRGRVGDTEAFF@TRB 85
6CAAKTGGGADGLTF@TRA,CASSPRGRVGDTEAFF@TRB 56
In [58]:
##默认展示前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())
No description has been provided for this image
In [59]:
##统计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")
In [60]:
##绘图展示
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
No description has been provided for this image

7.5 不同链克隆分布¶

In [61]:
##子集提取
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
No description has been provided for this image
In [62]:
##子集提取
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
No description has been provided for this image