R/plottingFunctions.R

#' Creates standard bar plot of stacked cell counts
#'
#' Each bar is an individual cell. The number on top of each bar repsents the
#' percentage of detected transcripts in that cell, including 3-color spots.
#'
#' @param exp.data experimental data from YAML file
#' @param snp.table SNPTable to be ploted
#' @param outdir Directory to save output to
#'
#' @import dplyr
#' @import ggplot2
#' @import tidyr
#' @importFrom scales percent
MakeStackedCellCountPlot <- function(exp.data, snp.table, outdir){

  by.cell.counts <- snp.table %>% group_by(cellID, labels) %>%
    summarize(rnaCount = n()) %>% spread(labels, rnaCount) %>%
    gather(labels, rnaCount, -cellID) %>%
    mutate(rnaCount = replace(rnaCount, is.na(rnaCount), 0))

  frac.detec <- by.cell.counts %>% group_by(cellID) %>%
    summarize(fracDetec = 1 - rnaCount[labels == 'undetec']/sum(rnaCount),
              totalRNACount = sum(rnaCount))


  ## Makes stacked cell count plot
  stacked.cell.count.plot <- ggplot() +
    geom_bar(data = by.cell.counts, aes(x = cellID, y = rnaCount,
                                        fill = labels), stat = "identity") +
    geom_text(data = frac.detec, aes(label = format(100*fracDetec, digits=1, drop0trailing=TRUE),
                                     x = cellID, y= totalRNACount ), stat= "identity", vjust = -1) +
    theme_minimal() +
    scale_fill_brewer(palette="Set1") +
    ggtitle(exp.data$name)
  plot(stacked.cell.count.plot)

  currdir <- getwd()
  setwd(outdir)
  ggsave(plot =  stacked.cell.count.plot,  width = 9.89, height = 9,
         filename =  paste(exp.data$name, "_Stacked_Cell_Count_Plot.pdf", sep = ""))
  setwd(currdir)
}

#' Creates standard pie chart for cell population average
#'
#' Each slice represents the propoertion that was detected to the according label.
#'
#' @param exp.data experimental data from YAML file
#' @param snp.table SNPTable to be ploted
#' @param outdir Directory to save output to
#'
#' @import dplyr
#' @import ggplot2
#' @import tidyr
#' @importFrom scales percent
MakeTotalCellCountPie <- function(exp.data, snp.table, outdir){

  ##Total Cell Count Ratio
  totals <- snp.table %>% group_by(labels) %>%
    summarize(rnaCount = n()) %>% mutate(totalFrac = rnaCount/sum(rnaCount))

  ## Plot Pie Graph
  blank_theme <- theme_minimal()+
    theme(
      axis.title.x = element_blank(),
      axis.title.y = element_blank(),
      panel.border = element_blank(),
      panel.grid=element_blank(),
      axis.ticks = element_blank(),
      plot.title=element_text(size=14, face="bold")
    )

  overall.pie.chart <- ggplot(data = totals) +
    geom_bar(aes(x="", y = totalFrac, fill = labels), stat = "identity", width = 1) +
    coord_polar(theta="y") + blank_theme +
    theme(axis.text.x=element_blank()) +
    geom_text(aes(x = 1.8,  y = totalFrac/3 + c(0,cumsum(totalFrac[-length(totalFrac)])),
                  label = percent(totalFrac)), size=5) +
    scale_fill_brewer(palette="Set1") +
    ggtitle(exp.data$name)
  plot(overall.pie.chart)

  currdir <- getwd()
  setwd(outdir)
  ggsave(plot = overall.pie.chart,  width = 9.89, height = 9,
         filename =  paste(exp.data$name, "_Total_Cell_Pie_Chart.pdf", sep = ""))
  setwd(currdir)
}

#' Creates bar graph comparing ratio of SNP A to SNP B in cell population
#'
#'
#' @param exp.data experimental data from YAML file
#' @param guide.table Guide SNPTable to be ploted
#' @param outdir Directory to save output to
#'
#' @import dplyr
#' @import ggplot2
#' @import tidyr
#' @importFrom scales percent
MakeOverallImbalanceBar <- function(exp.data, guide.table, outdir){

total.SNP <- guide.table %>% group_by(labels) %>% summarize(rnaCount = n()) %>%
  filter((!labels %in% c("undetec", "3-color"))) %>% mutate(rnaFrac = rnaCount/sum(rnaCount))

stacked.allelic.ratio <- ggplot() +
  geom_bar(data = total.SNP, aes(x = labels, y = rnaFrac), stat = "identity") +
  ggtitle(exp.data$name) +
  ylim(0,1) +
  geom_text(data = total.SNP, aes(x = labels, y = rnaFrac + 0.1, label = percent(rnaFrac)), size = 5) +
  theme_minimal() +
  scale_fill_brewer(palette="Set1")
plot(stacked.allelic.ratio)

currdir <- getwd()
setwd(outdir)
ggsave(plot = stacked.allelic.ratio,  width = 9.89, height = 9,
       filename =  paste(exp.data$name, "_overall_imbalance_bar.pdf", sep = ""))
setwd(currdir)
}




#' Creates scatter plot between SNPA vs SNPB counts
#'
#' Can be used to measure intrinsic vs extrinsic noise
#'
#' @param exp.data experimental data from YAML file
#' @param guide.table Guide SNPTable to be ploted
#' @param outdir Directory to save output to
#'
#' @import dplyr
#' @import ggplot2
#' @import tidyr
#' @importFrom scales percent

MakeAlleleScatterPlot <- function(exp.data, guide.table, outdir){

by.cell.counts.allele <- guide.table %>% group_by(cellID, labels) %>%
  summarize(rnaCount = n()) %>% filter(!labels %in% c('undetec', '3-color') ) %>%
  spread(labels, rnaCount)

by.cell.counts.allele[is.na(by.cell.counts.allele)] <- 0

labels <- levels(guide.table$labels)

correlation <- cor(by.cell.counts.allele[labels[1]], by.cell.counts.allele[labels[2]])

allele.scatterplot <- ggplot() +
  geom_point(data = by.cell.counts.allele, aes_string(x = ggname(labels[1]), y = ggname(labels[2]))) +
  theme_minimal() +
  scale_fill_brewer(palette="Set1") +
  ggtitle(paste(exp.data$name, " Correlation = ", correlation, sep = ""))
plot(allele.scatterplot)

currdir <- getwd()
setwd(outdir)
ggsave(plot = allele.scatterplot,  width = 9.89, height = 9,
       filename =  paste(exp.data$name, "_Allele_ScatterPlot.pdf", sep = ""))
setwd(currdir)
}
pginart/SNPFISHAnalysis documentation built on May 25, 2019, 2:54 a.m.