{% extends "layout.html" %}
{% from "utils.html" import comments_panel, activity_panel, pedigree_panel %}
{% from "variants/utils.html" import compounds_table %}

{% block title %}
  {{ super() }} - {{ institute.display_name }} - {{ case.display_name }} - {{ variant.display_name }}
{% endblock %}

{% block top_nav %}
  <li>
    <a href="{{ url_for('cases.cases', institute_id=institute._id) }}">
      {{ institute.display_name }}
    </a>
  </li>
  <li>
    <a href="{{ url_for('cases.case', institute_id=institute._id, case_name=case.display_name) }}">
      {{ case.display_name }}
    </a>
  </li>
  <li>
    {% if cancer %}
      <a href="{{ url_for('variants.cancer_variants', institute_id=institute._id, case_name=case.display_name, variant_type=variant.variant_type) }}">
        {{ variant.variant_type|capitalize }} cancer variants
      </a>
    {% else %}
      <a href="{{ url_for('variants.variants', institute_id=institute._id, case_name=case.display_name, variant_type=variant.variant_type, gene_panels=case.panels|selectattr('is_default')|map(attribute='panel_name')|list) }}">
        {{ variant.variant_type|capitalize }} SNV and INDELs
      </a>
    {% endif %}
  </li>
  <li class="active">
    <p class="navbar-text">{{ variant.display_name|truncate(20, True) }}</p>
  </li>
{% endblock %}

{% block top_nav_right %}
  {% if config['MAIL_USERNAME'] %}
    {# Email setting must be setup #}
    <li>{{ sanger_button() }}</li>
  {% endif %}
  <li>{{ pin_button() }}</li>
  <li>{{ causative_button() }}</li>
  {{ super() }}
{% endblock %}

{% block content_main %}
  {% if variant.missing_data %}
    <div class="alert alert-warning">Showing only first 30 genes!</div>
  {% endif %}
  <div class="row">
    <div class="col-xs-12">{{ matching_variants() }}</div>
    <div class="col-xs-12 col-sm-6 col-md-4">{{ panel_basics() }}</div>
    <div class="col-xs-12 col-sm-8 col-md-8">
      <div class="col-xs-12 col-sm-12 col-md-12">{{ panel_summary() }}</div>
      <div class="col-xs-12 col-md-12">{{ transcript_overview() }}</div>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-sm-6">{{ inheritance_panel() }}</div>
    <div class="col-xs-12 col-sm-6">{{ omim_phenotypes() }}</div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-sm-6">{{ frequency_list() }}</div>
    <div class="col-xs-12 col-sm-6">{{ severity_list() }}</div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-sm-6 {{ 'col-md-4' if has_pedigree }}">
      {{ comments_panel(institute, case, current_user, variant.comments, variant_id=variant._id) }}
    </div>
    <div class="col-xs-12 col-sm-6 {{ 'col-md-5' if has_pedigree }}">{{ gtcall_panel() }}</div>
    {% set has_pedigree = case.madeline_info and case.individuals|length > 1 %}
    {% if has_pedigree %}
      <div class="col-xs-12 col-md-3">{{ pedigree_panel(case) }}</div>
    {% endif %}
  </div>
  {% if variant.compounds %}
    <div class="row">
      <div class="col-xs-12">{{ compounds_panel() }}</div>
    </div>
  {% endif %}
  <div class="row">
    <div class="col-xs-12">{{ rankscore_panel() }}</div>
  </div>
  <div class="row">
    <div class="col-xs-12">{{ overlapping_panel() }}</div>
  </div>
  <div class="row">
    <div class="col-xs-12">
      <ul class="nav nav-tabs nav-justified">
        <li><a href="#genes" data-toggle="tab">Genes</a></li>
        <li class="active"><a href="#transcripts" data-toggle="tab">Transcripts</a></li>
        <li><a href="#proteins" data-toggle="tab">Proteins</a></li>
      </ul>
      <div class="tab-content">
        <div class="tab-pane fade" id="genes">{{ genes_panel() }}</div>
        <div class="tab-pane fade in active" id="transcripts">{{ transcripts_panel() }}</div>
        <div class="tab-pane fade" id="proteins">{{ proteins_panel() }}</div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12">{{ external_links() }}</div>
  </div>
  <div class="row">
    <div class="col-xs-12">{{ activity_panel(events|reverse) }}</div>
  </div>

  {% if config['MAIL_USERNAME'] %}
    {# Email setting must be setup #}
    {{ modal_sanger() }}
  {% endif %}

  {% if config['MAIL_USERNAME'] %}
    {# Email setting must be setup #}
    {{ modal_cancel_sanger() }}
  {% endif %}

{% endblock %}

{% macro sanger_button() %}
  {% if variant.sanger_ordered %}
    {% if variant.validation in ['True positive', 'False positive']%}
      <p class="navbar-text">Sanger: {{ variant.validation }}</p>
    {% else %}
      <button class="btn btn-default navbar-btn" data-toggle="modal" data-target="#cancelSangerModal">
        Sanger pending...
      </button>
    {% endif %}
  {% else %}
    <button class="btn btn-default navbar-btn" data-toggle="modal" data-target="#sangerModal">
      Sanger
    </button>
  {% endif %}
{% endmacro %}

{% macro sanger_form() %}
  <ul class="list-group">
    <li class="list-group-item">
      <strong>Case {{ case.display_name }}</strong><br>
      <a href="{{ url_for('variants.variant', institute_id=institute._id,
                          case_name=case.display_name, variant_id=variant._id) }}">
        {{ variant.display_name }}
      </a>
    </li>
    <li class="list-group-item">
      <strong>HGNC symbols</strong>:
      {{ variant.hgnc_symbols|join(', ') }}
    </li>
    <li class="list-group-item">
      <strong>Gene panels</strong>:
      {{ variant.panels|join(', ') }}
    </li>
    <li class="list-group-item disabled"><strong>Amino acid changes</strong></li>
    {% for gene in variant.genes %}
      {% for transcript in gene.transcripts %}
        {% if transcript.refseq_id %}
          <li class="list-group-item">{{ transcript.change_str }}</li>
        {% endif %}
      {% endfor %}
    {% endfor %}
    <li class="list-group-item disabled"><strong>GT call</strong></li>
    {% for individual in variant.samples %}
      <li class="list-group-item">
        {{ individual.display_name }}: {{ individual.genotype_call }}
      </li>
    {% endfor %}
    <li class="list-group-item">
      <strong>Ordered by</strong>:
      {{ current_user.name }}
    </li>
  </ul>
{% endmacro %}

