-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransgender-gdTCR.R
243 lines (155 loc) · 7.32 KB
/
Transgender-gdTCR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Transgender ------------------------------------------------------------
library(Seurat)
library(ggplot2)
library(purrr)
library(dplyr)
library(Matrix)
library(ggplot2)
library(tibble)
library(cowplot)
library(ggrastr)
library(ggrepel)
library(xlsx)
library(stringr)
library(magrittr)
library(openxlsx)
library(harmony)
source('/home/big/tanlikai/script/rscripts/funcs.r')
file.edit('/home/big/tanlikai/script/rscripts/funcs.r')
# set working dr ----------------------------------------------------------
setwd('/home/big/tanlikai/Transgender/')
TG_Casar <- readRDS('all_samples_cca_ds_subclustering_tcr.rds')
[email protected]$bc_backup <- rownames([email protected])
TG
# creat project -----------------------------------------------------------
dir_all <- list.files('cellranger_out/')
# TCR library
dir_TCR <- str_subset(dir_all, 'tcr')
# gdTCR library
dir_gdTCR<- str_subset(dir_all, 'gdTCR')
# gene expression library
dir_GEX <- str_subset(dir_all, 'tcr|gdTCR', negate = T)
samplesheet <- data.frame(sample = dir_GEX, donor = str_extract(dir_GEX, 'sample0\\d\\d'), tp = str_extract(dir_GEX, '\\w\\w$'))
TG_Casar$bc_backup
# introduce gdTCR ---------------------------------------------------------
dir_gdTCR
raw_gdTCR <- map(dir_gdTCR, ~ read.csv(paste0('cellranger_out/',.x, '/outs/all_contig_annotations.csv' ))%>%
filter(productive == 'True'& is_cell == 'True'& grepl('GV|DV', v_gene)) %>%
dplyr::select(c(1, 5:10, 13,14)) %>%
mutate(
bc_backup = paste0(gsub('_gdTCR', '_', .x), gsub('-1', '', barcode)))%>% dplyr::select(-barcode)
)%>% reduce(.f = rbind)
raw_gdTCR
TRGs <- raw_gdTCR %>% filter(grepl('GV', v_gene))%>% distinct(bc_backup, .keep_all = T) %>%
# mutate(v_gene = str_remove(v_gene,'DV\\d')) %>%
rename_at(vars(-bc_backup), funs(sub('$','_TRG',.)))
TRGs
TRDs <- raw_gdTCR %>% filter(grepl('DV', v_gene))%>% distinct(bc_backup, .keep_all = T) %>%
rename_at(vars(-bc_backup), funs(sub('$','_TRD',.))) %>%
mutate(v_gene_TRD = str_remove(v_gene_TRD, 'AV\\d\\d'))%>%
mutate(v_gene_TRD = str_remove(v_gene_TRD, '-2'))
TRDs
TCRs <- full_join(TRGs,TRDs, by = c('bc_backup'))
TCRs %<>% mutate(paired =
case_when(!is.na(v_gene_TRD) & !is.na(v_gene_TRG) ~
paste0(str_remove(v_gene_TRG,'TR'),' ',str_remove(v_gene_TRD,'TR'))))
TCRs %<>% mutate(cdr3_paired =
case_when(!is.na(v_gene_TRD) & !is.na(v_gene_TRG) ~
paste(str_remove(v_gene_TRG,'TR'),cdr3_TRG,str_remove(v_gene_TRD,'TR'),cdr3_TRD)))
top9paired <- TCRs %>% filter(!is.na(paired)) %>%count(paired) %>%
arrange(desc(n)) %>% top_n(9,n) %>% pull(paired)
TCRs<- TCRs %>%
mutate(paired_sp =
case_when(paired %in% top9paired ~ paired,
!is.na(paired) ~ "Other paired") ) %>%
mutate(paired_sp = factor(paired_sp, levels = c(top9paired, "Other paired")))
# paired TRG and J
TCRs<- TCRs %>% mutate(TRGJP = case_when(!is.na(chain_TRG) ~ paste(v_gene_TRG, j_gene_TRG)))%>%
mutate(TRGJP = str_replace(TRGJP, ' TRG',' '))
[email protected] %<>% left_join(TCRs, by = 'bc_backup', suffix = c('','')) %>% `rownames<-`([email protected]$bc_backup )
TCRs
[email protected] %<>% mutate(TCR_ab_gd = case_when(
!is.na(CTgene)& ( !is.na(chain_TRD ) | !is.na(chain_TRG )) ~ 'dual-TCR',
!is.na(CTgene) ~ 'abTCR',
!is.na(chain_TRD ) | !is.na(chain_TRG ) ~ 'gdTCR'
))
DimPlot(TG_Casar, group.by = 'TCR_ab_gd')
# only gdT ----------------------------------------------------------------
TG_gdT <- subset(TG_Casar, TCR_ab_gd == 'gdTCR')
TG_gdT$S.Score
TG_gdT %<>% ScaleData(vars.to.regress = c("percent.mito", "patient",'percent.ribo','S.Score', 'G2M.Score' ))
dim(TG_gdT)
TG_gdT %<>% RunPCA(npcs= 100, nfeatures.print = 40) %>% JackStraw(dims = 80) %>%
ScoreJackStraw(dims = 1:80)
JackStrawPlot(TG_gdT, dims = 1:80 )%T>%
figsave('Transgender_gdt.jackstraw.pdf' , w = 400, h = 400,device = 'pdf')
# UMAP and clustering -----------------------------------------------------
TG_gdT <- RunUMAP(TG_gdT, dims = 1:37,
reduction = 'pca', min.dist = 0.2) %>%
FindNeighbors(dims = c(1:28))
DimPlot(TG_gdT,group.by = 'condition')
TRDmapp <- [email protected] %>% count(orig.ident, v_gene_TRD) %>% group_by(orig.ident) %>%
mutate(percent = n/sum(n)*100)
TRGmapp <- [email protected] %>% count(orig.ident,v_gene_TRG) %>% group_by(orig.ident) %>%
mutate(percent = n/sum(n)*100)
mappedTCR <- map2(list(TRDmapp, TRGmapp), list('v_gene_TRD','v_gene_TRG'),~
ggplot(.x, aes_string('orig.ident', y = "n", color = .y, fill = .y))+
geom_bar(stat = 'identity', width = 0.5,position = position_stack(reverse = T))+
color_m()+
fill_m()+
xlab(NULL)+
ylab('%')+
ggtitle(.y)+
guides(color = F, fill = guide_legend(nrow =4, title = NULL))+
theme_minimal()+
mytheme+gglp('r')
)
# TCR delta sharing between two time points
TCRD_freq <- [email protected] %>% group_by(patient) %>% count(cdr3_TRD) %>% `colnames<-`(c('patient', 'cdr3_TRD','TRD_freq'))
TCRD_percent <- [email protected] %>% group_by(orig.ident) %>% count(cdr3_TRD) %>% mutate(TRD_perc = n/sum(n)*100 ) %>% select(-n)
sum(TCRD_percent$TRD_perc)
TCRD_freq
[email protected] %<>% left_join(TCRD_freq, c('patient', 'cdr3_TRD') , suffix = c('', '') ) %>% `rownames<-`(TG_gdT$bc_backup)
[email protected] %<>% left_join(TCRD_percent, c('orig.ident', 'cdr3_TRD') , suffix = c('', '') ) %>% `rownames<-`(TG_gdT$bc_backup)
FeaturePlot(TG_gdT, 'TRD_perc',split.by = 'condition')
TCRD_condition <- [email protected] %>%filter(!is.na(v_gene_TRD) &TRD_freq>1) %>%
group_by(condition, patient) %>% count(cdr3_TRD) %>% mutate(percent = n/sum(n)*100)%>%
arrange(desc(percent))
library(ggalluvial)
ggplot(data = TCRD_condition,
aes( x = condition,
y = percent, fill = cdr3_TRD, stratum= cdr3_TRD,
alluvium = cdr3_TRD))+
# ggtitle(paste0('GV9DV2 TCR'))+
geom_flow(stat = "alluvium",size = 0.2,
color = "darkgray") +
scale_y_continuous(breaks = seq(0,100,20), limits = c(0,100))+
theme_minimal_hgrid()+
# scale_fill_manual(values = set_sample(ggplotColours(45)))+
geom_stratum(size = 0.1)+
xlab(NULL) +ylab("% of repertoire")+mytheme+
theme(axis.text.x = element_text(angle = 45,size =8, vjust = 0.5))+
theme(legend.position = 'none')+
facet_grid(~patient)
# phenotypes --------------------------------------------------------------
for (i in seq(0.5,0.9,0.1) %>% rev()) {
TG_gdT <- FindClusters(TG_gdT, resolution = i)
}
TG_gdT$integrated_snn_res.0.9
Feature_rast(TG_gdT,g= 'integrated_snn_res.0.9', facets = c( 'patient', 'condition'))
Feature_rast(TG_gdT,g= paste0('integrated_snn_res.0.',5:9))
Idents(TG_gdT) <- TG_gdT$integrated_snn_res.0.5
table(TG_gdT$patient, TG_gdT$condition)
table(TG_Casar$patient, TG_Casar$condition)
DefaultAssay(TG_gdT) <- 'RNA'
ClusterCompare(TG_gdT, '1', '4',min.pct = 0.1, log2fc = 0.25)
TG_gdT %<>% ScaleData( vars.to.regress = c(
"percent.mito",
"S.Score",
'G2M.Score',
"patient",
'nCount_RNA',
'nFeature_RNA' ))
ClusterCompare(TG_gdT, group.by = 'condition', id1 = 'BL', id2 = 'V2')