1
2
3
4
5
6
7
8
9
10
11
12 """ Graph module
13
14 Provides:
15
16 o GraphData - Contains data from which a graph will be drawn, and
17 information about its presentation
18
19 For drawing capabilities, this module uses reportlab to draw and write
20 the diagram:
21
22 http://www.reportlab.com
23
24 For dealing with biological information, the package expects BioPython
25 objects:
26
27 http://www.biopython.org
28 """
29
30
31 from reportlab.lib import colors
32
33 from math import sqrt
34
36 """ GraphData
37
38 Provides:
39
40 Methods:
41
42 o __init__(self, id=None, data=None, name=None, style='bar',
43 color=colors.lightgreen, altcolor=colors.darkseagreen)
44 Called on instantiation
45
46 o set_data(self, data) Load the object with data to be plotted
47
48 o get_data(self) Returns the data to be plotted as a list of
49 (position, value) tuples
50
51 o add_point(self, point) Add a single point to the data set
52
53 o quartiles(self) Returns a tuple of the data quartiles
54
55 o range(self) Returns a tuple of the base range covered by the graph
56 data
57
58 o mean(self) Returns a float of the mean data point value
59
60 o stdev(self) Returns the sample standard deviation of the data values
61
62 o __len__(self) Returns the length of sequence covered by the data
63
64 o __getitem__(self, key) Returns the value at the base specified
65
66 o __getslice__(self, low, high) Returns graph data in the base range
67 specified
68
69 o __str__(self) Returns a formatted string describing the graph data
70
71 Attributes:
72
73 o id Unique identifier for the data
74
75 o data Dictionary of describing the data, keyed by position
76
77 o name String describing the data
78
79 o style String ('bar', 'heat', 'line') describing how to draw the data
80
81 o poscolor colors.Color for drawing high (some styles) or all
82 values
83
84 o negcolor colors.Color for drawing low values (some styles)
85
86 o linewidth Int, thickness to draw the line in 'line' styles
87 """
88 - def __init__(self, id=None, data=None, name=None, style='bar',
89 color=colors.lightgreen, altcolor=colors.darkseagreen,
90 center=None, colour=None, altcolour=None, centre=None):
91 """__init__(self, id=None, data=None, name=None, style='bar',
92 color=colors.lightgreen, altcolor=colors.darkseagreen)
93
94 o id Unique ID for the graph
95
96 o data List of (position, value) tuples
97
98 o name String describing the graph
99
100 o style String describing the presentation style ('bar', 'line',
101 'heat')
102
103 o color colors.Color describing the color to draw all or the
104 'high' (some styles) values (overridden by backwards
105 compatible argument with UK spelling, colour).
106
107 o altcolor colors.Color describing the color to draw the 'low'
108 values (some styles only) (overridden by backwards
109 compatible argument with UK spelling, colour).
110
111 o center Value at which x-axis crosses y-axis (overridden by
112 backwards comparible argument with UK spelling, centre).
113
114 """
115
116
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 self.id = id
125 self.data = {}
126 if data is not None:
127 self.set_data(data)
128 self.name = name
129
130
131 self.style = style
132 self.poscolor = color
133 self.negcolor = altcolor
134 self.linewidth = 2
135 self.center = center
136
139 centre = property(fget = lambda self : self.center,
140 fset = _set_centre,
141 doc="Backwards compatible alias for center (OBSOLETE)")
142
144 """ set_data(self, data)
145
146 o data List of (position, value) tuples
147
148 Add data with a list of (position, value) tuples
149 """
150 for (pos, val) in data:
151 self.data[pos] = val
152
153
155 """ get_data(self) -> [(int, float), (int, float), ...]
156
157 Return data as a list of sorted (position, value) tuples
158 """
159 data = []
160 for xval in self.data.keys():
161 yval = self.data[xval]
162 data.append((xval, yval))
163 data.sort()
164 return data
165
166
168 """ add_point(self, point)
169
170 o point (position, value) tuple
171
172 Add a single point to the set of data
173 """
174 pos, val = point
175 self.data[pos] = val
176
177
179 """ quartiles(self) -> (float, float, float, float, float)
180
181 Returns the (minimum, lowerQ, medianQ, upperQ, maximum) values as
182 a tuple
183 """
184 data = self.data.values()
185 data.sort()
186 datalen = len(data)
187 return(data[0], data[datalen/4], data[datalen/2],
188 data[3*datalen/4], data[-1])
189
190
192 """ range(self) -> (int, int)
193
194 Returns the range of the data, i.e. its start and end points on
195 the genome as a (start, end) tuple
196 """
197 positions = self.data.keys()
198 positions.sort()
199
200
201 return (positions[0], positions[-1])
202
203
205 """ mean(self) -> Float
206
207 Returns the mean value for the data points
208 """
209 data = self.data.values()
210 sum = 0.
211 for item in data:
212 sum += float(item)
213 return sum/len(data)
214
215
217 """ stdev(self) -> Float
218
219 Returns the sample standard deviation for the data
220 """
221 data = self.data.values()
222 m = self.mean()
223 runtotal = 0.
224 for entry in data:
225 runtotal += float((entry - m)**2)
226
227
228 return sqrt(runtotal/(len(data)-1))
229
230
232 """ __len__(self) -> Int
233
234 Returns the number of points in the data set
235 """
236 return len(self.data)
237
238
240 """ __getitem__(self, key) -> Float
241
242 o key Integer representing position on the sequence
243
244 Returns the data value at the passed position
245 """
246 return self.data[key]
247
248
250 """ __getslice__(self, low, high) -> [(int, float), (int, float), ...]
251
252 o low The start point for the data range
253
254 o high The end point for the data range
255
256 Returns a slice of the graph data from the passed low to the passed
257 high value as a list of (position, value) tuples
258 """
259 positions = self.data.keys()
260 positions.sort()
261 outlist = []
262 for pos in positions:
263 if pos >= low and pos <=high:
264 outlist.append((pos, self.data[pos]))
265 return outlist
266
267
269 """ __str__(self) -> ""
270
271 Returns a string describing the graph data
272 """
273 outstr = ["\nGraphData: %s, ID: %s" % (self.name, self.id)]
274 outstr.append("Number of points: %d" % len(self.data))
275 outstr.append("Mean data value: %s" % self.mean())
276 outstr.append("Sample SD: %.3f" % self.stdev())
277 outstr.append("Minimum: %s\n1Q: %s\n2Q: %s\n3Q: %s\nMaximum: %s" % self.quartiles())
278 outstr.append("Sequence Range: %s..%s" % self.range())
279 return "\n".join(outstr)
280