index.html 32.2 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html>
<head>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    <!--
    # Copyright (C) 2016 Jerry Hoogenboom
    #
    # This file is part of FDSTools, data analysis tools for Next
    # Generation Sequencing of forensic DNA markers.
    #
    # FDSTools is free software: you can redistribute it and/or modify it
    # under the terms of the GNU General Public License as published by the
    # Free Software Foundation, either version 3 of the License, or (at
    # your option) any later version.
    #
    # FDSTools is distributed in the hope that it will be useful, but
    # WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    # General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with FDSTools.  If not, see <http://www.gnu.org/licenses/>.
    -->
23
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
24
    <meta name="viewport" content="width=device-width, initial-scale=1">
25
    <meta charset="UTF-8">
26
    <title>Stutter Model Visualisation - FDSTools</title>
27
    <!-- VERSION 2.0.3 -->
28
    <!-- BEGIN_LIBRARIES -->
29 30
    <script src="https://vega.github.io/vega-editor/vendor/d3.min.js"></script>
    <script src="https://vega.github.io/vega/vega.min.js"></script>
31 32
    <!-- END_LIBRARIES -->
    <style>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    input {
        font-family: inherit;
        font-size: 1em;
    }
    .icon {
        padding: 1em;
        margin-right: .25em;
        background-size: contain;
        background-position: center;
        background-repeat: no-repeat;
    }
    @media screen {
        html, body {
            /* First the UI fonts of WinVista+, OSX10.11, OSX10.10, OSX10.9-, WinXP-, Ubuntu. Then some fallbacks. */
            font-family: Segoe UI, San Francisco, Helvetica Neue, Lucida Grande, Tahoma, Ubuntu, Helvetica, Arial, sans-serif;
48 49 50
            font-size: 10pt;
        }
        body {
51
            margin: 0rem;
52
        }
53 54 55 56
        a {
            color: inherit;
            text-decoration: inherit;
            border-bottom: 1px dotted;
57
        }
58 59
        a:hover {
            border-bottom: 1px solid;
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
        a.disabled {
            color: hsl(220, 20%, 53%);
        }
        a.disabled:hover {
            color: hsl(220, 20%, 53%);
            border-bottom: 1px dotted;
            cursor: default;
        }
        #outer {
            display: flex;
            flex-flow: column nowrap;
            height: 100vh;
            overflow: hidden;
        }
        #inner {
            display: flex;
            flex-flow: row nowrap;
            flex: 1 1 auto;
            overflow: hidden;
        }
        #optscrollbox {
            display: flex;
            flex-flow: column nowrap;
            flex: 0 0 auto;
            width: 22.5em;
            order: -1;
            overflow: auto;
            background-color: hsl(220, 20%, 77%);
            border-right: 1px solid hsl(220, 20%, 3%);
            border-bottom: none;
            cursor: default;
        }
        #visscrollbox {
            flex: 1 1 auto;
            overflow: auto;
            background-color: hsl(220, 20%, 97%); /* eef8ff */
            text-align: right;
        }
        .optiongroup {
            flex: 0 0 auto;
            padding: .2rem .2rem 1rem;
        }
        .optiongroup:last-child {
            padding: .2rem;
105
        }
106 107 108 109
        .optiongroup > table {
            margin: 0rem;
            border-spacing: 0rem;
            border: none;
110
        }
111
        .optiongroup > .optionheader {
112 113
            text-align: left;
            font-weight: bold;
114
            text-transform: uppercase;
115
        }
