annotate probecoverage.r @ 6:7c4feda2d9c7 draft

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