Back to index

python-biopython  1.60
__init__.py
Go to the documentation of this file.
00001 # Copyright 2008 by Michiel de Hoon.  All rights reserved.
00002 # Copyright 2008-2009 by Peter Cock.  All rights reserved.
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 ################################################################################
00007 """Bio.Graphics offsers several graphical ouputs, all using ReportLab."""
00008 
00009 # Check if ReportLab is installed.
00010 try:
00011     import reportlab as r
00012     del r
00013 except ImportError:
00014     from Bio import MissingPythonDependencyError
00015     raise MissingPythonDependencyError( \
00016         "Please install ReportLab if you want "
00017         "to use Bio.Graphics. You can find ReportLab at "
00018         "http://www.reportlab.org/downloads.html")
00019 
00020 # The following code is to allow all the Bio.Graphics
00021 # code to deal with the different ReportLab renderers
00022 # and the API quirks consistently.
00023 def _write(drawing, output_file, format, dpi=72):
00024     """Helper function to standize output to files (PRIVATE).
00025 
00026     Writes the provided drawing out to a file in a prescribed format.
00027 
00028     drawing - suitable ReportLab drawing object.
00029     output_file - a handle to write to, or a filename to write to.
00030     format - String indicating output format, one of PS, PDF, SVG,
00031              or provided the ReportLab renderPM module is installed,
00032              one of the bitmap formats JPG, BMP, GIF, PNG, TIFF or TIFF.
00033              The format can be given in any case.
00034     dpi - Resolution (dots per inch) for bitmap formats.
00035 
00036     No return value.
00037     """
00038     from reportlab.graphics import renderPS, renderPDF, renderSVG
00039     try:
00040         from reportlab.graphics import renderPM
00041     except ImportError:
00042         #This is an optional part of ReportLab, so may not be installed.
00043         #We'll raise a missing dependency error if rendering to a
00044         #bitmap format is attempted.
00045         renderPM=None
00046 
00047     formatdict = {'PS': renderPS, 'EPS': renderPS,
00048                   # not sure which you actually get, PS or EPS, but
00049                   # GenomeDiagram used PS while other modules used EPS.
00050                   'PDF': renderPDF,
00051                   'SVG': renderSVG,
00052                   'JPG': renderPM,
00053                   'BMP': renderPM,
00054                   'GIF': renderPM,
00055                   'PNG': renderPM,
00056                   'TIFF': renderPM,
00057                   'TIF': renderPM
00058                  }
00059     try:
00060         #If output is not a string, then .upper() will trigger
00061         #an attribute error...
00062         drawmethod = formatdict[format.upper()] # select drawing method
00063     except (KeyError,AttributeError):
00064         raise ValueError("Output format should be one of %s" \
00065                          % ", ".join(formatdict))
00066 
00067     if drawmethod is None:
00068         #i.e. We wanted renderPM but it isn't installed
00069         #See the import at the top of the function.
00070         from Bio import MissingPythonDependencyError
00071         raise MissingPythonDependencyError(
00072             "Please install ReportLab's renderPM module")
00073 
00074     if drawmethod == renderPM:
00075         #This has a different API to the other render objects
00076         return drawmethod.drawToFile(drawing, output_file,
00077                                      format, dpi=dpi)
00078     else:
00079         return drawmethod.drawToFile(drawing, output_file)