-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp1_clusteringComparison.Rmd
219 lines (145 loc) · 6.04 KB
/
exp1_clusteringComparison.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
---
title: "Cluster comparison of spatial spleen data"
author: "Sara Gosline"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
library(BayesSpace)
library(ggplot2)
library(dplyr)
library(tidyr)
library(RColorBrewer)
library(cowplot)
```
## Format data
First grab data and format into single table. Evaluate distributions of values. There are clearly different distributions when we median center the data.
```{r get data, message=F,warning=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)
source('spleenDataFormatting.R')
```
Now we have a sense of how the data are distributed. Obviously this varies based on the normalization that we use in the original data. This will help guide our future analysis.
## PCA
Here we will use leverage the scater package to to compare how samples are distributed based on metadata.
```{r pca sce}
library(scater)
#define function do do PCA on sce object?
pumap<-scater::runUMAP(sc.prot)%>%
scater::runPCA()
phumap<-scater::runUMAP(sc.phos)%>%
scater::runPCA()
##now we can plot each of them to see how they compare based on tissue annotation.
p1<-plotPCA(pumap,color_by='pulpAnnotation')+ggtitle("Proteomics PCA")
p2<-plotUMAP(pumap,color_by='pulpAnnotation')+ggtitle("Proteomics UMAP")
p3<-plotPCA(phumap,color_by='pulpAnnotation')+ggtitle("Phosphoproteomic PCA")
p4<-plotUMAP(phumap,color_by='pulpAnnotation')+ggtitle("Phosphoproteomic UMAP")
p5<-cowplot::plot_grid(p1,p2,p3,p4)
ggsave('spleenUnsupervisedPlot.png',p5)
p5
```
There are notable differences in the clustering - the green are outliers in the PCA plot, while the white separate out on the second principal component. the UMAP is interesting as it clusters things distinctly.
## KMeans clustering
Determine the the optimal number of clusters and assign each sample toa cluster, then re-plot PCA with cluster labels. Here we try to use the most basic form of clustering by values alone - both protein and phosphoprotein.
```{r kmeans sce}
library(scran)
plotClusters<-function(psce,prefix='Proteomics'){
p1<-plotPCA(psce,color_by='rawClust')+ggtitle(paste(prefix,"PCA"))
p2<-plotUMAP(psce,color_by='rawClust')+ggtitle(paste(prefix,"UMAP"))
p3<-plotPCA(psce,color_by='pcaClust')+ggtitle(paste(prefix,"PCA"))
p4<-plotUMAP(psce,color_by='umapClust')+ggtitle(paste(prefix,"UMAP"))
p5<-cowplot::plot_grid(p1,p2,p3,p4)
ggsave(paste0(prefix,'spleenUnsupervisedClustering.png'),p5)
p5
}
clusterData<-function(psce){
new.annote<-data.frame(rawClust=scran::clusterCells(psce,assay.type='logcounts'),
umapClust=scran::clusterCells(psce,use.dimred='UMAP'),
pcaClust=scran::clusterCells(psce,use.dimred='PCA'))
colData(psce)<-cbind(colData(psce),new.annote)
psce
}
pumap<-clusterData(pumap)
phumap<-clusterData(phumap)
pp<-plotClusters(pumap)
pph<-plotClusters(phumap,'Phosphoproteomics')
```
The proteomics data clusters more cleanly when dimensionality reduction is applied:
```{r plot prot}
pp
```
However the phosphoproteomics data is a bit less robust
```{r plot phos}
pph
```
We can also move to bayes space to see how they compare
## BayesSpace Cluster and plotting function
We put all the functionality into a single function for now.
Here we do preprocessing, which works, cluster analysis, the clustering. We iterate through 1-4 clusters, but generally always use 3 since that is what we are expecting. However, it seems that 4 also works.
```{r spatial process, echo=FALSE}
bayesSpaceClusterPlot<-function(sce,prefix){
library(BayesSpace)
#pre-process data
set.seed(102)
panc <- spatialPreprocess(sce, platform="ST", skip.PCA=TRUE,
n.PCs=4, n.HVGs=500, log.normalize=FALSE)
##identify optimal number of cluseters
panc <- qTune(panc, qs=seq(1, 8), platform="ST", d=15,use.dimred='UMAP')
qPlot(panc)
##get optimal from data above. how do i do this?
optimal<-ll$q[which(ll$loglik==min(ll$loglik,na.rm=T))]
panc1 <- spatialCluster(panc, q=optimal, platform="ST", d=15,
init.method="kmeans", model="normal", gamma=2,
nrep=2000, burn.in=200,
save.chain=TRUE)
panc3 <- spatialCluster(panc, q=3, platform="ST", d=15,
init.method="kmeans", model="normal", gamma=2,
nrep=2000, burn.in=200,
save.chain=TRUE)
panc4 <- spatialCluster(panc, q=4, platform="ST", d=15,
init.method="kmeans", model="normal", gamma=2,
nrep=2000, burn.in=200,
save.chain=TRUE)
#pance2 <-spatialEnhance(panc1,q=optimal,d=15)
cols<- RColorBrewer::brewer.pal(10,'Dark2')#[c(1,2,3,6,4,5)]
c0=clusterPlot(panc,
palette=cols,
label='pulpAnnotation')+
ggtitle(paste(prefix,'Data'))
c1=clusterPlot(panc1,
palette=cols)+
ggtitle(paste(prefix,'data with optimal clusters'))
c2=clusterPlot(panc3,
palette=cols)+
ggtitle(paste(prefix,'data with 3 clusters'))
c3=clusterPlot(panc4,
palette=cols)+
ggtitle(paste(prefix,'data with 4 clusters'))
res<-cowplot::plot_grid(c0,c1,c2,c3)
return(res)
}
#do this for proteomics and phosphoproteomics data
p1<-bayesSpaceClusterPlot(pumap,"Global")
ggsave('globalBayesSpaceResults.png',p1)
ph1<-bayesSpaceClusterPlot(phumap,"Phospho")
ggsave('phosphoBayesSpaceResults.png',ph1)
```
This is not really wroking that well?!!?
## Compare clustering algorithms and labels
```{r cluster, message=F,warning=F}
clust<-colData(pumap)%>%
as.data.frame()%>%
dplyr::select(pulpAnnotation,rawClust,umapClust,pcaClust)
##just do this manually
cor(apply(clust,2,function(x) as.numeric(as.factor(x))),use='pairwise.complete.obs')
```