
{% macro rankscore_panel(variant) %}
  <div class="card panel-default">
    <div class="panel-heading">Rank score results</div>
    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered">
          <thead class="thead-light">
            <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>
  </div>
{% endmacro %}

{% macro overlapping_panel(variant, overlapping_vars, case, institute) %}
  <div class="card panel-default">
    {% if variant.category == 'sv' %}
      <div class="panel-heading">Overlapping SNVs</div>
    {% else %}
      <div class="panel-heading">Overlapping SVs</div>
    {% endif %}
    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered">
          <thead class="thead-light">
            <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 overlapping_variant in overlapping_vars %}
            <tr>
              <td>
                {% if overlapping_variant.category in ("sv", "cancer_sv") %}
                  <a href="{{url_for('variant.sv_variant', institute_id=institute._id,
                                   case_name=case.display_name, variant_id=overlapping_variant._id)}}">
                    {{ overlapping_variant.display_name|truncate(20, True) }}
                  </a>
                {% else %}
                  <a href="{{url_for('variant.variant', institute_id=institute._id,
                                 case_name=case.display_name, variant_id=overlapping_variant._id)}}">
                    {{ overlapping_variant.display_name|truncate(20, True) }}
                  </a>
                {% endif %}
              </td>
              <td>{{ overlapping_variant.hgnc_symbols|join(', ')|truncate(40, True) }}</td>
              <td>{{ overlapping_variant.sub_category|upper }}</td>
	            <td class="text-right">{{ variant.rank_score + overlapping_variant.rank_score }}</td>
              <td class="text-right">{{ overlapping_variant.rank_score }}</td>
              <td class="text-right">{{ overlapping_variant.length }}</td>
              <td>{{ overlapping_variant.region_annotations|join(', ')|truncate(40, True) }}</td>
              <td>{{ overlapping_variant.functional_annotations|join(', ')|truncate(40, True) }}</td>
            </tr>
          {% else %}
            <tr>
              <td colspan="8">No overlapping variants found</td>
            </tr>
          {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </div>
{% endmacro %}

{% macro genes_panel(variant) %}
  <div class="card panel-default">
    <table class="table table-bordered table-sm">
      <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(variant) %}
  <div class="card panel-default table-responsive fixed-panel">
    <table id="transcripts_panel_table" class="table table-bordered card-sm">
      <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 class="d-flex justify-content-around align-items-center">
                <a target="_blank" href="{{ transcript.ensembl_link }}">
                  {{ transcript.transcript_id }}
                </a>
                {% if transcript.is_canonical %}
                  <span class="badge badge-info">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(variant) %}
  <div class="panel panel-default table-responsive">
    <table id="proteins_panel_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 sv_alignments(variant, case, zoom)%}
    <div>
      {% if zoom == "start" %}
        {{ variant.chromosome }}:{{ variant.position }}
      {% elif zoom == "end" %}
        {{ variant.chromosome }}:{{ variant.end }}
      {% elif zoom == "length" %}
        {{ variant.length }}
      {% endif %}
      <form role="form" name="bp1_form" action="{{ url_for('alignviewers.igv') }}" target="_blank" method="POST">
        {% if case.mt_bams or case.bam_files %}
        <!-- general hidden fields required to submit POST request to IGV viewer -->
          <input type="hidden" name="sample" value="{{case.sample_names|join(',')}}">
          <input type="hidden" name="build" value="{{case.genome_build}}">
          <input type="hidden" name="contig" value="{{variant.chromosome}}">
          <input type="hidden" name="center_guide" value="T">
          {% if zoom == "start" %}
            <input type="hidden" name="start" value="{{variant.position - 500}}">
            <input type="hidden" name="stop" value="{{variant.position + 500}}">
            <!--<input type="hidden" name="now" values="{{now}}"-->
          {% elif zoom == "end" %}
            <input type="hidden" name="start" value="{{variant.end - 500}}">
            <input type="hidden" name="stop" value="{{variant.end + 500}}">
          {% elif zoom == "length" %}
            <input type="hidden" name="start" value="{{variant.position - 500}}">
            <input type="hidden" name="stop" value="{{variant.end + 500}}">
          {% endif %}
        {% endif %}

        {% if variant.chromosome == "MT" and case.mt_bams %}
          <input type="hidden" name="mt_bam" value="{{case.mt_bams|join(',')}}">
          <input type="hidden" name="mt_bai" value="{{case.mt_bais|join(',')}}">
          - Alignment:
          <button class="btn btn-outline-secondary btn-sm" name="align" value="mt_bam" type=submit>IGV</button>
        {% elif case.bam_files %}
          - Alignment:
          <input type="hidden" name="bam" value="{{case.bam_files|join(',')}}">
          <input type="hidden" name="bai" value="{{case.bai_files|join(',')}}">
          {% if case.rhocall_beds %}
            <input type="hidden" name="rhocall_bed" value="{{case.rhocall_beds|join(',')}}">
            <input type="hidden" name="rhocall_wig" value="{{case.rhocall_wigs|join(',')}}">
          {% endif %}
          {% if case.tiddit_coverage_wigs %}
            <input type="hidden" name="tiddit_coverage_wig" value="{{case.tiddit_coverage_wigs|join(',')}}">
          {% endif %}
          {% if case.upd_regions_beds %}
            <input type="hidden" name="upd_regions_bed" value="{{case.upd_regions_beds|join(',')}}">
          {% endif %}
          {% if case.upd_sites_beds %}
            <input type="hidden" name="upd_sites_bed" value="{{case.upd_sites_beds|join(',')}}">
          {% endif %}
          {% if zoom == "length" and variant.chromosome != variant.end_chrom %}
            - Complex rearrangement: check breakpoint alignments.
          {% else %}
            <button class="btn btn-outline-secondary btn-sm" name="align" value="bam" type=submit>IGV</button>
          {% endif %}
        {% else %}
          - BAM file(s) missing
        {% endif %}

        {% if zoom == "length" and variant.chromosome == variant.end_chrom %}
          - <a class="btn btn-outline-secondary btn-sm" href="http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&position=chr{{ variant.chromosome }}:{{ variant.position }}-{{ variant.end }}&dgv=pack&knownGene=pack&omimGene=pack" target="_blank">UCSC</a>
        {% elif zoom == "start" %}
          - <a class="btn btn-outline-secondary btn-sm" href="http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&position=chr{{ variant.chromosome }}:{{ variant.position }}&dgv=pack&knownGene=pack&omimGene=pack" target="_blank">UCSC</a>
        {% elif zoom == "end" %}
          - <a class="btn btn-outline-secondary btn-sm" href="http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&position=chr{{ variant.chromosome }}:{{ variant.end }}&dgv=pack&knownGene=pack&omimGene=pack" target="_blank">UCSC</a>
        {% endif %}
      </form>
    </div>
{% endmacro %}

{% macro pin_button(variant, case, institute) %}
  {% 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-light 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-light" title="Unpin variant">Unpin</button>
    </form>
  {% endif %}
{% endmacro %}

{% macro causative_button(variant, case, institute) %}
  {% 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,
                               partial_causative=False) }}"
            method="POST">
        <button name="action" value="DELETE" type="submit" class="btn btn-light navbar-btn" title="Reset causative">
          Reset causative
        </button>
      </form>
  {% elif variant._id in case.partial_causatives %}
    <form action="{{ url_for('cases.mark_causative',
                             institute_id=institute._id,
                             case_name=case.display_name,
                             variant_id=variant._id,
                             partial_causative=True) }}"
          method="POST">
    <button name="action" value="DELETE" type="submit" class="btn btn-light navbar-btn" title="Reset causative">
      Reset partial causative
    </button>
  </form>
  {% else %}
  <button class="btn btn-light navbar-btn" data-toggle="modal" data-target="#causativeModal">
    Mark causative
  </button>
  {% endif %}
{% endmacro %}
