{% from "cases/utils.html" import variant_transcripts %}

{% extends "report_base.html" %}

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

{% set affected = [] %}
{% set printed_vars = [] %}

{% block body %}
<nav class="navbar sticky-top navbar-expand-lg navbar-light bg-light">
  <!-- Brand and toggle get grouped for better mobile display -->
  <a class="nav-link" href="{{ url_for('cases.case', institute_id=institute._id, case_name=case.display_name) }}" target="_blank">Case {{case.display_name}}</a>

  <!-- Collect the nav links, forms, and other content for toggling -->
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarText">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="#phenotype">Phenotype overview<span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#gene_panels">Gene panels</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#causative_variants">Causative Variants</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#pinned_variants">Pinned variants</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#acmg_variants">ACMG-classified variants</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#manual_ranked_variants">Manual ranked</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#commented_variants">Commented variants</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#dismissed_variants">Dismissed variants</a>
      </li>
    </ul>
    <span class="navbar-text">
      <a href="{{ url_for('cases.pdf_case_report', institute_id=institute._id, case_name=case.display_name) }}"><button class="btn btn-sm btn-outline-secondary" type="button">Download PDF</button></a>
    </span>
  </div>
</nav>

<div id="container" class="container">
  <h4>Scout case analysis report</h4> - created on:&nbsp;<strong>{{report_created_at}}</strong><br><br>
   {{ phenotype_panel() }}
   {{ gene_panels_panel()}}
   {{ causatives_panel()}}
   {{ pinned_panel() }}
   {{ classified_panel() }}
   {{ tagged_panel() }}
   {{ commented_panel() }}
   {{ dismissed_panel() }}
   [END OF VARIANT REPORT]<br><br>
   {% if coverage_report %}
    {{ coverage_report|safe }}<br>
   {% endif %}
   <a href="https://clinical-genomics.github.io/scout" target="_blank">clinical-genomics.github.io/scout</a>
</div>
{% endblock %} <!-- end of block body -->