{% macro pin_button() %}
  {% if variant._id not in case.suspects %}
    <form action="{{ url_for('cases.pin_variant',
                             institute_id=institute._id,
                             case_name=case.display_name,
                             variant_id=variant._id) }}"
          method="POST">
      <button name="action" value="ADD" type="submit" class="btn btn-default navbar-btn" title="Pin variant">Pin</button>
    </form>
  {% else %}
    <form action="{{ url_for('cases.pin_variant',
                             institute_id=institute._id,
                             case_name=case.display_name,
                             variant_id=variant._id) }}"
          method="POST">
      <button name="action" value="DELETE" type="submit" class="btn btn-default navbar-btn" title="Unpin variant">Unpin</button>
    </form>
  {% endif %}
{% endmacro %}

{% macro causative_button() %}
  {% if variant._id in case.causatives %}
      <form action="{{ url_for('cases.mark_causative',
                               institute_id=institute._id,
                               case_name=case.display_name,
                               variant_id=variant._id) }}"
            method="POST">
        <button name="action" value="DELETE" type="submit" class="btn btn-default navbar-btn" title="Reset causative">
          Reset causative
        </button>
      </form>
  {% else %}
    <form action="{{ url_for('cases.mark_causative',
                             institute_id=institute._id,
                             case_name=case.display_name,
                             variant_id=variant._id) }}"
          method="POST">
      <button name="action" value="ADD" type="submit" class="btn btn-default navbar-btn" title="Mark causative" onclick="return confirm('Are you sure?')">
        Mark causative
      </button>
    </form>
  {% endif %}
{% endmacro %}

{% macro matching_variants() %}
  <div class="panel panel-default">
    <div class="panel-heading">Matching causatives from other cases</div>
    <ul class="list-group">
      {% for other_variant in causatives %}
        <li class="list-group-item">
          <a href="{{ url_for('variants.variant',
                              institute_id=institute._id,
                              case_name=other_variant.case_display_name,
                              variant_id=other_variant._id) }}">
            {{ other_variant.case_display_name }}
          </a>
        </li>
      {% else %}
        <li class="list-group-item">No matching causative variants.</li>
      {% endfor %}
    </ul>
  </div>
{% endmacro %}

