changeset 0:8e10184368a0 draft

Uploaded
author immport-devteam
date Mon, 27 Feb 2017 12:45:51 -0500
parents
children 99bf034c674d
files convert_fcs_to_text/FCSConvert.R convert_fcs_to_text/convertFCSToText.xml convert_fcs_to_text/test-data/nocomp.flowtext convert_fcs_to_text/test-data/testfcs1.fcs convert_fcs_to_text/test-data/withcomp.flowtext
diffstat 5 files changed, 230 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_fcs_to_text/FCSConvert.R	Mon Feb 27 12:45:51 2017 -0500
@@ -0,0 +1,150 @@
+# ImmPort FCSConvert
+######################################################################
+#                  Copyright (c) 2016 Northrop Grumman.
+#                          All rights reserved.
+######################################################################
+#
+# Converts the FCS file to text without transformaton
+# To run in R
+# 1) library(flowCore)
+# 2) source("FCSConvert.R")
+# 3) transformFCS("filename")
+#
+# Version 1.4.1
+# March 2016 -- added lines to run directly from command line
+#
+
+library(flowCore)
+
+convertFCS <- function(fcs,compensate=FALSE,debug=FALSE) {
+  # Check file type and FCS version
+  if (class(fcs)[1] != "flowFrame") {
+    print("convertFCS requires flowFrame object as input")
+    return(FALSE)
+  }
+
+  keywords <- keyword(fcs)
+  markers <- colnames(fcs)
+  print_markers <- as.vector(pData(parameters(fcs))$desc)
+  # Update print_markers if the $P?S not in the FCS file
+  for (i in 1:length(print_markers)) {
+    if (is.na(print_markers[i])) {
+      print_markers[i] <- markers[i]
+    }
+  }
+
+  if (debug) {
+    print("****Inside convertFCS")
+    print(paste("    FCS version:", keywords$FCSversion))
+    print(paste("    DATATYPE:", keywords['$DATATYPE']))
+  }
+
+  if (keywords$FCSversion == "2" ||
+      keywords$FCSversion == "3" ||
+      keywords$FCSversion == "3.1" ) {
+      datatype = unlist(keywords['$DATATYPE'])
+    if (datatype == 'F') {
+        # Apply compensation if available and requested
+      spill <- keyword(fcs)$SPILL
+      if (is.null(spill) == FALSE && compensate == TRUE) {
+        if (debug) {
+          print("Attempting compensation")
+        }
+        tryCatch({ fcs = compensate(fcs, spill) },
+                   error = function(ex) { str(ex); })
+      }
+      # Process fcs expression data, using transformation
+      # based on category of the marker.
+      fcs_exprs <- exprs(fcs)
+      colnames(fcs_exprs) <- print_markers
+        } else if (datatype == 'I') {
+          fcs_exprs <- exprs(fcs)
+          colnames(fcs_exprs) = print_markers
+        } else {
+          print(paste("Data type", datatype, "in FCS 3 is not supported"))
+          fcs_exprs <- FALSE
+        }
+  } else {
+    print(paste("FCS version", keyword(fcs)$FCSversion, "is not supported"))
+    fcs_exprs <- FALSE
+  }
+  fcs_exprs
+}
+
+
+#
+# Starting function for processing a FCS file
+#
+processFCSFile <- function(input_file, output_file="",
+                           keyword_file="",compensate=FALSE, debug=FALSE) {
+
+  #
+  # Generate the file names for the output_file and keyword_file
+  #
+  pieces <- unlist(strsplit(input_file, .Platform$file.sep))
+  filename <- pieces[length(pieces)]
+
+  if (output_file == "") {
+    filepieces = unlist(strsplit(filename, '\\.'))
+    #replace .fcs with .txt; append .txt if not ending in .fcs
+    if (filepieces[length(filepieces)] == 'fcs') {
+      filepieces[length(filepieces)] <- 'txt'
+    } else {
+      filepieces[length(filepieces)+1] <- 'txt'
+    }
+    output_file <- paste(filepieces, collapse = '.')
+  }
+
+  if (keyword_file == "") {
+    filepieces <- unlist(strsplit(filename, '\\.'))
+    #replace .fcs with .keyword; append .keyword if not ending in .fcs
+    if (filepieces[length(filepieces)] == 'fcs') {
+      filepieces[length(filepieces)] <- 'keyword'
+    } else {
+      filepieces[length(filepieces)+1] <- 'keyword'
+    }
+    keyword_file <- paste(filepieces, collapse = '.')
+  }
+
+  if (debug) {
+    print (paste("Converting file: ",input_file))
+    print (paste("Original file name: ",filename))
+    print (paste("Output file name: ",output_file))
+    print (paste("Keyword file name: ",keyword_file))
+  }
+  fcs <- read.FCS(input_file, transformation=F)
+  keywords <- keyword(fcs)
+  write.table(as.matrix(keywords),file=keyword_file, quote=F,
+              row.names=T, col.names=F, sep='=', append=F)
+
+  transformed_data <- convertFCS(fcs,compensate,debug)
+  write.table(transformed_data, file=output_file, quote=F,
+              row.names=F,col.names=T, sep='\t', append=F)
+}
+
+# Convert FCS file without transformation
+# @param input_file     FCS file to be transformed
+# @param output_file    FCS file transformed ".txt" extension
+# @param keyword_file   FCS file keywords ".keywords" extension"
+# @param compensate     Flag indicating whether to apply compensation
+#                       matrix if it exists.
+transformFCS <- function(input_file, output_file="",
+                         compensate=FALSE, keyword_file="", debug=FALSE) {
+
+  isValid <- F
+  # Check file beginning matches FCS standard
+  tryCatch({
+    isValid <- isFCSfile(input_file)
+  }, error = function(ex) {
+    print (paste("    ! Error in isFCSfile", ex))
+  })
+
+  if (isValid) {
+    processFCSFile(input_file, output_file, keyword_file, compensate, debug)
+  } else {
+    print (paste(input_file, "does not meet FCS standard"))
+  }
+}
+
+args <- commandArgs(trailingOnly = TRUE)
+transformFCS(args[2], args[3], args[4])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_fcs_to_text/convertFCSToText.xml	Mon Feb 27 12:45:51 2017 -0500
@@ -0,0 +1,58 @@
+<tool id="convert_fcs_to_text" name="Convert FCS to Text" version="1.0">
+  <description>with no transformation.</description>
+  <requirements>
+    <requirement type="package" version="3.3.0">r</requirement>
+    <requirement type="package" version="1.38.2">bioconductor-flowcore</requirement>
+  </requirements>
+  <stdio>
+    <exit_code range="1:" />
+  </stdio>
+  <command><![CDATA[
+      Rscript --slave --vanilla $__tool_directory__/FCSConvert.R --args "${input}" "${output_file}" $compensate
+  ]]>
+  </command>
+  <inputs>
+    <param format="fcs" name="input" type="data" label="FCS file"/>
+    <param name="compensate" type="boolean" checked="false" truevalue="TRUE" falsevalue="FALSE" label="Compensate?">
+    </param>
+  </inputs>
+  <outputs>
+    <data format="flowtext" name="output_file" label="No Transformation ${input.name}"/>
+  </outputs>
+  <tests>
+    <test>
+      <param name="input" value="testfcs1.fcs"/>
+      <param name="compensate" value="FALSE"/>
+      <output name="output_file" file="nocomp.flowtext"/>
+    </test>
+    <test>
+      <param name="input" value="testfcs1.fcs"/>
+      <param name="compensate" value="TRUE"/>
+      <output name="output_file" file="withcomp.flowtext"/>
+    </test>
+  </tests>
+  <help><![CDATA[
+   This tool converts FCS files to text format with no tranformation.
+
+-----
+
+**Input files**
+
+This tool requires valid FCS files as input. Files are processed serially. Applying compensation is an option for FCS files including a compensation matrix.
+
+**Output file**
+
+The output is tab-separated text, containing the fluorescence intensity values for each marker.
+
+-----
+
+**Example**::
+
+   Forward Scatter Side Scatter Marker1 Marker2 Marker3 Marker4
+   449             157          551     129     169     292
+   894             1023	        199     277     320     227
+   262             73           437     69      0       146
+   340             115          509     268     0       74
+  ]]>
+  </help>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_fcs_to_text/test-data/nocomp.flowtext	Mon Feb 27 12:45:51 2017 -0500
@@ -0,0 +1,11 @@
+FSC-A	FSC-H	SSC-A	SSC-H	CD127	CD45RO	LIVE	CD4	CD3	HLA-DR	CD25	CCR4	Time
+9081	8811	19575.009765625	18578	-76	2140.15991210938	278.460021972656	19.1100006103516	3.83999991416931	146.879989624023	125.440002441406	942.760009765625	0
+13879.5	11372	76400.875	66800	-70.6800003051758	167.959991455078	157.43000793457	80.0800018310547	51.8400001525879	83.5199966430664	-4.90000009536743	-11.7600002288818	0
+53197.5	49698	32821.8828125	30290	243.959991455078	1424.23999023438	212.940002441406	66.4300003051758	731.519958496094	822.719970703125	-8.81999969482422	40.1800003051758	0
+94011.75	85861	46558.33203125	45425	-55.4799995422363	178.599990844727	600.600036621094	103.740005493164	546.239990234375	13994.8798828125	42.1399993896484	34.2999992370605	0.100000001490116
+56965.5	51060	42377.79296875	41492	2128.76000976562	12543.0400390625	239.330001831055	9038.1201171875	2529.59985351562	2429.76000976562	620.340026855469	1694.42004394531	0.100000001490116
+102877.5	91646	74486.234375	70382	2954.8798828125	8467.919921875	346.710021972656	276.640014648438	12951.359375	9643.2001953125	353.779998779297	209.720001220703	0.200000002980232
+170482.5	135955	126331.6640625	106115	196.839996337891	4349.47998046875	1876.42004394531	1431.43005371094	919.679992675781	12119.0400390625	265.580017089844	941.780029296875	0.200000002980232
+140555.25	100224	108512.046875	72196	173.279998779297	1068.55993652344	1397.76000976562	447.720001220703	1393.919921875	24694.080078125	210.699996948242	183.260009765625	0.200000002980232
+46518.75	37218	138006.046875	113970	74.4799957275391	1400.67993164062	2420.60009765625	755.300048828125	435.839996337891	7570.56005859375	211.68000793457	242.059997558594	0.200000002980232
+11892.75	11583	10502.310546875	10123	-37.2399978637695	220.399993896484	55.5100021362305	49.1400032043457	-7.67999982833862	7.67999982833862	-12.7399997711182	65.6600036621094	0.300000011920929
Binary file convert_fcs_to_text/test-data/testfcs1.fcs has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_fcs_to_text/test-data/withcomp.flowtext	Mon Feb 27 12:45:51 2017 -0500
@@ -0,0 +1,11 @@
+FSC-A	FSC-H	SSC-A	SSC-H	CD127	CD45RO	LIVE	CD4	CD3	HLA-DR	CD25	CCR4	Time
+9081	8811	19575.009765625	18578	-87.0409478040183	1862.84642436843	271.401077855461	-21.5928234634463	-3.8953788307554	128.962897543941	-105.49762363372	883.862950370797	0
+13879.5	11372	76400.875	66800	-78.6039894593431	181.219255881273	155.837498648	65.4989955303374	49.1162320405613	34.35089684109	-93.9379731423774	-20.2250618932269	0
+53197.5	49698	32821.8828125	30290	230.052594687552	1373.99921235714	197.676575400523	43.2961600914911	719.022620194678	260.803809001924	-123.26357251272	-7.84948514046658	0
+94011.75	85861	46558.33203125	45425	-71.9173894410761	175.966101207251	32.020003628979	59.0097517206453	-16.3262596884212	14004.8608140457	19.4103482134535	26.3251000552498	0.100000001490116
+56965.5	51060	42377.79296875	41492	1141.7755127904	10749.6103892967	198.722094418903	8932.67673912722	2473.54527111587	532.454297443733	422.911926152405	942.872373887256	0.100000001490116
+102877.5	91646	74486.234375	70382	2882.43278670468	7885.97842835489	269.438114676268	171.646872288934	12954.6785962761	-229.3446155909	202.938466043071	-74.2736739022447	0.200000002980232
+170482.5	135955	126331.6640625	106115	36.6180435027011	3913.39806836873	1398.12822721842	1227.24406134117	435.463239428049	11681.3109026932	-612.66079632735	757.567147469633	0.200000002980232
+140555.25	100224	108512.046875	72196	118.194682368212	954.043993675198	406.120956366245	316.018805963466	412.280494784593	24349.6063726745	-40.2882648979737	135.879221006295	0.200000002980232
+46518.75	37218	138006.046875	113970	10.0578231845294	1267.0939533411	2123.37003323943	495.398550791435	125.007577719306	7314.15255846409	-1039.4176032792	173.430016675029	0.200000002980232
+11892.75	11583	10502.310546875	10123	-43.2553273261776	203.396825762157	55.0774261065933	45.3952130843187	-8.67869816746458	10.0850033972805	-49.5506216433673	57.4240755386454	0.300000011920929