Libraries

rm(list=ls())

library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## 
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## 
## The following object is masked from 'package:purrr':
## 
##     compact
library(readxl)
library(dplyr)
library(reshape)
## 
## Attaching package: 'reshape'
## 
## The following objects are masked from 'package:plyr':
## 
##     rename, round_any
## 
## The following object is masked from 'package:lubridate':
## 
##     stamp
## 
## The following object is masked from 'package:dplyr':
## 
##     rename
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, smiths
library(cowplot)
## 
## Attaching package: 'cowplot'
## 
## The following object is masked from 'package:reshape':
## 
##     stamp
## 
## The following object is masked from 'package:lubridate':
## 
##     stamp

Load Object data

Load data

rm(list=ls())
load(file="./_processed_data/PDL1_imm_objects.rdata")
load(file="./_processed_data/LAG3_imm_objects.rdata")
df_meta <- read.csv(file="./_processed_data/metadata.csv")

LAG3_markers <- c('NK_cell', 'NK_cell_LAG3', 'B_cell', 'B_cell_LAG3', 'DC', 'DC_LAG3', 'SOX2_DAPI_pos')
PDL1_markers <- c('M1_TAM', 'M1_TAM_PDL1', 'M2_TAM', 'M2_TAM_PDL1', 'CD15', 'CD15_PDL1', 'CD45RO', 'CD45RO_PDL1')

load("./_processed_data/LAG3_corr_filenames.RData") # corr_LAG3_filenames
load("./_processed_data/PDL1_corr_filenames.RData") # corr_PDL1_filenames

corr_filenames = rbind(corr_PDL1_filenames, corr_LAG3_filenames)

List of samples: unique(df_meta$sample_id)

unique(df_meta$sample_id)
##  [1] "C003018" "C006279" "C006312" "C006864" "C007312" "C007313" "C007646"
##  [8] "C007319" "C006887" "C005253" "C000116" "C001285" "C006807" "C007153"
## [15] "C008785" "C008811" "C009860" "C001944" "C001368" "C003205" "C004462"
## [22] "C000209" "C000233" "C000252" "C000458" "C000490" "C000498" "C001039"
## [29] "C002922" "C002923" "C000184" "C000468" "C001199" "C002644" "C003354"
## [36] "C006609" "C006770" "C007253" "C007649" "C007675" "C008391" "C008977"
## [43] "C004752" "C003130" "C006578" "C008690" "C006033" "C007604" "C006693"
## [50] "C008277" "C008279" "C009812"
sample_toUse <- "C006864"
print(paste0("Generating pseudo-image for sample: ", as.character(sample_toUse)))
## [1] "Generating pseudo-image for sample: C006864"
findfile = df_meta %>% select(filename_clean, 
                              sample_id, multiplex, cycle, run, sample_type) %>% unique(.) %>%
  plyr::join(., corr_filenames) %>%
  filter(sample_id == sample_toUse)
## Joining by: filename_clean
findfile_PDL1_primary = findfile %>% filter(multiplex == "PDL1" & sample_type == "A") %>% .$filename_object
print(paste0("Using PDL1 file: ", as.character(findfile_PDL1_primary)))
## [1] "Using PDL1 file: C006864_PDL1_2_R2_A.tif"
findfile_LAG3_primary = findfile %>% filter(multiplex == "LAG3" & sample_type == "A") %>% .$filename_object
print(paste0("Using LAG3 file: ", as.character(findfile_LAG3_primary)))
## [1] "Using LAG3 file: C006864_LAG3_2_R2_A.tif"

Plot pseudoimage

# ggplot(df_PDL1_imm_only %>% filter(filename == "C004462_PDL1_R2_A.tif" &
# ggplot(df_PDL1_imm_only %>% filter(filename == "C000490_PDL1_R2_A.tif" &
                                     # `Analysis Region` == "TC")) + 
