-
Notifications
You must be signed in to change notification settings - Fork 2
/
TMB_TCRdiversity.Rmd
368 lines (264 loc) · 11.1 KB
/
TMB_TCRdiversity.Rmd
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
---
title: "TCR"
output: html_document
editor_options:
chunk_output_type: console
---
```{r}
miTCR = data.table::fread("Data/mitcr_sampleStatistics_20160714.tsv")
head(miTCR)
#format sampleID
miTCR$SampleID = unlist(lapply(miTCR$SampleBarcode, stringr::str_sub, start = 1, end = 15))
#add patient clinical data
pheno = data.table::fread("Data/tcga_clinical_data.tsv")
miTCR$DiagnosisAge = NA
miTCR$CancerType = NA
miTCR$MutationCount = NA
miTCRSub = miTCR[which(miTCR$SampleID %in% pheno$`Sample ID`)]
for(i in 1:nrow(miTCRSub)){
matched_pheno_ind = which(pheno$`Sample ID` == miTCRSub$SampleID[i])
miTCRSub$DiagnosisAge[i] = pheno$`Diagnosis Age`[matched_pheno_ind]
miTCRSub$CancerType[i] = pheno$`TCGA PanCanAtlas Cancer Type Acronym`[matched_pheno_ind]
miTCRSub$MutationCount[i] = pheno$`Mutation Count`[matched_pheno_ind]
}
#remove observations without known patient age or cancer type
nainds = which(is.na(miTCRSub$DiagnosisAge))
which(is.na(miTCRSub$CancerType))
miTCRSub = miTCRSub[-nainds,]
miTCRSub$CancerType = as.factor(miTCRSub$CancerType)
#normalize number of TCR clones by total TCR reads
miTCRSub$CPR = miTCRSub$numClones/miTCRSub$totTCR_reads
#normalize Shannon by nClones/nTotReads
miTCRSub$NormShannon = miTCRSub$shannon * (miTCRSub$numClones/miTCRSub$totTCR_reads)
miTCRAgeModel = lm(NormShannon~DiagnosisAge, miTCRSub)
summary(miTCRAgeModel)
#we've been only using indviduals 30+ in age for other analyses, due to concerns about patients younger being highly abnormal in some regard that is not easy to account for, so filter those out
miTCRSub = miTCRSub[which(miTCRSub$DiagnosisAge >= 30),]
miTCRAgeModel = lm(NormShannon~DiagnosisAge, miTCRSub)
summary(miTCRAgeModel)
#include cancer type
miTCR2 = lm(NormShannon~DiagnosisAge+CancerType, miTCRSub)
summary(miTCR2)
saveRDS(summary(miTCR2)$coefficients[2,], "Data/TCGA_TMB_LM_Age_term.rds")
jtools::effect_plot(miTCR2, pred = DiagnosisAge, interval = T, plot.points = T)
#plot
miTCRSub$ageGroup = NA
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 30 & miTCRSub$DiagnosisAge < 40)] = "30-39"
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 40 & miTCRSub$DiagnosisAge < 50)] = "40-49"
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 50 & miTCRSub$DiagnosisAge < 60)] = "50-59"
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 60 & miTCRSub$DiagnosisAge < 70)] = "60-69"
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 70 & miTCRSub$DiagnosisAge < 80)] = "70-79"
miTCRSub$ageGroup[which(miTCRSub$DiagnosisAge >= 80 & miTCRSub$DiagnosisAge <= 90)] = "80-90"
miTCRSub$ageGroup =as.factor(miTCRSub$ageGroup)
means = aggregate(miTCRSub$NormShannon~miTCRSub$ageGroup, FUN = mean)
df = data.frame(age = means[1:6,1], PC = means[1:6,2])
library(ggplot2)
ggplot(data = df, aes(x = age, y = PC)) +
geom_col(fill = "steelblue")+
labs(title="TCR Normalized Clonality",
x="Age Group", y = "TCR Normalized Clonality") +
theme_minimal()
dfbox = data.frame(age = miTCRSub$ageGroup, NormClonality = miTCRSub$NormShannon)
ggplot(data = dfbox, aes(x = age, y = NormClonality)) +
geom_boxplot(fill = "steelblue")
```
TCR Prognostic
```{r}
library(survival)
#add survival data
miTCRSub$OSS = NA
miTCRSub$OSM = NA
for(i in 1:nrow(miTCRSub)){
matched_pheno_ind = which(pheno$`Sample ID` == miTCRSub$SampleID[i])
miTCRSub$OSS[i] = pheno$`Overall Survival Status`[matched_pheno_ind]
miTCRSub$OSM[i] = pheno$`Overall Survival (Months)`[matched_pheno_ind]
}
liveinds = which(miTCRSub$OSS == "LIVING")
deadinds = which(miTCRSub$OSS == "DECEASED")
miTCRSub$OSS[liveinds] = 0
miTCRSub$OSS[deadinds] = 1
miTCRSub$OSS = as.numeric(miTCRSub$OSS)
length(which(miTCRSub$OSS == 0 | miTCRSub$OSS == 1))
miTCRSub3 = miTCRSub[-which(is.na(miTCRSub$OSS)),]
#coxph model
coxph(Surv(OSM, OSS)~NormShannon + DiagnosisAge, miTCRSub3)
#include cancer type
coxph(Surv(OSM, OSS)~NormShannon+ DiagnosisAge + strata(CancerType), miTCRSub3) # p = 3.34e-05
#unnormalized shannon
coxph(Surv(OSM, OSS)~shannon + DiagnosisAge + strata(CancerType), miTCRSub3)
#p = 1.92e-05
#nClones/nReads
coxph(Surv(OSM, OSS)~CPR+ DiagnosisAge + strata(CancerType), miTCRSub3)
#p = 0.756
#examine where there has been expansion of TCR clones, indicating recognition of antigen
miTCRSub$Expansion = miTCRSub$CPR < 1
log = glm(Expansion ~ DiagnosisAge, family = binomial(link = "logit"), data = miTCRSub)
summary(log)
#p = 1.28e-04
countByAgeGroup = aggregate(miTCRSub$Expansion~miTCRSub$ageGroup, FUN = sum)
#divide by total
summary(miTCRSub$ageGroup)
barplot(countByAgeGroup[,2]/summary(miTCRSub$ageGroup))
library(ggplot2)
ggplot(as.data.frame(countByAgeGroup[,2]/summary(miTCRSub$ageGroup)), aes(x=names(summary(miTCRSub$ageGroup)), y=countByAgeGroup[,2]/summary(miTCRSub$ageGroup)))+
geom_bar(stat="identity", fill = "steelblue")+
labs(title="Proportion of TCR Clonal Expansion by Age", x="Age Group", y = "TCR Expansion Proportion")+
theme_classic()
#what if you don't include the 30-39 year olds
no3039 = miTCRSub[-which(miTCRSub$ageGroup == "30-39"),]
log2 = glm(Expansion ~ DiagnosisAge, family = binomial(link = "logit"), data = no3039)
summary(log2)
#no longer significant
#cox
miTCRSub3$Expansion = miTCRSub3$CPR < 1
coxph(Surv(OSM, OSS)~Expansion + DiagnosisAge + strata(CancerType), miTCRSub3)
#this small an effect on a binary characterstic is essentially nothing
```
TCR by cancer type
```{r}
summary(miTCRSub$CancerType)
lmList = list()
cancerTypes= list()
for(i in seq_along(levels(miTCRSub$CancerType))) {
cancerObs = miTCRSub[which(miTCRSub$CancerType == levels(miTCRSub$CancerType)[i]),]
lm = lm(NormShannon~DiagnosisAge, cancerObs)
print(levels(miTCRSub$CancerType)[i])
print(summary(lm))
lmList[[i]] = lm
cancerTypes[[i]] = levels(miTCRSub$CancerType)[i]
}
unlist(cancerTypes)
pvals = unlist(lapply(lmList, function(x){
if(!is.null(x)){
return(summary(x)$coefficients[2,4])
}
}))
padj = p.adjust(pvals, "BH")
names(padj) = unlist(cancerTypes)
padj
lmAgeTerms = lapply(lmList, function(x){
summary(x)$coefficients[2,]
})
for(i in seq_along(lmAgeTerms)){
x = lmAgeTerms[[i]]
x = c(x,padj[i])
names(x)[5] = "p.adj"
lmAgeTerms[[i]] = x
}
names(lmAgeTerms) = levels(miTCRSub$CancerType)
saveRDS(lmAgeTerms, "TCRLMAgeTerms_TCGA_CT.rds")
effectSizes = unlist(lapply(lmList, function(x){
if(!is.null(x)){
return(summary(x)$coefficients[2,1])
}
}))
effectSizes
lmLogPvals = unlist(lapply(lmList, function(x){
if(!is.null(x)){
return(-log10(summary(x)$coefficients[2,4]))
}
}))
#plot
cancerTypes = levels(miTCRSub$CancerType)
library(ggplot2)
ggplot(as.data.frame(lmLogPvals), aes(x=reorder(cancerTypes, -lmLogPvals), y=lmLogPvals))+
geom_bar(stat="identity")+
labs(x="Cancer Type", y = "-log10(p-value)")+
theme_classic()
ggplot(as.data.frame(effectSizes), aes(x=reorder(cancerTypes, -effectSizes), y=effectSizes))+
geom_bar(stat="identity")+
labs(x="Cancer Type", y = "Coefficient Estimate")+
theme_classic()
#coxph model
coxphList = list()
pvals = c()
for(i in seq_along(levels(miTCRSub3$CancerType))) {
cancerObs = miTCRSub3[which(miTCRSub3$CancerType == levels(miTCRSub$CancerType)[i]),]
coxph = coxph(Surv(OSM, OSS)~NormShannon + DiagnosisAge, cancerObs)
print(levels(miTCRSub$CancerType)[i])
print(coxph)
pval = summary(coxph)$coefficients[1,5]
pvals = c(pvals, pval)
coxphList[[i]] = coxph
}
p.adjust(pvals, "BH")
coxStats = unlist(lapply(coxphList, function(x){
if(!is.null(x)){
return(summary(x)$coefficients[1,4])
}
}))
#plot
cancerTypes = levels(miTCRSub3$CancerType)
ggplot(as.data.frame(coxStats), aes(x=reorder(cancerTypes, -coxStats), y=coxStats))+
geom_bar(stat="identity")+
labs(title="Cox Statistic for Normalized Clonality", x="Cancer Type", y = "Statistic")+
theme_classic()
```
age and TMB
```{r}
#TMB and age
pheno$LogTMB = log(pheno$`Mutation Count`)
tmblm = lm(LogTMB~`Diagnosis Age` +`TCGA PanCanAtlas Cancer Type Acronym`, pheno)
summary(tmblm)
write.csv(summary(tmblm)$coefficients, "~/Fertig Lab/csv/TCGAMutationsAgeLM.csv")
jtools::effect_plot(tmblm, `Diagnosis Age`, interval = T, plot.points = T)
pheno$`Overall Survival Status`[which(pheno$`Overall Survival Status` == "LIVING")] = 0
pheno$`Overall Survival Status`[which(pheno$`Overall Survival Status` == "DECEASED")] = 1
pheno$`Overall Survival Status` = as.numeric(pheno$`Overall Survival Status`)
mCox = coxph(Surv(pheno$`Overall Survival (Months)`, pheno$`Overall Survival Status`)~log(pheno$`Mutation Count`)+ pheno$`Diagnosis Age` + strata(pheno$`TCGA PanCanAtlas Cancer Type Acronym`))
write.csv(summary(mCox)$coefficients, "~/Fertig Lab/csv/TCGAMutationsCoxModel.csv")
#by cancer type
#split by cancer type
phenoSub = pheno[-which(is.na(pheno$`Mutation Count`) | is.na(pheno$`Diagnosis Age`)),]
phenoCT = split(phenoSub, phenoSub$`TCGA PanCanAtlas Cancer Type Acronym`)
nSamples = lapply(phenoCT, nrow)
phenoCT = phenoCT[-which(nSamples < 100)]
f = function(phenoData) {
tmpLM = lm(log(`Mutation Count`) ~ `Diagnosis Age`, data = phenoData)
t = broom::tidy(tmpLM)
return(t)
}
phenoLms = lapply(phenoCT, f)
lmPvals = unlist(lapply(phenoLms, function(x){x[2,5]}))
p.adjust(lmPvals, "BH")
lmStats = data.frame(lmPvals = -log10(lmPvals), CancerType = names(phenoCT))
lmStats$CancerType = as.character(lmStats$CancerType)
x=reorder(lmStats[,2], -lmStats[,1])
ggplot(lmStats, aes(x=x, y=lmPvals))+
geom_bar(stat="identity")+
theme_classic()+
ylab("-log10(p-value)")+
xlab("Cancer Type")
lmES = unlist(lapply(phenoLms, function(x){x[2,2]}))
lmES = data.frame(lmES = lmES, CancerType = names(phenoCT))
lmES$CancerType = as.character(lmES$CancerType)
x=reorder(lmES[,2], -lmES[,1])
ggplot(lmES, aes(x=x, y=lmES))+
geom_bar(stat="identity")+
theme_classic()+
ylab("Coefficient Estimate")+
xlab("Cancer Type")
tmbData = lapply(phenoLms, function(x){x[2,]})
saveRDS(tmbData, "Data/tmbCT_LMResults.rds")
#age alone cox
aCox = coxph(Surv(pheno$`Overall Survival (Months)`, pheno$`Overall Survival Status`)~pheno$`Diagnosis Age` + pheno$Sex + pheno$`TCGA PanCanAtlas Cancer Type Acronym`)
write.csv(summary(aCox)$coefficients, "~/Fertig Lab/csv/TCGAAgeCoxModel.csv")
#age among hn, nsclc, kidney, melanoma, urothelial
phenoSub = pheno[which(pheno$`TCGA PanCanAtlas Cancer Type Acronym` %in% c("BLCA", "HNSC", "KIRC", "LUAD", "SKCM"))]
aCox2 = coxph(Surv(phenoSub$`Overall Survival (Months)`, phenoSub$`Overall Survival Status`)~phenoSub$`Diagnosis Age` + phenoSub$Sex + phenoSub$`TCGA PanCanAtlas Cancer Type Acronym`)
write.csv(summary(aCox2)$coefficients, "~/Fertig Lab/csv/TCGAAgeCoxModel_ICBcancers.csv")
phenoSub$AgeGroup = cut(phenoSub$`Diagnosis Age`, c(29,65,90), labels = c("65Under", "Over65"))
library(survminer)
kmFit <- survfit(Surv(`Overall Survival (Months)`, `Overall Survival Status`)~AgeGroup, data = phenoSub)
ggsurvplot(kmFit, risk.table = T, pval = T, conf.int = T, xlim = c(0, 350))
```
TMB - TCR correlation
```{r}
#filter and match
phenoSub = pheno[which(pheno$`Sample ID` %in% miTCRSub$SampleID),]
#remove miTCR duplicate samples
miTCRSub2 = miTCRSub[-which(duplicated(miTCRSub$SampleID)),]
phenoMatched = phenoSub[match(miTCRSub2$SampleID, phenoSub$`Sample ID`),]
#correlation
cor(phenoMatched$LogTMB, miTCRSub2$NormShannon, use = "complete.obs", method = "spearman")
```