Last updated: 2021-02-10

Checks: 7 0

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.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

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 2e443a5. 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:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    ._.DS_Store
    Ignored:    code/.DS_Store
    Ignored:    code/._.DS_Store
    Ignored:    code/helper_functions/._findCommunity.R
    Ignored:    data/.DS_Store
    Ignored:    data/._.DS_Store
    Ignored:    data/._layer_1_classification_rna.csv
    Ignored:    data/._survdat_for_modelling.csv
    Ignored:    data/12plex_validation/
    Ignored:    data/200323_TMA_256_Hot Cold_Clinical Data_Updated Response Data_For Collaborators_latest updated_Mar_2020_for_Coxph_modeling.csv
    Ignored:    data/layer_1_classification_protein.csv
    Ignored:    data/layer_1_classification_rna.csv
    Ignored:    data/manual_infiltration/
    Ignored:    data/protein/
    Ignored:    data/rna/
    Ignored:    data/safety_copy_SCE/
    Ignored:    data/sce_RNA.rds
    Ignored:    data/sce_protein.rds
    Ignored:    data/survdat_for_modelling.csv
    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/04_1_Protein_celltype_classification.rmd
    Modified:   analysis/04_1_RNA_celltype_classification.rmd
    Modified:   analysis/Supp-Figure_2.rmd
    Modified:   analysis/Supp-Figure_4.rmd
    Deleted:    code/findPackages.R

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/Supp-Figure_3.rmd) and HTML (docs/Supp-Figure_3.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
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 73caa28 toobiwankenobi 2021-01-12 minor corrections
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

Introduction

This script generates plots for Supplementary Figure 3.

Preparations

Load libraries

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/findClusters.R
value   ?                                   
visible FALSE                               
        code/helper_functions/findCommunity.R
value   ?                                    
visible FALSE                                
        code/helper_functions/getCellCount.R
value   ?                                   
visible FALSE                               
        code/helper_functions/getInfoFromString.R
value   ?                                        
visible FALSE                                    
        code/helper_functions/getSpotnumber.R
value   ?                                    
visible FALSE                                
        code/helper_functions/plotBarFracCluster.R
value   ?                                         
visible FALSE                                     
        code/helper_functions/plotCellCounts.R
value   ?                                     
visible FALSE                                 
        code/helper_functions/plotCellFrac.R
value   ?                                   
visible FALSE                               
        code/helper_functions/plotCellFracGroups.R
value   ?                                         
visible FALSE                                     
        code/helper_functions/plotCellFracGroupsSubset.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(dplyr)
library(ggplot2)
library(ggbeeswarm)
library(tidyr)
library(scater)
library(dittoSeq)
library(gridExtra)
library(cowplot)
library(data.table)
library(ggpmisc)
library(ggpubr)
library(rstatix)

Load Data

# SCE object
sce_rna = readRDS(file = "data/sce_RNA.rds")
sce_prot = readRDS(file = "data/sce_protein.rds")

# meta data
dat_relation = fread(file = "data/protein/Object relationships.csv",stringsAsFactors = FALSE)
dat_relation_rna = fread(file = "data/RNA/Object relationships.csv",stringsAsFactors = FALSE)

targets <- metadata(sce_rna)$chemokines_morethan600_withcontrol

Supp Figure 3A

Expressor in Tcell groups

targets <- metadata(sce_rna)$chemokines_morethan600_withcontrol

frac <- data.frame(colData(sce_rna)) %>%
  filter(Location != "CTRL") %>%
  group_by(Description, Tcell_density_score_image, expressor) %>%
  summarise(n=n()) %>%
  mutate(fraction = n / sum(n)) %>%
  filter(expressor %in% targets) %>%
  reshape2::dcast(Description + Tcell_density_score_image ~ expressor, value.var = "fraction", fill = 0) %>%
  reshape2::melt(id.vars = c("Description", "Tcell_density_score_image"), variable.name = "expressor", value.name = "fraction")

ggplot(frac, aes(x=expressor, y = fraction, fill = Tcell_density_score_image)) + 
  geom_boxplot(alpha=.75, outlier.size = 0.5) +
  #geom_jitter(size = 1, alpha=0.7, position = position_jitterdodge(dodge.width = 0.75,jitter.width = 0.05), aes(col=Tcell_density_score_image)) +
  scale_color_discrete(guide = FALSE) +
  theme_bw() +
  theme(text = element_text(size = 15),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) + 
  guides(fill=guide_legend(title="Tcell Score")) +
  xlab("") + 
  ylab("Fractions")  +
  coord_cartesian(ylim = c(0,0.06))

Supp Figure 3B

Tumor Marker Profile for different Tcell Scoring Groups

tumor_marker_protein <- c("bCatenin", "Sox9", "pERK", "p75", "Ki67", "SOX10", "PARP", "S100", "MiTF")
tumor_marker_rna <- c("Mart1", "pRB")

# rna data 
dat_rna <- data.frame(t(assay(sce_rna[tumor_marker_rna, sce_rna$celltype == "Tumor"], "asinh")))
dat_rna$cellID <- rownames(dat_rna)
dat_rna <- left_join(dat_rna, data.frame(colData(sce_rna))[,c("cellID", "Tcell_density_score_image", "Description", "MM_location", "Location")])

# filter
dat_rna <- dat_rna %>%
  filter(Location != "CTRL")

# mean per image
dat_rna <- dat_rna %>%
  select(-cellID) %>%
  group_by(Description, Tcell_density_score_image) %>%
  summarise_if(is.numeric, mean, na.rm = TRUE)

# melt
dat_rna <- dat_rna %>%
  reshape2::melt(id.vars = c("Description", "Tcell_density_score_image"), variable.name = "channel", value.name = "asinh")

# protein data
dat_prot <- data.frame(t(assay(sce_prot[tumor_marker_protein,, sce_prot$celltype == "Tumor"], "asinh")))
dat_prot$cellID <- rownames(dat_prot)
dat_prot <- left_join(dat_prot, data.frame(colData(sce_prot))[,c("cellID", "Tcell_density_score_image", "Description", "MM_location", "Location")])

# filter
dat_prot <- dat_prot %>%
  filter(Location != "CTRL")

# mean per image
dat_prot <- dat_prot %>%
  select(-cellID) %>%
  group_by(Description, Tcell_density_score_image) %>%
  summarise_if(is.numeric, mean, na.rm = TRUE)

# melt
dat_prot <- dat_prot %>%
  reshape2::melt(id.vars = c("Description", "Tcell_density_score_image"), variable.name = "channel", value.name = "asinh")

# join both data sets
comb <- rbind(dat_prot, dat_rna)

# adjusted wilcox.test for groups
group_comparison <- list(c("absent", "high"), c("med", "high"))

stat.test <- comb %>%
  group_by(channel) %>%
  wilcox_test(data = ., asinh ~ Tcell_density_score_image) %>%
  adjust_pvalue(method = "BH") %>%
  add_significance("p.adj") %>%
  add_xy_position(x = "Tcell_density_score_image", dodge = 0.8, comparisons = group_comparison) %>%
  filter(is.na(y.position) == FALSE)

# plot 
p <- ggplot(comb, aes(x=Tcell_density_score_image, y=asinh)) + 
  geom_boxplot(alpha=0.2, lwd=1, aes(fill=Tcell_density_score_image)) +
  geom_quasirandom(alpha=0.6, size=2, aes(col=Tcell_density_score_image)) +
  scale_color_discrete(guide = FALSE) +
  theme_bw() +
  theme(text = element_text(size=18),
        axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank()) +
  facet_wrap(~channel, scales = "free") + 
  stat_pvalue_manual(stat.test, label = "p.adj.signif", size = 7) + 
  xlab("") + 
  ylab("Mean Count per Image (asinh)") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) +
  guides(fill=guide_legend(title="Tcell Score", override.aes = c(lwd=0.5, alpha=1)))