{% macro phenotype_panel() %}
	<div class="card mb-3" style="border-width: 5px;">
	  <div class="card-header">
	    <a name="phenotype" href="{{ url_for('cases.case', institute_id=institute._id, case_name=case.display_name) }}" target="_blank"><strong>Case {{ case.display_name }}</a> - Phenotype Overview</strong>
	  </div>
	  <div class="card-body">
	    <table class="table table-sm">
	      <tr>
          {% if case.madeline_info and case.individuals|length > 1 %}
	        <td>
	            {% if format == 'html' %}
	              {{ case.madeline_info|safe }}
	            {% else %}
	              <object data="{{url_for('cases.static', filename='madeline.svg')}}"/>
	            {% endif %}
          </td>
          {% endif %}
          <td>
	          <table class="table table-sm">
	            <tr>
	              <td>Case created: <strong>{{case.created_at.strftime('%Y-%m-%d')}}</strong></td>
	              <td>Last updated: <strong>{{case.updated_at.strftime('%Y-%m-%d')}}</strong></td>
	            </tr>
	          </table>
	          <br>
	          <table class="table table-sm">
	            <thead>
	              <tr>
	                <th>Sample</th>
	                <th>Sex</th>
                  <th>Status</th>
	                <th>Sequencing</th>
                  {% if not cancer %}
	                 <th>Ancestry (pred.)</th>
	                 <th>Parenthood</th>
                  {% endif %}
	              </tr>
	             </thead>
	            <tbody>
	              {% for ind in case.individuals %}
	                <tr {% if ind.phenotype == 2 %} class="table-danger" {% endif %}>
	                  <td>{{ ind.display_name }}</td>
	                  <td>
                      {% if ind.sex_human == 'female' %}
                        F
                      {% elif ind.sex_human == 'male' %}
                        M
                      {% else %}
                        n.a.
                      {% endif %}
	                    {% if ind.confirmed_sex %}
	                      <span class="badge badge-pill badge-sm badge-secondary">V</span>
	                    {% endif %}
	                  </td>
	                  <td>
	                  {% if ind.phenotype==2 %} <!--for later use-->
	                    {% do affected.append(ind.display_name) %}
                      <span class="badge badge-pill badge-danger" title="{{ ind.phenotype_human }}">{{ ind.phenotype_human }}</span>
                    {% else %}
                      <span class="badge badge-pill badge-secondary" title="{{ ind.phenotype_human }}">{{ ind.phenotype_human }}</span>
	                  {% endif %}
	                  </td>
	                  <td>{{ ind.analysis_type|upper }}</td>
                    {% if not cancer %}
	                   <td>{{ ind.predicted_ancestry or 'N/A' }}</td>
                     <td>
	                    {% if ind.confirmed_parent == True %}
	                      <span class="badge badge-pill badge-sm badge-secondary">V</span>
	                    {% elif ind.confirmed_parent == False %}
	                      <span class="badge badge-pill badge-sm badge-danger">!</span>
	                    {% else %}
	                      N/A
	                    {% endif %}
	                   </td>
                    {% endif %}
	                </tr>
	              {% endfor %}
	            </tbody>
	          </table>
	        </td>
	      </tr>
	      <tr>
	        <td>Case status: <strong>
	          {% if 'solved' in case.status%}
	            <font color="green">{{case.status.upper()}}</font>
	          {% else %}
	            {{case.status.upper()}}
	          {% endif %}
	          </strong><br><br>
	        </td>
	      </tr>
	      <tr>
	        <td style="width:50%">
	          {% if case.synopsis %}
	            <strong>Synopsis:</strong>
	            {{ case.synopsis|markdown }}
	          {% endif %}
	        </td>
	        <td>
	          <strong>Associated phenotypes:</strong><br>
	          {% if case.phenotype_terms %}
	            <ul>
	              {% for pheno in case.phenotype_terms %}
	                <li>
	                {{pheno.feature}} - (<a href="{{pheno.hpo_link}}" target="_blank">{{pheno.phenotype_id}})</a>
	                </li>
	              {% endfor %}
	            </ul>
	          {% else %}
	            No phenotype terms associated.
	          {% endif %}
	        </td>
	      </tr>
              {% if comments | count_cursor > 0 %}
	        <tr>
	          <td colspan=2>
	            <table id="panel-table" class="table table-sm" style="background-color: transparent">
	              <thead>
	                <tr>
	                  <td><strong>Case-related comments</strong></td>
	                </tr>
	              </thead>
	              <tbody>
	                <tr>
	                  <td>
	                    <table id="panel-table" class="table table-sm" style="background-color: transparent">
	                    {% for comment in comments %}
	                      <tr>
	                        <td style="width:50%;">{{comment.created_at.strftime('%Y-%m-%d')}} {{comment.user_name}}</td>
	                        <td class="text-break"><strong>{{ comment.content | fix_punctuation }}</strong></td>
	                      </tr>
	                    {% endfor %}
	                    </table>
	                  </td>
	                </tr>
	              </tbody>
	            </table>
	          </td>
	        </tr>
	      {% else %}
	        <tr>
	          <td colspan=2>
	            <table id="panel-table" class="table table-sm" style="background-color: transparent">
	              <tr>
	                <td>No comments left for this case.</td>
	              </tr>
	            </table>
	          </td>
	        </tr>
	      {% endif %}
	    </table>
	  </div>
	</div>
{% endmacro %}

{% macro gene_panels_panel() %}
<div class="card mb-3" style="border-width: 5px;">
  <div class="card-header">
    <a name="gene_panels"><strong>Default gene panels</strong></a>
  </div>
  <div class="card-body">
    <table id="panel-table" class="table table-sm">
      <thead>
        <tr>
          <th>Panel</th>
          <th>Version</th>
          <th>Genes</th>
        </tr>
      </thead>
      <tbody>
        {% for panel in case.panels %}
          {% if panel.is_default %}
            <tr>
              <td>
                <a href="{{ url_for('panels.panel', panel_id=panel.panel_id, case_id=case._id, institute_id=institute._id) }}" target="_blank">
                  {{ panel.display_name|truncate(75, True) }}
                </a>
              </td>
              <td>{{ panel.version }} <small class="text-muted">({{ panel.updated_at.date() }})</small></td>
              <td>{{ panel.nr_genes }}</td>
            </tr>
          {% endif %}
        {% else %}
          <tr>
            <td colspan="5">No panels linked to case</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endmacro %}

{% macro causatives_panel() %}
<div class="card border-success mb-3" style="border-width: 5px;">
  <div class="card-header bg-success text-white">
    <a name="causative_variants"><strong>Causative Variants</strong></a>
  </div>
  <div class="card-body">
    {% if causatives_detailed or partial_causatives_detailed %}
      {% for causative in causatives_detailed|sort(attribute='variant_rank') + partial_causatives_detailed|sort(attribute='variant_rank') %}
        {% do printed_vars.append(causative['_id']) %}
        {{ variant_content(causative, loop.index) }}
        <br>
      {% endfor %}
    {% else %}
      No causative variants available for this case
    {% endif %}
  </div>
</div>
{% endmacro%}

