<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cohort Quality Control Report</title>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --accent-color: #e74c3c;
            --background-color: #f8f9fa;
            --text-color: #2c3e50;
            --border-color: #dee2e6;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--background-color);
            margin: 0;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }

        h1, h2, h3, h4 {
            color: var(--primary-color);
            margin-top: 1.5em;
            margin-bottom: 0.5em;
        }

        h1 {
            font-size: 2.5em;
            border-bottom: 3px solid var(--secondary-color);
            padding-bottom: 0.3em;
        }

        h2 {
            font-size: 2em;
            color: var(--secondary-color);
            border-bottom: 2px solid var(--border-color);
            padding-bottom: 0.2em;
        }

        h3 {
            font-size: 1.5em;
            color: var(--primary-color);
        }

        h4 {
            font-size: 1.2em;
            color: var(--accent-color);
        }

        .section {
            margin-bottom: 2em;
            padding: 1.5em;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }

        details {
            margin-bottom: 1em;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            overflow: hidden;
        }

        details summary {
            padding: 1em;
            background-color: var(--primary-color);
            color: white;
            cursor: pointer;
            user-select: none;
            font-weight: bold;
            font-size: 1.2em;
        }

        details summary:hover {
            background-color: var(--secondary-color);
        }

        details[open] summary {
            border-bottom: 1px solid var(--border-color);
        }

        details > .section {
            margin: 0;
            border-radius: 0;
            box-shadow: none;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 1em 0;
            background-color: white;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border: 1px solid var(--border-color);
        }

        th {
            background-color: var(--primary-color);
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        tr:hover {
            background-color: #f1f3f5;
        }

        ul {
            padding-left: 1.5em;
        }

        li {
            margin-bottom: 0.5em;
        }

        strong {
            color: var(--secondary-color);
        }

        .plot-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin: 1em 0;
        }

        .plot {
            flex: 1;
            min-width: 300px;
            max-width: 100%;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .plot img {
            width: 100%;
            height: auto;
            display: block;
        }

        .interpretation-box {
            background-color: #f8f9fa;
            border-left: 4px solid var(--secondary-color);
            padding: 1em;
            margin: 1em 0;
        }

        .metric-value {
            font-size: 1.2em;
            color: var(--accent-color);
            font-weight: bold;
        }

        .confidence-level {
            padding: 1em;
            margin: 1em 0;
            border-radius: 8px;
            background-color: #f8f9fa;
        }

        .confidence-level.very-high {
            border-left: 4px solid #27ae60;
        }

        .confidence-level.high {
            border-left: 4px solid #3498db;
        }

        .confidence-level.moderate {
            border-left: 4px solid #f1c40f;
        }

        .confidence-level.low {
            border-left: 4px solid #e74c3c;
        }

        .plot-grid {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            justify-content: center;
        }

        .plot-item {
            flex: 1 1 45%;
            max-width: 45%;
            box-sizing: border-box;
        }

        .plot-item img {
            width: 100%;
            height: auto;
            border: 1px solid #ccc;
            border-radius: 6px;
            box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
        }

        .association-matrix td {
            padding: 15px;
            vertical-align: top;
        }

        .association-value {
            display: block;
            margin: 5px 0;
            font-family: monospace;
        }

        .association-value strong {
            color: var(--primary-color);
            font-weight: 600;
        }

        .significant-pvalue {
            color: var(--accent-color);
            font-weight: bold;
        }

        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }

            .plot-container {
                flex-direction: column;
            }

            .plot {
                min-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Cohort Quality Control Report</h1>
        
        <details open>
            <summary>Cohort Summary</summary>
            <div class="section">
                <ul>
                    {% for key, value in summary.items() %}
                        {% if key != "Samples by Covariate Combination" %}
                            <li><strong>{{ key }}:</strong> {{ value }}</li>
                        {% endif %}
                    {% endfor %}
                </ul>

                {% if samples_by_comb %}
                    <h3>Samples by Covariate Combination</h3>
                    <table>
                        <thead>
                            <tr>
                                {% for cov in covariates %}
                                    <th>{{ cov }}</th>
                                {% endfor %}
                                <th>Number of Samples</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for row in samples_by_comb %}
                                <tr>
                                    {% for cov in covariates %}
                                        <td>{{ row[cov] }}</td>
                                    {% endfor %}
                                    <td>{{ row["Number of Samples"] }}</td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
            </div>
        </details>

        <details open>
            <summary>PCA Analysis and Variance Explained</summary>
            <div class="section">
                <p>
                    Principal Component Analysis (PCA) helps to identify the main sources of variation in the data.
                    We perform PCA before and after batch effect correction to see if the batch effects dominate the first few principal components (PCs).
                    The variance explained by each PC indicates how much of the data's variation is captured by that PC.
                    After correction, the variance should ideally be more evenly distributed across PCs.
                </p>

                <h3>PCA Plots (PC1 vs PC2) Before and After Batch Correction</h3>
                {% for plot in pca_plots %}
                    <div class="plot-container">
                        {% if plot.before %}
                        <div class="plot">
                            <h4>PCA Plot Before Correction - Colored by {{ plot.covariate }}</h4>
                            <img src="data:image/png;base64,{{ plot.before }}" alt="PCA before - {{ plot.covariate }}">
                        </div>
                    {% else %}
                        <div class="plot">
                            <h4>Data before correction not available for {{ plot.covariate }}</h4>
                        </div>
                    {% endif %}
                        <div class="plot">
                            <h4>PCA Plot After Correction - Colored by {{ plot.covariate }}</h4>
                            <img src="data:image/png;base64,{{ plot.after }}" alt="PCA after - {{ plot.covariate }}">
                        </div>
                    </div>
                {% endfor %}

                <h3>PC Variance Explained Before and After Batch Correction</h3>
                <p>
                    Variance explained by each principal component before and after batch effect correction.
                    Lower variance in the first few PCs suggests successful batch effect correction.
                </p>
                <div class="plot-container">
                    {% if pca_variance_before %}
                        <div class="plot">
                            <h4>Variance explained by PCs before correction</h4>
                            <img src="data:image/png;base64,{{ pca_variance_before }}" alt="PCA variance before">
                        </div>
                    {% endif %}
                    <div class="plot">
                        <h4>Variance explained by PCs after correction</h4>
                        <img src="data:image/png;base64,{{ pca_variance_after }}" alt="PCA variance after">
                    </div>
                </div>

                <h3>Association between PCs and Clinical Annotations</h3>
                <div class="interpretation-box">
                    <h4>Interpretation</h4>
                    <p>
                        - High correlation between PCs and batch information before correction indicates batch effects. Lower correlation after correction suggests successful batch effect removal.
                    </p>
                    <p>
                        - However, it is important to note that even after batch correction, a strong association between datasets and PCs might still persist, especially in cases where:
                    </p>
                    <ul>
                        <li>
                            <strong>Diverse cohort:</strong> When dealing with a highly diverse cohort, the first few principal components might capture this diversity, leading to a natural association between the PCs and the datasets. This diversity could be in terms of biological variation, differences in sample types, or varying conditions across the datasets.
                        </li>
                        <li>
                            <strong>Specific sample types per dataset:</strong> e.g., tumor vs normal samples. If datasets are composed of samples that are biologically distinct, such as normal versus tumor samples, the PCs might reflect these inherent biological differences. In such cases, the association between datasets and PCs is not due to batch effects, but rather due to the underlying biological differences that the PCA is capturing.
                        </li>
                    </ul>
                    <p>
                        In these scenarios, the observed associations are expected and reflect meaningful biological or experimental differences rather than technical artifacts.
                    </p>
                </div>
                {% if association_matrix_before %}
                <h4>Before Batch Effect:</h4>
                <p>Results format: statistics, p-value, test performed, and number of samples used.</p>
                {{ association_matrix_before | safe }}
            {% endif %}
            
            <h4>After Batch Effect:</h4>
            {{ association_matrix_after | safe }}
            
            </div>
        </details>

        <details open>
            <summary>Correction Effect Metric</summary>
            <div class="section">
                <p>
                    This metric quantifies the impact of the batch effect correction on the variability of gene expression data. The metric is calculated as follows:
                </p>

                <ul>
                    <li><strong>Step 1:</strong> Calculate the Median Absolute Deviation (MAD) for each gene before correction. This involves computing the median expression level for each gene across all samples, then calculating the absolute deviations from this median, and finally taking the median of these deviations.
                        {% if mad_before %}
                        <ul>
                            <li>Median absolute deviation before correction =</li>
                            <li class="metric-value">{{ mad_before|format_float(4) }}</li>
                        </ul>
                        {% else %}
                        <ul><li>Data before correction not available</li></ul>
                        {% endif %}
                    </li>

                    <li><strong>Step 2:</strong> Repeat the same process to calculate the MAD for each gene after the batch effect correction.
                        <ul>
                            <li>Median absolute deviation after correction =</li>
                            <li class="metric-value">{{ mad_after|format_float(4) }}</li>
                        </ul>
                    </li>

                    <li><strong>Step 3:</strong> Compute the correction effect metric as the ratio of the mean MAD after correction to the mean MAD before correction.
                        {% if mad_before %}
                        <ul>
                            <li>The correction effect metric calculated is:</li>
                            <li class="metric-value">{{ effect_metric|format_float(4) }}</li>
                        </ul>
                        {% else %}
                        <ul><li>Data before correction not available</li></ul>
                        {% endif %}
                    </li>
                </ul>

                <div class="interpretation-box">
                    <h4>Interpretation:</h4>
                    <p>This ratio helps quantify how much variability remains after correction.</p>
                    <ul>
                        <li>If close to 1: the correction may not have reduced batch effects much.</li>
                        <li>A ratio significantly less than 1: indicates that the correction has reduced the batch effect (lower variability).
                            A metric around 0.5 might indicate that the batch correction was strong. While this reduction in variability could be due to effective correction, it also raises concerns about potentially removing biological variability.
                        </li>
                        <li>A metric much higher than 1 could indicate that the correction process introduced additional variability. This could be due to overcorrection, which might have removed important biological signals along with the batch effects.</li>
                        <li>The correction effect metric can depend on the number of datasets or batches. With more datasets, batch effects might be stronger, necessitating more aggressive correction, potentially leading to a lower metric. Fewer datasets might result in a correction effect closer to 1, as the variability due to batch effects could be less significant.</li>
                    </ul>
                </div>

                <h3>Sample Distribution by Covariate Combination</h3>
                <p>
                    The following boxplots compare gene expression across datasets (batches) before and after correction for different covariate combinations.
                </p>

                <ul>
                    <li><strong>Before Correction:</strong> Look for variability across batches, which may indicate batch effects.</li>
                    <li><strong>After Correction:</strong> Ideally, the distributions should become more consistent across batches, suggesting effective correction.</li>
                </ul>

                <p>
                    These plots help assess whether the correction process has successfully reduced batch-related variability without masking important biological differences.
                </p>

                <div class="plot-grid">
                    {% for plot in distribution_plots %}
                        <div class="plot-item">
                            <h4>Distribution for covariate combination: {{ plot.combination }}</h4>
                            <img src="data:image/png;base64,{{ plot.img }}" alt="Distribution plot">
                        </div>
                    {% endfor %}
                </div>
                
                <div class="plot">
                    <h4>Total Sample Distribution</h4>
                    <img src="data:image/png;base64,{{ global_distribution_plot }}" alt="Global distribution">
                </div>
            </div>
        </details>

        <details open>
            <summary>Silhouette Score</summary>
            <div class="section">
                <p>
                    The Silhouette Score measures how similar each sample is to its own batch compared to other batches.
                    A high score before correction indicates strong batch effects. A lower score after correction means these effects were reduced.
                </p>

                <ul>
                    <li>
                        {% if silhouette_before is not none %}
                            Silhouette Score Before Correction: <span class="metric-value">{{ silhouette_before|format_float(4) }}</span>
                        {% else %}
                            Data before correction not available.
                        {% endif %}
                    </li>
                    <li>
                        Silhouette Score After Correction: <span class="metric-value">{{ silhouette_after|format_float(4) }}</span>
                    </li>
                </ul>

                <div class="interpretation-box">
                    <h4>Interpretation:</h4>
                    <p>
                        - A decrease in the Silhouette Score after correction suggests successful batch effect mitigation.
                    </p>
                </div>
            </div>
        </details>

        <details open>
            <summary>Entropy of Batch Mixing (EBM)</summary>
            <div class="section">
                <p>
                    The Entropy of Batch Mixing (EBM) measures how well samples from different batches are mixed after correction.
                    Higher entropy indicates better mixing, meaning batch effects have been reduced.
                </p>

                <ul>
                    <li>
                        {% if entropy_before is not none %}
                            Entropy Before Correction: <span class="metric-value">{{ entropy_before|format_float(4) }}</span>
                        {% else %}
                            Data before correction not available.
                        {% endif %}
                    </li>
                    <li>
                        Entropy After Correction: <span class="metric-value">{{ entropy_after|format_float(4) }}</span>
                    </li>
                </ul>

                <div class="interpretation-box">
                    <h4>Interpretation:</h4>
                    <p>
                        - An increase in entropy after correction indicates improved mixing of batches, suggesting successful batch effect correction.
                    </p>
                </div>
            </div>
        </details>

        <details close>
            <summary>Mixed Dataset Summary Report</summary>
            <div class="section">
                <h3>Confidence in Batch Effect Correction regarding Mixed Datasets and Samples</h3>
                <ul>
                    <li>Total Mixed Datasets: {{ summary_mixed["total_mixed_datasets"] }}</li>
                    <li>Total Non-Mixed Datasets: {{ summary_mixed["total_non_mixed_datasets"] }}</li>
                    <li>Total Mixed Samples: {{ summary_mixed["total_mixed_samples"] }}</li>
                    <li>Total Non-Mixed Samples: {{ summary_mixed["total_non_mixed_samples"] }}</li>
                </ul>

                <h4>Proportion of mixed datasets in the cohort: {{ mixed_dataset_ratio|percentage }}</h4>
                <h4>Proportion of mixed samples in the cohort: {{ mixed_sample_ratio|percentage }}</h4>

                <h3>Mixed Samples by Covariate</h3>
                <h4>Mixed Samples</h4>
                <ul>
                    {% for cov, count in summary_mixed["mixed_samples_by_covariate"].items() %}
                        <li>{{ cov }}: {{ count }} samples</li>
                    {% endfor %}
                </ul>

                <h4>Non-Mixed Samples</h4>
                <ul>
                    {% for cov, count in summary_mixed["non_mixed_samples_by_covariate"].items() %}
                        <li>{{ cov }}: {{ count }} samples</li>
                    {% endfor %}
                </ul>

                <h3>Overall Proportion of Mixed Samples by Covariate Combination</h3>
                <table>
                    <tr>
                        {% for cov in covariate_names %}
                            <th>{{ cov }}</th>
                        {% endfor %}
                        <th>Proportion of Mixed Samples</th>
                    </tr>
                
                    {% for row in mixed_proportions_table %}
                        <tr>
                            {% for cov in covariate_names %}
                                <td>{{ row["cov" ~ loop.index] }}</td>
                            {% endfor %}
                            <td>{{ row.proportion | percentage }}</td>
                        </tr>
                    {% endfor %}
                </table>
                

                <div class="interpretation-box">
                    <h4>Interpretation</h4>
                    <div class="confidence-level very-high">
                        <strong>Very High Confidence: >50% mixed datasets and >50% mixed samples</strong>
                        <p>Confidence is very high in the batch effect correction due to a substantial proportion of mixed datasets and samples. This suggests that the correction algorithm was applied across a highly diverse set of conditions, minimizing the risk that batch effects confound the biological signals. The variability across different conditions was well-represented, leading to more reliable results.</p>
                    </div>
                    <div class="confidence-level high">
                        <strong>High Confidence: 30-50% mixed datasets or 30-50% mixed samples</strong>
                        <p>Confidence is high in the batch effect correction due to a substantial proportion of mixed datasets and samples. This indicates that the correction algorithm was applied across a diverse range of conditions, reducing the likelihood that batch effects are confounded with biological signals. A higher representation of mixed datasets means that the variability across different conditions was well-represented during the correction, leading to more reliable and robust results.</p>
                    </div>
                    <div class="confidence-level moderate">
                        <strong>Moderate Confidence: 15-30% mixed datasets or 15-30% mixed samples</strong>
                        <p>Confidence is moderate in the batch effect correction. There is a reasonable proportion of mixed datasets and samples, suggesting that the correction was performed on a fairly diverse dataset. However, there's still a possibility that some batch effects might not have been fully corrected if certain covariate combinations were underrepresented. While the results are likely to be reliable, some caution is advised in interpreting the findings.</p>
                    </div>
                    <div class="confidence-level low">
                        <strong>Low Confidence: <15% mixed datasets and <15% mixed samples</strong>
                        <p>Confidence is low in the batch effect correction. The mixed datasets and samples form a small proportion of the cohort, which indicates that the correction may have been applied under limited conditions. This can lead to insufficient representation of the variability across different conditions, increasing the risk that batch effects may still confound the biological signals. In such cases, the reliability of the corrected data could be compromised, and further validation might be necessary.</p>
                    </div>
                </div>

                <h2>Detailed Summary for Each Mixed Dataset</h2>
                {% for dataset, details in summary_mixed["mixed_dataset_details"].items() %}
                    <div class="section">
                        <h3>Dataset: {{ dataset }}</h3>
                        <ul>
                            <li>Total Samples: {{ details["total_samples"] }}</li>
                            <li>Samples by Covariate Combination:
                                <ul>
                                    {% for comb, count in details["samples_by_covariate_combination"].items() %}
                                        <li>{{ comb }}: {{ count }} samples ({{ details["proportion_by_covariate_combination"][comb]|percentage }})</li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </div>
                {% endfor %}
            </div>
        </details>
    </div>
</body>
</html>
