index.html 13.9 KB
Newer Older
jhoogenboom's avatar
jhoogenboom committed
1
2
3
4
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
5
    <title>Background Noise Visualisation - FDSTools</title>
jhoogenboom's avatar
jhoogenboom committed
6
7
8
9
10
11
12
    <!-- BEGIN_LIBRARIES -->
    <script src="http://vega.github.io/vega-editor/vendor/d3.min.js"></script>
    <script src="http://vega.github.io/vega/vega.min.js"></script>
    <!-- END_LIBRARIES -->
    <style>
        * {
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
jhoogenboom's avatar
jhoogenboom committed
13
            font-size: 10pt;
jhoogenboom's avatar
jhoogenboom committed
14
15
        }
        body {
jhoogenboom's avatar
jhoogenboom committed
16
            margin: 0px;
jhoogenboom's avatar
jhoogenboom committed
17
18
19
        }
        div.options {
            position: absolute;
jhoogenboom's avatar
jhoogenboom committed
20
21
            padding: 5px;
            background-color: rgba(255, 255, 255, 0.8);
jhoogenboom's avatar
jhoogenboom committed
22
23
            z-index: 10;
        }
jhoogenboom's avatar
jhoogenboom committed
24
25
26
27
28
29
30
31
        table.optiongroup {
            padding: 10px 0px 0px 0px;
            margin: 0px;
            border-spacing: 0px;
            border: none;
        }
        table.optiongroup td {
            padding-right: 10px;
jhoogenboom's avatar
jhoogenboom committed
32
        }
jhoogenboom's avatar
jhoogenboom committed
33
34
35
36
37
38
39
40
        table.optiongroup th {
            text-align: left;
            font-weight: bold;
            padding-top: 5px;
        }
        #optionsheader {
            cursor: pointer;
            font-variant: small-caps;
jhoogenboom's avatar
jhoogenboom committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
            border-bottom: 1px dashed black;
        }
        div#vis {
            position: absolute;
            overflow: auto;
            bottom: 0px;
            top: 0px;
            right: 0px;
            left: 0px;
            text-align: right;
        }
    </style>
</head>
<body>
    <div class="options">
        <strong id="optionsheader">Options</strong><br>
        <div id="options">
jhoogenboom's avatar
jhoogenboom committed
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
            <table class="optiongroup" id="fileselectgroup" style="display: none">
                <tr>
                    <th>Input file</th>
                </tr>
                <tr>
                    <td>Raw background noise data file:</td>
                </tr>
                <tr>
                    <td><input id="fileselect" type="file"></td>
                </tr>
                <tr>
                    <td>(or drag a file onto this page)</td>
                </tr>
            </table>
            <table class="optiongroup">
                <tr>
                    <th colspan="2">Filtering options</th>
                </tr>
                <tr>
                    <td>Require at least</td>
                    <td><input type="text" value="15" id="minN" size="3"> reads</td>
                </tr>
                <tr>
                    <td>Require at least</td>
                    <td><input type="text" value="0.5" id="minP" size="3">% of highest allele per marker</td>
                </tr>
                <tr>
                    <td>Marker name</td>
jhoogenboom's avatar
jhoogenboom committed
86
                    <td><input type="text" id="markerFilter" size="20" title="Supports regular expression syntax: e.g., use '.*' to match anything."></td>
jhoogenboom's avatar
jhoogenboom committed
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
                </tr>
            </table>
            <table class="optiongroup">
                <tr>
                    <th colspan="3">Display options</th>
                </tr>
                <tr>
                    <td>Graph width</td>
                    <td colspan="2"><input type="text" value="600" id="graphwidth" size="3"> px</td>
                </tr>
                <tr>
                    <td>Sequence spacing</td>
                    <td colspan="2"><input type="text" value="15" id="barwidth" size="3"> px</td>
                </tr>
                <tr>
                    <td>Subgraph spacing</td>
                    <td colspan="2"><input type="text" value="70" id="subgraphoffset" size="3"> px</td>
                </tr>
                <tr>
                    <td>Axis scale</td>
                    <td><input type="radio" name="scale" value="linear" id="scaleLinear" checked> Linear</td>
                    <td><input type="radio" name="scale" value="log" id="scaleLog"> Logarithmic
                    </td>
                </tr>
                <tr>
jhoogenboom's avatar
jhoogenboom committed
112
                    <td>Renderer</td>
jhoogenboom's avatar
jhoogenboom committed
113
114
115
116
                    <td><input type="radio" name="renderer" value="svg" id="renderSVG" checked> SVG</td>
                    <td><input type="radio" name="renderer" value="canvas" id="renderCanvas"> Canvas</td>
                </tr>
            </table>
