{% extends "humanization/humanness_layout.html" %}
{% set title = result.input.name + " - Humanness Report" %}

{% set MIN_SUBJ = result.oasis_params.min_fraction_subjects %}
{% set RARE_FAMILY_FREQUENCY = 0.01 %}

{%
  from 'humanization/humanize_alignment_component.html'
  import chain_humanness_header, format_sequence_tooltip, format_percent_subjects, format_aa_frequency_tooltip, format_aa_frequency, annot_num with context
%}

{% block main %}

<div class="row mt-3">
  <div class="col-sm-5">
    <a class="btn btn-light" href="{{ url_for('biophi_humanization.humanness_report_table_get', task_id=task_id) }}">
      {{ icon('chevron-left') }} Results
    </a>
  </div>
  {% if result_index > 1 or result_index < len_results %}
    <div class="col-sm-2">
      {% if result_index > 1  %}
        <a class="btn btn-light" href="{{ url_for('biophi_humanization.humanness_report_detail_get', task_id=task_id,  result_index=result_index-1) }}"> {{ icon('caret-left-fill') }}  </a>
      {% else %}
        <a class="btn btn-light" style="visibility:hidden"> {{ icon('caret-left-fill') }} </a>
      {% endif %}
      {{result_index}}/{{len_results}}
      {% if result_index < len_results %}
        <a class="btn btn-light" href="{{ url_for('biophi_humanization.humanness_report_detail_get', task_id=task_id,  result_index=result_index+1) }}"> {{ icon('caret-right-fill') }} </a>
      {% else %}
        <a class="btn btn-light" style="visibility:hidden"> {{ icon('caret-right-fill') }} </a>
      {% endif %}
    </div>
  {% endif %}
  <div class="col-sm-5 d-print-none">
    <div class="mb-3 text-end">
      <div class="d-inline-block">
        <a class="btn btn-light" href="{{ url_for('biophi_humanization.humanize_get', humanness_task_id=task_id, index=result_index) }}">
          {{ icon('arrow-right-short') }} Humanize
        </a>
      </div>
      <div class="d-inline-block">
        <a class="btn btn-light" target="_blank" href="{{ url_for('biophi_humanization.designer_get', humanness_task_id=task_id, index=result_index) }}">
          {{ icon('pencil') }} Edit in Designer
        </a>
      </div>
      <div class="d-inline-block dropdown">
        <a class="btn btn-primary dropdown-toggle" role="button" id="exportDropdown" data-bs-toggle="dropdown" aria-expanded="false">
          Export
        </a>
        <ul class="dropdown-menu" aria-labelledby="exportDropdown">
          <li>
            <a class="dropdown-item" target="_blank"
                 href="{{ url_for('biophi_humanization.humanness_detail_export_oasis_table', task_id=task_id, result_index=result_index) }}">OASis table</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>


<div class="col-sm-9">
    <h3>Humanness report <span class="text-secondary fw-light">{{ result.input.name }}</span></h3>
</div>


