Last updated: 2021-04-13

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/11_Bcell_Score.Rmd) and HTML (docs/11_Bcell_Score.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
Rmd 2e443a5 toobiwankenobi 2021-02-09 remove files that are not needed

Introduction

This script detects interaction networks of a given celltype (here: B cells) and defines these networks as clusters. Once a cluster is defined, an algorithm screens the neighbourhood of those clusters to identify cells within/surrounding a cluster. These cells are defined as the community of a cluster.

Load packages and helper functions

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(ggplot2)
library(scater)
library(viridis)
library(igraph)
library(CATALYST)
library(reshape2)
library(cowplot)
library(ggridges)
library(tidyverse)
library(viridis)
library(dplyr)
library(cytomapper)
library(concaveman)
library(data.table)
library(sf)
library(ggbeeswarm)
library(RANN)

Load data

sce_prot = readRDS(file = "data/data_for_analysis/sce_protein.rds")
sce_rna = readRDS(file = "data/data_for_analysis/sce_RNA.rds")

image_prot <- read.csv("data/data_for_analysis/protein/Image.csv")

sce_prot$bcell_patch_score <- NULL
sce_rna$bcell_patch_score <- NULL

Find patches of B cells and their milieus

A B cell cluster is defined by at least 20 adjacent B cells (Bcell and BnTcell, max distance of 15µm between them). A milieu is defined by all cells within a cluster and the proximity (enlarging distance = 15µm)

sce_prot$bcell_patch <- NULL
sce_prot$bcell_milieu <- NULL
sce_prot$bcell_patch_score <- NULL

start = Sys.time()
# quantiles of cell radius
quantile(sqrt(sce_prot[,sce_prot$celltype %in% c("B cell")]$Area/pi))
      0%      25%      50%      75%     100% 
1.128379 2.820948 3.385138 3.908820 9.097284 
# find B cell clusters
sce_prot <- findPatch(sce_prot, sce_prot[,colData(sce_prot)$celltype %in% c("B cell", "BnT cell")]$cellID, 
                    'cellID', 'Center_X', 'Center_Y', 'ImageNumber', 
                    distance = 15, min_clust_size = 20, output_colname = "bcell_patch")
Time difference of 9.650667 mins
[1] "patches successfully added to sce object"
# number of B cell clusters
length(unique(sce_prot$bcell_patch))
[1] 205
# define cells within/surrounding a cluster of B cells
sce_prot <- findMilieu(sce_prot, 
                     'cellID', 'Center_X', 'Center_Y', 'ImageNumber', 'bcell_patch', 
                     distance = 50, output_colname = "bcell_milieu")
Time difference of 1.704651 mins
[1] "milieus successfully added to sce object"
# number of chemokine communities
length(unique(sce_prot$bcell_milieu))
[1] 205
end = Sys.time()
print(end-start)
Time difference of 11.36425 mins

Define grouping for B cell / patch densities

Cell Densities

# Protein
im_size_prot <- (image_prot$Height_cellmask * image_prot$Width_cellmask)/1000000
im_size_prot <- data.frame(im_size_prot)
im_size_prot$Description <- image_prot$Metadata_Description

B cell / patch grouping

# patches per image
data.frame(colData(sce_prot)) %>%
  group_by(Description) %>%
  filter(bcell_patch != 0) %>%
  distinct(bcell_patch, .keep_all = T) %>%
  summarise(n=n()) %>%
  arrange(-n)
# A tibble: 43 x 2
   Description     n
   <chr>       <int>
 1 L8             17
 2 C9             15
 3 L7             11
 4 O10            11
 5 J9             10
 6 A5              8
 7 B10             8
 8 E3              8
 9 F9              8
10 G1              8
# … with 33 more rows
# max patch size per image
max_patch <- data.frame(colData(sce_prot)) %>%
  filter(bcell_patch != 0) %>%
  group_by(Description, bcell_patch) %>%
  summarise(n=n()) %>%
  summarise(max_patch_size = max(n)) %>%
  arrange(-max_patch_size)

ggplot(max_patch, aes(x=Description, y=max_patch_size)) + 
  geom_col() + 
  geom_hline(yintercept = 250)

Plot some patches

example <- findPatch(sce_prot[,sce_prot$Description %in% c("D4")], sce_prot[,sce_prot$celltype %in% c("B cell", "BnT cell")]$cellID, 
                    'cellID', 
                    'Center_X', 'Center_Y', 
                    'ImageNumber', 
                    distance = 15, 
                    min_clust_size = 20,
                    output_colname = "example_patch")
Time difference of 0.3245077 secs
[1] "patches successfully added to sce object"
example <- findMilieu(example, 
              'cellID', 
              'Center_X', 'Center_Y', 
              'ImageNumber', 
              'example_patch', 
              distance = 50,
              output_colname = "example_milieu",
              plot = TRUE)
Time difference of 0.9367313 secs
[1] "milieus successfully added to sce object"

Make grouping

1 No B cells (B cell density < 10 Bcells/mm2 and no patches) 2 No B cell Patches (B cell density > 10 Bcells/mm2 and no patches) 3 Small B cell Patches (max patch size < 250 B cells) 4 B cell Follicles (max patch size > 250 B cells)

Bcell <- data.frame(colData(sce_prot)) %>%
  group_by(Description,celltype) %>%
  summarise(n=n()) %>%
  reshape2::dcast(Description ~ celltype, value.var = "n", fill = 0) %>%
  select(Description, `B cell`)

Bcell$density <- Bcell$`B cell` / im_size_prot[match(Bcell$Description, im_size_prot$Description),]$im_size_prot

ggplot(Bcell) + geom_col(aes(x=Description, y=density))

im_w_patches <- data.frame(colData(sce_prot)) %>%
  group_by(Description) %>%
  filter(bcell_patch != 0) %>%
  distinct(bcell_patch, .keep_all = T) %>%
  summarise(n=n()) %>%
  arrange(-n)
Bcell$Bcell_patch_score <- ""
Bcell$Bcell_patch_score <- ifelse(Bcell$density < 10,"No B cells", Bcell$Bcell_patch_score)
Bcell$Bcell_patch_score <- ifelse(Bcell$density > 10,"No B cell Patches", Bcell$Bcell_patch_score)
Bcell[Bcell$Description %in% max_patch[max_patch$max_patch_size<250,]$Description,]$Bcell_patch_score <- "Small B cell Patches"
Bcell[Bcell$Description %in% max_patch[max_patch$max_patch_size>=250,]$Description,]$Bcell_patch_score <- "B cell Follicles"

Bcell %>%
  group_by(Bcell_patch_score) %>%
  summarise(n=n())
# A tibble: 4 x 2
  Bcell_patch_score        n
  <chr>                <int>
1 B cell Follicles        26
2 No B cell Patches       36
3 No B cells              87
4 Small B cell Patches    17
Bcell$Bcell_patch_score <- factor(Bcell$Bcell_patch_score, levels = c("No B cells", "No B cell Patches", "Small B cell Patches", "B cell Follicles"))

# add to sce
cur_df <- data.frame(colData(sce_prot))
cur_df <- left_join(cur_df, Bcell[,c("Description", "Bcell_patch_score")])
sce_prot$bcell_patch_score <- cur_df$Bcell_patch_score

# add to rna sce
cur_df <- data.frame(colData(sce_rna))
cur_df <- left_join(cur_df, Bcell[,c("Description", "Bcell_patch_score")])
sce_rna$bcell_patch_score <- cur_df$Bcell_patch_score

Analysis

B cell densities of the different groups

ggplot(Bcell,aes(x=Bcell_patch_score, y = log10(density+1))) + 
  geom_boxplot() + 
  geom_quasirandom() + 
  ylab("B cell density (log10)")

Number of Patients in different groups

data.frame(colData(sce_rna)) %>%
  filter(Location != "CTRL") %>%
  distinct(PatientID, .keep_all = T) %>%
  group_by(bcell_patch_score) %>%
  summarise(patients = n())
# A tibble: 4 x 2
  bcell_patch_score    patients
  <fct>                   <int>
1 No B cells                 30
2 No B cell Patches          16
3 Small B cell Patches       12
4 B cell Follicles           11

Save SCE object

saveRDS(sce_prot, file = "data/data_for_analysis/sce_protein.rds")
saveRDS(sce_rna, file = "data/data_for_analysis/sce_RNA.rds")

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] RANN_2.6.1                  ggbeeswarm_0.6.0           
 [3] sf_0.9-7                    data.table_1.13.6          
 [5] concaveman_1.1.0            cytomapper_1.3.1           
 [7] EBImage_4.32.0              forcats_0.5.0              
 [9] stringr_1.4.0               dplyr_1.0.2                