jhoogenboom's avatar
jhoogenboom committed
117
118
119
120
121
122
            <a id="saveLink" href="javascript:void(saveImage())" style="display: none">Save image</a>
        </div>
    </div>
    <div id="vis"></div>
    <script type="text/javascript">
        var graph = false;
jhoogenboom's avatar
jhoogenboom committed
123
        var fileName = "graph";
jhoogenboom's avatar
jhoogenboom committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        function parse(){
            vg.parse.spec(graph_spec, function(chart){
                var rendererName = "canvas";
                if(document.getElementById("renderSVG").checked)
                    rendererName="svg";
                graph = chart({el: "#vis", renderer: rendererName});
                graph.update();
                document.getElementById("saveLink").style.display = "inline";

                //Scroll to the right; the graph is more interesting than the long labels.
                var visdiv = document.getElementById("vis");
                visdiv.scrollLeft = visdiv.scrollWidth;
            });
        }

        function setScale(value){
            if(!graph_spec)
                return;
            for(i in graph_spec["marks"])
                if(graph_spec["marks"][i]["scales"])
                    for(j in graph_spec["marks"][i]["scales"])
jhoogenboom's avatar
jhoogenboom committed
145
                        if(graph_spec["marks"][i]["scales"][j]["name"] == "x")
jhoogenboom's avatar
jhoogenboom committed
146
147
148
149
150
151
                            graph_spec["marks"][i]["scales"][j]["type"] = value;

            if(graph)
                parse();
        }

jhoogenboom's avatar
jhoogenboom committed
152
153
154
155
156
157
158
159
160
161
162
        function getScale(){
            if(!graph_spec)
                return "linear";
            for(i in graph_spec["marks"])
                if(graph_spec["marks"][i]["scales"])
                    for(j in graph_spec["marks"][i]["scales"])
                        if(graph_spec["marks"][i]["scales"][j]["name"] == "x")
                            return graph_spec["marks"][i]["scales"][j]["type"];
            return "linear";
        }

