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

Source Code for Module Bio.Graphics.Comparative

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4  # 
  5   
  6  """Plots to compare information between different sources. 
  7   
  8  This file contains high level plots which are designed to be used to 
  9  compare different types of information. The most basic example is comparing 
 10  two variables in a traditional scatter plot. 
 11  """ 
 12  # reportlab 
 13  from reportlab.lib import colors 
 14  from reportlab.graphics.charts.lineplots import LinePlot 
 15  from reportlab.lib.pagesizes import letter 
 16  from reportlab.lib.units import inch 
 17   
 18  from reportlab.graphics.shapes import Drawing, String 
 19  from reportlab.graphics.charts.markers import makeEmptySquare, makeFilledSquare 
 20  from reportlab.graphics.charts.markers import makeFilledDiamond, makeSmiley 
 21  from reportlab.graphics.charts.markers import makeFilledCircle, makeEmptyCircle 
 22   
 23  from Bio.Graphics import _write 
 24   
 25   
26 -class ComparativeScatterPlot(object):
27 """Display a scatter-type plot comparing two different kinds of info. 28 29 Attributes; 30 - display_info - a 2D list of the information we'll be outputting. Each 31 top level list is a different data type, and each data point is a 32 two-tuple of the coordinates of a point. 33 34 So if you had two distributions of points, it should look like:: 35 36 display_info = [[(1, 2), (3, 4)], 37 [(5, 6), (7, 8)]] 38 39 If everything is just one set of points, display_info can look like:: 40 41 display_info = [[(1, 2), (3, 4), (5, 6)]] 42 """
43 - def __init__(self, output_format='pdf'):
44 # customizable attributes 45 self.number_of_columns = 1 46 self.page_size = letter 47 self.title_size = 20 48 49 self.output_format = output_format 50 51 # the information we'll be writing 52 self.display_info = [] 53 54 # initial colors and shapes used for drawing points 55 self.color_choices = [colors.red, colors.green, colors.blue, 56 colors.yellow, colors.orange, colors.black] 57 self.shape_choices = [makeFilledCircle, makeEmptySquare, 58 makeFilledDiamond, makeFilledSquare, 59 makeEmptyCircle, makeSmiley]
60
61 - def draw_to_file(self, output_file, title):
62 """Write the comparative plot to a file. 63 64 Arguments: 65 66 - output_file - The name of the file to output the information to, 67 or a handle to write to. 68 - title - A title to display on the graphic. 69 """ 70 width, height = self.page_size 71 cur_drawing = Drawing(width, height) 72 73 self._draw_title(cur_drawing, title, width, height) 74 75 start_x = inch * .5 76 end_x = width - inch * .5 77 end_y = height - 1.5 * inch 78 start_y = .5 * inch 79 self._draw_scatter_plot(cur_drawing, start_x, start_y, end_x, end_y) 80 81 return _write(cur_drawing, output_file, self.output_format)
82
83 - def _draw_title(self, cur_drawing, title, width, height):
84 """Add a title to the page we are outputting. 85 """ 86 title_string = String(width / 2, height - inch, title) 87 title_string.fontName = 'Helvetica-Bold' 88 title_string.fontSize = self.title_size 89 title_string.textAnchor = "middle" 90 91 cur_drawing.add(title_string)
92
93 - def _draw_scatter_plot(self, cur_drawing, x_start, y_start, 94 x_end, y_end):
95 """Draw a scatter plot on the drawing with the given coordinates.""" 96 scatter_plot = LinePlot() 97 98 # set the dimensions of the scatter plot 99 scatter_plot.x = x_start 100 scatter_plot.y = y_start 101 scatter_plot.width = abs(x_start - x_end) 102 scatter_plot.height = abs(y_start - y_end) 103 104 scatter_plot.data = self.display_info 105 106 scatter_plot.joinedLines = 0 107 108 # set the axes of the plot 109 x_min, x_max, y_min, y_max = self._find_min_max(self.display_info) 110 scatter_plot.xValueAxis.valueMin = x_min 111 scatter_plot.xValueAxis.valueMax = x_max 112 scatter_plot.xValueAxis.valueStep = (x_max - x_min) / 10.0 113 114 scatter_plot.yValueAxis.valueMin = y_min 115 scatter_plot.yValueAxis.valueMax = y_max 116 scatter_plot.yValueAxis.valueStep = (y_max - y_min) / 10.0 117 118 self._set_colors_and_shapes(scatter_plot, self.display_info) 119 120 cur_drawing.add(scatter_plot)
121
122 - def _set_colors_and_shapes(self, scatter_plot, display_info):
123 """Set the colors and shapes of the points displayed. 124 125 By default this just sets all of the points according to the order 126 of colors and shapes defined in self.color_choices and 127 self.shape_choices. The first 5 shapes and colors are unique, the 128 rest of them are just set to the same color and shape (since I 129 ran out of shapes!). 130 131 You can change how this function works by either changing the 132 values of the color_choices and shape_choices attributes, or 133 by inheriting from this class and overriding this function. 134 """ 135 for value_num in range(len(display_info)): 136 # if we have unique colors, add them 137 if (value_num + 1) < len(self.color_choices): 138 scatter_plot.lines[value_num].strokeColor = \ 139 self.color_choices[value_num] 140 scatter_plot.lines[value_num].symbol = \ 141 self.shape_choices[value_num] 142 # otherwise just use the last number 143 else: 144 scatter_plot.lines[value_num].strokeColor = \ 145 self.color_choices[-1] 146 scatter_plot.lines[value_num].symbol = \ 147 self.shape_choices[-1]
148
149 - def _find_min_max(self, info):
150 """Find min and max for x and y coordinates in the given data.""" 151 x_min = info[0][0][0] 152 x_max = info[0][0][0] 153 y_min = info[0][0][1] 154 y_max = info[0][0][1] 155 156 for two_d_list in info: 157 for x, y in two_d_list: 158 if x > x_max: 159 x_max = x 160 if x < x_min: 161 x_min = x 162 if y > y_max: 163 y_max = y 164 if y < y_min: 165 y_min = y 166 167 return x_min, x_max, y_min, y_max
168