annotate probecoverage.r @ 2:35d2db3753d9 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 301fc26e062ac02a28676a05aa9c82e4407e3d29
author artbio
date Sun, 24 Sep 2017 13:34:16 -0400
parents ebe5ec2e244d
children 4f744d3aaf0b
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"),
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
15 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
16 )
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 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
19 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
20 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
21 samples = strsplit(samples, ", ")
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
22
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
23 # data frames implementation
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
24
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
25 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
26 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
27 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
28 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
29 colnames(Table) <- headers
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
30 }
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 ## function
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
33
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
34 cumul <- function(x,y) sum(Table[,y]/(Table$end-Table$start) > x)/length(Table$chromosome)
1
ebe5ec2e244d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 0d963d33685a407873cd17924c7ec2b6255b006f
artbio
parents: 0
diff changeset
35 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
36
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
37 ## end of function
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
38 ## 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
39 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
40 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
41 i <- 5
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
42 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
43 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
44 i <- i + 1
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 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
47 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
48
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
49 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
50
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
51 ## GRAPHS
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 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
54 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
55 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
56 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
57 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
58 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
59 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
60
ebe5ec2e244d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 0d963d33685a407873cd17924c7ec2b6255b006f
artbio
parents: 0
diff changeset
61
ebe5ec2e244d planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 0d963d33685a407873cd17924c7ec2b6255b006f
artbio
parents: 0
diff changeset
62 ## 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
63
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
64 devname=dev.off()
dbeb4a0abfc6 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/probecoverage commit 102edc0c210d94e9d72f913e2d18c19220c4167c
artbio
parents:
diff changeset
65