leg <- get_legend(p)

grid.arrange(p + theme(legend.position = "none"))
grid.arrange(leg)

Supp Figure 3C

Prepare Relation Data RNA

# prepare data and add cellID
dat_relation_rna$cellID_first <- paste("RNA", paste(dat_relation_rna$`First Image Number`, dat_relation_rna$`First Object Number`, sep = "_"), sep = "_")
dat_relation_rna$cellID_second <- paste("RNA", paste(dat_relation_rna$`Second Image Number`, dat_relation_rna$`Second Object Number`, sep = "_"), sep = "_")

# add celltype status to first and second label
celltype_first <- data.frame(colData(sce_rna))[,c("cellID", "celltype", "celltype_clustered")]
colnames(celltype_first) <- c("cellID_first", "celltype_first", "celltype_clust_first")
celltype_second <- data.frame(colData(sce_rna))[,c("cellID", "celltype", "celltype_clustered")]
colnames(celltype_second) <- c("cellID_second", "celltype_second", "celltype_clust_second")

dat_relation_rna <- left_join(dat_relation_rna, celltype_first, by = "cellID_first")
dat_relation_rna <- left_join(dat_relation_rna, celltype_second, by = "cellID_second")

colnames(dat_relation_rna)[5] <- "FirstImageNumber"

CD8/Tumor Interactions and Chemokine Freqs

# number of interactions tcytotoxic and tumor per image
dat_relation_sub <- dat_relation_rna[dat_relation_rna$celltype_first %in% c("Tcytotoxic") & 
                                   dat_relation_rna$celltype_second == "Tumor"]

# count number of interactions
count <- dat_relation_sub %>%
  group_by(FirstImageNumber) %>%
  summarise(n=n())
names(count)[1] <- "ImageNumber"

