Last updated: 2022-02-25

Checks: 6 1

Knit directory: MelanomaIMC/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). 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 0deb8ee. 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:    code/.DS_Store
    Ignored:    code/._.DS_Store
    Ignored:    data/.DS_Store
    Ignored:    data/._.DS_Store
    Ignored:    data/data_for_analysis/
    Ignored:    data/data_for_analysis_zenodo/
    Ignored:    data/full_data.zip
    Ignored:    data/full_data/

Unstaged changes:
    Modified:   .gitignore
    Modified:   analysis/09_Tcell_Score.Rmd
    Modified:   analysis/12_Bcell_Score.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/12_Bcell_Score.rmd) and HTML (docs/12_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
html fe331cb toobiwankenobi 2022-02-22 re-run whole analysis
html 73aa800 toobiwankenobi 2022-02-22 add .html for static website
Rmd 3da15db toobiwankenobi 2021-11-24 changes for revision

Load packages and helper functions

sapply(list.files("code/helper_functions", full.names = TRUE), source)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
        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 code/helper_functions/read_Data.R
value   ?                                ?                                
visible FALSE                            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)
Loading required package: SummarizedExperiment
Loading required package: MatrixGenerics
Loading required package: matrixStats

Attaching package: 'matrixStats'
The following object is masked from 'package:dplyr':

    count

Attaching package: 'MatrixGenerics'
The following objects are masked from 'package:matrixStats':

    colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
    colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
    colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
    colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
    colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
    colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
    colWeightedMeans, colWeightedMedians, colWeightedSds,
    colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
    rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
    rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
    rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
    rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
    rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
    rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
    rowWeightedSds, rowWeightedVars
Loading required package: GenomicRanges
Loading required package: stats4
Loading required package: BiocGenerics

Attaching package: 'BiocGenerics'
The following objects are masked from 'package:dplyr':

    combine, intersect, setdiff, union
The following objects are masked from 'package:stats':

    IQR, mad, sd, var, xtabs
The following objects are masked from 'package:base':

    anyDuplicated, append, as.data.frame, basename, cbind, colnames,
    dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
    grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
    order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
    rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
    union, unique, unsplit, which.max, which.min
Loading required package: S4Vectors

Attaching package: 'S4Vectors'
The following objects are masked from 'package:dplyr':

    first, rename
The following objects are masked from 'package:base':

    expand.grid, I, unname
Loading required package: IRanges

Attaching package: 'IRanges'
The following objects are masked from 'package:dplyr':

    collapse, desc, slice
Loading required package: GenomeInfoDb
Loading required package: Biobase
Welcome to Bioconductor

    Vignettes contain introductory material; view with
    'browseVignettes()'. To cite Bioconductor, see
    'citation("Biobase")', and for packages 'citation("pkgname")'.

Attaching package: 'Biobase'
The following object is masked from 'package:MatrixGenerics':

    rowMedians
The following objects are masked from 'package:matrixStats':

    anyMissing, rowMedians
library(ggplot2)
library(scater)
Loading required package: scuttle
library(viridis)
Loading required package: viridisLite
library(igraph)

Attaching package: 'igraph'
The following object is masked from 'package:scater':

    normalize
The following object is masked from 'package:GenomicRanges':

    union
The following object is masked from 'package:IRanges':

    union
The following object is masked from 'package:S4Vectors':

    union
The following objects are masked from 'package:BiocGenerics':

    normalize, path, union
The following objects are masked from 'package:dplyr':

    as_data_frame, groups, union
The following objects are masked from 'package:stats':

    decompose, spectrum
The following object is masked from 'package:base':

    union
