-
Notifications
You must be signed in to change notification settings - Fork 3
/
Extend_data_1.Rmd
executable file
·271 lines (212 loc) · 9.33 KB
/
Extend_data_1.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
---
title: "Atlas_UMIs"
author: "yejg"
date: "2017/12/25"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Library necessary packages
```{r,warning=FALSE,echo=FALSE,message=FALSE}
library(NMF)
library(rsvd)
library(Rtsne)
library(ggplot2)
library(cowplot)
library(sva)
library(igraph)
library(cccd)
library(KernSmooth)
library(beeswarm)
library(stringr)
library(formatR)
```
### Load data
```{r,warning=F,message=FALSE}
source('Fxns.R')
atlas_umis<-load_data('./Extend_data/GSE92332_atlas_UMIcounts.txt.gz')
atlas_tpm = data.frame(log2(1+tpm(atlas_umis)))
```
### Select variables
```{r,warning=FALSE,message=FALSE}
v = get.variable.genes(atlas_umis, min.cv2 = 100)
var.genes = as.character(rownames(v)[v$p.adj<0.05])
```
### Check batch effect
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
get_field = function(string,field=1,delim="_", fixed=T) return(strsplit(string,delim, fixed=fixed)[[1]][field])
batch.labels = factor(unlist(lapply(colnames(atlas_umis), get_field, 1,"_")))
table(batch.labels)
```
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
batch_mean_tpm = group.means(counts = atlas_tpm, groups = batch.labels)
x = batch_mean_tpm[, 1]
y = batch_mean_tpm[,2]
expr.cor = round(cor(x,y),2)
smoothScatter(x, y, nrpoints=Inf, pch=16, cex=0.25, main=sprintf("Before batch correction, correlation between \ntwo illustrative batches is %s", expr.cor),
xlab="All genes Batch 2, mean log2(TPM+1)", ylab="All genes Batch 1, mean log2(TPM+1)")
```
### Compensate for batch effect using ComBat
```{r,warning=FALSE,message=FALSE}
# Takes a few minutes
atlas_tpm_norm = batch.normalise.comBat(counts = as.matrix(atlas_tpm), batch.groups = batch.labels)
batch_mean_tpm_norm = group.means(counts = atlas_tpm_norm, groups = batch.labels)
x = batch_mean_tpm_norm[, 1]
y = batch_mean_tpm_norm[,2]
expr.cor = round(cor(x,y),2)
smoothScatter(x, y, nrpoints=Inf, pch=16, cex=0.25, main=sprintf("After batch correction, correlation between \ntwo illustrative batches is %s", expr.cor),
xlab="All genes Batch 2, mean log2(TPM+1)", ylab="All genes Batch 1, mean log2(TPM+1)")
```
### Figure c
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
cell_groups<-unlist(lapply(colnames(atlas_umis),function(x){
return(str_split(x,'_')[[1]][3])
}))
cell_types<-unique(cell_groups)
colors = c('#483D8B','#00FFFF','#EEE8AA','#CD5C5C','#CD853F','#B22222','#CDC9C9','#7CFC00',
'#668B8B','#008B45','#FF6A6A','#8B4726','#FF3030','#8B0A50','#4F4F4F')
cell_batch<-as.matrix(table(cell_groups,batch.labels))
cell_names<-rownames(cell_batch)
cell_tables<-table(cell_groups)
#jpeg(file='./data/Extend_data_1/Figure_b.jpeg')
#par(mfrow=c(5,4))
for(i in 1:length(cell_types)){
#pie(cell_batch[i,],labels = names(cell_batch[i,]),main=str_c(cell_names[i],'(n=',cell_tables[i],')'))
if(i==length(cell_types)){
pie(cell_batch[i,],labels=colnames(cell_batch[i,]),col=colors,main=str_c(cell_names[i],'(n=',cell_tables[i],')'))
}else{
pie(cell_batch[i,],labels=NA,col=colors,main=str_c(cell_names[i],'(n=',cell_tables[i],')'))
}
}
#dev.off()
#par(mfrow=c(1,1))
#legend('bottomright',legend =colnames(cell_batch),fill = colors,horiz = TRUE) # not good ,will try more
```
### Figure d
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
XcellTypes<-c("Endocrine","Enterocyte.Mature.Distal","Enterocyte.Mature.Proximal",
"Goblet","Paneth","Stem", "Tuft" )
Xcell<-atlas_tpm[,colnames(atlas_tpm)[cell_groups%in%XcellTypes]]
Xcell_types<-unlist(lapply(colnames(Xcell),function(x){
return(str_split(x,'_')[[1]][3])
}))
Xcell_batches<-unlist(lapply(colnames(Xcell),function(x){
return(str_split(x,'_')[[1]][1])
}))
Xcell_table<-as.matrix(table(Xcell_types,Xcell_batches))
Xcell_table_fac<-Xcell_table/apply(Xcell_table,1,sum)
Xcell_table_fac<-as.data.frame.matrix(Xcell_table_fac)
#jpeg(file='./data/Extend_data_1/Figure_d.jpeg')
beeswarm(as.data.frame(t(Xcell_table_fac)),las=2,col='blue',pch=20,ylab=c('Factions of cells')) #,col = as.numeric(as.factor(rownames(Xcell_table_fac))))
bxplot(as.data.frame(t(Xcell_table_fac)),probs=0.5,add=T)
#dev.off()
#Boxplot(t(Xcell_table_fac),id.method='none',outline=F,notch=F,las=2)
```
### Figure e
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
Stem_cells<-atlas_tpm[var.genes,cell_groups=='Stem']
stem_pearson<-cor(Stem_cells,method = 'pearson')
stem_pearson_matrix<-as.matrix(stem_pearson)
stem_pearson_vec<-stem_pearson_matrix[upper.tri(stem_pearson_matrix)]
get_cell_tpm<-function(cell){
cell_matrix<-as.matrix(atlas_tpm[var.genes,cell_groups==cell])
cell_pearson<-cor(cell_matrix,method = 'pearson')
cell_pearson_vec<-cell_pearson[upper.tri(cell_pearson)]
return(cell_pearson_vec)
}
cell_pearson_list<-lapply(cell_types,get_cell_tpm)
#jpeg(file='./data/Extend_data_1/Figure_e.jpeg')
boxplot(cell_pearson_list,names=cell_types,outline=F,horizontal=T,las=2,
xlab=c('Pearson correlation between mice,n=6'),ylab.cex=0.3)
#dev.off()
```
### Figure g
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
tsne.rot<-PCA_TSNE.scores(data.tpm = atlas_tpm_norm,data.umis = atlas_umis,
var_genes = var.genes,data_name = './Extend_data/atlas')
tsne.rot<-data.frame(tsne.rot)
colnames(tsne.rot)<-c('tSNE_1','tSNE_2')
pca<-read.table('./Extend_data/atlas_pca_scores.txt')
```
#### Heatmap
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
# Unsupervised clustering
## Run kNN-graph clustering
# build cell-cell euclidean distance matrix using significant PC scores
dm = as.matrix(dist(pca[, 1:13]))
# build nearest neighbor graph
knn = build_knn_graph(dm, k = 200)
clustering = cluster_graph(knn)$partition
# merge a spurious cluster (cluster 16 is only a single cell) into the most similar cluster
clustering = merge_clusters(clustering, c(8, 16))
## confirm that clusters are extremely similar to those in the paper (infomap is a random-walk based alg, so there may begetwd minor differences)
clusters_from_paper = factor(unlist(lapply(colnames(atlas_umis), get_field, 3,"_")))
overlap = as.data.frame.matrix(table(clusters_from_paper, clustering))
overlap = round(sweep(overlap,2,colSums(overlap),`/`),2)
#jpeg(file='./data/Extend_data_1/aheatmap.jpeg')
aheatmap(overlap, color = cubehelix1.16, border_color = list("cell"="white"), txt=overlap, Colv = NA, Rowv = NA)
#dev.off()
```
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
ggplot(tsne.rot, aes(x=tSNE_1, y=tSNE_2, color=cell_groups)) + geom_point() + scale_color_manual(values=brewer16)
```
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
Genes_mean_tpm<-function(genes,tpm_data,tsne_data,title,fun=mean,doplot=TRUE){
Log2TPM<-as.numeric(apply(tpm_data[genes,],2,fun))
if(doplot){
title_1<-paste(genes,collapse = ',')
title_2<-paste(title,'(',title_1,')',sep='')
ggplot(tsne_data, aes(x=tSNE_1, y=tSNE_2))+geom_point(aes(color=Log2TPM))+theme(legend.title = element_text(size=8,color='blue',face='bold'),
legend.position = 'right') +ggtitle(title_2)+
scale_color_gradient2(low='lightblue',mid='green',high='red')
}
else{
return(Log2TPM)
}
}
```
#### Mark genes
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
stem_mark_genes<-c('Lgr5','Ascl2','Slc12a2','Axin2','Olfm4','Gkn3')
Genes_mean_tpm(stem_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Stem')
# Cell cycle
cell_cycle_mark_genes<-c('Mki67','Cdk4','Mcm5','Mcm6','Pcna')
Genes_mean_tpm(cell_cycle_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Cell cycle')
# Enterocyte
Enterocyte_mark_genes<-c('Alpi','Apoa1','Apoa4','Fabp1')
Genes_mean_tpm(Enterocyte_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Enterocyte')
# Globlet
Globlet_mark_genes<-c('Muc2','Clca1','Tff3','Agr2')
Genes_mean_tpm(Globlet_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Globet')
# Paneth
Paneth_mark_genes<-c('Lyz1','Defa17','Defa22','Defa24','Ang4')
Genes_mean_tpm(Paneth_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Paneth')
# Enteroendocrine
Enteroendocrine_mark_genes<-c('Chga','Chgb','Tac1','Tph1','Neurog3')
Genes_mean_tpm(Enteroendocrine_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Enteroendocrine')
# Tuft
Tuft_mark_genes<-c('Dclk1','Trpm5','Gfi1b','Il25')
Genes_mean_tpm(Tuft_mark_genes,tpm_data = atlas_tpm_norm,tsne_data =tsne.rot,title = 'Tuft')
```
### reads,umis
```{r,tidy=TRUE,message=FALSE,warning=FALSE}
per_cell_umis<-as.numeric(apply(atlas_umis[var.genes,],2,sum))
ggplot(tsne.rot, aes(x=tSNE_1, y=tSNE_2))+geom_point(aes(color=per_cell_umis))+theme(legend.title = element_text("UMIS/Cell",size=8,color='blue',face='bold'),
legend.position = 'right') +scale_color_gradient2(low='lightblue',mid='green',high='red')
```
### number of detected genes
```{r,tidy=TRUE,warning=FALSE,message=FALSE}
Count_genes<-function(x){
count<-0
for(c in x){
if(c!=0){
count<-count+1
}
}
return(count)
}
Genes_per_cell<-as.numeric(apply(atlas_umis,2,Count_genes))
ggplot(tsne.rot, aes(x=tSNE_1, y=tSNE_2))+geom_point(aes(color=Genes_per_cell))+theme(legend.title = element_text("Genes/Cell",size=8,color='blue',face='bold'),
legend.position = 'right') +scale_color_gradient2(low='lightblue',mid='green',high='red')
```