-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_sc_ZhangTNBC2021_v1.R
171 lines (134 loc) · 6.84 KB
/
prepare_sc_ZhangTNBC2021_v1.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
#### -------------------------------------------------------------------------------
#### created on 12 apr 2023, 04:40pm
#### author: dhrubas2
#### -------------------------------------------------------------------------------
.wpath. <- "/Users/dhrubas2/OneDrive - National Institutes of Health/Projects/TMEcontribution/analysis/submission/Code/analysis/"
.mpath. <- "miscellaneous/r/miscellaneous.R"
setwd(.wpath.) # current path
source(.mpath.)
library(Matrix)
fcat <- function(...) cat(paste0(glue::glue(...), "\n")) # f-string print akin to python
cat("\014") # clears console
#### -------------------------------------------------------------------------------
## get data & annotations.
data.paths <- c("../data/SC_data/ZhangTNBC2021/",
"../data/TransNEO/use_data/")
data.files <- c("TNBC_scExp_resp_pre_ZhangEtAl2021.RDS",
"TNBC_scData_ZhangEtAl2021.xlsx",
"gene_length_ensembl.grch37.87_SRD_17Mar2022.txt")
tnbc.data <- readRDS(paste0(data.paths[1], data.files[1]))
## use TransNEO annotations (hg19) to filter protein-coding genes.
gene.annot <- read.table(paste0(data.paths[2], data.files[3]), sep = "\t",
header = T, as.is = T)
gene.annot.pc <- gene.annot %>% filter(gene_biotype == "protein_coding") %>%
(function(df) df[!duplicated(df$gene_name), ]) %>% `rownames<-`(.$gene_name)
## patient/sample lists by treatment + sample type.
sample.type <- "tissue" # use "tissue" samples (other option: "blood")
sample.type <- sample.type %>% substr(1, 1) %>% `names<-`(sample.type)
sc.samples <- tnbc.data$patients$Treatment %>% unique %>%
sapply(simplify = F, function(trt){
tnbc.data$patients %>% filter(Treatment == trt, Origin == sample.type) %>%
.$Sample.id
})
## rename cell types to match with TransNEO.
## B cell: B-cells, T cell: T-cells, Myeloid cell: Myeloid, ILC cell: ILC
cells.new <- tnbc.data$cells %>% sapply(function(ctp){
strsplit(ctp, split = " ")[[1]] %>% (function(x){
ifelse((x[1] %>% str_length) == 1,
yes = paste(x[1], "cells", sep = "-"), # B cell / T cell
no = x[1])
})
})
#### -------------------------------------------------------------------------------
## relevant functions.
## rename cell types to match with deconvolved data.
rename.cell.type <- function(ctp.list){
ctp.list.new <- ctp.list
for (ctp in cells.new %>% names){
ctp.list.new <- ctp.list.new %>%
gsub(pattern = ctp, replacement = cells.new[ctp])
}
ctp.list.new
}
## SC data is from 10X - in log(1 + x) format, where sum(x) = 1e4.
## reverse the log-transform & multiply by 100 for TPM equivalence.
tpm.from.10x <- function(expr.10x){
expr.10x.tpm <- ((exp(expr.10x) - 1) * 100) %>% as.matrix %>% as.data.frame
expr.10x.tpm
}
#### -------------------------------------------------------------------------------
## get chemotherapy data.
treat <- "Chemo"
samples.cm <- sc.samples[[treat]]
annot.cm <- tnbc.data$annot %>%
filter(Treatment == treat, Origin == sample.type) %>%
mutate(Cell.type = .$Cluster_org %>% rename.cell.type)
pb <- ProgressBar(N = tnbc.data$cells %>% length)
sc.exp.cm <- tnbc.data$cells %>% sapply(simplify = F, function(ctp){
pb$tick()
## get relevant cell ids.
annot.ctp <- annot.cm %>% filter(Cluster_org == ctp)
cells.ctp <- samples.cm %>% lapply(function(smpl){
annot.ctp %>% filter(Sample.id == smpl) %>% .$Cell.id # cell ids per patient sample
}) %>% Reduce(f = union)
## keep protein-coding genes only.
sc.exp.ctp <- tnbc.data$tissue.norm[[ctp]] # use normalized sc exp
genes.ctp <- sc.exp.ctp %>% rownames %>% intersect(., gene.annot.pc$gene_name)
sc.exp.ctp <- sc.exp.ctp[genes.ctp, cells.ctp] %>% tpm.from.10x # convert to tpm
sc.exp.ctp
})
names(sc.exp.cm) <- cells.new
#### -------------------------------------------------------------------------------
## get immunotherapy data.
treat <- "Anti-PD-L1+Chemo"
samples.im <- sc.samples[[treat]]
annot.im <- tnbc.data$annot %>%
filter(Treatment == treat, Origin == sample.type) %>%
mutate(Cell.type = .$Cluster_org %>% rename.cell.type)
pb <- ProgressBar(N = tnbc.data$cells %>% length)
sc.exp.im <- tnbc.data$cells %>% sapply(simplify = F, function(ctp){
pb$tick()
## get relevant cell ids.
annot.ctp <- annot.im %>% filter(Cluster_org == ctp)
cells.ctp <- samples.im %>% lapply(function(smpl){
annot.ctp %>% filter(Sample.id == smpl) %>% .$Cell.id # cell ids per patient sample
}) %>% Reduce(f = union)
## keep protein-coding genes only.
sc.exp.ctp <- tnbc.data$tissue.norm[[ctp]] # use normalized sc exp
genes.ctp <- sc.exp.ctp %>% rownames %>% intersect(., gene.annot.pc$gene_name)
sc.exp.ctp <- sc.exp.ctp[genes.ctp, cells.ctp] %>% tpm.from.10x
sc.exp.ctp
})
names(sc.exp.im) <- cells.new
#### -------------------------------------------------------------------------------
## save data.
svdat <- F # set T to save data
if (svdat){
## save chemo data.
out.file <- glue("TNBC_scExp_{sample.type %>% names}_pre_Chemo_ZhangEtAl2021.tsv")
pb <- ProgressBar(N = cells.new %>% length)
cells.new %>% plyr::l_ply(function(ctp){
pb$tick()
out.file.ctp <- out.file %>%
gsub(pattern = ".tsv", replacement = glue("_{ctp}.tsv"))
write.table(sc.exp.cm[[ctp]], file = paste0(data.paths[1], out.file.ctp),
sep = "\t", col.names = T, row.names = T)
})
# WriteXlsx(sc.exp.cm, file.name = paste0(data.paths[1], out.file))
## save anti-PD-L1+chemo data.
out.file <- glue("TNBC_scExp_{sample.type %>% names}_pre_Anti-PD-L1_Chemo_ZhangEtAl2021.tsv")
pb <- ProgressBar(N = cells.new %>% length)
cells.new %>% plyr::l_ply(function(ctp){
pb$tick()
out.file.ctp <- out.file %>%
gsub(pattern = ".tsv", replacement = glue("_{ctp}.tsv"))
write.table(sc.exp.im[[ctp]], file = paste0(data.paths[1], out.file.ctp),
sep = "\t", col.names = T, row.names = T)
})
# WriteXlsx(sc.exp.im, file.name = paste0(data.paths[1], out.file))
## save annotations.
out.file <- glue("TNBC_scAnnot_{sample.type %>% names}_pre_ZhangEtAl2021.xlsx")
out.obj <- list("Chemo" = annot.cm, "Anti-PD-L1_Chemo" = annot.im)
WriteXlsx(out.obj, file.name = paste0(data.paths[1], out.file),
col.names = T, row.names = F)
}