inst/extcode/eProteinView.R

#' Display the distribution of nucleotide and amino acid.
#'
#' @docType methods
#' @name eProteinView
#' @rdname eProteinView
#'
#' @param blastRes_files paths to blastRes files.
#' @param sampleNames The sample names.
#' @param outdir The output directory.
#'
#' @author Wubing Zhang
#' @import ggplot2
#' @export

eProteinView <- function(blastRes_files, sampleNames, outdir){
  library(ggplot2)
  blastRes = c()
  for(f in blastRes_files){
    tmp = read.table(f, header = TRUE, sep = "\t", stringsAsFactors = FALSE, quote = "")
    tmp$Sample = f
    blastRes = rbind(blastRes, tmp)
  }
  blastRes$Sample = sampleNames
  blastRes$ProteinID = gsub("^.......|_.*", "", blastRes$ID)
  blastRes$ProteinName = gsub(".*_", "", blastRes$ID)

  ## Visualize the results
  gg = blastRes
  idx = duplicated(paste(gg$ProteinID, gg$Sample))
  gg = gg[!idx, ]
  countP = sort(table(gg$ProteinID), decreasing = TRUE)
  for(i in seq(1, length(countP), by = 50)){
    gg2 = gg[gg$ProteinID%in%names(countP)[i:min(i+49, length(countP))], ]
    gg2$ProteinID = factor(gg2$ProteinID, levels = names(countP)[i:min(i+49, length(countP))])
    gg2 = gg2[order(gg2$ProteinID, -gg2$lfc), ]
    tmpX = 1:length(unique(gg2$Sample)); names(tmpX) = sort(unique(gg2$Sample))
    tmpY = 1:length(unique(gg2$ProteinID)); names(tmpY) = sort(unique(gg2$ProteinID))
    gg2$x = tmpX[gg2$Sample]; gg2$y = tmpY[gg2$ProteinID]
    p = ggplot(gg2, aes(x, y, color = lfc))
    p = p + geom_point()
    p = p + scale_x_continuous(breaks = tmpX, labels = names(tmpX))
    p = p + scale_y_continuous(breaks = tmpY, labels = names(tmpY))
    p = p + scale_color_gradient2()
    p = p + labs(x = NULL, y = NULL)
    p = p + theme_bw()
    ggsave(paste0(outdir, "/Enriched_Proteins_", round((i+50)/50), ".png"), p,
           width = 5, height = 8, dpi = 200)
  }
}
WubingZhang/PhageR documentation built on July 2, 2019, 9:03 p.m.