data-raw/GenerateSignatureWeightedDistributionPlots.R

#'Plot histogram for each mutation class for a given signature and uPBM experiment name
#'
#'@description
#'
#'@param uPBM_QBiC_scores All QBiC scores for a universal PBM
#'
#'@param spectrum A list of GR/LR generated by Signature-QBiC from observed mutation spectrum
#'
#'@return A plot of histograms
#'
#'@export
#'
GenerateSignatureWeightedDistributionPlots <- function(uPBM_QBiC_scores, mutation.spectrum,
                                                       output_name) {

  uPBM_QBiC_scores <- data.frame(uPBM_QBiC_scores)
  colnames(uPBM_QBiC_scores) <- "z_score"
  uPBM_QBiC_scores <- uPBM_QBiC_scores$z_score[which(!is.na(uPBM_QBiC_scores$z_score))]
  PBM.scores <- data.frame(uPBM_QBiC_scores)

  PBM.scores$uPBM_QBiC_scores[PBM.scores$uPBM_QBiC_scores>20] <-20
  PBM.scores$uPBM_QBiC_scores[PBM.scores$uPBM_QBiC_scores<(-20)] <-(-20)
  row.names(PBM.scores) <- all.possible.twelvemers$seq
  PBM.scores$mutclass <- all.possible.twelvemers$final_signature
  number <- as.integer(max(PBM.scores[, 1])) + 2
  densities.weighted <- 0

  pdf(output_name, width = 16, height = 32)
  par(mfrow = c(8, 4))
  for (mutation.type in mutation.type.list) {
    histogram_plot <- hist(PBM.scores[which(PBM.scores$mutclass ==
                                              mutation.type), 1], breaks = seq(-number, number, 0.5), plot = F)
    density_temp <- histogram_plot$density * mutation.spectrum[mutation.type, 2]
    densities.weighted <- densities.weighted + density_temp
    plot(histogram_plot, main = mutation.type, col = "black", freq = F, ylim = c(0,
                                                                                 ylimits))
    histogram_plot$density <- density_temp
    plot(histogram_plot, main = mutation.type, col = "grey", freq = F, ylim = c(0,
                                                                                ylimits))
  }
  histogram_plot <- hist(PBM.scores[, 1], breaks = seq(-number, number,
                                                       0.5), plot = F)

  plot(histogram_plot, col = "black", freq = F, ylim = c(0, ylimits))
  histogram_plot$density <- densities.weighted
  plot(histogram_plot, col = "grey", freq = F, ylim = c(0, ylimits))
  dev.off()
  return(NULL)
}
liumoLM/SigQBiC documentation built on Feb. 5, 2021, 12:53 a.m.