4
|
1 #!/usr/bin/Rscript
|
|
2
|
35
|
3 # load getopt library
|
|
4 library('getopt');
|
4
|
5
|
|
6 # convert multi parameter string (i.e. key1: value, key2: value, ...) to object
|
|
7 split <- function(argument){
|
|
8 # process parameter string
|
|
9 options <- list()
|
|
10 list <- gsub("\\s","", argument)
|
|
11 list <- strsplit(list, ",")
|
|
12 if (length(list) > 0) {
|
|
13 list <- list[[1]]
|
|
14 for (entry in list) {
|
|
15 pair <- strsplit(entry, ":")
|
|
16 if (length(pair) > 0) {
|
|
17 pair <- pair[[1]]
|
|
18 if (length(pair) == 2) {
|
|
19 options[[pair[1]]] <- pair[2]
|
|
20 }
|
|
21 }
|
|
22 }
|
|
23 }
|
|
24 return(options)
|
|
25 }
|
|
26
|
|
27 # get options, using the spec as defined by the enclosed list.
|
|
28 spec = matrix(c(
|
|
29 'workdir', 'w', 1, 'character', 'Work directory',
|
|
30 'module', 'm', 1, 'character', 'Module name',
|
|
31 'input', 'i', 1, 'character', 'Input tabular file',
|
|
32 'columns', 'c', 1, 'character', 'Columns string',
|
|
33 'settings', 's', 1, 'character', 'Settings string',
|
|
34 'output', 'o', 1, 'character', 'Output tabular file',
|
|
35 'help', 'h', 0, '', 'Help',
|
|
36 'verbose', 'v', 0, '', 'Verbose'
|
|
37 ), byrow=TRUE, ncol=5);
|
|
38 opt = getopt(spec);
|
|
39
|
|
40 # show help
|
|
41 if ( !is.null(opt$help) ||
|
|
42 is.null(opt$module) ||
|
|
43 is.null(opt$input) ||
|
|
44 is.null(opt$columns) ||
|
|
45 is.null(opt$output)) {
|
|
46 cat(getopt(spec, usage=TRUE))
|
|
47 q(status=1);
|
|
48 }
|
|
49
|
|
50 # read columns/settings
|
|
51 columns = split(opt$columns)
|
|
52 settings = split(opt$settings)
|
|
53
|
|
54 # read table
|
|
55 table <- read.table(opt$input)
|
|
56
|
|
57 # identify module file
|
|
58 module_file = paste(opt$workdir, opt$module, '.r', sep='')
|
|
59
|
|
60 # source module
|
|
61 source(module_file)
|
|
62
|
|
63 # run module
|
|
64 l = wrapper (table, columns, settings)
|
|
65
|
|
66 # header
|
|
67 header_title <- '# title - Chart Utilities (charts)'
|
|
68 header_date <- paste('# date -', Sys.time(), sep=' ')
|
|
69 header_module <- paste('# module -', opt$module, sep=' ')
|
|
70 header_settings <- paste('# settings -', opt$settings, sep=' ')
|
|
71 header_columns <- paste('# columns -', opt$columns, sep=' ')
|
|
72
|
35
|
73 # check result
|
4
|
74 if (length(l) > 0) {
|
|
75 # print details
|
|
76 if (!is.null(opt$verbose)) {
|
|
77 print ('Columns:')
|
|
78 print (columns)
|
|
79 print ('Settings:')
|
|
80 print (settings)
|
|
81 print ('Result:')
|
|
82 print (l)
|
|
83 }
|
|
84
|
|
85 # create output file
|
|
86 output <- file(opt$output, open='wt')
|
|
87
|
|
88 # write header
|
|
89 writeLines('#', output)
|
|
90 writeLines(header_title, output)
|
|
91 writeLines(header_date, output)
|
|
92 writeLines(header_module, output)
|
|
93 writeLines(header_settings, output)
|
|
94 writeLines(header_columns, output)
|
|
95 writeLines('#', output)
|
|
96
|
|
97 # write table
|
|
98 write.table(l, file=output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t')
|
|
99
|
|
100 # close file
|
|
101 close(output)
|
|
102 } else {
|
35
|
103 # print details
|
4
|
104 print ('Columns:')
|
|
105 print (columns)
|
|
106 print ('Settings:')
|
|
107 print (settings)
|
|
108 print ('No output generated.')
|
|
109 } |