<div class="card-group mb-3" style="max-width: 700px;">
  <div class="card">
    <div class="card-body">
      <h5 class="card-title fs-4 fw-light">
        <span class="fw-normal">{{ '{:.0%}'.format(result.humanness.get_oasis_identity(MIN_SUBJ)) }}</span> OASis identity
      </h5>
      <p class="card-text text-secondary fs-small">
        {{ result.humanness.get_num_human_peptides(MIN_SUBJ) }}/{{ result.humanness.get_num_peptides() }} peptides are considered human
        <br>(are found in at least {{ '{:.0%}'.format(MIN_SUBJ) }} of human subjects)
      </p>
      <p class="card-text text-secondary fs-small">
        This corresponds to the
        <span class="badge fw-normal bg-spectral{{ '{:.0f}'.format(result.humanness.get_oasis_percentile(MIN_SUBJ)*100) }}">
          {{ '{:.0%}'.format(result.humanness.get_oasis_percentile(MIN_SUBJ)) }}
        </span>
        percentile {{ info_icon('Percentile of OASis identity among therapeutic mAbs at the current prevalence threshold. Zero percentile corresponds to the least human and 100% percentile corresponds to the most human mAb in the clinic, including all clinical stage human, humanized and murine therapeutics.') }}
      </p>
    </div>
  </div>
  <div class="card">
    <div class="card-body">
      <h5 class="card-title fs-4 fw-light">
        <span class="fw-normal">{{ '{:.0%}'.format(result.humanness.get_germline_content()) }}</span>
        germline content
      </h5>
      <p class="card-text text-secondary fs-small">Sequence identity with nearest heavy and light human germline sequences.</p>
      <p class="card-text">
        {{ result.humanness.vh.v_germline_family }}<span class="text-secondary">{{ result.humanness.vh.v_germline_suffix }}</span>
        {% if result.humanness.vh and result.humanness.vl %}+{% endif %}
        {{ result.humanness.vl.v_germline_family }}<span class="text-secondary">{{ result.humanness.vl.v_germline_suffix }}</span>
      </p>
    </div>
  </div>
</div>

{% if result.humanness.vh %}
  <h4 class="fw-light">
    Heavy chain
    {{ chain_humanness_header(result.humanness.vh, MIN_SUBJ) }}
  </h4>
  {{
    oasis_sequence(result.humanness.vh)
  }}
{% endif %}

{% if result.humanness.vl %}
  <h4 class="fw-light">
    Light chain
    {{ chain_humanness_header(result.humanness.vl, MIN_SUBJ) }}
  </h4>
  {{
    oasis_sequence(result.humanness.vl)
  }}
{% endif %}

{% if result.humanness.vh %}
  <h4 class="fw-light">Heavy chain table</h4>
  {{ oasis_table(result.humanness.vh) }}
{% endif %}

{% if result.humanness.vl %}
  <h4 class="fw-light">Light chain table</h4>
  {{ oasis_table(result.humanness.vl) }}
{% endif %}

{% endblock %}


{% macro oasis_sequence(chain_humanness) %}
  <div class="text-nowrap">
    <div class="mb-2 font-monospace fs-smaller">
        {% for pos, aa, non_human_peptides in chain_humanness.get_positional_humanness(MIN_SUBJ)
        %}{% set peptide = chain_humanness.get_peptide(pos, edges=True)
        %}{% set num_non_human = (non_human_peptides | length)
        %}{% set freqs = chain_humanness.germline_family_residue_frequency.get(pos)
        %}<a href="#{{ pos }}" class="oasis-seq-aa {% if not freqs or freqs.get(aa, 0) < RARE_FAMILY_FREQUENCY %}oasis-seq-aa-rare{% endif %}"
             data-bs-toggle="tooltip" data-bs-html="true" data-tooltip-classes="tooltip-peptides"
             title="{{ format_sequence_tooltip(chain_humanness.v_germline_family, pos, aa, freqs, non_human_peptides, peptide) }}"
                style="border-top: 3px solid {% if pos.is_in_cdr() %}#777777{% elif pos.cdr_definition == 'kabat' and pos.is_in_vernier() %}#cccccc{% else %}white{% endif %};
                background-color: rgb(255, {{ 255 - num_non_human**0.80 * 30 }}, {{ 255 - num_non_human**0.65 * 40 }});">{{ aa }}</a>{%
        endfor %}
    </div>
    <div class="mb-3">
      {% set v_germlines = chain_humanness.get_v_germline_chains(5) %}
      {% set j_germlines = chain_humanness.get_j_germline_chains(5) %}
      {% for i in range(v_germlines | length) %}
        <div class="font-monospace fs-smaller">
          {% for pos, seq_aa in chain_humanness.imgt_chain
          %}{% set germline_aa = v_germlines[i][pos] or j_germlines[i][pos] or '-' %}<span
                style="{% if seq_aa == germline_aa %}background: #b3f7b3;{% endif %}" class="d-inline-block">{{ germline_aa }}</span>{%
          endfor %}
          {{ v_germlines[i].name }}, {{ j_germlines[i].name }}
        </div>
      {% endfor %}
    </div>
  </div>
{% endmacro %}

