comparison SMART/Java/Python/GetReadSizes.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents 769e306b7933
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
46 self.xLab = "Size" 46 self.xLab = "Size"
47 self.yLab = "# reads" 47 self.yLab = "# reads"
48 self.verbosity = verbosity 48 self.verbosity = verbosity
49 self.number = random.randint(0, 100000) 49 self.number = random.randint(0, 100000)
50 self.log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity) 50 self.log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
51 self.parsers = {} 51 self.parsers = {}
52 self.sizes = {} 52 self.sizes = {}
53 self.factors = {} 53 self.factors = {}
54 self.regions = None 54 self.regions = None
55 self.tmpDatName = None 55 self.tmpDatName = None
56 self.tmpRName = None 56 self.tmpRName = None
57 self.width = 800 57 self.width = 800
58 self.height = 300 58 self.height = 300
59 self.arial = False
59 60
60 def setNames(self, names): 61 def setNames(self, names):
61 self.names = names 62 self.names = names
62 63
63 def setInputFiles(self, fileNames, format): 64 def setInputFiles(self, fileNames, format):
90 def setImageSize(self, width, height): 91 def setImageSize(self, width, height):
91 if width != None: 92 if width != None:
92 self.width = width 93 self.width = width
93 if height != None: 94 if height != None:
94 self.height = height 95 self.height = height
96
97 def setArial(self, arial):
98 self.arial = arial
95 99
96 def _checkOptions(self): 100 def _checkOptions(self):
97 if not self.parsers: 101 if not self.parsers:
98 self.logAndRaise("ERROR: Missing input file names") 102 self.logAndRaise("ERROR: Missing input file names")
99 103
172 176
173 def _writeScript(self, region): 177 def _writeScript(self, region):
174 self.tmpRName = "tmpFile%d.R" % (self.number) 178 self.tmpRName = "tmpFile%d.R" % (self.number)
175 fileName = self.outputFileName if region == DEFAULT_REGION else "%s_%s.png" % (os.path.splitext(self.outputFileName)[0], region) 179 fileName = self.outputFileName if region == DEFAULT_REGION else "%s_%s.png" % (os.path.splitext(self.outputFileName)[0], region)
176 colors = "scale_fill_brewer(palette=\"Set1\")" if self.colors == None else "scale_fill_manual(values = c(%s))" % (", ".join(["\"%s\"" % (color) for color in self.colors])) 180 colors = "scale_fill_brewer(palette=\"Set1\")" if self.colors == None else "scale_fill_manual(values = c(%s))" % (", ".join(["\"%s\"" % (color) for color in self.colors]))
177 title = "" if region == DEFAULT_REGION else " of %s" % (region) 181 title = "" if region == DEFAULT_REGION else " + labs(title = \"Sizes of %s\")" % (region)
178 handle = open(self.tmpRName, "w") 182 handle = open(self.tmpRName, "w")
183 arial = ", text = element_text(family=\"Arial\", size=20)" if self.arial else ""
184 if self.arial:
185 handle.write("library(extrafont)\nloadfonts()\n")
179 handle.write("library(ggplot2)\n") 186 handle.write("library(ggplot2)\n")
180 handle.write("data <- read.table(\"%s\", header = T)\n" % (self.tmpDatName)) 187 handle.write("data <- read.table(\"%s\", header = T)\n" % (self.tmpDatName))
181 handle.write("data$Sample <- factor(data$Sample, levels=c(%s))\n" % (", ".join(["\"%s\"" % (name) for name in self.names]))) 188 handle.write("data$Sample <- factor(data$Sample, levels=c(%s))\n" % (", ".join(["\"%s\"" % (name) for name in self.names])))
182 handle.write("data$Size <- factor(data$Size, levels=c(%s))\n" % (", ".join(["%d" % (size) for size in range(self.minSize, self.maxSize+1)]))) 189 handle.write("data$Size <- factor(data$Size, levels=c(%s))\n" % (", ".join(["%d" % (size) for size in range(self.minSize, self.maxSize+1)])))
183 handle.write("png(\"%s\", width = %d, height = %d)\n" % (fileName, self.width, self.height)) 190 handle.write("png(\"%s\", width = %d, height = %d)\n" % (fileName, self.width, self.height))
184 handle.write("ggplot(data, aes(x = Size, y = Count, fill = Size)) + opts(title = \"Size distribution%s\") + geom_bar(stat = \"identity\") + facet_grid(. ~ Sample, space=\"free_x\") + xlab(\"%s\") + ylab(\"%s\") + %s + opts(legend.position = \"none\", panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), panel.background = theme_blank())\n" % (title, self.xLab, self.yLab, colors)) 191 handle.write("ggplot(data, aes(x = Size, y = Count, fill = Size)) %s + geom_bar(stat = \"identity\") + facet_grid(. ~ Sample, space=\"free_x\") + xlab(\"%s\") + ylab(\"%s\") + %s + theme(legend.position = \"none\", panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()%s)\n" % (title, self.xLab, self.yLab, colors, arial))
185 handle.write("dev.off()\n") 192 handle.write("dev.off()\n")
186 193
187 def _runR(self): 194 def _runR(self):
188 rCommand = "R" 195 rCommand = os.environ["SMARTRPATH"] if "SMARTRPATH" in os.environ else "R"
189 if "SMARTRPATH" in os.environ: 196 command = "\"%s\" CMD BATCH %s" % (rCommand, self.tmpRName)
190 rCommand = os.environ["SMARTRPATH"] 197 status = subprocess.call(command, shell=True)
191 command = "\"%s\" CMD BATCH %s" % (rCommand, self.tmpRName)
192 status = subprocess.call(command, shell=True)
193 if status != 0: 198 if status != 0:
194 raise Exception("Problem with the execution of script file %s, status is: %s" % (self.tmpRName, status)) 199 raise Exception("Problem with the execution of script file %s, status is: %s" % (self.tmpRName, status))
195 200
196 def _plot(self): 201 def _plot(self):
197 progress = Progress(len(self.sizes), "Plotting data", self.verbosity) 202 progress = Progress(len(self.sizes), "Plotting data", self.verbosity)
238 parser.add_option("-c", "--colors", dest="colors", action="store", default=None, type="string", help="colors of the bars, separated by commas [format: string]") 243 parser.add_option("-c", "--colors", dest="colors", action="store", default=None, type="string", help="colors of the bars, separated by commas [format: string]")
239 parser.add_option("-a", "--factors", dest="factors", action="store", default=None, type="string", help="normalization factors, separated by commas [format: string]") 244 parser.add_option("-a", "--factors", dest="factors", action="store", default=None, type="string", help="normalization factors, separated by commas [format: string]")
240 parser.add_option("-r", "--regions", dest="regionsFileName", action="store", default=None, type="string", help="regions to plot [format: transcript file in GFF format]") 245 parser.add_option("-r", "--regions", dest="regionsFileName", action="store", default=None, type="string", help="regions to plot [format: transcript file in GFF format]")
241 parser.add_option("-z", "--width", dest="width", action="store", default=800, type="int", help="width of the image [format: int] [default: 800]") 246 parser.add_option("-z", "--width", dest="width", action="store", default=800, type="int", help="width of the image [format: int] [default: 800]")
242 parser.add_option("-Z", "--height", dest="height", action="store", default=300, type="int", help="height of the image [format: int] [default: 300]") 247 parser.add_option("-Z", "--height", dest="height", action="store", default=300, type="int", help="height of the image [format: int] [default: 300]")
248 parser.add_option("-A", "--arial", dest="arial", action="store_true", default=False, help="use Arial font [format: boolean] [default: false]")
243 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]") 249 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
244 options = parser.parse_args()[0] 250 options = parser.parse_args()[0]
245 iGetReadSizes = GetReadSizes(options.verbosity) 251 iGetReadSizes = GetReadSizes(options.verbosity)
246 iGetReadSizes.setNames(options.names.split(",")) 252 iGetReadSizes.setNames(options.names.split(","))
247 iGetReadSizes.setInputFiles(options.inputFileNames.split(","), options.format) 253 iGetReadSizes.setInputFiles(options.inputFileNames.split(","), options.format)
250 iGetReadSizes.setSizes(options.minSize, options.maxSize) 256 iGetReadSizes.setSizes(options.minSize, options.maxSize)
251 iGetReadSizes.setColors(None if options.colors == None else options.colors.split(",")) 257 iGetReadSizes.setColors(None if options.colors == None else options.colors.split(","))
252 iGetReadSizes.setFactors(None if options.factors == None else map(float, options.factors.split(","))) 258 iGetReadSizes.setFactors(None if options.factors == None else map(float, options.factors.split(",")))
253 iGetReadSizes.setRegionsFile(options.regionsFileName) 259 iGetReadSizes.setRegionsFile(options.regionsFileName)
254 iGetReadSizes.setImageSize(options.width, options.height) 260 iGetReadSizes.setImageSize(options.width, options.height)
261 iGetReadSizes.setArial(options.arial)
255 iGetReadSizes.run() 262 iGetReadSizes.run()