[11] purrr_0.3.4                 readr_1.4.0                
[13] tidyr_1.1.2                 tibble_3.0.4               
[15] tidyverse_1.3.0             ggridges_0.5.3             
[17] cowplot_1.1.1               reshape2_1.4.4             
[19] CATALYST_1.12.2             igraph_1.2.6               
[21] viridis_0.5.1               viridisLite_0.3.0          
[23] scater_1.16.2               ggplot2_3.3.3              
[25] SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0
[27] Biobase_2.50.0              GenomicRanges_1.42.0       
[29] GenomeInfoDb_1.26.2         IRanges_2.24.1             
[31] S4Vectors_0.28.1            BiocGenerics_0.36.0        
[33] MatrixGenerics_1.2.0        matrixStats_0.57.0         
[35] workflowr_1.6.2            

loaded via a namespace (and not attached):
  [1] utf8_1.1.4                  shinydashboard_0.7.1       
  [3] tidyselect_1.1.0            htmlwidgets_1.5.3          
  [5] grid_4.0.3                  BiocParallel_1.22.0        
  [7] Rtsne_0.15                  flowCore_2.0.1             
  [9] munsell_0.5.0               units_0.6-7                
 [11] codetools_0.2-18            withr_2.3.0                
 [13] colorspace_2.0-0            knitr_1.30                 
 [15] rstudioapi_0.13             labeling_0.4.2             
 [17] git2r_0.28.0                GenomeInfoDbData_1.2.4     
 [19] farver_2.0.3                flowWorkspace_4.0.6        
 [21] rprojroot_2.0.2             vctrs_0.3.6                
 [23] generics_0.1.0              TH.data_1.0-10             
 [25] xfun_0.20                   R6_2.5.0                   
 [27] clue_0.3-58                 rsvd_1.0.3                 
 [29] locfit_1.5-9.4              bitops_1.0-6               
 [31] DelayedArray_0.16.0         assertthat_0.2.1           
 [33] promises_1.1.1              scales_1.1.1               
 [35] multcomp_1.4-15             beeswarm_0.2.3             
 [37] gtable_0.3.0                RProtoBufLib_2.0.0         
 [39] sandwich_3.0-0              rlang_0.4.10               
 [41] systemfonts_0.3.2           GlobalOptions_0.1.2        
 [43] splines_4.0.3               hexbin_1.28.2              
 [45] broom_0.7.3                 yaml_2.2.1                 
 [47] abind_1.4-5                 modelr_0.1.8               
 [49] backports_1.2.1             httpuv_1.5.4               
 [51] RBGL_1.64.0                 tools_4.0.3                
 [53] ellipsis_0.3.1              raster_3.4-5               
 [55] RColorBrewer_1.1-2          Rcpp_1.0.5                 
 [57] plyr_1.8.6                  base64enc_0.1-3            
 [59] zlibbioc_1.36.0             classInt_0.4-3             
 [61] RCurl_1.98-1.2              FlowSOM_1.20.0             
 [63] GetoptLong_1.0.5            zoo_1.8-8                  
 [65] haven_2.3.1                 ggrepel_0.9.0              
 [67] cluster_2.1.0               fs_1.5.0                   
 [69] magrittr_2.0.1              ncdfFlow_2.34.0            
 [71] openxlsx_4.2.3              circlize_0.4.12            
 [73] reprex_0.3.0                mvtnorm_1.1-1              
 [75] whisker_0.4                 hms_0.5.3                  
 [77] mime_0.9                    evaluate_0.14              
 [79] fftwtools_0.9-9             xtable_1.8-4               
 [81] XML_3.99-0.5                rio_0.5.16                 
 [83] jpeg_0.1-8.1                readxl_1.3.1               
 [85] gridExtra_2.3               shape_1.4.5                
 [87] ggcyto_1.16.0               compiler_4.0.3             
 [89] V8_3.4.0                    KernSmooth_2.23-18         
 [91] crayon_1.3.4                htmltools_0.5.0            
 [93] later_1.1.0.1               tiff_0.1-6                 
 [95] RcppParallel_5.0.2          lubridate_1.7.9.2          
 [97] DBI_1.1.0                   dbplyr_2.0.0               
 [99] ComplexHeatmap_2.4.3        MASS_7.3-53                
