comparison small_rna_maps.r @ 2:507383cce5a8 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit edbb53cb13b52bf8e71c562fa8acc2c3be2fb270
author artbio
date Mon, 14 Aug 2017 05:52:34 -0400
parents 6d48150495e3
children 12c14642e6ac
comparison
equal deleted inserted replaced
1:40972a8dfab9 2:507383cce5a8
6 library(lattice) 6 library(lattice)
7 library(latticeExtra) 7 library(latticeExtra)
8 library(grid) 8 library(grid)
9 library(gridExtra) 9 library(gridExtra)
10 library(optparse) 10 library(optparse)
11 11
12 option_list <- list( 12 option_list <- list(
13 make_option(c("-r", "--output_tab"), type="character", help="path to tabular file"), 13 make_option(c("-f", "--first_dataframe"), type="character", help="path to first dataframe"),
14 make_option(c("-s", "--sizes"), type="character", help="path to size dataframe"), 14 make_option(c("-e", "--extra_dataframe"), type="character", help="path to additional dataframe"),
15 make_option("--output_pdf", type = "character", help="path to the pdf file with plot"), 15 make_option("--extra_plot_method", type = "character", help="How additional data should be plotted"),
16 make_option("--extra_plot", type = "character", help="what additional data should be plotted") 16 make_option("--output_pdf", type = "character", help="path to the pdf file with plots")
17 ) 17 )
18 18
19 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) 19 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
20 args = parse_args(parser) 20 args = parse_args(parser)
21 if (length(args$sizes) != 0) { args$extra_plot <- "SizeDistribution"}
22 21
23 # dataset manipulation 22 # data frames implementation
24 23
25 Table = read.delim(args$output_tab, header=T, row.names=NULL) 24 Table = read.delim(args$first_dataframe, header=T, row.names=NULL)
26 Table <- within(Table, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1)) 25 Table <- within(Table, Counts[Polarity=="R"] <- (Counts[Polarity=="R"]*-1))
27 n_samples=length(unique(Table$Dataset)) 26 n_samples=length(unique(Table$Dataset))
28 genes=unique(levels(Table$Chromosome)) 27 genes=unique(levels(Table$Chromosome))
29 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x)) 28 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x))
30 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) ) 29 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) )
31 n_genes=length(per_gene_readmap) 30 n_genes=length(per_gene_readmap)
32 if (args$extra_plot == "SizeDistribution") { 31
33 size=read.delim(args$sizes, header=T, row.names=NULL) 32 ExtraTable=read.delim(args$extra_dataframe, header=T, row.names=NULL)
34 size <- within(size, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1)) 33 if (args$extra_plot_method=='Size') {
35 per_gene_size=lapply(genes, function(x) subset(size, Chromosome==x)) 34 ExtraTable <- within(ExtraTable, Count[Polarity=="R"] <- (Count[Polarity=="R"]*-1))
36 } 35 }
37 36 per_gene_size=lapply(genes, function(x) subset(ExtraTable, Chromosome==x))
37
38 ## end of data frames implementation 38 ## end of data frames implementation
39 39
40 ## functions 40 ## functions
41 41
42 plot_readmap=function(df, ...) { 42 first_plot = function(df, ...) {
43 combineLimits(xyplot(Nbr_reads~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)), 43 combineLimits(xyplot(Counts~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
44 data=df, 44 data=df,
45 type='h', 45 type='h',
46 lwd=1.5, 46 lwd=1.5,
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)), 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, 48 xlab=NULL, main=NULL, ylab=NULL,
54 par.strip.text = list(cex=0.7), 54 par.strip.text = list(cex=0.7),
55 ...)) 55 ...))
56 } 56 }
57 57
58 58
59 plot_size=function(df, ...) { 59 second_plot = function(df, ...) {
60 #smR.prepanel=function(x,y,...) {; yscale=c(y*0, max(abs(y)));list(ylim=yscale);} 60 #smR.prepanel=function(x,y,...) {; yscale=c(y*0, max(abs(y)));list(ylim=yscale);}
61 sizeplot = xyplot(eval(as.name(args$extra_plot))~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)), 61 sizeplot = xyplot(eval(as.name(args$extra_plot_method))~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
62 data=df, 62 data=df,
63 type='p', 63 type='p',
64 cex=0.35, 64 cex=0.35,
65 pch=19, 65 pch=19,
66 scales= list(relation="free", x=list(rot=0, cex=0, axs="i", tck=0.5), y=list(tick.number=4, rot=90, cex=0.7)), 66 scales= list(relation="free", x=list(rot=0, cex=0, axs="i", tck=0.5), y=list(tick.number=4, rot=90, cex=0.7)),
73 par.strip.text = list(cex=0.7), 73 par.strip.text = list(cex=0.7),
74 ...) 74 ...)
75 combineLimits(sizeplot) 75 combineLimits(sizeplot)
76 } 76 }
77 77
78 plot_size_distribution= function(df, ...) { 78 second_plot_size = function(df, ...) {
79 # smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);} 79 # smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);}
80 bc= barchart(Nbr_reads~as.factor(Size)|factor(Dataset, levels=unique(Dataset))+Chromosome, data = df, origin = 0, 80 bc= barchart(Count~as.factor(Size)|factor(Dataset, levels=unique(Dataset))+Chromosome, data = df, origin = 0,
81 horizontal=FALSE, 81 horizontal=FALSE,
82 group=Polarity, 82 group=Polarity,
83 stack=TRUE, 83 stack=TRUE,
84 col=c('red', 'blue'), 84 col=c('red', 'blue'),
85 cex=0.75, 85 cex=0.75,
101 ## function parameters 101 ## function parameters
102 par.settings.readmap=list(layout.heights=list(top.padding=0, bottom.padding=0), strip.background = list(col=c("lightblue","lightgreen")) ) 102 par.settings.readmap=list(layout.heights=list(top.padding=0, bottom.padding=0), strip.background = list(col=c("lightblue","lightgreen")) )
103 par.settings.size=list(layout.heights=list(top.padding=0, bottom.padding=0)) 103 par.settings.size=list(layout.heights=list(top.padding=0, bottom.padding=0))
104 graph_title=list(Coverage="Read Maps and Coverages", Median="Read Maps and Median sizes", Mean="Read Maps and Mean sizes", SizeDistribution="Read Maps and Size Distributions") 104 graph_title=list(Coverage="Read Maps and Coverages", Median="Read Maps and Median sizes", Mean="Read Maps and Mean sizes", SizeDistribution="Read Maps and Size Distributions")
105 graph_legend=list(Coverage="Read counts / Coverage", Median="Read counts / Median size", Mean="Read counts / Mean size", SizeDistribution="Read counts") 105 graph_legend=list(Coverage="Read counts / Coverage", Median="Read counts / Median size", Mean="Read counts / Mean size", SizeDistribution="Read counts")
106 graph_bottom=list(Coverage="Nucleotide coordinates", Median="Nucleotide coordinates", Mean="Nucleotide coordinates", SizeDistribution="Read sizes / Nucleotide coordinates") 106 graph_bottom=list(Coverage="Nucleotide coordinates", Median="Nucleotide coordinates", Mean="Nucleotide coordinates", Size="Read sizes / Nucleotide coordinates")
107 ## end of function parameters' 107 ## end of function parameters'
108 108
109 ## GRAPHS 109 ## GRAPHS
110 110
111 if (n_genes > 5) {page_height_simple = 11.69; page_height_combi=11.69; rows_per_page=6} else { 111 if (n_genes > 5) {page_height_simple = 11.69; page_height_combi=11.69; rows_per_page=6} else {
117 if (rows_per_page %% 2 != 0) { rows_per_page = rows_per_page + 1} 117 if (rows_per_page %% 2 != 0) { rows_per_page = rows_per_page + 1}
118 for (i in seq(1,n_genes,rows_per_page/2)) { 118 for (i in seq(1,n_genes,rows_per_page/2)) {
119 start=i 119 start=i
120 end=i+rows_per_page/2-1 120 end=i+rows_per_page/2-1
121 if (end>n_genes) {end=n_genes} 121 if (end>n_genes) {end=n_genes}
122 readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, strip=FALSE, par.settings=par.settings.readmap)) 122 first_plot.list=lapply(per_gene_readmap[start:end], function(x) first_plot(x, strip=FALSE, par.settings=par.settings.readmap))
123 if (args$extra_plot == "SizeDistribution") { 123 if (args$extra_plot_method == "Size") {
124 size_plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, par.settings=par.settings.size)) 124 second_plot.list=lapply(per_gene_size[start:end], function(x) second_plot_size(x, par.settings=par.settings.size))
125 } 125 }
126 else { 126 else {
127 size_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_size(x, par.settings=par.settings.size)) 127 second_plot.list=lapply(per_gene_size[start:end], function(x) second_plot(x, par.settings=par.settings.size))
128 } 128 }
129 129
130 130
131 plot.list=rbind(size_plot.list, readmap_plot.list) 131 plot.list=rbind(second_plot.list, first_plot.list)
132 args_list=c(plot.list, list(nrow=rows_per_page+1, ncol=1, 132 args_list=c(plot.list, list(nrow=rows_per_page+1, ncol=1,
133 top=textGrob(graph_title[[args$extra_plot]], gp=gpar(cex=1), just="top"), 133 top=textGrob(graph_title[[args$extra_plot_method]], gp=gpar(cex=1), just="top"),
134 left=textGrob(graph_legend[[args$extra_plot]], gp=gpar(cex=1), vjust=1, rot=90), 134 left=textGrob(graph_legend[[args$extra_plot_method]], gp=gpar(cex=1), vjust=1, rot=90),
135 sub=textGrob(graph_bottom[[args$extra_plot]], gp=gpar(cex=1), just="bottom") 135 sub=textGrob(graph_bottom[[args$extra_plot_method]], gp=gpar(cex=1), just="bottom")
136 ) 136 )
137 ) 137 )
138 do.call(grid.arrange, args_list) 138 do.call(grid.arrange, args_list)
139 } 139 }
140 devname=dev.off() 140 devname=dev.off()