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