comparison plot_size_readmap.r @ 8:be0c6b6466cc draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_readmap_and_size_histograms commit 97b40d7a593cef6c3303f7baba781a84d242e454
author mvdbeek
date Mon, 19 Sep 2016 06:16:21 -0400
parents
children 45df4c72a780
comparison
equal deleted inserted replaced
7:c9e267cb84c0 8:be0c6b6466cc
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("-r", "--readmap_tab"), type="character", help="Path to file with tabular readmap"),
14 make_option(c("-s", "--size_distribution_tab"), type="character", help="Path to file with tabular size distribution"),
15 make_option("--readmap_pdf", type="character", help="Path to file with readmap plot"),
16 make_option("--size_distribution_pdf", type="character", help="Path to file with size distribution plot"),
17 make_option("--combi_pdf", type="character", help="Path to file with size distribution and readmap plot"),
18 make_option("--title", type="character", help="Title for readmaps and size distribution"),
19 make_option("--xlabel", type="character", help="xlabel for readmaps and size distribution"),
20 make_option("--ylabel", type="character", help="ylabel for readmaps and size distribution"),
21 make_option("--yrange", type="integer", help="Y-axis range"),
22 make_option("--rows_per_page", type="integer", help="rows_per_page")
23 )
24
25 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
26 args = parse_args(parser)
27
28 ## data frames implementation
29
30 rm=read.delim(args$readmap_tab, header=T, row.names=NULL)
31 n_samples=length(unique(rm$sample))
32 genes=unique(levels(rm$gene))
33 per_gene_readmap=lapply(genes, function(x) subset(rm, gene==x))
34 n_genes=length(per_gene_readmap)
35
36 size=read.delim(args$size_distribution_tab, header=T, row.names=NULL)
37 per_gene_size=lapply(genes, function(x) subset(size, gene==x))
38
39 ## end of data frames implementation
40
41 ## functions
42
43 plot_readmap=function(df, ...) {
44 combineLimits(xyplot(count~coord|factor(sample, levels=unique(sample))+reorder(gene, count, function(x) -sum(abs(x))),
45 data=df,
46 type='h',
47 scales= list(relation="free", x=list(rot=0, cex=0.7, axs="i", tck=0.5), y=list(tick.number=4, rot=90, cex=0.7)),
48 xlab=NULL, main=NULL, ylab=NULL,
49 as.table=T,
50 origin = 0,
51 horizontal=FALSE,
52 group=polarity,
53 col=c("red","blue"),
54 par.strip.text = list(cex=0.7),
55 ...))
56 }
57
58 plot_size_distribution= function(df, ...) {
59 smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);}
60 bc= barchart(count~as.factor(size)|factor(sample, levels=unique(sample))+gene, data = df, origin = 0,
61 horizontal=FALSE,
62 group=polarity,
63 stack=TRUE,
64 col=c('red', 'blue'),
65 cex=0.75,
66 scales=list(y=list(tick.number=4, rot=90, relation="free", cex=0.7), x=list(cex=0.7) ),
67 prepanel=smR.prepanel,
68 xlab = NULL,
69 ylab = NULL,
70 main = NULL,
71 as.table=TRUE,
72 newpage = T,
73 par.strip.text = list(cex=0.7),
74 ...)
75 combineLimits(bc)
76 }
77
78 ## end of functions
79
80 ## function parameters'
81
82 par.settings.readmap=list(layout.heights=list(top.padding=0, bottom.padding=-2.5), strip.background = list(col=c("lightblue","lightgreen")) )
83 par.settings.size=list(layout.heights=list(top.padding=-1, bottom.padding=-2.5), strip.background = list(col=c("lightblue","lightgreen")) )
84 par.settings.combination.readmap=list(layout.heights=list(top.padding=0, bottom.padding=-3), strip.background=list(col=c("lightblue","lightgreen")) )
85 par.settings.combination.size=list(layout.heights=list(top.padding=-2, bottom.padding=-0.5), strip.background=list(col=c("lightblue", "lightgreen")) )
86
87 ## end of function parameters'
88
89 ## GRAPHS
90
91 if (n_genes > 7) {page_height_simple = 11.69; page_height_combi=11.69; rows_per_page=args$rows_per_page} else {
92 rows_per_page= n_genes; page_height_simple = 2.5*n_genes; page_height_combi=page_height_simple*2 }
93 if (n_samples > 4) {page_width = 8.2677*n_samples/4} else {page_width = 8.2677*n_samples/3} # to test
94
95
96 pdf(file=args$readmap_pdf, paper="special", height=page_height_simple, width=page_width)
97 for (i in seq(1,n_genes,rows_per_page)) {
98 start=i
99 end=i+rows_per_page-1
100 if (end>n_genes) {end=n_genes}
101 if (args$yrange == 0) { readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, par.settings=par.settings.readmap)) } else {
102 readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, ylim=c(-args.yrange, args.yrange) , par.settings=par.settings.readmap)) }
103 args_list=c(readmap_plot.list, list(nrow=rows_per_page, ncol=1,
104 top=textGrob("Read Maps (nucleotide coordinates)", gp=gpar(cex=1), just="top"),
105 left=textGrob(args$ylabel, gp=gpar(cex=1), vjust=1, rot=90)
106 )
107 )
108 do.call(grid.arrange, args_list)
109 }
110 devname=dev.off()
111
112 pdf(file=args$size_distribution_pdf, paper="special", height=page_height_simple, width=page_width)
113 for (i in seq(1,n_genes,rows_per_page)) {
114 start=i
115 end=i+rows_per_page-1
116 if (end>n_genes) {end=n_genes}
117 plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, par.settings=par.settings.size) )
118 args_list=c(plot.list, list(nrow=rows_per_page, ncol=1,
119 top=textGrob("Size distributions (in nucleotides)", gp=gpar(cex=1), just="top"),
120 left=textGrob(args$ylabel, gp=gpar(cex=1), vjust=1, rot=90)
121 )
122 )
123 do.call(grid.arrange, args_list)
124 }
125 devname=dev.off()
126
127 pdf(file=args$combi_pdf, paper="special", height=page_height_combi, width=page_width)
128 if (rows_per_page %% 2 != 0) { rows_per_page = rows_per_page + 1}
129 for (i in seq(1,n_genes,rows_per_page/2)) {
130 start=i
131 end=i+rows_per_page/2-1
132 if (end>n_genes) {end=n_genes}
133 if (args$yrange == 0) {readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, par.settings=par.settings.readmap)) } else {
134 readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, ylim=c(-args.yrange, args.yrange), par.settings=par.settings.readmap)) }
135 size_plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, strip=FALSE, par.settings=par.settings.combination.size))
136 plot.list=rbind(readmap_plot.list, size_plot.list )
137 args_list=c(plot.list, list(nrow=rows_per_page+1, ncol=1,
138 top=textGrob(args$title, gp=gpar(cex=1), just="top"),
139 left=textGrob(args$ylabel, gp=gpar(cex=1), vjust=1, rot=90),
140 sub=textGrob(args$xlabel, gp=gpar(cex=1), just="bottom")
141 )
142 )
143 do.call(grid.arrange, args_list)
144 }
145 devname=dev.off()