{% macro panel_basics() %}
  <div class="panel panel-default">
    <div class="panel-heading">Basics</div>
    <table class="table table-bordered table-fixed">
      <tbody>
        <tr>
          <td>
            Position
            <span class="pull-right">
              <strong>{{ variant.chromosome }}:<span class="text-muted">{{ variant.position }}</span></strong>
              {% if variant.is_par %}
                <span class="label label-info">PAR</span>
              {% endif %}
            </span>
          </td>
          <td>
            Change
            <span class="pull-right"><strong>{{ variant.reference }} → {{ variant.alternative }}</strong></span>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="table-responsive">
      <table class="table table-bordered">
        <thead class="border-top">
          <tr class="active">
            <th>Gene</th>
            <th>Region</th>
            <th>Function</th>
          </tr>
        </thead>
        <tbody>
          {% for gene in variant.genes %}
            <tr>
              <th>
                <a href="{{ url_for('genes.gene', hgnc_id=gene.hgnc_id) }}">
                  {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
                </a>
              </th>
              <td>{{ gene.region_annotation }}</td>
              <td>{{ gene.functional_annotation|truncate(20, True) }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    <ul class="list-group">
      <li class="list-group-item">
        <strong>Gene panels</strong>
        <ul class="list-inline pull-right">
          {% for panel_id in variant.panels %}
            <li>
                <a href="{{ url_for('panels.panel', panel_id=panel_id) }}">{{ panel_id }}</a>
            </li>
          {% endfor %}
        </ul>
      </li>
    </ul>
    <div class="panel-body">
      <form action="{{ url_for('variants.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
        <label>Variant tag</label>
        <div class="row">
          <div class="col-xs-8">
            <select name="manual_rank" class="form-control">
              <option value="-1">Select a tag...</option>
              {% for rank, data in manual_rank_options.items() %}
                <option {% if rank == variant.manual_rank %} selected {% endif %} value="{{ rank }}">
                  {{ data.label }}
                </option>
              {% endfor %}
            </select>
          </div>
          <div class="col-xs-4">
            <button type="submit" class="btn btn-default form-control">Save</button>
          </div>
        </div>
      </form>
    </div>
    <div class="panel-body">
      <form action="{{ url_for('variants.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
        <label>Dismiss variant</label>
        <div class="row">
          <div class="col-xs-8">
            <select multiple name="dismiss_variant" class="form-control">
              {% for rank, data in dismiss_variant_options.items() %}
                <option {% if rank~"" in variant.dismiss_variant %} selected {% endif %} value="{{ rank }}">
                  {{ data.label }}
                </option>
              {% endfor %}
            </select>
          </div>
          <div class="col-xs-4">
            <button type="submit" class="btn btn-default form-control">Save</button>
          </div>
        </div>
      </form>
    </div>
    <div class="panel-body">
      {{ acmg_form(variant.acmg_classification.code if variant.acmg_classification) }}
    </div>
    <div class="panel-body">
      <a href="{{ url_for('variants.variant_acmg', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" class="btn btn-default form-control">Classify</a>
      {% if variant._id in case.suspects and not variant.clinvar_clinsig %}
        <a href="{{ url_for('variants.clinvar', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" class="btn btn-default form-control">Submit to ClinVar (BETA)</a>
      {% endif %}
      {% if variant.clinvar_clinsig%}
        <a href="{{ url_for('cases.clinvar_submissions', institute_id=institute._id) }}" class="btn btn-default form-control">Modify clinvar submission</a>
      {% endif %}
    </div>
    {% if evaluations %}
      <ul class="list-group">
        {% for evaluation in evaluations %}
          {{ acmg_classification_item(evaluation) }}
        {% endfor %}
      </ul>
    {% endif %}
  </div>
{% endmacro %}

{% macro panel_summary() %}
  <div class="panel panel-default">
    <div class="panel-heading">Summary</div>
    <table class="table table-bordered table-fixed">
      <tbody>
        <tr>
          <td>
            Rank
            <span class="pull-right"><strong>{{ variant.variant_rank }}</strong></span>
          </td>
          <td>
            Rank score
            <span class="pull-right"><strong>{{ variant.rank_score }}</strong></span>
          </td>
          <td>
            CADD score
            <span class="pull-right"><strong>{{ variant.cadd_score or '-' }}</strong></span>
          </td>
        </tr>
      </tbody>
    </table>
    <table class="table table-bordered table-fixed">
      <tbody class="border-top">
        <tr>
          <td>
            Matches OMIM inhert.
            {% if variant.is_matching_inheritance %}
              <span class="label label-success pull-right">Yes</span>
            {% else %}
              <div class="label label-warning pull-right">No</div>
            {% endif %}
          </td>
          <td>
            Frequency
            <div class="label label-{% if variant.frequency == 'common' %}danger{% elif variant.frequency == 'uncommon' %}warning{% else %}success{% endif %} pull-right">
              {{ variant.frequency }}
            </div>
          </td>
        </tr>
      </tbody>
    </table>
    {% if variant.clinsig_human %}
      <table class="table table-bordered table-fixed">
        <thead>
          <thead>
            <tr class="active">
              <th>CLINSIG</th>
              <th>Accession</th>
              <th>Revstat</th>
            </tr>
          </thead>
        </thead>
        <tbody>
          {% for clinsig in variant.clinsig_human %}
            <tr>
              <td>{{ clinsig.human }}</td>
              <td>
                <a href="{{ clinsig.link }}" target="_blank">{{ clinsig.accession }}</a>
              </td>
              <td>{{ clinsig.revstat }}</td>
            </tr>
          {% else %}
            <i>No annotations</i>
          {% endfor %}
        </tbody>
      </table>
    {% endif %}
    <ul class="list-group">
      {% if variant.dbsnp_id %}
        <li class="list-group-item">
          SNPedia
          <span class="pull-right">
            <a target="_blank" href="http://snpedia.com/index.php/{{ variant.dbsnp_id }}">
              {{ variant.dbsnp_id }}
            </a>
          </span>
        </li>
      {% endif %}
    </ul>
    <div class="panel-heading">Gene coverage</div>
    <table class="table table-bordered table-fixed">
      <tbody>
        <tr>
          <td>
            {% if variant.chromosome == "MT" %}
              {% if case.mt_bams %}
                <a class="btn btn-default btn-sm" href="{{ url_for('alignviewers.pileup', bam=case.mt_bams, bai=case.mt_bais, vcf=case.region_vcf_file, sample=case.sample_names, contig=variant.chromosome, start=(variant.position - 50), stop=(variant.end_position + 50)) }}" target="_blank">Pileup viewer</a><br>
                <a class="btn btn-default btn-sm" href="{{ url_for('alignviewers.igv', sample=case.sample_names, build=case.genome_build, bam=case.mt_bams, bai=case.mt_bais, contig=variant.chromosome, start=(variant.position - 50), stop=(variant.end_position + 50), center_guide="T") }}" target="_blank">IGV viewer (BETA)</a>
              {% else %}
                <span class="text-muted">MT BAM file(s) missing</span>
              {% endif %}
            {% elif case.bam_files %}
              <a class="btn btn-default btn-sm" href="{{ url_for('alignviewers.pileup', bam=case.bam_files, bai=case.bai_files, vcf=case.region_vcf_file, sample=case.sample_names, contig=variant.chromosome, start=(variant.position - 50), stop=(variant.end_position + 50)) }}" target="_blank">Pileup viewer</a><br>
              <a class="btn btn-default btn-sm" href="{{ url_for('alignviewers.igv', sample=case.sample_names, build=case.genome_build, bam=case.bam_files, bai=case.bai_files, contig=variant.chromosome, start=(variant.position - 50), stop=(variant.end_position + 50), center_guide="T") }}" target="_blank">IGV viewer (BETA)</a>
            {% else %}
              <span class="text-muted">BAM file(s) missing</span>
            {% endif %}
          </td>
          {% if config.SQLALCHEMY_DATABASE_URI %}
            {% for gene in variant.genes %}
              <td>
                <a target="_blank" href="{{ url_for('report.gene', gene_id=gene.hgnc_id, sample_id=variant.samples|map(attribute='sample_id')|list) }}">
                  {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
                </a>
              </td>
            {% endfor %}
          {% endif %}
        </tr>
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro transcript_overview() %}
  <div class="panel panel-default fixed-panel">
    <div class="panel-heading">Disease associated transcripts</div>
    <div class="panel-body">
        {{ variant.disease_associated_transcripts|join(', ')
          if variant.disease_associated_transcripts else '-' }}
    </div>
    <div class="panel-heading">Transcript overview</div>
    <table class="table table-bordered table-hover">
      <thead>
        <tr>
          <th>Gene</th>
          <th>Transcript</th>
          <th>HGVS Description</th>
        </tr>
      </thead>
      <tbody>
        {% for gene in variant.genes %}
          {% for transcript in gene.primary_transcripts %}
            {% for refseq_id in transcript.refseq_identifiers %}
              <tr {% if transcript.is_primary %} class="bg-info" {% endif %}>
                <td>
                  <a href="{{ url_for('genes.gene', hgnc_id=gene.hgnc_id) }}">
                    {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
                  </a>
                  {% if transcript.is_canonical %}
                    <span class="badge pull-right" title="canonical">C</span>
                  {% endif %}
                </td>
                <td>
                  <div>
                    {{ refseq_id }}<span class="text-muted"> ({{ transcript.transcript_id }})</span>
                  </div>
                </td>
                <td title="{{ transcript.coding_sequence_name or '' }}">
                  {{ (transcript.coding_sequence_name or '')|truncate(20, True) }}
                  <span class="text-muted pull-right">
                    {{ (transcript.protein_sequence_name or '')|url_decode }}
                  </span>
                </td>
              </tr>
            {% endfor %}
          {% endfor %}
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro frequency_list() %}
  <div class="panel panel-default">
    <div class="panel-heading">Frequency</div>
    <ul class="list-group">
      <li class="list-group-item">
        {% if variant.dbsnp_id %}
          <a href="{{ variant.thousandg_link }}" target="_blank">1000G</a>
        {% else %}
          1000G
        {% endif %}
        <span class="pull-right">
          {% if variant.max_thousand_genomes_frequency %}
            {{ variant.max_thousand_genomes_frequency|human_decimal }} <small>(max)</small> |
          {% endif %}
          {{ variant.thousand_genomes_frequency|human_decimal if variant.thousand_genomes_frequency }}
        </span>
      </li>
      <li class="list-group-item">
        <a title="Exome Aggregation Consortium" target="_blank" href="{{ variant.exac_link }}">ExAC</a>
        <span class="pull-right">
          {% if variant.max_exac_frequency %}
            {{ variant.max_exac_frequency|human_decimal }} <small>(max)</small> |
          {% endif %}
          {{ variant.exac_frequency|human_decimal if variant.exac_frequency }}
        </span>
      </li>
      <li class="list-group-item">
        <a title="genome Aggregation Database" target="_blank" href="{{ variant.gnomad_link }}">gnomAD</a>
        <span class="pull-right">
          {% if 'gnomad_frequency' in variant%}
            {% if variant.max_gnomad_frequency %}
              {{ variant.max_gnomad_frequency|human_decimal }}
              <small>(max)</small> |
            {% endif %}
            {{ variant.gnomad_frequency|human_decimal if variant.gnomad_frequency }}
          {% else %}
            <span class="text-muted">Not annotated</span>
          {% endif %}
        </span>
      </li>
      <li class="list-group-item">
        <div class="row">
          <div class="col-xs-3"><strong>Links</strong></div>
          <div class="col-xs-3">
            <a href="{{ variant.swegen_link }}" target="_blank">SweGen</a>
          </div>
          <div class="col-xs-3">
            <a href="{{ variant.beacon_link }}" target="_blank">Beacon</a>
          </div>
          <div class="col-xs-3">
            {% if variant.cosmic_link %}
              <a href="{{ variant.cosmic_link }}" target="_blank">COSMIC</a>
            {% else %}
              COSMIC
            {% endif %}
          </div>
        </div>
      </li>
    </ul>
    {% if config.LOQUSDB_SETTINGS %}
      <div class="panel-heading">
        <a href="https://github.com/moonso/loqusdb" target="_blank">Local observations</a>
      </div>
      <ul class="list-group">
        <li class="list-group-item">
          <div class="row">
            <div class="col-xs-4">
              {{ observations.observations|default('N/A') }} <small>obs.</small>
            </div>
            <div class="col-xs-4">
              {{ observations.homozygote|default('N/A') }} <small>homo.</small>
            </div>
            <div class="col-xs-4">
              {{ observations.total }} <small>tot.</small>
            </div>
          </div>
        </li>
        {% if observations['cases'] %}
          <li class="list-group-item">
            <div class="row">
              <div class="col-xs-3"><strong>Cases</strong></div>
              <div class="col-xs-9">
                {% for data in observations['cases'] %}
                  {% if data.variant %}
                    <a href="{{ url_for('variants.variant', institute_id=data.case.owner, case_name=data.case.display_name, variant_id=data.variant._id) }}">{{ data.case.display_name }}</a>
                  {% else %}
                    <span class="text-muted">{{ data.case.display_name }}</span>
                  {% endif %}
                {% endfor %}
              </div>
            </div>
          </li>
        {% endif %}
      </ul>
      <div class="panel-heading">Local observations (archive 2017-05-31)</div>
      <ul class="list-group">
        <li class="list-group-item">
          <div class="row">
            <div class="col-xs-4">
              {{ variant.local_obs_old or 'N/A' }} <small>obs.</small>
            </div>
            <div class="col-xs-4">
              {{ variant.local_obs_hom_old or 'N/A' }} <small>homo.</small>
            </div>
            <div class="col-xs-4">
              {{ variant.local_obs_total_old or 638 }} <small>tot.</small>
            </div>
          </div>
        </li>
      </ul>
    {% endif %}
  </div>
{% endmacro %}

{% macro severity_list() %}
  <div class="panel panel-default">
    <div class="panel-heading">Severity</div>
    <ul class="list-group">
      <li class="list-group-item">
        Sift
        <span class="pull-right">{{ variant.sift_predictions|join(', ') }}</span>
      </li>
      <li class="list-group-item">
        <a href="http://genetics.bwh.harvard.edu/pph2/" target="_blank">Polyphen</a>
        <span class="pull-right">{{ variant.polyphen_predictions|join(', ') }}</span>
      </li>
      <li class="list-group-item">
        SPIDEX
        <span class="pull-right">{{ variant.spidex_human }}</span>
      </li>
    </ul>
    <div class="panel-heading">Conservation</div>
    <ul class="list-group">
      <li class="list-group-item">
        PHAST
        <span class="pull-right">{{ variant.phast_conservation|join(', ') or '-' }}</span>
      </li>
      <li class="list-group-item">
        GERP
        <span class="pull-right">{{ variant.gerp_conservation|join(', ') or '-' }}</span>
      </li>
      <li class="list-group-item">
        phyloP
        <span class="pull-right">{{ variant.phylop_conservation|join(', ') or '-' }}</span>
      </li>
    </ul>
  </div>
{% endmacro %}

{% macro inheritance_panel() %}
  <div class="panel panel-default">
    <div class="panel-heading">Gene annotations</div>
    <ul class="list-group">
      <li class="list-group-item">
        Variant inheritance models
        <span class="pull-right">
          {% for model in variant.genetic_models|sort %}
            <span class="label label-info">{{ model }}</span>
          {% else %}
            <span class="label label-warning">No models followed</span>
          {% endfor %}
        </span>
      </li>
    </ul>

    {% if variant.azlength %}
    <div class="panel-heading">Autozygous region</div>
    <table class="table table-bordered table-fixed">
      <tbody>
        <tr>
          <td>
            Length
            <span class="pull-right">{{ variant.azlength }}</span>
          </td>
          <td>
            Qual
            <span class="pull-right">{{ variant.azqual }}</span>
          </td>
        </tr>
      </tbody>
    </table>
    {% endif %}

    <div class="panel-heading">Gene models</div>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th rowspan="2">Gene</th>
          <th colspan="2">Inheritance</th>
          <th colspan="2">Penetrance</th>
        </tr>
        <tr>
          <th>OMIM</th>
          <th>Manual</th>
          <th>OMIM</th>
          <th>Manual</th>
        </tr>
      </thead>
      <tbody>
        {% for gene in variant.genes %}
          <tr>
            <td>{{ gene.hgnc_symbol }}</td>
            <td>
              {% for model in gene.omim_inheritance %}
                <span class="label label-info">{{ model }}</span>
              {% endfor %}
            </td>
            <td>
              {% for model in gene.manual_inheritance %}
                <span class="label label-info">{{ model }}</span>
              {% endfor %}
            </td>
            <td>
              {% if gene.omim_penetrance %}
                <span class="label label-success">yes</span>
              {% else %}
                <span class="label label-default">no</span>
              {% endif %}
            </td>
            <td>
              {% if gene.manual_penetrance %}
                <span class="label label-success">yes</span>
              {% else %}
                <span class="label label-default">no</span>
              {% endif %}
            </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro omim_phenotypes() %}
  <div class="panel panel-default">
    <div class="panel-heading">OMIM</div>
    <div class="table-responsive fixed-panel">
      <table class="table table-bordered table-condensed">
        <thead>
          <tr>
            <th>Gene</th>
            <th>Phenotype</th>
            <th>Inheritance model</th>
          </tr>
        </thead>
        <tbody>
          {% for gene in variant.genes %}
            {% for disease_term in gene.disease_terms %}
              <tr>
                <td>
                  {% if gene.common %}
                    <a href="http://omim.org/entry/{{ gene.common.omim_id }}" target="_blank">
                      {{ gene.common.hgnc_symbol }}
                    </a>
                  {% else %}
                    {{ gene.hgnc_id }}
                  {% endif %}
                </td>
                <td>
                  <a href="http://omim.org/entry/{{ disease_term.disease_id|replace('OMIM:', '') }}" target="_blank">
                    {{ disease_term.description }}
                  </a>
                </td>
                <td>{{ disease_term.inheritance|join(', ') }}</td>
              </tr>
            {% else %}
              {% if gene.common %}
                <tr>
                  <td>
                    <a href="http://omim.org/entry/{{ gene.common.omim_id }}" target="_blank">
                      {{ gene.common.hgnc_symbol }}
                    </a>
                  </td>
                  <td></td>
                  <td></td>
                </tr>
              {% endif %}
            {% endfor %}
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
{% endmacro %}

{% macro gtcall_panel() %}
  <div class="panel panel-default">
    <div class="panel-heading">
      GT call
      {% if variant.is_par %}
        <span class="label label-info">PAR</span>
      {% endif %}
    </div>
    <div class="table-responsive">
      <table class="table table-bordered">
        <thead>
          <tr>
            <th rowspan="2">Sample</th>
              <th rowspan="2">Genotype (GT)</th>
              <th colspan="2" title="Unfiltered count of reads that support a given allele.">Allele depth (AD)</th>
              <th rowspan="2" title="Phred-scaled confidence that the true genotype is the one provided in GT (max=99).">Genotype quality (GQ)</th>
            </tr>
            <tr>
              <th>Reference</th>
              <th>Alternative</th>
            </tr>
          </tr>
        </thead>
        <tbody>
          {% for sample in variant.samples %}
            <tr {{ 'class="danger"' if sample.is_affected }}>
              <td>{{ sample.display_name }}</td>
              <td class="text-center">{{ sample.genotype_call }}</td>
              {% if sample.allele_depths %}
                  {% for number in sample.allele_depths %}
                    <td class="text-right">{{ number }}</td>
                  {% endfor %}
              {% else %}
                  <td class="text-right"><small>N/A</small></td>
                  <td class="text-right"><small>N/A</small></td>
              {% endif %}
              <td class="text-right">{{ sample.genotype_quality }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    <div class="panel-footer">
      {% for name, caller in variant.callers %}
        <span class="label label-default">{{ name }}: {{ caller }}</span>
      {% endfor %}
    </div>
  </div>
{% endmacro %}

{% macro compounds_panel() %}
  <div class="panel panel-default">
    <div class="panel-heading">Compounds (top 20)</div>
    {{ compounds_table(institute, case, variant.compounds[:20]) }}
  </div>
{% endmacro %}

{% macro rankscore_panel() %}
  <div class="panel panel-default">
    <div class="panel-heading">Rank score results</div>
    <div class="table-responsive">
      <table class="table table-bordered">
        <thead>
          <tr>
            {% for result in variant.rank_score_results %}
              <th><small>{{ result.category|replace('_', ' ') }}</small></th>
            {% endfor %}
          </tr>
        </thead>
        <tbody>
          <tr>
            {% for result in variant.rank_score_results %}
              <td class="text-right">
                {{ result.score }}
              </td>
            {% endfor %}
          <tr>
        </tbody>
      </table>
    </div>
  </div>
{% endmacro %}

{% macro overlapping_panel() %}
  <div class="panel panel-default">
    <div class="panel-heading">Overlapping SVs</div>
    <div class="table-responsive">
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>Variant</th>
            <th>Gene</th>
	    <th>Type</th>
            <th>Combined score</th>
            <th>Rank score</th>
            <th>Length</th>
            <th>Region</th>
            <th>Function</th>
          </tr>
        </thead>
        <tbody>
          {% for sv_variant in overlapping_svs %}
            <tr>
              <td>
                <a href="{{url_for('variants.sv_variant', institute_id=institute._id,
                                   case_name=case.display_name, variant_id=sv_variant._id)}}">
                  {{ sv_variant.display_name|truncate(20, True) }}
                </a>
              </td>
              <td>{{ sv_variant.hgnc_symbols|join(', ')|truncate(40, True) }}</td>
              <td>{{ sv_variant.sub_category|upper }}</td>
	      <td class="text-right">{{ variant.rank_score + sv_variant.rank_score }}</td>
              <td class="text-right">{{ sv_variant.rank_score }}</td>
              <td class="text-right">{{ sv_variant.length }}</td>

              <td>{{ sv_variant.region_annotations|join(', ')|truncate(40, True) }}</td>
              <td>{{ sv_variant.functional_annotations|join(', ')|truncate(40, True) }}</td>
            </tr>
          {% else %}
            <tr>
              <td colspan="7">No overlapping SVs found</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
{% endmacro %}

