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

Source Code for Module Bio.Graphics.GenomeDiagram._GraphSet

  1  # Copyright 2003-2008 by Leighton Pritchard.  All rights reserved. 
  2  # Revisions copyright 2008-2010 by Peter Cock. 
  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  # Contact:       Leighton Pritchard, Scottish Crop Research Institute, 
  8  #                Invergowrie, Dundee, Scotland, DD2 5DA, UK 
  9  #                L.Pritchard@scri.ac.uk 
 10  ################################################################################ 
 11  # 
 12  # TODO: Make representation of Ymax and Ymin values at this level, so that 
 13  #       calculation of graph/axis drawing is simplified 
 14   
 15  """ GraphSet module 
 16   
 17      Provides: 
 18   
 19      o GraphSet - container for GraphData objects 
 20   
 21      For drawing capabilities, this module uses reportlab to draw and write 
 22      the diagram: 
 23   
 24      http://www.reportlab.com 
 25   
 26      For dealing with biological information, the package expects BioPython 
 27      objects: 
 28   
 29      http://www.biopython.org 
 30  """ 
 31   
 32  # ReportLab imports 
 33  from reportlab.lib import colors 
 34   
 35  from _Graph import GraphData 
 36   
 37   
38 -class GraphSet(object):
39 """ GraphSet 40 41 Provides: 42 43 Methods: 44 45 o __init__(self, set_id=None, name=None) Called on instantiation 46 47 o new_graph(self, data, name, style='bar', color=colors.lightgreen, 48 altcolor=colors.darkseagreen) Create new graph in the set 49 from the passed data, with the passed parameters 50 51 o del_graph(self, graph_id) Delete graph with the passed id 52 53 o get_graphs(self) Returns a list of all graphs 54 55 o get_ids(self) Returns a list of graph ids 56 57 o range(self) Returns the range covered by the graphs in the set 58 59 o to_string(self, verbose=0) Returns a string describing the set 60 61 o __len__(self) Returns the length of sequence covered by the set 62 63 o __getitem__(self, key) Returns the graph with the id of the passed key 64 65 o __str__(self) Returns a string describing the set 66 67 Attributes: 68 69 o id Unique identifier for the set 70 71 o name String describing the set 72 73 """
74 - def __init__(self, name=None):
75 """ __init__(self, name=None) 76 77 o name String identifying the graph set sensibly 78 """ 79 self.id = id # Unique identifier for the set 80 self._next_id = 0 # Holds unique ids for graphs 81 self._graphs = {} # Holds graphs, keyed by unique id 82 self.name = name # Holds description of graph
83
84 - def new_graph(self, data, name=None, style='bar', color=colors.lightgreen, 85 altcolor=colors.darkseagreen, linewidth=1, center=None, 86 colour=None, altcolour=None, centre=None):
87 """ new_graph(self, data, name=None, style='bar', color=colors.lightgreen, 88 altcolor=colors.darkseagreen) 89 90 o data List of (position, value) int tuples 91 92 o name String, description of the graph 93 94 o style String ('bar', 'heat', 'line') describing how the graph 95 will be drawn 96 97 o color colors.Color describing the color to draw all or 'high' 98 (some styles) data (overridden by backwards compatible 99 argument with UK spelling, colour). 100 101 o altcolor colors.Color describing the color to draw 'low' (some 102 styles) data (overridden by backwards compatible argument 103 with UK spelling, colour). 104 105 o linewidth Float describing linewidth for graph 106 107 o center Float setting the value at which the x-axis 108 crosses the y-axis (overridden by backwards 109 compatible argument with UK spelling, centre) 110 111 Add a GraphData object to the diagram (will be stored 112 internally 113 """ 114 #Let the UK spelling (colour) override the USA spelling (color) 115 if colour is not None: 116 color = colour 117 if altcolour is not None: 118 altcolor = altcolour 119 if centre is not None: 120 center = centre 121 122 id = self._next_id # get id number 123 graph = GraphData(id, data, name, style, color, altcolor, center) 124 graph.linewidth = linewidth 125 self._graphs[id] = graph # add graph data 126 self._next_id += 1 # increment next id 127 return graph
128
129 - def del_graph(self, graph_id):
130 """ del_graph(self, graph_id) 131 132 o graph_id Identifying value of the graph 133 134 Remove a graph from the set, indicated by its id 135 """ 136 del self._graphs[graph_id]
137
138 - def get_graphs(self):
139 """ get_graphs(self) -> [Graph, Graph, ...] 140 141 Return a list of all graphs in the graph set, sorted by id (for 142 reliable stacking...) 143 """ 144 ids = self._graphs.keys() 145 ids.sort() 146 return [self._graphs[id] for id in ids]
147
148 - def get_ids(self):
149 """ get_ids(self) -> [int, int, ...] 150 151 Return a list of all ids for the graph set 152 """ 153 return self._graphs.keys()
154
155 - def range(self):
156 """ range(self) -> (int, int) 157 158 Returns the lowest and highest base (or mark) numbers as a tuple 159 """ 160 lows, highs = [], [] 161 for graph in self._graphs.values(): 162 low, high = graph.range() 163 lows.append(low) 164 highs.append(high) 165 return (min(lows), max(highs))
166
167 - def data_quartiles(self):
168 """ data_quartiles(self) -> (float, float, float, float, float) 169 170 Returns the (minimum, lowerQ, medianQ, upperQ, maximum) values as 171 a tuple 172 """ 173 data = [] 174 for graph in self._graphs.values(): 175 data += graph.data.values() 176 data.sort() 177 datalen = len(data) 178 return(data[0], data[datalen/4], data[datalen/2], 179 data[3*datalen/4], data[-1])
180
181 - def to_string(self, verbose=0):
182 """ to_string(self, verbose=0) -> "" 183 184 o verbose Flag indicating whether a short or complete account 185 of the set is required 186 187 Returns a formatted string with information about the set 188 """ 189 if not verbose: 190 return "%s" % self 191 else: 192 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 193 outstr.append("%d graphs" % len(self._graphs)) 194 for key in self._graphs: 195 outstr.append("%s" % self._graphs[key]) 196 return "\n".join(outstr)
197
198 - def __len__(self):
199 """ __len__(self) -> int 200 201 Return the number of graphs in the set 202 """ 203 return len(self._graphs)
204
205 - def __getitem__(self, key):
206 """ __getitem__(self, key) -> Graph 207 208 Return a graph, keyed by id 209 """ 210 return self._graphs[key]
211
212 - def __str__(self):
213 """ __str__(self) -> "" 214 215 Returns a formatted string with information about the feature set 216 """ 217 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 218 outstr.append("%d graphs" % len(self._graphs)) 219 outstr = "\n".join(outstr) 220 return outstr
221 222 223 ################################################################################ 224 # RUN AS SCRIPT 225 ################################################################################ 226 227 if __name__ == '__main__': 228 229 # Test code 230 gdgs = GraphSet(0, 'test data') 231 232 testdata1 = [(1, 10), (5, 15), (10, 20), (20, 40)] 233 testdata2 = [(250, .34), (251, .7), (252, .7), (253, .54), (254, .65)] 234 235 gdgs.add_graph(testdata1, 'TestData 1') 236 gdgs.add_graph(testdata2, 'TestData 2') 237 238 print gdgs 239