0
|
1 # Density Plot Module for Galaxy
|
|
2 # ggplot2
|
|
3 ######################################################################
|
|
4 # Copyright (c) 2016 Northrop Grumman.
|
|
5 # All rights reserved.
|
|
6 ######################################################################
|
|
7 #
|
|
8 # Version 1
|
|
9 # Cristel Thomas
|
|
10 #
|
|
11 #
|
|
12
|
|
13 library(ggplot2)
|
|
14 library(grid)
|
|
15 # Multiple plot function
|
|
16 # from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/
|
|
17 # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
|
|
18 # - cols: Number of columns in layout
|
|
19 # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
|
|
20 #
|
|
21 # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
|
|
22 # then plot 1 will go in the upper left, 2 will go in the upper right, and
|
|
23 # 3 will go all the way across the bottom.
|
|
24 #
|
|
25 multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
|
|
26 # Make a list from the ... arguments and plotlist
|
|
27 plots <- c(list(...), plotlist)
|
|
28 numPlots = length(plots)
|
|
29
|
|
30 # If layout is NULL, then use 'cols' to determine layout
|
|
31 if (is.null(layout)) {
|
|
32 # Make the panel
|
|
33 # ncol: Number of columns of plots
|
|
34 # nrow: Number of rows needed, calculated from # of cols
|
|
35 layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
|
|
36 ncol = cols, nrow = ceiling(numPlots/cols))
|
|
37 }
|
|
38
|
|
39 if (numPlots==1) {
|
|
40 print(plots[[1]])
|
|
41 } else {
|
|
42 # Set up the page
|
|
43 grid.newpage()
|
|
44 pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
|
|
45
|
|
46 # Make each plot, in the correct location
|
|
47 for (i in 1:numPlots) {
|
|
48 # Get the i,j matrix positions of the regions that contain this subplot
|
|
49 matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
|
|
50 print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
|
|
51 layout.pos.col = matchidx$col))
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55
|
|
56 generateGraphFromText <- function(input, channels, output, plot_default, flag_pdf, pdf_out) {
|
|
57 fcs <- read.table(input, header = TRUE, sep = "\t", check.names = FALSE)
|
|
58 ## marker names
|
|
59 markers <- colnames(fcs)
|
|
60
|
|
61 if (plot_default) {
|
|
62 channels <- c(grep(colnames(fcs), pattern="Forward scatter", ignore.case=TRUE),
|
|
63 grep(colnames(fcs), pattern="Side scatter", ignore.case=TRUE))
|
|
64 if (length(channels) == 0){
|
|
65 channels <- c(grep(colnames(fcs), pattern="FSC"),
|
|
66 grep(colnames(fcs), pattern="SSC"))
|
|
67 if (length(channels) > 2) {
|
|
68 #get first FSC and corresponding SSC
|
|
69 channels <- c(grep(colnames(fcs), pattern="FSC-A"),
|
|
70 grep(colnames(fcs), pattern="SSC-A"))
|
|
71 if (length(channels) == 0) {
|
|
72 channels <- c(grep(colnames(fcs), pattern="FSC-H"),
|
|
73 grep(colnames(fcs), pattern="SSC-H"))
|
|
74 if (length(channels) == 0) {
|
|
75 channels <- c(grep(colnames(fcs), pattern="FSC-W"),
|
|
76 grep(colnames(fcs), pattern="SSC-W"))
|
|
77 }
|
|
78 }
|
|
79 }
|
|
80 }
|
|
81 if (length(channels) == 0) {
|
|
82 warning('No forward/side scatter channels found, no plots will be generated.')
|
|
83 quit(save = "no", status = 10, runLast = FALSE)
|
|
84 }
|
|
85 }
|
|
86
|
|
87 nb_markers <- length(channels)
|
|
88
|
|
89 for (j in nb_markers) {
|
|
90 if (channels[j] > length(markers)){
|
|
91 warning('Please indicate markers between 1 and ', length(markers))
|
|
92 quit(save = "no", status = 10, runLast = FALSE)
|
|
93 }
|
|
94 }
|
|
95
|
|
96 plots <- list()
|
|
97 i <- 0
|
|
98 for (m in 1:(nb_markers - 1)) {
|
|
99 for (n in (m+1):nb_markers) {
|
|
100 x <- fcs[,channels[m]]
|
|
101 y <- fcs[,channels[n]]
|
|
102 df <- data.frame(x = x, y = y,
|
|
103 d = densCols(x, y, colramp = colorRampPalette(rev(rainbow(10, end = 4/6)))))
|
|
104 p <- ggplot(df) +
|
|
105 geom_point(aes(x, y, col = d), size = 0.2) +
|
|
106 scale_color_identity() +
|
|
107 theme_bw() +
|
|
108 labs(x = markers[channels[m]]) +
|
|
109 labs(y = markers[channels[n]])
|
|
110 i <- i + 1
|
|
111 plots[[i]] <- p
|
|
112 }
|
|
113 }
|
|
114 png(output, type="cairo", width=800, height=800)
|
|
115 multiplot(plotlist = plots, cols = 2)
|
|
116 dev.off()
|
|
117 if (flag_pdf){
|
|
118 pdf(pdf_out, useDingbats=FALSE, onefile=TRUE)
|
|
119 multiplot(plotlist = plots, cols = 2)
|
|
120 dev.off()
|
|
121 }
|
|
122 }
|
|
123
|
|
124 args <- commandArgs(trailingOnly = TRUE)
|
|
125 channels <- ""
|
|
126 flag_default <- FALSE
|
|
127 flag_pdf <- FALSE
|
|
128 pdf_output <- ""
|
|
129
|
|
130 if (args[3]=="None") {
|
|
131 flag_default <- TRUE
|
|
132 } else {
|
|
133 if (args[3] == "i.e.:1,3,4"){
|
|
134 flag_default <- TRUE
|
|
135 } else {
|
|
136 channels <- as.numeric(strsplit(args[3], ",")[[1]])
|
|
137 for (channel in channels){
|
|
138 if (is.na(channel)){
|
|
139 quit(save = "no", status = 11, runLast = FALSE)
|
|
140 }
|
|
141 }
|
|
142 if (length(channels) == 1){
|
|
143 warning('Please indicate more than one marker to plot.')
|
|
144 quit(save = "no", status = 10, runLast = FALSE)
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148
|
|
149 if (args[5] == "TRUE"){
|
|
150 pdf_output <- args[6]
|
|
151 flag_pdf <- TRUE
|
|
152 }
|
|
153 generateGraphFromText(args[2], channels, args[4], flag_default, flag_pdf, pdf_output)
|