annotate probecoverage.r @ 7:bea8435e1e79 draft

"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
author artbio
date Thu, 03 Jun 2021 23:59:31 +0000
parents daec4df60281
children 0adb846ca056
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
1 ## Setup R error handling to go to stderr
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
2 options(show.error.messages = F,
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
3 error = function() {
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
4 cat(geterrmessage(), file = stderr()); q("no", 1, F)
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
5 }
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
6 )
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
7 warnings()
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
8 library(optparse)
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
9 library(ggplot2)
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
10 library(reshape2)
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
11
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
12 option_list <- list(
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
13 make_option(c("-i", "--input"), type = "character", help = "Path to dataframe"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
14 make_option(c("-t", "--title"), type = "character", help = "Main Title"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
15 make_option("--xlab", type = "character", help = "X-axis legend"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
16 make_option("--ylab", type = "character", help = "Y-axis legend"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
17 make_option("--sample", type = "character", help = "a space separated of sample labels"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
18 make_option("--method", type = "character", help = "bedtools or pysam"),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
19 make_option(c("-o", "--output"), type = "character", help = "path to the pdf plot")
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
20 )
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
21
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
22 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
23 args <- parse_args(parser)
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
24 samples <- substr(args$sample, 2, nchar(args$sample) - 2)
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
25 samples <- strsplit(samples, ", ")
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
26
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
27 # data frames implementation
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
28
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
29 table <- read.delim(args$input, header = F)
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
30 headers <- c("chromosome", "start", "end", "id")
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
31 for (i in seq(1, length(table) - 4)) {
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
32 headers <- c(headers, samples[[1]][i])
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
33 colnames(table) <- headers
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
34 }
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
35
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
36 ## function
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
37 if (args$method == "bedtools") {
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
38 cumul <- function(x, y) sum(table[, y] / (table$end - table$start) > x) / length(table$chromosome)
3
4f744d3aaf0b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 8159fa0b4667a953c05aca50d412c33e619b5080
artbio
parents: 1
diff changeset
39 } else {
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
40 cumul <- function(x, y) sum(table[, y] > x) / length(table$chromosome)
3
4f744d3aaf0b planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 8159fa0b4667a953c05aca50d412c33e619b5080
artbio
parents: 1
diff changeset
41 }
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
42 scale_fun <- function(x) sprintf("%.3f", x)
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
43
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
44 ## end of function
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
45 ## let's do a dataframe before plotting
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
46 if (args$method == "bedtools") {
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
47 maxdepth <- trunc(max(table[, 5:length(table)] / (table$end - table$start))) + 20
4
daec4df60281 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 2b4a222804a40730abdaec6fb6aefc2295e89e11
artbio
parents: 3
diff changeset
48 } else {
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
49 maxdepth <- trunc(max(table[, 5:length(table)])) + 20
4
daec4df60281 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 2b4a222804a40730abdaec6fb6aefc2295e89e11
artbio
parents: 3
diff changeset
50 }
daec4df60281 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 2b4a222804a40730abdaec6fb6aefc2295e89e11
artbio
parents: 3
diff changeset
51
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
52 graphpoints <- data.frame(1:maxdepth)
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
53 i <- 5
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
54 for (colonne in colnames(table)[5:length(colnames(table))]) {
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
55 graphpoints <- cbind(graphpoints, mapply(cumul, 1:maxdepth, rep(i, maxdepth)))
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
56 i <- i + 1
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
57 }
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
58 colnames(graphpoints) <- c("Depth", colnames(table)[5:length(table)])
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
59 maxfrac <- max(graphpoints[, 2:length(graphpoints)])
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
60
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
61 graphpoints <- melt(graphpoints, id.vars = "Depth", variable.name = "Samples", value.name = "sample_value")
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
62
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
63 ## GRAPHS
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
64
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
65 pdf(file = args$output)
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
66 ggplot(data = graphpoints, aes(x = Depth, y = sample_value, colour = Samples)) +
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
67 geom_line(size = 1) +
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
68 scale_x_continuous(trans = "log2", breaks = 2 ^ (seq(0, log(maxdepth, 2)))) +
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
69 scale_y_continuous(breaks = seq(0, maxfrac, by = maxfrac / 10), labels = scale_fun) +
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
70 labs(x = args$xlab, y = args$ylab, title = args$title) +
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
71 theme(legend.position = "top", legend.title = element_blank(),
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
72 legend.text = element_text(colour = "blue", size = 7))
0
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
73
7
bea8435e1e79 "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents: 4
diff changeset
74 devname <- dev.off()