Mercurial > repos > immport-devteam > fcs_scatterplot
comparison fcs_scatterplot/getDensityPlots.R @ 0:164cd5384f1d draft
Uploaded
author | immport-devteam |
---|---|
date | Mon, 27 Feb 2017 12:52:20 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:164cd5384f1d |
---|---|
1 # Density Plot Module for Galaxy | |
2 # FlowDensity | |
3 ###################################################################### | |
4 # Copyright (c) 2016 Northrop Grumman. | |
5 # All rights reserved. | |
6 ###################################################################### | |
7 # | |
8 # Version 1 | |
9 # Cristel Thomas | |
10 # | |
11 # | |
12 | |
13 library(flowCore) | |
14 library(flowDensity) | |
15 | |
16 generateGraph <- function(input, channels, output, plot_default, flag_pdf, pdf_out) { | |
17 fcs <- read.FCS(input, transformation=F) | |
18 ## marker names | |
19 markers <- colnames(fcs) | |
20 print_markers <- as.vector(pData(parameters(fcs))$desc) | |
21 # Update print_markers if the $P?S not in the FCS file | |
22 for (i in 1:length(print_markers)) { | |
23 if (is.na(print_markers[i])) { | |
24 print_markers[i] <- markers[i] | |
25 } | |
26 } | |
27 | |
28 if (plot_default) { | |
29 channels <- c(grep(colnames(fcs), pattern="Forward scatter", ignore.case=TRUE), | |
30 grep(colnames(fcs), pattern="Side scatter", ignore.case=TRUE)) | |
31 if (length(channels) == 0){ | |
32 channels <- c(grep(colnames(fcs), pattern="FSC"), | |
33 grep(colnames(fcs), pattern="SSC")) | |
34 if (length(channels) > 2) { | |
35 #get first FSC and corresponding SSC | |
36 channels <- c(grep(colnames(fcs), pattern="FSC-A"), | |
37 grep(colnames(fcs), pattern="SSC-A")) | |
38 if (length(channels) == 0) { | |
39 channels <- c(grep(colnames(fcs), pattern="FSC-H"), | |
40 grep(colnames(fcs), pattern="SSC-H")) | |
41 if (length(channels) == 0) { | |
42 channels <- c(grep(colnames(fcs), pattern="FSC-W"), | |
43 grep(colnames(fcs), pattern="SSC-W")) | |
44 } | |
45 } | |
46 } | |
47 } | |
48 if (length(channels) == 0) { | |
49 warning('No forward/side scatter channels found, no plots will be generated.') | |
50 quit(save = "no", status = 10, runLast = FALSE) | |
51 } | |
52 } | |
53 | |
54 nb_markers <- length(channels) | |
55 for (j in nb_markers) { | |
56 if (channels[j] > length(markers)){ | |
57 warning('Please indicate markers between 1 and ', length(markers)) | |
58 quit(save = "no", status = 10, runLast = FALSE) | |
59 } | |
60 } | |
61 png(output, type="cairo", height=600, width=600) | |
62 par(mfrow=c(2,2)) | |
63 for (m in 1:(nb_markers - 1)) { | |
64 for (n in (m+1):nb_markers) { | |
65 plotDens(fcs, c(channels[m],channels[n]), xlab = print_markers[channels[m]], ylab = print_markers[channels[n]]) | |
66 } | |
67 } | |
68 dev.off() | |
69 | |
70 if (flag_pdf) { | |
71 pdf(pdf_out, useDingbats=FALSE, onefile=TRUE) | |
72 par(mfrow=c(2,2)) | |
73 for (m in 1:(nb_markers - 1)) { | |
74 for (n in (m+1):nb_markers) { | |
75 plotDens(fcs, c(channels[m],channels[n]), xlab = print_markers[channels[m]], ylab = print_markers[channels[n]]) | |
76 } | |
77 } | |
78 dev.off() | |
79 } | |
80 } | |
81 | |
82 checkFCS <- function(input_file, channels, output_file, plot_default, flag_pdf, pdf_out) { | |
83 isValid <- F | |
84 # Check file beginning matches FCS standard | |
85 tryCatch({ | |
86 isValid = isFCSfile(input_file) | |
87 }, error = function(ex) { | |
88 print (paste(" ! Error in isFCSfile", ex)) | |
89 }) | |
90 | |
91 if (isValid) { | |
92 generateGraph(input_file, channels, output_file, plot_default, flag_pdf, pdf_out) | |
93 } else { | |
94 print (paste(input_file, "does not meet FCS standard")) | |
95 } | |
96 } | |
97 | |
98 args <- commandArgs(trailingOnly = TRUE) | |
99 channels <- "" | |
100 flag_default <- FALSE | |
101 flag_pdf <- FALSE | |
102 pdf_output <- "" | |
103 | |
104 if (args[3]=="None") { | |
105 flag_default <- TRUE | |
106 } else { | |
107 if (args[3] == "i.e.:1,3,4"){ | |
108 flag_default <- TRUE | |
109 } else { | |
110 channels <- as.numeric(strsplit(args[3], ",")[[1]]) | |
111 for (channel in channels){ | |
112 if (is.na(channel)){ | |
113 quit(save = "no", status = 11, runLast = FALSE) | |
114 } | |
115 } | |
116 if (length(channels) == 1){ | |
117 warning('Please indicate more than one marker to plot.') | |
118 quit(save = "no", status = 10, runLast = FALSE) | |
119 } | |
120 } | |
121 } | |
122 | |
123 if (args[5] == "TRUE"){ | |
124 pdf_output <- args[6] | |
125 flag_pdf <- TRUE | |
126 } | |
127 | |
128 checkFCS(args[2], channels, args[4], flag_default, flag_pdf, pdf_output) |