Mercurial > repos > moheydarian > ggplot_histogram
comparison ggplotscripthistogram.R @ 31:0d872b4dfca1 draft default tip
Uploaded
| author | moheydarian |
|---|---|
| date | Tue, 11 Apr 2017 19:15:18 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 30:240d9329bf4b | 31:0d872b4dfca1 |
|---|---|
| 1 # Setup R error handling to go to stderr | |
| 2 options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)}) | |
| 3 | |
| 4 # We need to not crash galaxy with an UTF8 error on German LC settings. | |
| 5 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
| 6 | |
| 7 | |
| 8 # Import library | |
| 9 library("getopt") | |
| 10 library("reshape2") | |
| 11 library("ggplot2") | |
| 12 options(stringAsfactors = FALSE, useFancyQuotes = FALSE) | |
| 13 # Take in trailing command line arguments | |
| 14 args <- commandArgs(trailingOnly = TRUE) | |
| 15 | |
| 16 | |
| 17 # get options, using the spec as defined by the enclosed list. | |
| 18 # we read the options from the default: commandArgs(TRUE). | |
| 19 option_specification = matrix(c( | |
| 20 'input', 'i', 2, 'character', | |
| 21 'title', 't',2, 'character', | |
| 22 'size', 's', 2, 'double', | |
| 23 'xlab', 'x', 2, 'character', | |
| 24 'ylab', 'y', 2, 'character', | |
| 25 'binwidth', 'b', 2, 'double', | |
| 26 'xaxismin', 'e', 2, 'integer', | |
| 27 'xaxismax', 'f', 2, 'integer', | |
| 28 'yaxismin', 'g', 2, 'integer', | |
| 29 'yaxismax', 'h', 2, 'integer', | |
| 30 'colors', 'q', 2, 'character', | |
| 31 'colorscheme', 'z', 2, 'character', | |
| 32 'colororder', 'r', 2, 'integer', | |
| 33 'facet', 'a', 2, 'character', | |
| 34 'transform', 'c', 2, 'character', | |
| 35 'woutputdim', 'w', 2, 'integer', | |
| 36 'houtputdim', 'd', 2, 'integer', | |
| 37 'dim', 'k', 2, 'character', | |
| 38 'scaling', 'j', 2, 'character', | |
| 39 'legend', 'l', 2, 'character', | |
| 40 'density', 'm', 2, 'character', | |
| 41 'output', 'o', 2, 'character' | |
| 42 ), byrow=TRUE, ncol=4); | |
| 43 | |
| 44 # Parse options | |
| 45 options = getopt(option_specification); | |
| 46 | |
| 47 | |
| 48 | |
| 49 # Print options to see what is going on | |
| 50 cat("\n input: ",options$input) | |
| 51 cat("\n title: ",options$title) | |
| 52 cat("\n size: ",options$size) | |
| 53 cat("\n transform: ",options$alpha) | |
| 54 cat("\n xlab: ",options$xlab) | |
| 55 cat("\n xlab: ",options$xlab) | |
| 56 cat("\n binwidth: ",options$binwidth) | |
| 57 cat("\n output: ",options$output) | |
| 58 | |
| 59 integrated <- read.csv(options$input,sep='\t',header=TRUE) | |
| 60 input <- melt(integrated) | |
| 61 | |
| 62 #Show/hide legend | |
| 63 if(options$legend == "yes"){ | |
| 64 gg_legend = NULL | |
| 65 } else { | |
| 66 gg_legend = theme(legend.position="none") | |
| 67 } | |
| 68 | |
| 69 #density | |
| 70 if(options$density == "counts"){ | |
| 71 gg_density = ggplot(input,aes(value, color=variable)) | |
| 72 gg_freq = NULL | |
| 73 }else if(options$density == "nfreq"){ | |
| 74 gg_density = ggplot(input,aes(value, ..ncount.., color=variable)) | |
| 75 gg_freq = NULL | |
| 76 }else if(options$density == "freq"){ | |
| 77 gg_density = ggplot(input,aes(value, color=variable)) | |
| 78 gg_freq = aes(y=..count../sum(..count..)) | |
| 79 }else { | |
| 80 | |
| 81 } | |
| 82 | |
| 83 #Choose between automatically scaled x and y axis or user defined | |
| 84 if(options$scaling == "Automatic"){ | |
| 85 gg_scalex = NULL | |
| 86 gg_scaley = NULL | |
| 87 } else { | |
| 88 gg_scalex = xlim(options$xaxismin,options$xaxismax) | |
| 89 gg_scaley = ylim(options$yaxismin,options$yaxismax) | |
| 90 cat("\n xaxismin: ",options$xaxismin) | |
| 91 cat("\n xaxismax: ",options$xaxismax) | |
| 92 cat("\n yaxismin: ",options$yaxismin) | |
| 93 cat("\n yaxismax: ",options$yaxismax) | |
| 94 } | |
| 95 | |
| 96 #Choose dimensions of output pdf | |
| 97 if(options$dim == "Default"){ | |
| 98 gg_width = 7 | |
| 99 gg_height = 7 | |
| 100 } else { | |
| 101 gg_width = options$woutputdim | |
| 102 gg_height = options$houtputdim | |
| 103 } | |
| 104 | |
| 105 | |
| 106 if(options$transform == "log2"){ | |
| 107 input["value"] <- log2(input["value"]) | |
| 108 }else if(options$transform == "log2plus1"){ | |
| 109 input["value"] <- log2(input["value"]+1) | |
| 110 }else if(options$transform == "log10"){ | |
| 111 input["value"] <- log10(input["value"]) | |
| 112 }else if(options$transform == "log10plus1"){ | |
| 113 input["value"] <- log10(input["value"]+1) | |
| 114 }else{ | |
| 115 } | |
| 116 | |
| 117 | |
| 118 if(options$facet == "facet"){ | |
| 119 gg_facet = facet_wrap(~ variable) | |
| 120 } else { | |
| 121 gg_facet = NULL | |
| 122 } | |
| 123 | |
| 124 if(options$colorscheme == "Default"){ | |
| 125 gg_colorscale = NULL | |
| 126 } else { | |
| 127 gg_colorscale = scale_color_brewer(palette=options$colors, direction=options$colororder) | |
| 128 } | |
| 129 | |
| 130 gg_density + | |
| 131 geom_freqpoly(gg_freq,binwidth=options$binwidth,size=options$size)+gg_facet+gg_colorscale+ | |
| 132 gg_scalex+gg_scaley+theme_bw()+xlab(options$xlab)+ylab(options$ylab)+gg_legend+ | |
| 133 ggtitle(options$title) | |
| 134 | |
| 135 ggsave(file=options$output) |