{% macro pinned_panel() %}
<div class="card border-info mb-3" style="border-width: 5px;">
  <div class="card-header bg-info text-white">
    <a name="pinned_variants"><strong>Pinned Variants</strong></a>
  </div>
  <div class="card-body">
    {% set duplicated_variants = [] %}
    {% if suspects_detailed %}
      {% for pinned in suspects_detailed|sort(attribute='variant_rank') %}
        {% if pinned['_id'] not in printed_vars %}
          {% do printed_vars.append(pinned['_id']) %}
          {{ variant_content(pinned, loop.index) }}
        {% else %}
          {% do duplicated_variants.append(pinned['_id']) %}
        {% endif %}
        <br>
      {% endfor %}
    {% else %}
      No pinned variants available for this case
    {% endif %}
    {% if suspects_detailed and duplicated_variants|length == suspects_detailed|length %}
      All pinned variants are described among the causative variants
    {% endif %}
  </div>
</div>
{% endmacro %}

{% macro classified_panel() %}
<div class="card border-warning mb-3" style="border-width: 5px;">
  <div class="card-header bg-warning text-black">
    <a name="acmg_variants"><strong>Other ACMG-classified Variants</strong></a>
  </div>
  <div class="card-body">
    {% set duplicated_variants = [] %}
    {% if classified_detailed %}
      {% for variant in classified_detailed|sort(attribute='variant_rank') %}
        {% if variant['_id'] not in printed_vars %}
          {% do printed_vars.append(variant['_id']) %}
          {{ variant_content(variant, loop.index) }}
        {% else %}
          {% do duplicated_variants.append(variant['_id']) %}
        {% endif %}
        <br>
      {% endfor %}
    {% else %}
      No ACMG-classified variants available for this case
    {% endif %}
    {% if classified_detailed and duplicated_variants|length == classified_detailed|length %}
      All ACMG-classified variants are already described in the previous views
    {% endif %}
  </div>
</div>
{% endmacro %}

{% macro tagged_panel() %}
<div class="card border-warning mb-3" style="border-width: 5px;">
  <div class="card-header bg-warning text-black">
    <a name="manual_ranked_variants"><strong>Other Manual Ranked (Tagged) Variants</strong></a>
  </div>
  <div class="card-body">
    {% set duplicated_variants = [] %}
    {% if tagged_detailed %}
      {% for variant in tagged_detailed|sort(attribute='variant_rank') %}
        {% if variant['_id'] not in printed_vars %}
          {% do printed_vars.append(variant['_id']) %}
          {{ variant_content(variant, loop.index) }}
        {% else %}
          {% do duplicated_variants.append(variant['_id']) %}
        {% endif %}
        <br>
      {% endfor %}
    {% endif %}
    {% if tier_detailed %}
      {% for variant in tier_detailed|sort(attribute='variant_rank') %}
        {% if variant['_id'] not in printed_vars %}
          {% do printed_vars.append(variant['_id']) %}
          {{ variant_content(variant, loop.index) }}
        {% else %}
          {% do duplicated_variants.append(variant['_id']) %}
        {% endif %}
        <br>
      {% endfor %}
    {% endif %}
    {% if not (tagged_detailed or tier_detailed) %}
      No tagged variants for this case
    {% endif %}
    {% if (tagged_detailed or tier_detailed) and duplicated_variants|length == (tagged_detailed|length+tier_detailed|length) %}
      All tagged variants are already described in the previous views
    {% endif %}
  </div>
</div>
{% endmacro %}

{% macro variant_content(variant, index) %}
  {% if variant.category == 'snv' %}
    {{ sn_variant_content(variant, index) }}
  {% elif variant.category == 'cancer' %}
    {{ sn_variant_content(variant, index) }}
  {% else %}
    {{ sv_variant_content(variant, index) }}
  {% endif %}
{% endmacro %}

{% macro commented_panel() %}
<div class="card border-warning mb-3" style="border-width: 5px;">
  <div class="card-header bg-warning text-black">
    <a name="commented_variants"><strong>Other Commented Variants</strong></a>
  </div>
  <div class="card-body">
    {% set duplicated_variants = [] %}
    {% if commented_detailed %}
      {% for variant in commented_detailed|sort(attribute='variant_rank') %}
        {% if variant['_id'] not in printed_vars %}
          {% do printed_vars.append(variant['_id']) %}
          {% if variant.category == 'snv' %}
            {{ sn_variant_content(variant, loop.index) }}
          {% elif variant.category == 'cancer' %}
            {{ sn_variant_content(variant, loop.index) }}
          {% else %}
            {{ sv_variant_content(variant, loop.index) }}
          {% endif %}
        {% else %}
          {% do duplicated_variants.append(variant['_id']) %}
        {% endif %}
        <br>
      {% endfor %}
    {% else %}
      No commented variants for this case
    {% endif %}
    {% if commented_detailed and duplicated_variants|length == commented_detailed|length %}
      All commented variants are already described in the previous views
    {% endif %}
  </div>
