annotate ez_histograms.R @ 1:fbedb212982d draft default tip

planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
author artbio
date Thu, 08 Feb 2024 02:15:11 +0000
parents bdf40b0924cb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
1
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
5 library(psych)
0
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
1
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
119 ##### prepare input data
0
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
120
1
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
121 data <- read.delim(file = opt$file, header = test_header(opt$file))
0
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
122 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
123 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
124
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
125 ##### main
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 # 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
128 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
129 # plot
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
130 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
131
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
132 # 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
133 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
134 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
135 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
136 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
137 TRUE ~ 14
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
138 )
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
139 # 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
140 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
141 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
142 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
143 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
144 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
145 )
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
146 # 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
147 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
148 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
149 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
150 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
151 TRUE ~ 3
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
152 )
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
153 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
154 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
155 dev.off()
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
156
1
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
157 # Summary statistics with psych package
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
158 summary_df <- describe(x = data, skew = FALSE, ranges = FALSE, quant = c(.25, .50, .75))
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
159 summary_df <- cbind(var_names = rownames(summary_df), summary_df)
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
160 colnames(summary_df)[2] <- "var_num"
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
161 summary_df <- summary_df[, -6]
fbedb212982d planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 5e25392164eca5585239b62c82b7f6ba326cda6e
artbio
parents: 0
diff changeset
162 summary_df[, 4:8] <- format(summary_df[, 4:8], scientific = TRUE)
0
bdf40b0924cb planemo upload for repository https://github.com/artbio/tools-artbio/tree/main/tools/ez_histograms commit 443759a746f78d67dc4ffcafdc6610d09d278846
artbio
parents:
diff changeset
163 write.table(summary_df, file = opt$summary, sep = "\t", quote = FALSE, row.names = FALSE)