comparison FCSMergeDownsample.R @ 0:8e568997abda draft

"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/fcs_merge_downsample commit 2fe0269eaff92916ca51729a7ca8d2017f65f89f"
author azomics
date Mon, 22 Jun 2020 20:35:09 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8e568997abda
1 #!/usr/bin/Rscript
2 # Aggregate FCS files Module for Galaxy
3 # with FlowSOM AggregateFlowFrames
4 ######################################################################
5 # Copyright (c) 2017 Northrop Grumman.
6 # All rights reserved.
7 ######################################################################
8 #
9 # Version 1
10 # Cristel Thomas
11 #
12 #
13 library(FlowSOM)
14 library(flowCore)
15
16 downsampleMergeFCS <- function(fcs_files, nb_cells, output="", flag_fcs=FALSE) {
17 ff <- AggregateFlowFrames(fcs_files, nb_cells, writeOutput = FALSE)
18 n <- length(colnames(ff)) - 2
19 exprs(ff) <- exprs(ff)[,1:n]
20 if (flag_fcs) {
21 write.FCS(ff, output)
22 } else {
23 saveRDS(ff, file=output)
24 }
25 }
26
27 checkFCSfiles <- function(fcsfiles, ds_factor=0.1, out_file ="",
28 flag_fcs=FALSE) {
29 isValid <- F
30 nb_events <- 0
31 markerCheck <- T
32
33 for (i in 1:length(fcsfiles)){
34 is_file_valid <- F
35 tryCatch({
36 fcs <- read.FCS(fcsfiles[i], transformation=FALSE)
37 is_file_valid <- T
38 nb_events <- nb_events + as.numeric(fcs@description$`$TOT`)
39 }, error = function(ex) {
40 print(paste(ex))
41 })
42 if (is_file_valid){
43 if (i == 1) {
44 m1 <- as.vector(pData(parameters(fcs))$desc)
45 } else {
46 m2 <- as.vector(pData(parameters(fcs))$desc)
47 if (is.na(all(m1==m2))) {
48 mm1 <- is.na(m1)
49 mm2 <- is.na(m2)
50 if (all(mm1==mm2)){
51 if (!all(m1==m2, na.rm=TRUE)){
52 markerCheck <- F
53 }
54 } else {
55 markerCheck <- F
56 }
57 } else if (!all(m1==m2)) {
58 markerCheck <- F
59 }
60 }
61 } else {
62 quit(save = "no", status = 10, runLast = FALSE)
63 }
64 }
65
66 if (markerCheck) {
67 isValid <- T
68 } else {
69 quit(save = "no", status = 12, runLast = FALSE)
70 }
71
72 ## translate ds_factor to nb of events
73 nb_cell <- floor(ds_factor*nb_events)
74
75 if (isValid) {
76 downsampleMergeFCS(fcsfiles, nb_cell, out_file, flag_fcs)
77 } else {
78 quit(save = "no", status = 10, runLast = FALSE)
79 }
80 }
81
82 args <- commandArgs(trailingOnly = TRUE)
83 flag_fcs <- FALSE
84
85 if (args[2] == "FCS"){
86 flag_fcs <- TRUE
87 }
88
89 if (args[3] == "" || args[3] == "i.e.:0.1 or 10X") {
90 factor <- 0.1
91 } else {
92 #rm last X if it's there
93 ds <- gsub("X", "", args[3])
94 if (!is.na(as.numeric(ds))) {
95 factor <- as.numeric(ds)
96 if (factor > 1 && factor <= 100) {
97 factor <- as.numeric(ds) / 100
98 } else if (factor > 100){
99 quit(save = "no", status = 11, runLast = FALSE)
100 }
101 } else {
102 quit(save = "no", status = 11, runLast = FALSE)
103 }
104 }
105
106 nb_files <- (length(args) - 3)
107 fcsfiles <- character(nb_files)
108 j <- 1
109 ## get files and file names
110 for (i in 4:length(args)) {
111 fcsfiles[[j]] <- args[i]
112 j <- j + 1
113 }
114
115 checkFCSfiles(fcsfiles, factor, args[1], flag_fcs)