116 117
        .optiongroup > .optionheader:first-letter {
            font-size: 1.25em;
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
        .optiongroup > table td {
            white-space: nowrap;
        }
        .optiongroup > table td:not(:last-child) {
            padding-right: .4rem;
        }
        .optiongroup > table tr.fileselectrow, .optiongroup > table tr.fileselect2row, .optiongroup > table tr.filenamedisplayrow {
            display: none;
        }
        .optiongroup > table tr.filenamedisplayrow #filenamedisplay {
            max-width: 20em;
            width: auto;
        }
        .optiongroup > table td.footnote {
            font-style: italic;
            white-space: wrap;
        }
        .optiongroup > table select {
            font-size: inherit;
            font-family: inherit;
        }
        .help {
            cursor: help;
            font-weight: bold;
            border-bottom: 1px dotted hsl(220, 20%, 3%);
        }
        .hidden {
            display: none;
        }
        .special-option {
            font-style: italic;
        }
        #vis {
            direction: rtl;
            display: inline-block;
        }
        #vis div {
            direction: ltr;
        }
        #vis div:nth-child(2n+3) {
            border-top: 1px solid hsl(220, 20%, 3%);
            padding-top: .8rem;
            margin-top: .8rem;
        }
        #vis div:last-child {
            padding-bottom: .8rem;
        }
        td.num {
167 168
            text-align: right;
        }
169 170 171 172
        td.wrap {
            white-space: normal !important;
            width: 1em;
        }
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
        input[type=range]::-moz-range-track {
            background-image: linear-gradient(to right, #eee, #000);
            border: 1px solid black;
            height: 11px;
        }
        input[type=range]::-webkit-slider-runnable-track {
            background-image: linear-gradient(to right, #eee, #000);
            border: 1px solid black;
            height: 11px;
        }
        input[type=range]::-webkit-slider-thumb {
            margin-top: -6px;
        }
        input[type=range]::-ms-track {
            background-image: linear-gradient(to right, #eee, #000);
            border: 1px solid black;
            height: 11px;
        }
        input[type=range]::-ms-fill-lower, input[type=range]::-ms-fill-upper {
            background: transparent;
        }
        input[type=range] {
            margin: 0px;
            padding: 0px;
            width: 150px;
        }
        td.jitterleft {
           text-align: right;
        }
        td.jittermiddle {
           text-align: center;
        }
        td.jitterleft {
           text-align: left;
        }
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
    }
    /*
    portrait, height >= 55:

      small collapsed   large collapsed   small top    large top
    |-----------------|-----------------|------------|------------>  width
    0                 45                60           68


    portrait, height < 55:

      small collapsed   large collapsed
    |-----------------|------------------------------------------->  width
    0                 45


    landscape:

      small collapsed   large collapsed   small left   large left
    |-----------------|-----------------|------------|------------>  width
    0                 45                60           68
    */
    @media only screen and (min-aspect-ratio: 40000/30000) and (min-width: 60em) and (max-width: 68em), /* Full menu is at the side, but viewport is not very wide */
           only screen and (max-aspect-ratio: 40000/30000) and (min-height: 55em) and (min-width: 60em) and (max-width: 68em), /* Full menus at the top, but viewport not wide enough */
           only screen and (max-width: 45em) { /* Very narrow; menu names don't fit anymore */
        /* Limited width: make menu smaller */
        #optscrollbox {
            font-size: .85em;
        }
    }
    @media only screen and (max-aspect-ratio: 40000/30000), only screen and (max-width: 60em) {
        /* Portrait or near-portrait, or rather narrow: menu at the top */
        #inner {
            flex-direction: column;
        }
        #optscrollbox {
            flex: 0 0 auto;
            flex-flow: row nowrap;
            width: 100%;
            border-right: none;
            border-bottom: 1px solid hsl(220, 20%, 3%);
        }
        #visscrollbox {
            flex: 1 1 auto;
        }
        .optiongroup {
            flex: 1 0 auto;
            padding: .2rem 1rem .2rem .2rem;
        }
    }
    @media only screen and (max-aspect-ratio: 40000/30000) and (max-height: 55em), only screen and (max-width: 60em) {
        /* Portrait or near-portrait combined with very limited height, or rather narrow: collapse menu */
        #optscrollbox {
            overflow: visible;
            z-index: 100;
            border-bottom: 1px solid hsl(220, 20%, 3%);
        }
        .optiongroup {
            position: relative;
        }
        .optiongroup > table {
            display: none;
            position: absolute;
            top: 100%;
            background-color: hsl(220, 20%, 77%);
            border: 1px solid hsl(220, 20%, 3%);
            border-top: none;
        }
        .optiongroup:last-child > table {
            left: auto;
            right: .2rem;
        }
        .optiongroup:hover > .optionheader {
            color: hsl(220, 20%, 97%);
        }
        .optiongroup:hover > table {
            display: table;
        }
    }

    @media print {
        html, body {
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
            font-size: 10pt;
        }
        .noprint, .help, .hidden {
            display: none;
        }
        #optscrollbox {
            display: flex;
            box-sizing: border-box;
            flex-flow: row wrap;
            align-items: stretch;
            width: 100%;
            border-top: 1pt solid black;
            border-right: 1pt solid black;
            font-size: 7pt;
        }
        .optiongroup {
            flex-grow: 1;
            padding: 5pt;
            border-left: 1pt solid black;
            border-bottom: 1pt solid black;
        }
        .optiongroup > table {
            padding: 0px;
            margin: 0px;
            border-spacing: 0px;
            border: none;
        }
        .optiongroup > .optionheader {
            text-align: left;
            font-weight: bold;
            text-transform: uppercase;
        }
        .optiongroup > .optionheader:first-letter {
            font-size: 1.25em;
        }
        .optiongroup > table td {
            white-space: nowrap;
        }
        .optiongroup > table td:not(:last-child) {
            padding-right: 5pt;
        }
        .optiongroup > table td.footnote {
            font-style: italic;
            white-space: wrap;
        }
        .optiongroup > table input {
            border: none;
        }
        .optiongroup > table select {
            /* No dropdow box arrow on FF and Webkit-based browsers */
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            font-size: inherit;
            border: none;
        }
        select::-ms-expand {
            /* No dropdown box arrow on IE10+ */
            display: none;
        }
        .optiongroup > table input[type=checkbox] {
            height: 1em;
            width: 1em;
        }
        #vis {
            width: 100%;
        }
        #vis > div {
            width: 100%;
            direction: rtl;
            text-align: right;
        }
        #vis > div > * {
            direction: ltr;
        }
        .vega, canvas {
            max-width: 100%;
            height: auto;
            overflow: visible;
        }
        svg {
            /* Using slightly smaller width to prevent clipping in Chrome */
            max-width: 98%;
            height: auto;
            overflow: visible;
        }
        canvas {
            object-fit: scale-down;
        }
        .num {
            text-align: right;
        }
        td.wrap {
            white-space: normal !important;
        }
    }
