R/mts_plotMutation.R

Defines functions mts_plotMutation

Documented in mts_plotMutation

globalVariables(c("mut_status", "log2_expression"))
mts_plotMutation <- function(resultList=resultList, mrna_gene=mrna_gene, mut_gene=mut_gene, mutMatrix=mutMatrix, tissueMatrix=tissueMatrix, diseaseFilter=diseaseFilter)
{
  ## Required input checks ## 
  if(missing(resultList))
  {
    stop("No result list provided: A mts_clusterAvg result object is required to run this function")
  } else {
    if (!is.list(resultList)) {
      stop("resultList must be a list")
      return(NULL)
    }
  }
  
  if(missing(mutMatrix))
  {
    stop("No Mutation Matrix Provided")
  } else {
    if (!is.data.frame(mutMatrix)) {
      stop("mutMatrix must be a data frame")
      return(NULL)
    }
    mutInd <- "Yes"
    if(dim(mutMatrix)[2] < 2)
    {
      stop("Object not assignable: please check this is a data.frame")
    }
  }
  
  if(length(which(names(resultList) %in% mrna_gene)) > 0)
  {
    mrna_gene <- mrna_gene
  } else {
    stop(paste(mrna_gene, " is not in supplied result list", sep=""))
  }
  
  if(length(which(rownames(mutMatrix) %in% mut_gene)) > 0)
  {
    mut_gene <- mut_gene
  } else {
    stop(paste(mut_gene, " is not in supplied mutation matrix", sep=""))
  }
  
  ## Optional input checks ##
  if(missing(tissueMatrix))
  {
    stop("No Tissue Matrix Provided")
  } else {
    if (!is.data.frame(tissueMatrix)) {
      stop("tissueMatrix must be a data frame")
      return(NULL)
    }
    tisInd <- "Yes"
  }
  
  colnames(tissueMatrix) <- c("cell_line", "tissue")

  
  if(missing(diseaseFilter))
  {
    diseaseFilter <- "All"
  } else {
    diseaseFilter <- diseaseFilter
  }
  
  
  # Set up color palette
  gg_color_hue <- function(n) {
    hues = seq(15, 375, length = n + 1)
    hcl(h = hues, l = 65, c = 100)[1:n]
  }
  
  cols <- gg_color_hue(30)
  cols2 <- c("cornflowerblue", "darkred")
  
  tab1 <- resultList[[mrna_gene]]
  tab1[,2] <- as.numeric(tab1[,2])
  tab1[,3] <- as.character(tab1[,3]) 
  colnames(tab1) <- c("cell_line", "log2_expression", "mode")
  
  #____
  mData <- as.data.frame(cbind(colnames(mutMatrix), as.character(mutMatrix[mut_gene,])), stringsAsFactors=FALSE)
  mData[,2] <- as.character(mData[,2])
  colnames(mData) <- c("cell_line", "mut_details")
  mData$mut_status <- "WT"
  mData$mut_status[which(mData$mut_details != "WT")] <- "MUT"
  
  mInt <- merge(tab1, mData)
  mInt <- merge(mInt, tissueMatrix)
  
  if(dim(mInt)[1] > 0)
  {
    if(diseaseFilter == "All")
    {
      mInt$mut_status <- factor(mInt$mut_status, levels=c("WT", "MUT"))
      ggplot(mInt, aes(x=mode, y=log2_expression, color=mut_status)) + geom_point(position = position_jitter(width = 0.2), size=8) +
        geom_boxplot(fill=NA, colour="grey") + scale_color_manual(values=cols2) + theme(panel.background = element_rect(fill = 'white', colour = 'black')) +
        theme(plot.margin = unit(c(1,1,1,1), "cm")) + labs(x=paste(mrna_gene, " mRNA Expression Cluster", sep=""),y=paste(mrna_gene, " log2 mRNA Expression", sep="")) +
        theme(axis.text.x = element_blank(), axis.text.y = element_text(size=14)) + ggtitle(paste(mrna_gene, " Expression Cluster ", mut_gene, " Enrichment", sep="")) +
        theme(plot.title = element_text(size=18, face="bold"), axis.title = element_text(size=14)) +
        theme(legend.title = element_text(size=14, face="bold"), legend.text = element_text(size=14)) + labs(color = "Mutation Class")
    }
    else {
      unD <- tissueMatrix[,2]
      if(length(which(unD %in% diseaseFilter)) == 0)
      {
        stop("Disease Filter not recognised: please check")
      }
      mInt2 <- mInt[which(mInt$tissue %in% diseaseFilter),]
      mInt2$mut_status <- factor(mInt2$mut_status, levels=c("WT", "MUT"))
      ggplot(mInt2, aes(x=mode, y=log2_expression, color=mut_status)) + geom_point(position = position_jitter(width = 0.2), size=8) +
        geom_boxplot(fill=NA, colour="grey") + scale_color_manual(values=cols2) + theme(panel.background = element_rect(fill = 'white', colour = 'black')) +
        theme(plot.margin = unit(c(1,1,1,1), "cm")) + labs(x=paste(mrna_gene, " mRNA Expression Cluster", sep=""),y=paste(mrna_gene, " log2 mRNA Expression", sep="")) +
        theme(axis.text.x = element_blank(), axis.text.y = element_text(size=14)) + ggtitle(paste(mrna_gene, " Expression Cluster ", mut_gene, " Enrichment", sep="")) +
        theme(plot.title = element_text(size=18, face="bold"), axis.title = element_text(size=14)) +
        theme(legend.title = element_text(size=14, face="bold"), legend.text = element_text(size=14)) + labs(color = "Mutation Class")
    }
  } else {
    stop("No mut available using these parameters")
  }
}

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.