jhoogenboom's avatar
jhoogenboom committed
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
        function setDataFormulaTransformValue(dataname, fieldname, value){
            if(!graph_spec)
                return false;
            for(i in graph_spec["data"]){
                if(graph_spec["data"][i]["name"] == dataname){
                    for(j in graph_spec["data"][i]["transform"]){
                        if(graph_spec["data"][i]["transform"][j]["type"] == "formula" && graph_spec["data"][i]["transform"][j]["field"] == fieldname){
                            graph_spec["data"][i]["transform"][j]["expr"] = "" + value;
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        function getDataFormulaTransformValue(dataname, fieldname){
            if(!graph_spec)
                return false;
            for(i in graph_spec["data"]){
                if(graph_spec["data"][i]["name"] == dataname){
                    for(j in graph_spec["data"][i]["transform"]){
                        if(graph_spec["data"][i]["transform"][j]["type"] == "formula" && graph_spec["data"][i]["transform"][j]["field"] == fieldname){
                            return graph_spec["data"][i]["transform"][j]["expr"];
                        }
                    }
                }
            }
            return false;
        }

        function setRenderer(value){
            if(graph)
jhoogenboom's avatar
jhoogenboom committed
196
                graph.renderer(value).update();
jhoogenboom's avatar
jhoogenboom committed
197
198
199
200
        }

        //Load the data (input is a fileList object; only the first file is loaded).
        function loadDataset(fileList){
201
            if(!graph_spec || !fileList || !fileList.length)
jhoogenboom's avatar
jhoogenboom committed
202
203
                return;
            var reader = new FileReader();
jhoogenboom's avatar
jhoogenboom committed
204
205
206
207
208
            reader.onload = function(e){
                if(fileList && fileList.length && fileList[0].name)
                    fileName = fileList[0].name.substr(0, fileList[0].name.lastIndexOf("."));
                else
                    fileName = "graph";
jhoogenboom's avatar
jhoogenboom committed
209
210
211
212
213
214
215
216
217
                graph_spec["data"][0]["values"] = reader.result;
                parse();
            };
            reader.readAsText(fileList[0]);
        }

        //Save image function.
        function saveImage(){
            var link = document.getElementById("saveLink");
jhoogenboom's avatar
jhoogenboom committed
218
219
220
            var imageType = document.getElementById("renderSVG").checked? "svg": "png";
            link.setAttribute("href", graph.toImageURL(imageType));
            link.setAttribute("download", fileName + "." + imageType);
jhoogenboom's avatar
jhoogenboom committed
221
222
            link.click();
            link.setAttribute("href", "javascript:void(saveImage())");
223
            link.removeAttribute("download");
jhoogenboom's avatar
jhoogenboom committed
224
225
226
227
228
            return false;
        }

        function onLoadSpec(has_data){
            if(!has_data){
jhoogenboom's avatar
jhoogenboom committed
229
                //Allow files to be dragged onto the page.
jhoogenboom's avatar
jhoogenboom committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
                document.addEventListener('dragover', function(evt){
                    evt.stopPropagation();
                    evt.preventDefault();
                }, false);
                document.addEventListener('drop', function(evt){
                    evt.stopPropagation();
                    evt.preventDefault();
                    loadDataset(evt.dataTransfer.files);
                }, false);

                //Handle files from the file input.
                document.getElementById("fileselect").addEventListener('change', function(){
                    loadDataset(document.getElementById("fileselect").files);
                }, false);
jhoogenboom's avatar
jhoogenboom committed
244
                document.getElementById("fileselectgroup").style.display = "table";
jhoogenboom's avatar
jhoogenboom committed
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
            }

            //Update graph when rendering mode or axis scale is changed.
            document.getElementById("renderCanvas").addEventListener('change', function(){
                setRenderer(this.value);
            }, false);
            document.getElementById("renderSVG").addEventListener('change', function(){
                setRenderer(this.value);
            }, false);
            document.getElementById("scaleLinear").addEventListener('change', function(){
                setScale(this.value);
            }, false);
            document.getElementById("scaleLog").addEventListener('change', function(){
                setScale(this.value);
            }, false);
            document.getElementById("graphwidth").addEventListener('change', function(){
                var value = parseFloat(this.value);
                if(isNaN(value))
                    return;
                graph_spec["width"] = value;
                if(graph)
                    parse();
            }, false);
            document.getElementById("barwidth").addEventListener('change', function(){
                var value = parseFloat(this.value);
                if(isNaN(value))
                    return;
                if(setDataFormulaTransformValue("yscale", "barwidth", value) && graph)
                    parse();
            }, false);
            document.getElementById("subgraphoffset").addEventListener('change', function(){
                var value = parseFloat(this.value);
                if(isNaN(value))
                    return;
                if(setDataFormulaTransformValue("yscale", "subgraphoffset", value) && graph)
                    parse();
            }, false);
            document.getElementById("minN").addEventListener('change', function(){
                var value = parseFloat(this.value);
                if(isNaN(value))
                    return;
                if(setDataFormulaTransformValue("table", "amplitude_threshold", value) && graph)
                    parse();
            }, false);
            document.getElementById("minP").addEventListener('change', function(){
                var value = parseFloat(this.value);
                if(isNaN(value))
                    return;
                if(setDataFormulaTransformValue("table", "amplitude_pct_threshold", value) && graph)
                    parse();
            }, false);
            document.getElementById("markerFilter").addEventListener('change', function(){
                if(setDataFormulaTransformValue("table", "filter_marker", "'" + this.value + "'") && graph)
                    parse();
            }, false);

            //Toggle options visibility.
            document.getElementById("optionsheader").addEventListener('click', function(){
                var opts = document.getElementById("options");
                if(opts.style.display == "none")
                    document.getElementById("options").style.display = "block";
                else
                    document.getElementById("options").style.display = "none";
            }, false);

            //Sync graph_spec and display.
jhoogenboom's avatar
jhoogenboom committed
311
312
313
314
315
316
317
318
            if(getScale() == "linear"){
                document.getElementById("scaleLinear").checked = true;
                document.getElementById("scaleLog").checked = false;
            }
            else{
                document.getElementById("scaleLinear").checked = false;
                document.getElementById("scaleLog").checked = true;
            }
jhoogenboom's avatar
jhoogenboom committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
            document.getElementById("markerFilter").value = getDataFormulaTransformValue("table", "filter_marker").replace(/^(['"]?)(.*(?=\1$))\1$/, '$2');
            document.getElementById("graphwidth").value = graph_spec["width"];
            document.getElementById("barwidth").value = getDataFormulaTransformValue("yscale", "barwidth");
            document.getElementById("subgraphoffset").value = getDataFormulaTransformValue("yscale", "subgraphoffset");
            document.getElementById("minN").value = getDataFormulaTransformValue("table", "amplitude_threshold");
            document.getElementById("minP").value = getDataFormulaTransformValue("table", "amplitude_pct_threshold");
            if(has_data){
                document.getElementById("options").style.display = "none";
                parse();
            }
        }
    </script>
    <!-- BEGIN_LOAD_SCRIPT -->
    <script type="text/javascript">
        var graph_spec = false;
        vg.util.load({url: "bgrawvis.json"}, function(err, result){
            graph_spec = JSON.parse(result);
            onLoadSpec(false);
        });
    </script>
    <!-- END_LOAD_SCRIPT -->
</body>
</html>