# HG changeset patch # User guerler # Date 1396300929 14400 # Node ID b4722f9d496f1a0a2d67db1789f0443ed48b5fba # Parent a6a023d1fa11b4565388ef0e294f358188725cc6 Uploaded diff -r a6a023d1fa11 -r b4722f9d496f chartskit.r --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chartskit.r Mon Mar 31 17:22:09 2014 -0400 @@ -0,0 +1,87 @@ +#!/usr/bin/Rscript + +# convert multi parameter string (i.e. key1: value, key2: value, ...) to object +split <- function(argument){ + # process parameter string + options <- list() + list <- gsub("\\s","", argument) + list <- strsplit(list, ",") + if (length(list) > 0) { + list <- list[[1]] + for (entry in list) { + pair <- strsplit(entry, ":") + if (length(pair) > 0) { + pair <- pair[[1]] + if (length(pair) == 2) { + options[[pair[1]]] <- pair[2] + } + } + } + } + return(options) +} + +# load package +if('getopt' %in% rownames(installed.packages()) == FALSE) { + install.packages('getopt', repos='http://cran.us.r-project.org') +} +library(getopt); + +# get options, using the spec as defined by the enclosed list. +spec = matrix(c( + 'module', 'm', 1, 'character', 'Module name', + 'input', 'i', 1, 'character', 'Input tabular file', + 'columns', 'c', 1, 'character', 'Columns string', + 'settings', 's', 1, 'character', 'Settings string', + 'output', 'o', 1, 'character', 'Output tabular file', + 'help', 'h', 0, '', 'Help', + 'verbose', 'v', 0, '', 'Verbose' +), byrow=TRUE, ncol=5); +opt = getopt(spec); + +# show help +if ( !is.null(opt$help) || is.null(opt$module) || is.null(opt$input) || is.null(opt$columns) || is.null(opt$output)) { + cat(getopt(spec, usage=TRUE)); + q(status=1); +} + +# read columns/settings +columns = split(opt$columns) +settings = split(opt$settings) + +# read table +table <- read.table(opt$input) + +# source module +source(opt$module) + +# run module +l = wrapper (table, columns, settings) + +# fill gaps +if (length(l) > 0) { + n <- max(sapply(l, length)) + ll <- lapply(l, function(X) { + c(as.character(X), rep("", times = n - length(X))) + }) + out <- do.call(cbind, ll) + + # print details + if (!is.null(opt$verbose)) { + print ('Columns:') + print (columns) + print ('Settings:') + print (settings) + print ('Result:') + print (out) + } + + # write table + write.table(out, file=opt$output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t') +} else { + print ('Columns:') + print (columns) + print ('Settings:') + print (settings) + print ('No output generated.') +} \ No newline at end of file