# fraction of exhausted cd8 per image
expressor_frac <- data.frame(colData(sce_rna)) %>%
  group_by(ImageNumber, expressor) %>%
  summarise(n=n()) %>%
  mutate(fraction=n/sum(n)) %>%
  filter(expressor %in% targets) %>%
  reshape2::dcast(ImageNumber ~ expressor, value.var = "fraction", fill = 0) %>%
  reshape2::melt(id.vars = c("ImageNumber"), variable.name = "expressor", value.name = "fraction")

# correlation plot
cur_dat <- left_join(expressor_frac, count)
cur_dat[is.na(cur_dat$n),]$n <- 0

ggplot(cur_dat, aes(x=log10(fraction), y=log10(n))) + 
  geom_point(size=2) + 
  geom_smooth(method="lm") +
  stat_poly_eq(formula = y ~ x, 
                aes(label =  ..rr.label..), 
                parse = TRUE, size=5, label.y = 0.01, label.x = 1) +
  theme_bw() +
  theme(text = element_text(size=15)) +
  xlab("Chemokine Fraction (log10)") +
  ylab("Number of CD8/Tumor Interactions (log10)") +
  facet_wrap(~expressor)
Warning: Removed 684 rows containing non-finite values (stat_smooth).
Warning: Removed 684 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] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] rstatix_0.6.0               ggpubr_0.4.0               
 [3] ggpmisc_0.3.7               data.table_1.13.6          
 [5] cowplot_1.1.1               gridExtra_2.3              
 [7] dittoSeq_1.0.2              scater_1.16.2              
 [9] tidyr_1.1.2                 ggbeeswarm_0.6.0           
[11] ggplot2_3.3.3               dplyr_1.0.2                
[13] SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0
[15] Biobase_2.50.0              GenomicRanges_1.42.0       
[17] GenomeInfoDb_1.26.2         IRanges_2.24.1             
[19] S4Vectors_0.28.1            BiocGenerics_0.36.0        
[21] MatrixGenerics_1.2.0        matrixStats_0.57.0         
[23] workflowr_1.6.2            

loaded via a namespace (and not attached):
 [1] colorspace_2.0-0          ggsignif_0.6.0           
 [3] ellipsis_0.3.1            rio_0.5.16               
 [5] ggridges_0.5.3            rprojroot_2.0.2          
 [7] XVector_0.30.0            BiocNeighbors_1.6.0      
 [9] fs_1.5.0                  rstudioapi_0.13          
[11] farver_2.0.3              ggrepel_0.9.0            
[13] splines_4.0.3             knitr_1.30               
[15] polynom_1.4-0             broom_0.7.3              
[17] pheatmap_1.0.12           compiler_4.0.3           
[19] backports_1.2.1           Matrix_1.3-2             
[21] limma_3.44.3              later_1.1.0.1            
[23] BiocSingular_1.4.0        htmltools_0.5.0          
[25] tools_4.0.3               rsvd_1.0.3               
[27] gtable_0.3.0              glue_1.4.2               
[29] GenomeInfoDbData_1.2.4    reshape2_1.4.4           
[31] Rcpp_1.0.5                carData_3.0-4            
[33] cellranger_1.1.0          vctrs_0.3.6              
[35] nlme_3.1-151              DelayedMatrixStats_1.10.1
[37] xfun_0.20                 stringr_1.4.0            
[39] openxlsx_4.2.3            lifecycle_0.2.0          
[41] irlba_2.3.3               edgeR_3.30.3             
[43] zlibbioc_1.36.0           scales_1.1.1             
[45] hms_0.5.3                 promises_1.1.1           
[47] RColorBrewer_1.1-2        yaml_2.2.1               
[49] curl_4.3                  stringi_1.5.3            
[51] zip_2.1.1                 BiocParallel_1.22.0      
[53] rlang_0.4.10              pkgconfig_2.0.3          
[55] bitops_1.0-6              evaluate_0.14            
[57] lattice_0.20-41           purrr_0.3.4              
[59] labeling_0.4.2            tidyselect_1.1.0         
[61] plyr_1.8.6                magrittr_2.0.1           
[63] R6_2.5.0                  generics_0.1.0           
[65] DelayedArray_0.16.0       pillar_1.4.7             
[67] haven_2.3.1               whisker_0.4              
[69] foreign_0.8-81            withr_2.3.0              
[71] mgcv_1.8-33               abind_1.4-5              
[73] RCurl_1.98-1.2            tibble_3.0.4             
[75] crayon_1.3.4              car_3.0-10               
[77] rmarkdown_2.6             viridis_0.5.1            
[79] locfit_1.5-9.4            grid_4.0.3               
[81] readxl_1.3.1              git2r_0.28.0             
[83] forcats_0.5.0             digest_0.6.27            
[85] httpuv_1.5.4              munsell_0.5.0            
[87] beeswarm_0.2.3            viridisLite_0.3.0        
[89] vipor_0.4.5