diff 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
line wrap: on
line diff
--- a/small_rna_maps.r	Mon Jul 24 12:31:04 2017 -0400
+++ b/small_rna_maps.r	Mon Aug 14 05:52:34 2017 -0400
@@ -8,39 +8,39 @@
 library(grid)
 library(gridExtra)
 library(optparse)
- 
+
 option_list <- list(
-    make_option(c("-r", "--output_tab"), type="character", help="path to tabular file"),
-    make_option(c("-s", "--sizes"), type="character", help="path to size dataframe"),
-    make_option("--output_pdf", type = "character", help="path to the pdf file with plot"),
-    make_option("--extra_plot", type = "character", help="what additional data should be plotted")
+    make_option(c("-f", "--first_dataframe"), type="character", help="path to first dataframe"),
+    make_option(c("-e", "--extra_dataframe"), type="character", help="path to additional dataframe"),
+    make_option("--extra_plot_method", type = "character", help="How additional data should be plotted"),
+    make_option("--output_pdf", type = "character", help="path to the pdf file with plots")
     )
  
 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
 args = parse_args(parser)
-if (length(args$sizes) != 0) { args$extra_plot <- "SizeDistribution"}
  
-# dataset manipulation
+# data frames implementation
 
-Table = read.delim(args$output_tab, header=T, row.names=NULL)
-Table <- within(Table, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1))
+Table = read.delim(args$first_dataframe, header=T, row.names=NULL)
+Table <- within(Table, Counts[Polarity=="R"] <- (Counts[Polarity=="R"]*-1))
 n_samples=length(unique(Table$Dataset))
 genes=unique(levels(Table$Chromosome))
 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x))
 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) )
 n_genes=length(per_gene_readmap)
-if (args$extra_plot == "SizeDistribution") {
-    size=read.delim(args$sizes, header=T, row.names=NULL)
-    size <- within(size, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1))
-    per_gene_size=lapply(genes, function(x) subset(size, Chromosome==x))
+
+ExtraTable=read.delim(args$extra_dataframe, header=T, row.names=NULL)
+if (args$extra_plot_method=='Size') {
+    ExtraTable <- within(ExtraTable, Count[Polarity=="R"] <- (Count[Polarity=="R"]*-1))
     }
-
+per_gene_size=lapply(genes, function(x) subset(ExtraTable, Chromosome==x))
+    
 ## end of data frames implementation
 
 ## functions
 
-plot_readmap=function(df, ...) {
-    combineLimits(xyplot(Nbr_reads~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
+first_plot = function(df, ...) {
+    combineLimits(xyplot(Counts~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
     data=df,
     type='h',
     lwd=1.5,
@@ -56,9 +56,9 @@
     }
 
 
-plot_size=function(df, ...) {
+second_plot = function(df, ...) {
     #smR.prepanel=function(x,y,...) {; yscale=c(y*0, max(abs(y)));list(ylim=yscale);}
-    sizeplot = xyplot(eval(as.name(args$extra_plot))~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
+    sizeplot = xyplot(eval(as.name(args$extra_plot_method))~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
     data=df,
     type='p',
     cex=0.35,
@@ -75,9 +75,9 @@
     combineLimits(sizeplot)
     }
 
-plot_size_distribution= function(df, ...) {
+second_plot_size = function(df, ...) {
 #  smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);}
-  bc= barchart(Nbr_reads~as.factor(Size)|factor(Dataset, levels=unique(Dataset))+Chromosome, data = df, origin = 0,
+  bc= barchart(Count~as.factor(Size)|factor(Dataset, levels=unique(Dataset))+Chromosome, data = df, origin = 0,
     horizontal=FALSE,
 group=Polarity,
 stack=TRUE,
@@ -103,7 +103,7 @@
 par.settings.size=list(layout.heights=list(top.padding=0, bottom.padding=0))
 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")
 graph_legend=list(Coverage="Read counts / Coverage", Median="Read counts / Median size", Mean="Read counts / Mean size", SizeDistribution="Read counts")
-graph_bottom=list(Coverage="Nucleotide coordinates", Median="Nucleotide coordinates", Mean="Nucleotide coordinates", SizeDistribution="Read sizes / Nucleotide coordinates")
+graph_bottom=list(Coverage="Nucleotide coordinates", Median="Nucleotide coordinates", Mean="Nucleotide coordinates", Size="Read sizes / Nucleotide coordinates")
 ## end of function parameters'
 
 ## GRAPHS
@@ -119,20 +119,20 @@
     start=i
     end=i+rows_per_page/2-1
     if (end>n_genes) {end=n_genes}
-    readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, strip=FALSE, par.settings=par.settings.readmap))
-    if (args$extra_plot == "SizeDistribution") {
-        size_plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, par.settings=par.settings.size))
+    first_plot.list=lapply(per_gene_readmap[start:end], function(x) first_plot(x, strip=FALSE, par.settings=par.settings.readmap))
+    if (args$extra_plot_method == "Size") {
+        second_plot.list=lapply(per_gene_size[start:end], function(x) second_plot_size(x, par.settings=par.settings.size))
         }
     else {
-        size_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_size(x, par.settings=par.settings.size))
+        second_plot.list=lapply(per_gene_size[start:end], function(x) second_plot(x, par.settings=par.settings.size))
         }
     
         
-    plot.list=rbind(size_plot.list, readmap_plot.list)
+    plot.list=rbind(second_plot.list, first_plot.list)
     args_list=c(plot.list, list(nrow=rows_per_page+1, ncol=1,
-                                    top=textGrob(graph_title[[args$extra_plot]], gp=gpar(cex=1), just="top"),
-                                    left=textGrob(graph_legend[[args$extra_plot]], gp=gpar(cex=1), vjust=1, rot=90),
-                                    sub=textGrob(graph_bottom[[args$extra_plot]], gp=gpar(cex=1), just="bottom")
+                                    top=textGrob(graph_title[[args$extra_plot_method]], gp=gpar(cex=1), just="top"),
+                                    left=textGrob(graph_legend[[args$extra_plot_method]], gp=gpar(cex=1), vjust=1, rot=90),
+                                    sub=textGrob(graph_bottom[[args$extra_plot_method]], gp=gpar(cex=1), just="bottom")
                                     )
            )
 do.call(grid.arrange, args_list)