387 388 389
    </style>
</head>
<body>
390 391 392 393 394 395 396
<div id="outer">
<div id="inner">
    <div id="optscrollbox">
        <div class="optiongroup noprint">
            <div class="optionheader">Input &amp; output</div>
            <table>
                <tr class="fileselectrow filenamedisplayrow">
jhoogenboom's avatar
jhoogenboom committed
397
                    <td>Stutter model file:</td>
398
                </tr>
399
                <tr class="fileselectrow">
400 401
                    <td><input id="fileselect" type="file"></td>
                </tr>
402
                <tr class="fileselectrow">
403 404
                    <td>(or drag a file onto this page)</td>
                </tr>
405 406
                <tr class="filenamedisplayrow">
                    <td id="filenamedisplay" class="wrap"></td>
407 408
                </tr>
                <tr>
409 410 411 412 413 414 415 416 417 418
                    <td>
                        <a id="saveImageLink" href="javascript:void(saveImage())" class="disabled">Save image</a>
                    </td>
                </tr>
            </table>
        </div>
        <div class="optiongroup noprint">
            <div class="optionheader">Raw data points</div>
            <table>
                <tr class="fileselect2row">
419 420
                    <td colspan="4">Raw data points file:</td>
                </tr>
421
                <tr class="fileselect2row">
