Mercurial > repos > artbio > ez_histograms
annotate ez_histograms.R @ 0:bdf40b0924cb draft
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
author | artbio |
---|---|
date | Wed, 07 Feb 2024 19:49:56 +0000 |
parents | |
children | fbedb212982d |
rev | line source |
---|---|
0
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
1 library(ggplot2) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
2 library(reshape2) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
3 library(dplyr) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
4 library(scales) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
5 library(vtable) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
6 library(optparse) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
7 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
8 options(show.error.messages = FALSE, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
9 error = function() { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
10 cat(geterrmessage(), file = stderr()) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
11 q("no", 1, FALSE) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
12 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
13 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
14 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
15 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
16 warnings() |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
17 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
18 option_list <- list( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
19 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
20 c("-f", "--file"), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
21 default = NA, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
22 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
23 help = "Input file that contains count values to transform" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
24 ), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
25 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
26 c("-d", "--profile"), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
27 default = "count", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
28 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
29 help = "Whether y-axis shows absolute counts or density: 'count' or 'density' [default : '%default' ]" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
30 ), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
31 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
32 "--xscale", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
33 default = "cartesian", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
34 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
35 help = "Whether x-axis is 'cartesian', 'log2' or 'log10' [default : '%default' ]" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
36 ), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
37 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
38 "--yscale", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
39 default = "cartesian", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
40 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
41 help = "Whether y-axis is 'cartesian', 'log2' or 'log10' [default : '%default' ]" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
42 ), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
43 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
44 c("-p", "--pdf"), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
45 default = "histograms.pdf", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
46 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
47 help = "Output pdf file name [default : '%default' ]" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
48 ), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
49 make_option( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
50 c("-s", "--summary"), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
51 default = "summary.tsv", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
52 type = "character", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
53 help = "statistics summary file name [default : '%default' ]" |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
54 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
55 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
56 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
57 opt <- parse_args(OptionParser(option_list = option_list), |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
58 args = commandArgs(trailingOnly = TRUE)) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
59 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
60 plot_histograms <- function(mdata, profile = "count", xscale = "cartesian", yscale = "cartesian", bins = 30) { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
61 if (profile == "count") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
62 # count histogram |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
63 p <- ggplot(mdata, aes(x = value, fill = variable, color = variable, y = after_stat(count)), show.legend = FALSE) + |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
64 geom_histogram(bins = bins) + theme(legend.position = "none") |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
65 if (xscale == "cartesian") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
66 if (yscale == "log2") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
67 p <- p + scale_y_continuous(trans = "log2", labels = trans_format("log2", math_format(2^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
68 } else { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
69 if (yscale == "log10") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
70 p <- p + scale_y_continuous(trans = "log10", labels = trans_format("log10", math_format(10^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
71 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
72 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
73 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
74 if (xscale == "log2") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
75 p <- p + scale_x_continuous(trans = "log2", labels = trans_format("log2", math_format(2^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
76 if (yscale == "log2") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
77 p <- p + scale_y_continuous(trans = "log2", labels = trans_format("log2", math_format(2^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
78 } else { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
79 if (yscale == "log10") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
80 p <- p + scale_y_continuous(trans = "log10", labels = trans_format("log10", math_format(10^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
81 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
82 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
83 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
84 if (xscale == "log10") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
85 p <- p + scale_x_continuous(trans = "log10", labels = trans_format("log10", math_format(10^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
86 if (yscale == "log2") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
87 p <- p + scale_y_continuous(trans = "log2", labels = trans_format("log2", math_format(2^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
88 } else { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
89 if (yscale == "log10") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
90 p <- p + scale_y_continuous(trans = "log10", labels = trans_format("log10", math_format(10^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
91 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
92 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
93 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
94 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
95 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
96 if (profile == "density") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
97 # density histogram |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
98 p <- ggplot(mdata, aes(x = value, fill = variable, color = variable)) + |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
99 geom_density() + theme(legend.position = "none") |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
100 if (xscale == "log2") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
101 p <- p + scale_x_continuous(trans = "log2", labels = trans_format("log2", math_format(2^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
102 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
103 if (xscale == "log10") { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
104 p <- p + scale_x_continuous(trans = "log10", labels = trans_format("log10", math_format(10^.x))) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
105 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
106 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
107 return(p) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
108 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
109 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
110 test_header <- function(file) { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
111 data <- read.delim(file = file, header = FALSE, row.names = 1, nrows = 2) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
112 if (all(is.na(as.numeric(data[1, seq_len(ncol(data))])))) { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
113 return(TRUE) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
114 } else { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
115 return(FALSE) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
116 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
117 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
118 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
119 test_rownames <- function(file) { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
120 data <- read.delim(file = file, header = FALSE, row.names = NULL, nrows = 2) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
121 if (is.na(as.numeric(data[2, 1]))) { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
122 return(1) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
123 } else { |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
124 return(NULL) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
125 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
126 } |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
127 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
128 ##### prepare input data |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
129 data <- read.delim(file = opt$file, header = test_header(opt$file), row.names = test_rownames(opt$file)) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
130 data <- data %>% select(where(is.numeric)) # remove non numeric columns |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
131 mdata <- melt(data) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
132 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
133 ##### main |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
134 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
135 # determine optimal number of bins (Sturges’ Rule) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
136 bins <- ceiling(log2(nrow(data)) + 1) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
137 # plot |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
138 p <- plot_histograms(mdata, profile = opt$profile, xscale = opt$xscale, bins = bins, yscale = opt$yscale) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
139 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
140 # determine optimal width for the graph |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
141 width <- length(data) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
142 width <- case_when( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
143 width == 1 ~ 14 / 3, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
144 width == 2 ~ (2 / 3) * 14, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
145 TRUE ~ 14 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
146 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
147 # determine optimal height for the graph |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
148 height <- length(data) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
149 height <- case_when( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
150 height <= 3 ~ 3, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
151 height <= 6 ~ 6, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
152 TRUE ~ (floor(height / 3) + 1) * 3 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
153 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
154 # determine optimal number of col for the graph |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
155 ncol <- length(data) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
156 ncol <- case_when( |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
157 ncol == 1 ~ 1, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
158 ncol == 2 ~ 2, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
159 TRUE ~ 3 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
160 ) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
161 pdf(opt$pdf, width = width, height = height) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
162 print(p + facet_wrap(~variable, ncol = ncol, scales = "free")) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
163 dev.off() |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
164 |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
165 # Summary statistics with vtable package |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
166 summary_df <- sumtable(data, digits = 8, out = "return", add.median = TRUE, |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
167 summ.names = c("N", "Mean", "Std. Dev.", "Min", "Pctl. 25", |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
168 "Median", "Pctl. 75", "Max")) |
bdf40b0924cb
planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff
changeset
|
169 write.table(summary_df, file = opt$summary, sep = "\t", quote = FALSE, row.names = FALSE) |