comparison small_rna_maps.r @ 7:a96e6a7df2b7 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_maps commit 06472d1bd1365e4f7b385d578a69f4646481e22f
author artbio
date Tue, 10 Oct 2017 18:48:37 -0400
parents a3be3601bcb3
children a561a71bd7d7
comparison
equal deleted inserted replaced
6:a3be3601bcb3 7:a96e6a7df2b7
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 () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
4 # options(warn = -1) 4 options(warn = -1)
5 library(RColorBrewer) 5 library(RColorBrewer)
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
12 option_list <- list( 13 option_list <- list(
13 make_option(c("-f", "--first_dataframe"), type="character", help="path to first dataframe"), 14 make_option(c("-f", "--first_dataframe"), type="character", help="path to first dataframe"),
14 make_option(c("-e", "--extra_dataframe"), type="character", help="path to additional dataframe"), 15 make_option(c("-e", "--extra_dataframe"), type="character", help="path to additional dataframe"),
15 make_option(c("-n", "--normalization"), type="character", help="space-separated normalization/size factors"), 16 make_option(c("-n", "--normalization"), type="character", help="space-separated normalization/size factors"),
16 make_option("--first_plot_method", type = "character", help="How additional data should be plotted"), 17 make_option("--first_plot_method", type = "character", help="How additional data should be plotted"),
17 make_option("--extra_plot_method", type = "character", help="How additional data should be plotted"), 18 make_option("--extra_plot_method", type = "character", help="How additional data should be plotted"),
19 make_option("--global", type = "character", help="data should be plotted as global size distribution"),
18 make_option("--output_pdf", type = "character", help="path to the pdf file with plots") 20 make_option("--output_pdf", type = "character", help="path to the pdf file with plots")
19 ) 21 )
20 22
21 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) 23 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
22 args = parse_args(parser) 24 args = parse_args(parser)
35 norm_factors = rep(1, n_samples) 37 norm_factors = rep(1, n_samples)
36 } 38 }
37 if (args$first_plot_method == "Counts" | args$first_plot_method == "Size" | args$first_plot_method == "Coverage") { 39 if (args$first_plot_method == "Counts" | args$first_plot_method == "Size" | args$first_plot_method == "Coverage") {
38 i = 1 40 i = 1
39 for (sample in samples) { 41 for (sample in samples) {
40 print(norm_factors[i])
41 Table[, length(Table)][Table$Dataset==sample] <- Table[, length(Table)][Table$Dataset==sample]*norm_factors[i] 42 Table[, length(Table)][Table$Dataset==sample] <- Table[, length(Table)][Table$Dataset==sample]*norm_factors[i]
42 i = i + 1 43 i = i + 1
43 } 44 }
44 print(tail(Table))
45 } 45 }
46 genes=unique(levels(Table$Chromosome)) 46 genes=unique(levels(Table$Chromosome))
47 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x)) 47 per_gene_readmap=lapply(genes, function(x) subset(Table, Chromosome==x))
48 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) ) 48 per_gene_limit=lapply(genes, function(x) c(1, unique(subset(Table, Chromosome==x)$Chrom_length)) )
49 n_genes=length(per_gene_readmap) 49 n_genes=length(per_gene_readmap)
62 } 62 }
63 per_gene_size=lapply(genes, function(x) subset(ExtraTable, Chromosome==x)) 63 per_gene_size=lapply(genes, function(x) subset(ExtraTable, Chromosome==x))
64 } 64 }
65 65
66 ## functions 66 ## functions
67 67 globalbc = function(df, global="", ...) {
68 if (global == "yes") {
69 bc <- barchart(Counts~as.factor(Size)|factor(Dataset, levels=unique(Dataset)),
70 data = df, origin = 0,
71 horizontal=FALSE,
72 col=c("darkblue"),
73 scales=list(y=list(tick.number=4, rot=90, relation="free", cex=0.5, alternating=T), x=list(rot=0, cex=0.6, tck=0.5, alternating=c(3,3))),
74 xlab=list(label=bottom_first_method[[args$first_plot_method]], cex=.85),
75 ylab=list(label=legend_first_method[[args$first_plot_method]], cex=.85),
76 main=title_first_method[[args$first_plot_method]],
77 layout = c(2, 6), newpage=T,
78 as.table=TRUE,
79 aspect=0.5,
80 strip = strip.custom(par.strip.text = list(cex = 1), which.given=1, bg="lightblue"),
81 ...
82 )
83 } else {
84 bc <- barchart(Counts~as.factor(Size)|factor(Dataset, levels=unique(Dataset)),
85 data = df, origin = 0,
86 horizontal=FALSE,
87 group=Polarity,
88 stack=TRUE,
89 col=c('red', 'blue'),
90 scales=list(y=list(tick.number=4, rot=90, relation="free", cex=0.5, alternating=T), x=list(rot=0, cex=0.6, tck=0.5, alternating=c(3,3))),
91 xlab=list(label=bottom_first_method[[args$first_plot_method]], cex=.85),
92 ylab=list(label=legend_first_method[[args$first_plot_method]], cex=.85),
93 main=title_first_method[[args$first_plot_method]],
94 layout = c(2, 6), newpage=T,
95 as.table=TRUE,
96 aspect=0.5,
97 strip = strip.custom(par.strip.text = list(cex = 1), which.given=1, bg="lightblue"),
98 ...
99 )
100 }
101 return(bc)
102 }
68 plot_unit = function(df, method=args$first_plot_method, ...) { 103 plot_unit = function(df, method=args$first_plot_method, ...) {
69 if (method == 'Counts') { 104 if (method == 'Counts') {
70 p = xyplot(Counts~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)), 105 p = xyplot(Counts~Coordinate|factor(Dataset, levels=unique(Dataset))+factor(Chromosome, levels=unique(Chromosome)),
71 data=df, 106 data=df,
72 type='h', 107 type='h',
159 ylab=list(label=legend_first_method[[args$first_plot_method]], cex=.85), 194 ylab=list(label=legend_first_method[[args$first_plot_method]], cex=.85),
160 main=title_first_method[[args$first_plot_method]], 195 main=title_first_method[[args$first_plot_method]],
161 par.strip.text = list(cex=0.7), 196 par.strip.text = list(cex=0.7),
162 nrow = 8, 197 nrow = 8,
163 as.table=TRUE, 198 as.table=TRUE,
164
165 ...) 199 ...)
166 p = update(useOuterStrips(p, strip.left=strip.custom(par.strip.text = list(cex=0.5))), layout=c(n_samples, rows_per_page)) 200 p = update(useOuterStrips(p, strip.left=strip.custom(par.strip.text = list(cex=0.5))), layout=c(n_samples, rows_per_page))
167
168 p = combineLimits(p, extend=TRUE) 201 p = combineLimits(p, extend=TRUE)
169 return (p) 202 return (p)
170 } 203 }
171 } 204 }
172 205
226 } 259 }
227 260
228 # main 261 # main
229 262
230 if (args$extra_plot_method != '') { double_plot() } 263 if (args$extra_plot_method != '') { double_plot() }
231 if (args$extra_plot_method == '') { 264 if (args$extra_plot_method == '' & !exists('global', where=args)) {
232 single_plot() 265 single_plot()
233 } 266 }
234 267 if (exists('global', where=args)) {
235 268 pdf(file=args$output, paper="special", height=11.69)
236 269 Table <- within(Table, Counts[Polarity=="R"] <- abs(Counts[Polarity=="R"])) # retropedalage
237 270 library(reshape2)
238 271 ml = melt(Table, id.vars = c("Dataset", "Chromosome", "Polarity", "Size"))
239 272 if (args$global == "nomerge") {
240 273 castml = dcast(ml, Dataset+Polarity+Size ~ variable, function(x) sum(x))
241 274 castml <- within(castml, Counts[Polarity=="R"] <- (Counts[Polarity=="R"]*-1))
242 275 bc = globalbc(castml, global="no")
276 } else {
277 castml = dcast(ml, Dataset+Size ~ variable, function(x) sum(x))
278 bc = globalbc(castml, global="yes")
279 }
280 plot(bc)
281 devname=dev.off()
282 }
283
284
285
286
287
288
289
290
291
292