<!DOCTYPE html>

{% load static %}

<html>
<head>
  <link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/calipho-sib/feature-viewer/v1.0.0/dist/feature-viewer.min.css">
  <script src="https://cdn.jsdelivr.net/gh/calipho-sib/feature-viewer@v1.0.6/dist/feature-viewer.bundle.js">

  </script>

  <script type="text/javascript" src={% static 'js/genomic_region.js' %}>
  </script>

  {% load custom_tags %}
  {% include "chlamdb/header.html" %}

</head>

<body>

{% if not menu %}
  <div class="container-fluid" id="container_no_margin" height="100%">
{% else %}
  <div class="container-fluid" id="main_container">
{% endif %}

<div class="row">
  <div id="wrapper">
    <div id="page-content-wrapper">
      <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12">
          {% if menu %}
            {% include "chlamdb/menu.html" %}
          {% endif %}
          {% if not valid_id %}
            <div class="alert alert-warning" role="alert" style="width:50%">
              <a>Unknown accession! Accept protein ids, locus tags and orthogroup IDs</a>
            </div>
          {% else %}
            <ul id="tabs" class="nav nav-tabs">
              <li class="active">
                <a href="#main_tab" data-toggle="tab">Overview</a>
              </li>
              <li>
                <a href="#tab3" data-toggle="tab">Sequences</a>
              </li>
              {% if feature_type == "CDS" and n_homologues > 1 %}
                <li>
                  <a href="#homologs_tab" data-toggle="tab">
                    Homologs <span class="badge">{{n_homologues}}</span><br>
                  </a>
                </li>
                <li>
                  <a href="#phylogenetic_distrib" data-toggle="tab">Phylogenetic distribution</a>
                </li>
                {% if n_homologues >= 2 %}
                  <li>
                    <a href="#og_phylogeny" data-toggle="tab" >Orthogroup phylogeny</a>
                  </li>
                {% endif %}

              {% endif %}

              {% if optional2status|keyvalue:"BLAST_database" and n_refseq_homologs > 0 %}
                <li>
                  <a href="#refseq_homologs_tab" data-toggle="tab">
                    RefSeq homologs <span class="badge">{{n_refseq_homologs}}</span>
                  </a>
                </li>
              {% endif %}

              {% if optional2status|keyvalue:"BLAST_swissprot" and swissprot.n_swissprot_hits > 0 %}
                <li>
                  <a href="#swissprot_homologs_tab" data-toggle="tab">
                    SwissProt homologs <span class="badge">{{swissprot.n_swissprot_hits}}</span>
                  </a>
                </li>
              {% endif %}

              {% if has_refseq_phylo and optional2status|keyvalue:"BBH_phylogenies" %}
                <li>
                  <a href="#refseq_phylogeny" data-toggle="tab">RefSeq best hits phylogeny</a>
                </li>
              {% endif %}
            </ul>

            <div id="my-tab-content" class="tab-content">

              <div class="tab-pane" id="homologs_tab">
                {% include "chlamdb/homologs.html" %}
              </div>

              <div class="tab-pane" id="phylogenetic_distrib">
                <h3>Distribution of the homologs in the orthogroup {{og_id|safe}} </h3>

                <div class="tab-pane active" id="tab1">
                  <div class="panel panel-success" style="width:100%; top: 200px; margin: 10px 10px 10px 10px">
                    <div class="panel-heading" style="width:100%">
                      <h3 class="panel-title">Help</h3>
                    </div>
                    <p style="margin: 10px 10px 10px 10px; line-height: 180%">
                      Number of homologs in each genome (first column) and amino-acid identity of the closest homolog (second column).
                    </p>
                  </div>

                  <object type="image/svg+xml" data="{% static asset_path %}"></object>

                  <a download="profile.svg" class="btn" href="{% static asset_path %}">
                    <i class="fa fa-download"></i> Download SVG
                  </a>
                </div>
              </div>

              <div class="tab-pane" id="og_phylogeny">
                <h3>Phylogeny of {{og_id|safe}} </h3>
                <a download="profile.svg" class="btn" href="{% static og_phylogeny %}">
                  <i class="fa fa-download"></i> Download SVG
                </a>

                <a download="alignment.faa" class="btn" href="{% static og_alignment %}">
                  <i class="fa fa-download"></i> Download alignment file
                </a>

                <div class="tab-pane active" id="tab1">
                  <object type="image/svg+xml" data="{% static og_phylogeny %}"></object>
                </div>
              </div>

              <div class="tab-pane" id="refseq_phylogeny">
                <h3>Phylogeny of the RefSeq best hits of {{og_id|safe}}</h3>
                <div class="tab-pane active" id="tab1">
                  <object type="image/svg+xml" data="{% static best_hits_phylogeny %}"></object>
                  <a download="profile.svg" class="btn" href="{% static best_hits_phylogeny %}">
                    <i class="fa fa-download"></i> Download SVG
                  </a>
                </div>
              </div>

              <div class="tab-pane" id="swissprot_homologs_tab">
                <div class="col-md-12 col-lg-10" style="padding-top:20px">
                  {% include "chlamdb/result_table.html" with results=swissprot %}
                </div>
              </div>

              <div class="tab-pane" id="refseq_homologs_tab">
                <br>
                <ul>
                 <li>Number of RefSeq hits: {{n_refseq_homologs}} </li>
                </ul>
                <table id="blast_nr_table" class="table">
                  <thead>
                    <tr>
                      {% for entry in refseq_headers %}
                        <th> {{entry}} </th>
                      {% endfor %}
                    </tr>
                  </thead>
                  <tbody>
                    {% for values in blast_data %}
                      <tr>
                        {% for entry in values %}
                          <td>{{entry|safe}}</td>
                        {% endfor %}
                      </tr>
                    {%  endfor %}
                  </tbody>
                </table>
              </div>

              <div class="tab-pane active" id="main_tab" style="padding-left: 15px;padding-top:20px">
                <div class="col-md-12 col-lg-4">
                  <div class="container-fluid">
                    <div class="row">
                      <div class="panel panel-default">
                        <div class="panel-heading">
                          <h3 class="panel-title">General</h3>
                        </div>
                        <table id="panel_table" class="table table-striped" style="width: 100%">
                          <tr>
                              <th>Source</th>
                              <td>{{organism}}</td>
                          </tr>
                          <tr>
                              <th>Locus tag</th>
                              <td>{{locus_tag}}</td>
                          </tr>
                          <tr>
                            <th> Feature type </th>
                            <td> {{feature_type}} </td>
                          </tr>
                          <tr>
                            <th>Gene</th>
                            <td>{{gene}}</td>
                          </tr>
                          <tr>
                            <th>Product</th>
                            <td>{{prot}}</td>
                          </tr>
                          <tr>
                            <th>Location</th>
                            <td>
                              {% for pos in gene_pos %}
                                {{pos.0}} - {{pos.1}} (strand: {{pos.2}})
                                {% if gene_pos|length > 1 %}
                                  <br>
                                {% endif %}
                              {% endfor %}
                            </td>
                          </tr>

                          <tr>
                            <th>Length</th>
                            {% if sequence_type == "CDS" %}
                              <td>{{nucl_length}} (nucleotides) / {{length}} (amino acids)</td>
                            {% else %}
                              <td>{{nucl_length}} (nucleotides)</td>
                            {% endif %}
                          </tr>
                        </table>
                      </div>
                    </div>

                    {% if sequence_type == "CDS" %}
                      <div class="row">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h3 class="panel-title">Orthology</h3>
                          </div>
                          <table class="table table-striped" style="width: 100%">
                            <tr>
                              <th>Orthogroup</th>
                              <td>{{og_id|safe}}</td>
                              <td></td>
                            </tr>
                            <tr>
                              <th>
                                Orthogroup size
                                <a href="#" data-delay='{"show":"50", "hide":"1000"}' data-toggle="tooltip"
                                   title='Total number of homologs in the whole database'>
                                  <span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span>
                                </a>
                              </th>
                              <td>{{og_size}}</td>
                              <td></td>
                            </tr>
                            <tr>
                              <th>
                                N. genomes
                                <a href="#" data-delay='{"show":"50", "hide":"1000"}' data-toggle="tooltip"
                                   title='Number of genomes encoding one or several homologs of this protein. See the tab "homologs" for details'>
                                  <span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span>
                                </a>
                              </th>
                              <td>{{og_num_genomes}}</td>
                              <td></td>
                            </tr>
                          </table>
                        </div>
                      </div>
                    {% endif %}

                    {% if sequence_type == "CDS" %}
                      <div class="row">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h3 class="panel-title">Actions</h3>
                          </div>
                          <div style="margin: 10px">

                            <a class="button_link" href="https://papers.genomics.lbl.gov/cgi-bin/litSearch.cgi?query={{translation}}" target="_blank">
                              <button class="btn btn-success">
                                Paper blast
                              </button>
                            </a>

                            <button id="blast_protein_button" class="btn btn-success">
                              Blast protein
                            </button>

                          </div>
                        </div>
                      </div>
                    {% endif %}

                  </div> <!-- fluid container -->
                </div> <!-- first column -->
                <div class="col-md-12 col-lg-8">
                  <div class=row>
                    <div class="panel panel-default">
                      <div class="panel-heading clearfix">
                        <div class="pull-right">
                          <button id="download_svg_button" class="btn-link btn-xs" style="background-color: transparent;">
                            <i class="glyphicon glyphicon-download"></i>
                          </button>
                        </div>
                        <h3 class="panel-title">Genomic region</h3>
                      </div>
                      <div class="panel-body" style="height:100px; display:flex; justify-content:center; align-items:center; margin-bottom:30px" id="genomic_region_rect">
                        <div id="div_genomic_region"></div>
                      </div>
                    </div>
                  </div>

                  {% if pfam_def|length > 0 %}
                    <div class="row">
                      <div class="panel panel-default">
                        <div class="panel-heading">
                          <h3 class="panel-title">Domains</h3>
                        </div>
                        <div id="div_features"></div>

                        <table id="panel_table" class="table table-striped" style="width: 100%">
                          {% for val in pfam_def %}
                            <tr>
                              <th>{{val.0|safe}}</th>
                              <td>{{val.1}}</td>
                            </tr>
                          {% endfor %}
                        </table>
                      </div>
                    </div>
                  {% endif %}

                  {% for result_table in result_tables%}
                    {% if result_table|keyvalue:"table_data"|length > 0 %}
                      <div class="row">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h3 class="panel-title">{{result_table|keyvalue:"title"}}</h3>
                          </div>
                          {% include "chlamdb/result_table.html" with results=result_table %}
                        </div>
                      </div>
                    {% endif %}
                  {% endfor %}

                </div> <!-- second column -->
              </div> <!-- first tab -->
              <div class="tab-pane" id="tab3" width="100%" height="100%">
                <div class="row" style="padding-top:15px;">
                  <div class="col-lg-6">

                    {% if sequence_type == "CDS" %}
                      <div class="row">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h3 class="panel-title">Protein Sequence</h3>
                          </div>
                          <div class="panel-body">
                            <p style="word-wrap: break-word;" class="text-justify">{{translation}}</p>
                          </div>
                        </div>
                      </div>
                    {% endif %}

                    <div class="row">
                      <div class="panel panel-default">
                        <div class="panel-heading">
                          <h3 class="panel-title">Flanking regions ( +/- flanking 50bp)</h3>
                        </div>
                        <div class="panel-body">
                          <p style="word-wrap: break-word;" class="text-justify">{{ seq|safe }}</p>
                        </div>
                      </div>
                    </div>

                  </div>
                </div>
              </div>
            </div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>
