-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp3_cellTypeSignatures.Rmd
224 lines (136 loc) · 6.5 KB
/
exp3_cellTypeSignatures.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
---
title: "exp3_pulpDeconvolution"
output: html_document
author: Sara Gosline
date: "2023-01-27"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(ggfortify)
library(cowplot)
#library(leapR)
library(dplyr)
source('spleenDataFormatting.R')
library(spammer)
#source('spatialProtUtils.R')
```
The assignment of pulp type from the basic data was not robust. Here we will use the cell type signatures from the sorted data to label the voxels.
## Cell type signatures
Here we get the differential expression from the sorted cells.
```{r cell type signatures}
##get fit values for diffex
pumap<-scater::runPCA(spat.prot)
phumap<-spat.phos%>%
scater::runPCA()
fullmap<-scater::runPCA(global.sorted)
fullmap<-spatialDiffEx(fullmap)
full<- fullmap%>%
rowData(.)%>%
as.data.frame()%>%
dplyr::select(featureID='X',
logFC='pulpAnnotation.limma.logFC',
adj.P.Val='pulpAnnotation.limma.adj.P.Val',
AveExpr='pulpAnnotation.limma.AveExpr')
upsig<-full%>%
subset(adj.P.Val<0.01)%>%
subset(logFC>1)
downsig<-full%>%
subset(adj.P.Val<0.01)%>%
subset(logFC<(-1))
print(paste0('we now have a white pulp signature of ',nrow(upsig)))
print(paste0('we now have a red pulp signature of ',nrow(downsig)))
```
## Plot values in heatmap
Let's now plot the signatures in heatmaps of the original and spatial data.
```{r pressure, echo=FALSE}
library(pheatmap)
pheatmap(as.matrix(exprs(fullmap)[rownames(upsig),]),annotation_col = as.data.frame(colData(fullmap)),cellwidth = 10,main='White pulp signature')
pheatmap(as.matrix(exprs(pumap)[intersect(rownames(upsig),rownames(rowData(pumap))),]),annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],main='White pulp signature')
pheatmap(as.matrix(exprs(fullmap)[rownames(downsig),]),annotation_col = as.data.frame(colData(fullmap)),cellwidth=10,main='Red pulp signature')
pheatmap(as.matrix(exprs(pumap)[intersect(rownames(downsig),rownames(rowData(pumap))),]),annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],main='Red pulp signature')
```
These signatures cluster voxels, but don't serve as great markers because tehy are still very much 'down' regulated. We want to pick the top expressed proteins that represent each pulp type.
```{r reduced signature}
whitesig<-full%>%
subset(adj.P.Val<0.01)%>%
subset(logFC>1)%>%
subset(AveExpr>1)
redsig<-full%>%
subset(adj.P.Val<0.01)%>%
subset(logFC<(-1))%>%
subset(AveExpr>(1))
print(paste0('we now have a reduced white pulp signature of ',nrow(whitesig)))
print(paste0('we now have a reduced red pulp signature of ',nrow(redsig)))
pheatmap(as.matrix(exprs(fullmap)[rownames(whitesig),]),annotation_col = as.data.frame(colData(fullmap)),cellwidth = 10,
main='White pulp signature',filename='whitePulpsorted.png')
pheatmap(as.matrix(exprs(pumap)[intersect(rownames(whitesig),rownames(rowData(pumap))),]),annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],
main='White pulp signature',filename='whitePulp2d.png')
pheatmap(as.matrix(exprs(fullmap)[rownames(redsig),]),annotation_col = as.data.frame(colData(fullmap)),cellwidth=10,main='Red pulp signature',
filename='redPulpsorted.png')
pheatmap(as.matrix(exprs(pumap)[intersect(rownames(redsig),rownames(rowData(pumap))),]),annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],main='Red pulp signature',
filename='redPulp2d.png')
```
So we have a handful of proteins that are marking the signature. Let's see if we can link them in a biological fashion.
```{r pathway enrichment}
#library(clusterProfiler)
library(leapR)
library(org.Hs.eg.db)
data('krbpaths')
map<-as.list(org.Hs.egSYMBOL2EG)
redgenes<-lapply(rownames(redsig),function(x) return(map[[x]][1]))
whitegenes<-lapply(rownames(whitesig),function(x) return(map[[x]][1]))
universe<-unlist(lapply(rownames(full),function(x) return(map[[x]][1])))
order.enrich<-leapR::leapR(geneset=krbpaths,
enrichment_method='enrichment_in_order',id_column='featureID',
datamatrix=full,primary_columns='logFC')#,greaterthan=T,threshold=1)
sig.enrich<-order.enrich%>%
subset(BH_pvalue<0.01)
print(sig.enrich)
groups<-stringr::str_split(sig.enrich[1,]$ingroupnames,pattern=', ')%>%unlist()
pheatmap(as.matrix(exprs(fullmap)[intersect(groups,rownames(rowData(fullmap))),]),
annotation_col = as.data.frame(colData(fullmap)),
main='CD3 and TCR signature')
pheatmap(as.matrix(exprs(pumap)[intersect(groups,rownames(rowData(pumap))),]),
annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],
main='CD3 and TCR signature')
pheatmap(as.matrix(exprs(fullmap)[intersect(groups,rownames(rowData(fullmap))),]),
annotation_col = as.data.frame(colData(fullmap)),
main='CD3 and TCR signature',filename='cd3tcrSorted.png')
pheatmap(as.matrix(exprs(pumap)[intersect(groups,rownames(rowData(pumap))),]),
annotation_col = as.data.frame(colData(pumap))[,c('pulpAnnotation','TMT.set')],
main='CD3 and TCR signature',filename='cd3tcr2d.png')
```
There is only one kegg/reactome pathway that is enriched, and it is related to TCR signaling.
```{r calculate signature scores}
pumap<-calcSigScore(pumap,rownames(redsig),'RedPulp')%>%
calcSigScore(rownames(whitesig),'WhitePulp')
p1<-plotPCA(pumap,color_by='WhitePulp')
p2<-plotPCA(pumap,color_by='RedPulp')
p3<-cowplot::plot_grid(p2,p1)
p3
ggsave('pcaLabeledByScore.png',p3,width=12)
```
This isn't quite right - the 'green' items are definitely on the right, but we need some sort of threshold to 'call' the white vs. red pulp items.
### Plot in image
Lastly we want to plot a set of scores, in this case the signature scores, in an image.
```{r plot image}
p <- plotGrid(pumap,'RedPulp')
p1<-plotGrid(pumap,'WhitePulp')
pc<-cowplot::plot_grid(p,p1)
pc
ggsave('scoredImage.png',pc,width=12)
```
Now that we have a rank score, we can "call" each voxel.
```{r voxel scoring}
cols<-list(red='darkred',white='white',None='darkgrey')
newDat<-colData(pumap)%>%
as.data.frame()%>%
mutate(isRed=RedPulp>0.5,isWhite=WhitePulp>0.5)%>%
mutate(pulp=ifelse(isRed,'red',ifelse(isWhite,'white','None')))
p1<-ggplot(newDat)+geom_point(aes(x=RedPulp,y=WhitePulp,col=pulp))+scale_color_manual(values=cols)
p2<-ggplot(newDat,aes(x=Xcoord,y=Ycoord,fill=pulp))+geom_raster()+scale_fill_manual(values=cols)
res<-cowplot::plot_grid(p1,p2)
res
ggsave('assignedVals.png',res,width=12)
```