-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
513 lines (498 loc) · 32.9 KB
/
plot.py
File metadata and controls
513 lines (498 loc) · 32.9 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import xml.dom.minidom as dom
import copy
import slidingWindow
import math
import json
import composite
document = dom.Document()
# Class that generates composite and reference lines svg elements
class Plot:
def __init__(self, title=None, xmin=None, xmax=None, ymin=None, ymax=None, xlabel=None, ylabel=None,
opacity=None, smoothing=None, bp_shift=None, combined=False, color_trace=False, hide_legend=False, resolution=None):
# Set variables to defaults if argument passed into constructor was None
self.title = title if title is not None else "Composite plot"
self.xmin = xmin if xmin is not None else -500
self.xmax = xmax if xmax is not None else 500
self.ymin = ymin if ymin is not None else -1
self.ymax = ymax if ymax is not None else 1
self.xlabel = xlabel if xlabel is not None else "Position (bp)"
self.ylabel = ylabel if ylabel is not None else "Occupancy (AU)"
self.opacity = opacity if opacity is not None else 1
self.smoothing = smoothing if smoothing is not None else 7
self.bp_shift = bp_shift if bp_shift is not None else 0
self.combined = combined
self.color_trace = color_trace
self.hide_legend = hide_legend
# Set dimensions to same constants as plotter
self.width = 460
self.height = 300
self.margins = {'top': 30, 'right': 170, 'bottom': 35, 'left': 40}
resolution = (int(resolution.split("x")[0]), int(resolution.split("x")[1])) if resolution is not None else (300, 300)
self.width = 160 + resolution[0]
self.height = resolution[1]
# Create groups for adding composites and reference lines
self.plot = document.createElement("g")
self.composite_group = document.createElement("g")
self.composite_group.setAttribute("class", "composite plotted")
self.gradients_group = document.createElement("defs")
self.reference_group = document.createElement("g")
self.xscale = XScale(self)
self.yscale = YScale(self)
self.num_composites = 0
self.composites = []
self.styles ={"dashed" : "5,5",
"solid" : "0",
"dotted" : "2,1"}
# Creates a composite svg element from separate sense and anti arrays - mimics plot_composite from plotter
def plot_composite(self, xmin, xmax, sense, anti, scale=1, color=None, secondary_color=None, i=None, opacity=None, smoothing=None, bp_shift=None, hide_sense=False, hide_anti=False, baseline=0):
# Set parameters to global values if not specified
opacity = opacity if opacity is not None else self.opacity
smoothing = smoothing if smoothing is not None else self.smoothing
bp_shift = bp_shift if bp_shift is not None else self.bp_shift
if (i is None):
i = self.num_composites
self.num_composites += 1
else:
i = i
# Set x domain as array of integers from xmin to xmax
xdomain = [i + xmin for i in range(xmax - xmin + 1)]
if (self.combined):
# Calculate defined x domain after shifting
shifted_xdomain = [x for x in xdomain if x - bp_shift >= xdomain[0] and x - bp_shift <= xdomain[-1]
and x + bp_shift >= xdomain[0] and x + bp_shift <= xdomain[-1]]
shifted_sense = [sense[j] for j in range(len(sense)) if xdomain[j] + bp_shift >= shifted_xdomain[0]
and xdomain[j] + bp_shift <= shifted_xdomain[-1]]
shifted_anti = [anti[j] for j in range(len(anti)) if xdomain[j] + bp_shift >= shifted_xdomain[0]
and xdomain[j] + bp_shift <= shifted_xdomain[-1]]
# Add occupancy for sense and anti
combined_occupancy = [shifted_sense[j] + shifted_anti[j] for j in range(len(shifted_sense))]
# Smooth occupancy with moving average
new_xdomain, smoothed_occupancy = slidingWindow.sliding_window(shifted_xdomain, combined_occupancy, smoothing).values()
# Truncate x domain to x axis limits
truncated_xdomain = [x for x in new_xdomain if x >= self.xmin and x <= self.xmax]
# Truncate occupancy and scale by scale factor, adding baseline value
scaled_occupancy = [value if (value := d * scale + baseline) > 0 else 0 for j, d in enumerate(smoothed_occupancy)
if int(new_xdomain[j]) >= self.xmin and int(new_xdomain[j]) <= self.xmax]
composite_fill_top = document.createElement("polygon")
composite_fill_top.setAttribute("points", " ".join(points := [f"{self.xscale.get(d)},{self.yscale.get(scaled_occupancy[j])}" for j, d in enumerate(truncated_xdomain)]) + f" {self.xscale.get(truncated_xdomain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_xdomain[0])},{self.yscale.get(0)}")
composite_fill_top.setAttribute("fill", "url(#composite-gradient-top" + str(i) + ")")
self.composite_group.appendChild(composite_fill_top)
#Create outline
wide_trace = document.createElement("path")
wide_trace.setAttribute("stroke-width", "1")
wide_trace.setAttribute("stroke", color)
wide_trace.setAttribute("fill", "none")
wide_trace.setAttribute("d", "M" + "L".join(points))
self.composite_group.appendChild(wide_trace)
if not self.color_trace:
wide_trace.setAttribute("stroke", "#FFFFFF")
narrow_trace = copy.deepcopy(wide_trace)
narrow_trace.setAttribute("stroke-width", "0.5")
narrow_trace.setAttribute("stroke", "#000000")
narrow_trace.setAttribute("d", "M" + "L".join(points))
self.composite_group.appendChild(narrow_trace)
else:
# Smooth sense and anti occupancy with moving average
new_xdomain, smoothed_sense = slidingWindow.sliding_window(xdomain, sense, smoothing).values()
smoothed_anti = list(slidingWindow.sliding_window(xdomain, anti, smoothing).values())[1]
# Truncate x domain to x axis limits
truncated_sense_domain = [j for x in new_xdomain if (j := x + bp_shift) >= self.xmin and j <= self.xmax]
truncated_anti_domain = [j for x in new_xdomain if (j := x - bp_shift) >= self.xmin and j <= self.xmax]
# Truncate sense and anti occupancy and scale by scale factor
scaled_sense = [value if (value := d * scale + baseline) > 0 else 0 for j, d in enumerate(smoothed_sense)
if int(new_xdomain[j] + bp_shift) >= self.xmin and int(new_xdomain[j] + bp_shift) <= self.xmax]
scaled_anti = [value if (value := d * scale + baseline) > 0 else 0 for j, d in enumerate(smoothed_anti)
if int(new_xdomain[j] - bp_shift) >= self.xmin and int(new_xdomain[j] - bp_shift) <= self.xmax]
# Create sense trace and polygon if not hidden
if not hide_anti:
# Create top polygon
composite_fill_top = document.createElement("polygon")
composite_fill_top.setAttribute("points", " ".join(sense_points := [f"{self.xscale.get(d)},{self.yscale.get(scaled_sense[j])}" for j, d in enumerate(truncated_sense_domain)]) + f" {self.xscale.get(truncated_sense_domain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_sense_domain[0])},{self.yscale.get(0)}")
composite_fill_top.setAttribute("fill", "url(#composite-gradient-top" + str(i) + ")")
self.composite_group.appendChild(composite_fill_top)
#Create trace
top_wide_trace = document.createElement("path")
top_wide_trace.setAttribute("stroke-width", "1")
top_wide_trace.setAttribute("stroke", color)
top_wide_trace.setAttribute("fill", "none")
top_wide_trace.setAttribute("d", "M" + "L".join(sense_points))
self.composite_group.appendChild(top_wide_trace)
if not self.color_trace:
top_wide_trace.setAttribute("stroke", "#FFFFFF")
top_narrow_trace = copy.deepcopy(top_wide_trace)
top_narrow_trace.setAttribute("stroke-width", "0.5")
top_narrow_trace.setAttribute("stroke", "#000000")
top_narrow_trace.setAttribute("d", "M" + "L".join(sense_points))
self.composite_group.appendChild(top_narrow_trace)
# Create anti trace and polygon if not hidden
if not hide_anti:
# Create polygon
composite_fill_bottom = document.createElement("polygon")
composite_fill_bottom.setAttribute("points", " ".join(anti_points := [f"{self.xscale.get(d)},{self.yscale.get(-scaled_anti[j])}" for j, d in enumerate(truncated_anti_domain)]) + f" {self.xscale.get(truncated_anti_domain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_anti_domain[0])},{self.yscale.get(0)}")
composite_fill_bottom.setAttribute("fill", "url(#composite-gradient-bottom" + str(i) + ")")
self.composite_group.appendChild(composite_fill_bottom)
#Create trace
bottom_wide_trace = document.createElement("path")
bottom_wide_trace.setAttribute("stroke-width", "1")
bottom_wide_trace.setAttribute("stroke", secondary_color)
bottom_wide_trace.setAttribute("fill", "none")
bottom_wide_trace.setAttribute("d", "M" + "L".join(anti_points))
self.composite_group.appendChild(bottom_wide_trace)
if not self.color_trace:
bottom_wide_trace.setAttribute("stroke", "#FFFFFF")
bottom_narrow_trace = copy.deepcopy(bottom_wide_trace)
bottom_narrow_trace.setAttribute("stroke-width", "0.5")
bottom_narrow_trace.setAttribute("stroke", "#000000")
bottom_narrow_trace.setAttribute("d", "M" + "L".join(anti_points))
self.composite_group.appendChild(bottom_narrow_trace)
self.generateGradients(opacity, i, color, secondary_color=secondary_color)
self.plot.appendChild(self.gradients_group)
self.plot.appendChild(self.composite_group)
# Creates a composite svg element from a composite object, like plotting a row form the settings table
def plot_composite(self, composite):
# Set parameters to global values if not specified
opacity = composite.opacity if composite.opacity is not None else self.opacity
smoothing = composite.smoothing if composite.smoothing is not None else self.smoothing
bp_shift = composite.bp_shift if composite.bp_shift is not None else self.bp_shift
i = self.num_composites
self.num_composites += 1
# Set x domain as array of integers from xmin to xmax
xdomain = [i + composite.xmin for i in range(composite.xmax - composite.xmin + 1)]
if (self.combined):
# Calculate defined x domain after shifting
shifted_xdomain = [x for x in xdomain if x - bp_shift >= xdomain[0] and x - bp_shift <= xdomain[-1]
and x + bp_shift >= xdomain[0] and x + bp_shift <= xdomain[-1]]
shifted_sense = [composite.sense[j] for j in range(len(composite.sense)) if xdomain[j] + bp_shift >= shifted_xdomain[0]
and xdomain[j] + bp_shift <= shifted_xdomain[-1]]
shifted_anti = [composite.anti[j] for j in range(len(composite.anti)) if xdomain[j] + bp_shift >= shifted_xdomain[0]
and xdomain[j] + bp_shift <= shifted_xdomain[-1]]
# Add occupancy for sense and anti
combined_occupancy = [shifted_sense[j] + shifted_anti[j] for j in range(len(shifted_sense))]
# Smooth occupancy with moving average
new_xdomain, smoothed_occupancy = slidingWindow.sliding_window(shifted_xdomain, combined_occupancy, smoothing).values()
# Truncate x domain to x axis limits
truncated_xdomain = [x for x in new_xdomain if x >= self.xmin and x <= self.xmax]
# Truncate occupancy and scale by scale factor, adding baseline value
scaled_occupancy = [value if (value := d * composite.scale + composite.baseline) > 0 else 0 for j, d in enumerate(smoothed_occupancy)
if int(new_xdomain[j]) >= self.xmin and int(new_xdomain[j]) <= self.xmax]
composite_fill_top = document.createElement("polygon")
composite_fill_top.setAttribute("points", " ".join(points := [f"{self.xscale.get(d)},{self.yscale.get(scaled_occupancy[j])}" for j, d in enumerate(truncated_xdomain)]) + f" {self.xscale.get(truncated_xdomain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_xdomain[0])},{self.yscale.get(0)}")
composite_fill_top.setAttribute("fill", "url(#composite-gradient-top" + str(i) + ")")
self.composite_group.appendChild(composite_fill_top)
#Create outline
wide_trace = document.createElement("path")
wide_trace.setAttribute("stroke-width", "1")
wide_trace.setAttribute("stroke", composite.color)
wide_trace.setAttribute("fill", "none")
wide_trace.setAttribute("d", "M" + "L".join(points))
self.composite_group.appendChild(wide_trace)
if not self.color_trace:
wide_trace.setAttribute("stroke", "#FFFFFF")
narrow_trace = copy.deepcopy(wide_trace)
narrow_trace.setAttribute("stroke-width", "0.5")
narrow_trace.setAttribute("stroke", "#000000")
narrow_trace.setAttribute("d", "M" + "L".join(points))
self.composite_group.appendChild(narrow_trace)
else:
# Smooth sense and anti occupancy with moving average
new_xdomain, smoothed_sense = slidingWindow.sliding_window(xdomain, composite.sense, smoothing).values()
smoothed_anti = list(slidingWindow.sliding_window(xdomain, composite.anti, smoothing).values())[1]
# Truncate x domain to x axis limits
truncated_sense_domain = [j for x in new_xdomain if (j := x + bp_shift) >= self.xmin and j <= self.xmax]
truncated_anti_domain = [j for x in new_xdomain if (j := x - bp_shift) >= self.xmin and j <= self.xmax]
# Truncate sense and anti occupancy and scale by scale factor
scaled_sense = [value if (value := d * composite.scale + composite.baseline) > 0 else 0 for j, d in enumerate(smoothed_sense)
if int(new_xdomain[j] + bp_shift) >= self.xmin and int(new_xdomain[j] + bp_shift) <= self.xmax]
scaled_anti = [value if (value := d * composite.scale + composite.baseline) > 0 else 0 for j, d in enumerate(smoothed_anti)
if int(new_xdomain[j] - bp_shift) >= self.xmin and int(new_xdomain[j] - bp_shift) <= self.xmax]
# Create sense trace and polygon if not hidden
if not composite.hide_sense:
# Create top polygon
composite_fill_top = document.createElement("polygon")
composite_fill_top.setAttribute("points", " ".join(sense_points := [f"{self.xscale.get(d)},{self.yscale.get(scaled_sense[j])}" for j, d in enumerate(truncated_sense_domain)]) + f" {self.xscale.get(truncated_sense_domain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_sense_domain[0])},{self.yscale.get(0)}")
composite_fill_top.setAttribute("fill", "url(#composite-gradient-top" + str(i) + ")")
self.composite_group.appendChild(composite_fill_top)
#Create trace
top_wide_trace = document.createElement("path")
top_wide_trace.setAttribute("stroke-width", "1")
top_wide_trace.setAttribute("stroke", composite.color)
top_wide_trace.setAttribute("fill", "none")
top_wide_trace.setAttribute("d", "M" + "L".join(sense_points))
self.composite_group.appendChild(top_wide_trace)
if not self.color_trace:
top_wide_trace.setAttribute("stroke", "#FFFFFF")
top_narrow_trace = copy.deepcopy(top_wide_trace)
top_narrow_trace.setAttribute("stroke-width", "0.5")
top_narrow_trace.setAttribute("stroke", "#000000")
top_narrow_trace.setAttribute("d", "M" + "L".join(sense_points))
self.composite_group.appendChild(top_narrow_trace)
# Create anti trace and polygon if not hidden
if not composite.hide_anti:
# Create polygon
composite_fill_bottom = document.createElement("polygon")
composite_fill_bottom.setAttribute("points", " ".join(anti_points := [f"{self.xscale.get(d)},{self.yscale.get(-scaled_anti[j])}" for j, d in enumerate(truncated_anti_domain)]) + f" {self.xscale.get(truncated_anti_domain[-1])},{self.yscale.get(0)} {self.xscale.get(truncated_anti_domain[0])},{self.yscale.get(0)}")
composite_fill_bottom.setAttribute("fill", "url(#composite-gradient-bottom" + str(i) + ")")
self.composite_group.appendChild(composite_fill_bottom)
#Create trace
bottom_wide_trace = document.createElement("path")
bottom_wide_trace.setAttribute("stroke-width", "1")
bottom_wide_trace.setAttribute("stroke", composite.secondary_color)
bottom_wide_trace.setAttribute("fill", "none")
bottom_wide_trace.setAttribute("d", "M" + "L".join(anti_points))
self.composite_group.appendChild(bottom_wide_trace)
if not self.color_trace:
bottom_wide_trace.setAttribute("stroke", "#FFFFFF")
bottom_narrow_trace = copy.deepcopy(bottom_wide_trace)
bottom_narrow_trace.setAttribute("stroke-width", "0.5")
bottom_narrow_trace.setAttribute("stroke", "#000000")
bottom_narrow_trace.setAttribute("d", "M" + "L".join(anti_points))
self.composite_group.appendChild(bottom_narrow_trace)
self.generateGradients(opacity, i, composite.color, secondary_color=composite.secondary_color)
self.plot.appendChild(self.gradients_group)
self.plot.appendChild(self.composite_group)
# Changes values and updates scale objects
def scale_axes(self, xmin=None, xmax=None, ymin=None, ymax=None):
self.xmin = xmin if xmin is not None else self.xmin
self.xmax = xmax if xmax is not None else self.xmax
self.ymin = ymin if ymin is not None else self.ymin
self.ymax = ymax if ymax is not None else self.ymax
self.xscale = XScale(self)
self.yscale = YScale(self)
# Finds the max/min x and y values from composites on plot and scales axes accordingly
def autoscale_axes(self, args):
xmin = min([group.xmin for group in self.composites])
xmax = max([group.xmax for group in self.composites])
if self.combined:
ymin = 0
ymax = round(max([(group.sense[i] + group.sense[i]) * group.scale for group in self.composites for i in range(min(len(group.sense), len(group.anti)))]), 2)
else:
ymin = min([-val * group.scale for group in self.composites for val in group.anti])
ymax = max([val * group.scale for group in self.composites for val in group.sense])
if args.no_resize:
self.scale_axes(args.xmin, args.xmax, args.ymin, args.ymax)
else:
self.scale_axes(args.xmin or xmin, args.xmax or xmax, args.ymin or ymin, args.ymax or ymax)
# Adds composite group object to plot
def add_composite_group(self, composite_group):
if composite_group.name == None:
composite_group.name = self.num_composites + 1
self.composites.append(composite_group)
# Plots all composites on plot
def plot_composites(self):
for group in self.composites:
self.plot_composite(group)
return self.plot
# Adds reference lines to plot
def plot_reference_line(self, axis=None, val=None, style=None, color=None, opacity=None):
# Sets default values for reference lines
axis = axis if axis is not None else "x"
val = val if val is not None else 0
style = style if style is not None else "dashed"
color = color if color is not None else "#FF0000"
opacity = opacity if opacity is not None else 1
bottom = self.height - self.margins.get('bottom')
top = self.margins.get("top")
right = self.width - (self.margins.get('right'))
left = self.margins.get("left")
# Draws reference lines on plot
line = document.createElement("line")
label = document.createElement("text")
if axis == "x":
val = int(val)
line = document.createElement("line")
line.setAttribute("x1", str(self.xscale.get(val)))
line.setAttribute("x2", str(self.xscale.get(val)))
line.setAttribute("y1", str(top))
line.setAttribute("y2", str(bottom))
label.setAttribute("x", str(self.xscale.get(val) - 4))
label.setAttribute("y", str(bottom + 8))
label.appendChild(document.createTextNode(str(val)))
elif axis == "y":
line = document.createElement("line")
line.setAttribute("x1", str(left))
line.setAttribute("x2", str(right))
line.setAttribute("y1", str(self.yscale.get(val)))
line.setAttribute("y2", str(self.yscale.get(val)))
label.setAttribute("x", str(right + 5))
label.setAttribute("y", str(self.yscale.get(val) + 4))
label.appendChild(document.createTextNode(str(val)))
line.setAttribute("stroke-dasharray", self.styles.get(style))
line.setAttribute("stroke-width", "1")
line.setAttribute("stroke", color)
line.setAttribute("opacity", str(opacity))
label.setAttribute("text-align", "middle")
label.setAttribute("fill", color)
label.setAttribute("font-size", "8px")
self.reference_group.appendChild(line)
self.reference_group.appendChild(label)
self.plot.appendChild(self.reference_group)
# Creates legend for plot
def create_legend(self):
if not self.hide_legend:
legend = document.createElement('g')
legend.setAttribute("transform", "translate(" + str(self.width - self.margins.get("right") + 25) + " " + str(self.margins.get("top")) + ")")
i = 0
for composite in self.composites:
# Creates legend entries for each composite
legend_element = document.createElement("g")
legend_element.setAttribute("transform", "translate(0," + str(24 * i) + ")")
legend_color_sense = document.createElement("polygon")
legend_color_sense.setAttribute("points", "0,0 15,0 15,15 0,15")
legend_color_sense.setAttribute("fill", composite.color)
legend_element.appendChild(legend_color_sense)
legend_color_anti = document.createElement("polygon")
legend_color_anti.setAttribute("points", "0,0 15,0 15,15 0,15")
legend_element.appendChild(legend_color_anti)
legend_color_anti.setAttribute("fill", composite.secondary_color)
legend_border = document.createElement("rect")
legend_border.setAttribute("width", "15")
legend_border.setAttribute("height", "15")
legend_border.setAttribute("stroke", "#000000")
legend_border.setAttribute("fill", "none")
legend_element.appendChild(legend_border)
id = document.createElement("text")
id.setAttribute("x", "20")
id.setAttribute("y", "10")
id.setAttribute("font-size", "10")
id.appendChild(document.createTextNode(str(composite.name)))
legend_element.appendChild(id)
legend.appendChild(legend_element)
i += 1
self.plot.appendChild(legend)
# Returns svg group with all composites and reference lines
def get_plot(self):
return self.plot
# Exports json of all composites and plot attributes
def export(self):
composite_arr = []
for composite in self.composites:
composite_arr.append({
'name': composite.name,
'xmin': composite.xmin,
'xmax': composite.xmax,
'sense': composite.sense,
'anti': composite.anti,
'color': composite.color,
'secondary-color': composite.secondary_color,
'scale': composite.scale,
'opacity': composite.opacity,
'smoothing': composite.smoothing,
'bp_shift': composite.bp_shift,
'hide_sense': composite.hide_sense,
'hide_anti': composite.hide_anti,
'files_loaded': composite.files_loaded
})
return {
'settings' :composite_arr,
'plot' : {'title': self.title, 'xlabel': self.xlabel, 'ylabel': self.ylabel, 'opacity': self.opacity,
'smoothing': self.smoothing, 'bp_shift': self.bp_shift, 'xmin': self.xmin, 'xmax': self.xmax, 'ymin': self.ymin,
'ymax': self.ymax, 'combined': self.combined, 'color_trace': self.color_trace, 'hide_legend': self.hide_legend}
}
# Imports JSON with plot attributes and composites if desired. Preserves plot options specified by most recent call
def import_data(self, file, args, import_composites):
with open(file) as f:
data = json.load(f)
if import_composites:
for c in data['settings']:
n = c.get('name')
# Add _imported to composite name if duplicate of existing composite
if any(n == self.composites[j].name for j in range(len(self.composites))):
n = str(n) + "_imported"
self.composites.append(composite.Composite(scale=float(c.get('scale')) if c.get('scale') is not None else None,
color=c.get('color'),
secondary_color=c.get('secondary_color'),
opacity=c.get('opacity') if c.get('smoothing') != False else None,
smoothing=c.get('smoothing') if c.get('smoothing') != False else None,
bp_shift=c.get('bp_shift') if c.get('bp_shift') != False else None,
hide_sense=hide if (hide := c.get('hide')) == True else c.get('hide_forward'),
hide_anti=hide if hide == True else c.get('hide_reverse'),
baseline=c.get('baseline'),
name=n,
sense=c.get('sense'),
anti=c.get('anti'),
xmin=c.get('xmin'),
xmax=c.get('xmax')))
plot_data = data['plot']
# Add plot variables
self.title = plot_data.get('title', self.title) if args.title is None else self.title
self.xmin = plot_data.get('xmin', self.xmin) if args.xmin is None else self.xmin
self.xmax = plot_data.get('xmax', self.xmax) if args.xmax is None else self.xmax
self.ymin = plot_data.get('ymin', self.ymin) if args.ymin is None else self.ymin
self.ymax = plot_data.get('ymax', self.ymax) if args.ymax is None else self.ymax
self.xlabel = plot_data.get('xlabel', self.xlabel) if args.xlabel is None else self.xlabel
self.ylabel = plot_data.get('ylabel', self.ylabel) if args.ylabel is None else self.ylabel
self.opacity = plot_data.get('opacity', self.opacity) if args.opacity is None else self.opacity
self.smoothing = plot_data.get('smoothing', self.smoothing) if args.smoothing is None else self.smoothing
self.bp_shift = plot_data.get('bp_shift', self.bp_shift) if args.bp_shift is None else self.bp_shift
self.combined = plot_data.get('combined', self.combined) if args.combined is None else self.combined
self.color_trace = plot_data.get('color_trace', self.color_trace) if args.color_trace is None else self.color_trace
self.hide_legend = plot_data.get('hide_legend', self.hide_legend) if args.hide_legend is None else self.hide_legend
self.xscale = XScale(self)
self.yscale = YScale(self)
def generateGradients(self, opacity, i, color, secondary_color=None):
# Creates DOM elements for top and bottom gradients
secondary_color = secondary_color if secondary_color is not None else color
# Generates top gradient
composite_gradient_top = document.createElement("linearGradient")
composite_gradient_top.setAttribute("class", "composite-gradient-top")
composite_gradient_top.setAttribute("x1", "0%")
composite_gradient_top.setAttribute("x2", "0%")
composite_gradient_top.setAttribute("y1", "0%")
composite_gradient_top.setAttribute("y2", "100%")
composite_gradient_top.setAttribute("id", "composite-gradient-top" + str(i))
top_stop_one = document.createElement("stop")
top_stop_one.setAttribute("offset", "0")
top_stop_one.setAttribute("stop-color", color)
top_stop_one.setAttribute("stop-opacity", str(opacity))
top_stop_two = document.createElement("stop")
top_stop_two.setAttribute("offset", "1")
top_stop_two.setAttribute("stop-color", color)
top_stop_two.setAttribute("stop-opacity", "0")
composite_gradient_top.appendChild(top_stop_one)
composite_gradient_top.appendChild(top_stop_two)
self.gradients_group.appendChild(composite_gradient_top)
# Generates bottom gradient
composite_gradient_bottom = document.createElement("linearGradient")
composite_gradient_bottom.setAttribute("class", "composite-gradient-bottom")
composite_gradient_bottom.setAttribute("x1", "0%")
composite_gradient_bottom.setAttribute("x2", "0%")
composite_gradient_bottom.setAttribute("y1", "100%")
composite_gradient_bottom.setAttribute("y2", "0%")
composite_gradient_bottom.setAttribute("id", "composite-gradient-bottom" + str(i))
bottom_stop_one = document.createElement("stop")
bottom_stop_one.setAttribute("offset", "0")
bottom_stop_one.setAttribute("stop-color", secondary_color)
bottom_stop_one.setAttribute("stop-opacity", str(opacity))
bottom_stop_two = document.createElement("stop")
bottom_stop_two.setAttribute("offset", "1")
bottom_stop_two.setAttribute("stop-color", secondary_color)
bottom_stop_two.setAttribute("stop-opacity", "0")
composite_gradient_bottom.appendChild(bottom_stop_one)
composite_gradient_bottom.appendChild(bottom_stop_two)
self.gradients_group.appendChild(composite_gradient_bottom)
# Class that mimics d3 scaleLinear() for x-axis of plot
class XScale:
def __init__(self, plot):
svg_width = plot.width - (plot.margins.get('right') + plot.margins.get('left'))
self.domain = [plot.xmin, plot.xmax, plot.xmax - plot.xmin]
self.range = [plot.margins.get('left'), plot.width - plot.margins.get('right'), svg_width]
self.zero = svg_width * (abs(plot.xmin) / (abs(plot.xmin) + abs(plot.xmax))) + plot.margins.get('left')
# Returns position given bp
def get(self, value):
return (self.range[2] / self.domain[2]) * value + self.zero
# Returns bp given position
def inverse(self, value):
return (value - self.zero) * (self.domain[2] / self.range[2])
# Class that mimics d3 scaleLinear() for y-axis of plot
class YScale:
def __init__(self, plot):
svg_height = plot.height - (plot.margins.get('top') + plot.margins.get('bottom'))
self.domain = [plot.ymin, plot.ymax, abs(plot.ymax) + abs(plot.ymin)]
self.range = [plot.margins.get('top'), plot.height - plot.margins.get('bottom'), svg_height]
self.zero = svg_height * (abs(plot.ymax) / (abs(plot.ymin) + abs(plot.ymax))) + plot.margins.get('top') if plot.combined is False else self.range[1]
# Returns position on svg given occupancy
def get(self, value):
return self.zero - (self.range[2] / self.domain[2]) * value
# Returns occupancy given position
def inverse(self, value):
return (value - self.zero) * (self.domain[2] / self.range[2])