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