Mercurial > repos > artbio > probecoverage
annotate probecoverage.r @ 8:0adb846ca056 draft default tip
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
author | artbio |
---|---|
date | Tue, 20 Feb 2024 08:34:16 +0000 |
parents | bea8435e1e79 |
children |
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 |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
2 options( |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
3 show.error.messages = FALSE, |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
4 error = function() { |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
5 cat(geterrmessage(), file = stderr()) |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
6 q("no", 1, FALSE) |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
7 } |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
8 ) |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
9 warnings() |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
10 library(optparse) |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
11 library(ggplot2) |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
12 library(reshape2) |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
13 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 make_option(c("-o", "--output"), type = "character", help = "path to the pdf plot") |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
22 ) |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
23 |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 samples <- strsplit(samples, ", ") |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
28 |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
29 # data frames implementation |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
30 |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
31 table <- read.delim(args$input, header = FALSE) |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
32 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
|
33 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
|
34 headers <- c(headers, samples[[1]][i]) |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
35 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
|
36 } |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
37 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
38 ## function |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
39 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
|
40 cumul <- function(x, y) sum(table[, y] / (table$end - table$start) > x) / length(table$chromosome) |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
41 } else { |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
42 cumul <- function(x, y) sum(table[, y] > x) / length(table$chromosome) |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
43 } |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
44 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
|
45 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
46 ## end of function |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
47 ## 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
|
48 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
|
49 maxdepth <- trunc(max(table[, 5:length(table)] / (table$end - table$start))) + 20 |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
50 } else { |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
51 maxdepth <- trunc(max(table[, 5:length(table)])) + 20 |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
52 } |
4
daec4df60281
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 2b4a222804a40730abdaec6fb6aefc2295e89e11
artbio
parents:
3
diff
changeset
|
53 |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
54 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
|
55 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
|
56 for (colonne in colnames(table)[5:length(colnames(table))]) { |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
57 graphpoints <- cbind(graphpoints, mapply(cumul, 1:maxdepth, rep(i, maxdepth))) |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
58 i <- i + 1 |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
59 } |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
60 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
|
61 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
|
62 |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
63 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
|
64 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
65 ## GRAPHS |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
66 |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
67 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
|
68 ggplot(data = graphpoints, aes(x = Depth, y = sample_value, colour = Samples)) + |
8
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
69 geom_line(size = 1) + |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
70 scale_x_continuous(trans = "log2", breaks = 2^(seq(0, log(maxdepth, 2)))) + |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
71 scale_y_continuous(breaks = seq(0, maxfrac, by = maxfrac / 10), labels = scale_fun) + |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
72 labs(x = args$xlab, y = args$ylab, title = args$title) + |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
73 theme( |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
74 legend.position = "top", legend.title = element_blank(), |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
75 legend.text = element_text(colour = "blue", size = 7) |
0adb846ca056
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/probecoverage commit 668f7e9be66ae301f840b395d72ddcc311da7aa2
artbio
parents:
7
diff
changeset
|
76 ) |
0
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
77 |
7
bea8435e1e79
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 6dc66b89f0792e4da40719ec83494a1fcee5e105"
artbio
parents:
4
diff
changeset
|
78 devname <- dev.off() |