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

<!--
This XSLT stylesheet can be used to transform InterProScan XML ouput
to HTML while adding internal and external links. It takes the following
parameters, all of which are link templates:
* source_link_template:    if given, a link for the submitted sequence will
                           be created [%id%]
* ip_extern_link_template: if given, links for InterPro hits will be created
                           (intended for external links) [%id]
* ip_intern_link_template: if given, links for InterPro hits will be created
                           (intended for internal links) [%id%]
* go_link_template:        if given, links for GO (GeneOnthology) annotations
                           will be created [%id%]
* other_dbs_link_template: if given, links for all matches will be created
                           [%db% %id%]
The special tokens %id% and %db% may be used as placeholders (s. description)
and will be recognized and replaced apropriately.

Important Note:
===============
The placeholder mechanism seems to work fine with Xalan-J and xsltproc, but
fails with Sablotron and Xalan-C, at least with my installations. Hope it
works for you.

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="source_link_template"/>
  <xsl:param name="ip_extern_link_template"/>
  <xsl:param name="ip_intern_link_template"/>
  <xsl:param name="go_link_template"/>
  <xsl:param name="other_dbs_link_template"/>

  <!-- Template matching the interpro_matches node. -->
  <xsl:template match="interpro_matches">
    <html>
      <head>
        <link rel="stylesheet" href="interpro2html.css" type="text/css"/>
        <title>InterProScan&#160;Results&#160;(<xsl:value-of select="protein/@id"/>)</title>
      </head>
      <body>
        <table border="0" align="center" width="60%" cellspacing="0" cellpadding="0">
          <tr>
            <td>
              <table border="0" cellspacing="0" cellpadding="3">
                <tr>
                  <td class="ips_title_0">
                    <h2>InterProScan</h2>
                  </td>
                  <td class="ips_title_1">
                    <h2>Results</h2>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td>
              <xsl:apply-templates select="protein"/>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>

  <!-- Template matching the protein node. -->
  <xsl:template match="protein">
    <table border="1" cellpadding="3" cellspacing="0" class="entry">
      <tr class="header">
        <td colspan="2">
          <b>SEQUENCE:</b>&#160;
          <xsl:choose>
            <xsl:when test="$source_link_template">
              <xsl:variable name="link">
                <xsl:call-template name="linkTemplate">
                  <xsl:with-param name="template" select="$source_link_template"/>
                  <xsl:with-param name="id" select="@id"/>
                </xsl:call-template>
              </xsl:variable>
              <a href="{$link}"><xsl:value-of select="@id"/></a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@id"/>
            </xsl:otherwise>
          </xsl:choose>
          &#160;
          <b>CRC64:</b>&#160;<xsl:value-of select="@crc64"/>&#160;
          <b>LENGTH:</b>&#160;<xsl:value-of select="@length"/>&#160;aa
        </td>
      </tr>
      <tr class="seperator"><td colspan="2"></td></tr>
      <xsl:apply-templates select="interpro"/>
    </table>
  </xsl:template>

  <!-- Template matching interpro nodes. -->
  <xsl:template match="interpro">
    <tr>
      <td valign="top">

        <b>
          <xsl:choose>
            <xsl:when test="$ip_extern_link_template and (@type != 'unintegrated')">
              <xsl:variable name="link">
                <xsl:call-template name="linkTemplate">
                  <xsl:with-param name="template" select="$ip_extern_link_template"/>
                  <xsl:with-param name="id" select="@id"/>
                </xsl:call-template>
              </xsl:variable>
              <a href="{$link}">InterPro</a>
            </xsl:when>
            <xsl:otherwise>
              InterPro
            </xsl:otherwise>
          </xsl:choose>
        </b><br/>

        <xsl:if test="@type != 'unintegrated'">
          <xsl:choose>
            <xsl:when test="$ip_intern_link_template">
              <xsl:variable name="link">
                <xsl:call-template name="linkTemplate">
                  <xsl:with-param name="template" select="$ip_intern_link_template"/>
                  <xsl:with-param name="id" select="@id"/>
                </xsl:call-template>
              </xsl:variable>
              <a href="{$link}"><xsl:value-of select="@id"/></a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@id"/>
            </xsl:otherwise>
          </xsl:choose>
          <br/>
        </xsl:if>
        <xsl:value-of select="@type"/>
      </td>
      <td>
        <table border="0" align="left" cellpadding="3" cellspacing="0" width="100%">
          <tr>
            <td colspan="4">
              <b><xsl:value-of select="@name"/></b>
            </td>
          </tr>
          <xsl:apply-templates select="match"/>
        </table>
      </td>
    </tr>

    <xsl:if test="@parent_id">
      <tr>
        <td valign="top">
          <b>Parent</b>
        </td>
        <td>
          <xsl:choose>
            <xsl:when test="$ip_intern_link_template">
              <xsl:variable name="link">
                <xsl:call-template name="linkTemplate">
                  <xsl:with-param name="template" select="$ip_intern_link_template"/>
                  <xsl:with-param name="id" select="@parent_id"/>
                </xsl:call-template>
              </xsl:variable>
              <a href="{$link}"><xsl:value-of select="@parent_id"/></a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@parent_id"/>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </tr>
    </xsl:if>

    <xsl:apply-templates select="child_list"/>
    <xsl:apply-templates select="found_in"/>
    <xsl:apply-templates select="contains"/>

    <xsl:if test="count(classification[@class_type='GO']) &gt; 0">
      <tr>
        <td valign="top">
          <b>GO terms</b>
        </td>
        <td>
          <xsl:apply-templates select="classification[@class_type='GO']"/>
        </td>
      </tr>
    </xsl:if>

    <tr class="seperator"><td colspan="2"></td></tr>

  </xsl:template>

  <!-- Template matching match nodes. -->
  <xsl:template match="match">
    <tr class="match">
      <td><xsl:value-of select="@dbname"/></td>
      <td>
        <xsl:choose>
          <xsl:when test="$other_dbs_link_template">
            <xsl:variable name="link">
              <xsl:call-template name="linkTemplate">
                <xsl:with-param name="template" select="$other_dbs_link_template"/>
                <xsl:with-param name="id" select="@id"/>
                <xsl:with-param name="db" select="@dbname"/>
              </xsl:call-template>
            </xsl:variable>
            <a href="{$link}"><xsl:value-of select="@id"/></a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@id"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <td><i><xsl:value-of select="@name"/></i></td>
      <td>
        <xsl:apply-templates select="location"/>
      </td>
    </tr>
  </xsl:template>

  <!-- Template matching location nodes. -->
  <xsl:template match="location">
    <xsl:value-of select="@score"/>&#160;[<xsl:value-of select="@start"/>-<xsl:value-of select="@end"/>]<xsl:value-of select="@status"/>&#160;
  </xsl:template>

  <!-- Template matching child_list nodes. -->
  <xsl:template match="child_list">
    <xsl:if test="count(rel_ref[@ipr_ref]) &gt; 0">
      <tr>
        <td valign="top">
          <b>Children</b>
        </td>
        <td>
          <xsl:apply-templates select="rel_ref[@ipr_ref]"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Template matching found_in nodes. -->
  <xsl:template match="found_in">
    <xsl:if test="count(rel_ref[@ipr_ref]) &gt; 0">
      <tr>
        <td valign="top">
          <b>Found in</b>
        </td>
        <td>
          <xsl:apply-templates select="rel_ref[@ipr_ref]"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Template matching contains nodes. -->
  <xsl:template match="contains">
    <xsl:if test="count(rel_ref[@ipr_ref]) &gt; 0">
      <tr>
        <td valign="top">
          <b>Contains</b>
        </td>
        <td>
          <xsl:apply-templates select="rel_ref[@ipr_ref]"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Template matching GO classification nodes. -->
  <xsl:template match="classification[@class_type='GO']">
    <xsl:value-of select="category"/>:&#160;
    <xsl:value-of select="description"/>&#160;
    <xsl:choose>
      <xsl:when test="$go_link_template">
        <xsl:variable name="link">
          <xsl:call-template name="linkTemplate">
            <xsl:with-param name="template" select="$go_link_template"/>
            <xsl:with-param name="id" select="@id"/>
          </xsl:call-template>
        </xsl:variable>
        <a href="{$link}"><xsl:value-of select="@id"/></a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@id"/>
      </xsl:otherwise>
    </xsl:choose>
    <br/>
  </xsl:template>

  <!-- Template matching rel_ref nodes with ipr_ref attribute. -->
  <xsl:template match="rel_ref[@ipr_ref]">
    <xsl:choose>
      <xsl:when test="$ip_intern_link_template">
        <xsl:variable name="link">
          <xsl:call-template name="linkTemplate">
            <xsl:with-param name="template" select="$ip_intern_link_template"/>
            <xsl:with-param name="id" select="@ipr_ref"/>
          </xsl:call-template>
        </xsl:variable>
        <a href="{$link}"><xsl:value-of select="@ipr_ref"/></a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@ipr_ref"/>
      </xsl:otherwise>
    </xsl:choose>
    &#160;
  </xsl:template>

  <!-- A template to create links from a template by replacing
       %id% and %db% tags with appropriate values. -->
  <xsl:template name="linkTemplate">
    <xsl:param name="template"/>
    <xsl:param name="id"/>
    <xsl:param name="db"/>

    <xsl:variable name="tmp">
      <xsl:choose>
        <xsl:when test="contains($template,'%id%')">
          <xsl:call-template name="stringReplace">
            <xsl:with-param name="theString" select="$template"/>
            <xsl:with-param name="theTarget" select="'%id%'"/>
            <xsl:with-param name="theValue"  select="$id"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$template"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="contains($tmp,'%db%')">
        <xsl:call-template name="stringReplace">
          <xsl:with-param name="theString" select="$tmp"/>
          <xsl:with-param name="theTarget" select="'%db%'"/>
          <xsl:with-param name="theValue"  select="$db"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tmp"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <!-- A template for string replacements. -->
  <xsl:template name="stringReplace">
    <xsl:param name="theString"/>
    <xsl:param name="theTarget"/>
    <xsl:param name="theValue"/>
    <xsl:choose>
      <xsl:when test="contains($theString,$theTarget)">
        <xsl:value-of select="substring-before($theString,$theTarget)"/>
        <xsl:value-of select="$theValue"/>
        <xsl:call-template name="stringReplace">
          <xsl:with-param name="theString" select="substring-after($theString,$theTarget)"/>
          <xsl:with-param name="theTarget" select="$theTarget"/>
          <xsl:with-param name="theValue" select="$theValue"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$theString"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

