-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransgender.R
202 lines (124 loc) · 6.26 KB
/
Transgender.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
# 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')
Feature_rast(TG_Casar, 'cloneType')
[email protected]$bc_backup <- rownames([email protected])
[email protected] %>% group_by(orig.ident) %>% slice(1L) %>% pull(bc_backup)
colnames([email protected] )
[email protected] %>% head
[email protected] %<>% mutate(TCR = case_when(
!is.na(CTgene) ~ 'abTCR'
))
Feature_rast(TG_Casar, 'TCR')
# 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$'))
# 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'
))
Feature_rast(TG_Casar, 'TCR_ab_gd')
[email protected]$nFeature_RNA
ViolinPlot(TG_Casar, 'nFeature_RNA', group.by = 'TCR_ab_gd')
# gene expression ---------------------------------------------------------
# read raw gene expression data
RAW_GEX <- map(dir_GEX, ~ Read10X_h5(paste0('cellranger_out/', .x,'/outs/filtered_feature_bc_matrix.h5')) ) %>% setNames(dir_GEX)
names(RAW_GEX)
names(RAW_GEX$sample001_BL)
# [1] "Gene Expression" "Antibody Capture"
RAW_GEX$sample001_BL$`Gene Expression` %>% str()
map(RAW_GEX, ~ dim(.x$`Gene Expression`))
RAW_GEX$sample001_BL$`Antibody Capture` %>% str()
rownames(RAW_GEX$sample001_BL$`Antibody Capture`)
# generate Seurat project ------------------------------------------------
TG_pos_inte <- map2(RAW_GEX, dir_GEX, ~ CreateSeuratObject(counts = .x$`Gene Expression`, project = .y, min.features =30) %>%
AddMetaData(col.name = c('donor', 'tp'), metadata = c(str_extract(dir_GEX, 'sample0\\d\\d'), str_extract(dir_GEX, '\\w\\w$')) ))
str(TG_pos_inte$sample001_BL)
summary(TG_pos_inte$sample001_BL)
for (x in dir_GEX) {
TG_pos_inte[[x]][['CITE']] <- CreateAssayObject(RAW_GEX[[x]]$`Antibody Capture`)
}
# QC ----------------------------------------------------------------------
# calculate mitochonrial, ribosomal, and hispa gene content
TG_pos_inte <- map(TG_pos_inte, ~ PercentageFeatureSet(.x, '^MT', col.name = 'percent.mito') %>%
PercentageFeatureSet('^RP', col.name = 'percent.ribo') %>%
PercentageFeatureSet('^HSPA', col.name = 'percent.hspa') )
dir.create('figs')
QCvio <- map(TG_pos_inte, ~ViolinPlot(.x,c("nFeature_RNA", "nCount_RNA",'percent.mito','percent.ribo'),
colors ='blue' ,box = T, jitter = T , ncol = 4 )) %>%
PG(labels = dir_GEX, ncol = 2) %T>%
figsave('beforeQC_violin.pdf', 220, 270)
QCvio
QC_scatter <- map(TG_pos_inte, ~ Feature_rast(.x, g = 'percent.mito', d1 ="nCount_RNA",d2 ='nFeature_RNA',
noaxis = F, axis.number = T)+grd+
geom_smooth(method = "lm")+
scale_x_continuous(breaks = seq(0, 15000, 1000), limits = c(0,15000))+
scale_y_continuous(breaks = seq(0, 3000, 500), limits = c(0,3000))+
geom_hline(yintercept = c(400,2000))+geom_vline(xintercept = c(1200,7000))) %>%
PG(labels = dir_GEX)%T>%
figsave('beforeQC_scatter.pdf',600,400)
QC_scatter