R/DEGsVisualization.R

Defines functions DEGsVisualization

Documented in DEGsVisualization

#' Visualization of differently expressed genes
#'
#' use the DEGsVisualization() fuction to show the results of DEGs in samples.
#' @import DESeq2 FactoMineR factoextra
#' @param dataPath the direction of download Data
#' @param log2FC Log2 Fold change threshold for the DEGs
#' @param pvalue P Vlaue threshold for the DEGs
#' @export
DEGsVisualization<-function(dataPath,log2FC,pvalue){

  message("绘制火山图...", appendLF = F)

  load(file = ".//output//step05.RData")
  TCGA_result <-
    result[result$pvalue < pvalue &
             abs(result$log2FoldChange) > log2FC, ]
  pdf(file = ".//output//step06_volcanon.pdf")
  plot(
    result$log2FoldChange,
    -log10(result$pvalue),
    xlab = "Log2FoldChange",
    ylab = "-Log10(Pvalue)",
    pch = 16
  )
  points(
    TCGA_result$log2FoldChange,
    -log10(TCGA_result$pvalue),
    col = ifelse(TCGA_result$log2FoldChange > 0, "red", "green"),
    pch = 16
  )
  dev.off()
  message("Completed!")

  message("绘制基因表达热图...", appendLF = F)
  TCGA_result <-
    result[result$pvalue < pvalue &
             abs(result$log2FoldChange) > log2FC, ]
  heat_x <-
    subset(x_reduce, rownames(x_reduce) %in% rownames(TCGA_result))#从原始表达数据中提取差异表达基因
  annotation_col = data.frame(Sample = group)#定义样本属性
  rownames(annotation_col) = colnames(heat_x)#定义注释的行名称
  pdf(file = ".//output//step06_heatmap.pdf")
  pheatmap::pheatmap(
    heat_x,
    annotation_col = annotation_col,
    scale = 'row',
    show_rownames = F,
    color = colorRampPalette(c('green', 'black', 'red'))(50),
    clustering_distance_cols = 'manhattan',
    show_colnames = F,
    annotation_colors = list(Sample = c(cancer = 'red', normal = 'green'))
  )
  dev.off()
  save(result, TCGA_result, heat_x, file = ".//output//step06.RData")
  message("Completed!")
  #table(ifelse(TCGA_result$log2FoldChange>0,"up","down"))

  message("绘制基因表达MA图...", appendLF = F)
  tT <- subset(result, select = c("baseMean", "log2FoldChange", "pvalue"))
  pdf(file = ".//output//step06_MA.pdf")
  plot(
    x = log2(tT$baseMean),
    tT$log2FoldChange,
    pch = 16,
    cex = 0.5,
    xlab = 'Average Expression',
    ylab = 'Fold change'
  )
  tT_x <- tT[which(abs(tT$log2FoldChange) > log2FC), ]
  tT_x <- tT_x[which(tT_x$pvalue < pvalue), ]
  points(
    log2(tT_x$baseMean),
    tT_x$log2FoldChange,
    pch = 16,
    cex = 0.5,
    col = ifelse(tT_x$log2FoldChange > 0, "red", "blue")
  )
  legend(
    list(x = floor(max(log2(tT_x$baseMean))), y = floor(max(tT_x$log2FoldChange))),
    legend = c("up", 'normal', 'down'),
    col = c('red', 'black', 'blue'),
    pch = 20,
    bty = "n"
  )
  dev.off()

  message("Completed!")
  
  message("绘制样本PCA图...", appendLF = F)
  pdf(file = ".//output//step06_PCA.pdf")
  res.pca=FactoMineR::PCA(t(log2(heat_x+1)),graph = F)
  p=factoextra::fviz_pca_ind(
    res.pca,
    geom.ind="point",
    col.ind=group,
    palette=c('#00AFBB','#E7B800'),
    addEllipses=T,
    legend.title="Groups",
    title="PCA of DEGs"
  )
  print(p)
  dev.off()
  
  message("Completed!")
}
dming1024/TCGApackages0226 documentation built on April 9, 2021, 7:48 a.m.