<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
This XSLT stylesheet can be used to transform NCBI BLAST XML ouput
to HTML while adding internal and external links.

Copyright (C) 2004  Ralf Jost
mailto:ralf.jost@justsoft.de

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <!-- Stylesheet parameters for link generation. -->
  <xsl:param name="Db" />
  <xsl:param name="Method" />

  <!-- Only create external links if both parameters are set -->
  <xsl:variable name="createExternalLinks">
    <xsl:choose>
      <xsl:when test="$Db and $Method">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!-- Prepare the external link template. -->
  <xsl:variable name="linkTemplate" select="concat('link?Method=',$Method,'&amp;Db=',$Db,'&amp;Id=')"/>

  <!-- Prepare the funcat extension to the external link template. -->
  <xsl:variable name="funcatExtension" select="'&amp;Description='"/>

  <!-- Template matching the root node. -->
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Template matching /BlastOutput. -->
  <xsl:template match="BlastOutput">

    <ul style="list-style-type:none">
      
      <!-- <li>program = <xsl:value-of select="BlastOutput_program"/></li> -->

      <li><b><xsl:value-of select="BlastOutput_version"/></b></li>

      <br/>

      <li>
        <b>Reference:</b>
        <xsl:call-template name="breakString">
          <xsl:with-param name="theString" select="substring-after(BlastOutput_reference,' ')"/>
          <xsl:with-param name="theDelimiter" select="'~'"/>
        </xsl:call-template>
      </li>

      <br/>

      <li>
        <table border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td><b>Query:</b></td>
            <td align="right">
              <p style="padding-left:10">
                <xsl:value-of select="BlastOutput_query-def"/>
              </p>
            </td>
          </tr>
          <!--
          <tr>
            <td>
              <p style="padding-left:10">
                QueryID:
              </p>
            </td>
            <td align="right">
              <p style="padding-left:10">
                <xsl:value-of select="BlastOutput_query-ID"/>
              </p>
            </td>
          </tr>
          -->
          <tr>
            <td>
              <p style="padding-left:10">
                Length:
              </p>
            </td>
            <td align="right">
              <p style="padding-left:10">
                <xsl:value-of select="BlastOutput_query-len"/>
              </p>
            </td>
          </tr>
          <xsl:if test="BlastOutput_query-seq">
            <tr>
              <td>
                <p style="padding-left:10">
                  Sequence:
                </p>
              </td>
              <td align="right">
                <p style="padding-left:10">
                  <xsl:value-of select="BlastOutput_query-seq"/>
                </p>
              </td>
            </tr>
          </xsl:if>
        </table>
      </li>

      <br/>

      <li><xsl:apply-templates select="BlastOutput_iterations"/></li>

      <li>
        <table border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>
              <b><xsl:value-of select="BlastOutput_program"/> paramters:</b>
            </td>
            <td><p style="padding-left:10"></p></td>
          </tr>
          <xsl:apply-templates select="BlastOutput_param"/>
        </table>
      </li>

    </ul>

  </xsl:template>

  <!-- Template matching /BlastOutput/BlastOutput_param/Parameters. -->
  <xsl:template match="Parameters">

      <xsl:if test="Parameters_matrix">
        <tr>
          <td><p style="padding-left:10">Matrix:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_matrix"/></p></td>
        </tr>
      </xsl:if>

      <tr>
        <td><p style="padding-left:10">E-value:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_expect"/></p></td>
      </tr>

      <xsl:if test="Parameters_include">
        <tr>
          <td><p style="padding-left:10">Include:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_include"/></p></td>
        </tr>
      </xsl:if>
      
      <xsl:if test="Parameters_sc-match">
        <tr>
          <td><p style="padding-left:10">SC-match:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_sc-match"/></p></td>
        </tr>
      </xsl:if>

      <xsl:if test="Parameters_sc-mismatch">
        <tr>
          <td><p style="padding-left:10">SC-mismatch:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_sc-mismatch"/></p></td>
        </tr>
      </xsl:if>
        
      <tr>
        <td><p style="padding-left:10">Gap-open:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_gap-open"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Gap-extend:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_gap-extend"/></p></td>
      </tr>
      
      <xsl:if test="Parameters_filter">
        <tr>
          <td><p style="padding-left:10">Filter:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_filter"/></p></td>
        </tr>
      </xsl:if>
      
      <xsl:if test="Parameters_pattern">
        <tr>
          <td><p style="padding-left:10">Pattern:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_pattern"/></p></td>
        </tr>
      </xsl:if>
      
      <xsl:if test="Parameters_entrez-query">
        <tr>
          <td><p style="padding-left:10">Entrez-query:</p></td>
          <td align="right"><p style="padding-left:10"><xsl:value-of select="Parameters_entrez-query"/></p></td>
        </tr>
      </xsl:if>

  </xsl:template>

  <!-- Template matching /BlastOutput/BlastOutput_iterations/Iteration. -->
  <xsl:template match="Iteration">

    <li>
      <table border="0" cellpadding="0" cellspacing="0">
        
        <tr>
          <td><b>Database:</b></td>
          <td align="right">
            <p style="padding-left:10">
              <xsl:call-template name="lastStringToken">
                <xsl:with-param name="theString" select="../../BlastOutput_db"/>
                <xsl:with-param name="theDelimiter" select="'/'"/>
              </xsl:call-template>
            </p>
          </td>
        </tr>
        
        <xsl:if test="Iteration_stat">
          <tr>
            <td>
              <p style="padding-left:10">
                Number of sequences:
              </p>
            </td>
            <td align="right">
              <p style="padding-left:10">
                <xsl:value-of select="Iteration_stat/Statistics/Statistics_db-num"/>
              </p>
            </td>
          </tr>
          <tr>
            <td>
              <p style="padding-left:10">
                Total number of letters:
              </p>
            </td>
            <td align="right">
              <p style="padding-left:10">
                <xsl:value-of select="Iteration_stat/Statistics/Statistics_db-len"/>
              </p>
            </td>
          </tr>
        </xsl:if>

      </table>
    </li>

    <br/>

    <xsl:if test="Iteration_message">
      <h2><xsl:value-of select="Iteration_message"/></h2>
      <br/>
    </xsl:if>
    
    <xsl:if test="Iteration_hits">
        
      <!-- Count the hits per iteration. -->
      <xsl:variable name="hitcount" select="count(//Hit)"/>
        
      <xsl:if test="$hitcount &gt; 0">
          
        <table border="0" cellspacing="3" cellpadding="0">
          <tr align="center">
            <th colspan="2"><p style="padding:5"><u>Sequences producing significant alignments:</u></p></th>
            <th><p style="padding:5"><u>Score (bits):</u></p></th>
            <th><p style="padding:5"><u>E-Value:</u></p></th>
          </tr>
          
          <!-- Iterate over all hits. -->
          <xsl:for-each select="Iteration_hits/Hit">
            
            <!-- Extract the hit id from Hit_def, since Id seems to be artifical. -->
            <xsl:variable name="hitid">
              <xsl:value-of select="substring-before(Hit_def,' ')"/>
            </xsl:variable>

            <!-- Strip hit id from Hit_def, since link should only work on hit id. -->
            <xsl:variable name="hitdef_no_id">
              <xsl:value-of select="substring-after(Hit_def,' ')"/>
            </xsl:variable>

            <!-- Prepare the hit description. -->
            <xsl:variable name="hitdef">
              <xsl:choose>
                <xsl:when test="string-length($hitdef_no_id) &gt; 64">
                  <xsl:value-of select="substring($hitdef_no_id,1,64)"/> ...
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$hitdef_no_id"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>

            <tr>
              <td align="left">
                <xsl:choose>
                  <xsl:when test="$createExternalLinks = 1">
                    <xsl:choose>
                      <xsl:when test="$Method = 'funcat'">
                        <a href="{concat($linkTemplate,$hitid,$funcatExtension,Hit_def)}">
                          <xsl:value-of select="$hitid"/>
                        </a>
                      </xsl:when>
                      <xsl:otherwise>
                        <a href="{concat($linkTemplate,$hitid)}">
                          <xsl:value-of select="$hitid"/>
                        </a>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$hitid"/>
                  </xsl:otherwise>
                </xsl:choose>
              </td>
              <td align="left">
                <xsl:value-of select="$hitdef"/>
              </td>
              <td align="right">
                <p style="padding-left:5;padding-right:5">
                  <a href="{concat('#iter-',../../Iteration_iter-num,'_hit-',Hit_num,'_hsp-',Hit_hsps/Hsp[position()=1]/Hsp_num)}">
                    <xsl:value-of select="Hit_hsps/Hsp[position()=1]/Hsp_bit-score"/>
                  </a>
                </p>
              </td>
              <td align="right">
                <p style="padding-left:5;padding-right:5">
                  <xsl:value-of select="Hit_hsps/Hsp[position()=1]/Hsp_evalue"/>
                </p>
              </td>
            </tr>
          </xsl:for-each>
        </table>

        <br/>

        <xsl:apply-templates select="Iteration_hits"/>
          
      </xsl:if>
    </xsl:if>

    <xsl:if test="Iteration_stat">
      <xsl:apply-templates select="Iteration_stat"/>
    </xsl:if>
    
  </xsl:template>

  <!-- Template matching /BlastOutput/BlastOutput_iterations/Iteration/Statistics. -->
  <xsl:template match="Statistics">

    <table border="0" cellspacing="0" cellpadding="0">

      <tr>
        <td><b>Statistics for iteration:</b></td>
        <td align="right">
          <p style="padding-left:10">
            <xsl:value-of select="../../Iteration_iter-num"/>
          </p>
        </td>
      </tr>

      <tr>
        <td><p style="padding-left:10">Database:</p></td>
        <td align="right">
          <p style="padding-left:10">
            <xsl:call-template name="lastStringToken">
              <xsl:with-param name="theString" select="/BlastOutput/BlastOutput_db"/>
              <xsl:with-param name="theDelimiter" select="'/'"/>
            </xsl:call-template>
          </p>
        </td>
      </tr>

      <tr>
        <td><p style="padding-left:10">Number of sequences:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_db-num"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Total number of letters:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_db-len"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Effective search space:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_eff-space"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Effective HSP length:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_hsp-len"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Kappa:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_kappa"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Lambda:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_lambda"/></p></td>
      </tr>
      
      <tr>
        <td><p style="padding-left:10">Entropy:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Statistics_entropy"/></p></td>
      </tr>
      
    </table>

    <br/>
    
  </xsl:template>

  <!-- Template matching /BlastOutput/BlastOutput_iterations/Iteration/Iteration_hits/Hit. -->
  <xsl:template match="Hit">

    <!-- Extract the hit id from Hit_def, since Id seems to be artifical. -->
    <xsl:variable name="hitid">
      <xsl:value-of select="substring-before(Hit_def,' ')"/>
    </xsl:variable>

    <!-- Strip hit id from Hit_def, since link should only work on hit id. -->
    <xsl:variable name="hitdef">
      <xsl:value-of select="substring-after(Hit_def,' ')"/>
    </xsl:variable>

    <a name="{concat('iter-',../../Iteration_iter-num,'_hit-',Hit_num,'_hsp-',Hit_hsps/Hsp[position()=1]/Hsp_num)}"></a>

    <table border="0" cellpadding="3" cellspacing="0">
      <tr>
        <td valign="top">
          <b>
            <xsl:choose>
              <xsl:when test="$createExternalLinks = 1">
                <xsl:choose>
                  <xsl:when test="$Method = 'funcat'">
                    &gt;<a href="{concat($linkTemplate,$hitid,$funcatExtension,Hit_def)}">
                      <xsl:value-of select="$hitid"/>
                    </a>
                  </xsl:when>
                  <xsl:otherwise>
                    &gt;<a href="{concat($linkTemplate,$hitid)}">
                      <xsl:value-of select="$hitid"/>
                    </a>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                &gt;<xsl:value-of select="$hitid"/>
              </xsl:otherwise>
            </xsl:choose>
          </b>
        </td>
        <td valign="top">
          <b>
            <xsl:value-of select="$hitdef"/>
          </b>
        </td>
      </tr>
    </table>

    <table border="0" cellspacing="0" cellpadding="0">

      <tr>
        <td><p style="padding-left:10">Length:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Hit_len"/></p></td>
      </tr>

      <!--
      <tr>
        <td><p style="padding-left:10">ID:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Hit_id"/></p></td>
      </tr>

      <tr>
        <td><p style="padding-left:10">Accession:</p></td>
        <td align="right"><p style="padding-left:10"><xsl:value-of select="Hit_accession"/></p></td>
      </tr>
      -->

    </table>

    <br/>

    <xsl:if test="Hit_hsps">
      <xsl:apply-templates select="Hit_hsps"/>
    </xsl:if>

    <br/>

  </xsl:template>

  <!-- Template matching /BlastOutput/BlastOutput_iterations/Iteration/Iteration_hits/Hit/Hit_hsps/Hsp. -->
  <xsl:template match="Hsp">

    <!-- Determine hsp direction in query. -->
    <xsl:variable name="qdir">
      <xsl:choose>
        <xsl:when test="number(Hsp_query-to) &lt; number(Hsp_query-from)">-1</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- Determine hsp direction in hit. -->
    <xsl:variable name="hdir">
      <xsl:choose>
        <xsl:when test="number(Hsp_hit-to) &lt; number(Hsp_hit-from)">-1</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <table border="0" cellspacing="0" cellpadding="0">

      <tr>
        <td>Score = <xsl:value-of select="Hsp_bit-score"/> bits (<xsl:value-of select="round(Hsp_score)"/>),</td>
        <td><p style="padding-left:10">Expect = <xsl:value-of select="Hsp_evalue"/>,</p></td>
        <td><p style="padding-left:10"></p></td>
      </tr>

      <tr>
        <td>
          <xsl:if test="Hsp_identity and Hsp_align-len">
            Identities = <xsl:value-of select="Hsp_identity"/>/<xsl:value-of select="Hsp_align-len"/> (<xsl:value-of select="round(Hsp_identity div Hsp_align-len * 100)"/>%),
          </xsl:if>
        </td>
        <td>
          <xsl:if test="/BlastOutput/BlastOutput_program != 'blastn'">
            <xsl:if test="Hsp_positive and Hsp_align-len">
              <p style="padding-left:10">
                Positives = <xsl:value-of select="Hsp_positive"/>/<xsl:value-of select="Hsp_align-len"/> (<xsl:value-of select="round(Hsp_positive div Hsp_align-len * 100)"/>%),
              </p>
            </xsl:if>
          </xsl:if>
        </td>
        <td>
          <xsl:if test="Hsp_gaps and Hsp_align-len">
            <p style="padding-left:10">
              Gaps = <xsl:value-of select="Hsp_gaps"/>/<xsl:value-of select="Hsp_align-len"/> (<xsl:value-of select="round(Hsp_gaps div Hsp_align-len * 100)"/>%),
            </p>
          </xsl:if>
        </td>
      </tr>

      <tr>
        <td>
          <xsl:if test="Hsp_query-frame or Hsp_hit-frame">
            <xsl:choose>
              <xsl:when test="/BlastOutput/BlastOutput_program = 'blastn'">
                Strand =
              </xsl:when>
              <xsl:otherwise>
                Frame =
              </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="Hsp_query-frame">
              <xsl:value-of select="Hsp_query-frame"/>
            </xsl:if>
            <xsl:if test="Hsp_query-frame and Hsp_hit-frame">
              /
            </xsl:if>
            <xsl:if test="Hsp_hit-frame">
              <xsl:value-of select="Hsp_hit-frame"/>
            </xsl:if>
            ,
          </xsl:if>
        </td>
        <td>
          <xsl:if test="Hsp_align-len">
            <p style="padding-left:10">
              Alignment length = <xsl:value-of select="Hsp_align-len"/>,
            </p>
          </xsl:if>
        </td>
        <td>
          <xsl:if test="Hsp_density">
            <p style="padding-left:10">
              Density = <xsl:value-of select="Hsp_density"/>
            </p>
          </xsl:if>
        </td>
      </tr>

      <tr>
        <td>
          <xsl:if test="Hsp_pattern-from">
            <p style="padding-left:10">
              Pattern from = <xsl:value-of select="Hsp_pattern-from"/>
            </p>
          </xsl:if>
        </td>
        <td>
          <xsl:if test="Hsp_pattern-to">
            <p style="padding-left:10">
              Pattern to = <xsl:value-of select="Hsp_pattern-to"/>
            </p>
          </xsl:if>
        </td>
    </tr>

    </table>

    <p style="font-family:monospace;white-space:nowrap;">
      <table border="0">
        <xsl:call-template name="formatAlignment">
          <xsl:with-param name="query" select="Hsp_qseq"/>
          <xsl:with-param name="hit" select="Hsp_hseq"/>
          <xsl:with-param name="alignment" select="Hsp_midline"/>
          <xsl:with-param name="query-from" select="Hsp_query-from "/>
          <xsl:with-param name="query-to" select="Hsp_query-to"/>
          <xsl:with-param name="query-dir" select="$qdir"/>
          <xsl:with-param name="hit-from" select="Hsp_hit-from"/>
          <xsl:with-param name="hit-to" select="Hsp_hit-to"/>
          <xsl:with-param name="hit-dir" select="$hdir"/>
          <xsl:with-param name="width" select="60"/>
        </xsl:call-template>
      </table>
    </p>

    <br/>

  </xsl:template>

  <!-- Template to format pairwise alignments. -->
  <xsl:template name="formatAlignment">

    <xsl:param name="query"/>
    <xsl:param name="hit"/>
    <xsl:param name="alignment"/>
    <xsl:param name="query-from"/>
    <xsl:param name="query-to"/>
    <xsl:param name="query-dir"/>
    <xsl:param name="hit-from"/>
    <xsl:param name="hit-to"/>
    <xsl:param name="hit-dir"/>
    <xsl:param name="width"/>

    <xsl:choose>
      <xsl:when test="string-length($query) &gt; $width">
        <xsl:variable name="qseq" select="substring($query,1,$width)"/>
        <xsl:variable name="qseqgaps" select="string-length($qseq) - string-length(translate($qseq,'-',''))"/>
        <xsl:variable name="hseq" select="substring($hit,1,$width)"/>
        <xsl:variable name="hseqgaps" select="string-length($hseq) - string-length(translate($hseq,'-',''))"/>
        <tr>
          <td style="padding-left:5;padding-right:5">Query:</td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$query-from"/></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="$qseq"/></pre></td>
          <td align="right" style="padding-left:5;padding-right:5">
            <xsl:choose>
              <xsl:when test="$query-dir &lt; 0">
                <xsl:value-of select="$query-from - $width + 1 + $qseqgaps"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$query-from + $width - 1 - $qseqgaps"/>
              </xsl:otherwise>
            </xsl:choose>
          </td>
        </tr>
        <tr>
          <td style="padding-left:5;padding-right:5"></td>
          <td style="padding-left:5;padding-right:5"></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="substring($alignment,1,$width)"/></pre></td>
          <td style="padding-left:5;padding-right:5"></td>
        </tr>
        <tr>
          <td style="padding-left:5;padding-right:5">Sbjct:</td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$hit-from"/></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="$hseq"/></pre></td>
          <td align="right" style="padding-left:5;padding-right:5">
            <xsl:choose>
              <xsl:when test="$hit-dir &lt; 0">
                <xsl:value-of select="$hit-from - $width + 1 + $hseqgaps"/>                
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$hit-from + $width - 1 - $hseqgaps"/>
              </xsl:otherwise>
            </xsl:choose>
          </td>
        </tr>
        <tr>
          <td height="10" colspan="4"></td>
        </tr>
        <xsl:call-template name="formatAlignment">
          <xsl:with-param name="query" select="substring($query,61)"/>
          <xsl:with-param name="hit" select="substring($hit,61)"/>
          <xsl:with-param name="alignment" select="substring($alignment,61)"/>
          <xsl:with-param name="query-from">
            <xsl:choose>
              <xsl:when test="$query-dir &lt; 0">
                <xsl:value-of select="$query-from - $width + $qseqgaps"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$query-from + $width - $qseqgaps"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="query-to" select="$query-to"/>
          <xsl:with-param name="query-dir" select="$query-dir"/>
          <xsl:with-param name="hit-from">
            <xsl:choose>
              <xsl:when test="$hit-dir &lt; 0">
                <xsl:value-of select="$hit-from - $width + $hseqgaps"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$hit-from + $width - $hseqgaps"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="hit-to" select="$hit-to"/>
          <xsl:with-param name="hit-dir" select="$hit-dir"/>
          <xsl:with-param name="width" select="$width"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <tr>
          <td style="padding-left:5;padding-right:5">Query:</td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$query-from"/></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="$query"/></pre></td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$query-to"/></td>
        </tr>
        <tr>
          <td style="padding-left:5;padding-right:5"></td>
          <td style="padding-left:5;padding-right:5"></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="$alignment"/></pre></td>
          <td style="padding-left:5;padding-right:5"></td>
        </tr>
        <tr>
          <td style="padding-left:5;padding-right:5">Sbjct:</td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$hit-from"/></td>
          <td style="padding-left:5;padding-right:5"><pre style="margin:0;padding=0;"><xsl:value-of select="$hit"/></pre></td>
          <td align="right" style="padding-left:5;padding-right:5"><xsl:value-of select="$hit-to"/></td>
        </tr>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- A template to break a string with <br/> tags where
       a given string or char matches (default: newline). -->
  <xsl:template name="breakString">

    <!-- The string to break. -->
    <xsl:param name="theString"/>

    <!-- The string or char to break at. -->
    <xsl:param name="theDelimiter"/>

    <!-- Define the current delimiter. -->
    <xsl:variable name="delim">
      <xsl:choose>
        <!-- Use the theDelimiter parameter if given. -->
        <xsl:when test="$theDelimiter">
          <xsl:value-of select="$theDelimiter"/>
        </xsl:when>
        <!-- Default is newline. -->
        <xsl:otherwise>
          <xsl:value-of select="'&#xA;'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- Now break. -->
    <xsl:choose>
      <!-- Only break if theString contains delim. -->
      <xsl:when test="contains($theString,$delim)">
        <xsl:value-of select="substring-before($theString,$delim)"/>
        <br/>
        <!-- Recursivly apply this template to the rest of theString. -->
        <xsl:call-template name="breakString">
          <xsl:with-param name="theString" select="substring-after($theString,$delim)"/>
          <xsl:with-param name="theDelimiter" select="$delim"/>
        </xsl:call-template>
      </xsl:when>
      <!-- If theString doesn't contain delim simply return it. -->
      <xsl:otherwise>
        <xsl:value-of select="$theString"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- A template to replace a string with another string in a string. -->
  <xsl:template name="replaceString">
    <xsl:param name="theString"/>
    <xsl:param name="theTarget"/>
    <xsl:param name="theReplacement"/>
    <xsl:choose>
      <xsl:when test="contains($theString,$theTarget)">
        <xsl:value-of select="substring-before($theString,$theTarget)"/>
        <xsl:value-of select="$theReplacement"/>
        <xsl:call-template name="replaceString">
          <xsl:with-param name="theString" select="substring-after($theString,$theTarget)"/>
          <xsl:with-param name="theTarget" select="$theTarget"/>
          <xsl:with-param name="theReplacement" select="$theReplacement"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$theString"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- A template to extract the last part of a string for a given delimiter. -->
  <xsl:template name="lastStringToken">
    <xsl:param name="theString"/>
    <xsl:param name="theDelimiter"/>
    <xsl:choose>
      <xsl:when test="contains($theString,$theDelimiter)">
        <xsl:call-template name="lastStringToken">
          <xsl:with-param name="theString" select="substring-after($theString,$theDelimiter)"/>
          <xsl:with-param name="theDelimiter" select="$theDelimiter"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$theString"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