library(CATALYST)
library(reshape2)
library(cowplot)
library(ggridges)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ tibble  3.1.6     ✓ purrr   0.3.4
✓ tidyr   1.2.0     ✓ stringr 1.4.0
✓ readr   2.1.2     ✓ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x tibble::as_data_frame() masks igraph::as_data_frame(), dplyr::as_data_frame()
x IRanges::collapse()     masks dplyr::collapse()
x Biobase::combine()      masks BiocGenerics::combine(), dplyr::combine()
x purrr::compose()        masks igraph::compose()
x matrixStats::count()    masks dplyr::count()
x tidyr::crossing()       masks igraph::crossing()
x IRanges::desc()         masks dplyr::desc()
x tidyr::expand()         masks S4Vectors::expand()
x dplyr::filter()         masks stats::filter()
x S4Vectors::first()      masks dplyr::first()
x igraph::groups()        masks dplyr::groups()
x dplyr::lag()            masks stats::lag()
x ggplot2::Position()     masks BiocGenerics::Position(), base::Position()
x purrr::reduce()         masks GenomicRanges::reduce(), IRanges::reduce()
x S4Vectors::rename()     masks dplyr::rename()
x purrr::simplify()       masks igraph::simplify()
x IRanges::slice()        masks dplyr::slice()
library(viridis)
library(dplyr)
library(cytomapper)
Loading required package: EBImage

Attaching package: 'EBImage'
The following object is masked from 'package:purrr':

    transpose
The following object is masked from 'package:SummarizedExperiment':

    resize
The following object is masked from 'package:Biobase':

    channel
The following objects are masked from 'package:GenomicRanges':

    resize, tile
The following objects are masked from 'package:IRanges':

    resize, tile

Attaching package: 'cytomapper'
The following objects are masked from 'package:Biobase':

    channelNames, channelNames<-
library(concaveman)
library(data.table)

Attaching package: 'data.table'
The following object is masked from 'package:EBImage':

    transpose
The following object is masked from 'package:purrr':

    transpose
The following objects are masked from 'package:reshape2':

    dcast, melt
The following object is masked from 'package:SummarizedExperiment':

    shift
The following object is masked from 'package:GenomicRanges':

    shift
The following object is masked from 'package:IRanges':

    shift
The following objects are masked from 'package:S4Vectors':

    first, second
The following objects are masked from 'package:dplyr':

    between, first, last
library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
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

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

# 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)
`summarise()` has grouped output by 'Description'. You can override using the
`.groups` argument.
# assing patch score
max_patch$bcell_patch_score <- ifelse(max_patch$max_patch_size >= median(max_patch$max_patch_size), "B cell Follicles", "Small B cell Patches")

show median for the groups “B cell follicles” and “Small B cell patches”

median(max_patch$max_patch_size)
[1] 361

Make grouping

1 No B cells (lower half of median split in images with no B cell patches) 2 No B cell Patches (upper half of median split in images with no B cell patches) 3 Small B cell Patches (lower half of median split for maximum patch size per image) 4 B cell Follicles (upper half of median split for maximum patch size per image)

# images with no patches
noPatch_img <- data.frame(colData(sce_prot)) %>%
  group_by(Description) %>%
  summarise(n=sum(bcell_patch)) %>%
  distinct(Description, .keep_all = T) %>%
  ungroup() %>%
  filter(n==0)

# remove all images with patches
Bcell <- data.frame(colData(sce_prot)) %>%
  filter(Description %in% noPatch_img$Description) %>%
  group_by(Description,celltype) %>%
  summarise(n=n()) %>%
  reshape2::dcast(Description ~ celltype, value.var = "n", fill = 0) %>%
  select(Description, `B cell`)
`summarise()` has grouped output by 'Description'. You can override using the
`.groups` argument.
Bcell$density <- Bcell$`B cell` / im_size_prot[match(Bcell$Description, im_size_prot$Description),]$im_size_prot

# assing patch score
Bcell$bcell_patch_score <- ifelse(Bcell$density >= median(Bcell$density), "No B cell Patches", "No B cells")

# merge both data sets
data <- rbind(Bcell[,c("Description", "bcell_patch_score")], max_patch[,c("Description", "bcell_patch_score")])

# factorize with levels
data$bcell_patch_score <- factor(data$bcell_patch_score, levels = c("No B cells", "No B cell Patches", "Small B cell Patches", "B cell Follicles"))

# group sizes
data %>%
  group_by(bcell_patch_score) %>%
  summarise(n=n())
# A tibble: 4 × 2
  bcell_patch_score        n
  <fct>                <int>
