changeset 1:b4722f9d496f draft

Uploaded
author guerler
date Mon, 31 Mar 2014 17:22:09 -0400
parents a6a023d1fa11
children 001c5e3e5517
files chartskit.r
diffstat 1 files changed, 87 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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