R/mts_plotCRISPRGeneCluster.R

Defines functions mts_plotCRISPRGeneCluster

Documented in mts_plotCRISPRGeneCluster

globalVariables(c("crispr_score", "tissue", "log2_expression", "Sample"))
mts_plotCRISPRGeneCluster <- function(mrna_gene, crispr_gene, resultList,
                                      crisprMatrix, tissueMatrix,
                                      diseaseFilter = "All",
                                      mixModelClustersXPR = NULL,
                                      plotType = "Integrated") {
  
  ## ---- validate plotType (applies to ALL input paths) ----
  pType <- c("Integrated", "mrna_only", "crispr_only")
  if (!(is.character(plotType) && length(plotType) == 1L && plotType %in% pType)) {
    stop("Plot type is not recognised, please use 'Integrated', 'mrna_only' or 'crispr_only'")
  }
  
  ## ---- required arguments ----
  if (missing(mrna_gene) || !(is.character(mrna_gene) && length(mrna_gene) == 1L)) {
    stop("mrna_gene must be a single gene name")
  }
  if (plotType != "mrna_only" &&
      (missing(crispr_gene) || !(is.character(crispr_gene) && length(crispr_gene) == 1L))) {
    stop("crispr_gene must be a single gene name (required unless plotType = 'mrna_only')")
  }
  if (missing(resultList) && is.null(mixModelClustersXPR)) {
    stop("Provide either 'resultList' or 'mixModelClustersXPR'")
  }
  
  ## ---- resolve expression-cluster table (tab1) from whichever source ----
  if (!is.null(mixModelClustersXPR)) {
    if (!is.list(mixModelClustersXPR)) stop("mixModelClustersXPR must be a list")
    if (!(mrna_gene %in% names(mixModelClustersXPR))) {
      stop(paste0(mrna_gene, " is not in the supplied mixModelClustersXPR"))
    }
    tab1 <- mixModelClustersXPR[[mrna_gene]]
  } else {
    if (!is.list(resultList)) stop("resultList must be a list")
    if (!(mrna_gene %in% names(resultList))) {
      stop(paste0(mrna_gene, " is not in supplied result list"))
    }
    tab1 <- resultList[[mrna_gene]][[2]]
  }
  colnames(tab1) <- c("Sample", "log2_expression", "mode")
  
  if (plotType != "mrna_only") {
    if (missing(crisprMatrix))        stop("No CRISPR Matrix Provided provided")
    if (!is.data.frame(crisprMatrix)) stop("crisprMatrix must be a data frame")
    if (!(crispr_gene %in% rownames(crisprMatrix))) {
      stop(paste0(crispr_gene, " is not in supplied crispr matrix"))
    }
    
    if (missing(tissueMatrix))        stop("No tissueMatrix provided. Please provide a valid tissueMatrix.")
    if (!is.data.frame(tissueMatrix)) stop("tissueMatrix must be a data frame")
    colnames(tissueMatrix) <- c("Sample", "tissue")
    if (length(which(tissueMatrix$Sample %in% colnames(crisprMatrix))) == 0) {
      stop("Samples in the provided tissueMatrix do not overlap with the crisprMatrix")
    }
  }
  
  ## ---- colour palette ----
  gg_color_hue <- function(n) {
    hues <- seq(15, 375, length = n + 1)
    hcl(h = hues, l = 65, c = 100)[1:n]
  }
  
  ## ---- build CRISPR score table (not needed for mrna_only) ----
  cData <- NULL
  if (plotType != "mrna_only") {
    cData <- data.frame(Sample       = colnames(crisprMatrix),
                        crispr_score = as.numeric(crisprMatrix[crispr_gene, ]),
                        stringsAsFactors = FALSE)
  }
  
  ## ---- plotting ----
  if (plotType == "Integrated") {
    
    mInt <- merge(tab1, cData)
    mInt$mode <- factor(mInt$mode)
    
    if (diseaseFilter != "All") {
      if (length(which(tissueMatrix[, 2] %in% diseaseFilter)) == 0) {
        stop("Disease filter not recognised: please check")
      }
      tissueMatrix <- tissueMatrix[which(tissueMatrix[, 2] %in% diseaseFilter), ]
    }
    mInt <- merge(mInt, tissueMatrix)
    mInt$mode <- factor(mInt$mode)
    
    unD  <- sort(unique(mInt$tissue))
    cols <- gg_color_hue(length(unD))
    names(cols) <- unD
    
    ggplot(mInt, aes(x = mode, y = crispr_score, color = tissue)) +
      geom_point(position = position_jitter(width = 0.2), size = 8) +
      geom_boxplot(fill = NA, colour = "grey") +
      scale_color_manual(values = cols) +
      theme(panel.background = element_rect(fill = "white", colour = "black")) +
      theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) +
      labs(x = paste0(mrna_gene, " mRNA Expression Cluster"),
           y = paste0(crispr_gene, " CRISPR Score")) +
      theme(axis.text.x = element_blank()) +
      ggtitle(paste0(crispr_gene, " CRISPR Score split by ", mrna_gene, " Expression Cluster")) +
      theme(plot.title = element_text(size = 18, face = "bold"),
            axis.title = element_text(size = 20)) +
      theme(legend.title = element_text(size = 14, face = "bold"),
            legend.text  = element_text(size = 12))
    
  } else if (plotType == "mrna_only") {
    
    tab1$mode <- factor(tab1$mode)
    tab1$log2_expression <- as.numeric(tab1$log2_expression)
    
    ggplot(tab1, aes(x = log2_expression, fill = mode)) +
      geom_density(alpha = .3) +
      theme(panel.background = element_rect(fill = "white", colour = "black")) +
      theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) +
      labs(x = paste0(mrna_gene, " log2 mRNA Expression"), y = "Density") +
      theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
      ggtitle(paste0(mrna_gene, " density split by MultiSEp cluster")) +
      theme(plot.title = element_text(lineheight = .12, face = "bold")) +
      theme(text = element_text(size = 14),
            axis.text.x = element_text(angle = 90, vjust = 1))
    
  } else if (plotType == "crispr_only") {
    
    mInt <- merge(tab1, cData)
    mInt$mode <- factor(mInt$mode)
    
    if (diseaseFilter != "All") {
      if (length(which(tissueMatrix[, 2] %in% diseaseFilter)) == 0) {
        stop("Disease filter not recognised: please check")
      }
      tissueMatrix <- tissueMatrix[which(tissueMatrix[, 2] %in% diseaseFilter), ]
      mInt <- merge(mInt, tissueMatrix)
    }
    mInt <- mInt[order(mInt$crispr_score), ]
    mInt$Sample <- factor(mInt$Sample, levels = unique(mInt$Sample))
    
    ggplot(mInt, aes(x = Sample, y = crispr_score, fill = mode)) +
      geom_bar(stat = "identity") +
      theme(panel.background = element_rect(fill = "white", colour = "black")) +
      theme(plot.margin = unit(c(1, 1, 1, 1), "cm")) +
      labs(x = "Sample", y = paste0(crispr_gene, " CRISPR Score")) +
      theme(axis.text.x = element_blank()) +
      ggtitle(paste0(crispr_gene, " CRISPR score coloured by ", mrna_gene, " expression cluster")) +
      theme(plot.title = element_text(size = 18, face = "bold"),
            axis.title = element_text(size = 20)) +
      theme(legend.title = element_text(size = 20, face = "bold"),
            legend.text  = element_text(size = 16))
  }
}

Try the MultiSEp package in your browser

Any scripts or data that you put into this service are public.

MultiSEp documentation built on Aug. 27, 2026, 5:07 p.m.