changeset 0:02b2412598b6 draft default tip

"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/edit_fcs_marker commit 05dd0e3c6e8eff9383d3f755ade2ca8557ebe7e7"
author azomics
date Mon, 22 Jun 2020 20:07:30 -0400
parents
children
files editFCSmarkers.R editFCSmarkers.xml test-data/input.fcs test-data/output.fcs test-data/output.flowframe test-data/report1.txt test-data/report2.txt
diffstat 7 files changed, 332 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/editFCSmarkers.R	Mon Jun 22 20:07:30 2020 -0400
@@ -0,0 +1,131 @@
+#!/usr/bin/Rscript
+# modify channels and marker names in FCS
+#
+######################################################################
+#                  Copyright (c) 2017 Northrop Grumman.
+#                          All rights reserved.
+######################################################################
+#
+# Cristel Thomas
+# Version 2 - May 2018
+# Modified to take in marker/channel names by name rather than index
+#
+library(flowCore)
+
+checkCandM <- function(m_set, channels=vector(), markers=vector()){
+  if (m_set[[3]]){
+    in_file <- m_set[[1]] %in% channels
+  } else {
+    in_file <- m_set[[1]] %in% markers
+  }
+  if (sum(in_file)==0) {
+    warning("Given original columns are either not in the channels or in the markers of the read object. Will fail.")
+    return(FALSE)
+  }
+  return(TRUE)
+}
+
+modifyMarkersFCS <- function(input, output="", report="", flag_fcs=F,
+                             marker_sets=list()) {
+
+  fcs <- read.FCS(input, transformation=F)
+  original_channels <- colnames(fcs)
+  original_markers <- as.vector(pData(parameters(fcs))$desc)
+  nb <- length(original_channels)
+  ## check if markers are in FCS files
+  check_markers <- sapply(marker_sets, checkCandM, channels=original_channels,
+                          markers=original_markers)
+  if (sum(check_markers)==0) {
+    quit(save = "no", status = 13, runLast = FALSE)
+  }
+
+  post_channels <- colnames(fcs)
+  post_markers <- as.vector(pData(parameters(fcs))$desc)
+
+  for (m_set in marker_sets) {
+    if (m_set[[3]]){
+      chan_to_replace <- post_channels %in% m_set[[1]]
+      for (i in 1:nb){
+        if (chan_to_replace[[i]]){
+          post_channels[[i]] <- m_set[[2]][[match(post_channels[[i]], m_set[[1]])[1]]]
+        }
+      }
+    } else {
+      marker_to_replace <- post_markers %in% m_set[[1]]
+      for (i in 1:nb){
+        if (marker_to_replace[[i]]){
+          post_markers[[i]] <- m_set[[2]][[match(post_markers[[i]], m_set[[1]])[1]]]
+          pm <- paste("$P", as.character(i), "S", sep="")
+          fcs@description[[pm]] <- post_markers[[i]]
+        }
+      }
+    }
+  }
+
+  colnames(fcs) <- post_channels
+  pData(parameters(fcs))$desc <- post_markers
+
+  # write report
+  sink(report)
+  cat("###########################\n")
+  cat("##    BEFORE RENAMING    ##\n")
+  cat("###########################\nFCS Channels\n")
+  cat("---------------------------\n")
+  cat(original_channels,"---------------------------", "FCS Markers","---------------------------",original_markers, sep="\n")
+  cat("\n###########################\n")
+  cat("##    AFTER  RENAMING    ##\n")
+  cat("###########################\nFCS Channels\n")
+  cat("---------------------------\n")
+  cat(post_channels,"---------------------------","FCS Markers","---------------------------", post_markers, sep="\n")
+  sink()
+
+  # output fcs
+  if (flag_fcs) {
+    write.FCS(fcs, output)
+  } else {
+    saveRDS(fcs, file = output)
+  }
+}
+
+checkFCS <- function(fcsfile, out_file ="", report="", flag_fcs=FALSE,
+                     marker_sets=list()) {
+  isValid <- F
+  tryCatch({
+    isValid <- isFCSfile(fcsfile)
+  }, error = function(ex) {
+    print(paste(ex))
+  })
+
+  if (isValid) {
+    modifyMarkersFCS(fcsfile, out_file, report, flag_fcs, marker_sets)
+  } else {
+    quit(save = "no", status = 10, runLast = FALSE)
+  }
+}
+
+################################################################################
+################################################################################
+args <- commandArgs(trailingOnly = TRUE)
+flag_fcs <- if (args[3]=="FCS") TRUE else FALSE
+
+items <- args[5:length(args)]
+marker_sets <- list()
+j <- 1
+for (i in seq(1, length(items), 3)) {
+  if (items[i]=="None" || items[i]== "" || items[i]== "i.e.:TLR 6, TLR6PE") {
+    quit(save = "no", status = 11, runLast = FALSE)
+  }
+  if (items[i+1]=="None" || items[i+1]=="" || items[i+1]=="i.e.:TLR6") {
+    quit(save = "no", status = 12, runLast = FALSE)
+  }
+
+  old_names <- strsplit(items[i], ",")[[1]]
+  to_replace <- sapply(old_names, trimws)
+  replacement <- sapply(strsplit(items[i+1], ",")[[1]], trimws)
+  flag_channel <- if (items[i+2]=="C") TRUE else FALSE
+  m_set <- list(to_replace, replacement, flag_channel)
+  marker_sets[[j]] <- m_set
+  j <- j + 1
+}
+
+checkFCS(args[1], args[2], args[4], flag_fcs, marker_sets)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/editFCSmarkers.xml	Mon Jun 22 20:07:30 2020 -0400
@@ -0,0 +1,119 @@
+<tool id="edit_fcs_markers" name="Edit markers or channels" version="1.0">
+  <description>in FCS files</description>
+  <requirements>
+    <requirement type="package" version="1.42.0">bioconductor-flowcore</requirement>
+  </requirements>
+  <stdio>
+    <exit_code range="10" level="fatal" description="Please provide a valid FCS file." />
+    <exit_code range="11" level="fatal" description="Please provide a comma separated list of integers for markers/channels you want to rename." />
+    <exit_code range="12" level="fatal" description="Please provide a comma separated list of names for markers/channels you want to rename." />
+    <exit_code range="13" level="fatal" description="List of marker indices and marker names must match. For instance, indices 1,3,4 and Marker4,Marker6,Marker8." />
+    <exit_code range="14" level="fatal" description="Provided indices are out of range in the FCS file given as input." />
+  </stdio>
+  <command><![CDATA[
+    Rscript --slave --vanilla '$__tool_directory__/editFCSmarkers.R' '${input}' '${output_file}' '${outformat}' '${report}' '${columns}' '${colnames}' '${corm}'
+  ]]>
+  </command>
+  <inputs>
+    <param format="fcs" name="input" type="data" label="Text file"/>
+    <param name="columns" type="text" label="Channels or markers to modify" help="These are comma separated (ie.: 'ColX,ColY'). The column names provided need to exist in the flowFrame / FCS file provided as input. If you're not sure, check your markers with the list of markers tool in the FCS File Tools section."/>
+    <param name="colnames" type="text" label="New column headings" help="New names for the column headers, a comma separated list of the same lenght as 'Channels or markers to modify' (i.e.: 'Marker1,Marker6,Marker4'). Check below for more details.">
+    </param>
+    <param name="corm" type="select" label="Modify Channels or Markers?" help="If you are not sure which to modify, check with the list of marker tools in the FCS File Tools section.">
+      <option value="M">Markers</option>
+      <option value="C">Channels</option>
+    </param>
+    <param name="outformat" type="select" label="Output Format">
+      <option value="flowFrame">R Data, flowFrame</option>
+      <option value="FCS">FCS 3.0</option>
+    </param>
+</inputs>
+  <outputs>
+    <data format="txt" name="report" label="Editing Report of FCS markers from ${input.name}"/>
+    <data format="flowframe" name="output_file" label="Edited FCS from ${input.name} in ${outformat}">
+      <change_format>
+        <when input="outformat" value="FCS" format="fcs" />
+      </change_format>
+    </data>
+  </outputs>
+  <tests>
+    <test>
+      <param name="input" value="input.fcs"/>
+      <param name="columns" value="Forward Scatter,Side Scatter,APC CD45RA"/>
+      <param name="colnames" value="m1,m2,m3"/>
+      <param name="corm" value="M"/>
+      <param name="outformat" value="flowFrame"/>
+      <output name="report" value="report1.txt"/>
+      <output name="output_file" file="output.flowframe" compare="sim_size" delta="500000"/>
+    </test>
+    <test>
+      <param name="input" value="input.fcs"/>
+      <param name="columns" value="FSC-H,SSC-H,FL4-H"/>
+      <param name="colnames" value="m1,m2,m3"/>
+      <param name="corm" value="C"/>
+      <param name="outformat" value="FCS"/>
+      <output name="report" value="report2.txt"/>
+      <output name="output_file" file="output.fcs" compare="sim_size" delta="500000"/>
+    </test>
+  </tests>
+  <help><![CDATA[
+   This tool enables editing FCS markers or channels.
+
+-----
+
+**Input files**
+
+This tool requires FCS files as input.
+
+**Column names**
+
+Please indicate the names of the markers / channels to edit (comma-separated list).
+
+.. class:: infomark
+
+Tip: One of the tools in the FCS File Tools section can help check the markers and channels indices:
+
+- Get list of markers and channels in FCS files.
+
+**Column names**
+
+Please indicate the new marker or channel names in the order in which they should appear in the output file (comma-separated list). The number of names should match the number of indices.
+
+**Output file**
+
+The output contains the input data with new marker or channel names. Output can be provided in FCS format or in a RData object containing a flowFrame.
+
+-----
+
+**Examples**
+
+**Input file**::
+
+   Marker1 Marker2 Marker3 Marker4 Marker5
+   4       45      123     1956    62534
+   3       65      104     1254    36576
+   7       26      767     4124    42235
+   4       56      323     7623    74634
+   5       83      532     6256    34763
+   4       15      877     9312    21265
+   ...     ...     ...     ...     ...
+
+*Example 1*
+
+- Indices: Marker1,Marker3,Marker5
+- Column names: Marker7,Marker8,Marker9
+
+*Output1*::
+
+   Marker7 Marker2 Marker8 Marker4 Marker9
+   4       45      123     1956    62534
+   3       65      104     1254    36576
+   7       26      767     4124    42235
+   4       56      323     7623    74634
+   5       83      532     6256    34763
+   4       15      877     9312    21265
+   ...     ...     ...     ...     ...
+
+  ]]>
+  </help>
+</tool>
Binary file test-data/input.fcs has changed
Binary file test-data/output.fcs has changed
Binary file test-data/output.flowframe has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/report1.txt	Mon Jun 22 20:07:30 2020 -0400
@@ -0,0 +1,41 @@
+###########################
+##    BEFORE RENAMING    ##
+###########################
+FCS Channels
+---------------------------
+FSC-H
+SSC-H
+FL1-H
+FL2-H
+FL3-H
+FL4-H
+---------------------------
+FCS Markers
+---------------------------
+Forward Scatter
+Side Scatter
+FITC CD4
+PE CD25
+PP CD3
+APC CD45RA
+
+###########################
+##    AFTER  RENAMING    ##
+###########################
+FCS Channels
+---------------------------
+FSC-H
+SSC-H
+FL1-H
+FL2-H
+FL3-H
+FL4-H
+---------------------------
+FCS Markers
+---------------------------
+m1
+m2
+FITC CD4
+PE CD25
+PP CD3
+m3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/report2.txt	Mon Jun 22 20:07:30 2020 -0400
@@ -0,0 +1,41 @@
+###########################
+##    BEFORE RENAMING    ##
+###########################
+FCS Channels
+---------------------------
+FSC-H
+SSC-H
+FL1-H
+FL2-H
+FL3-H
+FL4-H
+---------------------------
+FCS Markers
+---------------------------
+Forward Scatter
+Side Scatter
+FITC CD4
+PE CD25
+PP CD3
+APC CD45RA
+
+###########################
+##    AFTER  RENAMING    ##
+###########################
+FCS Channels
+---------------------------
+m1
+m2
+FL1-H
+FL2-H
+FL3-H
+m3
+---------------------------
+FCS Markers
+---------------------------
+Forward Scatter
+Side Scatter
+FITC CD4
+PE CD25
+PP CD3
+APC CD45RA