params <- list(test = FALSE) ## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE ) library(BiocStyle) ## ----eval = FALSE------------------------------------------------------------- # if (!require("BiocManager")) { # install.packages("BiocManager") # } # BiocManager::install("spicyR") ## ----warning=FALSE, message=FALSE--------------------------------------------- # load required packages library(spicyR) library(ggplot2) library(SpatialExperiment) library(SpatialDatasets) library(imcRtools) library(dplyr) library(survival) ## ----warning=FALSE, message=FALSE--------------------------------------------- kerenSPE <- SpatialDatasets::spe_Keren_2018() ## ----------------------------------------------------------------------------- spicyTestPair <- spicy( kerenSPE, condition = "tumour_type", from = "CD8_T_cell", to = "Neutrophils" ) topPairs(spicyTestPair) ## ----------------------------------------------------------------------------- spicyTest <- spicy( kerenSPE, condition = "tumour_type" ) topPairs(spicyTest) ## ----------------------------------------------------------------------------- bind(spicyTest)[1:5, 1:5] ## ----------------------------------------------------------------------------- signifPlot( spicyTest, breaks = c(-3, 3, 1), marksToPlot = c("Macrophages", "DC_or_Mono", "dn_T_CD3", "Neutrophils", "CD8_T_cell", "Keratin_Tumour") ) ## ----------------------------------------------------------------------------- spicyBoxPlot(results = spicyTest, # from = "Macrophages", # to = "dn_T_CD3" rank = 1) ## ----------------------------------------------------------------------------- kerenSPE <- imcRtools::buildSpatialGraph(kerenSPE, img_id = "imageID", type = "knn", k = 20, coords = c("x", "y")) pairAbundances <- convPairs(kerenSPE, colPair = "knn_interaction_graph") head(pairAbundances["B_cell__B_cell"]) ## ----------------------------------------------------------------------------- spicyTestColPairs <- spicy( kerenSPE, condition = "tumour_type", alternateResult = pairAbundances, weights = FALSE ) topPairs(spicyTestColPairs) ## ----------------------------------------------------------------------------- signifPlot( spicyTestColPairs, breaks = c(-3, 3, 1), marksToPlot = c("Macrophages", "dn_T_CD3", "CD4_T_cell", "B_cell", "DC_or_Mono", "Neutrophils", "CD8_T_cell") ) ## ----------------------------------------------------------------------------- kerenSPE$event = 1 - kerenSPE$Censored kerenSPE$survival = Surv(kerenSPE$`Survival_days_capped*`, kerenSPE$event) ## ----------------------------------------------------------------------------- # Running survival analysis spicySurvival = spicy(kerenSPE, condition = "survival") # top 10 significant pairs head(spicySurvival$survivalResults, 10) ## ----------------------------------------------------------------------------- # filter SPE object to obtain image 24 data kerenSubset = kerenSPE[, colData(kerenSPE)$imageID == "24"] pairwiseAssoc = getPairwise(kerenSubset, sigma = NULL, Rs = 100) |> as.data.frame() pairwiseAssoc[["Keratin_Tumour__Neutrophils"]] ## ----------------------------------------------------------------------------- pairwiseAssoc = getPairwise(kerenSubset, sigma = 20, Rs = 100) |> as.data.frame() pairwiseAssoc[["Keratin_Tumour__Neutrophils"]] ## ----------------------------------------------------------------------------- # obtain colData for image 24 cData = colData(kerenSPE) |> as.data.frame() |> dplyr::filter(imageID == "24") # obtain cells present in image 24 coords = spatialCoords(kerenSPE) |> as.data.frame() coords$cellID = rownames(coords) coords = coords |> dplyr::filter(cellID %in% cData$CellID) cData$X = coords$x cData$Y = coords$y cData = cData |> dplyr::mutate(cellTypeNew = ifelse(cellType %in% c("Keratin_Tumour", "Neutrophils"), cellType, "Other")) pal = setNames(c("#d6b11c", "#850f07"), c("Keratin_Tumour", "Neutrophils")) ggplot() + stat_density_2d(data = cData, aes(x = X, y = Y, fill = after_stat(density)), geom = "raster", contour = FALSE) + geom_point(data = cData |> filter(cellType != "Other"), aes(x = X, y = Y, colour = cellTypeNew), size = 1) + scale_color_manual(values = pal) + scale_fill_distiller(palette = "Blues", direction = 1) + theme_classic() + labs(title = "image ID: 24") ## ----eval=FALSE--------------------------------------------------------------- # spicyMixedTest <- spicy( # diabetesData, # condition = "stage", # subject = "case" # ) ## ----------------------------------------------------------------------------- sessionInfo()