422 423
                    <td colspan="4"><input id="fileselect2" type="file"></td>
                </tr>
424 425 426
                <tr class="fileselect2row">
                    <td colspan="4">(or drag a file onto this page)</td>
                </tr>
427
                <tr>
428 429 430 431
                    <td><label for="jitter">Jitter</label></td>
                    <td class="jitterleft"><label for="jitter">Low</label></td>
                    <td class="jittermiddle"><label><input type="range" min="0" max="100" value="25" step="any" id="jitter"></label></td>
                    <td class="jitterright"><label for="jitter">High</label></td>
432 433
                </tr>
            </table>
434 435 436 437
        </div>
        <div class="optiongroup">
            <div class="optionheader">Filtering options</div>
            <table>
438
                <tr>
439 440 441 442 443 444 445
                    <td rowspan="2"><label for="unitSelect">Repeat unit</label></td>
                    <td>
                        <select id="unitSelect">
                            <option value="" class="special-option" selected>Show all</option>
                            <option value="CUSTOM" class="special-option">Custom filter</option>
                        </select>
                    </td>
446 447
                </tr>
                <tr>
448 449 450 451 452
                    <td id="unitFilterRow" class="hidden">
                        <label>
                            <input type="text" id="unitFilter" size="20">
                            <span class="help" title="Type AG to show all repeat units containing 'AG'. Type =AG to show the 'AG' repeat unit only. Separate multiple values with spaces.">?</span>
                       </label>
453
                    </td>
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
454
                </tr>
455
                <tr>
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
456 457 458 459 460 461 462
                    <td><label for="foldSelect">Stutter amount</label></td>
                    <td>
                        <select id="foldSelect">
                            <option value="" class="special-option" selected>Show all</option>
                        </select>
                    </td>
                </tr>
463
                <tr>
464
                    <td><label for="markerFilter">Marker name</label></td>
465
                    <td>
466 467 468 469
                        <label>
                            <input type="text" id="markerFilter" size="20">
                            <span class="help" title="Type xyz to show all markers with 'xyz' in their names. Type =xyz to show only the marker 'xyz'. Separate multiple values with spaces.">?</span>
                        </label>
470
                    </td>
471
                </tr>
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
472 473 474 475 476 477 478 479 480
                <tr>
                    <td><label for="r2Filter">Minimum <i>r</i><sup>2</sup></label></td>
                    <td>
                        <label>
                            <input type="text" id="r2Filter" size="20">
                            <span class="help" title="R squared, the coefficient of determination, is 1 if the fit explains the data as good as possible and lower otherwise.">?</span>
                        </label>
                    </td>
                </tr>
jhoogenboom's avatar
jhoogenboom committed
481
                <tr>
482 483
                    <td><label for="allDataFilter">Fit to all data</label></td>
                    <td><label><input type="checkbox" id="allDataFilter" checked> show</label></td>
jhoogenboom's avatar
jhoogenboom committed
484
                </tr>
485
            </table>
486 487 488 489
        </div>
        <div class="optiongroup noprint">
            <div class="optionheader">Display options</div>
            <table>
490 491
                <tr>
                    <td>Graph dimensions</td>
492
                    <td colspan="2"><input type="text" value="600" id="graphwidth" size="3"> x <input type="text" value="400" id="graphheight" size="3"> px</td>
493 494
                </tr>
                <tr>
495 496
                    <td><label for="subgraphoffset">Subgraph spacing</label></td>
                    <td colspan="2"><label><input type="text" value="70" id="subgraphoffset" size="3"> px</label></td>
497 498
                </tr>
                <tr>
jhoogenboom's avatar
jhoogenboom committed
499
                    <td>Renderer</td>
500 501
                    <td><label><input type="radio" name="renderer" value="svg" id="renderSVG" checked> SVG</label></td>
                    <td><label><input type="radio" name="renderer" value="canvas" id="renderCanvas"> Canvas</label></td>
502 503 504
                </tr>
            </table>
        </div>
