comparison FCSConvert.R @ 1:99bf034c674d draft default tip

"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/convert_fcs_to_text commit 5ac9759a4f1850b200cd7a301e6662ef8adca55d"
author azomics
date Mon, 22 Jun 2020 17:26:06 -0400
parents
children
comparison
equal deleted inserted replaced
0:8e10184368a0 1:99bf034c674d
1 #!/usr/bin/env Rscript
2 # ImmPort FCSConvert
3 ######################################################################
4 # Copyright (c) 2016 Northrop Grumman.
5 # All rights reserved.
6 ######################################################################
7 #
8 # Converts the FCS file to text without transformaton
9 # To run in R
10 # 1) library(flowCore)
11 # 2) source("FCSConvert.R")
12 # 3) transformFCS("filename")
13 #
14 # Version 1.4.1
15 # March 2016 -- added lines to run directly from command line
16 #
17
18 library(flowCore)
19
20 convertFCS <- function(fcs,compensate=FALSE,debug=FALSE) {
21 # Check file type and FCS version
22 if (class(fcs)[1] != "flowFrame") {
23 print("convertFCS requires flowFrame object as input")
24 return(FALSE)
25 }
26
27 keywords <- keyword(fcs)
28 markers <- colnames(fcs)
29 print_markers <- as.vector(pData(parameters(fcs))$desc)
30 # Update print_markers if the $P?S not in the FCS file
31 for (i in 1:length(print_markers)) {
32 if (is.na(print_markers[i])) {
33 print_markers[i] <- markers[i]
34 }
35 }
36
37 if (debug) {
38 print("****Inside convertFCS")
39 print(paste(" FCS version:", keywords$FCSversion))
40 print(paste(" DATATYPE:", keywords['$DATATYPE']))
41 }
42
43 if (keywords$FCSversion == "2" ||
44 keywords$FCSversion == "3" ||
45 keywords$FCSversion == "3.1" ) {
46 datatype = unlist(keywords['$DATATYPE'])
47 if (datatype == 'F') {
48 # Apply compensation if available and requested
49 spill <- keyword(fcs)$SPILL
50 if (is.null(spill) == FALSE && compensate == TRUE) {
51 if (debug) {
52 print("Attempting compensation")
53 }
54 tryCatch({ fcs = compensate(fcs, spill) },
55 error = function(ex) { str(ex); })
56 }
57 # Process fcs expression data, using transformation
58 # based on category of the marker.
59 fcs_exprs <- exprs(fcs)
60 colnames(fcs_exprs) <- print_markers
61 } else if (datatype == 'I') {
62 fcs_exprs <- exprs(fcs)
63 colnames(fcs_exprs) = print_markers
64 } else {
65 print(paste("Data type", datatype, "in FCS 3 is not supported"))
66 fcs_exprs <- FALSE
67 }
68 } else {
69 print(paste("FCS version", keyword(fcs)$FCSversion, "is not supported"))
70 fcs_exprs <- FALSE
71 }
72 fcs_exprs
73 }
74
75
76 #
77 # Starting function for processing a FCS file
78 #
79 processFCSFile <- function(input_file, output_file="",
80 keyword_file="",compensate=FALSE, debug=FALSE) {
81
82 #
83 # Generate the file names for the output_file and keyword_file
84 #
85 pieces <- unlist(strsplit(input_file, .Platform$file.sep))
86 filename <- pieces[length(pieces)]
87
88 if (output_file == "") {
89 filepieces = unlist(strsplit(filename, '\\.'))
90 #replace .fcs with .txt; append .txt if not ending in .fcs
91 if (filepieces[length(filepieces)] == 'fcs') {
92 filepieces[length(filepieces)] <- 'txt'
93 } else {
94 filepieces[length(filepieces)+1] <- 'txt'
95 }
96 output_file <- paste(filepieces, collapse = '.')
97 }
98
99 if (keyword_file == "") {
100 filepieces <- unlist(strsplit(filename, '\\.'))
101 #replace .fcs with .keyword; append .keyword if not ending in .fcs
102 if (filepieces[length(filepieces)] == 'fcs') {
103 filepieces[length(filepieces)] <- 'keyword'
104 } else {
105 filepieces[length(filepieces)+1] <- 'keyword'
106 }
107 keyword_file <- paste(filepieces, collapse = '.')
108 }
109
110 if (debug) {
111 print (paste("Converting file: ",input_file))
112 print (paste("Original file name: ",filename))
113 print (paste("Output file name: ",output_file))
114 print (paste("Keyword file name: ",keyword_file))
115 }
116 fcs <- read.FCS(input_file, transformation=F)
117 keywords <- keyword(fcs)
118 write.table(as.matrix(keywords),file=keyword_file, quote=F,
119 row.names=T, col.names=F, sep='=', append=F)
120
121 transformed_data <- convertFCS(fcs,compensate,debug)
122 write.table(transformed_data, file=output_file, quote=F,
123 row.names=F,col.names=T, sep='\t', append=F)
124 }
125
126 # Convert FCS file without transformation
127 # @param input_file FCS file to be transformed
128 # @param output_file FCS file transformed ".txt" extension
129 # @param keyword_file FCS file keywords ".keywords" extension"
130 # @param compensate Flag indicating whether to apply compensation
131 # matrix if it exists.
132 transformFCS <- function(input_file, output_file="",
133 compensate=FALSE, keyword_file="", debug=FALSE) {
134
135 isValid <- F
136 # Check file beginning matches FCS standard
137 tryCatch({
138 isValid <- isFCSfile(input_file)
139 }, error = function(ex) {
140 print (paste(" ! Error in isFCSfile", ex))
141 })
142
143 if (isValid) {
144 processFCSFile(input_file, output_file, keyword_file, compensate, debug)
145 } else {
146 print (paste(input_file, "does not meet FCS standard"))
147 }
148 }
149
150 args <- commandArgs(trailingOnly = TRUE)
151 transformFCS(args[1], args[2], args[3])