Last updated: 2021-04-12
Checks: 6 1
Knit directory: melanoma_publication_old_data/
This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish
to commit the R Markdown file and build the HTML.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20200728)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 3203891. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .Rproj.user/
Ignored: ._.DS_Store
Ignored: analysis/._clinical metadata preparation.Rmd
Ignored: code/.DS_Store
Ignored: code/._.DS_Store
Ignored: data/.DS_Store
Ignored: data/._.DS_Store
Ignored: data/data_for_analysis/
Ignored: data/full_data/
Ignored: output/.DS_Store
Ignored: output/._.DS_Store
Ignored: output/._protein_neutrophil.png
Ignored: output/._rna_neutrophil.png
Ignored: output/PSOCKclusterOut/
Ignored: output/bcell_grouping.png
Ignored: output/dysfunction_correlation.pdf
Unstaged changes:
Modified: .gitignore
Modified: analysis/10_Dysfunction_Score.rmd
Modified: analysis/11_Bcell_Score.Rmd
Modified: analysis/Figure_1.rmd
Modified: analysis/Figure_2.rmd
Modified: analysis/Figure_4.rmd
Modified: analysis/Figure_5.rmd
Modified: analysis/Summary_Statistics.rmd
Modified: analysis/Supp-Figure_1.rmd
Modified: analysis/Supp-Figure_2.rmd
Modified: analysis/Supp-Figure_4.rmd
Modified: analysis/Supp-Figure_5.rmd
Modified: analysis/XX_hazard_ratio.rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were made to the R Markdown (analysis/Figure_2.rmd
) and HTML (docs/Figure_2.html
) files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 3203891 | toobiwankenobi | 2021-02-19 | change celltype names |
html | 3203891 | toobiwankenobi | 2021-02-19 | change celltype names |
Rmd | ee1595d | toobiwankenobi | 2021-02-12 | clean repo and adapt files |
html | ee1595d | toobiwankenobi | 2021-02-12 | clean repo and adapt files |
html | 3f5af3f | toobiwankenobi | 2021-02-09 | add .html files |
Rmd | afa7957 | toobiwankenobi | 2021-02-08 | minor changes on figures and figure order |
Rmd | 20a1458 | toobiwankenobi | 2021-02-04 | adapt figure order |
Rmd | f9bb33a | toobiwankenobi | 2021-02-04 | new Figure 5 and minor changes in figure order |
Rmd | 2ac1833 | toobiwankenobi | 2021-01-08 | changes to Figures |
Rmd | 9442cb9 | toobiwankenobi | 2020-12-22 | add all new files |
Rmd | 1af3353 | toobiwankenobi | 2020-10-16 | add stuff |
Rmd | a6b51cd | toobiwankenobi | 2020-10-14 | clean scripts, add new subfigures |
Rmd | d8819f2 | toobiwankenobi | 2020-10-08 | read new data (nuclei expansion) and adapt scripts |
Rmd | a21c858 | toobiwankenobi | 2020-08-06 | adapt pipeline |
Rmd | 2c11d5c | toobiwankenobi | 2020-08-05 | add new scripts |
This script generates plots for Figure 2.
knitr::opts_chunk$set(echo = TRUE, message= FALSE)
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
sapply(list.files("code/helper_functions", full.names = TRUE), source)
code/helper_functions/calculateSummary.R
value ?
visible FALSE
code/helper_functions/censor_dat.R
value ?
visible FALSE
code/helper_functions/detect_mRNA_expression.R
value ?
visible FALSE
code/helper_functions/DistanceToClusterCenter.R
value ?
visible FALSE
code/helper_functions/findMilieu.R code/helper_functions/findPatch.R
value ? ?
visible FALSE FALSE
code/helper_functions/getInfoFromString.R
value ?
visible FALSE
code/helper_functions/getSpotnumber.R
value ?
visible FALSE
code/helper_functions/plotCellCounts.R
value ?
visible FALSE
code/helper_functions/plotCellFractions.R
value ?
visible FALSE
code/helper_functions/plotDist.R
value ?
visible FALSE
code/helper_functions/scatter_function.R
value ?
visible FALSE
code/helper_functions/sceChecks.R
value ?
visible FALSE
code/helper_functions/validityChecks.R
value ?
visible FALSE
library(SingleCellExperiment)
library(ComplexHeatmap)
library(data.table)
library(dplyr)
library(janitor)
library(tidyr)
library(ggpmisc)
library(cowplot)
library(corrplot)
library(gridExtra)
library(ggalluvial)
library(ggbeeswarm)
library(ggpubr)
library(RColorBrewer)
library(colorRamps)
library(circlize)
library(forcats)
library(ggpmisc)
sce_rna <- readRDS(file = "data/data_for_analysis/sce_RNA.rds")
sce_prot <- readRDS(file= "data/data_for_analysis/sce_protein.rds")
cur_dt <- as.data.table(colData(sce_rna))
# combinations with more than 600 occurrences
targets <- metadata(sce_rna)$chemokines_morethan600_withcontrol
targets_no_control <- replace(targets, match(c("CXCL8", "CCL18"),targets), c("CCL18", "CXCL8"))
# remove control samples
cur_dt <- cur_dt[MM_location_simplified != "control",]
# extract chemokine columns
chemokines <- cbind(cur_dt[,c("ImageNumber", "MM_location_simplified", "celltype")], cur_dt[,grepl(glob2rx("C*L*"),names(cur_dt)), with = F])
# create combination matrix
m <- make_comb_mat(chemokines, top_n_sets = 11)
# filter based on combination size and combination degree
m <- m[comb_size(m) >= 600 & comb_degree(m) > 0]
# sort according to abundance
m <- m[order(-comb_size(m))]
# extract comb names
comb_names <- comb_name(m)
# count celltypes for each combination
celltypes <- data.table()
location <- data.table()
# summarize statistics for each combination (celltype fractions, location)
for (i in comb_names){
# subset
set <- chemokines[extract_comb(m, i)]
# chemokines celltypes
set1 <- set %>%
group_by(celltype) %>%
summarise(n=n()) %>%
reshape2::dcast(.,i ~ celltype, value.var = "n")
# chemokines by location
set2 <- set %>%
group_by(ImageNumber, MM_location_simplified) %>%
summarise(n=n())
# add images with no combinations to not distort median
set2_add <- distinct(cur_dt[,c("ImageNumber", "MM_location_simplified")], ImageNumber, .keep_all = T)
set2_add$n <- 0
# subset to only contain images which are not already part of set2
set2_add <- set2_add[!(ImageNumber %in% set2$ImageNumber),]
set2 <- set2 %>%
rbind(., set2_add) %>%
group_by(MM_location_simplified) %>%
mutate(median = median(n)) %>%
distinct(MM_location_simplified, median) %>%
reshape2::dcast(.,i ~ MM_location_simplified, value.var = "median")
# add to data.frame
celltypes <- rbind(celltypes, set1, fill = TRUE)
location <- rbind(location, set2, fill = TRUE)
}
# replace NA
celltypes[is.na(celltypes)] <- 0
location[is.na(location)] <- 0
# properties of combination matrix
ss = set_size(m)
cs = comb_size(m)
# create plot
ht = UpSet(m,
set_order = order(ss),
comb_order = order(cs, decreasing = T),
top_annotation = HeatmapAnnotation(
"Number of\nExpressing Cells" = anno_barplot(celltypes[,-1],
ylim = c(0, max(cs)*1.1),
border = FALSE,
gp = gpar(fill = metadata(sce_rna)$colour_vectors$celltype[colnames(celltypes[,-1])]),
axis_param = list(gp = gpar(fontsize=20)),
height = unit(12, "cm")),
annotation_name_side = "left",
annotation_name_rot = 0,
annotation_name_gp = gpar(fontsize=20)),
left_annotation = rowAnnotation(
"Total Number of\nExpressing Cells" = anno_barplot(-ss,
baseline = 0,
axis_param = list(
at = c(0, -5000, -10000, -15000),
labels = c(0, 5000, 10000, 15000),
labels_rot = 0,
gp = gpar(fontsize = 15)),
border = FALSE,
gp = gpar(fill = "black"),
width = unit(5, "cm"),
),
set_name = anno_text(set_name(m),
location = 0.5,
gp = gpar(fontsize=15),
just = "center",
width = max_text_width(set_name(m)) + unit(4, "mm")),
annotation_name_gp = gpar(fontsize=20)),
right_annotation = NULL,
show_row_names = FALSE,
pt_size = unit(5, "mm"),
lwd = 2,
width = unit(14, "cm"),
height = unit(14, "cm")
)
# draw heatmap
ht = draw(ht)
# add absolute numbers on top of barplot
od = column_order(ht)
row_od = row_order(ht)
decorate_annotation("Number of\nExpressing Cells", {
grid.text(cs[od], x = seq_along(cs), y = unit(cs[od], "native") + unit(2, "pt"),
default.units = "native", just = c("left", "bottom"),
gp = gpar(fontsize = 15, col = "#404040"), rot = 45)
})
decorate_annotation("Total Number of\nExpressing Cells", {
grid.text(ss[row_od],
x = unit(-ss[row_od], "native") + unit(-0.75, "cm"),
y = rev(seq_len(length(-ss))),
default.units = "native", rot = 0,
gp = gpar(fontsize = 13))
})
# legend for celltypes
lgd1 = Legend(labels = colnames(celltypes[,-1]),
title = "Celltypes",
legend_gp = gpar(fill = metadata(sce_rna)$colour_vectors$celltype[colnames(celltypes[,-1])],
fontsize = 18),
nrow = 5)
draw(packLegend(lgd1,column_gap = unit(0.5, "cm"),
max_height = unit(7, "cm")))
# add marker expression to cells
marker_expression <- data.frame(t(assay(sce_rna[rowData(sce_rna)$good_marker,], "asinh")))
marker_expression$cellID <- rownames(marker_expression)
# chemokine info
chemo <- data.frame(colData(sce_rna))[,c("cellID", "expressor", "celltype")]
dat <- left_join(chemo, marker_expression, by = "cellID")
dat$cellID <- NULL
# aggregate data
dat_aggr <- dat %>%
filter(expressor %in% colnames(colData(sce_rna))[grepl("CXCL|CCL", colnames(colData(sce_rna)))]) %>%
group_by(celltype, expressor) %>%
summarise_all(funs(mean))
Warning: `funs()` is deprecated as of dplyr 0.8.0.
Please use a list of either functions or lambdas:
# Simple named list:
list(mean = mean, median = median)
# Auto named with `tibble::lst()`:
tibble::lst(mean, median)
# Using lambdas
list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
# prepare matrix for heatmap
dat_aggr <- dat_aggr %>%
arrange(celltype, expressor)
stats <- dat %>%
filter(expressor %in% colnames(colData(sce_rna))[grepl("CXCL|CCL", colnames(colData(sce_rna)))]) %>%
group_by(celltype, expressor) %>%
summarise(n=n()) %>%
filter(n>1000) %>%
arrange(celltype, expressor)
dat_aggr <- dat_aggr %>%
filter(paste0(celltype,expressor) %in% paste0(stats$celltype,stats$expressor))
# factorize expressor for column sorting in heatmap
dat_aggr$expressor <- factor(dat_aggr$expressor, levels = c("CCL4", "CCL18", "CCL22", "CXCL8",
"CCL8", "CXCL9", "CXCL10", "CXCL13", "CCL2", "CXCL12", "CCL19"))
stats$expressor <- factor(stats$expressor, levels = c("CCL4", "CCL18", "CCL22", "CXCL8",
"CCL8", "CXCL9", "CXCL10", "CXCL13", "CCL2", "CXCL12", "CCL19"))
dat_aggr <- dat_aggr %>%
arrange(celltype, expressor)
stats <- stats %>%
arrange(celltype, expressor)
# create and scale scale matrix
m <- as.matrix(t(dat_aggr[,-c(1:2)]))
m <- t(scale(t(m)))
colnames(m) <- dat_aggr$celltype
# create top annotations
ha <- HeatmapAnnotation("Chemokine" = dat_aggr$expressor,
"Cells" = anno_barplot(stats[,3],
height = unit(1.5,"cm"),
axis_param = list(gp = gpar(fontsize=14))),
"Cell Numbers" = anno_text(t(stats[,3]),
which = "column",
rot = 90,
just = "center",
location = 0.5,
gp = gpar(fontsize=10)),
col = list("Chemokine" = metadata(sce_rna)$colour_vectors$chemokine_single),
show_legend = FALSE,
annotation_name_gp = gpar(fontsize = 16))
# row_split for markers
rowData(sce_rna)$heatmap_relevance <- ""
rowData(sce_rna[rowData(sce_rna)$good_marker,])$heatmap_relevance <- "Lineage"
rowData(sce_rna[grepl("CXCL|CCL|DapB", rownames(sce_rna)),])$heatmap_relevance <- "Chemokine"
rowData(sce_rna[grepl("B2M|GLUT1|CD134|Lag3|CD163|cleavedPARP|pRB", rownames(sce_rna)),])$heatmap_relevance <- "Other"
# create heatmap
h <- Heatmap(m, name = "Scaled\nExpression",
row_split = rowData(sce_rna[rowData(sce_rna)$good_marker,])$heatmap_relevance,
cluster_columns = FALSE,
show_column_names = FALSE,
top_annotation = ha,
show_heatmap_legend = FALSE,
column_split = colnames(m),
column_title_rot = 90,
cluster_column_slices = TRUE,
row_names_gp = gpar(fontsize = 16),
column_title_gp = gpar(fontsize = 23),
row_title_gp = gpar(fontsize = 23),
col = colorRamp2(c(-3, 0, 3), c("blue", "white", "red")),
height = unit(20, "cm"),
width = unit(25,"cm"))
# draw heatmap
draw(h)
lgd1 = color_mapping_legend(h@matrix_color_mapping, plot = FALSE, legend_direction = "horizontal", legend_width=unit(3,"cm"), at = c(-3:3))
lgd2 = color_mapping_legend(ha@anno_list$Chemokine@color_mapping, plot = FALSE, legend_direction = "horizontal", nrow = 4)
lgd_list = packLegend(lgd1,lgd2,direction = "horizontal", gap = unit(1,"cm"))
draw(lgd_list)
# top abundant chemokines
cur_rna <- data.frame(colData(sce_rna))
# sum
rna_sum <- cur_rna %>%
group_by(Description, expressor) %>%
summarise(n = n()) %>%
reshape2::dcast(Description ~ expressor, value.var = "n", fill = 0)
# only keep highly abundant chemokines
rna_sum <- rna_sum[,colnames(rna_sum) %in% targets]
# correlation
cor <- cor(rna_sum, rna_sum, method = "pearson")
corrplot(cor,
order = "FPC",
addCoef.col = "white",
method = "circle",
tl.col="black",
tl.cex = 1.5)
# top abundant chemokines
cur_rna <- data.frame(colData(sce_rna))
# protein data
cur_prot <- data.frame(colData(sce_prot))
# sum
rna_sum <- cur_rna %>%
group_by(Description) %>%
mutate(total_cells=n()) %>%
ungroup() %>%
group_by(Description, total_cells, expressor) %>%
summarise(n=n()) %>%
mutate(fraction=n/total_cells) %>%
reshape2::dcast(Description ~ expressor, value.var = "fraction", fill = 0)
# only keep highly abundant chemokines
rna_sum <- rna_sum[,c("Description", targets)]
prot_sum <- cur_prot %>%
group_by(Description, celltype) %>%
summarise(n = n()) %>%
group_by(Description) %>%
mutate(fraction = n/sum(n)) %>%
reshape2::dcast(Description ~ celltype, value.var = "fraction", fill = 0)
# equal images
all(rna_sum$Description == prot_sum$Description)
[1] TRUE
# correlation
cor <- cor(rna_sum[,-1], prot_sum[,-1], method = "pearson")
corrplot(cor,
addCoef.col = "white",
method = "circle",
tl.col="black",
tl.cex = 1.5)
# sum
chemokines <- data.frame(colData(sce_rna)) %>%
group_by(Description) %>%
mutate(total_cells=n()) %>%
ungroup() %>%
group_by(Description, total_cells, expressor) %>%
summarise(n=n()) %>%
mutate(fraction=n/total_cells) %>%
reshape2::dcast(Description ~ expressor, value.var = "fraction", fill = 0) %>%
select(Description, CXCL12, CXCL9, CCL22)
celltypes <- data.frame(colData(sce_prot)) %>%
group_by(Description, celltype) %>%
summarise(n=n()) %>%
mutate(fraction=n/sum(n)) %>%
reshape2::dcast(Description ~ celltype, value.var = "fraction", fill = 0) %>%
select(Description, `CD8+ T cell`, pDC, `FOXP3+ T cell`) %>%
reshape2::melt(id.vars = "Description", variable.name = "celltype", value.name = "cell_fraction")
sum <- left_join(celltypes, chemokines, by = "Description") %>%
reshape2::melt(id.vars = c("Description", "celltype", "cell_fraction"), variable.name = "expressor", value.name = "chemo_fraction")
sum <- sum %>%
mutate(celltype_chemo = paste(celltype, expressor, sep = " ~ ")) %>%
filter(celltype_chemo %in% c("`CD8+ T cell` ~ CXCL9", "pDC ~ CXCL12", "`FOXP3+ T cell` ~ CCL22"))
sum$celltype_chemo <- factor(sum$celltype_chemo, levels = c("pDC ~ CXCL12", "`FOXP3+ T cell` ~ CCL22","`CD8+ T cell` ~ CXCL9"))
ggplot(sum, aes(x=log10(chemo_fraction), y=log10(cell_fraction))) +
geom_point() +
geom_smooth(method="lm") +
ylab("Celltype Fraction (log10)\n") +
xlab("Chemokine Fraction (log10)") +
scale_y_continuous(position = "right") +
theme_bw() +
theme(text=element_text(size=16),
plot.margin = margin(0.5,0.5,0.5,0.5, "cm"),
axis.title.y = element_text(hjust=1)) +
guides(col=guide_legend(title="Chemokines")) +
stat_poly_eq(formula = y ~ x, size=7,
aes(label = ..rr.label..),
parse = TRUE) +
facet_wrap(~celltype_chemo, ncol = 2, scales = "free", as.table = FALSE)
Warning: Removed 48 rows containing non-finite values (stat_smooth).
Warning: Removed 48 rows containing non-finite values (stat_poly_eq).
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS
Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid parallel stats4 stats graphics grDevices utils
[8] datasets methods base
other attached packages:
[1] forcats_0.5.0 circlize_0.4.12
[3] colorRamps_2.3 RColorBrewer_1.1-2
[5] ggpubr_0.4.0 ggbeeswarm_0.6.0
[7] ggalluvial_0.12.3 gridExtra_2.3
[9] corrplot_0.84 cowplot_1.1.1
[11] ggpmisc_0.3.7 ggplot2_3.3.3
[13] tidyr_1.1.2 janitor_2.1.0
[15] dplyr_1.0.2 data.table_1.13.6
[17] ComplexHeatmap_2.4.3 SingleCellExperiment_1.12.0
[19] SummarizedExperiment_1.20.0 Biobase_2.50.0
[21] GenomicRanges_1.42.0 GenomeInfoDb_1.26.2
[23] IRanges_2.24.1 S4Vectors_0.28.1
[25] BiocGenerics_0.36.0 MatrixGenerics_1.2.0
[27] matrixStats_0.57.0 workflowr_1.6.2
loaded via a namespace (and not attached):
[1] colorspace_2.0-0 ggsignif_0.6.0 rjson_0.2.20
[4] ellipsis_0.3.1 rio_0.5.16 rprojroot_2.0.2
[7] snakecase_0.11.0 XVector_0.30.0 GlobalOptions_0.1.2
[10] fs_1.5.0 clue_0.3-58 rstudioapi_0.13
[13] farver_2.0.3 lubridate_1.7.9.2 splines_4.0.3
[16] knitr_1.30 polynom_1.4-0 broom_0.7.3
[19] cluster_2.1.0 png_0.1-7 compiler_4.0.3
[22] backports_1.2.1 Matrix_1.3-2 later_1.1.0.1
[25] htmltools_0.5.0 tools_4.0.3 gtable_0.3.0
[28] glue_1.4.2 GenomeInfoDbData_1.2.4 reshape2_1.4.4
[31] Rcpp_1.0.5 carData_3.0-4 cellranger_1.1.0
[34] vctrs_0.3.6 nlme_3.1-151 xfun_0.20
[37] stringr_1.4.0 openxlsx_4.2.3 lifecycle_0.2.0
[40] rstatix_0.6.0 zlibbioc_1.36.0 scales_1.1.1
[43] hms_0.5.3 promises_1.1.1 yaml_2.2.1
[46] curl_4.3 stringi_1.5.3 zip_2.1.1
[49] shape_1.4.5 rlang_0.4.10 pkgconfig_2.0.3
[52] bitops_1.0-6 evaluate_0.14 lattice_0.20-41
[55] purrr_0.3.4 labeling_0.4.2 tidyselect_1.1.0
[58] plyr_1.8.6 magrittr_2.0.1 R6_2.5.0
[61] generics_0.1.0 DelayedArray_0.16.0 pillar_1.4.7
[64] haven_2.3.1 whisker_0.4 foreign_0.8-81
[67] withr_2.3.0 mgcv_1.8-33 abind_1.4-5
[70] RCurl_1.98-1.2 tibble_3.0.4 crayon_1.3.4
[73] car_3.0-10 rmarkdown_2.6 GetoptLong_1.0.5
[76] readxl_1.3.1 git2r_0.28.0 digest_0.6.27
[79] httpuv_1.5.4 munsell_0.5.0 beeswarm_0.2.3
[82] vipor_0.4.5