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   - GraphSet - container for GraphData objects 
 19   
 20  For drawing capabilities, this module uses reportlab to draw and write 
 21  the diagram: http://www.reportlab.com 
 22  """ 
 23   
 24  # ReportLab imports 
 25  from __future__ import print_function 
 26   
 27  from reportlab.lib import colors 
 28   
 29  from ._Graph import GraphData 
 30   
 31   
32 -class GraphSet(object):
33 """Graph Set. 34 35 Attributes: 36 - id Unique identifier for the set 37 - name String describing the set 38 39 """ 40
41 - def __init__(self, name=None):
42 """Initialize. 43 44 Arguments: 45 - name String identifying the graph set sensibly 46 47 """ 48 self.id = id # Unique identifier for the set 49 self._next_id = 0 # Holds unique ids for graphs 50 self._graphs = {} # Holds graphs, keyed by unique id 51 self.name = name # Holds description of graph
52
53 - def new_graph(self, data, name=None, style='bar', color=colors.lightgreen, 54 altcolor=colors.darkseagreen, linewidth=1, center=None, 55 colour=None, altcolour=None, centre=None):
56 """Add a GraphData object to the diagram. 57 58 Arguments: 59 - data List of (position, value) int tuples 60 - name String, description of the graph 61 - style String ('bar', 'heat', 'line') describing how the graph 62 will be drawn 63 - color colors.Color describing the color to draw all or 'high' 64 (some styles) data (overridden by backwards compatible 65 argument with UK spelling, colour). 66 - altcolor colors.Color describing the color to draw 'low' (some 67 styles) data (overridden by backwards compatible argument 68 with UK spelling, colour). 69 - linewidth Float describing linewidth for graph 70 - center Float setting the value at which the x-axis 71 crosses the y-axis (overridden by backwards 72 compatible argument with UK spelling, centre) 73 74 Add a GraphData object to the diagram (will be stored internally). 75 """ 76 # Let the UK spelling (colour) override the USA spelling (color) 77 if colour is not None: 78 color = colour 79 if altcolour is not None: 80 altcolor = altcolour 81 if centre is not None: 82 center = centre 83 84 id = self._next_id # get id number 85 graph = GraphData(id, data, name, style, color, altcolor, center) 86 graph.linewidth = linewidth 87 self._graphs[id] = graph # add graph data 88 self._next_id += 1 # increment next id 89 return graph
90
91 - def del_graph(self, graph_id):
92 """Remove a graph from the set, indicated by its id.""" 93 del self._graphs[graph_id]
94
95 - def get_graphs(self):
96 """Return list of all graphs in the graph set, sorted by id. 97 98 Sorting is to ensure reliable stacking. 99 """ 100 return [self._graphs[id] for id in sorted(self._graphs)]
101
102 - def get_ids(self):
103 """Return a list of all ids for the graph set.""" 104 return list(self._graphs.keys())
105
106 - def range(self):
107 """Return the lowest and highest base (or mark) numbers as a tuple.""" 108 lows, highs = [], [] 109 for graph in self._graphs.values(): 110 low, high = graph.range() 111 lows.append(low) 112 highs.append(high) 113 return (min(lows), max(highs))
114
115 - def data_quartiles(self):
116 """Return (minimum, lowerQ, medianQ, upperQ, maximum) values as a tuple.""" 117 data = [] 118 for graph in self._graphs.values(): 119 data += list(graph.data.values()) 120 data.sort() 121 datalen = len(data) 122 return(data[0], data[datalen / 4], data[datalen / 2], 123 data[3 * datalen / 4], data[-1])
124
125 - def to_string(self, verbose=0):
126 """Return a formatted string with information about the set. 127 128 Arguments: 129 - verbose - Flag indicating whether a short or complete account 130 of the set is required 131 132 """ 133 if not verbose: 134 return "%s" % self 135 else: 136 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 137 outstr.append("%d graphs" % len(self._graphs)) 138 for key in self._graphs: 139 outstr.append("%s" % self._graphs[key]) 140 return "\n".join(outstr)
141
142 - def __len__(self):
143 """Return the number of graphs in the set.""" 144 return len(self._graphs)
145
146 - def __getitem__(self, key):
147 """Return a graph, keyed by id.""" 148 return self._graphs[key]
149
150 - def __str__(self):
151 """Return a formatted string with information about the feature set.""" 152 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 153 outstr.append("%d graphs" % len(self._graphs)) 154 outstr = "\n".join(outstr) 155 return outstr
156 157 158 ################################################################################ 159 # RUN AS SCRIPT 160 ################################################################################ 161 162 if __name__ == '__main__': 163 164 # Test code 165 gdgs = GraphSet(0, 'test data') 166 167 testdata1 = [(1, 10), (5, 15), (10, 20), (20, 40)] 168 testdata2 = [(250, .34), (251, .7), (252, .7), (253, .54), (254, .65)] 169 170 gdgs.add_graph(testdata1, 'TestData 1') 171 gdgs.add_graph(testdata2, 'TestData 2') 172 173 print(gdgs) 174