annotate charts.r @ 57:5472b173c67b draft

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