[101] Matrix_1.3-2                car_3.0-10                 
[103] cli_2.2.0                   pkgconfig_2.0.3            
[105] sp_1.4-5                    foreign_0.8-81             
[107] xml2_1.3.2                  svglite_1.2.3.2            
[109] vipor_0.4.5                 XVector_0.30.0             
[111] drc_3.0-1                   rvest_0.3.6                
[113] digest_0.6.27               tsne_0.1-3                 
[115] ConsensusClusterPlus_1.52.0 graph_1.66.0               
[117] rmarkdown_2.6               cellranger_1.1.0           
[119] gdtools_0.2.3               DelayedMatrixStats_1.10.1  
[121] curl_4.3                    shiny_1.5.0                
[123] gtools_3.8.2                rjson_0.2.20               
[125] lifecycle_0.2.0             jsonlite_1.7.2             
[127] carData_3.0-4               BiocNeighbors_1.6.0        
[129] fansi_0.4.1                 pillar_1.4.7               
[131] lattice_0.20-41             fastmap_1.0.1              
[133] httr_1.4.2                  plotrix_3.7-8              
[135] survival_3.2-7              glue_1.4.2                 
[137] zip_2.1.1                   svgPanZoom_0.3.4           
[139] png_0.1-7                   Rgraphviz_2.32.0           
[141] class_7.3-17                stringi_1.5.3              
[143] nnls_1.4                    BiocSingular_1.4.0         
[145] CytoML_2.0.5                latticeExtra_0.6-29        
[147] cytolib_2.0.3               e1071_1.7-4                
[149] irlba_2.3.3