{% macro genes_panel() %}
  <div class="panel panel-default">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Gene</th>
          <th>Ensembl ID</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        {% for gene in variant.genes %}
          <tr>
            <td>
              <a href="{{ url_for('genes.gene', hgnc_id=gene.hgnc_id) }}">
                {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
              </a>
            </td>
            <td>
              {% if gene.common %}
                <a target="_blank" href="{{ gene.ensembl_link }}">
                  {{ gene.common.ensembl_id }}
                </a>
              {% endif %}
            </td>
            <td>{{ gene.common.description if gene.common }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro transcripts_panel() %}
  <div class="panel panel-default table-responsive fixed-panel">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Gene</th>
          <th>Transcript</th>
          <th>RefSeq</th>
          <th>Biotype</th>
          <th>Mutation type</th>
          <th>Strand</th>
          <th>Exon</th>
          <th>Intron</th>
          <th>cDNA</th>
          <th>Amino acid</th>
        </tr>
      </thead>
      <tbody>
        {% for gene in variant.genes %}
          {% for transcript in gene.transcripts %}
            <tr class="{{ 'danger' if transcript.is_disease_associated }}">
              <td>
                <a href="{{ url_for('genes.gene', hgnc_id=gene.hgnc_id) }}">
                  {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
                </a>
              </td>
              <td>
                <a target="_blank" href="{{ transcript.ensembl_link }}">
                  {{ transcript.transcript_id }}
                </a>
                {% if transcript.is_canonical %}
                  <span class="badge pull-right">C</span>
                {% endif %}
              </td>
              <td>
                {{ transcript.refseq_identifiers|join(',') }}
              </td>
              <td>{{ transcript.biotype or '' }}</td>
              <td data-toggle="tooltip" data-placement="right" title="{{ transcript.functional_annotations|join(', ') }}">
                {{ transcript.functional_annotations
                      |join(', ')
                      |truncate(20, True) }}
              </td>
              <td class="text-center">{{ transcript.strand }}</td>
              <td>{{ transcript.exon or '' }}</td>
              <td>{{ transcript.intron or '' }}</td>
              <td>{{ (transcript.coding_sequence_name or '') }}</td>
              <td>{{ (transcript.protein_sequence_name or '')|url_decode }}</td>
            </tr>
          {% endfor %}
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro proteins_panel() %}
  <div class="panel panel-default table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Gene</th>
          <th>Transcript</th>
          <th>Protein</th>
          <th>SWISS PROT</th>
          <th>Sift</th>
          <th>Polyphen</th>
          <th>Pfam</th>
          <th>ProSite</th>
          <th>Smart</th>
        </tr>
      </thead>
      <tbody>
        {% for gene in variant.genes %}
          {% for transcript in gene.transcripts %}
            <tr>
              <td>{{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}</td>
              <td>
                <a target="_blank" href="{{ transcript.ensembl_link }}">
                  {{ transcript.transcript_id }}
                </a>
              </td>
              <td>{{ transcript.protein_id if transcript.protein_id }}</td>
              <td>
                {% if transcript.swiss_prot != 'unknown' %}
                  <a target="_blank" href="{{ transcript.swiss_prot_link }}">
                    {{ transcript.swiss_prot }}
                  </a>
                {% else %}
                  {{ transcript.swiss_prot }}
                {% endif %}
              </td>
              <td>{{ transcript.sift_prediction }}</td>
              <td>{{ transcript.polyphen_prediction }}</td>
              <td>
                <a target="_blank" href="{{ transcript.pfam_domain_link }}">
                  {{ transcript.pfam_domain if transcript.pfam_domain }}
                </a>
              </td>
              <td>
                <a target="_blank" href="{{ transcript.prosite_profile_link }}">
                  {{ transcript.prosite_profile if transcript.prosite_profile }}
                </a>
              </td>
              <td>
                <a target="_blank" href="{{ transcript.smart_domain_link }}">
                  {{ transcript.smart_domain if transcript.smart_domain }}
                </a>
              </td>
            </tr>
          {% endfor %}
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% macro modal_sanger() %}
  <form action="{{ url_for('variants.verify', institute_id=institute._id,
                           case_name=case.display_name, variant_id=variant._id, variant_category=variant.category, order=True) }}"
        method="POST">
    <div class="modal fade" id="sangerModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Order Sanger sequencing</h4>
          </div>
          <div class="modal-body">
            {{ sanger_form() }}
          </div>
          <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Send sanger order request</button>
          </div>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro modal_cancel_sanger() %}
  <form action="{{ url_for('variants.verify', institute_id=institute._id,
                           case_name=case.display_name, variant_id=variant._id, variant_category=variant.category, order=False) }}"
        method="POST">
    <div class="modal fade" id="cancelSangerModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Cancel Sanger sequencing</h4>
          </div>
          <div class="modal-body">
            {{ sanger_form() }}
          </div>
          <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Send sanger cancellation request</button>
          </div>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro external_links() %}
  <div class="panel panel-default">
    {% for gene in variant.genes %}
      <div class="panel-heading">
        External links: {{ gene.common.hgnc_symbol if gene.common else gene.hgnc_id }}
      </div>
      <div class="panel-body">
        <div class="btn-group">
          <a href="{{ gene.ensembl_link }}" class="btn btn-default" target="_blank">Ensembl</a>
          <a href="{{ gene.hpa_link }}" class="btn btn-default" target="_blank">HPA</a>
          <a href="{{ gene.string_link }}" class="btn btn-default" target="_blank">STRING</a>
          <a href="{{ variant.ucsc_link }}" class="btn btn-default" target="_blank">UCSC</a>
          {% if gene.entrez_link %}
            <a href="{{ gene.entrez_link }}" class="btn btn-default" target="_blank">Entrez</a>
          {% endif %}
          <a href="http://tools.genes.toronto.edu/" class="btn btn-default" target="_blank">SPANR</a>
          <a href="{{ gene.reactome_link }}" class="btn btn-default" target="_blank">Reactome</a>
          <a href="{{ gene.expression_atlas_link }}" class="btn btn-default" target="_blank">Expr. Atlas</a>
          <a href="{{ variant.alamut_link }}" class="btn btn-default" target="_blank">Alamut</a>
          <a href="{{ gene.ppaint_link }}" class="btn btn-default" target="_blank">Protein Paint</a>
          {% if variant.chromosome == 'MT' %}
            <a href="https://www.mitomap.org/foswiki/bin/view////Main/SearchAllele" target="_blank">MitoMap</a>
          {% endif %}
        </div>
      </div>
      <div class="panel-body">
        <form action="http://marrvel.org/search" method="POST" target="_blank">
          <button type="submit" class="btn btn-default">MARRVEL</button>
          <input type="hidden" name="variantGeneId" value="{{ variant.chromosome }}:{{ variant.position }} {{ variant.reference }}>{{ variant.alternative }}">
          <input type="hidden" name="inGeneSymbol" value="{{ gene.common.hgnc_symbol if gene.common }}">
          <input type="hidden" name="OMIMdbCheck" value="on">
          <input type="hidden" name="ExACdbCheck" value="on">
          <input type="hidden" name="Geno2MPdbCheck" value="on">
          <input type="hidden" name="DGVdbCheck" value="on">
          <input type="hidden" name="DECIPHERdbCheck" value="on">
        </form>
      </div>
    {% endfor %}
  </div>
{% endmacro %}

