# HG changeset patch # User azomics # Date 1592870850 14400 # Node ID 02b2412598b6c9efa7db8503a220c2c1730300a6 "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/edit_fcs_marker commit 05dd0e3c6e8eff9383d3f755ade2ca8557ebe7e7" diff -r 000000000000 -r 02b2412598b6 editFCSmarkers.R --- /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) diff -r 000000000000 -r 02b2412598b6 editFCSmarkers.xml --- /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 @@ + + in FCS files + + bioconductor-flowcore + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 000000000000 -r 02b2412598b6 test-data/input.fcs Binary file test-data/input.fcs has changed diff -r 000000000000 -r 02b2412598b6 test-data/output.fcs Binary file test-data/output.fcs has changed diff -r 000000000000 -r 02b2412598b6 test-data/output.flowframe Binary file test-data/output.flowframe has changed diff -r 000000000000 -r 02b2412598b6 test-data/report1.txt --- /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 diff -r 000000000000 -r 02b2412598b6 test-data/report2.txt --- /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