Mercurial > repos > artbio > probecoverage
annotate probecoverage.r @ 3:4f744d3aaf0b draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 8159fa0b4667a953c05aca50d412c33e619b5080
author | artbio |
---|---|
date | Sun, 24 Sep 2017 14:47:11 -0400 |
parents | ebe5ec2e244d |
children | daec4df60281 |
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 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
43 maxdepth <- trunc(max(Table[,5:length(Table)]/(Table$end-Table$start))) + 20 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
44 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
|
45 i <- 5 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
46 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
|
47 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
|
48 i <- i + 1 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
49 } |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
50 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
|
51 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
|
52 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
53 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
|
54 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
55 ## GRAPHS |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
56 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 |
ebe5ec2e244d
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 0d963d33685a407873cd17924c7ec2b6255b006f
artbio
parents:
0
diff
changeset
|
65 |
ebe5ec2e244d
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 0d963d33685a407873cd17924c7ec2b6255b006f
artbio
parents:
0
diff
changeset
|
66 ## 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
|
67 |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
68 devname=dev.off() |
dbeb4a0abfc6
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff
changeset
|
69 |