comparison size_histogram.r @ 0:234b83159ea8 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_read_size_histograms commit ab983b2e57321e8913bd4d5f8fc89c3223c69869
author artbio
date Tue, 11 Jul 2017 11:44:36 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:234b83159ea8
1 ## Setup R error handling to go to stderr
2 options( show.error.messages=F,
3 error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
4 library(RColorBrewer)
5 library(lattice)
6 library(latticeExtra)
7 library(grid)
8 library(gridExtra)
9 library(optparse)
10
11 # Parse arguments
12 option_list <- list(
13 make_option(c("-g", "--global"), type="character", help="Whether distribution is plotted globally or by chromosome"),
14 make_option(c("-s", "--size_distribution_tab"), type="character", help="Path to file with tabular size distribution"),
15 make_option("--size_distribution_pdf", type="character", help="Path to file with size distribution plot"),
16 make_option("--title", type="character", help="Title for readmaps and size distribution"),
17 make_option("--ylabel", type="character", help="ylabel for readmaps and size distribution"),
18 make_option("--yrange", type="integer", help="Y-axis range"),
19 make_option("--rows_per_page", type="integer", help="rows_per_page")
20 )
21
22 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
23 args = parse_args(parser)
24
25 ##cheetahtemplate data frame implementation
26 size=read.delim(args$size_distribution_tab, header=T, row.names=NULL)
27 n_samples = length(unique (size$sample))
28 n_genes = length (unique (levels(size$gene)))
29
30 if (args$yrange != 0) {
31 # This is used for specifying the y-axis limits
32 ylim=c(-args$yrange, args$yrange)
33 } else { ylim="" }
34
35 par.settings.size=list(layout.heights=list(top.padding=1, bottom.padding=1),
36 strip.background = list(col = c("lightblue", "lightgreen"))
37 )
38
39 smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);} # use if one want y axis in the middle of the plot
40
41 plot_size_distribution = function(df, ...) {
42 bc= barchart(count~as.factor(size)|factor(sample, levels=unique(sample))+gene, data = df, origin = 0,
43 horizontal=FALSE,
44 group=polarity,
45 stack=TRUE,
46 col=c('red', 'blue'),
47 cex=0.75,
48 scales=list(y=list(tick.number=4, rot=90, relation="free", cex=0.5, alternating=T), x=list(cex=.6 ) ),
49 xlab = "readsize in nucleotides",
50 ylab = args$ylabel,
51 main = args$title,
52 par.strip.text = list(cex=0.75),
53 as.table=TRUE,
54 newpage = T,
55 ...)
56
57 combineLimits(update(useOuterStrips(bc,
58 strip.left = strip.custom(par.strip.text = list(cex=0.5))
59 ),
60 layout=c(n_samples,args$rows_per_page)),
61 margin.x=F, margin.y=1)
62 }
63
64 # per_gene_size=lapply(genes, function(x) subset(size, gene==x)) # no object in this script
65
66 if (args$global == "no") {
67 width = 8.2677*n_samples/4
68 } else { width = 8.2677 }
69
70 options(warn=-1)
71 pdf(file=args$size_distribution_pdf, paper="special", height=11.69, width=width)
72
73 if (ylim == "" && args$global=="no") {
74 plot_size_distribution(size, par.settings=par.settings.size)
75 }
76 if (ylim != "" && args$global=="no") { plot_size_distribution(size, par.settings=par.settings.size, ylim=ylim)
77 }
78 if (ylim == "" && args$global=="yes") { bc= barchart(count~as.factor(size)|factor(sample, levels=unique(sample)),
79 data = size, origin = 0,
80 horizontal=FALSE,
81 group=polarity,
82 stack=TRUE,
83 col=c('red', 'blue'),
84 scales=list(y=list(tick.number=4, rot=90, relation="same"), cex=1),
85 xlab = "readsize in nucleotides",
86 ylab = args$ylabel,
87 main = args$title, as.table=TRUE, newpage = T,
88 aspect=0.5,
89 strip = strip.custom(par.strip.text = list(cex = 1), which.given=1, bg="lightblue")
90 )
91 bc
92 }
93 if (ylim != "" && args$global=="yes") { bc= barchart(count~as.factor(size)|factor(sample, levels=unique(sample)),
94 data = size, origin = 0,
95 horizontal=FALSE,
96 group=polarity,
97 stack=TRUE,
98 col=c('red', 'blue'),
99 scales=list(y=list(tick.number=4, rot=90, relation="same"), cex=1),
100 xlab = "readsize in nucleotides",
101 ylab = args$ylabel,
102 ylim = ylim,
103 main = args$title, as.table=TRUE, newpage = T,
104 aspect=0.5,
105 strip = strip.custom(par.strip.text = list(cex = 1), which.given=1, bg="lightblue")
106 )
107 bc
108 }
109
110 devname=dev.off()