505
    </div>
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    <div id="visscrollbox">
        <div id="vis"></div>
    </div>
</div>
</div>
<script type="text/javascript">
var graph = false;
var data_format = {};
var data_values = [];
var fileName = "stuttermodel";
var stamp = 0;
function parse(){
    var this_stamp = ++stamp;
    var visdiv = document.getElementById("vis");
    var scrolldiv = document.getElementById("visscrollbox");
    var rendererName = document.querySelector("input[name=renderer]:checked").value;
    removeChildren(visdiv);
    document.getElementById("saveImageLink").setAttribute("class", "disabled");
    vg.parse.spec(graph_spec, function(chart){
        //Cancel rendering if a new parse() call was made.
        if(this_stamp != stamp)
            return;
528

529 530 531 532
        //Add new visualisation divs.
        var newvis = document.createElement("div");
        newvis.setAttribute("id", "vis0");
        visdiv.appendChild(newvis);
533

534 535 536 537 538
        graph = chart({el: newvis, renderer: rendererName});
        graph.update();
        if(rendererName == "svg")
            updateViewBox(graph._el.childNodes[0]);
        document.getElementById("saveImageLink").setAttribute("class", "");
539

540 541 542 543
        //Scroll to the right; the graph is more interesting than the long labels.
        scrolldiv.scrollLeft = scrolldiv.scrollWidth;
    });
}
544

545 546 547
function compileFilter(filter){
    return new RegExp('(?:' + filter.replace(/(^| )=(.*?)(?= |$)/g, '$1^$2$$').replace(/ +/g, ')|(?:') + ')')
}
548

549 550 551 552 553
function updateViewBox(svg){
    //Setting the viewBox enables shrink-to-fit behaviour when printing.
    svg.setAttribute("viewBox",
        "0 0 " + svg.getAttribute("width") + " " + svg.getAttribute("height"));
}
554

555 556 557 558 559
function removeChildren(el){
    while(el.lastChild){
      el.removeChild(el.lastChild);
    }
}
560

561 562 563 564 565 566 567 568 569 570 571
function setSignalValue(signalname, value){
    if(!graph_spec)
        return;
    var scrolldiv = document.getElementById("visscrollbox");
    var scrollRight = (scrolldiv.scrollLeft >= scrolldiv.scrollWidth - scrolldiv.clientWidth);
    if(signalname == "width")
        graph_spec.width = value;
    for(i in graph_spec.signals){
        if(graph_spec.signals[i].name == signalname){
            graph_spec.signals[i].init = value;
            break;
572
        }
573 574 575 576 577 578 579 580 581 582
    }
    if(graph){
        graph.signal(signalname, value).padding("auto").update();
        if(document.getElementById("renderSVG").checked)
            updateViewBox(graph._el.childNodes[0]);
    }
    if(scrollRight)
        scrolldiv.scrollLeft = scrolldiv.scrollWidth;
    return;
}
583

584 585 586 587 588 589 590 591 592 593
function getSignalValue(signalname){
    if(!graph_spec)
        return false;
    if(signalname == "width")
        return graph_spec.width;
    for(i in graph_spec.signals)
        if(graph_spec.signals[i].name == signalname)
            return graph_spec.signals[i].init;
    return false;
}
594

595 596 597 598 599 600 601
function setRenderer(value){
    if(graph){
        graph.renderer(value).update();
        if(value == "svg")
            updateViewBox(graph._el.childNodes[0]);
    }
}
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
function setFileName(value){
    if(!value)
        value = "stuttermodel";
    fileName = value;
    if(value == "stuttermodel")
        document.title = "Stutter Model Visualisation - FDSTools";
    else
        document.title = value + " - Stutter Model Visualisation - FDSTools";
}

