/
07_Combined_Clustering.Rmd
850 lines (693 loc) · 26.5 KB
/
07_Combined_Clustering.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
---
title: "Combined Clustering"
output:
workflowr::wflow_html:
code_folding: hide
df_print: paged
---
```{r knitr, include = FALSE}
DOCNAME = "07_Combined_Clustering"
knitr::opts_chunk$set(autodep = TRUE,
cache = TRUE,
cache.path = paste0("cache/", DOCNAME, "/"),
cache.comments = FALSE,
echo = TRUE,
error = FALSE,
fig.align = "center",
fig.width = 10,
fig.height = 8,
message = FALSE,
warning = FALSE)
```
```{r libaries, cache = FALSE}
# scRNA-seq
library("Seurat")
library("limma")
# Plotting
library("clustree")
library("viridis")
# Presentation
library("glue")
library("knitr")
# Parallel
library("BiocParallel")
# Paths
library("here")
# Output
library("writexl")
library("jsonlite")
# Tidyverse
library("tidyverse")
```
```{r source, cache = FALSE}
source(here("R/output.R"))
```
```{r paths}
combined.path <- here("data/processed/Combined_Seurat.Rds")
```
```{r bpparam, cache = FALSE}
bpparam <- MulticoreParam(workers = 10)
```
Introduction
============
In this document we are going to load the combined Organoids and Linstrom
dataset and complete the rest of the Seurat analysis.
```{r load, cache.extra = tools::md5sum(combined.path), cache.lazy = FALSE}
if (file.exists(combined.path)) {
combined <- read_rds(combined.path)
} else {
stop("Combined dataset is missing. ",
"Please run '06_Combined_Integration.Rmd' first.",
call. = FALSE)
}
```
Clustering
==========
Selecting resolution
--------------------
```{r cluster, cache.lazy = FALSE}
n.dims <- 20
resolutions <- seq(0, 1, 0.1)
combined <- FindClusters(combined, reduction.type = "cca.aligned",
dims.use = 1:n.dims, resolution = resolutions)
```
`Seurat` has a resolution parameter that indirectly controls the number of
clusters it produces. We tried clustering at a range of resolutions from
`r min(resolutions)` to `r max(resolutions)`.
### t-SNE plots {.tabset}
Here are t-SNE plots of the different clusterings.
```{r cluster-tSNE, results = "hide"}
src_list <- lapply(resolutions, function(res) {
src <- c("#### Res {{res}} {.unnumbered}",
"```{r cluster-tSNE-{{res}}}",
"TSNEPlot(combined, group.by = 'res.{{res}}', do.return = TRUE)",
"```",
"")
knit_expand(text = src)
})
out <- knit_child(text = unlist(src_list), options = list(cache = FALSE))
```
`r out`
### Clustering tree
#### Standard
Coloured by clustering resolution.
```{r clustree}
clustree(combined)
```
#### Stability
Coloured by the SC3 stability metric.
```{r clustree-stability}
clustree(combined, node_colour = "sc3_stability")
```
#### Gene expression {.tabset}
Coloured by the expression of some well-known kidney marker genes.
```{r select-clustree-genes}
genes <- c("PECAM1", "CDH5", "MEIS1", "PDGFRA", "HMGB2", "CENPA", "SIX1",
"DAPL1", "NPHS1", "PODXL", "S100A8", "TYROBP", "MAL", "EMX2",
"LRP2", "GATA3", "SLC12A1", "SPINT2", "TUBB2B", "STMN2", "TTHY1",
"HBA1", "HBG1")
is_present <- genes %in% rownames(combined@data)
```
> The following genes aren't present in this dataset and will be skipped:
> `r genes[!is_present]`
```{r clustree-genes, results = "hide"}
src_list <- lapply(genes[is_present], function(gene) {
src <- c("##### {{gene}} {.unnumbered}",
"```{r clustree-{{gene}}}",
"clustree(combined, node_colour = '{{gene}}',",
"node_colour_aggr = 'mean',",
"exprs = 'scale.data') +",
"scale_colour_viridis_c(option = 'plasma', begin = 0.3)",
"```",
"")
knit_expand(text = src)
})
out <- knit_child(text = unlist(src_list), options = list(cache = FALSE))
```
`r out`
### Selected resolution
```{r res, cache.lazy = FALSE}
res <- 0.5
combined <- SetIdent(combined,
ident.use = combined@meta.data[, paste0("res.", res)])
n.clusts <- length(unique(combined@ident))
```
Based on these plots we will use a resolution of `r res`.
Clusters
--------
Let's have a look at the clusters on a t-SNE plot.
```{r tSNE, fig.height = 6, cache.lazy = FALSE}
p1 <- TSNEPlot(combined, do.return = TRUE, pt.size = 0.5,
group.by = "DatasetSample")
p2 <- TSNEPlot(combined, do.label = TRUE, do.return = TRUE, pt.size = 0.5)
plot_grid(p1, p2)
```
We can also look at the number of cells in each cluster.
```{r cluster-sizes}
plot.data <- combined@meta.data %>%
select(Dataset, cluster = paste0("res.", res)) %>%
mutate(cluster = factor(as.numeric(cluster))) %>%
group_by(cluster, Dataset) %>%
summarise(count = n()) %>%
mutate(clust_total = sum(count)) %>%
mutate(clust_prop = count / clust_total) %>%
group_by(Dataset) %>%
mutate(dataset_total = sum(count)) %>%
ungroup() %>%
mutate(dataset_prop = count / dataset_total)
ggplot(plot.data, aes(x = cluster, y = count, fill = Dataset)) +
geom_col()
```
We are also interested in what proportions of the cells in each cluster come
from each datasets (i.e. are there dataset specific clusters?).
```{r cluster-props}
ggplot(plot.data, aes(x = cluster, y = clust_prop, fill = Dataset)) +
geom_col()
```
Alternatively we can look at what proportion of the cells in each **dataset**
are in each cluster. If each dataset has the same distribution of cell types the
heights of the bars should be the same.
```{r dataset-props}
ggplot(plot.data, aes(x = cluster, y = dataset_prop, fill = Dataset)) +
geom_col(position = position_dodge(0.9))
```
Marker genes
============
Clustering is not very useful if we don't know what cell types the clusters
represent. One way to work that out is to look at marker genes, genes that are
differentially expressed in one cluster compared to all other cells. Here we
use the Wilcoxon rank sum test genes that are present in at least 10 percent
of cells in at least one group (a cluster or all other cells).
```{r markers}
markers <- bplapply(seq_len(n.clusts) - 1, function(cl) {
cl.markers <- FindMarkers(combined, cl, logfc.threshold = 0, min.pct = 0.1,
print.bar = FALSE)
cl.markers$cluster <- cl
cl.markers$gene <- rownames(cl.markers)
return(cl.markers)
}, BPPARAM = bpparam)
markers <- bind_rows(markers) %>%
select(gene, cluster, everything())
```
Here we print out the top two markers for each cluster.
```{r print-markers}
markers %>% group_by(cluster) %>% top_n(2, abs(avg_logFC)) %>% data.frame
```
A heatmap can give us a better view. We show the top five positive marker genes
for each cluster.
```{r markers-heatmap, fig.height = 10}
top <- markers %>% group_by(cluster) %>% top_n(5, avg_logFC)
cols <- viridis(100)[c(1, 50, 100)]
DoHeatmap(combined, genes.use = top$gene, slim.col.label = TRUE,
remove.key = TRUE, col.low = cols[1], col.mid = cols[2],
col.high = cols[3])
```
By cluster {.tabset}
----------
```{r markers-cluster}
markers.list <- lapply(0:(n.clusts - 1), function(x) {
markers %>%
filter(cluster == x, p_val < 0.05) %>%
dplyr::arrange(-avg_logFC) %>%
select(Gene = gene, LogFC = avg_logFC, pVal = p_val)
})
names(markers.list) <- paste("Cluster", 0:(n.clusts - 1))
```
```{r marker-cluster-counts}
marker.summary <- markers.list %>%
map2_df(seq_along(markers.list), ~ mutate(.x, Cluster = .y)) %>%
mutate(IsUp = LogFC > 0) %>%
group_by(Cluster) %>%
summarise(Up = sum(IsUp), Down = sum(!IsUp)) %>%
mutate(Down = -Down) %>%
gather(key = "Direction", value = "Count", -Cluster)
ggplot(marker.summary,
aes(x = fct_rev(factor(Cluster)), y = Count, fill = Direction)) +
geom_col() +
geom_text(aes(y = Count + sign(Count) * max(abs(Count)) * 0.07,
label = abs(Count)),
size = 6, colour = "grey25") +
coord_flip() +
scale_fill_manual(values = c("#377eb8", "#e41a1c")) +
ggtitle("Number of identified genes") +
xlab("Cluster") +
theme(axis.title.x = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.position = "bottom")
```
We can also look at the full table of significant marker genes for each cluster.
```{r marker-cluster-table, results = "hide"}
src_list <- lapply(0:(n.clusts - 1), function(i) {
src <- c("### {{i}} {.unnumbered}",
"```{r marker-cluster-{{i}}}",
"markers.list[[{{i}} + 1]]",
"```",
"")
knit_expand(text = src)
})
out <- knit_child(text = unlist(src_list),
options = list(echo = FALSE, cache = FALSE))
```
`r out`
Conserved markers
=================
Here we are going to look for genes that are cluster markers in both the
Organoid and Lindstrom datasets. Each dataset will be tested individually and the
results combined to see if they are present in both datasets.
```{r skip-clusters, cache.lazy = FALSE}
combined@meta.data$Group <- gsub("[0-9]", "", combined@meta.data$Dataset)
skip <- combined@meta.data %>%
count(Group, Cluster = !! rlang::sym(paste0("res.", res))) %>%
spread(Group, n) %>%
replace_na(list(Organoid = 0L, Lindstrom = 0L)) %>%
rowwise() %>%
mutate(Skip = min(Organoid, Lindstrom) < 3) %>%
arrange(as.numeric(Cluster)) %>%
pull(Skip)
```
> **Skipped clusters**
>
> Testing conserved markers isn't possible for clusters that only contain cells
> from one dataset. In this case the following clusters are skipped:
> `r glue_collapse(seq(0, n.clusts - 1)[skip], sep = ", ", last = " and ")`
```{r con-markers-mc}
con.markers <- bplapply(seq_len(n.clusts) - 1, function(cl) {
if (skip[cl + 1]) {
message("Skipping cluster ", cl)
cl.markers <- c()
} else {
cl.markers <- FindConservedMarkers(combined, cl, grouping.var = "Group",
logfc.threshold = 0, min.pct = 0.1,
print.bar = FALSE)
cl.markers$cluster <- cl
cl.markers$gene <- rownames(cl.markers)
}
return(cl.markers)
}, BPPARAM = bpparam)
con.markers <- bind_rows(con.markers) %>%
mutate(mean_avg_logFC = rowMeans(select(., ends_with("avg_logFC")))) %>%
select(gene, cluster, mean_avg_logFC, max_pval, minimump_p_val,
everything())
```
Here we print out the top two conserved markers for each cluster.
```{r print-con-markers}
con.markers %>%
group_by(cluster) %>%
top_n(2, abs(mean_avg_logFC)) %>%
data.frame
```
Again a heatmap can give us a better view. We show the top five positive
conserved marker genes for each cluster.
```{r con-markers-heatmap, fig.height = 10}
top <- con.markers %>% group_by(cluster) %>% top_n(5, mean_avg_logFC)
cols <- viridis(100)[c(1, 50, 100)]
DoHeatmap(combined, genes.use = top$gene, slim.col.label = TRUE,
remove.key = TRUE, col.low = cols[1], col.mid = cols[2],
col.high = cols[3])
```
By cluster {.tabset}
----------
```{r con-markers-cluster}
con.markers.list <- lapply(0:(n.clusts - 1), function(x) {
con.markers %>%
filter(cluster == x, max_pval < 0.05) %>%
dplyr::arrange(-mean_avg_logFC) %>%
select(Gene = gene,
MeanLogFC= mean_avg_logFC,
MaxPVal = max_pval,
MinPVal = minimump_p_val,
OrganoidLogFC = Organoid_avg_logFC,
OrganoidPVal = Organoid_p_val,
LindstromLogFC = Lindstrom_avg_logFC,
LindstromPVal = Lindstrom_p_val)
})
names(con.markers.list) <- paste("Cluster", 0:(n.clusts - 1))
```
```{r con-marker-cluster-counts}
con.marker.summary <- con.markers.list %>%
map2_df(seq_along(con.markers.list), ~ mutate(.x, Cluster = .y)) %>%
mutate(IsUp = MeanLogFC > 0) %>%
group_by(Cluster) %>%
summarise(Up = sum(IsUp), Down = sum(!IsUp)) %>%
mutate(Down = -Down) %>%
gather(key = "Direction", value = "Count", -Cluster)
ggplot(con.marker.summary,
aes(x = fct_rev(factor(Cluster)), y = Count, fill = Direction)) +
geom_col() +
geom_text(aes(y = Count + sign(Count) * max(abs(Count)) * 0.07,
label = abs(Count)),
size = 6, colour = "grey25") +
coord_flip() +
scale_fill_manual(values = c("#377eb8", "#e41a1c")) +
ggtitle("Number of identified genes") +
xlab("Cluster") +
theme(axis.title.x = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.position = "bottom")
```
We can also look at the full table of significant conserved marker genes for
each cluster.
```{r con-marker-cluster-table, results = "hide"}
src_list <- lapply(0:(length(con.markers.list)-1), function(i) {
src <- c("### {{i}} {.unnumbered}",
"```{r con-marker-cluster-{{i}}}",
"con.markers.list[[{{i}} + 1]]",
"```",
"")
knit_expand(text = src)
})
out <- knit_child(text = unlist(src_list),
options = list(echo = FALSE, cache = FALSE))
```
`r out`
Within cluster DE
=================
We can also look for genes that are differentially expressed between the two
datasets in the same cluster. This might help to identify differences in the
same cell type between the difference experiments.
```{r set-ident, cache.lazy = FALSE}
combined@meta.data$GroupCluster <- paste(combined@meta.data$Group,
combined@ident, sep = "_")
combined <- StashIdent(combined, save.name = "Cluster")
combined <- SetAllIdent(combined, id = "GroupCluster")
```
```{r de-plots, fig.height = 8}
plot.data <- AverageExpression(combined, show.progress = FALSE) %>%
rownames_to_column("Gene") %>%
gather(key = "GroupCluster", value = "AvgExp", -Gene) %>%
separate(GroupCluster, c("Group", "Cluster"), sep = "_") %>%
mutate(Cluster = factor(as.numeric(Cluster))) %>%
mutate(LogAvgExp = log1p(AvgExp)) %>%
select(-AvgExp) %>%
spread(Group, LogAvgExp) %>%
replace_na(list(Organoid = 0, Lindstrom = 0)) %>%
mutate(Avg = 0.5 * (Organoid + Lindstrom),
Diff = Organoid - Lindstrom)
ggplot(plot.data, aes(x = Avg, y = Diff)) +
geom_hline(yintercept = 0, colour = "red") +
geom_point(size = 0.6, alpha = 0.2) +
xlab("0.5 * (Organoid + Lindstrom)") +
ylab("Organoid - Lindstrom") +
facet_wrap(~ Cluster)
```
```{r cluster-de-mc}
cluster.de <- bplapply(seq_len(n.clusts) - 1, function(cl) {
if (skip[cl + 1]) {
message("Skipping cluster ", cl)
cl.de <- c()
} else {
cl.de <- FindMarkers(combined, paste("Organoid", cl, sep = "_"),
paste("Lindstrom", cl, sep = "_"),
logfc.threshold = 0, min.pct = 0.1,
print.bar = FALSE)
cl.de$cluster <- cl
cl.de$gene <- rownames(cl.de)
}
return(cl.de)
}, BPPARAM = bpparam)
cluster.de <- bind_rows(cluster.de) %>%
select(gene, cluster, everything())
```
Here we print out the top two DE genes for each cluster.
```{r print-de}
cluster.de %>% group_by(cluster) %>% top_n(2, abs(avg_logFC)) %>% data.frame
```
Again a heatmap can give us a better view. We show the top five positive DE
genes for each cluster.
```{r de-heatmap, fig.height = 10}
top <- cluster.de %>% group_by(cluster) %>% top_n(5, avg_logFC)
cols <- viridis(100)[c(1, 50, 100)]
DoHeatmap(combined, genes.use = top$gene, slim.col.label = TRUE,
remove.key = TRUE, col.low = cols[1], col.mid = cols[2],
col.high = cols[3])
```
By cluster {.tabset}
----------
```{r de-cluster}
cluster.de.list <- lapply(0:(n.clusts - 1), function(x) {
cluster.de %>%
filter(cluster == x, p_val < 0.05) %>%
dplyr::arrange(p_val) %>%
select(Gene = gene, LogFC = avg_logFC, pVal = p_val)
})
names(cluster.de.list) <- paste("Cluster", 0:(n.clusts - 1))
```
```{r de-cluster-counts}
cluster.de.summary <- cluster.de.list %>%
map2_df(seq_along(cluster.de.list), ~ mutate(.x, Cluster = .y)) %>%
mutate(IsUp = LogFC > 0) %>%
group_by(Cluster) %>%
summarise(Up = sum(IsUp), Down = sum(!IsUp)) %>%
mutate(Down = -Down) %>%
gather(key = "Direction", value = "Count", -Cluster)
ggplot(cluster.de.summary,
aes(x = fct_rev(factor(Cluster)), y = Count, fill = Direction)) +
geom_col() +
geom_text(aes(y = Count + sign(Count) * max(abs(Count)) * 0.07,
label = abs(Count)),
size = 6, colour = "grey25") +
coord_flip() +
scale_fill_manual(values = c("#377eb8", "#e41a1c")) +
ggtitle("Number of identified genes") +
xlab("Cluster") +
theme(axis.title.x = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.position = "bottom")
```
We can also look at the full table of significant DE genes for each cluster.
```{r de-cluster-table, results = "hide"}
src_list <- lapply(0:(length(cluster.de.list) - 1), function(i) {
src <- c("### {{i}} {.unnumbered}",
"```{r de-cluster-{{i}}}",
"cluster.de.list[[{{i}} + 1]]",
"```",
"")
knit_expand(text = src)
})
out <- knit_child(text = unlist(src_list),
options = list(echo = FALSE, cache = FALSE))
```
`r out`
Traditional DE
==============
To see if there are any global differences between the datasets we are going to
perform traditional differential expression testing between the two groups.
```{r set-ident-DE, cache.lazy = FALSE}
combined <- SetAllIdent(combined, id = "Group")
```
```{r de}
de <- FindMarkers(combined, "Organoid", "Lindstrom", logfc.threshold = 0,
min.pct = 0.1, print.bar = FALSE)
```
```{r print-trad-de}
de <- de %>% rownames_to_column("gene")
de %>% top_n(10, avg_logFC) %>% data.frame
```
```{r reset-ident, cache.lazy = FALSE}
combined <- SetAllIdent(combined, id = "Cluster")
```
From these results we can identify an overall signature of the differences
between the datasets by selecting significant genes with an absolute log
foldchange greater than 0.5.
```{r de-sig}
de.sig <- de %>%
filter(p_val_adj < 0.05, abs(avg_logFC) > 0.5) %>%
pull(gene)
```
This identifies a signature with `r length(de.sig)` genes.
This signature can then be removed from the within cluster differential
expression results to better highlight biological differences.
```{r filter-cluster-de}
cluster.de.filt <- filter(cluster.de, !(gene %in% de.sig))
cluster.de.list.filt <- map(cluster.de.list,
function(x) {filter(x, !(Gene %in% de.sig))})
```
Gene plots
==========
Plots of known kidney genes we are specifically interested in.
```{r split-dotplot}
SplitDotPlotGG(combined, grouping.var = "Group",
genes.plot = c("ZEB2", "PDGFRA", "PDGFRB", "MFAP4", "KCNE4",
"TCF12", "REN", "DLK1", "GATA3", "TCF21",
"PRRX1", "DCN", "PECAM1", "CDH5", "KDR", "PAX2",
"LYPD1", "DAPL1", "PODXL", "NPHS2", "MAFB",
"HMGB2", "CENPU", "SFRP2", "NFIA", "ASPN",
"CENPA", "IGFBP7", "EMX2", "MAL"),
cols.use = c("#E41A1C", "#377EB8"),
x.lab.rot = TRUE)
```
```{r split-dotplot2}
SplitDotPlotGG(combined, grouping.var = "Group",
genes.plot = c("SULT1E1", "ACTA2", "TAGLN", "MAB21L2", "CXCL14",
"PRRX1", "REN", "DLK1", "GATA3", "CRABP1",
"FIBIN", "OGN", "PECAM1", "CDH5", "KDR", "LYPD1",
"DAPL1", "TMEM100", "PODXL", "NPHS2", "MAFB",
"HIST1H1A", "HMGB2", "CENPU", "DCN", "LUM",
"SFRP2", "IGFBP7", "EMX2", "MAL", "TTYH1",
"FABP7", "TUBB2B", "TYROBP", "LYZ", "S100A9",
"STMN2", "ATOH1", "HBG2", "HBG1",
"HBB"),
cols.use = c("#E41A1C", "#377EB8"),
x.lab.rot = TRUE)
```
```{r split-dotplot3}
SplitDotPlotGG(combined, grouping.var = "Group",
genes.plot = c("HSPA1A", "DNAJB1", "FOS", "H3F3A", "EEF1A1",
"MARCKS"),
cols.use = c("#E41A1C", "#377EB8"),
x.lab.rot = TRUE)
```
```{r feature-plots}
FeaturePlot(combined, c("HSPA1A", "DNAJB1", "FOS", "H3F3A", "EEF1A1", "MARCKS"),
cols.use = viridis(100), no.axes = TRUE)
```
Heat shock response
-------------------
```{r heatshock}
hs.genes <- read_tsv(here("data/response_to_heat_shock_BP.txt"),
col_names = c("gene", "description"),
col_types = cols(
gene = col_character(),
description = col_character()
))
fc <- de$avg_logFC
idx <- rownames(de) %in% hs.genes$gene
barcodeplot(fc, idx)
idx <- rownames(combined@data) %in% hs.genes$gene
group.fac <- factor(combined@meta.data$Group,
levels = c("Organoid", "Lindstrom"))
des.mat <- cbind(Intercept = 1, Group = as.numeric(group.fac) - 1)
roast.res <- roast(combined@data, idx, des.mat)
```
A ROAST test for this gene set testing for up-regulation in the Lindstrom data
gives a p-value of `r roast.res$p.value["Up", "P.Value"]`.
Summary
=======
Parameters
----------
This table describes parameters used and set in this document.
```{r parameters}
params <- toJSON(list(
list(
Parameter = "resolutions",
Value = resolutions,
Description = "Range of possible clustering resolutions"
),
list(
Parameter = "res",
Value = res,
Description = "Selected resolution parameter for clustering"
),
list(
Parameter = "n.clusts",
Value = n.clusts,
Description = "Number of clusters produced by selected resolution"
),
list(
Parameter = "skipped",
Value = paste(seq(0, n.clusts - 1))[skip],
Description = "Clusters skipped for conserved marker and DE testing"
),
list(
Parameter = "n.sig",
Value = length(de.sig),
Description = "Number of genes in the group DE signature"
)
), pretty = TRUE)
kable(fromJSON(params))
```
Output files
------------
This table describes the output files produced by this document. Right click
and _Save Link As..._ to download the results.
```{r write-combined}
write_rds(combined, here("data/processed/Combined_clustered.Rds"))
```
```{r cluster-details}
expr <- AverageExpression(combined, show.progress = FALSE) %>%
rename_all(function(x) {paste0("Mean", x)}) %>%
rownames_to_column("Gene")
prop <- AverageDetectionRate(combined) %>%
rename_all(function(x) {paste0("Prop", x)}) %>%
rownames_to_column("Gene")
alt.cols <- c(rbind(colnames(prop), colnames(expr)))[-1]
cluster.expr <- expr %>%
left_join(prop, by = "Gene") %>%
select(alt.cols)
cluster.assign <- combined@meta.data %>%
select(Cell, Dataset, Sample, Barcode, Cluster)
```
```{r output}
dir.create(here("output", DOCNAME), showWarnings = FALSE)
write_lines(params, here("output", DOCNAME, "parameters.json"))
write_csv(cluster.assign, here("output", DOCNAME, "cluster_assignments.csv"))
write_csv(cluster.expr, here("output", DOCNAME, "cluster_expression.csv"))
write_csv(markers, here("output", DOCNAME, "markers.csv"))
write_xlsx(markers.list, here("output", DOCNAME, "markers.xlsx"))
write_csv(con.markers, here("output", DOCNAME, "conserved_markers.csv"))
write_xlsx(con.markers.list, here("output", DOCNAME, "conserved_markers.xlsx"))
write_csv(cluster.de, here("output", DOCNAME, "cluster_de.csv"))
write_xlsx(cluster.de.list, here("output", DOCNAME, "cluster_de.xlsx"))
write_csv(de, here("output", DOCNAME, "group_de.csv"))
write_csv(data.frame(Gene = de.sig),
here("output", DOCNAME, "de_signature.csv"))
write_csv(cluster.de.filt, here("output", DOCNAME, "cluster_de_filtered.csv"))
write_xlsx(cluster.de.list.filt,
here("output", DOCNAME, "cluster_de_filtered.xlsx"))
kable(data.frame(
File = c(
glue("[parameters.json]({getDownloadURL('parameters.json', DOCNAME)})"),
glue("[cluster_assignments.csv]",
"({getDownloadURL('cluster_assignments.csv', DOCNAME)})"),
glue("[cluster_expression.csv]",
"({getDownloadURL('cluster_expression.csv', DOCNAME)})"),
glue("[markers.csv]({getDownloadURL('markers.csv', DOCNAME)})"),
glue("[markers.xlsx]({getDownloadURL('markers.xlsx', DOCNAME)})"),
glue("[conserved_markers.csv]",
"({getDownloadURL('conserved_markers.csv', DOCNAME)})"),
glue("[conserved_markers.xlsx]",
"({getDownloadURL('conserved_markers.xlsx', DOCNAME)})"),
glue("[cluster_de.csv]",
"({getDownloadURL('cluster_de.csv', DOCNAME)})"),
glue("[cluster_de.xlsx]",
"({getDownloadURL('cluster_de.xlsx', DOCNAME)})"),
glue("[group_de.csv]({getDownloadURL('group_de.csv', DOCNAME)})"),
glue("[de_signature.csv]",
"({getDownloadURL('de_signature.csv', DOCNAME)})"),
glue("[cluster_de_filtered.csv]",
"({getDownloadURL('cluster_de_filtered.csv', DOCNAME)})"),
glue("[cluster_de_filtered.xlsx]",
"({getDownloadURL('cluster_de_filtered.xlsx', DOCNAME)})")
),
Description = c(
"Parameters set and used in this analysis",
"Cluster assignments for each cell",
"Cluster expression for each gene",
"Results of marker gene testing in CSV format",
paste("Results of marker gene testing in XLSX format with one tab",
"per cluster"),
"Results of conserved marker gene testing in CSV format",
paste("Results of conserved marker gene testing in XLSX format with",
"one tab per cluster"),
paste("Results of within cluster differential expression testing",
"in CSV format"),
paste("Results of within cluster differential expression testing",
"in XLSX format with one cluster per tab"),
paste("Results of between group differential expression testing",
"in CSV format"),
"Between group differential expression signature genes",
paste("Results of within cluster differential expression testing",
"after removing group DE signature in CSV format"),
paste("Results of within cluster differential expression testing",
"after removing group DE signature in XLSX format with one",
"cluster per tab")
)
)
)
```