view chartskit.r @ 8:9a943eee46f4 draft

Deleted selected files
author guerler
date Tue, 01 Apr 2014 11:48:04 -0400
parents b4722f9d496f
children 4aeb334de0e3
line wrap: on
line source

#!/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.')
}