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