-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
180 lines (148 loc) · 4.93 KB
/
functions.js
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
// https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color
function decideTextColour(bgColor, lightColor = "#FFFFFF", darkColor = "#000000") {
var color = (bgColor.charAt(0) === '#') ? bgColor.substring(1, 7) : bgColor;
var r = parseInt(color.substring(0, 2), 16); // hexToR
var g = parseInt(color.substring(2, 4), 16); // hexToG
var b = parseInt(color.substring(4, 6), 16); // hexToB
return (((r * 0.299) + (g * 0.587) + (b * 0.114)) > 186) ?
darkColor : lightColor;
}
function saveAs(text, filename) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
}
function deselectWells() {
$.each($(".ui-selected"), function() {
$(this).removeClass("ui-selected")
})
}
$(function() {
$("#selectable").selectable();
})
function setWellColour(id, colour) {
$("#" + id).css("background", colour)
$("#" + id).css("color", decideTextColour(colour))
}
function setWellAnnotation(id, value) {
$("#" + id).attr("annotation", value)
}
function setWellTitle(id, value) {
$("#" + id).attr("title", value)
}
function addAnnotations() {
var annotation = $("[name='annotation']").val()
var colour = $("[name='colour']").val()
$(".ui-selected").each(function() {
setWellColour(this.id, colour)
setWellAnnotation(this.id, annotation)
setWellTitle(this.id, `${this.id}: ${annotation}`)
})
deselectWells()
drawLegend()
}
function addAnnotationsFromArray(annotations) {
// given an array of annotations, parsed from a file
// add these annotations to the on-screen wells
for (const annotation of annotations) {
setWellColour(annotation.id, annotation.colour)
setWellAnnotation(annotation.id, annotation.annotation)
setWellTitle(annotation.id, `${annotation.id}: ${annotation.annotation}`)
}
drawLegend()
}
function rgbStringToHex(rgb) {
// convert style RGB string to colour hex code
// e.g "rgb(255, 255, 255)" -> "#FFFFFF"
var a = rgb.split("(")[1].split(")")[0]
a = a.split(",")
var b = a.map(function(x) {
x = parseInt(x).toString(16)
return (x.length == 1) ? "0" + x : x
})
return "#" + b.join("")
}
function exportAnnotations() {
let excl_empty = $("#chk-excl-empty").prop("checked")
var csv = "well,annotation,colour\n"
// loop though wells, get well-id and annotation
$("#selectable li").each(function() {
well = this.id
var annotation = $(this).attr("annotation")
var rgbString = $(this).prop("style")["background-color"]
if (rgbString.length > 0) {
var colourHex = rgbStringToHex(rgbString)
} else {
var colourHex = ""
}
if (excl_empty && annotation == "") {
return true
}
csv += `${well},${annotation},${colourHex}\n`
})
saveAs(csv, "platemap.csv")
}
const importAnnotations = async (event) => {
// read in plate annotation from previously exported file
// TODO: parse file to annotations
// TODO: apply annotations to on-screen elements
const file = event.target.files[0]
const fileContents = await readUploadedFileAsText(file)
let annotations = parseCSV(fileContents)
addAnnotationsFromArray(annotations)
}
const readUploadedFileAsText = (inputFile) => {
// I hate javascript so so much
const temporaryFileReader = new FileReader();
return new Promise((resolve, reject) => {
temporaryFileReader.onerror = () => {
temporaryFileReader.abort()
reject(new DOMException("Problem parsing input file."))
};
temporaryFileReader.onload = () => {
resolve(temporaryFileReader.result);
}
temporaryFileReader.readAsText(inputFile)
});
};
function parseCSV(csvText) {
// parse csv text into an array of objects {id, annotation, colour}
var annotations = []
var well, annotation, colour
const lines = csvText.split("\n")
for (var i = 1; i < lines.length; i++) {
if (lines[i] != "") {
[well, annotation, colour] = lines[i].split(",")
annotations.push({ id: well, annotation: annotation, colour: colour })
}
}
console.log(annotations)
return annotations
}
function getUniqueAnnotations() {
// scan through selectable elements and return an array of
// unique colour + annotation
var annotations = new Set()
$("#selectable li").each(function() {
colour = $(this).attr("style")
annotation = $(this).attr("annotation")
if (annotation == "") { return true }
annotations.add(`<li><span style="${colour}">${annotation}</span></li>`)
})
return Array.from(annotations)
}
function buildLegendHtml(annotations) {
// create HTML for legend from an array of [[colour, annotation-name]]
var list = `<ul>`
for (i = 0; i < annotations.length; i++) {
list += annotations[i]
}
list += "</ul>"
return list
}
function drawLegend() {
var annotations = getUniqueAnnotations()
var legendHtml = buildLegendHtml(annotations)
$("#legend").html(legendHtml)
}