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 __future__ import print_function 
 34   
 35  from reportlab.lib import colors 
 36   
 37  from ._Graph import GraphData 
 38   
 39   
40 -class GraphSet(object):
41 """ GraphSet 42 43 Provides: 44 45 Methods: 46 47 o __init__(self, set_id=None, name=None) Called on instantiation 48 49 o new_graph(self, data, name, style='bar', color=colors.lightgreen, 50 altcolor=colors.darkseagreen) Create new graph in the set 51 from the passed data, with the passed parameters 52 53 o del_graph(self, graph_id) Delete graph with the passed id 54 55 o get_graphs(self) Returns a list of all graphs 56 57 o get_ids(self) Returns a list of graph ids 58 59 o range(self) Returns the range covered by the graphs in the set 60 61 o to_string(self, verbose=0) Returns a string describing the set 62 63 o __len__(self) Returns the length of sequence covered by the set 64 65 o __getitem__(self, key) Returns the graph with the id of the passed key 66 67 o __str__(self) Returns a string describing the set 68 69 Attributes: 70 71 o id Unique identifier for the set 72 73 o name String describing the set 74 75 """
76 - def __init__(self, name=None):
77 """ __init__(self, name=None) 78 79 o name String identifying the graph set sensibly 80 """ 81 self.id = id # Unique identifier for the set 82 self._next_id = 0 # Holds unique ids for graphs 83 self._graphs = {} # Holds graphs, keyed by unique id 84 self.name = name # Holds description of graph
85
86 - def new_graph(self, data, name=None, style='bar', color=colors.lightgreen, 87 altcolor=colors.darkseagreen, linewidth=1, center=None, 88 colour=None, altcolour=None, centre=None):
89 """ new_graph(self, data, name=None, style='bar', color=colors.lightgreen, 90 altcolor=colors.darkseagreen) 91 92 o data List of (position, value) int tuples 93 94 o name String, description of the graph 95 96 o style String ('bar', 'heat', 'line') describing how the graph 97 will be drawn 98 99 o color colors.Color describing the color to draw all or 'high' 100 (some styles) data (overridden by backwards compatible 101 argument with UK spelling, colour). 102 103 o altcolor colors.Color describing the color to draw 'low' (some 104 styles) data (overridden by backwards compatible argument 105 with UK spelling, colour). 106 107 o linewidth Float describing linewidth for graph 108 109 o center Float setting the value at which the x-axis 110 crosses the y-axis (overridden by backwards 111 compatible argument with UK spelling, centre) 112 113 Add a GraphData object to the diagram (will be stored 114 internally 115 """ 116 #Let the UK spelling (colour) override the USA spelling (color) 117 if colour is not None: 118 color = colour 119 if altcolour is not None: 120 altcolor = altcolour 121 if centre is not None: 122 center = centre 123 124 id = self._next_id # get id number 125 graph = GraphData(id, data, name, style, color, altcolor, center) 126 graph.linewidth = linewidth 127 self._graphs[id] = graph # add graph data 128 self._next_id += 1 # increment next id 129 return graph
130
131 - def del_graph(self, graph_id):
132 """ del_graph(self, graph_id) 133 134 o graph_id Identifying value of the graph 135 136 Remove a graph from the set, indicated by its id 137 """ 138 del self._graphs[graph_id]
139
140 - def get_graphs(self):
141 """ get_graphs(self) -> [Graph, Graph, ...] 142 143 Return a list of all graphs in the graph set, sorted by id (for 144 reliable stacking...) 145 """ 146 return [self._graphs[id] for id in sorted(self._graphs)]
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 list(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 += list(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