comparison charts.r @ 1:344ac3ca7557 draft default tip

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