comparison plot_grouped_barplot.R @ 0:6c9aac0d6ca2 draft default tip

planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/plot_grouped_barplot commit c6ba903395dc6d41cb318c7d95a2d9cf3ca65313-dirty
author bebatut
date Mon, 18 Apr 2016 10:08:25 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6c9aac0d6ca2
1 library('getopt')
2
3 option_specification = matrix(c(
4 'input_file', 'i', 2, 'character',
5 'output_pdf_file', 'p', 2, 'character',
6 'output_png_file', 'o', 2, 'character',
7 'output_svg_file', 's', 2, 'character',
8 'data_columns', 'd', 2, 'list',
9 'names', 'a', 2, 'character',
10 'names_column', 'n', 2, 'integer',
11 'xlab', 'x', 2, 'character',
12 'log', 'g', 2, 'logical',
13 'col', 'c', 2, 'list',
14 'bottom_margin', 'b', 2, 'integer',
15 'left_margin', 'l', 2, 'integer',
16 'top_margin', 't', 2, 'integer',
17 'right_margin', 'r', 2, 'integer',
18 'legend_pos','e',2,'character',
19 'replace_null','u',2,'logical',
20 'order','w',2,'logical',
21 'header','y',2,'logical'
22 ), byrow=TRUE, ncol=4);
23
24 options = getopt(option_specification);
25
26 header = TRUE
27 if(!is.null(options$header)) header = options$header
28
29 data = read.table(options$input_file, sep = '\t', h = header)
30 if(!is.null(options$replace_null) && options$replace_null){
31 data[data == 0] = NA
32 }
33 if(!is.null(options$order) && options$order){
34 order = order(data[,2])
35 data = data[order,]
36 }
37
38
39 data_columns = c(2,3)
40 if(!is.null(options$data_columns)){
41 data_columns = unlist(strsplit(options$data_columns, split=","))
42 data_columns = sapply(data_columns,as.integer)
43 }
44 names_column = 1
45 if(!is.null(options$names_column)) names_column = options$names_column
46
47 margin = c(5,19,1,1)
48 if(!is.null(options$bottom_margin)) margin[1] = options$bottom_margin
49 if(!is.null(options$left_margin)) margin[2] = options$left_margin
50 if(!is.null(options$top_margin)) margin[3] = options$top_margin
51 if(!is.null(options$right_margin)) margin[4] = options$right_margin
52
53 xlab = ""
54 if(!is.null(options$xlab)) xlab = options$xlab
55
56 col = c('blue','red')
57 if(!is.null(options$col)) col = unlist(strsplit(options$col, split=","))
58
59 log = ""
60 if(!is.null(options$log) && options$log) log = "x"
61
62 legend_pos="topright"
63 if(!is.null(options$legend_pos)) legend_pos = options$legend_pos
64
65 names = c('Sample1','Sample2')
66
67 if(!is.null(options$names)) names = unlist(strsplit(options$names, split=","))
68 plot_barplot <- function(){
69 par(las=2)
70 par(mar=margin)
71 barplot(t(data[, data_columns]), horiz = T, xlab = xlab, beside=TRUE,
72 names.arg = data[, names_column], col = col, cex.names=0.7,
73 cex.axis = 0.8, log = log)
74 legend(legend_pos, legend = names, fill = col, cex = 0.7)
75 }
76
77 if(!is.null(options$output_pdf_file)){
78 pdf(options$output_pdf_file)
79 plot_barplot()
80 dev.off()
81 }
82
83 if(!is.null(options$output_svg_file)){
84 svg(options$output_svg_file)
85 plot_barplot()
86 dev.off()
87 }