comparison barplot.r @ 7:3bddd7ab96e3 draft

"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/sr_bowtie_dataset_annotation commit 1010eff91c8ce7415d50adaf2f83ecc278295df8"
author artbio
date Sun, 24 Oct 2021 23:52:11 +0000
parents 8829656d6999
children fd4a60fc3fca
comparison
equal deleted inserted replaced
6:8829656d6999 7:3bddd7ab96e3
1 if (length(commandArgs(TRUE)) == 0) { 1 if (length(commandArgs(TRUE)) == 0) {
2 system("Rscript barplot.r -h", intern = F) 2 system("Rscript barplot.r -h", intern = F)
3 q("no") 3 q("no")
4 } 4 }
5 5
6
7 # load packages that are provided in the conda env 6 # load packages that are provided in the conda env
8 options( show.error.messages=F, 7 options(show.error.messages = F,
9 error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) 8 error = function() {
9 cat(geterrmessage(), file = stderr())
10 q("no", 1, F)
11 }
12 )
10 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") 13 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
11 warnings() 14 warnings()
12 library(optparse) 15 library(optparse)
13 library(ggplot2) 16 library(ggplot2)
14 library(ggrepel) 17 library(ggrepel)
15 library(RColorBrewer) 18 library(RColorBrewer)
16 19
17 20 option_list <- list(
18 #Arguments
19 option_list = list(
20 make_option( 21 make_option(
21 c("-i", "--input"), 22 c("-i", "--input"),
22 default = NA, 23 default = NA,
23 type = 'character', 24 type = "character",
24 help = "Input file that contains count data (no header)" 25 help = "Input file that contains count data (no header)"
25 ), 26 ),
26 make_option( 27 make_option(
27 c("-o", "--barplot"), 28 c("-o", "--barplot"),
28 default = NA, 29 default = NA,
29 type = 'character', 30 type = "character",
30 help = "PDF output file" 31 help = "PDF output file"
31 ) 32 )
32 ) 33 )
33 34
34 opt = parse_args(OptionParser(option_list = option_list), 35 opt <- parse_args(OptionParser(option_list = option_list),
35 args = commandArgs(trailingOnly = TRUE)) 36 args = commandArgs(trailingOnly = TRUE))
36 37
37 38 annotations <- read.delim(opt$input, header = F)
38 ## 39 colnames(annotations) <- c("sample", "class", "percent_of_reads", "total")
39 annotations = read.delim(opt$input, header=F) 40 annotations$percent <- round(annotations$percent_of_reads / annotations$total * 100,
40 colnames(annotations) = c("sample", "class", "percent_of_reads", "total") 41 digits = 2)
41 annotations$percent=round(annotations$percent_of_reads/annotations$total*100, digits=2) 42 ## ggplot2 plotting
42 # ggplot2 plotting
43 43
44 # Define the number of colors you want 44 # Define the number of colors you want
45 Sasha.Trubetskoy.Palette <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', 45 mycolors <- c("#e6194b", "#3cb44b", "#ffe119", "#4363d8", "#f58231",
46 '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', 46 "#911eb4", "#46f0f0", "#f032e6", "#bcf60c",
47 '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', 47 "#008080", "#e6beff", "#9a6324", "#fffac8", "#800000",
48 '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080') 48 "#aaffc3", "#808000", "#ffd8b1", "#000075", "#808080")
49 nb.cols <- 19 # 10 with colorRampPalette 49 ggtitle("Class proportions")
50 # mycolors <- colorRampPalette(brewer.pal(8, "Paired"))(nb.cols) 50 ggplot(annotations, aes(x = total / 2, y = percent_of_reads, fill = class, width = total)) +
51 mycolors <- Sasha.Trubetskoy.Palette[1:nb.cols] 51 geom_bar(position = "fill", stat = "identity") +
52 52 facet_wrap(~sample, ncol = 3) +
53 ggtitle('Class proportions') 53 geom_label_repel(aes(label = percent), position = position_fill(vjust = 0.5),
54 ggplot(annotations, aes(x=total/2, y = percent_of_reads, fill = class, width = total)) + 54 size = 2, show.legend = F) +
55 geom_bar(position="fill", stat="identity") + 55 coord_polar(theta = "y") +
56 facet_wrap(~sample, ncol=3 ) +
57 geom_label_repel(aes(label = percent), position = position_fill(vjust = 0.5), size=2,show.legend = F) +
58 coord_polar(theta="y") +
59 labs(x = "Class fractions (%)") + 56 labs(x = "Class fractions (%)") +
60 scale_fill_manual(values = mycolors) + 57 scale_fill_manual(values = mycolors) +
61 theme(axis.text = element_blank(), 58 theme(axis.text = element_blank(),
62 axis.ticks = element_blank(), 59 axis.ticks = element_blank(),
63 panel.grid = element_blank(), 60 panel.grid = element_blank(),
64 axis.title.y = element_blank(), 61 axis.title.y = element_blank(),
65 legend.position="bottom") + 62 legend.position = "bottom") +
66 geom_text(aes(x = total/2, y= .5, label = paste(round(total/1000000, digits=3), "M"), vjust = 4, hjust=-1), size=2) 63 geom_text(aes(x = total / 2, y = .5, label = paste(round(total / 1000000, digits = 3),
67 ggsave(file=opt$barplot, device="pdf") 64 "M"),
68 65 vjust = 4, hjust = -1), size = 2)
66 ggsave(file = opt$barplot, device = "pdf")