R/02_signature_plot.R

Defines functions rankingplot_separate_group PCAplot

Documented in PCAplot rankingplot_separate_group

#' PCAplot() Function
#'
#' This function generate PCA plots according to batch
#' @param vsd : vsd data (processed with DESeq2 + vst)
#' @param selected_group : groups that is visualised in PCA plots, default = "group"
#' @param batch : batch = NULL if not, check PCA plots with Chip variable
#' @param outDIR : directory to save plots
#' @return list 
#' @export
#' @examples
#' PCAplot()
#' 
PCAplot <- function(vsd, selected_group = "group", batch, outDIR){
  message("** Performing PCA")
  
  # PCA plot by group variable
  pca1 <- DESeq2::plotPCA(vsd,intgroup=c("group")) 
  ggplot2::ggsave(pca1, filename = paste0("./",outDIR,"/1_PCA_byGroup.png"), width = 14, height = 9, bg = "white")
  
  # PCA plot with selected group variable
  pcaData <- DESeq2::plotPCA(vsd,intgroup=selected_group,returnData=TRUE)
  percentVar <- round(100*attr(pcaData,"percentVar"))
  
  # need BulkName for labeling
  pca2 <- ggplot2::ggplot(pcaData, aes(PC1,PC2,color=group.1)) + 
    geom_point() +
    geom_text(
      label=pcaData$BulkName,
      nudge_x=0.45, nudge_y=0.1,
      check_overlap=T) +
    xlab(paste0("PC1: ",percentVar[1],"% variance")) +
    ylab(paste0("PC2: ",percentVar[2],"% variance"))
  ggplot2::ggsave(pca2, filename = paste0("./",outDIR,"/2_PCA_byGroup_wSamples.png"), width = 14, height = 9, bg = "white")
  
  if(!is.null(batch)){
    message("** Generating PCA plots after batch correction")
    pcaData$Chip <- as.factor(pcaData$Batch)
    pca2 <- ggplot2::ggplot(pcaData,aes(PC1,PC2,color=Batch)) + 
      geom_point() +
      geom_text(
        label=pcaData$BulkName,
        nudge_x=0.45, nudge_y=0.1,
        check_overlap=T) +
      xlab(paste0("PC1: ",percentVar[1],"% variance")) +
      ylab(paste0("PC2: ",percentVar[2],"% variance"))
    ggplot2::ggsave(pca2, filename = paste0("./",outDIR,"/2_PCA_byChip_wSamples_batch.png"), width = 14, height = 9, bg = "white")
  }
  
}


#' rankingplot_separate_group() Function
#'
#' This function plots ranking in group separation
#' @param gene_ranked : data contains gene ranking
#' @param colorvalue : values of colors with ranking groups
#' @param ranking_groups : groups that are used to calculate the ranking
#' @param outDIR : directory to save plots
#' @param fileName : names added in file name
#' @keywords ranking
#' @return normalised matrix 
#' @export
#' @examples
#' rankingplot_separate_group()
#' 
rankingplot_separate_group <- function(gene_ranked, colorvalue, ranking_groups, outDIR, fileName){
  
  # convert the data format to plot
  n <- ncol(gene_ranked)-1
  gene_ranked_final <- list()
  for(i in 1:n){
    setD <- gene_ranked[,c(i, ncol(gene_ranked))]
    setD$variable <- colnames(setD)[1]
    colnames(setD) <- c("value", "GeneName", "Group")
    gene_ranked_final[[i]] <- setD
  }
  gene_ranked_final <- rbindlist(gene_ranked_final)
  
  # to plot in order of ranking groups
  gene_ranked_final$Group <- factor(gene_ranked_final$Group, levels=c("final_score", ranking_groups))
  all_plot <- ggplot2::ggplot(gene_ranked_final, aes(GeneName,value,fill=Group)) + geom_bar(stat="identity") +
    theme_bw()+
    facet_wrap("Group",ncol = 1,scales = "free") + 
    theme(text = element_text(size=10),
          axis.text.x = element_text(angle = 40, hjust = 1),
          strip.text.x = element_text(size = 8)) +
    labs(y='Score', x='Gene') + scale_fill_manual(values=colorvalue) +
    geom_vline(xintercept = 26, linetype='dashed', color='red', size=1) 
  
  # save the plot
  ggplot2::ggsave(all_plot,filename = paste0("./",outDIR,"/",fileName,"_Score_byGene_ranked_group.png"),dpi = 300, width = 22,height = 12, bg = "white")
}
jyoh1248/MyoSignature documentation built on May 18, 2022, 12:37 a.m.