</div>
{% endmacro %}

{% macro sn_variant_content(variant, index) %} <!--The entire description of a single nucleotide variant, dynamic content -->
  <div class="card" style="border-width: 5px;">
    <div class="card-header">
      <div class="row d-flex align-items-center">
        # <a href="{{ url_for('variant.variant', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" target="_blank"><strong>{{variant.display_name[:30]}}</strong></a>&nbsp;
          <span class="badge badge-pill badge-success">SNV</span>&nbsp;
          {% if variant.sanger_ordered and variant.validation %}
            <span class="badge badge-pill badge-info" title="verif-status">Verification:{{variant.validation}}</span>
          {% elif variant.sanger_ordered %}
            <span class="badge badge-info" title="sanger-status">Verification ordered</span>
          {% endif %}
      </div>
      {% if variant.get('phenotypes') %}
        <div class="row mt-3">
          <h6 class="card-subtitle mb-2 text-muted"><span class="badge badge-secondary">Partial causative</span>
            OMIM terms: {{ variant['phenotypes'].get('diagnosis_phenotypes')|join(' ')}} -
            HPO terms:&nbsp;
            {%- for hpo_term in variant['phenotypes'].get('phenotype_terms') -%}
              {{ hpo_term.phenotype_id}}({{hpo_term.feature}})&nbsp;
            {%- endfor -%}
          </h6>
        </div>
      {% endif %}
    </div>
    <div class="card-body">
      <table id="panel-table" class="table table-sm">
        <thead>
          <tr>
            <th>Coordinates</th>
            <th>Cytoband</th>
            <th>nucl. change</th>
            <th>SNP ids</th>
            <th>Gene panels</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>chr{{variant.chromosome}}:{{variant.position}}-{{variant.end}}</td>
            {% if variant.cytoband_start and variant.cytoband_end %}
              <td>{{variant.cytoband_start}}-{{variant.cytoband_end}}</td>
            {% else %}
              <td>-</td>
            {% endif %}
            <td style="word-break: break-all;">{{variant.reference}} → {{variant.alternative}}</td>
            <td>
              {% if variant.dbsnp_id %}
                {% set dbsnp_ids = variant.dbsnp_id.split(';') %}
                {% for snp in dbsnp_ids %}
                  {% if "rs" in snp %}
                    <a target="_blank" href="{{variant.snp_links[snp]}}">
                      dbSNP:{{ snp }}
                    </a>
                  {% else %}
                    <a target="_blank" href="{{variant.snp_links[snp]}}">
                      ClinVar:{{ snp }}
                    </a>
                  {% endif %}
                {% endfor %}
              {% else %}
                -
              {% endif %}
            </td>
            <td>
              {% for panel_id in variant.panels %}
                <a href="{{ url_for('panels.panel', panel_id=panel_id) }}" target="_blank" class="badge badge-secondary">{{ panel_id }}</a>
              {% endfor %}
            </td>
          </tr>
        </tbody>
      </table>
      <table class="table table-sm">
        <tr>
          <td style="width=45%">
            <table id="panel-table" class="table table-bordered table-sm" style="background-color: transparent">
              <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>
                    {% if cancer %}
                      <th rowspan="2" title="Variant Allele Frequency">VAF</th>
                    {% else %}
                      <th rowspan="2" title="Phred-scaled confidence that the true genotype is the one provided in GT (max=99).">Genotype quality (GQ)</th>
                    {% endif %}
                  </tr>
                  <tr>
                    <th>Reference</th>
                    <th>Alternative</th>
                  </tr>
                </tr>
              </thead>
              <tbody>
                {% for sample in variant.samples %}
                  <tr {% if sample.display_name in affected %} class="table-danger" {% endif%}>
                    <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 %}
                    {% if cancer %}
                      {% if sample.read_depth and sample.allele_depths[1] != -1 %}
                        <td class="text-right">{{ (sample.allele_depths[1]/sample.read_depth)|round(4) }}</td>
                      {% else %}
                        <td class="text-right">N/A</td>
                      {% endif %}
                    {% else %}
                      <td class="text-right">{{ sample.genotype_quality }}</td>
                    {% endif %}
                  </tr>
                {% endfor %}
              </tbody>
            </table>
          </td>
          <td style="width:3%"></td>
          <td>
            <table id="panel-table" class="table table-sm" style="background-color: transparent">
              <thead>
                <tr>
                  <th>Pop. frequency
                    {% if variant.frequency == 'common' %}
                      <span class="badge badge-danger">{{variant.frequency}}</span>
                    {% elif variant.frequency == 'uncommon' %}
                      <span class="badge badge-warning">{{variant.frequency}}</span>
                    {% else %}
                      <span class="badge badge-success">{{variant.frequency}}</span>
                    {% endif %}
                  </th>
                  <th>
                  </th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>
                    {% if variant.dbsnp_id %}
                      <a href="{{ variant.thousandg_link }}" target="_blank">1000G</a>
                    {% else %}
                      1000G
                    {% endif %}
                  </td>
                  <td>
                    {% 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 }}
                    {% if not variant.max_thousand_genomes_frequency and not variant.thousand_genomes_frequency %}
                      <span class="text-muted">Not annotated</span>
                    {% endif %}
                  </td>
                </tr>
                <tr>
                  <td><a title="Exome Aggregation Consortium" target="_blank" href="{{ variant.exac_link }}">ExAC</a></td>
                  <td>
                    {% if variant.max_exac_frequency %}
                      {{ variant.max_exac_frequency|human_decimal }} <small>(max)</small> |
                    {% endif %}
                    {{ variant.exac_frequency|human_decimal if variant.exac_frequency }}
                    {% if not variant.max_exac_frequency and not variant.exac_frequency %}
                      <span class="text-muted">Not annotated</span>
                    {% endif %}
                  </td>
                </tr>
                <tr>
                  <td><a title="genome Aggregation Database" target="_blank" href="{{ variant.gnomad_link }}">gnomAD</a></td>
                  <td>
                    {% 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 %}
                  </td>
                  <td>
                  </td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </table>
      <table id="panel-table" class="table table-sm" style="background-color: transparent">
        <thead>
          <tr>
            <th>Scout Rank</th>
            <th>Scout score</th>
            <th>Manual rank</th>
            <th>CADD score</th>
            {% if not cancer %}
              <th>Inheritance models</th>
            {% endif %}
            <th>ACMG classification</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>{{variant.variant_rank}}</td>
            <td>{{variant.rank_score}}</td>
            <td>
            {% if variant.manual_rank %}
              <span class="badge badge-pill badge-secondary" title="Manual rank">{{ manual_rank_options[variant.manual_rank]['label'] }}</span>
            {% endif %}
            {% if variant.cancer_tier %}
              <span class="badge badge-pill badge-secondary" title="Tier">{{ cancer_tier_options[variant.cancer_tier]['label'] }}</span>
            {% endif %}
            {% if not (variant.manual_rank or variant.cancer_tier) %}
              -
            {% endif %}
            </td>
            <td>
              <span class="float-left">
                <strong>
                  {% if variant.cadd_score %}
                    {{ variant.cadd_score|round(1) }}
                  {% else %}
                    -
                  {% endif %}
                </strong>
              </span>
            </td>
            {% if not cancer %}
              <td>
                <span class="float-left">
                  {% for model in variant.genetic_models|sort %}
                    <span class="badge badge-info" title="{{genetic_models[model]}}">{{ model }}</span>
                  {% else %}
                    <span class="badge badge-warning">No models followed</span>
                  {% endfor %}
                </span>
              </td>
            {% endif %}
            <td>
              {% if variant.acmg_classification %}
                <span class="badge badge-pill badge-{{variant.acmg_classification['color'] if variant.acmg_classification['color'] else 'secondary'}}" title="{{variant.acmg_classification['code']}}">{{variant.acmg_classification['code'] }}</span>
              {% else %}
                -
              {% endif %}
            </td>
          </tr>
        </tbody>
      </table>
      <table id="panel-table" class="table table-sm" style="background-color: transparent">
        <thead>
          <tr>
            <th>Affected gene(s)</th>
            <th>Description</th>
            <th>Region annotation</th>
            <th>Transcript - HGVS - Protein</th>
            <th>OMIM phenotypes [inheritance]</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>{{gene.description|title}}</td>
              <td>{{gene.region_annotation}}</td>
              <td>
                {{ variant_transcripts(gene) }}
              </td>
              <td>
                <ul>
                {% for disease_term in gene.disease_terms %}
                  <li>{{ disease_term.description }}&nbsp;{{ disease_term.inheritance }}</li>
                {% endfor %}
              </ul>
              </td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
      {% if variant.comments | count_cursor > 0  %}
        <table id="panel-table" class="table table-sm" style="background-color: transparent">
          <thead>
            <tr>
              <td><strong>Variant-related comments</strong></td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                <table id="panel-table" class="table table-sm" style="background-color: transparent">
                {% for comment in variant.comments %}
                  <tr>
                    <td style="width:50%;">{{comment.created_at.strftime('%Y-%m-%d')}} {{comment.user_name}}</td>
                    <td class="text-break"><strong>{{ comment.content | fix_punctuation }}</strong></td>
                  </tr>
                {% endfor %}
                </table>
              </td>
            </tr>
          </tbody>
        </table>
      {% endif %}
    </div>
  </div>
  {% endmacro %}

