-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig6_distance_based_analysis.Rmd
385 lines (281 loc) · 11.3 KB
/
fig6_distance_based_analysis.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
382
383
384
385
---
title: "Distance based analysis"
author: "Sara Gosline"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
library(dplyr)
library(purrr)
library(devtools)
library(ggplot2)
library(ggfortify)
library(cowplot)
library(leapR)
source('loadHumanPancData.R')
```
## Map to distances
get data and metadata nad join to get distance data
```{r distnace data, message=FALSE, warning=FALSE, message=FALSE,error=FALSE}
#m2<- MSnSet(exprs = crosstab2, pData = isletMeta)
fixed.crosstab<-correctMissingProteins(fulltab)
long.tab<-fixed.crosstab%>%#exprs(m2)%>%
as.data.frame()%>%
tibble::rownames_to_column('protein')%>%
tidyr::pivot_longer(cols=c(1:ncol(fixed.crosstab)+1),
names_to='sample',
values_to='logFC')%>%
left_join(tibble::rownames_to_column(isletMeta,'sample'))%>%
dplyr::select(protein,`sample`,logFC,`Distance to islet`,`Islet Number`)
ct<-function(x,y,method){
ret=NA
try(ret<-cor.test(x,y,method=method)$p.value)
ret
}
cor.vals<-long.tab%>%
dplyr::group_by(protein,`Islet Number`)%>%
dplyr::summarize(corVal=cor(logFC,`Distance to islet`,use='pairwise.complete.obs',method='pearson'),
corTest=ct(logFC,`Distance to islet`,method='pearson'))
cor.na.vals<-long.tab|>
dplyr::group_by(protein,`Islet Number`)|>
dplyr::summarize(corVal=cor(logFC,`Distance to islet`,use='pairwise.complete.obs',method='spearman'),
corTest=ct(logFC,`Distance to islet`,method='spearman'))
write.table(cor.na.vals,file='suppTab6.csv',sep=',',quote=F,row.names=F,col.names=T)
##which ones are highly anti-correlated
```
## Gene mapping and enrichment
now we want to evaluate the enrichment of the proteins whose expression is correlated with distance (lower in islet) and anti-correlated (higher in islet)
```{r mapping, warning=FALSE, message=FALSE}
map<-read.table('uniprotMap.txt',header = TRUE)
# tidyr::separate(feature,sep='\\|',into=c('sp','id','From'))%>%
# left_join(map)%>%
##TODO: summary table off differential expression with venn diagram
cor.names<-cor.na.vals%>%
# tidyr::separate(protein,sep='',into=c('sp','id','From'))%>%
dplyr::rename(From='protein')%>%
left_join(map)%>%
subset(!is.na(To))
cor.mat<-cor.names%>%
dplyr::ungroup()%>%
dplyr::select(-c(corTest,From))%>%
dplyr::mutate(`Islet Number`=as.character(`Islet Number`))%>%
tidyr::pivot_wider(names_from=`Islet Number`,values_from=corVal,values_fn=list(corVal=mean))#%>%
# tibble::column_to_rownames('To')
##now remove dupes
if(any(duplicated(cor.mat$To))){
dupes<-cor.mat$To[which(duplicated(cor.mat$To))]
cor.mat<-cor.mat%>%
subset(!To%in%dupes)%>%
tibble::column_to_rownames('To')
}
hist(cor.names$corVal)
#autoplot(pc.vals,label=TRUE)
#ggsave('correlationPCA.pdf')
```
now we have all the correlation values
```{r go enrichment, message=F, warning=F}
library(leapR)
library(pheatmap)
#data("ncipid")
go.order.res<-lapply(unique(as.character(cor.names$`Islet Number`)),function(x) {
print(x)
leapR(datamatrix=as.data.frame(cor.mat),gosigs,'enrichment_in_order',
primary_columns=x,id_column='To')%>%
subset(pvalue<0.01)%>%
# dplyr::select(ingroup_n,ingroup_mean,BH_pvalue,zscore)%>%
mutate(image=x)%>%
tibble::rownames_to_column('Pathway')
})
go.order.tab<-do.call(rbind,go.order.res)%>%
#subset(pvalue<0.05)%>%
dplyr::select(Pathway,zscore,image)%>%
mutate(Pathway=stringr::str_replace_all(Pathway," \\(GO:[0-9]*\\)",''))%>%
tidyr::pivot_wider(names_from='image',values_from='zscore',values_fill=list(zscore=0.0))%>%
tibble::column_to_rownames('Pathway')
terms<-which(apply(go.order.tab,1,function(x) length(which(x==0))<3))
pheatmap(go.order.tab[names(terms),],cellwidth = 10,cellheight=10,filename = 'goBPterms.pdf')
```
now lets do kegg
```{r KEGG enrichment, message=F, warning=F}
#data("ncipid")
data('krbpaths')
kegg.order.res<-lapply(unique(as.character(cor.names$`Islet Number`)), function(x) {
print(x)
leapR(datamatrix=as.data.frame(cor.mat),krbpaths,'enrichment_in_order',
primary_columns=x,id_column='To',minsize=5)%>%
subset(pvalue<0.01)%>%
# dplyr::select(ingroup_n,ingroup_mean,BH_pvalue,zscore)%>%
dplyr::mutate(image=x)%>%
tibble::rownames_to_column('pathway')
})
krb.order.tab<-do.call(rbind,kegg.order.res)%>%
subset(BH_pvalue<0.1)%>%
dplyr::select(zscore,image,pathway,pvalue,BH_pvalue,ingroupnames)
kegg.order.tab<-krb.order.tab[grep("KEGG",krb.order.tab$pathway),]
reac.order.tab<-krb.order.tab[grep("REAC",krb.order.tab$pathway),]
kegg.order.tab<-kegg.order.tab%>%
dplyr::select(-c(pvalue,BH_pvalue,ingroupnames))%>%
mutate(pathway=stringr::str_replace_all(pathway,'_',' '))%>%
mutate(pathway=stringr::str_replace_all(pathway,'REACTOME|KEGG|BIOCARTA',''))%>%
mutate(pathway=tolower(pathway))%>%
tidyr::pivot_wider(names_from='image',values_from='zscore',values_fill=list(zscore=0.0))%>%
tibble::column_to_rownames('pathway')
reac.order.tab<-reac.order.tab%>%
dplyr::select(-c(pvalue,BH_pvalue,ingroupnames))%>%
mutate(pathway=stringr::str_replace_all(pathway,'_',' '))%>%
mutate(pathway=stringr::str_replace_all(pathway,'REACTOME|KEGG|BIOCARTA',''))%>%
mutate(pathway=tolower(pathway))%>%
tidyr::pivot_wider(names_from='image',values_from='zscore',values_fill=list(zscore=0.0))%>%
tibble::column_to_rownames('pathway')
#terms<-which(apply(kegg.order.tab,1,function(x) length(which(x==0))<6))
pheatmap(kegg.order.tab)
pheatmap(kegg.order.tab,cellwidth = 10,cellheight=10,filename = 'keggterms.pdf')
terms<-which(apply(reac.order.tab,1,function(x) length(which(x==0))<4))
pheatmap(reac.order.tab[terms,])
pheatmap(reac.order.tab[terms,],cellwidth = 10,cellheight=10,filename = 'reacterms.pdf')
```
One of the reactome terms that was associated was insulin signaling, what if we focus on that?
```{r insu}
path<-subset(krb.order.tab,pathway=='REACTOME_INSULIN_SYNTHESIS_AND_SECRETION')%>%
select(ingroupnames)
prots<-path[1,]%>%
stringr::str_split(.,pattern=', ')%>%
unlist()
uprots<-subset(map,To%in%prots)%>%
dplyr::select("From")
insgluc<-fixed.crosstab[unlist(uprots),]%>%
as.data.frame()%>%
tibble::rownames_to_column('protein')%>%
tidyr::pivot_longer(2:(1+ncol(fixed.crosstab)),names_to='sample',values_to='logRatio')%>%
left_join(tibble::rownames_to_column(isletMeta,'sample'))
path.avg<-insgluc%>%
dplyr::group_by(sample,`X-coord`,`Y-coord`,`Islet Number`,IsletOrNot)%>%
summarize(meanExp=mean(logRatio))
##now we can plot grid
p<- ggplot(path.avg)+
geom_raster(aes(x=`X-coord`,y=`Y-coord`,fill=meanExp))+
scale_fill_viridis_c()+coord_equal()+
geom_point(aes(x=`X-coord`,y=`Y-coord`,col=IsletOrNot,alpha=0.1))+
scale_color_manual(values=c('black','white'))+
facet_grid(.~`Islet Number`)+theme_classic()
p
##not great, just munges things together
with.signif<-cor.names%>%
mutate(signif=(corTest<0.05))%>%
subset(From%in%unlist(uprots))
cor.means<-with.signif%>%
ungroup()%>%
dplyr::group_by(To)%>%
dplyr::summarize(mval=mean(corVal))%>%
arrange(mval)
p2<-with.signif%>%
ggplot(aes(x=factor(To,levels=cor.means$To),y=corVal,col=signif))+
geom_jitter()+
theme(axis.text.x=element_blank())
p2
ggsave('allCorrelationsInsulinSig.pdf',p2)
```
Here we can filter out the correlations that are interesting within the insulin signaling pathway, and
dive into specific proteins of interest
```{r proteins}
signif<-with.signif%>%
subset(signif)%>%
dplyr::group_by(From)%>%
summarize(numSignif=n())%>%
subset(numSignif>1)
insgluc<-fixed.crosstab[signif$From,]%>%
as.data.frame()%>%
tibble::rownames_to_column('From')%>%
left_join(map)%>%
dplyr::rename(protein='To')%>%
tidyr::pivot_longer(2:(1+ncol(fixed.crosstab)),names_to='sample',
values_to='logRatio')%>%
left_join(tibble::rownames_to_column(isletMeta,'sample'))
p<- ggplot(insgluc)+
geom_raster(aes(x=`X-coord`,y=`Y-coord`,fill=logRatio))+
scale_fill_viridis_c()+coord_equal()+
geom_point(aes(x=`X-coord`,y=`Y-coord`,col=IsletOrNot,alpha=0.1))+
scale_color_manual(values=c('black','white'))+
facet_grid(protein~`Islet Number`)+theme_classic()
p
ggsave('sigInsulinSig.pdf',p,height=10)
```
these are the proteins within insulin signaling that re correalted with distance.
We can generalize this code now in a function
```{r all pathways}
#' this function iterates through all the correlations
#' and plots those with proteins with significant correlation
#' in a specific pathway
plotPathwayCors<-function(krb.order.tab,pathname){
#print(pathname)
path<-subset(krb.order.tab,pathway==pathname)%>%
select(ingroupnames)
prots<-path[1,]%>%
stringr::str_split(.,pattern=', ')%>%
unlist()
uprots<-subset(map,To%in%prots)%>%
dplyr::select("From")
insgluc<-fixed.crosstab[unlist(uprots),]%>%
as.data.frame()%>%
tibble::rownames_to_column('protein')%>%
tidyr::pivot_longer(2:(1+ncol(fixed.crosstab)),names_to='sample',values_to='logRatio')%>%
left_join(tibble::rownames_to_column(isletMeta,'sample'))
path.avg<-insgluc%>%
dplyr::group_by(sample,`X-coord`,`Y-coord`,`Islet Number`,IsletOrNot)%>%
summarize(meanExp=mean(logRatio))
##now we can plot grid
p<- ggplot(path.avg)+
geom_raster(aes(x=`X-coord`,y=`Y-coord`,fill=meanExp))+
scale_fill_viridis_c()+coord_equal()+
geom_point(aes(x=`X-coord`,y=`Y-coord`,col=IsletOrNot,alpha=0.1))+
scale_color_manual(values=c('black','white'))+
facet_grid(.~`Islet Number`)+theme_classic()
##nget proteins that are significantly correlated with distance
with.signif<-cor.names%>%
mutate(signif=(corTest<0.05))%>%
subset(From%in%unlist(uprots))
cor.means<-with.signif%>%
ungroup()%>%
dplyr::group_by(To)%>%
dplyr::summarize(mval=mean(corVal))%>%
arrange(mval)
p2<-with.signif%>%
ggplot(aes(x=factor(To,levels=cor.means$To),y=corVal,col=signif))+
geom_jitter()+
theme(axis.text.x=element_blank())
# p2
ggsave(paste0('allCorrelations',pathname,'.pdf'),p2)
signif<-with.signif%>%
subset(signif)%>%
dplyr::group_by(From)%>%
summarize(numSignif=n())%>%
subset(numSignif>1) ##number significant genes is at least 2
#print(signif)
if(nrow(signif)>1){
insgluc<-fixed.crosstab[signif$From,]%>%
as.data.frame()%>%
tibble::rownames_to_column('From')%>%
left_join(map)%>%
dplyr::rename(protein='To')%>%
tidyr::pivot_longer(2:(1+ncol(fixed.crosstab)),names_to='sample',
values_to='logRatio')%>%
left_join(tibble::rownames_to_column(isletMeta,'sample'))
p3<- ggplot(insgluc)+
geom_raster(aes(x=`X-coord`,y=`Y-coord`,fill=logRatio))+
scale_fill_viridis_c()+coord_equal()+
geom_point(aes(x=`X-coord`,y=`Y-coord`,col=IsletOrNot,alpha=0.1))+
scale_color_manual(values=c('black','white'))+
facet_grid(protein~`Islet Number`)+theme_classic()
#p4<-cowplot::plot_grid(p,p2,p3)
ggsave(paste0('sig_',pathname,'.pdf'),p3,height=10)
}
}
```
Here we can filter out those pathways that are significant across ore than 2 voxels, then plot
```{r splicing proteins}
sig<-krb.order.tab%>%
group_by(pathway)%>%
summarize(num=n())%>%subset(num>3)
## significantly correlated in at least three images
lapply(sig$pathway,function(x) plotPathwayCors(krb.order.tab,x))
```