-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEarly_vs_Late_Analysis.Rmd
More file actions
1978 lines (1753 loc) · 66.5 KB
/
Early_vs_Late_Analysis.Rmd
File metadata and controls
1978 lines (1753 loc) · 66.5 KB
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Data Analysis for Early and Late Lung-colonizing Tumors"
author: "Emily Franz, Ryan Roberts, Yogesh Budhathoki and Matt Cannon"
date: "`r format(Sys.time(), '%m/%d/%Y')`"
output:
html_document:
toc: true
toc_float: true
toc_depth: 5
number_sections: false
code_folding: hide
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
tidy = TRUE,
echo = TRUE,
cache = TRUE,
collapse = TRUE,
tidy.opts = list(width.cutoff = 95),
message = FALSE,
warning = FALSE,
cache.lazy = FALSE
)
```
```{r lib}
# Load packages
library(Seurat)
library(tidyverse)
library(ggplot2)
library(rrrSingleCellUtils)
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
library(patchwork)
library(msigdbr)
# Set random generator seed to facilitate reproducibility
set.seed(888)
```
## Helper functions
```{r helper_functions}
theme_robert <- function(axis_font_size = 5,
axis_title_font_size = 8,
facet_font_size = 5,
font_name = "Arial",
legend_key_size = "0.2",
legend_text_font_size = 5,
legend_title_font_size = 6,
subtitle_font_size = 6,
title_font_size = 10) {
if (font_name == "Arial" &
!"Arial" %in% names(grDevices::pdfFonts())) {
# !"Arial" %in% sysfonts::font_families()) {
message("Adding Arial font to system")
tryCatch(
{
extrafont::font_import(paths = "/home/gdrobertslab/lab/Tools/fonts/Arial/",
prompt = FALSE)
extrafont::loadfonts(quiet = TRUE)
},
error = function(e) {
message("Arial font files not found in /home/gdrobertslab/lab/Tools/fonts/Arial/")
message("Either use a different font or install Arial")
print(e)
}
)
}
list(ggplot2::theme(
axis.text = ggplot2::element_text(size = axis_font_size),
axis.title = ggplot2::element_text(size = axis_title_font_size),
plot.title = ggplot2::element_text(size = title_font_size,
face = "bold",
hjust = 0.5),
strip.text = ggplot2::element_text(size = facet_font_size,
face = "bold"),
plot.subtitle = ggplot2::element_text(hjust = 0.5,
size = subtitle_font_size),
legend.text = ggplot2::element_text(size = legend_text_font_size),
legend.title = ggplot2::element_text(size = legend_title_font_size,
face = "bold"),
strip.background = ggplot2::element_blank(),
legend.key.size = ggplot2::unit(0.2, "cm"),
legend.position = "top"),
ggplot2::scale_color_manual(values = plot_cols))
}
```
## Make up directory structure
```{bash mkdirs, eval=FALSE}
for directoryName in \
output \
output/figures \
output/rdata \
output/de \
output/gsea
do
if [ ! -d ${directoryName} ]
then
mkdir ${directoryName}
fi
done
```
# Load and Process Datasets
Datasets are from tumors collected from mouse lungs at early (day 14 post injection of tumor cells) and late (day 35 post injection of tumor cells) timepoints and at the time of injection. mCherry was used to label the tumor cells as well as the surrounding niche cells.
```{r load}
sample_list <- read_tsv("misc/sample_cutoffs.txt", show_col_types = FALSE)
sobj_list <- parallel::mclapply(seq_len(nrow(sample_list)), function(i) {
message(paste("Starting sample", i))
species_pattern <- sample_list$species_pattern[i]
obj_name <- sample_list$obj_name[i]
sobj <-
tenx_load_qc(paste0("/home/gdrobertslab/lab/Counts/",
sample_list$sample_name[i],
"/filtered_feature_bc_matrix"),
violin_plot = FALSE,
species_pattern = species_pattern)
# Add metadata to dataset
for (colname in colnames(sample_list)) {
sobj[[colname]] <- sample_list[[colname]][i]
}
cutoff_tibble <-
tribble(~feature, ~min_val, ~max_val,
"nCount_RNA",
sample_list$ncount_rna_min[i],
sample_list$ncount_rna_max[i],
"percent.mt",
0,
sample_list$mt_max[i])
plotted <-
feature_hist(sobj,
features = c("nCount_RNA", "percent.mt"),
cutoff_table = cutoff_tibble)
ggsave(paste0("output/figures/feature_hist_",
obj_name,
".png"),
width = 10,
height = 10,
plot = plotted)
sobj <-
sobj %>%
subset(nCount_RNA >= sample_list$ncount_rna_min[i] &
nCount_RNA <= sample_list$ncount_rna_max[i] &
percent.mt <= sample_list$mt_max[i]) %>%
process_seurat()
message(paste("Sample", i, "completed"))
return(sobj)
}, mc.cores = parallelly::availableCores())
names(sobj_list) <- sample_list$obj_name
qs::qsave(sobj_list,
file = "output/rdata/sobj_list.qs")
```
## UMAP with all samples
```{r umap_alldependson='load', cache.vars=''}
sobj_list <- qs::qread("output/rdata/sobj_list.qs")
all_samples <-
merge(sobj_list[[1]],
y = sobj_list[2:length(sobj_list)],
add.cell.ids = names(sobj_list)) %>%
process_seurat(resolution = 0.2)
plot_name <-
DimPlot(all_samples,
group.by = "sample_description",
split.by = "sample_description",
shuffle = TRUE,
ncol = 2,
cols = plot_cols[seq_along(sobj_list)],
pt.size = 0.01,
order = FALSE) +
theme_robert() +
labs(title = NULL)
plot_name
ggsave("output/figures/umap_all_samples.pdf",
width = 4,
height = 6,
plot = plot_name)
```
## Human Cell Only Processing
```{r human-processing, dependson="load"}
# Human
# Normalize the number of cells per sample
n_cells <- c()
for (i in seq_along(sobj_list)) {
n_cells[i] <- length(colnames(sobj_list[[i]]))
}
names(n_cells) <- names(sobj_list)
# Merge day 14 and day 28 Seurat objects together
tumor <-
merge(sobj_list[["d14_pos_h"]],
y = sobj_list[c("d28_pos_h", "tibia_tumor_h")],
add.cell.ids = c("d14", "d28", "tibia"),
project = "Early vs Late OS Mets") %>%
NormalizeData() %>%
ScaleData() %>%
FindVariableFeatures() %>%
RunPCA() %>%
FindNeighbors(dims = 1:30) %>%
FindClusters(resolution = 0.15, k.param = 30) %>%
RunUMAP(dims = 1:30, n.neighbors = 30L, metric = "euclidean")
tumor$group <-
factor(str_replace_all(tumor$group, "_", " "),
levels = c("Primary tumor",
"Early metastatic tumor",
"Midpoint metastatic tumor",
"Late metastatic tumor"))
# Save human mCherry+ merged object
qs::qsave(tumor, file = "output/rdata/human_tumor.qs")
# Use timepoint naming (Day 14/Day 28) for UMAP plotting
umap_timepoint <-
r_dim_plot(tumor,
title = "Tumor at metastatic timepoints",
group.by = "timepoint",
label = TRUE)
umap_timepoint
pdf("output/figures/human_umap_timepoint.pdf",
width = 5,
height = 5)
umap_timepoint
dev.off()
# Use basic naming (early/late) for UMAP plotting
umap_group <-
r_dim_plot(tumor,
title = "Tumor at metastatic timepoints",
group.by = "group",
label = TRUE,
repel = TRUE)
umap_group
pdf("output/figures/human_umap_group.pdf",
width = 5,
height = 5)
umap_group
dev.off()
# Reorder the group metadata variable
tumor$group[tumor$group == "Early metastatic tumor"] <- "Early"
tumor$group[tumor$group == "Midpoint metastatic tumor"] <- "Established"
tumor$group[tumor$group == "Primary tumor"] <- "Primary"
tumor$group <- factor(tumor$group, levels = c("Primary", "Early", "Established"))
umap_group_split <-
r_dim_plot(tumor,
title = "Tumor at metastatic timepoints",
split.by = "group",
label = TRUE,
repel = TRUE)
umap_group_split
```
## Human Cells Only: Pre-cell cycle regression
```{r human_geneplots, dependson=c("load","human-processing")}
# Dot plot of expression of genes of interest by group (early/late)
gene_list <-
c("BCL2",
"BCL2L1",
"BCL2L2",
"MCL1")
dot_plot <-
DotPlot(tumor,
features = gene_list,
group.by = "group",
scale = FALSE,
cols = c("lightgoldenrod", "darkred")) +
RotatedAxis() +
coord_fixed() +
scale_y_discrete(limits = rev) +
labs(y = NULL,
x = NULL)
# Save as pdf
pdf("output/figures/early_late_dot_plot.pdf",
width = 7,
height = 4)
dot_plot
dev.off()
expr_data <-
GetAssayData(tumor) %>%
as.matrix() %>%
as.data.frame() %>%
rownames_to_column("gene") %>%
filter(gene %in% gene_list) %>%
as_tibble() %>%
pivot_longer(-gene,
names_to = "cell",
values_to = "expression") %>%
mutate(sample = str_remove(cell, "_.*")) %>%
left_join(tumor@meta.data %>%
rownames_to_column("cell") %>%
mutate(sample = str_remove(cell, "_.*")) %>%
select(sample, group) %>%
distinct()) %>%
group_by(group, gene) %>%
summarize(mean_expr = mean(expression),
pct_expr = sum(expression > 0) / n(),
.groups = "drop") %>%
mutate(gene_group = if_else(gene == "MCL1",
"MCL1",
"BCL2 family"),
group = factor(group,
levels = c("Primary tumor",
"Early metastatic tumor",
"Midpoint metastatic tumor",
"Late metastatic tumor")))
bcl_dotplot <- function(data) {
plot_name <-
ggplot(data,
aes(x = gene,
y = group,
size = pct_expr,
color = mean_expr)) +
geom_point() +
theme_robert() +
scale_color_gradient(low = "lightgoldenrod",
high = "darkred",
name = "Mean expr") +
scale_size_continuous(limits = c(0, 1),
name = "% cells expr") +
theme(panel.grid.major = element_blank(),
legend.box = "vertical",
legend.key.width = unit(15, "points")) +
labs(x = "",
y = "") +
scale_y_discrete(limits = rev)
return(plot_name)
}
split_dotplot <-
cowplot::plot_grid(bcl_dotplot(expr_data),
bcl_dotplot(subset(expr_data, gene_group == "MCL1")) +
theme(axis.text.y = element_blank()),
align = "h",
rel_widths = c(1, 0.5))
# Save as pdf
pdf("output/figures/early_late_split_dot_plot.pdf",
width = 6,
height = 4)
split_dotplot
dev.off()
expr_data <-
GetAssayData(tumor) %>%
as.matrix() %>%
as.data.frame() %>%
rownames_to_column("gene") %>%
filter(gene %in% gene_list) %>%
as_tibble() %>%
pivot_longer(-gene,
names_to = "cell",
values_to = "expression") %>%
mutate(sample = str_remove(cell, "_.*")) %>%
left_join(tumor@meta.data %>%
rownames_to_column("cell") %>%
mutate(sample = str_remove(cell, "_.*")) %>%
select(sample, group) %>%
distinct()) %>%
group_by(group, gene) %>%
summarize(mean_expr = mean(expression),
pct_expr = sum(expression > 0) / n(),
.groups = "drop") %>%
mutate(gene_group = if_else(gene == "MCL1",
"MCL1",
"BCL2 family"),
group = factor(group,
levels = c("Primary tumor",
"Early metastatic tumor",
"Midpoint metastatic tumor",
"Late metastatic tumor")))
bcl_dotplot <- function(data) {
plot_name <-
ggplot(data,
aes(x = gene,
y = group,
size = pct_expr,
color = mean_expr)) +
geom_point() +
theme_robert() +
scale_color_gradient(low = "lightgoldenrod",
high = "darkred",
name = "Mean expr") +
scale_size_continuous(limits = c(0, 1),
name = "% cells expr") +
theme(panel.grid.major = element_blank(),
legend.box = "vertical",
legend.key.width = unit(15, "points")) +
labs(x = "",
y = "") +
scale_y_discrete(limits = rev)
return(plot_name)
}
split_dotplot <-
cowplot::plot_grid(bcl_dotplot(expr_data),
bcl_dotplot(subset(expr_data, gene_group == "MCL1")) +
theme(axis.text.y = element_blank()),
align = "h",
rel_widths = c(1, 0.5))
# Save as pdf
pdf("output/figures/early_late_split_dot_plot.pdf",
width = 6,
height = 4)
split_dotplot
dev.off()
dot_plot <-
DotPlot(tumor,
features = gene_list[1:3],
group.by = "group",
scale = FALSE,
cols = c("lightgoldenrod", "darkred")) +
RotatedAxis() +
coord_fixed() +
scale_y_discrete(limits = rev) +
labs(y = NULL,
x = NULL) +
DotPlot(tumor,
features = gene_list[4],
group.by = "group",
scale = FALSE,
cols = c("lightgoldenrod", "darkred")) +
RotatedAxis() +
coord_fixed() +
scale_y_discrete(limits = rev) +
labs(y = NULL,
x = NULL)
# Save as pdf
pdf("output/figures/early_late_dot_plot.pdf",
width = 7,
height = 4)
dot_plot
dev.off()
# Violin plot of expression of genes of interest by group (early/late)
vln_plot <-
VlnPlot(tumor,
features = gene_list,
group.by = "group") +
RotatedAxis()
# Save as pdf
pdf("output/figures/early_late_vln_plot.pdf",
width = 6,
height = 5)
vln_plot
dev.off()
# Feature plot (UMAP) of expression of genes of interest by group (early/late)
feature_plot <-
FeaturePlot(tumor,
features = gene_list,
split.by = "group",
order = TRUE)
# Save as pdf
pdf("output/figures/early_late_feature_plot.pdf",
width = 15,
height = 18)
feature_plot
dev.off()
```
## Human Cells Only: Post-cell cycle regression
Regress out cell cycle genes so that the dataset does not cluster on the UMAP
by cell cycle gene expression.
```{r cc, dependson=c("load","human-processing")}
# Calculate cell cycle phase and view its effects on clustering
# Note: default for kill_cc() is not performing cell cycle regression
# Save as pdf
pdf("output/figures/early_late_tumor_pre-kill_cc.pdf",
width = 5,
height = 5)
kill_cc(tumor)
dev.off()
# Calculate cell cycle phase and remove its effects on clustering
tumor_cc <-
kill_cc(tumor,
cc_regress = "Y") %>%
FindNeighbors(dims = 1:20) %>%
FindClusters(resolution = 0.15) %>%
RunUMAP(dims = 1:20)
# Plot tumor UMAP after cell cycle genes are regressed out
DimPlot(tumor_cc,
group.by = "Phase",
label = TRUE,
pt.size = 1) +
theme_robert()
# Save as pdf
pdf("output/figures/early_late_post-kill_cc.pdf",
width = 5,
height = 5)
DimPlot(tumor_cc,
group.by = "Phase",
label = TRUE,
pt.size = 1) +
theme_robert()
dev.off()
# Plot tumor UMAP by timepoint post cell cycle regression
umap_cc <-
DimPlot(tumor_cc,
pt.size = 1,
label = TRUE,
group.by = "timepoint") +
theme_robert() +
theme(aspect.ratio = 1)
umap_cc
# Save as pdf
pdf("output/figures/early_late_umap_cc_bytrtment.pdf",
width = 5,
height = 5)
umap_cc
dev.off()
# Save the cell cycle regressed tumor object
save(tumor_cc, file = "output/rdata/early_late_tumor_cc.RData")
```
Repeat plots of gene expression post cell cycle regression.
Add S0131 to dotplot
```{r human_geneplots_postcc, dependson=c("load","human-processing","cc")}
# Dot plot of expression of genes of interest by group (early/late)
dot_plot <-
DotPlot(tumor_cc,
features = gene_list,
group.by = "group",
scale = FALSE,
cols = c("lightgoldenrod", "darkred")) +
RotatedAxis() +
coord_fixed()
# Save as pdf
pdf("output/figures/early_late_dot_plot_postccr.pdf",
width = 7,
height = 4)
dot_plot
dev.off()
# Violin plot of expression of genes of interest by group (early/late)
vln_plot <-
VlnPlot(tumor_cc,
features = gene_list,
group.by = "group") +
RotatedAxis()
# Save as pdf
pdf("output/figures/early_late_vln_plot_postccr.pdf",
width = 6,
height = 5)
vln_plot
dev.off()
# Feature plot (UMAP) of expression of genes of interest by group (early/late)
feature_plot <-
FeaturePlot(tumor_cc,
features = gene_list,
split.by = "group",
order = TRUE) +
theme(aspect.ratio = 1)
# Save as pdf
pdf("output/figures/early_late_feature_plot_postccr.pdf",
width = 6,
height = 12)
feature_plot
dev.off()
```
## Growth factor expression in normal vs lungs with mets
Also plot each time point separately as a time-course
```{r deGrowthFactorMets, dependson='load'}
sobj_list <- qs::qread("output/rdata/sobj_list.qs")
sobj_list$S0006_mouse$group_name <- "Mouse control lung"
for (sample_name in c("d14_pos_m", "d28_pos_m", "d49_pos_m")) {
sobj_list[[sample_name]]$group_name <- "Stromal combined timepoints"
}
combined_data <-
merge(x = sobj_list$S0006_mouse,
y = list(sobj_list[["d14_pos_m"]],
sobj_list[["d28_pos_m"]],
sobj_list[["d49_pos_m"]])) %>%
process_seurat(umap_metric = "cosine",
run_umap_dims = 1:10,
umap_n_neighbors = 30,
neighbor_k_param = 20,
resolution = 1)
combined_data$group_name <-
factor(combined_data$group_name,
levels = c("Mouse control lung",
"Stromal combined timepoints"))
combined_data$sample_description <-
factor(combined_data$sample_description,
levels = c("SCID control lung",
"OS17 mets - 2wk",
"OS17 mets - 4wk",
"OS17 mets - 7wk"))
combined_data$sample_name <-
combined_data$sample_name %>%
str_remove("/outs") %>%
factor(., levels = c("S0006-WTLung",
"S0082",
"S0084",
"S0093"))
combined_data$group <-
factor(combined_data$group,
levels = c("Mouse_control_lung",
"Early_mouse_stroma",
"Midpoint_mouse_stroma",
"Late_mouse_stroma"))
```
## Code to try to assess stromal cell type assignment
To assess the stromal cell type assignment, we will use the SingleR package to assign cell types from a couple of different cell references. We then use marker genes taken from several different papers to assess the cell type assignments. We then use a combination of these to assign cell types for each cluster.
```{r stromal_cell_type_assignment, eval=FALSE}
# Load the already-processed mouse lung reference
ref_path <- "/home/gdrobertslab/lab/GenRef/sc_ref_datasets/mouse"
load(paste0(ref_path, "/GSE151974/mouse_lung_ref.RData"))
mouse_lung_ref$label.fine <- mouse_lung_ref$cell_type
# Add it to immune and whole-mouse references
ref <- list(
immune = celldex::ImmGenData(),
whole_mouse = celldex::MouseRNAseqData(),
lung = as.SingleCellExperiment(mouse_lung_ref)
)
# ref_slots <- c("label.fine", "label.main", "cell_type")
# names(ref_slots) <- names(ref)
# Cluster the combined data to a finer resolution
combined_data <- FindClusters(combined_data, resolution = 1)
# Make cell assignments by cluster for each of the references separately
cell_assign <- lapply(ref, function(x) {
a <- SingleR::SingleR(as.SingleCellExperiment(combined_data),
ref = x,
labels = x$label.fine,
clusters = combined_data$seurat_clusters)
a <- tibble(cluster = rownames(a), type = a$labels)
return(a)
})
# Combine the assignments into a table
cell_assign <- left_join(cell_assign[[1]], cell_assign[[2]], by = "cluster") %>%
left_join(., cell_assign[[3]], by = "cluster") %>%
as_tibble()
colnames(cell_assign) <- c("cluster", "immune", "whole_mouse", "lung")
cell_assign
# Add the tables that Yogesh assembled from PMID 35690521
mac_subsets <-
readxl::read_excel("misc/Finalmacrophage_and_monocyte_subsets.xlsx") %>%
filter(Species == "Mouse" & Assay == "RNA")
mac_subsets$Major[mac_subsets$Major == 0] <- "Minor"
mac_subsets$Major[mac_subsets$Major == 1] <- "Major"
mac_subsets$Annotation <- paste0(mac_subsets$Annotation, "_", mac_subsets$Major)
mac_subsets <- select(mac_subsets, Annotation, ID)
mac_subset_list <- lapply(unique(mac_subsets$Annotation), function(s) {
return(mac_subsets$ID[mac_subsets$Annotation == s])
})
names(mac_subset_list) <- unique(mac_subsets$Annotation)
# Examine the macrophage clusters more closely
# Look at markers published in PMID 35732118, 35812401, 32302573, 35690521
murine_mac_marks <- c(mac_subset_list, list(
inflam_mono = c("Ms4a4c", "Irf7", "Plac8", "Ly6c2"),
Cd36_Spp1_mac = c("Clec4d", "Hmox1", "Cd36", "Spp1", "Arg1"),
C1qa_mac = c("C1qa", "Fcgr3", "Ccr2", "Tgfbi"),
MhcHigh = c("Cd74", "H2-Aa", "Plbd1", "Ciita", "Il1r2", "Ifitm1", "Napsa",
"H2-Oa", "H2-DMb2", "Cd209a", "Plet1"),
Batf3_Dc = c("Flt3", "Batf3", "Zbtb46", "Ccr7", "Fscn1", "Ccl22", "Nfkb2"),
Cd14Mono = c("S100a8", "S100a9", "S100a12", "Vcan", "Cd36", "Cd14"),
Cd16Mono = c("Tcf7l2", "Mtss1", "Serpina1", "Rhoc", "Ms4a7", "Lilra1",
"Ifitm2", "Fcgr3a", "Siglec10", "Cx3cr1", "Lilrb1"),
Nlrp3Mac = c("Nlrp3", "Ereg", "Itgax", "Il1b", "Vegfa", "Ccl3"),
C1qcTAM = c("Apoe", "C1qa", "C1qb", "C1qc", "Cd81", "Trem2", "Acp5",
"Apoc1", "Prdm1", "Csf1r", "Slamf8"),
Spp1TAM = c("Cstb", "Spp1", "Fn1", "Abl2", "Pparg", "Sdc4", "Rgcc",
"Adm", "Marco"),
Mast = c("Ms4a2", "Cpa3", "Ccl3", "Ccl6", "Tpsb2", "Tpsd1", "Hdc",
"Slc18a2", "Gata2", "Kit", "Il1r1", "Hpgds", "Vwa5a"),
pDC = c("Gzmb", "Tcf4", "Jchain", "Mzb1", "Irf7", "Il3ra", "Slc15a4"),
Cd1c_cDC2 = c("Axl", "Fcer1a", "Cd1c", "Fcgr2b", "Cd1e", "Cd1a"),
Batf3_cDC1 = c("Cst7", "Dapp1", "Flt3", "C1orf54", "Ccr7", "Ido1"),
C1qaMac = c("C1qa", "Marco", "Apoe", "Cxcl10", "Cd163", "Cd68"),
Thbs1Mac = c("Thbs1", "Fcn1", "Vcan", "S100a12", "Nlrp3", "Il1b"),
MafbMac = c("Klf2", "Klf4", "Nr4a1", "Nr4a2", "Hsph1", "Lyve1", "Vegfa"),
Mki67Mac = c("Stmn1", "Top2a", "Mki67", "Mcm4", "Mcm5", "Mcm7"),
S100a8Mac = c("S100a8", "Ctsa", "Ctsd", "S100a2", "S100a6"),
Spp1Mac = c("Spp1", "Tmsb4x", "Tmsb10", "Ctsb"),
Neutro = c("S011a9", "S100a8", "Csfr", "Clec4d"),
cDC2_Cd209a = c("Cd209a"),
cDC2_Itgax = c("Itgax"),
cDC1_Clec9a = c("Cd207", "Clec9a", "Batf3"),
cDC1_Ccl22 = c("Ccl22", "Ccr7", "Fscn1", "Ccl5"),
Mono_Ly6c2 = c("Cxcl10", "Ly6c1", "Fcgr1", "Ly6c2"),
Mono_Nr4a1 = c("Fn1", "Nr4a1"),
Mono_Itga1 = c("Itga1", "Cd300a", "Clec4a1"),
Macro_Maf = c("Maf", "Cx3cr1", "Ccl2"),
Macro_Mgl2 = c("Mgl2", "Clec4b1"),
Macro_Vegfa = c("Vegfa", "Hilpda", "Hmox1", "Spp1"))
)
pdf("output/figures/mac_subset_marks.pdf",
width = 12,
height = 12)
gridExtra::grid.table(cell_assign)
DimPlot(combined_data,
pt.size = 1,
label = TRUE) +
coord_fixed()
DimPlot(combined_data,
pt.size = 1,
split.by = "timepoint") +
coord_fixed()
for (i in names(murine_mac_marks)) {
p <- r_feature_plot(combined_data,
title = i,
features = murine_mac_marks[[i]],
ncol = 3)
print(p)
}
dev.off()
r_feature_plot(combined_data,
title = "Irf4",
features = "Irf4")
```
### Final cell type assignment
The following was run previously to establish the cell types within the stromal populations based on the preceding analysis:
cell_assignments <-
read_csv("misc/macrophage_cell_type.csv",
show_col_types = FALSE) %>%
pull(label.main, name = cluster)
combined_data <-
RenameIdents(combined_data, cell_assignments)
combined_data$cell_type_final <- Idents(combined_data)
cell_type_assignments <- combined_data$cell_type_final %>%
as.data.frame() %>%
rownames_to_column(var = "cell_id") %>%
as_tibble()
saveRDS(cell_type_assignments, file = "misc/cell_type_assignments")
In the final step, assignments were saved by cell ID to avoid erors related to future changes in clustering.
```{r assignCellTypes}
cell_type_assignments <- readRDS(file = "misc/cell_type_assignments")
cell_type_assignments <- left_join(
tibble(cell_id = names(combined_data$cell_type_final)),
cell_type_assignments)
combined_data$cell_type_final <- cell_type_assignments$assignment
cell_type_markers <- c(
"Sftpc", "Cldn18", "Ager", # Epithelial
"Pdpn", "Upk3b", "Cdh2", # Mesothelial
"Nkg7", "Klrd1", "Gzma", # NK
"Zbtb46", "Il12b", "Serpinb9", # Dendritic
"Gsn", "Mfap4", "Lum", # Fibroblast
"Plac8", "S100a4", "Ly6c2", # Monocyte
"Cstb", "Spp1", "Fn1", # Interstitial Mac
"Ms4a4c", "Irf7", # TIM
"Cd36", "Pecam1", "Cd93", # Endothelial
"Ccl8", "C1qa", "C1qb", "TAM", # TAM
"Tcf7l2", "Axl", "Siglecf" # Alveolar Mac
)
DotPlot(combined_data, features = cell_type_markers,
cols = c("lightgoldenrod", "darkred")) +
theme(axis.text.x = element_text(
angle = 90, vjust = 0))
qs::qsave(combined_data,
file = "output/rdata/combined_data.qs")
```
### Run DE on the mouse samples
```{r deGrowthFactorMetsDE, dependson='deGrowthFactorMets'}
combined_data <- qs::qread("output/rdata/combined_data.qs")
Idents(combined_data) <- combined_data$group_name
overall_de <-
FindMarkers(combined_data,
ident.1 = "Mouse control lung",
logfc.threshold = 0)
qs::qsave(overall_de,
file = "output/rdata/overall_de.qs")
```
### Count cell types
```{r deGrowthFactorMetsCellType, dependson='deGrowthFactorMetsDE'}
cell_type_list <-
table(combined_data$cell_type_final, combined_data$group) %>%
as.data.frame() %>%
group_by(Var1) %>%
summarize(sum = sum(Freq), .groups = "drop") %>%
filter(sum > 10)
```
### Make supplemental figures with all cell types
```{r deGrowthFactorMetsLoop, dependson='deGrowthFactorMetsCellType'}
overall_de <-
qs::qread("output/rdata/overall_de.qs")
target_genes <-
read_tsv("misc/growth_factor_genes.txt",
col_names = "human_gene",
show_col_types = FALSE) %>%
left_join(read_tsv("misc/HMD_HumanPhenotype.rpt",
col_names = c("human_gene",
"h_entrez",
"mouse_gene",
"mgi",
"pheno",
"junk"),
show_col_types = FALSE) %>%
select(human_gene, mouse_gene),
by = "human_gene") %>%
select(mouse_gene)
target_genes <-
left_join(target_genes,
overall_de %>%
rownames_to_column("mouse_gene"))
################################################################################
# DE between lung and primary mets by cell type
combined_subset <-
combined_data %>%
subset(cell_type_final %in% cell_type_list$Var1)
combined_subset <-
combined_subset[rownames(combined_subset) %in% target_genes$mouse_gene, ]
combined_subset$four_groups <-
combined_subset$group %>%
str_replace_all("_", " ") %>%
factor(levels = c("Mouse control lung",
"Early mouse stroma",
"Midpoint mouse stroma",
"Late mouse stroma"))
Idents(combined_subset) <- combined_subset$group_name
qs::qsave(combined_subset,
file = "output/rdata/combined_subset.qs")
table(combined_subset$cell_type_final, combined_subset$group_name) %>%
as.data.frame() %>%
pivot_wider(names_from = Var2,
values_from = Freq)# %>%
# gt::gt() %>%
# gt::gtsave(filename = "output/figures/cell_type_table.pdf")
########### Clean this up
cell_type_de <- tibble(gene = character())
for (cell_type in unique(combined_subset$cell_type_final)) {
cell_type_de <-
subset(combined_subset,
subset = cell_type_final == {{ cell_type }}) %>%
FindMarkers(ident.1 = "Mouse control lung",
logfc.threshold = 0,
min.pct = 0) %>%
as.data.frame() %>%
rownames_to_column("gene") %>%
mutate(cell_type = cell_type) %>%
select(gene, avg_log2FC, p_val_adj, cell_type) %>%
arrange(p_val_adj) %>%
bind_rows(cell_type_de)
}
write_tsv(cell_type_de,
file = "output/de/cell_type_de.txt")
for (factor_name in c("group_name", "four_groups")) {
############################################################################
# Plot DE between lung and primary mets by cell type
temp_data <-
GetAssayData(combined_subset) %>%
as.matrix() %>%
t() %>%
as.data.frame() %>%
select(target_genes$mouse_gene[target_genes$mouse_gene %in%
rownames(combined_subset)]) %>%
rownames_to_column("cell") %>%
mutate(cell_type = combined_subset$cell_type_final,
group_col = combined_subset[[factor_name]] %>% pull()) %>%
pivot_longer(cols = c(-group_col,
-cell_type,
-cell),
names_to = "gene",
values_to = "expression") %>%
group_by(cell_type, group_col, gene) %>%
summarize(expr_mean = mean(expression),
num_cells = n(),
pct_expr = sum(expression > 0) / n(),
.groups = "drop") %>%
mutate(gene_group = paste(gene, group_col, sep = "\n"))
plot_name <-
ggplot(temp_data,
aes(x = group_col,
y = cell_type,
group = group_col,
color = expr_mean,
size = pct_expr)) +
geom_point() +
facet_wrap(~ gene, nrow = 1) +
theme_robert() +
scale_color_gradient(low = "white",
high = "darkred") +
theme(axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
scale_y_discrete(limits = rev) +
labs(x = "",
y = "",
title = paste0("Gene Expression Split by ",
str_replace(factor_name, "_", " ") %>%
str_to_title(),
" and Cell Type"))
ggsave(paste0("output/figures/dotplot_cell_type_by_",
factor_name,
".pdf"),
plot = plot_name,
width = 15,
height = 8)
plot_name <-
temp_data %>%
left_join(cell_type_de) %>%
mutate(fct_order = as.numeric(group_col),
group = paste0(group_col,
" (n=",
num_cells,
")") %>%
factor() %>%
fct_reorder(fct_order),
p_val_adj = replace_na(p_val_adj, 1),
sig = ifelse(p_val_adj <= 0.05,
"Significant",
"Non-significant")) %>%
ggplot(aes(x = group,
y = gene,
fill = pct_expr)) +