scTCR-seq分析: MIXCR + scRepertoire

rm(list=ls())
#devtools::install_github("ncborcherding/scRepertoire")
# Load the package into R
library(immunarch)  
library(scRepertoire)
library(tidyverse)
library(SingleCellExperiment)
library(Seurat)
read_mixcr_n_trans<- function(file,...){
  df <- read.delim(file)
  # transform the cell id {be consist with RNA data}
  {
    well<- gsub(df$cellId,pattern = "[AGCT]*-",replacement = "")
    hp<- gsub(df$cellId,pattern = "-[AGCT]*-.*",replacement = "")
    rt<- str_extract(df$cellId,pattern = "-[AGCT]{10}")%>%
      str_sub(.,start = 2,end = nchar(.))
  }
  # add a column named "tagValueCELL"  {as normal mixcr output}
  df$tagValueCELL<- paste(well,hp,rt,sep ="_" )
  return(df)
}

# Step1: Load MIXCR output -----------------------------------------

fn1 <- "mixcr_output/xxxxxxxxxxxx.clones.tsv"
fn2 <- "mixcr_output/xxxxxxxxxxxx..clones.tsv"
fn3 <- "mixcr_output/xxxxxxxxxxxx..clones.tsv"

filelist <- c(fn1,fn2,fn3)
samples <- c("A","B","C")

contig_list<- lapply(filelist, function(x) read_mixcr_n_trans(x))
  colnames(contig_list[[1]])
  
# convert to "scRepertoire" style
contig.list <- loadContigs(input = contig_list, 
                           format = "MiXCR")
  names(contig.list)
  colnames(contig.list[[1]])
  head((contig.list[[1]]))
#[1] "barcode" "chain"   "reads"   "v_gene"  "d_gene"  "j_gene"  "c_gene"  "cdr3_nt" "cdr3"
  
combined.TCR <- combineTCR(contig.list, 
                         #  samples = samples, # names of different samples # can be NULL
                           removeNA = FALSE, 
                           removeMulti = FALSE, 
                           filterMulti = FALSE)
names(combined.TCR)
colnames(combined.TCR[[1]])
head((combined.TCR[[1]]))[,1:5]   # sample name pasted to cell_id

# [1] "barcode"  "sample"   "TCR1"     "cdr3_aa1" "cdr3_nt1" "TCR2"     "cdr3_aa2" "cdr3_nt2"
#[9] "CTgene"   "CTnt"     "CTaa"     "CTstrict"

# Basic clone analysis-------------------------------------

## 獨(dú)特克隆數(shù)量----- 
#the total or relative numbers of unique clones.
clonalQuant(combined.TCR, 
            cloneCall="strict", 
            chain = "both", 
            scale = TRUE)
  # 指定自定義分組進(jìn)行統(tǒng)計(jì)
clonalQuant(combined.TCR, cloneCall = "gene", group.by = "sample", scale = TRUE)

##克隆豐度 ----
clonalAbundance(combined.TCR, 
                cloneCall = "gene", 
                scale = FALSE)
#密度圖
clonalAbundance(combined.TCR, cloneCall = "gene", scale = TRUE)

##克隆長(zhǎng)度-----

clonalLength(combined.TCR, 
             cloneCall="aa", #cloneCall can only be “nt” or “aa”
             chain = "both")   # “both” for combined chain visualization
                              # “TRA”, “TRB”, “TRD”, “TRG”, “IGH” or “IGL” to select single chain

clonalLength(combined.TCR, 
             cloneCall="aa", 
             chain = "TRA", 
             scale = TRUE) 

## 克隆比較
# 沖積圖
clonalCompare(combined.TCR, 
              top.clones = 10, 
              samples = samples, 
              # highlight.clones = c("CAYRSGARDDKIIF"),
              cloneCall="aa", 
              graph = "alluvial")
#散點(diǎn)圖
clonalScatter(combined.TCR, 
              cloneCall ="gene", 
              x.axis = samples[1], 
              y.axis = samples[2],
              dot.size = "total",
              graph = "proportion")
clonalScatter(combined.TCR, 
              cloneCall ="gene", 
              x.axis = samples[2], 
              y.axis = samples[3],
              dot.size = "total",
              graph = "proportion")

##克隆穩(wěn)態(tài)
clonalHomeostasis(combined.TCR, 
                  cloneCall = "gene")
## 克隆比例
clonalProportion(combined.TCR, 
                 cloneCall = "gene") 

## TCR cluster
sub_combined <- clonalCluster(combined.TCR[[1]], 
                              chain = "TRA", 
                              sequence = "aa", 
                              threshold = 0.85, 
                              group.by = NULL)
sub_combined$TRA_cluster
#Cluster denotes if the cluster was called using the normalized Levenshtein distance, which takes the edit distance calculated between 2 sequences 
#and divides that by the mean of the sequence lengths.
#Unconnected sequences will have NA values.


##combine with RNA ------
# seurat object after umap :
combined <- readRDS(file = "RNA/Seurat_output/prefiltered_combined.rds")

sce <- Seurat::as.SingleCellExperiment(combined)

sce <- combineExpression(combined.TCR, 
                         sce, 
                         cloneCall="gene", 
                         #group.by = "sample", 
                         proportion = TRUE)

#Define color palette 
colorblind_vector <- hcl.colors(n=7, palette = "inferno", fixup = TRUE)
colnames(colData(sce))
singleCellTK::plotUMAP(sce,colorBy = "cloneSize") +
  scale_color_manual(values=rev(colorblind_vector[c(1,3,5,7)]))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子消痛,更是在濱河造成了極大的恐慌呕臂,老刑警劉巖朴上,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件略荡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡恨胚,警方通過(guò)查閱死者的電腦和手機(jī)脚翘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)灼卢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人来农,你說(shuō)我怎么就攤上這事〕缪撸” “怎么了沃于?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)海诲。 經(jīng)常有香客問(wèn)我繁莹,道長(zhǎng),這世上最難降的妖魔是什么特幔? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任咨演,我火速辦了婚禮,結(jié)果婚禮上蚯斯,老公的妹妹穿的比我還像新娘薄风。我一直安慰自己,他們只是感情好拍嵌,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布遭赂。 她就那樣靜靜地躺著,像睡著了一般横辆。 火紅的嫁衣襯著肌膚如雪撇他。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 48,970評(píng)論 1 284
  • 那天狈蚤,我揣著相機(jī)與錄音困肩,去河邊找鬼。 笑死脆侮,一個(gè)胖子當(dāng)著我的面吹牛锌畸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播他嚷,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼蹋绽,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼芭毙!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起卸耘,我...
    開(kāi)封第一講書(shū)人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤退敦,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后蚣抗,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體侈百,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年翰铡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钝域。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锭魔,死狀恐怖例证,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情迷捧,我是刑警寧澤织咧,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站漠秋,受9級(jí)特大地震影響笙蒙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庆锦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一捅位、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧搂抒,春花似錦艇搀、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至誉帅,卻和暖如春淀散,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蚜锨。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工档插, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人亚再。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓郭膛,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親氛悬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子则剃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容