rule msaviewer__visualise_alignment:
    """
    Visualise multiple alignment between aligned sequences
    :input alignment: Aligned sequences in ALN format
    :output html: Interactive visualization of the alignment
    """
    input:
        alignment = 'reference/{reference}/alignment/{reference}.aln'
    output:
        html      = 'reference/{reference}/alignment/{reference}.aln.html'
    run:
        TEMPLATE = '''
        <html>
        <head>
            <title>{sample}</title>
            <script src=https://s3.eu-central-1.amazonaws.com/cdn.bio.sh/msa/latest/msa.min.gz.js></script>
        </head>
        <body>
            <h1>{sample}</h1>
            <div id="alignment">press "Run with JS"</div>
            <script type="text/javascript">
                var fasta = "{seqs}";
                var seqs = msa.io.fasta.parse(fasta);
                var opts = {{
                    el: document.getElementById("alignment"),
                    vis: {{
                        conserv: false,
                        metaIdentity: true,
                        overviewbox: true,
                        seqlogo: true
                    }},
                    seqs: seqs,
                    colorscheme: {{"scheme": "nucleotide"}},
                    // smaller menu for JSBin
                    menu: "small",
                    bootstrapMenu: true
                }};
                var m = new msa.msa(opts);
                m.render()
            </script>
        </body>
        '''
        seq_list = map(str.strip, open(input.alignment))
        seqs = '\\n\\\n'.join(seq_list)
        html = TEMPLATE.format(sample=wildcards.reference, seqs=seqs)
        with open(output.html, 'w') as out:
            out.write(html)