{% macro oasis_table(chain_humanness) %}
<table class="table table-humanization">
  <thead>
    <tr>
      <th colspan="2" class="border-end">Position</th>
      <th colspan="3" class="border-end">Amino acid</th>
      <th colspan="3">OASis 9-mer peptide search</th>
      <th></th>
    </tr>
  </thead>
  <thead>
    <tr>
      <th>Region</th>
      <th class="border-end">{{ list(chain_humanness.peptides)[0].scheme.title() }}</th>
      <th>AA</th>
      <th>Chain<br>Freq&nbsp;{{ info_icon('Residue frequency in {} chain at given {} position'.format(chain_humanness.chain.chain_type, chain_humanness.chain.scheme.title())) }}</th>
      <th class="border-end">Family<br>Freq&nbsp;{{ info_icon('Residue frequency in {} germline family at given {} position'.format(chain_humanness.v_germline_family, chain_humanness.chain.scheme.title())) }}</th>
      <th>Peptide&nbsp;{{ info_icon('9-mer peptide starting at given position.') }}</th>
      <th>OAS<br>Subjects&nbsp;{{ info_icon('Percentage of OAS human subjects containing this peptide in their repertoire.') }}</th>
      <th>OAS<br>Sequences&nbsp;{{ info_icon('Number of total antibody sequences containing this peptide across across all human subjects in OAS.') }}</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {% for position, aa in chain_humanness.chain %}
      {{
        oasis_table_row(
          position,
          aa,
          chain_humanness.peptides.get(position),
          chain_humanness.chain_type_residue_frequency.get(position),
          chain_humanness.germline_family_residue_frequency.get(position),
          chain_humanness.v_germline_family
        )
      }}
    {% endfor %}
  </tbody>
</table>
{% endmacro %}

{% macro oasis_table_row(position, aa, peptide, chain_freqs, family_freqs, v_germline_family) %}
<tr>
  <td class="text-end" style="width: 3.5em">{{ position.get_region() }}</td>
  <td class="border-end" style="width: 3.5em"><span class="chain-pos-anchor" id="{{ position }}">{{ position }}</span></td>
  <td class="taylor-{{ aa }}" style="width: 2.5em">{{ aa }}</td>
  <td class="border-end {% if chain_freqs is none or chain_freqs.get(aa, 0) < RARE_FAMILY_FREQUENCY %}text-danger{% endif %}"
             style="width: 5em" data-bs-toggle="tooltip" data-tooltip-classes="tooltip-wide"
             data-bs-html="true"
             title="{{ format_aa_frequency_tooltip(position.chain_type+' chain', aa, chain_freqs) }}">
    {{ format_aa_frequency(chain_freqs.get(aa, 0) if chain_freqs else None) }}
  </td>
  <td class="border-end {% if family_freqs is none or family_freqs.get(aa, 0) < RARE_FAMILY_FREQUENCY %}text-danger{% endif %}"
             style="width: 5em" data-bs-toggle="tooltip" data-tooltip-classes="tooltip-wide"
             data-bs-html="true"
             title="{{ format_aa_frequency_tooltip(v_germline_family, aa, family_freqs) }}">
    {{ format_aa_frequency(family_freqs.get(aa, 0) if family_freqs else None) }}
  </td>
  {% if peptide %}
    <td style="width: 9em" class="font-monospace {% if not peptide.is_human(MIN_SUBJ) %}text-danger{% endif %}">{{ peptide.seq }}</td>
    <td style="width: 8em">{{ format_percent_subjects(peptide.fraction_oas_subjects) }}&nbsp;<span class="text-secondary">subjs</span></td>
    <td style="width: 8em">{{ annot_num(peptide.num_oas_occurrences, 'seqs', 'seq') }}</td>
  {% endif %}
</tr>
{% endmacro %}