function makeUnitSelectionOptions(data){
    var sel = document.getElementById("unitSelect");
    var val = sel.value;
    removeChildren(sel);
    var opt = document.createElement("option");
    opt.setAttribute("value", "");
    opt.setAttribute("class", "special-option");
    opt.innerText = "Show all";
    sel.appendChild(opt);

    var units = {}
    var validValue = (val == "CUSTOM");
    data.forEach(function(datum){
        units[datum.unit] = true;
    });
    Object.keys(units).sort(function(a, b){
        if(a.length == b.length){
            if(a < b)
                return -1;
            else if(a > b)
                return 1;
            return 0;
635
        }
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
        return a.length - b.length;
    }).forEach(function(unit){
        opt = document.createElement("option");
        opt.setAttribute("value", "=" + unit);
        opt.innerText = unit;
        sel.appendChild(opt);
        if(val == "=" + unit)
            validValue = true;
    });
    opt = document.createElement("option");
    opt.setAttribute("value", "CUSTOM");
    opt.setAttribute("class", "special-option");
    opt.innerText = "Custom filter";
    sel.appendChild(opt);
    sel.value = validValue? val : "";
    if(!validValue){
        document.getElementById("unitFilter").value = "";
        if(graph)  // Don't overwrite the filter_unit on initial data load.
            setSignalValue("filter_unit", "");
    }
}
657

Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
function makeFoldSelectionOptions(data){
    var sel = document.getElementById("foldSelect");
    var val = sel.value;
    removeChildren(sel);
    var opt = document.createElement("option");
    opt.setAttribute("value", "");
    opt.setAttribute("class", "special-option");
    opt.innerText = "Show all";
    sel.appendChild(opt);

    var folds = {}
    var validValue = false;
    data.forEach(function(datum){
        folds[datum.stutter] = true;
    });
    Object.keys(folds).sort(function(a, b){
        return parseFloat(a) - parseFloat(b);
    }).forEach(function(fold){
        opt = document.createElement("option");
        opt.setAttribute("value", fold);
        opt.innerText = (fold < 0? "\u2212" + (-fold) : "+" + fold) + " stutter";
        sel.appendChild(opt);
        if(val == fold)
            validValue = true;
    });
    sel.value = validValue? val : "";
    if(!validValue)
        if(graph)  // Don't overwrite the filter_fold on initial data load.
            setSignalValue("filter_fold", "");
}

689 690 691 692 693 694 695 696 697 698 699 700
var currentlyLoadedFitFunctionsFile = "no/file/loaded";
function loadFitFunctions(filename, data){
    if(filename == currentlyLoadedFitFunctionsFile)
        return false;
    currentlyLoadedFitFunctionsFile = filename;
    if(filename)
        setFileName(filename.substr(0, filename.lastIndexOf(".")));
    else
        setFileName(false);
    data_values = vg.util.read(data, data_format);
    graph_spec.data[0].values = data_values;
    makeUnitSelectionOptions(data_values);
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
701
    makeFoldSelectionOptions(data_values);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    return true;
}
var currentlyLoadedRawDataFile = "no/file/loaded";
function loadRawData(filename, data){
    if(filename == currentlyLoadedRawDataFile)
        return false;
    currentlyLoadedRawDataFile = filename;
    graph_spec.data[1].values = data;
    return !!graph;
}
function setSelectedFiles(input, fileList){
    //Try to clear the currently displayed file, then try to set
    //the drag-'n-dropped file as the selected one on the file input.
    //Both actions are not supported in all major browsers.
    input.value = "";
    input.files = fileList;
}
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
//Input is a fileList object; only the first file is loaded.
var embeddedFiles = 0;
function loadFile(fileList, setInput){
    if(!graph_spec || !fileList || !fileList.length)
        return;
    var reader = new FileReader();
    reader.onload = function(e){
        var success;
        var cols = reader.result.substr(0, reader.result.indexOf("\n")).split("\t");
        if(Math.min(cols.indexOf("length"), cols.indexOf("forward"), cols.indexOf("reverse")) == -1){
            //This is a file with fit functions.
            if((embeddedFiles & 1) == 0){
                success = loadFitFunctions(fileList[0].name, reader.result);
                if(setInput)
                    setSelectedFiles(document.getElementById("fileselect"), fileList);
735
            }
736 737 738 739 740 741 742 743 744 745 746 747
        }
        else if((embeddedFiles & 2) == 0){
            //This is a file with raw data points.
            success = loadRawData(fileList[0].name, reader.result);
            if(setInput)
                setSelectedFiles(document.getElementById("fileselect2"), fileList);
        }
        if(success)
            parse();
    };
    reader.readAsText(fileList[0]);
}
748

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
//Save image function.
function saveImage(){
    var link = document.getElementById("saveImageLink");
    if(link.getAttribute("class") == "disabled")
        return false;
    var imageType = document.getElementById("renderSVG").checked? "svg": "png";
    if(window.navigator.msSaveOrOpenBlob){
        //Internet Explorer has its own ways of doing things.
        var b;
        if(imageType == "svg")
            b = new Blob([graph._el.innerHTML], {type: "image/svg+xml;charset=utf-8"});
        else
            b = graph._el.firstChild.msToBlob();
        window.navigator.msSaveOrOpenBlob(b, fileName + "." + imageType);
        if(b.msClose)
            b.msClose();
    }
    else{
        link.setAttribute("href", graph.toImageURL(imageType));
        link.setAttribute("download", fileName + "." + imageType);
        link.click();
        link.setAttribute("href", "javascript:void(saveImage())");
        link.removeAttribute("download");
    }
    return false;
}
775

