Back to home

Generate ternary plots for batch characterization

libraries

suppressPackageStartupMessages({
    library(scater)
    library(ggtern)
    library(gridExtra)
    library(scran)
    library(cowplot)
    library(dplyr)
    library(magrittr)
    library(purrr)
    library(jcolors)
    library(hrbrthemes)
    library(tidyr)
  library(here)
    })

Data

#data_path <- c("/run/user/1000/gvfs/sftp:host=imlsportmacquarie.uzh.ch/home/aluetg/batch_characterization/batch_snakemake/out/variance_part_ct/")
data_path <- params$data_path
datasets <- list.files(path = data_path, pattern = "sce.rds")

cols_data <-c(c(jcolors('pal6'),jcolors('pal8'))[c(1,8,14,2:4,6,12:13,15:20)],jcolors('pal4'))
names(cols_data) <- c()
out_path <- params$out_path

Ternary plots

#prepare data frame
prep_data <- function(dat){
    data_nam <- gsub("vp_ct_", "", dat) %>% gsub("_sce.rds", "",.)
    sce <- readRDS(paste0(data_path, dat))
    type <-  grep("_ct$", names(rowData(sce)), value = TRUE)
    tab <- as.data.frame(rowData(sce)[,type])
    tab$dataset <- rep(data_nam, nrow(tab))
    rm(sce)
    gc()
    tab
}

type_data <- datasets %>% map(prep_data) %>% bind_rows()
type_data$dataset <- as.factor(type_data$dataset)
type_data <- type_data %>% dplyr::filter(vp_residuals_ct < 0.95)

Plot

t1 <- ggtern(data=type_data, aes(vp_celltype_ct, vp_batch_ct, vp_cellt_by_batch_ct)) +
  guides(color = guide_legend(override.aes = list(size = 3, alpha = 1))) +
  geom_point(aes(colour = dataset),size= 1.6, alpha = 0.9)  +
  scale_colour_manual(values = cols_data) +
  #stat_density_tern(aes(fill=..level.., alpha=..level..),geom='polygon',bdl = 0.01,bdl.val = 1, base = "identity", n = 20) +
  geom_density_tern(
        bins=10,
        bdl = 0.02,
        color='black',
        base = "identity", n = 20,
        bdl.val = 0.1) +
  Llab("cell\n \t type") +
  Tlab("batch") +
  Rlab("int.") +
  ggtitle("Percent variance explained by ...:") +
  theme_ipsum(axis = TRUE,
              base_family = 'Helvetica',
              base_size = 19,
              axis_title_size = 20,
              strip_text_face = "bold",
              grid_col = "#cccccc",
              plot_margin = margin(0, 0, 0, 0)) +
   facet_wrap(~dataset, ncol = 3) +
    theme(plot.title = element_text(size=24, hjust = 0.3),
          plot.subtitle = element_text(size=20),
          panel.grid.major = element_blank(),
          axis.text.x = element_blank(),
          axis.text.y = element_blank(),
          panel.spacing = unit(0.2, "lines"),
          legend.position = "none",
          strip.text = element_text(face="bold", size=22)
        )

t1
## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

saveRDS(t1, paste0(out_path, "ternary_ct.rds"))
ggtern::ggsave(filename = paste0(out_path, "ternary_ct.pdf"), plot = t1, dpi = 600)
## Saving 14 x 14 in image
## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9
type_data <- type_data %>% dplyr::filter(!dataset %in% "pancreas")

t1 <- ggtern(data=type_data, aes(vp_celltype_ct, vp_batch_ct, vp_cellt_by_batch_ct)) +
  guides(color = guide_legend(override.aes = list(size = 3, alpha = 1))) +
  geom_point(aes(colour = dataset),size= 1.6, alpha = 0.9)  +
  scale_colour_manual(values = cols_data) +
  #stat_density_tern(aes(fill=..level.., alpha=..level..),geom='polygon',bdl = 0.01,bdl.val = 1, base = "identity", n = 20) +
  geom_density_tern(
        bins=10,
        bdl = 0.02,
        color='black',
        base = "identity", n = 20,
        bdl.val = 0.1) +
  Llab("cell\n \t type") +
  Tlab("batch") +
  Rlab("int.") +
  ggtitle("Percent variance explained by ...:") +
  theme_ipsum(axis = TRUE,
              base_family = 'Helvetica',
              base_size = 19,
              axis_title_size = 20,
              strip_text_face = "bold",
              grid_col = "#cccccc",
              plot_margin = margin(0, 0, 0, 0)) +
   facet_wrap(~dataset, ncol = 2) +
    theme(plot.title = element_text(size=24, hjust = 0.3),
          plot.subtitle = element_text(size=20),
          panel.grid.major = element_blank(),
          axis.text.x = element_blank(),
          axis.text.y = element_blank(),
          panel.spacing = unit(0.2, "lines"),
          legend.position = "none",
          strip.text = element_text(face="bold", size=22)
        )

t1
## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

saveRDS(t1, paste0(out_path, "ternary_ct_nopan.rds"))
ggtern::ggsave(filename = paste0(out_path, "ternary_ct_nopan.pdf"), plot = t1, dpi = 600)
## Saving 9 x 18 in image
## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)

## Warning: Computation failed in `stat_density_tern()`:
## unused argument (z.range = z.range)
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9

## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x9