</div>
</div>

<style>
  #description_field{
      width:100px;
  }
  h3.panel-title > a{
    color: #337ab7;
  }
</style>

</body>

{% csrf_token %}

<script src="{% static 'js/cookie.js' %}"></script>
<script src="{% static 'js/jquery.redirect.js' %}"></script>

<script>

$(document).ready(function() {
    $('#domains_table').DataTable( {
        dom: 't',
        "order": [[ 0, "desc" ]],
        "pageLength": 200
    } );
} );


$(document).ready(function() {
    $('#homologues_table').DataTable( {
        dom: 'Bfrtip',
        columnDefs: [
            {
                targets: 6,
                visible: true
            },
            {
                targets: 8,
                visible: true
            },
            {
                targets: 9,
                visible: true
            }
        ],
        buttons: [
            {
                extend: 'colvis',
                columns: ':not(.noVis)',
            },
            {
                extend: 'excel',
                title: 'homologs'
            },
            {
                extend: 'csv',
                title: 'homologs'
            }

        ],
        "pageLength": 10
    } );
} );


$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();
});

{% if pfam_def|length > 0 %}
window.onload = function() {
  var ft2 = new FeatureViewer("{{translation}}",
    "#div_features",
    {
      showAxis: true,
      showSequence: false,
      brushActive: false,
      toolbar:false,
      bubbleHelp:true,
      zoomMax:1}
  );

  features = {{pfam_domains|safe}};

  for(fet in features) {
    let curr_fet = features[fet];
    ft2.addFeature(curr_fet);
  }
}
{% endif %}

