-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp2_spleenSignatures.Rmd
383 lines (251 loc) · 11.1 KB
/
exp2_spleenSignatures.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
---
title: "Figure 2 Spleen Signatures"
author: "Sara Gosline"
date: "`r Sys.Date()`"
output: html_document
---
```{r loading, echo=F, warning=F, message=F}
library(dplyr)
library(purrr)
library(devtools)
#library(MSnSet.utils)
if(!require(MSnSet.utils))
devtools::install_github('PNNL-Comp-Mass-Spec/MSnSet.utils')
library(ggplot2)
library(ggfortify)
library(cowplot)
library(leapR)
source('spleenDataFormatting.R')
#this loads data into SCE for us
```
## PCA depicts separation by pulp annotation
Here we do the PCA plots again to remind us of the original annotation and batch
```{r pca}
library(scMerge)
library(scater)
#define function do do PCA on sce object?
pumap<-#scater::runUMAP(sc.prot)%>%
scater::runPCA(sc.prot)
phumap<-scater::runUMAP(sc.phos)%>%
scater::runPCA()
fullmap<-scater::runPCA(full.spleen)
#scater::runPCA()
p3<-plotPCA(pumap,color_by='pulpAnnotation')+ggtitle("Proteomic PCA")
#p4<-plotPCA(phumap,color_by='pulpAnnotation')+ggtitle("Phosphoproteomic PCA")
p5<-plotPCA(fullmap,color_by='pulpAnnotation')+ggtitle('Sorted PCA')
p3
#p4
p5
universe<-intersect(rownames(exprs(pumap)),rownames(exprs(fullmap)))
library(scMerge)
combined <-sce_cbind(list(pumap,fullmap),exprs='logcounts',
colData_names=c('pulpAnnotation','source'))%>%
runPCA()
p6<-plotPCA(combined,color_by='batch')
p7<-plotPCA(combined,color_by='pulpAnnotation')
p6
p7
##now lets do batch correction
library(batchelor)
library(scran)
library(scater)
#batchNorm <- multiBatchNorm(spatial=pumap[universe,],original=fullmap[universe,],assay.type='logcounts')
#get gene variance
##can't get this to work right now
##TODO: debug
#dec1 <- modelGeneVar(batchNorm[[1]])
#dec2 <- modelGeneVar(batchNorm[[2]])
#combined.dec <- combineVar(dec1, dec2)
#chosen.hvgs <- getTopHVGs(combined.dec, n=1000)
#combined <- correctExperiments(spatial=batchNorm[[1]], original=batchNorm[[2]],assay.type='logcounts',
# PARAM=NoCorrectParam())%>%
# runPCA()
# runTSNE(dimred='PCA')
#COMBat might work? https://rdrr.io/github/compbiomed/singleCellTK/man/runComBatSeq.html
# bc <- runComBatSeq(combined,'logcounts','source',covariates='pulpAnnotation',assay='combatCorrected')
```
Batch correction doesn't work! Need to update algorithm to enable different assuptions of log ratio data.Can we just use combat?
## Differential expression
We can use the single cell toolkit to do differential expression on each set of proteins.
```{r bc}
library(limma)
doDiffEx<-function(sce,column='pulpAnnotation', vals=c('red','white')){
#collect samples by factor
samp1<-which(colData(sce)[[column]]==vals[1])
samp2<-which(colData(sce)[[column]]==vals[2])
fac <- factor(rep(c(2,1),c(length(samp2), length(samp1))))
#print(fac)
design <- model.matrix(~fac)
#print(design)
fit <- lmFit(exprs(sce)[,c(samp2,samp1)], design)
fit <- eBayes(fit)
# print(topTable(fit, coef=2))
res <- topTable(fit, coef=2, number=Inf, sort.by="P")
res <- data.frame(featureID=rownames(res), res, stringsAsFactors = F)
return(res)
}
##get fit values for diffex
pu<-doDiffEx(pumap)
full<-doDiffEx(fullmap)
sig.pu<-subset(pu,adj.P.Val<0.01)#%>%subset(abs(logFC)>.5)
sig.full<-subset(full,adj.P.Val<0.01)#%>%subset(abs(logFC)>.5)
##let's plot the overlap of the significant genes
library(ggVennDiagram)
vg<-ggVennDiagram(list(Spatial=sig.pu$featureID,Original=sig.full$featureID))+ggtitle('Differentially expressed genes between red and white pulp')
vg
ggsave('diffEx0.01VennDiag.png',vg)
##et test values
#putest<-decideTests(pu,p.value=0.01)%>%as.data.frame()%>%dplyr::rename(Spatial='fac2')
#fulltest<-decideTests(full,p.value=0.01)%>%as.data.frame()%>%dplyr::rename(Original='fac2')
#overlap<-intersect(rownames(putest),rownames(fulltest))
#cbind(putest[overlap,],fulltest[overlap,])%>%
# dplyr::select('Spatial','Original')%>%
# vennCounts()%>%ggVennDiagram()
##and the correlation of fold changes
comb<-full%>%dplyr::select(featureID,Original='logFC',Orig.Sig='adj.P.Val')%>%
inner_join(dplyr::select(pu,c(featureID,Spatial='logFC',Spatial.Sig='adj.P.Val')))%>%
mutate(SigInSpatial=Spatial.Sig<0.01,SigInOrig=Orig.Sig<0.01)
dp<-ggplot(comb)+geom_point(aes(x=Original,y=Spatial,col=SigInSpatial))+
ggtitle("Log fold change between red and white pulp")
dp
ggsave("RedWhitePulpExpression.png",dp)
```
Overlap between spatial data and original data is good! Let's look at differential expression signature from original data.Used a more stringent p-value of 0.01 because there are so few proteins, and already half of them are differentially expressed.
## Get functional enrichment of gene signatures
```{r func enrichment}
library(clusterProfiler)
library(org.Hs.eg.db)
map<-as.list(org.Hs.egSYMBOL2EG)
revmap<-as.list(org.Hs.egSYMBOL)
comb$gene<-lapply(comb$featureID,function(x) return(map[[x]][1]))
go.enrich<-enrichGO(gene=comb$gene[comb$SigInOrig],
OrgDb=org.Hs.eg.db,ont='BP',
qvalueCutoff=0.05,
universe=comb$gene)
kegg.enrich<-enrichKEGG(gene=comb$gene[comb$SigInOrig],
qvalueCutoff=0.05,
universe=comb$gene)
#rownames(lfc)<-sig.full[rownames(lfc),'gene']
lfc<-comb$Original
names(lfc)<-comb$gene
gsea.enrich<-gseGO(geneList=sort(lfc,decreasing=T),
OrgDb=org.Hs.eg.db,ont='BP')
goplot(gsea.enrich,color='NES')+ggtitle('Enrichment in original data')
lfc<-comb$Spatial
names(lfc)<-comb$gene
spat.enrich<-gseGO(geneList=sort(lfc,decreasing=T),
OrgDb=org.Hs.eg.db,ont='BP')
goplot(spat.enrich,color='NES')+ggtitle('Enrichment in spatial data')
```
IN the global data we can see enrichment in translation in the red pulp, and depletion of the metabolism/homeostasis. But the signal is week.
## Get loadings for first two components
We can now get the loadings for the first two principal components to determine the signal that is there aside from the RNA translation signal.
```{r pca loadings, warning=FALSE, echo=FALSE, message=FALSE}
library(leapR)
pp<-prcomp(exprs(pumap),.center=TRUE)
pph<-prcomp(exprs(phumap),.center=TRUE)
#data("krbpaths")
pc1<-pp$x[,'PC1']
pc2<-pp$x[,'PC2']
names(pc1)<-lapply(names(pc1),function(x) return(map[[x]][1]))
names(pc2)<-lapply(names(pc2),function(x) return(map[[x]][1]))
gsea.enrich.pc1<-gseGO(geneList=sort(pc1,decreasing=T),
OrgDb=org.Hs.eg.db,ont='BP')
p<-goplot(gsea.enrich.pc1,color='NES')+ggtitle("First principal component")
p
ggsave('goTermsPC1.png',p)
gsea.enrich.pc2<-gseGO(geneList=sort(pc2,decreasing=T),
OrgDb=org.Hs.eg.db,ont='BP')
p<-goplot(gsea.enrich.pc2,color='NES')+ggtitle('Second principal component')
ggsave('goTermsPC2.png',p)
top.tab<-summary(gsea.enrich.pc2)%>%
subset(p.adjust<0.001)%>%
dplyr::select('Description','enrichmentScore','NES','pvalue','p.adjust','rank','qvalue')
top.tab
```
The second principal component shows a clear metal ion/metabolism signature distinct from the RNA processing signature.
Now do the differential expression with the normalized data
## Plot pathway activity in image
Now we can try to plot the pathway activity in an image by averaging the expression of the proteins selected in the pathway of intest.
```{r pathway plot, message=F, warning=F}
fillcolors<-list(red='darkred',white='lightgrey',green='darkgreen',`NA`='white')
p<-ggplot(as.data.frame(colData(pumap)),aes(x=Xcoord,y=Ycoord,fill=pulpAnnotation))+geom_raster()+scale_fill_manual(values=fillcolors)
p
ggsave('origAnnotations.png',p)
###
#plotAvgExpression
#plots a specific pathway list from an enrichment object across a grid determined from row/col values in scobjs
#
plotAvgExpression<-function(pathwayName,spatialDat,protNames=c(),y='GO'){
library(ggplot2)
library(cowplot)
##collect expression amd map to coordinates
expr.dat<-exprs(spatialDat)%>%
as.matrix()%>%
as.data.frame()%>%
tibble::rownames_to_column('Gene')%>%
tidyr::pivot_longer(-Gene,names_to='sample',values_to='logratio')%>%
left_join(tibble::rownames_to_column(as.data.frame(colData(pumap)),'sample'))%>%
subset(Gene%in%protNames)
path.vals<-expr.dat%>%
subset(!is.na(pulpAnnotation))%>%
group_by(sample,Xcoord,Ycoord,pulpAnnotation)%>%
summarize(meanExpr=median(logratio),.groups='keep')%>%
dplyr::select(sample,meanExpr,Xcoord,Ycoord,pulpAnnotation)%>%distinct()
path.meds<-path.vals%>%group_by(pulpAnnotation)%>%
summarize(meanPath=median(meanExpr))
redpath<-path.meds$meanPath[which(path.meds$pulpAnnotation=='red')]
whitepath<-path.meds$meanPath[which(path.meds$pulpAnnotation=='white')]
if(redpath>whitepath)
gradfun<-scale_fill_gradient2(low='darkgreen',mid='lightgrey',high='darkred',midpoint=whitepath)
else
gradfun<-scale_fill_gradient2(low='darkgreen',mid='darkred',high='lightgrey',midpoint=redpath)
p<-ggplot(path.vals,aes(x=Xcoord,y=Ycoord,fill=meanExpr))+geom_raster()+gradfun+theme_bw()+
ggtitle(paste0("mean logratio of ",length(protNames),' ',pathwayName,' proteins'))
p2<-ggplot(path.vals,aes(x=pulpAnnotation,y=meanExpr,fill=pulpAnnotation))+geom_boxplot()+scale_fill_manual(values=fillcolors)+
theme_bw()
res<-cowplot::plot_grid(p,p2,rel_widths=c(1.8,1))
ggsave(paste0(gsub(' ','_',pathwayName),'_',y,'_imagePlots.png'),width=12,res)
res
}
plotPathway<-function(eobj,pathwayName,revmap,spatialDat,y='GO'){
po<-subset(summary(eobj),Description==pathwayName)
genes<-po$core_enrichment
genelist<-stringr::str_split(genes,pattern='/')%>%unlist()
genenames<-lapply(genelist,function(x) revmap[[x]][1])%>%unlist()
plotAvgExpression(pathwayName,spatialDat,genenames,y)
}
###let's pick the most interesting pathways...
allplots<-lapply(top.tab$Description,function (x) plotPathway(gsea.enrich.pc2,x,revmap,pumap))
allplots[[1]]
write.table(top.tab,file='topGoTermsOnPC2.tsv',sep='\t')
```
## KEGG
Next we should do the KEGG pathways.
```{r kegg pathways, warning=FALSE, echo=FALSE, message=FALSE}
gsea.enrich.pc1<-gseKEGG(geneList=sort(pc1,decreasing=T))
#goplot(gsea.enrich.pc1,color='NES')+ggtitle("First principal component")
gsea.enrich.pc2<-gseKEGG(geneList=sort(pc2,decreasing=T))
top.tab<-summary(gsea.enrich.pc2)%>%
subset(p.adjust<0.05)%>%
dplyr::select('Description','enrichmentScore','NES','pvalue','p.adjust','rank','qvalue')
top.tab
###let's pick the most interesting pathways...
allplots<-lapply(top.tab$Description,function (x) plotPathway(gsea.enrich.pc2,x,revmap,pumap,y='KEGG'))
allplots[[1]]
write.table(top.tab,file='topKEGGTermsOnPC2.tsv',sep='\t')
```
## Red pulp vs white pulp signatures
What if we use the red/white pulp signature? how does that look? Can we use that to understand the activity?
```{r plot pulp, warning=FALSE, echo=FALSE, message=FALSE}
white.only<-subset(sig.pu,logFC>0)
red.only<-subset(sig.pu,logFC<0)
plotAvgExpression('Red Signature',pumap,red.only$featureID,y='')
plotAvgExpression('White Signature',pumap,white.only$featureID,y='')
write.table(sig.pu,file='differentiallyExpressedRedvsWhiteprots.tsv',sep='\t')
```
## Kinase analysis
## Network analysis
lastly we can do the network analysis on the up vs. down signatures to see what else we are missing.
```{r network}
```