Package Bio :: Package Graphics
[hide private]
[frames] | no frames]

Source Code for Package Bio.Graphics

 1  # Copyright 2008 by Michiel de Hoon.  All rights reserved. 
 2  # Copyright 2008-2013 by Peter Cock.  All rights reserved. 
 3  # This code is part of the Biopython distribution and governed by its 
 4  # license.  Please see the LICENSE file that should have been included 
 5  # as part of this package. 
 6  ################################################################################ 
 7  """Bio.Graphics offers several graphical outputs, all using ReportLab.""" 
 8   
 9  # Check if ReportLab is installed. 
10  try: 
11      import reportlab as r 
12      del r 
13  except ImportError: 
14      from Bio import MissingPythonDependencyError 
15      raise MissingPythonDependencyError( 
16          "Please install ReportLab if you want " 
17          "to use Bio.Graphics. You can find ReportLab at " 
18          "http://www.reportlab.com/software/opensource/") 
19   
20   
21  # The following code is to allow all the Bio.Graphics 
22  # code to deal with the different ReportLab renderers 
23  # and the API quirks consistently. 
24 -def _write(drawing, output_file, format, dpi=72):
25 """Helper function to standardize output to files (PRIVATE). 26 27 Writes the provided drawing out to a file in a prescribed format. 28 29 drawing - suitable ReportLab drawing object. 30 output_file - a handle to write to, or a filename to write to. 31 format - String indicating output format, one of PS, PDF, SVG, 32 or provided the ReportLab renderPM module is installed, 33 one of the bitmap formats JPG, BMP, GIF, PNG, TIFF or TIFF. 34 The format can be given in any case. 35 dpi - Resolution (dots per inch) for bitmap formats. 36 37 No return value. 38 """ 39 from reportlab.graphics import renderPS, renderPDF, renderSVG 40 try: 41 from reportlab.graphics import renderPM 42 except ImportError: 43 #This is an optional part of ReportLab, so may not be installed. 44 #We'll raise a missing dependency error if rendering to a 45 #bitmap format is attempted. 46 renderPM=None 47 48 formatdict = {'PS': renderPS, 'EPS': renderPS, 49 # not sure which you actually get, PS or EPS, but 50 # GenomeDiagram used PS while other modules used EPS. 51 'PDF': renderPDF, 52 'SVG': renderSVG, 53 'JPG': renderPM, 54 'BMP': renderPM, 55 'GIF': renderPM, 56 'PNG': renderPM, 57 'TIFF': renderPM, 58 'TIF': renderPM 59 } 60 try: 61 #If output is not a string, then .upper() will trigger 62 #an attribute error... 63 drawmethod = formatdict[format.upper()] # select drawing method 64 except (KeyError, AttributeError): 65 raise ValueError("Output format should be one of %s" 66 % ", ".join(formatdict)) 67 68 if drawmethod is None: 69 #i.e. We wanted renderPM but it isn't installed 70 #See the import at the top of the function. 71 from Bio import MissingPythonDependencyError 72 raise MissingPythonDependencyError( 73 "Please install ReportLab's renderPM module") 74 75 if drawmethod == renderPM: 76 #This has a different API to the other render objects 77 return drawmethod.drawToFile(drawing, output_file, 78 format, dpi=dpi) 79 else: 80 return drawmethod.drawToFile(drawing, output_file)
81