1 No B cells              58
2 No B cell Patches       59
3 Small B cell Patches    24
4 B cell Follicles        25

show median for the groups “No B cells” and “No B cell Patches”

median(Bcell$density)
[1] 1.982121

add to sce object

cur_rna <- data.frame(colData(sce_rna))[,c("Description", "ImageNumber")]
cur_prot <- data.frame(colData(sce_prot))[,c("Description", "ImageNumber")]

cur_rna <- left_join(cur_rna, data)
Joining, by = "Description"
cur_prot <- left_join(cur_prot, data)
Joining, by = "Description"
sce_rna$bcell_patch_score <- cur_rna$bcell_patch_score
sce_prot$bcell_patch_score <- cur_prot$bcell_patch_score

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 × 2
  bcell_patch_score    patients
  <fct>                   <int>
1 No B cells                 21
2 No B cell Patches          21
3 Small B cell Patches       17
4 B cell Follicles           10

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.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 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=en_US.UTF-8   
 [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] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] RANN_2.6.1                  ggbeeswarm_0.6.0           
 [3] sf_1.0-5                    data.table_1.14.2          
 [5] concaveman_1.1.0            cytomapper_1.6.0           
 [7] EBImage_4.36.0              forcats_0.5.1              
 [9] stringr_1.4.0               purrr_0.3.4                
[11] readr_2.1.2                 tidyr_1.2.0                
[13] tibble_3.1.6                tidyverse_1.3.1            
[15] ggridges_0.5.3              cowplot_1.1.1              
[17] reshape2_1.4.4              CATALYST_1.18.1            
[19] igraph_1.2.11               viridis_0.6.2              
[21] viridisLite_0.4.0           scater_1.22.0              
[23] scuttle_1.4.0               ggplot2_3.3.5              
[25] SingleCellExperiment_1.16.0 SummarizedExperiment_1.24.0
[27] Biobase_2.54.0              GenomicRanges_1.46.1       
[29] GenomeInfoDb_1.30.1         IRanges_2.28.0             
[31] S4Vectors_0.32.3            BiocGenerics_0.40.0        
[33] MatrixGenerics_1.6.0        matrixStats_0.61.0         
[35] dplyr_1.0.7                 workflowr_1.7.0            

loaded via a namespace (and not attached):
  [1] scattermore_0.7             flowWorkspace_4.6.0        
  [3] knitr_1.37                  irlba_2.3.5                
  [5] multcomp_1.4-18             DelayedArray_0.20.0        
  [7] RCurl_1.98-1.5              doParallel_1.0.16          
  [9] generics_0.1.2              flowCore_2.6.0             
 [11] ScaledMatrix_1.2.0          terra_1.5-17               
 [13] callr_3.7.0                 TH.data_1.1-0              
 [15] proxy_0.4-26                ggpointdensity_0.1.0       
 [17] tzdb_0.2.0                  xml2_1.3.3                 
 [19] lubridate_1.8.0             httpuv_1.6.5               
 [21] assertthat_0.2.1            xfun_0.29                  
 [23] hms_1.1.1                   jquerylib_0.1.4            
 [25] evaluate_0.14               promises_1.2.0.1           
 [27] fansi_1.0.2                 dbplyr_2.1.1               
 [29] readxl_1.3.1                Rgraphviz_2.38.0           
 [31] DBI_1.1.2                   htmlwidgets_1.5.4          
 [33] ellipsis_0.3.2              ggcyto_1.22.0              
 [35] ggnewscale_0.4.5            ggpubr_0.4.0               
 [37] backports_1.4.1             cytolib_2.6.1              
 [39] svgPanZoom_0.3.4            RcppParallel_5.1.5         
 [41] sparseMatrixStats_1.6.0     vctrs_0.3.8                
 [43] abind_1.4-5                 withr_2.4.3                
 [45] ggforce_0.3.3               aws.signature_0.6.0        
 [47] svglite_2.0.0               cluster_2.1.2              
 [49] crayon_1.4.2                drc_3.0-1                  
 [51] units_0.7-2                 pkgconfig_2.0.3            
 [53] tweenr_1.0.2                vipor_0.4.5                
 [55] rlang_1.0.0                 lifecycle_1.0.1            
 [57] sandwich_3.0-1              modelr_0.1.8               
 [59] rsvd_1.0.5                  cellranger_1.1.0           
 [61] rprojroot_2.0.2             polyclip_1.10-0            
 [63] graph_1.72.0                tiff_0.1-11                
 [65] Matrix_1.4-0                raster_3.5-15              
 [67] carData_3.0-5               Rhdf5lib_1.16.0            
 [69] zoo_1.8-9                   reprex_2.0.1               
 [71] base64enc_0.1-3             beeswarm_0.4.0             
 [73] whisker_0.4                 GlobalOptions_0.1.2        
 [75] processx_3.5.2              pheatmap_1.0.12            
 [77] png_0.1-7                   rjson_0.2.21               
 [79] bitops_1.0-7                shinydashboard_0.7.2       
 [81] getPass_0.2-2               KernSmooth_2.23-20         
 [83] rhdf5filters_1.6.0          ConsensusClusterPlus_1.58.0
 [85] DelayedMatrixStats_1.16.0   classInt_0.4-3             
 [87] shape_1.4.6                 jpeg_0.1-9                 
 [89] rstatix_0.7.0               ggsignif_0.6.3             
 [91] aws.s3_0.3.21               beachmat_2.10.0            
 [93] scales_1.1.1                magrittr_2.0.2             
 [95] plyr_1.8.6                  hexbin_1.28.2              
 [97] zlibbioc_1.40.0             compiler_4.1.2             
 [99] RColorBrewer_1.1-2          plotrix_3.8-2              
