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