{% macro dismissed_gene_list(variant) %}
  {% if variant.genes|length > 3%}
    {{ variant.genes|length }}&nbsp;genes
  {% else %}
    {% for gene in variant.genes %}
     <a href="http://www.genenames.org/cgi-bin/gene_symbol_report?match={{ gene.hgnc_symbol }}" target="_blank">{{ gene.hgnc_symbol }}&nbsp;<span class="badge badge-secondary">{{gene.region_annotation}}</span></a><br>
    {% else %} <!-- 0 genes. strs for instance -->
      <span class="badge badge-secondary">-</span>
    {% endfor %}
  {% endif %}
{% endmacro %}

{% macro dismissed_panel() %}
  <div class="card border-danger mb-3" style="border-width: 5px;">
    <div class="card-header bg-danger text-black">
      <a name="dismissed_variants"><strong>Dismissed Variants</strong></a>
    </div>
    <div class="card-body">
      {% if dismissed_detailed %}
        <table class="table table-striped table-sm" style="background-color: transparent">
          <thead>
            <tr>
                <td width="5%"></td>
                <td width="25%"><strong>Variant</strong></td>
                <td width="5%"><strong>Category</strong></td>
  	            <td width="25%"><strong>Genes</strong></td>
                <td width="45%"><strong>Dismissed description</strong></td>
            </tr>
          </thead>
          {% for variant in dismissed_detailed|sort(attribute='variant_rank') %}
            <tr>
              <td width="5%">#{{loop.index}}</td>
              <td width="25%">
                {% if variant.category == 'snv' %}
                  <a href="{{ url_for('variant.variant', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" target="_blank"><strong>{{variant.display_name[:30]}}</strong></a>
                {% elif variant.category == 'sv' %}
                  <a href="{{ url_for('variant.sv_variant', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" target="_blank"><strong>chr{{variant.chromosome}}:{{variant.position}}_{{variant.sub_category|upper}}</strong></a>
                {% else %} <!--strs -->
                  <a href="{{ url_for('variant.variant', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" target="_blank"><strong>rep. {{variant.str_repid}}</strong></a>
                {% endif %}
              </td>
              <td width="5%">{{variant.category|upper}}</td>
              <td width="15%">
                {{ dismissed_gene_list(variant) }}
              </td>

              <td>
                <ul>
                  {% for reason in variant.dismiss_variant if not reason == "Select a tag"  %}
                    <li>{{dismissed_options[reason|int]['description']}}
                      {% if reason == '2' and variant.category == 'snv'%} <!--variant dismissed as too common in public databases-->
                        <table class="table-borderless table-sm">
                          <thead>
                            <tr style="background-color: transparent">
                              <th>Pop. frequency
                                {% if variant.frequency == 'common' %}
                                  <span class="badge badge-danger">{{variant.frequency}}</span>
                                {% elif variant.frequency == 'uncommon' %}
                                  <span class="badge badge-warning">{{variant.frequency}}</span>
                                {% else %}
                                  <span class="badge badge-success">{{variant.frequency}}</span>
                                {% endif %}
                              </th>
                              <th>
                              </th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr style="background-color: transparent">
                              <td>
                                {% if variant.dbsnp_id %}
                                  <a href="{{ variant.thousandg_link }}" target="_blank">1000G</a>
                                {% else %}
                                  1000G
                                {% endif %}
                              </td>
                              <td>
                                {% 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 }}
                                {% if not variant.max_thousand_genomes_frequency and not variant.thousand_genomes_frequency %}
                                  <span class="text-muted">Not annotated</span>
                                {% endif %}
                              </td>
                            </tr>
                            <tr style="background-color: transparent">
                              <td><a title="Exome Aggregation Consortium" target="_blank" href="{{ variant.exac_link }}">ExAC</a></td>
                              <td>
                                {% if variant.max_exac_frequency %}
                                  {{ variant.max_exac_frequency|human_decimal }} <small>(max)</small> |
                                {% endif %}
                                {{ variant.exac_frequency|human_decimal if variant.exac_frequency }}
                                {% if not variant.max_exac_frequency and not variant.exac_frequency %}
                                  <span class="text-muted">Not annotated</span>
                                {% endif %}
                              </td>
                            </tr>
                            <tr style="background-color: transparent">
                              <td><a title="genome Aggregation Database" target="_blank" href="{{ variant.gnomad_link }}">gnomAD</a></td>
                              <td>
                                {% 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 %}
                              </td>
                              <td>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      {% elif reason == '7' %} <!-- Inheritance pattern not relevant -->
                      <br>
                        ({% for model in variant.genetic_models|sort %}
                          <span class="badge badge-info" title="{{genetic_models[model]}}">{{model}}</span>
                        {% else %}
                          No models followed
                        {% endfor %})
                        <br>
                      {% elif reason== '23' %} <!-- inherited from unaffected -->
                        <table id="panel-table" class="table-borderless table-sm" >
                          <thead>
                            <tr style="background-color: transparent">
                              <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</th>
                              </tr>
                              <tr style="background-color: transparent">
                                <th>Ref.</th>
                                <th>Alt.</th>
                              </tr>
                            </tr>
                          </thead>
                          <tbody>
                            {% for sample in variant.samples %}
                              <tr style="background-color: transparent" {% if sample.display_name in affected %} class="table-danger" {% endif %}>
                                <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-center">{{ number }}</td>
                                    {% endfor %}
                                {% else %}
                                    <td class="text-center"><small>N/A</small></td>
                                    <td class="text-center"><small>N/A</small></td>
                                {% endif %}

                                <td class="text-center">{{ sample.genotype_quality }}</td>
                              </tr>
                            {% endfor %}
                          </tbody>
                        </table>
                      {% endif %}
                    </li>
                  {% endfor %}
                </ul>
              </td>
            </tr>
    {% endfor %}
  </table>
  {% else %}
    No dismissed variants for this case
  {% endif %}
  </div>
