1
2
3
4
5
6
7
8
9
10
11
12
13
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
33 from reportlab.lib import colors
34
35 from _Graph import GraphData
36
38 """ GraphSet
39
40 Provides:
41
42 Methods:
43
44 o __init__(self, set_id=None, name=None) Called on instantiation
45
46 o new_graph(self, data, name, style='bar', color=colors.lightgreen,
47 altcolor=colors.darkseagreen) Create new graph in the set
48 from the passed data, with the passed parameters
49
50 o del_graph(self, graph_id) Delete graph with the passed id
51
52 o get_graphs(self) Returns a list of all graphs
53
54 o get_ids(self) Returns a list of graph ids
55
56 o range(self) Returns the range covered by the graphs in the set
57
58 o to_string(self, verbose=0) Returns a string describing the set
59
60 o __len__(self) Returns the length of sequence covered by the set
61
62 o __getitem__(self, key) Returns the graph with the id of the passed key
63
64 o __str__(self) Returns a string describing the set
65
66 Attributes:
67
68 o id Unique identifier for the set
69
70 o name String describing the set
71
72 """
74 """ __init__(self, name=None)
75
76 o name String identifying the graph set sensibly
77 """
78 self.id = id
79 self._next_id = 0
80 self._graphs = {}
81 self.name = name
82
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
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
123 graph = GraphData(id, data, name, style, color, altcolor, center)
124 graph.linewidth = linewidth
125 self._graphs[id] = graph
126 self._next_id += 1
127 return graph
128
129
131 """ del_graph(self, graph_id)
132
133 o graph_id Identifying value of the graph
134
135 Remove a graph from the set, indicated by its id
136 """
137 del self._graphs[graph_id]
138
139
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 ids = self._graphs.keys()
147 ids.sort()
148 return [self._graphs[id] for id in ids]
149
150
152 """ get_ids(self) -> [int, int, ...]
153
154 Return a list of all ids for the graph set
155 """
156 return self._graphs.keys()
157
158
160 """ range(self) -> (int, int)
161
162 Returns the lowest and highest base (or mark) numbers as a tuple
163 """
164 lows, highs = [], []
165 for graph in self._graphs.values():
166 low, high = graph.range()
167 lows.append(low)
168 highs.append(high)
169 return (min(lows), max(highs))
170
171
173 """ data_quartiles(self) -> (float, float, float, float, float)
174
175 Returns the (minimum, lowerQ, medianQ, upperQ, maximum) values as
176 a tuple
177 """
178 data = []
179 for graph in self._graphs.values():
180 data += graph.data.values()
181 data.sort()
182 datalen = len(data)
183 return(data[0], data[datalen/4], data[datalen/2],
184 data[3*datalen/4], data[-1])
185
186
188 """ to_string(self, verbose=0) -> ""
189
190 o verbose Flag indicating whether a short or complete account
191 of the set is required
192
193 Returns a formatted string with information about the set
194 """
195 if not verbose:
196 return "%s" % self
197 else:
198 outstr = ["\n<%s: %s>" % (self.__class__, self.name)]
199 outstr.append("%d graphs" % len(self._graphs))
200 for key in self._graphs:
201 outstr.append("%s" % self._graphs[key])
202 return "\n".join(outstr)
203
204
206 """ __len__(self) -> int
207
208 Return the number of graphs in the set
209 """
210 return len(self._graphs)
211
212
214 """ __getitem__(self, key) -> Graph
215
216 Return a graph, keyed by id
217 """
218 return self._graphs[key]
219
220
222 """ __str__(self) -> ""
223
224 Returns a formatted string with information about the feature set
225 """
226 outstr = ["\n<%s: %s>" % (self.__class__, self.name)]
227 outstr.append("%d graphs" % len(self._graphs))
228 outstr = join(outstr, '\n')
229 return outstr
230
231
232
233
234
235
236 if __name__ == '__main__':
237
238
239 gdgs = GraphSet(0, 'test data')
240
241 testdata1 = [(1, 10), (5, 15), (10, 20), (20, 40)]
242 testdata2 = [(250, .34), (251, .7), (252, .7), (253, .54), (254, .65)]
243
244 gdgs.add_graph(testdata1, 'TestData 1')
245 gdgs.add_graph(testdata2, 'TestData 2')
246
247 print gdgs
248