Mercurial > repos > azomics > flowsom_tree
comparison FlowSOMGenerateTree.R @ 2:0efc47dba930 draft default tip
planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/flowsom_tree commit bbff20e20dc2b9dbb40b613a0d5f16ee8132446d
| author | azomics | 
|---|---|
| date | Fri, 29 Sep 2023 07:20:10 +0000 | 
| parents | 54a25f1139b4 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 1:6c1721e7d0d6 | 2:0efc47dba930 | 
|---|---|
| 1 #!/usr/bin/Rscript | 1 #!/usr/bin/env Rscript | 
| 2 # Module for Galaxy | 2 # Module for Galaxy | 
| 3 # Generates FlowSOM reference tree | 3 # Generates FlowSOM reference tree | 
| 4 # with FlowSOM AggregateFlowFrames | 4 # with FlowSOM AggregateFlowFrames | 
| 5 ###################################################################### | 5 ###################################################################### | 
| 6 # Copyright (c) 2017 Northrop Grumman. | 6 # Copyright (c) 2017 Northrop Grumman. | 
| 12 # | 12 # | 
| 13 # | 13 # | 
| 14 library(FlowSOM) | 14 library(FlowSOM) | 
| 15 library(flowCore) | 15 library(flowCore) | 
| 16 | 16 | 
| 17 generateTree <- function(ff, output="", columns=list(), cluster=10, xgrid=10, | 17 generate_tree <- function(ff, output = "", columns = list(), | 
| 18 ygrid=10,plot="", plot_pdf=FALSE, mplot="", flag_def=T, | 18 cluster = 10, xgrid = 10, | 
| 19 table="", mtable="", flag_meta=FALSE, user_seed=42, | 19 ygrid = 10, plot = "", plot_pdf = FALSE, | 
| 20 flag_nodesize=F) { | 20 mplot = "", flag_def = TRUE, | 
| 21 | 21 table = "", mtable = "", | 
| 22 # check default -- if def get all except FSC/SSC | 22 flag_meta = FALSE, user_seed = 42, | 
| 23 # also check nb of markers/channels and indices | 23 flag_nodesize = FALSE) { | 
| 24 | |
| 25 # check default -- if def get all except FSC / SSC | |
| 26 # also check nb of markers / channels and indices | |
| 24 markers <- colnames(ff) | 27 markers <- colnames(ff) | 
| 25 print_markers <- as.vector(pData(parameters(ff))$desc) | 28 print_markers <- as.vector(pData(parameters(ff))$desc) | 
| 26 # Update print_markers if the $P?S not in the FCS file | 29 # Update print_markers if the $P?S not in the FCS file | 
| 27 for (i in 1:length(print_markers)) { | 30 for (i in seq_along(print_markers)) { | 
| 28 if (is.na(print_markers[i])) { | 31 if (is.na(print_markers[i])) { | 
| 29 print_markers[i] <- markers[i] | 32 print_markers[i] <- markers[i] | 
| 30 } | 33 } | 
| 31 } | 34 } | 
| 32 | 35 | 
| 33 if (flag_def){ | 36 if (flag_def) { | 
| 34 channels_to_exclude <- c(grep(markers, pattern="FSC"), | 37 channels_to_exclude <- c(grep(markers, pattern = "FSC"), | 
| 35 grep(markers, pattern="SSC"), | 38 grep(markers, pattern = "SSC"), | 
| 36 grep(markers, pattern="Time")) | 39 grep(markers, pattern = "Time")) | 
| 37 columns <- markers[-channels_to_exclude] | 40 columns <- markers[-channels_to_exclude] | 
| 38 } | 41 } | 
| 39 | 42 | 
| 40 set.seed(user_seed) | 43 set.seed(user_seed) | 
| 41 fs <- ReadInput(ff, compensate=F, transform=F, scale=T) | 44 fs <- ReadInput(ff, compensate = FALSE, transform = FALSE, scale = TRUE) | 
| 42 fs <- BuildSOM(fs, colsToUse = columns, xdim=xgrid, ydim=ygrid) | 45 fs <- BuildSOM(fs, colsToUse = columns, xdim = xgrid, ydim = ygrid) | 
| 43 fst <- BuildMST(fs, tSNE=T) | 46 fst <- BuildMST(fs, tSNE = TRUE) | 
| 44 | 47 | 
| 45 if (!mplot==""){ | 48 if (!mplot == "") { | 
| 46 pdf(mplot, useDingbats=FALSE, onefile=TRUE) | 49 pdf(mplot, useDingbats = FALSE, onefile = TRUE) | 
| 47 for (marker in markers){ | 50 for (marker in markers) { | 
| 48 PlotMarker(fst, marker) | 51 PlotMarker(fst, marker) | 
| 49 } | 52 } | 
| 50 dev.off() | 53 dev.off() | 
| 51 } | 54 } | 
| 52 metaC <- metaClustering_consensus(fst$map$codes, k=cluster, seed=user_seed) | 55 meta_c <- metaClustering_consensus( | 
| 53 | 56 fst$map$codes, | 
| 54 if (!plot==""){ | 57 k = cluster, | 
| 55 if (flag_nodesize){ | 58 seed = user_seed) | 
| 56 fst <- UpdateNodeSize(fst, reset=TRUE) | 59 | 
| 57 fst$MST$size <- fst$MST$size/2 | 60 if (!plot == "") { | 
| 61 if (flag_nodesize) { | |
| 62 fst <- UpdateNodeSize(fst, reset = TRUE) | |
| 63 fst$MST$size <- fst$MST$size / 2 | |
| 58 } | 64 } | 
| 59 if (plot_pdf) { | 65 if (plot_pdf) { | 
| 60 pdf(plot, useDingbats=FALSE) | 66 pdf(plot, useDingbats = FALSE) | 
| 61 PlotStars(fst, backgroundValues = as.factor(metaC)) | 67 PlotStars(fst, backgroundValues = as.factor(meta_c)) | 
| 62 dev.off() | 68 dev.off() | 
| 63 } else { | 69 } else { | 
| 64 png(plot, type="cairo", height=800, width=800) | 70 png(plot, type = "cairo", height = 800, width = 800) | 
| 65 PlotStars(fst, backgroundValues = as.factor(metaC)) | 71 PlotStars(fst, backgroundValues = as.factor(meta_c)) | 
| 66 dev.off() | 72 dev.off() | 
| 67 } | 73 } | 
| 68 } | 74 } | 
| 69 if (!table==""){ | 75 if (!table == "") { | 
| 70 m <- matrix(0,nrow=nrow(ff),ncol=1) | 76 m <- matrix(0, nrow = nrow(ff), ncol = 1) | 
| 71 s <- seq_len(nrow(ff)) | 77 s <- seq_len(nrow(ff)) | 
| 72 if (flag_meta){ | 78 if (flag_meta) { | 
| 73 m[s,] <- metaC[fst$map$mapping[,1]] | 79 m[s, ] <- meta_c[fst$map$mapping[, 1]] | 
| 74 } else { | 80 } else { | 
| 75 m[s,] <- fst$map$mapping[,1] | 81 m[s, ] <- fst$map$mapping[, 1] | 
| 76 } | 82 } | 
| 77 colnames(m) <- "FlowSOM" | 83 colnames(m) <- "FlowSOM" | 
| 78 ff <- cbind2(ff,m) | 84 ff <- cbind2(ff, m) | 
| 79 out <- exprs(ff) | 85 out <- exprs(ff) | 
| 80 print_markers <- append(print_markers, "Population") | 86 print_markers <- append(print_markers, "Population") | 
| 81 colnames(out) <- print_markers | 87 colnames(out) <- print_markers | 
| 82 write.table(out, file=table, quote=F, row.names=F, col.names=T, sep='\t', | 88 write.table(out, file = table, quote = FALSE, | 
| 83 append=F) | 89 row.names = FALSE, col.names = TRUE, sep = "\t", | 
| 84 | 90 append = FALSE) | 
| 85 nb_nodes <- max(fst$map$mapping[,1]) | 91 | 
| 86 mm <- matrix(0, nrow=nb_nodes, ncol=2) | 92 nb_nodes <- max(fst$map$mapping[, 1]) | 
| 93 mm <- matrix(0, nrow = nb_nodes, ncol = 2) | |
| 87 ss <- seq_len(nb_nodes) | 94 ss <- seq_len(nb_nodes) | 
| 88 mm[,1]<- as.character(ss) | 95 mm[, 1] <- as.character(ss) | 
| 89 mm[ss,2]<- as.character(metaC) | 96 mm[ss, 2] <- as.character(meta_c) | 
| 90 colnames(mm) <- c("Node", "Meta-Cluster") | 97 colnames(mm) <- c("Node", "Meta-Cluster") | 
| 91 write.table(mm, file=mtable, quote=F, row.names=F, col.names=T, sep='\t', | 98 write.table(mm, file = mtable, quote = FALSE, | 
| 92 append=F) | 99 row.names = FALSE, col.names = TRUE, sep = "\t", | 
| 93 | 100 append = FALSE) | 
| 94 } | 101 | 
| 95 saveRDS(fst, file = output) | 102 } | 
| 96 } | 103 saveRDS(fst, file = output) | 
| 97 | 104 } | 
| 98 flowFrameOrFCS <- function(input, output="", columns=list(),cluster=10,xgrid=10, | 105 | 
| 99 ygrid=10,plot="",plot_pdf=FALSE, mplot="", default=T, | 106 flow_frame_or_fcs <- function(input, output = "", columns = list(), | 
| 100 table="", mtable="", flag_meta=FALSE, user_seed=42, | 107 cluster = 10, xgrid = 10, | 
| 101 nodesize=FALSE) { | 108 ygrid = 10, plot = "", plot_pdf = FALSE, | 
| 102 isValid <- F | 109 mplot = "", default = TRUE, | 
| 103 is_fcs <- F | 110 table = "", mtable = "", | 
| 104 is_ff <- F | 111 flag_meta = FALSE, user_seed = 42, | 
| 112 nodesize = FALSE) { | |
| 113 is_fcs <- FALSE | |
| 114 is_ff <- FALSE | |
| 105 ff <- "" | 115 ff <- "" | 
| 106 tryCatch({ | 116 tryCatch({ | 
| 107 is_fcs <- isFCSfile(input) | 117 is_fcs <- isFCSfile(input) | 
| 108 }, error = function(ex) { | 118 }, error = function(ex) { | 
| 109 print(paste(ex)) | 119 print(paste(ex)) | 
| 110 }) | 120 }) | 
| 111 | 121 | 
| 112 if (!is_fcs){ | 122 if (!is_fcs) { | 
| 113 tryCatch({ | 123 tryCatch({ | 
| 114 ff <- readRDS(input) | 124 ff <- readRDS(input) | 
| 115 is_ff <- T | 125 is_ff <- TRUE | 
| 116 }, error = function(ex) { | 126 }, error = function(ex) { | 
| 117 print(paste(ex)) | 127 print(paste(ex)) | 
| 118 }) | 128 }) | 
| 119 } else { | 129 } else { | 
| 120 ff <- read.FCS(input, transformation=FALSE) | 130 ff <- read.FCS(input, transformation = FALSE) | 
| 121 } | 131 } | 
| 122 | 132 | 
| 123 if (!is_ff && !is_fcs) { | 133 if (!is_ff && !is_fcs) { | 
| 124 quit(save = "no", status = 10, runLast = FALSE) | 134 quit(save = "no", status = 10, runLast = FALSE) | 
| 125 } else { | 135 } else { | 
| 126 for (cols in columns){ | 136 for (cols in columns) { | 
| 127 if (cols > length(colnames(ff))){ | 137 if (cols > length(colnames(ff))) { | 
| 128 quit(save = "no", status = 12, runLast = FALSE) | 138 quit(save = "no", status = 12, runLast = FALSE) | 
| 129 } | 139 } | 
| 130 } | 140 } | 
| 131 generateTree(ff, output, columns, cluster, xgrid, ygrid, plot, plot_pdf, | 141 generate_tree(ff, output, columns, cluster, xgrid, ygrid, plot, plot_pdf, | 
| 132 mplot, default, table, mtable, flag_meta, user_seed, nodesize) | 142 mplot, default, table, mtable, flag_meta, user_seed, nodesize) | 
| 133 } | 143 } | 
| 134 } | 144 } | 
| 135 | 145 | 
| 136 args <- commandArgs(trailingOnly = TRUE) | 146 args <- commandArgs(trailingOnly = TRUE) | 
| 137 flag_default <- FALSE | 147 flag_default <- FALSE | 
| 138 columns <- list() | 148 columns <- list() | 
| 139 | 149 | 
| 140 if (args[3] == "" || args[3] == "i.e.:1,2,5") { | 150 if (args[3] == "" || args[3] == "i.e.:1,2,5") { | 
| 141 flag_default <- TRUE | 151 flag_default <- TRUE | 
| 142 } else { | 152 } else { | 
| 143 #rm last X if it's there | 153 #rm last X if it's there | 
| 144 columns <- as.numeric(strsplit(args[3], ",")[[1]]) | 154 columns <- as.numeric(strsplit(args[3], ",")[[1]]) | 
| 145 for (col in columns){ | 155 for (col in columns) { | 
| 146 if (is.na(col)){ | 156 if (is.na(col)) { | 
| 147 quit(save = "no", status = 11, runLast = FALSE) | 157 quit(save = "no", status = 11, runLast = FALSE) | 
| 148 } | 158 } | 
| 149 } | 159 } | 
| 150 } | 160 } | 
| 151 | 161 | 
| 152 cluster <- 10 | 162 cluster <- 10 | 
| 153 if (!args[4] == ""){ | 163 if (!args[4] == "") { | 
| 154 if (!is.na(as.integer(args[4]))){ | 164 if (!is.na(as.integer(args[4]))) { | 
| 155 cluster <- as.integer(args[4]) | 165 cluster <- as.integer(args[4]) | 
| 156 } else { | 166 } else { | 
| 157 quit(save = "no", status = 13, runLast = FALSE) | 167 quit(save = "no", status = 13, runLast = FALSE) | 
| 158 } | 168 } | 
| 159 } | 169 } | 
| 160 | 170 | 
| 161 xgrid <- 10 | 171 xgrid <- 10 | 
| 162 if (!args[5] == ""){ | 172 if (!args[5] == "") { | 
| 163 if (!is.na(as.integer(args[5]))){ | 173 if (!is.na(as.integer(args[5]))) { | 
| 164 cluster <- as.integer(args[5]) | 174 cluster <- as.integer(args[5]) | 
| 165 } else { | 175 } else { | 
| 166 quit(save = "no", status = 14, runLast = FALSE) | 176 quit(save = "no", status = 14, runLast = FALSE) | 
| 167 } | 177 } | 
| 168 } | 178 } | 
| 169 | 179 | 
| 170 ygrid <- 10 | 180 ygrid <- 10 | 
| 171 if (!args[6] == ""){ | 181 if (!args[6] == "") { | 
| 172 if (!is.na(as.integer(args[6]))){ | 182 if (!is.na(as.integer(args[6]))) { | 
| 173 cluster <- as.integer(args[6]) | 183 cluster <- as.integer(args[6]) | 
| 174 } else { | 184 } else { | 
| 175 quit(save = "no", status = 14, runLast = FALSE) | 185 quit(save = "no", status = 14, runLast = FALSE) | 
| 176 } | 186 } | 
| 177 } | 187 } | 
| 178 seed <- 42 | 188 seed <- 42 | 
| 179 if (!args[7]==""){ | 189 if (!args[7] == "") { | 
| 180 if (!is.na(as.integer(args[7]))){ | 190 if (!is.na(as.integer(args[7]))) { | 
| 181 seed <- as.integer(args[7]) | 191 seed <- as.integer(args[7]) | 
| 182 } else { | 192 } else { | 
| 183 quit(save = "no", status = 15, runLast = FALSE) | 193 quit(save = "no", status = 15, runLast = FALSE) | 
| 184 } | 194 } | 
| 185 } | 195 } | 
| 186 | 196 | 
| 187 plot <- "" | 197 plot <- "" | 
| 188 mplot <- "" | 198 mplot <- "" | 
| 191 mtable <- "" | 201 mtable <- "" | 
| 192 flag_meta <- FALSE | 202 flag_meta <- FALSE | 
| 193 nodesize <- FALSE | 203 nodesize <- FALSE | 
| 194 nb_args <- length(args) | 204 nb_args <- length(args) | 
| 195 | 205 | 
| 196 if (nb_args==16) { | 206 if (nb_args == 16) { | 
| 197 plot <- args[8] | 207 plot <- args[8] | 
| 198 if (args[9]=='PDF') { | 208 if (args[9] == "PDF") { | 
| 199 plot_pdf <- TRUE | 209 plot_pdf <- TRUE | 
| 200 } | 210 } | 
| 201 nodesize <- args[10] | 211 nodesize <- args[10] | 
| 202 mplot <- args[11] | 212 mplot <- args[11] | 
| 203 table <- args[13] | 213 table <- args[13] | 
| 204 mtable <- args[14] | 214 mtable <- args[14] | 
| 205 if (args[12]=='meta'){ | 215 if (args[12] == "meta") { | 
| 206 flag_meta<-TRUE | 216 flag_meta <- TRUE | 
| 207 } | 217 } | 
| 208 } else if (nb_args==15){ | 218 } else if (nb_args == 15) { | 
| 209 plot <- args[8] | 219 plot <- args[8] | 
| 210 if (args[9]=='PDF') { | 220 if (args[9] == "PDF") { | 
| 211 plot_pdf <- TRUE | 221 plot_pdf <- TRUE | 
| 212 } | 222 } | 
| 213 nodesize <- args[10] | 223 nodesize <- args[10] | 
| 214 table <- args[12] | 224 table <- args[12] | 
| 215 mtable <- args[13] | 225 mtable <- args[13] | 
| 216 if (args[11]=='meta'){ | 226 if (args[11] == "meta") { | 
| 217 flag_meta<-TRUE | 227 flag_meta <- TRUE | 
| 218 } | 228 } | 
| 219 } else if (nb_args==13) { | 229 } else if (nb_args == 13) { | 
| 220 mplot <- args[8] | 230 mplot <- args[8] | 
| 221 table <- args[10] | 231 table <- args[10] | 
| 222 mtable <- args[11] | 232 mtable <- args[11] | 
| 223 if (args[9]=='meta'){ | 233 if (args[9] == "meta") { | 
| 224 flag_meta<-TRUE | 234 flag_meta <- TRUE | 
| 225 } | 235 } | 
| 226 } else if (nb_args==12) { | 236 } else if (nb_args == 12) { | 
| 227 table <- args[9] | 237 table <- args[9] | 
| 228 mtable <- args[10] | 238 mtable <- args[10] | 
| 229 if (args[8]=='meta'){ | 239 if (args[8] == "meta") { | 
| 230 flag_meta<-TRUE | 240 flag_meta <- TRUE | 
| 231 } | 241 } | 
| 232 } else if (nb_args==11) { | 242 } else if (nb_args == 11) { | 
| 233 plot <- args[8] | 243 plot <- args[8] | 
| 234 if (args[9]=='PDF') { | 244 if (args[9] == "PDF") { | 
| 235 plot_pdf <- TRUE | 245 plot_pdf <- TRUE | 
| 236 } | 246 } | 
| 237 nodesize <- args[10] | 247 nodesize <- args[10] | 
| 238 mplot <- args[11] | 248 mplot <- args[11] | 
| 239 } else if (nb_args==10) { | 249 } else if (nb_args == 10) { | 
| 240 plot <- args[8] | 250 plot <- args[8] | 
| 241 if (args[9]=='PDF') { | 251 if (args[9] == "PDF") { | 
| 242 plot_pdf <- TRUE | 252 plot_pdf <- TRUE | 
| 243 } | 253 } | 
| 244 nodesize <- args[10] | 254 nodesize <- args[10] | 
| 245 } else if (nb_args==8){ | 255 } else if (nb_args == 8) { | 
| 246 mplot <- args[8] | 256 mplot <- args[8] | 
| 247 } | 257 } | 
| 248 | 258 | 
| 249 flowFrameOrFCS(args[1], args[2], columns, cluster, xgrid, ygrid, plot, plot_pdf, | 259 flow_frame_or_fcs(args[1], args[2], | 
| 250 mplot, flag_default, table, mtable, flag_meta, seed, nodesize) | 260 columns, cluster, xgrid, ygrid, plot, plot_pdf, | 
| 261 mplot, flag_default, table, mtable, flag_meta, seed, nodesize) | 