</div>
{% endmacro %}

{% macro sv_variant_content(variant,index) %} <!--The entire description of a structural variant, dynamic contentc -->
<div class="card" style="border-width: 5px;">
  <div class="card-header">
    <div class="row d-flex align-items-center">
    # <a href="{{ url_for('variant.sv_variant', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" target="_blank"><strong>chr{{variant.chromosome}}:{{variant.position}}_{{variant.sub_category|upper}}</strong></a>
    &nbsp;<span class="badge badge-pill badge-warning">SV</span>
    </div>
    {% if variant.get('phenotypes') %}
      <div class="row mt-3">
        <h6 class="card-subtitle mb-2 text-muted"><span class="badge badge-secondary">Partial causative</span>
          OMIM terms: {{ variant['phenotypes'].get('diagnosis_phenotypes')|join(' ')}} -
          HPO terms:&nbsp;
          {%- for hpo_term in variant['phenotypes'].get('phenotype_terms') -%}
            {{ hpo_term.phenotype_id}}({{hpo_term.feature}})&nbsp;
          {%- endfor -%}
        </h6>
      </div>
    {% endif %}
  </div>
  <div class="card-body">
    <table id="panel-table" class="table table-sm" style="background-color: transparent">
      <thead>
        <tr>
          <th>Variant type</th>
          <th>length</td>
          <th>Coordinates</th>
          <th>Cytoband</th>
          <th>Gene panels</th>
          <th>Callers</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><span class="badge badge-warning float-left">{{variant.sub_category|upper}}</span></td>
          <td>{{ variant.length }}</td>
          <td>
            {% if variant.chromosome == variant.end_chrom %}
              chr{{variant.chromosome}}:{{variant.position}}-{{variant.end}}
            {% else %}
              chr{{variant.chromosome}}:{{variant.position}}/{{'chr'+variant.end_chrom}}:{{variant.end}}
            {% endif %}
          </td>
          {% if variant.cytoband_start and variant.cytoband_end %}
            <td>{{variant.cytoband_start}}-{{variant.cytoband_end}}</td>
          {% else %}
            <td>-</td>
          {% endif %}
          <td>
            {% for panel_id in variant.panels %}
              <a href="{{ url_for('panels.panel', panel_id=panel_id) }}" target="_blank">{{ panel_id }}</a><br>
            {% endfor %}
          </td>
          <td>
            {% if variant.callers %}
              {% for caller,call in variant.callers|sort %}
                <span class="badge badge-secondary">{{ caller+':'+call }}</span>
              {% endfor %}
            {% else %}
            -
            {% endif %}
          </td>
        </tr>
      </tbody>
    </table>
    <table id="panel-table" class="table table-sm" style="background-color: transparent">
      <tr>
        <td>
          <table id="panel-table" class="table table-bordered table-sm" style="background-color: transparent">
            <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>
                  {% if cancer %}
                    <th rowspan="2" title="Variant Allele Frequency">VAF</th>
                  {% else %}
                    <th rowspan="2" title="Phred-scaled confidence that the true genotype is the one provided in GT (max=99).">Genotype quality (GQ)</th>
                  {% endif %}
                </tr>
                <tr>
                  <th>Reference</th>
                  <th>Alternative</th>
                </tr>
              </tr>
            </thead>
            <tbody>
              {% for sample in variant.samples %}
                <tr {% if sample.display_name in affected %} class="table-danger" {% endif %}>
                  <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 %}
                  {% if cancer %}
                    {% if sample.read_depth and sample.allele_depths[1] != -1 %}
                      <td class="text-right">{{ (sample.allele_depths[1]/sample.read_depth)|round(4) }}</td>
                    {% else %}
                      <td class="text-right">N/A</td>
                    {% endif %}
                  {% else %}
                    <td class="text-right">{{ sample.genotype_quality }}</td>
                  {% endif %}
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </td>
        <td style="width:3%"></td>
        <td>
          <table id="panel-table" class="table table-sm" style="background-color: transparent">
            <thead>
              <th colspan=2>Pop. frequency</th>
            </thead>
            <tbody>
              {% for freq_name, value, link in variant.frequencies %}
                <tr>
                  <td>{{ freq_name }}</td>
                  <td>
                    {% if value %}
                      <span class="badge badge-secondary">{{ value|human_decimal }}</span>
                    {% else %}
                      <span class="text-muted">Not annotated</span>
                    {% endif %}
                  </td>
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </td>
      </tr>
    </table>
    <table id="panel-table" class="table table-sm" style="background-color: transparent">
      <thead>
        <tr>
          <th>Scout Rank</th>
          <th>Scout score</th>
          <th>Manual rank</th>
          {% if not cancer %}
            <th>Inheritance models</th>
          {% endif %}
          <th>ACMG classification</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>{{variant.variant_rank}}</td>
          <td>{{variant.rank_score}}</td>
          <td>
          {% if variant.manual_rank %}
            <span class="badge badge-pill badge-secondary" title="Manual rank">{{ manual_rank_options[variant.manual_rank]['label'] }}</span>
          {% endif %}
          {% if variant.cancer_tier %}
            <span class="badge badge-pill badge-secondary" title="Tier">{{ cancer_rank_options[variant.cancer_tier]['label'] }}</span>
          {% endif %}
          {% if not (variant.manual_rank or variant.cancer_tier) %}
            -
          {% endif %}
          </td>
          <td>
            <span class="float-left">
              {% for model in variant.genetic_models|sort %}
                <span class="badge badge-info" title="{{genetic_models[model]}}">{{ model }}</span>
              {% else %}
                <span class="badge badge-warning">No models followed</span>
              {% endfor %}
            </span>
          </td>
          <td>
            {% if variant.acmg_classification %}
              <span class="badge badge-pill badge-{{variant.acmg_classification['color'] if variant.acmg_classification['color'] else 'secondary'}}" title="{{variant.acmg_classification['code']}}">{{variant.acmg_classification['code'] }}</span>
            {% else %}
              -
            {% endif %}
          </td>
        </tr>
      </tbody>
    </table>
    <table id="panel-table" class="table table-sm" style="background-color: transparent">
      <thead>
        <tr>
          <th>Affected gene(s)</th>
          <th>Description</th>
          <th>Region annotation</th>
          <th>Transcript - HGVS - Protein</th>
          <th>OMIM phenotypes [inheritance]</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>{{gene.description|title}}</td>
            <td>{{gene.region_annotation}}</td>
            <td>
              {{ variant_transcripts(gene) }}
            </td>
            <td>
              <ul>
              {% for disease_term in gene.disease_terms %}
                <li>{{ disease_term.description }}&nbsp;{{ disease_term.inheritance }}</li>
              {% endfor %}
              </ul>
            </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>


    {% if variant.comments | count_cursor > 0  %}
      <br>
      <table id="panel-table" class="table table-sm" style="background-color: transparent">
        <thead>
          <tr>
            <td colspan=3><strong>Variant-related comments</strong></td>
          </tr>
        </thead>
        <tbody>
          {% for comment in variant.comments %}
            <tr>
              <td style="width:50%;">{{comment.created_at.strftime('%Y-%m-%d')}} {{comment.user_name}}</td>
              <td class="text-break"><strong>{{ comment.content | fix_punctuation}}</strong></td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    {% endif %}
  </div>
</div>
{% endmacro %}


{% block scripts %}
{{ super() }}
  <script src="{{ url_for('cases.static', filename='madeline.js') }}"></script>
{% endblock %}