p_pdl1 <- ggplot(df_PDL1_imm_only %>% filter(filename == findfile_PDL1_primary)) + 
  # geom_point(aes(x=X.Coord, y=Y.Coord, col=factor(M2_TAM)), size=0.0001) +
  geom_point(data = . %>% filter(Epithelial_cell == 1), aes(x=X.Coord, y=Y.Coord, color='Epithelial cell'), 
             alpha=1/10, size=0.0001) +
  geom_point(data = . %>% filter(Epithelial_cell == 0), aes(x=X.Coord, y=Y.Coord, color='Stroma'), 
             alpha=1/10, size=0.0001) +
  geom_point(data = . %>% filter(CD15 == 1), aes(x=X.Coord, y=Y.Coord, color='CD15'), size=0.1) +
  geom_point(data = . %>% filter(CD15_PDL1 == 1), aes(x=X.Coord, y=Y.Coord, color='CD15_PDL1'), alpha=1/2, size=0.1) +
  geom_point(data = . %>% filter(M2_TAM == 1), aes(x=X.Coord, y=Y.Coord, color='M2_TAM'), size=0.1) +
  geom_point(data = . %>% filter(M2_TAM_PDL1 == 1), aes(x=X.Coord, y=Y.Coord, color='M2_TAM_PDL1'),alpha=1/2,  size=0.1) +
  geom_point(data = . %>% filter(M1_TAM == 1), aes(x=X.Coord, y=Y.Coord, color='M1_TAM'), size=0.1) +
  geom_point(data = . %>% filter(M1_TAM_PDL1 == 1), aes(x=X.Coord, y=Y.Coord, color='M1_TAM_PDL1'),alpha=1/2,  size=0.1) +
  # scale_color_manual(values = c("grey", "red", "blue")) + 
  scale_colour_manual(name = "Cell type", drop=FALSE,
         values = c('Epithelial cell' = 'black', 
                    'Stroma' = 'lightyellow',
                    'CD15'='lightgreen',
                    'CD15_PDL1'='darkgreen',
                    'M1_TAM'='lightpink', 
                    'M1_TAM_PDL1' = 'red',
                    'M2_TAM'='lightblue', 
                    'M2_TAM_PDL1' = 'blue'),
         breaks=c('Epithelial cell', 'Stroma', 'CD15', 'CD15_PDL1', 'M1_TAM', 'M1_TAM_PDL1','M2_TAM','M2_TAM_PDL1')) +
  guides(colour = guide_legend(override.aes = list(size=3, shape=19))) +
  theme_minimal() +
  coord_equal(expand=FALSE) +
  theme(panel.background = element_rect(linewidth=1, fill=NA), legend.position = "right",
        strip.text = element_text(face="bold", size=12),
        axis.title = element_blank(), axis.text = element_blank()) + 
    # facet_grid(`Analysis Region`~filename, switch="y")
    facet_wrap(~filename)
# ggplot(df_LAG3_imm_only %>% filter(filename == "C000490_LAG3_R2_A.tif" &
# ggplot(df_LAG3_imm_only %>% filter(filename == "C004462_LAG3_R2_A.tif" &
# ggplot(df_LAG3_imm_only %>% filter(filename == "C000252_LAG3_R2_A.tif" &
# ggplot(df_LAG3_imm_only %>% filter(filename == "C000233_LAG3_R2_A.tif" &
                                     # `Analysis Region` == "TC")) + 
# p_lag3 <- ggplot(df_LAG3_imm_only %>% filter(filename == "C003130_LAG3_2_R1_A.tif")) + 
p_lag3 <- ggplot(df_LAG3_imm_only %>% filter(filename == findfile_LAG3_primary)) + 
  # geom_point(aes(x=X.Coord, y=Y.Coord, col=factor(M2_TAM)), size=0.0001) +
  geom_point(data = . %>% filter(Epithelial == 1), aes(x=X.Coord, y=Y.Coord, color='Epithelial cell'), 
             alpha=1/10, size=0.0001) +
  geom_point(data = . %>% filter(Epithelial == 0), aes(x=X.Coord, y=Y.Coord, color='Stroma'), 
             alpha=1/2, size=0.0001) +
  geom_point(data = . %>% filter(NK_cell_noLAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='NK_cell_noLAG3'), size=0.1) +
  geom_point(data = . %>% filter(NK_cell_LAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='NK_cell_LAG3'), size=0.1) +
  geom_point(data = . %>% filter(B_cell_noLAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='B_cell_noLAG3'), size=0.1) +
  geom_point(data = . %>% filter(B_cell_LAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='B_cell_LAG3'),  size=0.1) +
  geom_point(data = . %>% filter(DC_noLAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='DC_noLAG3'), size=0.1) +
  geom_point(data = . %>% filter(DC_LAG3 == 1), aes(x=X.Coord, y=Y.Coord, color='DC_LAG3'),  size=0.1) +
  # geom_point(data = . %>% filter(SOX2_DAPI_neg == 1), aes(x=X.Coord, y=Y.Coord, color='SOX2_DAPI_neg'), size=0.1) +
  geom_point(data = . %>% filter(SOX2_DAPI_pos == 1), aes(x=X.Coord, y=Y.Coord, color='SOX2_DAPI_pos'),  size=0.1) +
  # scale_color_manual(values = c("grey", "red", "blue")) + 
  scale_colour_manual(name = "Cell type", drop=FALSE,
         values = c('Epithelial cell' = 'grey30', 
                    'Stroma' = 'lightyellow',
                    'NK_cell_noLAG3'='#b8e186',
                    'NK_cell_LAG3'='#4dac26',
                    'B_cell_noLAG3'='#f1b6da', 
                    'B_cell_LAG3' = '#d01c8b',
                    'DC_noLAG3'='#92c5de', 
                    'DC_LAG3' = '#0571b0',
                    # 'SOX2_DAPI_neg'='#c2a5cf', 
                    'SOX2_DAPI_pos' = '#7b3294'),
         breaks=c('Epithelial cell', 'Stroma', 'NK_cell_noLAG3', 'NK_cell_LAG3', 
         'B_cell_noLAG3', 'B_cell_LAG3','DC_noLAG3','DC_LAG3', #'SOX2_DAPI_neg', 
                  'SOX2_DAPI_pos')) +
  guides(colour = guide_legend(override.aes = list(size=3, shape=19))) +
  theme_minimal() +
  coord_equal(expand=FALSE) +
  theme(panel.background = element_rect(linewidth=1, fill=NA), legend.position = "right",
        strip.text = element_text(face="bold", size=12),
        axis.title = element_blank(), axis.text = element_blank()) + 
  # facet_grid(`Analysis Region`~filename, switch="y")
  facet_wrap(~filename) #+ 
  # geom_polygon(aes(x = X.Coord, y = Y.Coord, colour=issue), fill=NA, data = hulls, alpha = 0.5)
