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