776
function onLoadSpec(has_data, has_rawdata){
777 778 779
    data_format = graph_spec.data[0].format;
    graph_spec.data[0].format = null;

780 781 782 783 784 785 786 787 788
    if(!has_data){
        //Handle files from the file input.
        document.getElementById("fileselect").addEventListener('change', function(){
            loadFile(document.getElementById("fileselect").files);
        }, false);
        var rows = document.querySelectorAll(".fileselectrow");
        for (var i = 0; i < rows.length; i++)
            rows[i].style.display = "table-row";
    }
789 790 791 792 793 794 795 796 797 798 799
    else{
        loadFitFunctions(fileName, graph_spec.data[0].values);
        if(graph_spec.data[0].fdstools_filename){
            var rows = document.querySelectorAll(".filenamedisplayrow");
            for (var i = 0; i < rows.length; i++)
                rows[i].style.display = "table-row";
            var cell = document.getElementById("filenamedisplay");
            removeChildren(cell);
            cell.appendChild(document.createTextNode(graph_spec.data[0].fdstools_filename));
            setFileName(graph_spec.data[0].fdstools_filename);
        }
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    }
    if(!has_rawdata){
        //Handle files from the file input.
        document.getElementById("fileselect2").addEventListener('change', function(){
            loadFile(document.getElementById("fileselect2").files);
        }, false);
        var rows = document.querySelectorAll(".fileselect2row");
        for (var i = 0; i < rows.length; i++)
            rows[i].style.display = "table-row";
    }
    if(!has_data || !has_rawdata){
        //Allow files to be dragged onto the page.
        document.addEventListener('dragover', function(evt){
            evt.stopPropagation();
            evt.preventDefault();
        }, false);
        document.addEventListener('drop', function(evt){
            evt.stopPropagation();
            evt.preventDefault();
            loadFile(evt.dataTransfer.files, true);
        }, false);
    }
    embeddedFiles = (has_data? 1:0) + (has_rawdata? 2:0);
823

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    //Update graph when rendering mode is changed.
    document.getElementById("renderCanvas").addEventListener('change', function(){
        setRenderer(this.value);
    }, false);
    document.getElementById("renderSVG").addEventListener('change', function(){
        setRenderer(this.value);
    }, false);
    document.getElementById("graphwidth").addEventListener('change', function(){
        var value = parseFloat(this.value);
        if(isNaN(value))
            return;
        setSignalValue("width", value);
    }, false);
    document.getElementById("graphheight").addEventListener('change', function(){
        var value = parseFloat(this.value);
        if(isNaN(value))
            return;
        setSignalValue("graphheight", value);
    }, false);
    document.getElementById("subgraphoffset").addEventListener('change', function(){
        var value = parseFloat(this.value);
        if(isNaN(value))
            return;
        setSignalValue("subgraphoffset", value);
    }, false);
    document.getElementById("unitSelect").addEventListener('change', function(){
        if(this.value == "CUSTOM")
            document.getElementById("unitFilterRow").setAttribute("class", "");
        else{
            document.getElementById("unitFilterRow").setAttribute("class", "hidden");
            document.getElementById("unitFilter").value = this.value;
            setSignalValue("filter_unit", this.value);
        }
    }, false);
    document.getElementById("unitFilter").addEventListener('change', function(){
        var value = this.value;
        try{
            compileFilter(value.trim());
        }
        catch(e){
            value = "=";
        }
        setSignalValue("filter_unit", value);
    }, false);
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
868 869 870
    document.getElementById("foldSelect").addEventListener('change', function(){
        setSignalValue("filter_fold", this.value);
    }, false);
871 872 873 874
    document.getElementById("markerFilter").addEventListener('change', function(){
        var value = this.value;
        try{
            compileFilter(value.trim());
875
        }
876 877 878 879 880
        catch(e){
            value = "=";
        }
        setSignalValue("filter_marker", value);
    }, false);
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
881 882 883 884 885 886
    document.getElementById("r2Filter").addEventListener('change', function(){
        var value = parseFloat(this.value);
        if(isNaN(value))
            return;
        setSignalValue("filter_r2", value);
    }, false);
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    document.getElementById("allDataFilter").addEventListener('change', function(){
        setSignalValue("show_all_data", this.checked);
    }, false);
    document.getElementById("jitter").addEventListener('mouseup', function(){
        var value = parseFloat(this.value)/100;
        if(isNaN(value) || value == getSignalValue("jitter"))
            return;
        setSignalValue("jitter", value);
    }, false);
    document.getElementById("jitter").addEventListener('keyup', function(){
        var value = parseFloat(this.value)/100;
        if(isNaN(value) || value == getSignalValue("jitter"))
            return;
        setSignalValue("jitter", value);
    }, false);

    //Sync graph_spec and display.
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
904 905 906 907 908 909 910
    var val = getSignalValue("filter_unit");
    document.getElementById("unitFilter").value = val;
    var sel = document.getElementById("unitSelect");
    sel.value = "CUSTOM";
    for(i = 0; i < sel.options.length; i++)
        if(val == sel.options.item(i).value)
            sel.value = val;
911
    document.getElementById("unitFilterRow").setAttribute("class", sel.value == "CUSTOM"? "" : "hidden");
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
912 913 914 915 916 917
    val = getSignalValue("filter_fold");
    sel = document.getElementById("foldSelect");
    sel.value = "";
    for(i = 0; i < sel.options.length; i++)
        if(val == sel.options.item(i).value)
            sel.value = val;
918
    document.getElementById("markerFilter").value = getSignalValue("filter_marker");
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
919
    document.getElementById("r2Filter").value = getSignalValue("filter_r2");
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    document.getElementById("allDataFilter").checked = getSignalValue("show_all_data");
    document.getElementById("graphwidth").value = getSignalValue("width");
    document.getElementById("graphheight").value = getSignalValue("graphheight");
    document.getElementById("subgraphoffset").value = getSignalValue("subgraphoffset");
    document.getElementById("jitter").value = getSignalValue("jitter")*100;
    if(has_data)
        parse();
}
</script>
<!-- BEGIN_LOAD_SCRIPT -->
<script type="text/javascript">
var graph_spec = false;
vg.util.load({url: "stuttermodelvis.json"}, function(err, result){
    graph_spec = JSON.parse(result);
    onLoadSpec(false, false);
});
</script>
<!-- END_LOAD_SCRIPT -->
938 939
</body>
</html>