comparison small_rna_clusters.r @ 0:8028521b6e4f draft

"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_clusters commit f38805cf151cbda1cf7de0a92cdfeb5978f26547"
author artbio
date Mon, 07 Oct 2019 12:51:25 -0400
parents
children 160e35e432a0
comparison
equal deleted inserted replaced
-1:000000000000 0:8028521b6e4f
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 options(warn = -1)
5 library(RColorBrewer)
6 library(lattice)
7 library(latticeExtra)
8 library(grid)
9 library(gridExtra)
10 library(optparse)
11
12 option_list <- list(
13 make_option(c("-f", "--first_dataframe"), type="character", help="path to first dataframe"),
14 make_option("--first_plot_method", type = "character", help="How additional data should be plotted"),
15 make_option("--output_pdf", type = "character", help="path to the pdf file with plots")
16 )
17
18 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
19 args = parse_args(parser)
20
21 # data frames implementation
22
23 ## first table
24 Table = read.delim(args$first_dataframe, header=T, row.names=NULL)
25 colnames(Table)[1] <- "Dataset"
26 dropcol <- c("Strandness", "z.score") # not used by this Rscript and is dropped for backward compatibility
27 Table <- Table[,!(names(Table) %in% dropcol)]
28 if (args$first_plot_method == "Counts" | args$first_plot_method == "Size") {
29 Table <- within(Table, Counts[Polarity=="R"] <- (Counts[Polarity=="R"]*-1))
30 }
31 n_samples=length(unique(Table$Dataset))
32 samples = unique(Table$Dataset)
33 genes=unique(Table$Chromosome)
34 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x))
35 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) )
36 n_genes=length(per_gene_readmap)
37
38 ## functions
39 plot_unit = function(df, method=args$first_plot_method, ...) {
40 p = xyplot(Counts~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
41 data=df,
42 type='h',
43 lwd=1.5,
44 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)),
45 xlab=NULL, main=NULL, ylab=NULL,
46 as.table=T,
47 origin = 0,
48 horizontal=FALSE,
49 group=Polarity,
50 col=c("red","blue"),
51 par.strip.text = list(cex=0.7),
52 ...)
53 p=combineLimits(p)
54 }
55
56 ## function parameters
57 par.settings.firstplot = list(layout.heights=list(top.padding=-2, bottom.padding=-2),strip.background=list(col=c("lightblue","lightgreen")))
58 title_first_method = list(Counts="Read Counts", Coverage="Coverage depths", Median="Median sizes", Mean="Mean sizes", Size="Size Distributions")
59 legend_first_method =list(Counts="Read count", Coverage="Coverage depth", Median="Median size", Mean="Mean size", Size="Read count")
60 bottom_first_method =list(Counts="Coordinates (nucleotides)",Coverage="Coordinates (nucleotides)", Median="Coordinates (nucleotides)", Mean="Coordinates (nucleotides)", Size="Sizes of reads")
61
62 ## Plotting Functions
63 single_plot <- function(...) {
64 width = 8.2677 * n_samples / 2
65 rows_per_page=8
66 graph_heights=c(rep(40,8),10)
67 pdf(file=args$output_pdf, paper="special", height=15, width=width)
68 for (i in seq(1,n_genes,rows_per_page)) {
69 start=i
70 end=i+rows_per_page-1
71 if (end>n_genes) {end=n_genes}
72 if (end-start+1 < 8) {graph_heights=c(rep(c(40),end-start+1),10,rep(c(40),8-(end-start+1)))}
73 first_plot.list = lapply(per_gene_readmap[start:end], function(x) update(useOuterStrips(plot_unit(x, par.settings=par.settings.firstplot),strip.left=strip.custom(par.strip.text = list(cex=0.5)))))
74 plot.list=rbind(first_plot.list)
75 args_list=c(plot.list, list( nrow=rows_per_page+1, ncol=1, heights=unit(graph_heights, rep("mm", 9)),
76 top=textGrob("Cluster Read Counts (Peaks in middle of clusters)", gp=gpar(cex=1), vjust=0, just="top"),
77 left=textGrob("Read Counts", gp=gpar(cex=1), vjust=0, hjust=0, x=1, y=(-0.41/7)*(end-start-(6.23/0.41)), rot=90),
78 sub=textGrob("Coordinates (nucleotides)", gp=gpar(cex=1), just="bottom", vjust=2)
79 )
80 )
81 do.call(grid.arrange, args_list)
82 }
83 devname=dev.off()
84 }
85
86 # main
87 single_plot()
88