view flowtext_scatterplot/getDensityPlots_text.R @ 0:cc2266d3e611 draft

Uploaded
author immport-devteam
date Mon, 27 Feb 2017 12:58:47 -0500
parents
children
line wrap: on
line source

# Density Plot Module for Galaxy
# ggplot2
######################################################################
#                  Copyright (c) 2016 Northrop Grumman.
#                          All rights reserved.
######################################################################
#
# Version 1
# Cristel Thomas
#
#

library(ggplot2)
library(grid)
# Multiple plot function
# from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

generateGraphFromText <- function(input, channels, output, plot_default, flag_pdf, pdf_out) {
  fcs <- read.table(input, header = TRUE, sep = "\t", check.names = FALSE)
  ## marker names
  markers <- colnames(fcs)

  if (plot_default) {
    channels <- c(grep(colnames(fcs), pattern="Forward scatter", ignore.case=TRUE),
                  grep(colnames(fcs), pattern="Side scatter", ignore.case=TRUE))
  	if (length(channels) == 0){
      channels <- c(grep(colnames(fcs), pattern="FSC"),
                    grep(colnames(fcs), pattern="SSC"))
      if (length(channels) > 2) {
        #get first FSC and corresponding SSC
        channels <- c(grep(colnames(fcs), pattern="FSC-A"),
                      grep(colnames(fcs), pattern="SSC-A"))
        if (length(channels) == 0) {
          channels <- c(grep(colnames(fcs), pattern="FSC-H"),
                        grep(colnames(fcs), pattern="SSC-H"))
          if (length(channels) == 0) {
            channels <- c(grep(colnames(fcs), pattern="FSC-W"),
                          grep(colnames(fcs), pattern="SSC-W"))
          }
        }
      }
    }
    if (length(channels) == 0) {
      warning('No forward/side scatter channels found, no plots will be generated.')
	  quit(save = "no", status = 10, runLast = FALSE)
    }
  }

  nb_markers <- length(channels)

  for (j in nb_markers) {
    if (channels[j] > length(markers)){
  	  warning('Please indicate markers between 1 and ', length(markers))
  	  quit(save = "no", status = 10, runLast = FALSE)
  	}
  }

  plots <- list()
  i <- 0
  for (m in 1:(nb_markers - 1)) {
    for (n in (m+1):nb_markers) {
      x <- fcs[,channels[m]]
      y <- fcs[,channels[n]]
      df <- data.frame(x = x, y = y,
                       d = densCols(x, y, colramp = colorRampPalette(rev(rainbow(10, end = 4/6)))))
      p <- ggplot(df) +
           geom_point(aes(x, y, col = d), size = 0.2) +
           scale_color_identity() +
           theme_bw() +
           labs(x = markers[channels[m]]) +
           labs(y = markers[channels[n]])
      i <- i + 1
      plots[[i]] <- p
    }
  }
  png(output, type="cairo", width=800, height=800)
      multiplot(plotlist = plots, cols = 2)
    dev.off()
  if (flag_pdf){
    pdf(pdf_out, useDingbats=FALSE, onefile=TRUE)
      multiplot(plotlist = plots, cols = 2)
    dev.off()
  }
}

args <- commandArgs(trailingOnly = TRUE)
channels <- ""
flag_default <- FALSE
flag_pdf <- FALSE
pdf_output <- ""

if (args[3]=="None") {
  flag_default <- TRUE
} else {
  if (args[3] == "i.e.:1,3,4"){
  	flag_default <- TRUE
  } else {
    channels <- as.numeric(strsplit(args[3], ",")[[1]])
    for (channel in channels){
	  if (is.na(channel)){
	    quit(save = "no", status = 11, runLast = FALSE)
	  }
    }
	if (length(channels) == 1){
	  warning('Please indicate more than one marker to plot.')
	  quit(save = "no", status = 10, runLast = FALSE)
	}
  }
}

if (args[5] == "TRUE"){
  pdf_output <- args[6]
  flag_pdf <- TRUE
}
generateGraphFromText(args[2], channels, args[4], flag_default, flag_pdf, pdf_output)