{% extends "layout_bs4.html" %}
{% from "variants/components.html" import gene_cell, frequency_cell %}
{% from "variants/utils.html" import cancer_filters, tier_cell, cell_rank %}


{% block title %}
  {{ variant_type|capitalize }} cancer variants
{% endblock %}

{% block css %}
{{ super() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css">
{% endblock %}

{% block top_nav %}
  {{ super() }}
  <li class="nav-item">
    <a class="nav-link" href="{{ url_for('cases.index') }}">Institutes</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="{{ url_for('cases.cases', institute_id=institute._id) }}">
      {{ institute.display_name }}
    </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="{{ url_for('cases.case', institute_id=institute._id, case_name=case.display_name) }}">
      {{ case.display_name }}
    </a>
  </li>
  <li class="nav-item active">
    <span class="navbar-text">{{ form.variant_type.data|capitalize }} Cancer Variants</span>
  </li>
{% endblock %}

{% block content_main %}
<div class="container-float">
  <form method="POST" id="filters_form">
    <div class="card panel-default" id="accordion">
      <div class="card-header">
        <strong><a data-toggle="collapse" data-parent="#accordion" href="#collapseFilters">Filters</a></strong>
      </div>
      <div class="card-body panel-collapse collapse" id="collapseFilters">
          {{ cancer_filters(form, institute, case) }}
      </div>
    </div>

  <div class="card mt-3">
    <table class="table table-hover table-bordered">
      <thead>
            <tr>
              <th style="width:4%" title="Rank position">Rank</th>
              <th>Gene:Transcript:Exon:HGVS</th>
              <th>Tier</th>
              <th>Score</th>
              <th>Gene</th>
              <th>Chr pos</th>
              <th>ExAC</th>
              <th>Type</th>
              <th>Consequence</th>
              <th>Tumor</th>
              <th>Normal</th>
            </tr>
      </thead>
      <tbody>
        {% for variant in variants %}
          {% if variant.dismiss_variant %}
            <tr class="dismiss">
          {% else %}
            <tr>
          {% endif %}
              <td class="text-left">{{cell_rank(variant, institute, case, form, manual_rank_options)}}</td>
            <td>
              <a href="{{ url_for('variant.variant', institute_id=institute._id, case_name=case.display_name,
                                  variant_id=variant._id, cancer='yes') }}">
              {% for annotation in variant.canonical_transcripts %}
                <div>{{ annotation }}</div>
              {% endfor %}
              </a>
            </td>
            <td>{{ tier_cell(variant, manual_rank_options, cancer_tier_options) }}</td>
            <td>{{ score_cell(variant) }}</td>
            <td>{{ gene_cell(variant) }}</td>
            <td>{{ position_cell(variant) }}</td>
            <td class="text-right">{{ frequency_cell(variant) }}</td>
            <td>{{ variant.sub_category }}</td>
            <td>
              {% for annotation in variant.functional_annotations %}
                <div>{{ annotation }}</div>
              {% endfor %}
            </td>
            <td>{{ allele_cell(variant.tumor or {}) }}</td>
            <td>{{ allele_cell(variant.normal or {}) }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div><!-- end of card-->
  <br>
  <div class="container-fluid">
      {{ pagination() }}
  </div>
</div><!--end of container-float -->
{% endblock %}

{% macro pagination() %}
  <div class="container-fluid">
    {% if more_variants %}
    <div class="d-flex justify-content-around">
        <div>
            <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value=1>First page</button>
        </div>
        <div>
          <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value={{ page + 1 }}>Next page</button>
        </div>
    </div>
    {% else %}
      <i class="text-muted">No more variants to display</i>
      <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value=1>First page</button>
    {% endif %}
  </div>
</form>
{% endmacro %}

{% macro actions_cell(variant) %}
  <a href="{{ url_for('variant.variant', institute_id=institute._id, case_name=case.display_name,
                      variant_id=variant._id, cancer='yes') }}">View</a>
  {% if variant.comments %}
    <span class="glyphicon glyphicon-comment"></span>
  {% endif %}
{% endmacro %}


{% macro score_cell(variant) %}
  {% if variant.rank_score %}
    {% if variant.rank_score <= 4 %}
      {% set label_class = 'default' %}
    {% elif variant.rank_score == 5 %}
      {% set label_class = 'info' %}
    {% elif variant.rank_score == 6 %}
      {% set label_class = 'primary' %}
    {% elif variant.rank_score == 7 %}
      {% set label_class = 'warning' %}
    {% elif variant.rank_score > 8 %}
      {% set label_class = 'danger' %}
    {% endif %}
  {% endif %}
  <div class="badge badge-{{ label_class }}">{{ variant.rank_score }} </div>
{% endmacro %}


{% macro position_cell(variant) %}
  {{ variant.chromosome }}<span class="text-muted">:{{ variant.position }}</span>
{% endmacro %}

{% macro allele_cell(allele) %}
  {% if 'alt_freq' in allele %}
    {{ (allele.alt_freq * 100)|round(2)  }}%
    <br>
    <small class="text-muted">{{ allele.alt_depth }} | {{ allele.ref_depth }}</small>
  {% else %}
    <span class="text-muted">N/A</span>
  {% endif %}
{% endmacro %}

{% block scripts %}
  {{ super() }}
  <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-table-headers/0.1.19/js/jquery.stickytableheaders.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/1.0.11/purify.min.js"></script>
  <script>
    $(function () {
      $('[data-toggle="tooltip"]').tooltip();
      $('[data-toggle="popover"]').popover({
        sanitizeFn: function (content) {
          return DOMPurify.sanitize(content)
        },
        container: 'body',
      });
    })
  </script>
{% endblock %}