# p_region <- ggplot(df_LAG3_imm_only %>% filter(filename == "C003130_LAG3_2_R1_A.tif")) + 
p_region <- ggplot(df_LAG3_imm_only %>% filter(filename == findfile_LAG3_primary)) + 
  geom_point(aes(x=X.Coord, y=Y.Coord, col=factor(`Analysis Region`)), size=0.0001, alpha=1/10) +
  guides(colour = guide_legend(override.aes = list(size=3, shape=19, alpha=1))) +
  theme_minimal() +
  coord_equal(expand=FALSE) +
  theme(panel.background = element_rect(linewidth=1, fill=NA), legend.position = "right",
        strip.text = element_text(face="bold", size=12),
        axis.title = element_blank(), axis.text = element_blank()) + 
  scale_colour_manual(name = "Analysis Region", drop=FALSE,
         values = c('IMT3'='#fde0ef',
                    'IMT2' = '#e9a3c9',
                    'IMT1' = '#c51b7d',
                    'IMS3' = '#4d9221',
                    'IMS2'='#7fbc41', 
                    'IMS1'='#b8e186',
                    'TC'='grey30', 
                    'SC' = 'grey70'),
         breaks = c("TC","IMT3", "IMT2", "IMT1", "IMS1", "IMS2", "IMS3", "SC")) +
  facet_wrap(~filename) #+ 
  # geom_point(data = hulls, aes(x = X.Coord, y = Y.Coord, colour=issue), shape=20) #+
  # geom_polygon(data = hulls, aes(x = X.Coord, y = Y.Coord, colour=issue), fill=NA, alpha = 0.5)
p_classifier <- ggplot(df_LAG3_imm_only %>% filter(filename == findfile_LAG3_primary)) + 
  geom_point(aes(x=X.Coord, y=Y.Coord, col=factor(`Classifier Label`)), size=0.0001, alpha=1/10) +
  guides(colour = guide_legend(override.aes = list(size=3, shape=19, alpha=1))) +
  theme_minimal() +
  coord_equal(expand=FALSE) +
  theme(panel.background = element_rect(linewidth=1, fill=NA), legend.position = "right",
        strip.text = element_text(face="bold", size=12),
        axis.title = element_blank(), axis.text = element_blank()) + 
  scale_colour_manual(name = "Classifier Label", drop=FALSE,
         values = c('KP'='#1f78b4',
                    'EDP' = '#b2df8a',
                    'DP' = '#33a02c',
                    'UDP' = '#fb9a99',
                    'Tumour'='#e31a1c', 
                    'Stroma' = 'grey'),
         breaks = c("KP","EDP", "DP", "UDP", "Tumour", "Stroma")) +
  facet_wrap(~filename) #+ 
  # geom_point(data = hulls, aes(x = X.Coord, y = Y.Coord, colour=issue), shape=20) #+
  # geom_polygon(data = hulls, aes(x = X.Coord, y = Y.Coord, colour=issue), fill=NA, alpha = 0.5)
p<- plot_grid(p_classifier, p_region, p_pdl1, p_lag3)
ggsave(p, height=12, width=16, filename=paste0("./_results/pseudoimage_", unique(findfile$sample_id), ".pdf"))

find Hulls / perimeteres using XY co-ordinates

# tt <- df_LAG3_imm_only %>% filter(filename == "C003130_LAG3_2_R1_A.tif") %>% select(issue=`Analysis Region`, X.Coord, Y.Coord)
# 
# #getting the convex hull of each unique point set
# find_hull <- function(df) df[chull(df$X.Coord, df$Y.Coord), ]
# hulls <- ddply(tt, "issue", find_hull)
# 
# plot <- ggplot(data = tt, aes(x = X.Coord, y = Y.Coord, colour=issue, fill = issue)) +
# # geom_point() + 
# geom_polygon(data = hulls, alpha = 0.5) +
# labs(x = "Efficiency", y = "Mandate")
# plot
#