<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:cis="http://zlab.bu.edu/schema/cisml"
>

<!-- A stylesheet to generate a text report about patterns found in each sequence in a group of sequences-->
<!-- Note: Use of XSLT 2.0 and scientific notation requires use of XSLT 2.0 compliant processor such as Saxon 7 -->

<xsl:output method="html" />

<xsl:template match="/">
<html>
<head>
	<title>Sequence Analysis by <xsl:value-of select="/cis:cis-element-search/cis:program-name"/></title>
	<link href="cisml.css" rel="stylesheet" type="text/css"></link>
</head> 
<body>
	<h1 align="center">Sequence Analysis with <xsl:value-of select="/cis:cis-element-search/cis:program-name"/></h1>

	<xsl:choose>
		<xsl:when test="//cis:multi-pattern-scan">
			<xsl:for-each select="//cis:multi-pattern-scan">
				<!-- Loop through each pattern group -->
				<xsl:sort select="@pvalue" data-type="number"/>


				<h2>Multi-pattern Scan #<xsl:value-of select="position()"/></h2>
				<h2>Group P-Value: <xsl:value-of select="@pvalue"/></h2>
				<xsl:for-each-group select="./cis:pattern/cis:scanned-sequence" group-by="@accession">
					<!-- Loop through each sequence scanned by this pattern group -->
					<xsl:sort select="@accession"/>
					<table>
						<tr><th colspan="4">Sequence #<xsl:value-of select="position()"/></th></tr>
						<tr><td colspan="4"><xsl:value-of select="@name"/></td></tr>
						<tr><th>Accession</th><td><xsl:value-of select="@accession"/></td><th>P-Value</th><td><xsl:value-of select="@pvalue"/></td></tr>
					</table>
					<xsl:for-each select="current-group()">
							<!-- Loop through each pattern used on a sequence scanned by this pattern group -->
						<xsl:sort select="../cis:pattern/@pvalue"/>
						<table>
						<tr><th colspan="5">Pattern #<xsl:value-of select="position()"/> Hits</th></tr>
						<tr><td colspan="5"><xsl:value-of select="../@name"/></td></tr>
						<tr><th colspan="2">Pattern Accession</th><th colspan="3">Pattern P-Value</th></tr>
						<tr><td colspan="2"><xsl:value-of select="../@accession"/></td><td colspan="3"><xsl:value-of select="../@pvalue"/></td></tr>

						<tr><th>Start</th><th>Stop</th><th>P-Value</th><th>Cluster Id</th><th>Sequence</th></tr>

						<xsl:for-each select="./cis:matched-element">
							<!-- Loop through each hit for this pattern -->
							<xsl:sort select="@start"/>
							<tr>
							<td><xsl:value-of select="@start"/></td>
							<td><xsl:value-of select="@stop"/></td>
							<td><xsl:value-of select="@pvalue"/></td>
							<td><xsl:value-of select="@clusterid"/></td>
							<td><xsl:value-of select="./cis:sequence"/></td>
							</tr>
						</xsl:for-each>
						</table>
					</xsl:for-each>
				</xsl:for-each-group>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<!-- Loop through all unique sequences -->
			<xsl:for-each-group select="//cis:scanned-sequence" group-by="@accession">
				<!-- Loop through each scanned sequence -->
				<xsl:sort select="@accession"/>
					<table>
						<tr><th colspan="4">Sequence #<xsl:value-of select="position()"/></th></tr>
						<tr><td colspan="4"><xsl:value-of select="@name"/></td></tr>
						<tr><th>Accession</th><td><xsl:value-of select="@accession"/></td><th>P-Value</th><td><xsl:value-of select="@pvalue"/></td></tr>
					</table>
				
				<!-- Loop through all hits for sequences that have the current accession -->
				<xsl:for-each select="current-group()">
					<xsl:sort select="../@pvalue" data-type="number"/>

					
					<table>
					<tr><th colspan="4">Pattern #<xsl:value-of select="position()"/> Hits</th></tr>
					<tr><td colspan="4"><xsl:value-of select="../@name"/></td></tr>
					<tr><th colspan="2">Pattern Accession</th><th colspan="2">Pattern P-Value</th></tr>
					<tr><td colspan="2"><xsl:value-of select="../@accession"/></td><td colspan="2"><xsl:value-of select="../@pvalue"/></td></tr>
					<tr><th>Start</th><th>Stop</th><th>P-value</th><th>Sequence</th></tr>
					<xsl:for-each select="./cis:matched-element">
						<!-- Loop through each hit on this sequence -->
						<xsl:sort select="@start" data-type="number"/>
						<tr><td><xsl:value-of select="@start"/></td><td><xsl:value-of select="@stop"/></td>
						<td><xsl:value-of select="@pvalue"/></td><td><xsl:value-of select="./cis:sequence"/></td></tr>
					</xsl:for-each>
					</table>
				</xsl:for-each>
			</xsl:for-each-group>
		</xsl:otherwise>
	</xsl:choose>

	<h3>Pattern File: <xsl:value-of select="//cis:pattern-file" /></h3>
	<h3>Sequences File: <xsl:value-of select="//cis:sequence-file" /></h3>
	<h3>Background Sequence File: <xsl:value-of select="//cis:background-seq-file" /></h3>
	<h3>Sequence Filtering: <xsl:value-of select="//cis:sequence-filtering/@on-off" /></h3>
	<h3>Sequence Pvalue Cutoff: <xsl:value-of select="//cis:sequence-pvalue-cutoff" /></h3>
	<h3>Site Pvalue Cutoff: <xsl:value-of select="//cis:site-pvalue-cutoff" /></h3>

	</body>
</html>

</xsl:template>

</xsl:stylesheet>