{% macro acmg_form(selected=None) %}
  <label>ACMG classification</label>
  <form action="{{ url_for('variants.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
    <div class="d-flex justify-content-between">
      {% for option in ACMG_OPTIONS %}
        <button class="btn btn-{{ option.color if (option.code == selected or not selected) else 'default' }} form-control {{ 'mr-1' if not loop.last }}" name="acmg_classification" value="{{ option.code }}" title="{{ option.label }}">
          {{ option.short }}
        </button>
      {% endfor %}
    </div>
  </form>
{% endmacro %}

{% macro acmg_classification_item(data) %}
  {% set current_variant = (data.variant_specific == variant._id) %}
  <li class="list-group-item {{ 'list-group-item-info' if current_variant }}">
    <div class="d-flex justify-content-between">
      <span>
        <a href="{{ url_for('variants.evaluation', evaluation_id=data._id) }}">
          {{ data.classification.label }}
        </a>
        <span class="label label-info">{{ data.classification.short }}</span>
      </span>
      <span>
        {% if not current_variant %}
          <small>{{ data.case.display_name }}</small>
        {% endif %}
      </span>
    </div>
    <small class="text-muted">
      <form action="{{ url_for('variants.evaluation', evaluation_id=data._id) }}" method="POST">
      {{ data.user_name }} on {{ data.created_at.date() }}
      {% if current_variant %}
        <button class="btn btn-xs btn-link">Delete</button>
      {% endif %}
      </form>
    </small>
  </li>
{% endmacro %}

{% block scripts %}
  {{ super() }}

  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>
  <script>
    $(function () {
      $('[data-toggle="tooltip"]').tooltip({
        container: 'body'
      });

      $('select[multiple]').multiselect({
        buttonWidth: '100%'
      });
    })
  </script>
{% endblock %}
