<?xml version="1.0"?>

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format"
 xmlns:cis="http://zlab.bu.edu/schema/cisml"
 xmlns:svg="http://www.w3.org/2000/svg"
>

<xsl:output method="xml" indent="yes"/>

<!-- A stylesheet to generate an svg graphic of binding sites for one sequence -->

<xsl:key name="seqs" match="cis:scanned-sequence" use="@accession" />

<xsl:template match="/">
	<fo:root>
		<fo:layout-master-set>
			<fo:simple-page-master 
            master-name="the-only-page-type"
            page-height="11in" page-width="8.5in"
            margin-top="0.5in" margin-bottom="0.5in"
            margin-left="0.5in" margin-right="0.5in">
			<fo:region-body/>
			</fo:simple-page-master>
		</fo:layout-master-set>
		
		<fo:page-sequence master-reference="the-only-page-type">
			<fo:flow flow-name="xsl-region-body">
			<fo:block>Sequence Analysis by <xsl:value-of select="//cis:program-name"/></fo:block>
    		<xsl:choose>
				<xsl:when  test="//cis:multi-pattern-scan">
					<!-- Loop through all multi-pattern-scans -->
					<xsl:for-each select="//cis:multi-pattern-scan">
						<xsl:sort select="@pvalue" data-type="number"/>	
						<fo:block>Multi-Pattern Scan #<xsl:value-of select="position()"/></fo:block>
						<fo:block>P-Value: <xsl:value-of select="@pvalue"/></fo:block>
						<!-- Loop through all unique sequences -->
						<xsl:for-each-group select="./cis:pattern/cis:scanned-sequence" group-by="@accession">
							<xsl:call-template name="seq2svg"/>
						</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">
							<xsl:call-template name="seq2svg"/>




					</xsl:for-each-group>
				</xsl:otherwise>
			</xsl:choose>
			</fo:flow>
		</fo:page-sequence>
	</fo:root>
</xsl:template>


<xsl:template name="seq2svg">
	
	<xsl:variable name="length" select="@length"/>
	<xsl:variable name="v-spacing" select="20"/>
	<xsl:variable name="l-margin" select="60"/>
	<xsl:variable name="r-margin" select="100"/>
	<xsl:variable name="header-size" select="80"/>
	<xsl:variable name="seq-accession" select="@accession"/>
	<xsl:variable name="height" select="(count(//cis:scanned-sequence[@accession=$seq-accession]) * (2 * $v-spacing)) + $header-size"/>
	<xsl:variable name="width"  select="$r-margin +  $l-margin + $length"/>
	<xsl:variable name="line-y" select="(($height - $header-size) div 2) + $header-size"/>
	<xsl:variable name="height-in" select="($height div $width) * 7.5"/>
	<!-- Start svg -->
	<fo:block text-align="center" space-before="0.25in" space-after="0.25in">
	<fo:instream-foreign-object width="7.5in" height="{$height-in}in">   			
	<svg:svg width="7.5in" height="{$height-in}in" viewBox="0 0 {$width} {$height}">
		<svg:title>Promoter Analysis by ROVER</svg:title>
		<svg:desc>Binding Sites</svg:desc>
		<svg:defs>
  			<svg:style type="text/css"><![CDATA[
  				text {
				fill: black;
				font-size: 10pt;
				font-family: helvetica, sans-serif;
				font-weight: bold;
				}
				line {
				stroke-width: 2;
				}
   			]]></svg:style>
  		</svg:defs>
			
		<svg:text x="5" y="20">Sequence #<xsl:value-of select="position()"/></svg:text>
		<svg:text x="5" y="40">Accession: <xsl:value-of select="@accession"/> Name: <xsl:value-of select="@name"/></svg:text>
		<svg:line x1="{$l-margin}" y1="{$line-y}" x2="{$length + $l-margin}" y2="{$line-y}" style="stroke: black;"/>
		<!-- Loop through all hits for sequences that have the current accession -->
		<xsl:for-each select="current-group()">
			<xsl:sort select="../cis:pattern/@pvalue"/>
			<xsl:variable name="v-offset" select="$v-spacing * position()"/>
				<svg:text x="5" y="{$line-y - $v-offset}"><xsl:value-of select="../@accession"/></svg:text>
				<svg:text x="5" y="{$line-y + $v-offset}"><xsl:value-of select="../@accession"/></svg:text>
				<svg:text x="{$l-margin + $length + 10}" y="{$line-y - $v-offset}"><xsl:value-of select="../@name"/></svg:text>
				<svg:text x="{$l-margin + $length + 10}" y="{$line-y + $v-offset}"><xsl:value-of select="../@name"/></svg:text>
			<xsl:for-each select="./cis:matched-element">
				<xsl:choose>
					<xsl:when test="number(@start) &gt; number(@stop)">
						<svg:line x1="{$l-margin + @start}" y1="{$line-y - $v-offset}" x2="{$l-margin + @stop}" y2="{$line-y - $v-offset}" style="stroke: red;"/>
					</xsl:when>
					<xsl:otherwise>
						<svg:line x1="{$l-margin + @stop}" y1="{$line-y + $v-offset}" x2="{$l-margin + @start}" y2="{$line-y + $v-offset}" style="stroke: red;"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:for-each>
	</svg:svg>
	</fo:instream-foreign-object> 
	</fo:block>

</xsl:template>

</xsl:stylesheet>















