comparison small_rna_clusters.r @ 1:160e35e432a0 draft default tip

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