annotate chartskit.r @ 21:ebd09eca8580 draft

Uploaded
author guerler
date Tue, 01 Apr 2014 20:18:19 -0400
parents b4722f9d496f
children 4aeb334de0e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
b4722f9d496f Uploaded
guerler
parents:
diff changeset
1 #!/usr/bin/Rscript
b4722f9d496f Uploaded
guerler
parents:
diff changeset
2
b4722f9d496f Uploaded
guerler
parents:
diff changeset
3 # convert multi parameter string (i.e. key1: value, key2: value, ...) to object
b4722f9d496f Uploaded
guerler
parents:
diff changeset
4 split <- function(argument){
b4722f9d496f Uploaded
guerler
parents:
diff changeset
5 # process parameter string
b4722f9d496f Uploaded
guerler
parents:
diff changeset
6 options <- list()
b4722f9d496f Uploaded
guerler
parents:
diff changeset
7 list <- gsub("\\s","", argument)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
8 list <- strsplit(list, ",")
b4722f9d496f Uploaded
guerler
parents:
diff changeset
9 if (length(list) > 0) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
10 list <- list[[1]]
b4722f9d496f Uploaded
guerler
parents:
diff changeset
11 for (entry in list) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
12 pair <- strsplit(entry, ":")
b4722f9d496f Uploaded
guerler
parents:
diff changeset
13 if (length(pair) > 0) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
14 pair <- pair[[1]]
b4722f9d496f Uploaded
guerler
parents:
diff changeset
15 if (length(pair) == 2) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
16 options[[pair[1]]] <- pair[2]
b4722f9d496f Uploaded
guerler
parents:
diff changeset
17 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
18 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
19 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
20 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
21 return(options)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
22 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
23
b4722f9d496f Uploaded
guerler
parents:
diff changeset
24 # load package
b4722f9d496f Uploaded
guerler
parents:
diff changeset
25 if('getopt' %in% rownames(installed.packages()) == FALSE) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
26 install.packages('getopt', repos='http://cran.us.r-project.org')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
27 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
28 library(getopt);
b4722f9d496f Uploaded
guerler
parents:
diff changeset
29
b4722f9d496f Uploaded
guerler
parents:
diff changeset
30 # get options, using the spec as defined by the enclosed list.
b4722f9d496f Uploaded
guerler
parents:
diff changeset
31 spec = matrix(c(
b4722f9d496f Uploaded
guerler
parents:
diff changeset
32 'module', 'm', 1, 'character', 'Module name',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
33 'input', 'i', 1, 'character', 'Input tabular file',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
34 'columns', 'c', 1, 'character', 'Columns string',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
35 'settings', 's', 1, 'character', 'Settings string',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
36 'output', 'o', 1, 'character', 'Output tabular file',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
37 'help', 'h', 0, '', 'Help',
b4722f9d496f Uploaded
guerler
parents:
diff changeset
38 'verbose', 'v', 0, '', 'Verbose'
b4722f9d496f Uploaded
guerler
parents:
diff changeset
39 ), byrow=TRUE, ncol=5);
b4722f9d496f Uploaded
guerler
parents:
diff changeset
40 opt = getopt(spec);
b4722f9d496f Uploaded
guerler
parents:
diff changeset
41
b4722f9d496f Uploaded
guerler
parents:
diff changeset
42 # show help
b4722f9d496f Uploaded
guerler
parents:
diff changeset
43 if ( !is.null(opt$help) || is.null(opt$module) || is.null(opt$input) || is.null(opt$columns) || is.null(opt$output)) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
44 cat(getopt(spec, usage=TRUE));
b4722f9d496f Uploaded
guerler
parents:
diff changeset
45 q(status=1);
b4722f9d496f Uploaded
guerler
parents:
diff changeset
46 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
47
b4722f9d496f Uploaded
guerler
parents:
diff changeset
48 # read columns/settings
b4722f9d496f Uploaded
guerler
parents:
diff changeset
49 columns = split(opt$columns)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
50 settings = split(opt$settings)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
51
b4722f9d496f Uploaded
guerler
parents:
diff changeset
52 # read table
b4722f9d496f Uploaded
guerler
parents:
diff changeset
53 table <- read.table(opt$input)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
54
b4722f9d496f Uploaded
guerler
parents:
diff changeset
55 # source module
b4722f9d496f Uploaded
guerler
parents:
diff changeset
56 source(opt$module)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
57
b4722f9d496f Uploaded
guerler
parents:
diff changeset
58 # run module
b4722f9d496f Uploaded
guerler
parents:
diff changeset
59 l = wrapper (table, columns, settings)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
60
b4722f9d496f Uploaded
guerler
parents:
diff changeset
61 # fill gaps
b4722f9d496f Uploaded
guerler
parents:
diff changeset
62 if (length(l) > 0) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
63 n <- max(sapply(l, length))
b4722f9d496f Uploaded
guerler
parents:
diff changeset
64 ll <- lapply(l, function(X) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
65 c(as.character(X), rep("", times = n - length(X)))
b4722f9d496f Uploaded
guerler
parents:
diff changeset
66 })
b4722f9d496f Uploaded
guerler
parents:
diff changeset
67 out <- do.call(cbind, ll)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
68
b4722f9d496f Uploaded
guerler
parents:
diff changeset
69 # print details
b4722f9d496f Uploaded
guerler
parents:
diff changeset
70 if (!is.null(opt$verbose)) {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
71 print ('Columns:')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
72 print (columns)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
73 print ('Settings:')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
74 print (settings)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
75 print ('Result:')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
76 print (out)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
77 }
b4722f9d496f Uploaded
guerler
parents:
diff changeset
78
b4722f9d496f Uploaded
guerler
parents:
diff changeset
79 # write table
b4722f9d496f Uploaded
guerler
parents:
diff changeset
80 write.table(out, file=opt$output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
81 } else {
b4722f9d496f Uploaded
guerler
parents:
diff changeset
82 print ('Columns:')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
83 print (columns)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
84 print ('Settings:')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
85 print (settings)
b4722f9d496f Uploaded
guerler
parents:
diff changeset
86 print ('No output generated.')
b4722f9d496f Uploaded
guerler
parents:
diff changeset
87 }