<!DOCTYPE html>
<html>
    <head>
        <title>MicroHapulator Report</title>
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" media="screen">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
        <script src="fancyTable.js"></script>
        <style type="text/css">
            .container {
                max-width: 750px;
                padding-top: 100px;
            }
            .alnrt {
                text-align: right;
            }
            img {
                margin: 10px auto;
                width: 48%;
            }
            img.small {
                width: 24.5%;
            }
            .fullwidth {
                width: 100%;
            }
            table {
                margin: 12pt auto;
                width: 100%;
            }
            .scrollwrapper {
                overflow-x: auto;
            }
            table.half {
                margin: 12pt auto 12pt 0;
                width: 50%;
            }
            td, th {
                padding-right: 10px;
                white-space: nowrap;
            }

            blockquote p {
                font-size: small;
            }

            blockquote h3 {
                margin-top: 0;
            }

            ::-webkit-scrollbar {
                width: 5px;
                height: 5px;
            }

            ::-webkit-scrollbar-thumb {
                background: gray;
                border-radius: 10px;
            }
            ::-webkit-scrollbar-track{
                -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
                border-radius: 10px;
                background-color: #F5F5F5;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>MicroHapulator Report</h1>
            <p>
                Report generated at {{date}},<br />
                using <a href="https://microhapulator.readthedocs.io" target="_blank">MicroHapulator</a> version {{mhpl8rversion}}.
            </p>

            <h2>Table of Contents</h2>
            <ol>
                <li><a href="#readqc">Read QA/QC</a></li>
                {% if "r1readlen" in plots %}
                <li><a href="#readmerging">Read Merging</a></li>
                {% endif %}
                <li><a href="#readmapping">Read Mapping</a></li>
                <li><a href="#typing">Haplotype Calling</a></li>
                <li><a href="#filters">Genotype Calling</a></li>
            </ol>
            <p>All statistics presented in this report are aggregated in a single table available at <code>analysis/summary.tsv</code> in the working directory. Full-resolution graphics for each figure are also available in each <code>analysis/{samplename}/</code> subdirectory.</p>

            <a name="readqc"></a>
            <h2>Read QA/QC</h2>
            <p>QC reports for the input reads are generated using <a href="https://www.bioinformatics.babraham.ac.uk/projects/fastqc/" target="_blank">FastQC</a>. Links to reports for each sample are provided below.</p>
            <blockquote><strong>NOTE</strong>: FastQC was designed for QC of whole-genome shotgun NGS reads prior to genome asssembly. A QC warning or failure for some modules (such as per-base sequence content or sequence duplication levels) may or may not be a concern with MH reads. Interpret results with care!</blockquote>

            {% if "r1readlen" in plots %}
            <table>
                <tr><th>Sample</th><th>R1 Report</th><th>R2 Report</th></tr>
                {% for sample in samples %}
                <tr>
                    <td>{{sample}}</td>
                    <td><a href="analysis/{{sample}}/fastqc/R1-fastqc.html" target="_blank">Click here to open in a new tab</a></td>
                    <td><a href="analysis/{{sample}}/fastqc/R2-fastqc.html" target="_blank">Click here to open in a new tab</a></td>
                </tr>
                {% endfor %}
            </table>
            {% else %}
            <table>
                <tr><th>Sample</th><th>Report</th></tr>
                {% for sample in samples %}
                <tr>
                    <td>{{sample}}</td>
                    <td><a href="analysis/{{sample}}/fastqc/report.html" target="_blank">Click here to open in a new tab</a></td>
                </tr>
                {% endfor %}
            </table>
            {% endif %}

            {% if read_length_table is none %}
                {% if "r1readlen" in plots %}
                    <p>The following histograms show the distribution of R1 and R2 read lengths for each sample.</p>
                    {% for r1plot, r2plot in zip(plots["r1readlen"], plots["r2readlen"]) %}
                    <img src={{r1plot}} class="small" />
                    <img src={{r2plot}} class="small" />
                    {% endfor %}
                {% else %}
                    <p>The following histograms show read length distributions for each sample.</p>
                    {% for rlplot in plots["readlen"] %}
                    <img src="{{rlplot}}" class="small" />
                    {% endfor %}
                {% endif %}
            {% else %}
                <p>Read lengths, uniform for all samples, are shown below.</p>
                {% if "r1readlen" in plots %}
                <table class="half">
                    <tr>
                        <th>Sample</th>
                        <th class="alnrt">Length R1</th>
                        <th class="alnrt">Length R2</th>
                    </tr>
                    {% for i, row in read_length_table.iterrows() %}
                    <tr>
                        <td>{{ row.Sample }}</td>
                        <td class="alnrt">{{ row.LengthR1 }}</td>
                        <td class="alnrt">{{ row.LengthR2 }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% else %}
                <table class="half">
                    <tr>
                        <th>Sample</th>
                        <th class="alnrt">Length</th>
                    </tr>
                    {% for i, row in read_length_table.iterrows() %}
                    <tr>
                        <td>{{ row.Sample }}</td>
                        <td class="alnrt">{{ row.Length }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endif %}
            {% endif %}

            {% if "r1readlen" in plots %}
            <a name="readmerging"></a>
            <h2>Read Merging</h2>
            <p>Paired end reads are merged using <a href="https://ccb.jhu.edu/software/FLASH/" target="_blank">FLASh</a>.</p>
            <table>
                <tr>
                    <th>Sample</th>
                    <th class="alnrt">Total Reads</th>
                    <th class="alnrt">Merged Reads</th>
                    <th class="alnrt">Merge Rate</th>
                </tr>
                {% for i, row in summary.iterrows() %}
                <tr>
                    <td>{{row.Sample}}</td>
                    <td class="alnrt">{{ "{:,}".format(row.TotalReads) }}</td>
                    <td class="alnrt">{{ "{:,}".format(row.Merged) }}</td>
                    <td class="alnrt">{{ "{:.2f}".format(row.MergeRate * 100) }}%</td>
                </tr>
                {% endfor %}
            </table>
            <p>The following histograms show the distribution of merged read lengths for each sample.</p>
            {% for plot in plots["mergedreadlen"] %}
            <img src="{{plot}}" />
            {% endfor %}
            {% endif %}

            <a name="readmapping"></a>
            <h2>Read Mapping</h2>
            <p>Merged reads are aligned to marker reference sequences using <a href="http://bio-bwa.sourceforge.net/bwa.shtml" target="_blank">BWA MEM</a> and formatted/sorted using <a href="http://www.htslib.org/" target="_blank">SAMtools</a>. The reads were also aligned to the full (entire chromosomes) human reference genome, to aid in discriminating between off-target sequences and e.g. contaminant sequences: reads that align to the entire chromosomes but not to the marker sequences represent off-target sequences, while reads that do not align to either are likely contaminants. The reported chi-square statistic is a measure of read coverage imbalance between markers, and can be compared among samples sequenced using the sample panel: the minimum value of 0 represents perfectly uniform coverage across markers, while the maximum value of D occurs when all reads map to a single marker (D represents the degrees of freedom, or the number of markers minus 1).</p>
            <div class="scrollwrapper">
            <table>
                <tr>
                    <th>Sample</th>
                    {% if "r1readlen" in plots %}
                    <th class="alnrt">Merged Reads</th>
                    {% else %}
                    <th class="alnrt">Total Reads</th>
                    {% endif %}
                    <th class="alnrt">Mapped</th>
                    <th class="alnrt">Mapping Rate</th>
                    <th class="alnrt">Mapped (Chrom)</th>
                    <th class="alnrt">Mapping Rate (Chrom)</th>
                    <th class="alnrt">Chi-square</th>
                </tr>
                {% for i, row in summary.iterrows() %}
                <tr>
                    <td>{{row.Sample}}</td>
                    {% if "r1readlen" in plots %}
                    <td class="alnrt">{{ "{:,}".format(row.Merged) }}</td>
                    {% else %}
                    <td class="alnrt">{{ "{:,}".format(row.TotalReads) }}</td>
                    {% endif %}
                    <td class="alnrt">{{ "{:,}".format(row.Mapped) }}</td>
                    <td class="alnrt">{{ "{:.2f}".format(row.MappingRate * 100) }}%</td>
                    <td class="alnrt">{{ "{:,}".format(row.MappedFullRefr) }}</td>
                    <td class="alnrt">{{ "{:.2f}".format(row.MappingRateFullRefr * 100) }}%</td>
                    <td class="alnrt">{{ "{:.2f}".format(row.InterlocChiSq)}}</td>
                </tr>
                {% endfor %}
            </table>
            </div>
            <br />
            <p>The following table shows the number of total reads mapped to each marker for each sample. Also shown is the number of off-target reads, determined by selecting the reads aligned to the marker, mapping them to the entire GRCh38 human reference sequence, and counting the reads that map preferentially to a locus outside the target marker locus.</p>
            <blockquote>
                <h3>Interpretation Hints</h3>
                <p>The "Mapping Rate (Chrom)" column in the table above indicates the proportion of the reads in the sample that map <em>anywhere</em> to the human genome. The percentage <em>not</em> mapped represents non-human material (typically contamination) in the sample.</p>
                <p>The difference between the "Mapping Rate" column and the "Mapping Rate (Chrom)" column indicates the proportion of reads in the sample that are off-target <em>with respect to the marker panel</em>: their origin is elsewhere in the genome, and their presence is an artifact of an imperfect enrichment strategy.</p>
                <p>In the table below, the "Off Target" columns refer to reads that can and do align to a marker reference sequence, but preferentially map elsewhere when aligned to the entire genome. These reads indicate repetitive content in the corresponding marker, which therefore requires extra scrutiny to ensure that reads from alternate loci are not jeopardizing the haplotype calling process. <strong>N/A values in the "Off Target" column indicate that the marker definition file did not include GRCh38 coordinates for all SNPs, which are required for off-target analysis.</strong></p>
            </blockquote>
            <div class="scrollwrapper">
                <table id="mapping_rates_table" class="pagination_table">
                    <thead>
                        <tr>
                            <td></td>
                            {% for sample in typing_rates.keys() %}
                            <td style="visibility: collapse;">{{sample}}</td>
                            <td style="font-weight:bold">{{sample}}</td>
                            {% endfor %}
                        </tr>
                        <tr>
                            <th>Marker</th>
                            {% for i in range(len(mapping_rates)) %}
                            <th class="alnrt" style="font-weight:bold" data-sortas="numeric">Reads</th>
                            <th class="alnrt" style="font-weight:bold" data-sortas="numeric">Off Target</th>
                            {% endfor %}
                            <td></td>
                        </tr>
                    </thead>
                    <tbody>
                        {% for markername in markernames %}
                        <tr>
                            <td><a href="marker-detail-report.html?marker={{markername}}" target="_blank">{{markername}}</a></td>
                            {% for sample_data in mapping_rates.values()%}
                            <td class="alnrt" data-sortvalue={{sample_data.loc[markername, 'ReadCount']}}>{{"{:,d}".format(sample_data.loc[markername, 'ReadCount'])}}</td>
                            {% if isna(sample_data.loc[markername, 'OffTargetReads']) %}
                            <td class="alnrt" data-sortvalue="0">N/A</td>
                            {% else %}
                            <td class="alnrt" data-sortvalue="{{sample_data.loc[markername, 'OffTargetReads']}}">{{"{:,d}".format(sample_data.loc[markername, 'OffTargetReads'])}}</td>
                            {% endif %}
                            {% endfor %}
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <br />
            <p>The following histograms show the interlocus balance for each sample.</p>
            {% for plot in plots["locbalance"] %}
            <img src="{{plot}}" />
            {% endfor %}

            <a name="typing"></a>
            <h2>Haplotype Calling</h2>
            <p>Haplotypes are called empirically on a per-read basis using <code>mhpl8r type</code>. Reads that span all SNPs of interest in the corresponding marker are examined; all other reads are discarded. The haplotype tallies represent a <em>typing result</em> for each sample.</p>
            <table>
                <tr>
                    <th>Sample</th>
                    <th class="alnrt">Mapped Reads</th>
                    <th class="alnrt">Typed Reads</th>
                    <th class="alnrt">Typing Success Rate</th>
                </tr>
                {% for i, row in summary.iterrows() %}
                <tr>
                    <td>{{row.Sample}}</td>
                    <td class="alnrt">{{ "{:,}".format(row.Mapped) }}</td>
                    <td class="alnrt">{{ "{:,}".format(row.Typed) }}</td>
                    <td class="alnrt">{{ "{:.2f}".format(row.TypingRate * 100) }}%</td>
                </tr>
                {% endfor %}
            </table>
            <br />
            <p>The table above shows the aggregate typing rate across all markers. The table below shows the typing rate per marker.</p>
            <div class="scrollwrapper">
                <table id="typing_rates_table" class="pagination_table">
                    <thead>
                        <tr>
                            <th>Marker</th>
                            {% for sample in typing_rates.keys() %}
                            <th class=alnrt style="font-weight:bold">{{sample}}</th>
                            {% endfor %}
                        </tr>
                    </thead>
                    <tbody>
                        {% for markername in markernames %}
                        <tr>
                            <td><a href="marker-detail-report.html?marker={{markername}}" target="_blank">{{markername}}</a></td>
                            {% for sample_data in typing_rates.values()%}
                            <td class="alnrt" data-sortvalue="{{sample_data.loc[markername, 'TypingRate']}}">{{"{:,.2f}".format(sample_data.loc[markername, 'TypingRate']*100)}}%</td>
                            {% endfor %}
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <a name="filters"></a>
            <h2>Genotype Calling</h2>
            <p>
                Two types of thresholds are applied to each typing result using <code>mhpl8r filter</code> to discriminate between true MH alleles (haplotypes) and false alleles resulting from sequencing error or other artifacts.
                A static detection threshold, based on a fixed number of reads, is used to filter out low-level noise.
                A dynamic analytical threshold, based on a percentage of the total reads at the locus (after removing alleles that fail the detection threshold), accounts for fluctuations in the depth of coverage between loci, samples, and runs, and can filter out higher-level noise in most cases.
                The haplotype tallies, after all filters have been applied, represents the <em>genotype call</em> for that sample.
            </p>
            <p>
                Thresholds were were applied to each marker as shown in the table below.
                Click on marker names in the table below to see plots of haplotype talles and thresholds in the marker detail report.
            </p>
            <table class="half pagination_table">
                <thead>
                    <tr>
                        <th>Marker</th>
                        <th class="alnrt">Detection</th>
                        <th class="alnrt">Analytical</th>
                    </tr>
                </thead>
                <tbody>
                    {% for marker, row in thresholds.iterrows() %}
                    <tr>
                        <td><a href="marker-detail-report.html?marker={{marker}}" target="_blank">{{marker}}</a></td>
                        <td class="alnrt" data-sortvalue="{{row.Static}}">{{row.Static}}</td>
                        <td class="alnrt" data-sortvalue="{{row.Dynamic}}">{{"{:.1f}".format(row.Dynamic * 100)}}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>

            <hr />

            <p>
                For single-source samples, we expect the two alleles at heterozygous loci to have roughly even abundance.
                The following plots show the <em>relative</em> abundance of the major and minor allele for each marker with a heterozygous genotype (markers are sorted by absolute combined abundance, which is printed above each pair of allele counts).
                For known DNA mixtures, these plots can be safely ignored.
                But for suspected single-source samples, if there is substantial imbalance between major and minor allele counts at numerous loci, the sample should be examined more closely for the presence of a minor DNA contributor.
            </p>
            {% for plot in plots["hetbalance"] %}
            <img src="{{plot}}" class="fullwidth" />
            {% endfor %}
        </div>
    </body>
<script type="text/javascript">
    $(".pagination_table").fancyTable({
        sortColumn: 0,
        pagination: true,
        perPage: 15,
        globalSearch: true,
        sortable: true,
        globalSearchExcludeColumns: [2, 3, 4, 5, 6, 7, 8, 9],
    });
</script>

</html>