$(document).ready(function() {
  $('#blast_nr_table').DataTable( {
    dom: 'lBfrtip',
    buttons: [
      {
        extend: 'colvis',
        columns: ':not(.noVis)',
      },
      {
        extend: 'excel',
        title: 'plast_refseq'
      },
      {
        extend: 'csv',
        title: 'plast_refseq'
      }
    ],
    "paging":   true,
    "ordering": true,
    "info":     false,
    } );
});


var genomic_region = {{genomic_region|safe}};
var genomic_region_rect = document.querySelector("#genomic_region_rect")
    .getBoundingClientRect();
var genomic_width = genomic_region_rect.right-genomic_region_rect.left;

createGenomicRegion(d3.select("#div_genomic_region"), genomic_width*.8, "genomic_region", [genomic_region],
    0, ["{{locus_tag}}"], {{window_size}});

document.querySelector("#download_svg_button").onclick = function() {
  var svg_elem = document.querySelector("#genomic_region");
  svgExport.downloadSvg(svg_elem, "{{locus_tag}}");
};

document.querySelector("#blast_protein_button").onclick = function() {
  let csrftoken = getCookie('csrftoken');
  $.redirect('/blast/',
             {'blast': 'blastp',
              'csrfmiddlewaretoken': csrftoken,
              'target': "all",
              'max_number_of_hits': 10,
              'blast_input': "{{translation}}" },
             'POST',
             '_blank');
};

</script>

{% include "chlamdb/style_menu.html" %}

</html>
