Mercurial > repos > moheydarian > ggplot_histogram
comparison ggplotscripthistogram.R @ 1:72abdc872e49 draft
Uploaded
| author | moheydarian |
|---|---|
| date | Wed, 22 Feb 2017 15:49:25 -0500 |
| parents | |
| children | 097e19ec0150 |
comparison
equal
deleted
inserted
replaced
| 0:d0945de2fc3a | 1:72abdc872e49 |
|---|---|
| 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 'output', 'o', 2, 'character' | |
| 40 ), byrow=TRUE, ncol=4); | |
| 41 | |
| 42 # Parse options | |
| 43 options = getopt(option_specification); | |
| 44 | |
| 45 | |
| 46 | |
| 47 # Print options to see what is going on | |
| 48 cat("\n input: ",options$input) | |
| 49 cat("\n title: ",options$title) | |
| 50 cat("\n size: ",options$size) | |
| 51 cat("\n transform: ",options$alpha) | |
| 52 cat("\n xlab: ",options$xlab) | |
| 53 cat("\n xlab: ",options$xlab) | |
| 54 cat("\n binwidth: ",options$binwidth) | |
| 55 cat("\n output: ",options$output) | |
| 56 | |
| 57 integrated <- read.csv(options$input,sep='\t',header=TRUE) | |
| 58 input <- melt(integrated) | |
| 59 | |
| 60 | |
| 61 #Choose between automatically scaled x and y axis or user defined | |
| 62 if(options$scaling == "Automatic"){ | |
| 63 gg_scalex = NULL | |
| 64 gg_scaley = NULL | |
| 65 } else { | |
| 66 gg_scalex = xlim(options$xaxismin,options$xaxismax) | |
| 67 gg_scaley = ylim(options$yaxismin,options$yaxismax) | |
| 68 cat("\n xaxismin: ",options$xaxismin) | |
| 69 cat("\n xaxismax: ",options$xaxismax) | |
| 70 cat("\n yaxismin: ",options$yaxismin) | |
| 71 cat("\n yaxismax: ",options$yaxismax) | |
| 72 } | |
| 73 | |
| 74 #Choose dimensions of output pdf | |
| 75 if(options$dim == "Default"){ | |
| 76 gg_width = 7 | |
| 77 gg_height = 7 | |
| 78 } else { | |
| 79 gg_width = options$woutputdim | |
| 80 gg_height = options$houtputdim | |
| 81 } | |
| 82 | |
| 83 | |
| 84 if(options$transform == "log2"){ | |
| 85 input["value"] <- log2(input["value"]) | |
| 86 }else if(options$transform == "log2plus1"){ | |
| 87 input["value"] <- log2(input["value"]+1) | |
| 88 }else if(options$transform == "log10"){ | |
| 89 input["value"] <- log10(input["value"]) | |
| 90 }else if(options$transform == "log10plus1"){ | |
| 91 input["value"] <- log10(input["value"]+1) | |
| 92 }else{ | |
| 93 } | |
| 94 | |
| 95 | |
| 96 if(options$facet == "facet"){ | |
| 97 gg_facet = facet_wrap(~ variable) | |
| 98 } else { | |
| 99 gg_facet = NULL | |
| 100 } | |
| 101 | |
| 102 if(options$colorscheme == "Default"){ | |
| 103 gg_colorscale = NULL | |
| 104 } else { | |
| 105 gg_colorscale = scale_color_brewer(palette=options$colors, direction=options$colororder) | |
| 106 } | |
| 107 | |
| 108 ggplot(input,aes(value,color=variable)) + | |
| 109 geom_freqpoly(binwidth=options$binwidth,size=options$size)+gg_facet+gg_colorscale+ | |
| 110 gg_scalex+gg_scaley+theme_bw()+xlab(options$xlab)+ylab(options$ylab)+ | |
| 111 ggtitle(options$title) | |
| 112 | |
| 113 ggsave(file=options$output) |