[101] clue_0.3-60                 cli_3.1.1                  
[103] XVector_0.34.0              ncdfFlow_2.40.0            
[105] ps_1.6.0                    FlowSOM_2.2.0              
[107] MASS_7.3-55                 tidyselect_1.1.1           
[109] stringi_1.7.6               RProtoBufLib_2.6.0         
[111] yaml_2.2.2                  BiocSingular_1.10.0        
[113] locfit_1.5-9.4              latticeExtra_0.6-29        
[115] ggrepel_0.9.1               grid_4.1.2                 
[117] sass_0.4.0                  tools_4.1.2                
[119] parallel_4.1.2              CytoML_2.6.0               
[121] circlize_0.4.13             rstudioapi_0.13            
[123] foreach_1.5.2               git2r_0.29.0               
[125] gridExtra_2.3               farver_2.1.0               
[127] Rtsne_0.15                  digest_0.6.29              
[129] shiny_1.7.1                 Rcpp_1.0.8                 
[131] car_3.0-12                  broom_0.7.12               
[133] later_1.3.0                 httr_1.4.2                 
[135] ComplexHeatmap_2.10.0       colorspace_2.0-2           
[137] rvest_1.0.2                 XML_3.99-0.8               
[139] fs_1.5.2                    splines_4.1.2              
[141] RBGL_1.70.0                 sp_1.4-6                   
[143] systemfonts_1.0.3           xtable_1.8-4               
[145] jsonlite_1.7.3              R6_2.5.1                   
[147] pillar_1.7.0                htmltools_0.5.2            
[149] mime_0.12                   nnls_1.4                   
[151] glue_1.6.1                  fastmap_1.1.0              
[153] BiocParallel_1.28.3         BiocNeighbors_1.12.0       
[155] fftwtools_0.9-11            class_7.3-20               
[157] codetools_0.2-18            mvtnorm_1.1-3              
[159] utf8_1.2.2                  lattice_0.20-45            
[161] bslib_0.3.1                 curl_4.3.2                 
[163] colorRamps_2.3              gtools_3.9.2               
[165] survival_3.2-13             rmarkdown_2.11             
[167] munsell_0.5.0               e1071_1.7-9                
[169] rhdf5_2.38.0                GetoptLong_1.0.5           
[171] GenomeInfoDbData_1.2.7      iterators_1.0.13           
[173] HDF5Array_1.22.1            haven_2.4.3                
[175] gtable_0.3.0