<?xml version="1.0" encoding="UTF-8"?>
<maxdQL version="0.1" name="SearchThenEnumerateAndFormatHTML" >
	
	<description>This takes some search term to match some gene names, and then retrieves the intensities of these gene for a specified Measurement. Formats as html.</description>
	
	<unitTest>
		php run.php -file=../../sequences/listATableWithAttributesPHPed.xml -display=last -profile="The mouse microarray experiment" -TableName=Measurement 
	</unitTest>
			
	
	<xfunction id="getGeneIds" comment="take a list of genes and make a geneids tag from their ids"  >
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="text" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"  media-type="text/html" />
				<xsl:strip-space elements="entries Gene"/>
				<xsl:template match="/" >
					<xsl:for-each select="entries/search/Gene"><xsl:value-of select="@ID"/>,</xsl:for-each>
				</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	
	<xfunction id="formatSearchResult" comment="does some groovy tricks"   >
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="xml" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"  media-type="text/html" />
				<xsl:strip-space elements="entries Gene"/>
				<xsl:template match="/" >
				<ul>
					<xsl:for-each select="entries/search/Gene">
						<li>
							
							<xsl:value-of select="@Name"/>
						</li>
					</xsl:for-each>
				</ul>
				</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	<pfunction id="formatEnumerateResult" comment="does some groovy number tricks" method="xml" >
		<![CDATA[
			// create a new dom
			$domNew = new DOMDocument();	
			$root = $domNew->createElement("result");
			$rootDom = $domNew->appendChild($root);
			
			
			
			foreach ($input->firstChild->childNodes as $enumerate)
			{
				
				if ($enumerate->nodeType == 1)
				{
					
					$enumerateName = $enumerate->getAttribute("Name");
				
					$h3 = $domNew->createElement("h3");
					$h3Dom = $rootDom->appendChild($h3);
					$text = $domNew->createTextNode($enumerateName);
					$text = $h3Dom->appendChild($text);
		
					$table = $domNew->createElement("table");
					$tableDom = $rootDom->appendChild($table);
					
					$columnsDone = false;
					
					foreach ($enumerate->childNodes as $enumeration)
					{
						//print "\t" . $enumeration->nodeName;
		
						
		
						if ($enumeration->nodeType == 1)
						{
							
							if ($columnsDone != true)
							{
							
								$tr = $domNew->createElement("tr");
								$trDom = $tableDom->appendChild($tr);
							
								foreach ($enumeration->childNodes as $attribute)
								{
									if  ($attribute->nodeType == 1) 
									{
										$attributeName = $attribute->getAttribute("Name");
										//print $attributeName . "\t";
										
										$td = $domNew->createElement("td");
										$tdDom = $trDom->appendChild($td);
										
										$text = $domNew->createTextNode($attributeName);
										$text = $tdDom->appendChild($text);
									}
									
								}
								$columnsDone = true;
							}
							//print "\n";
							
							$tr = $domNew->createElement("tr");
							$trDom = $tableDom->appendChild($tr);
		
							foreach ($enumeration->childNodes as $attribute)
							{
								if ($attribute->nodeType == 1)
								{
									//print $attribute->nodeValue . "\t";
									$attributeValue = $attribute->nodeValue;
									
									$td = $domNew->createElement("td");
									$tdDom = $trDom->appendChild($td);
									
									$text = $domNew->createTextNode($attributeValue);
									$text = $tdDom->appendChild($text);
								}
							}
						}
					}
					
				}
				//print "\n\n";
			}
			
			$output = $domNew;
			
			//die();
		]]>
	</pfunction>
	
	<xfunction id="formatEnumerateResult" comment="does some groovy number tricks"   >
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="xml" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"  media-type="text/html" />
				<xsl:strip-space elements="entries Gene"/>
				<xsl:template match="/" >
					<table>	
						<xsl:for-each select="entries/enumerate">
							<tr>
								<th>Measurement Name</th>
								<xsl:for-each select="enumeration[1]/attribute">
									<th>
										<xsl:value-of select="@Name"/>
									</th>
								</xsl:for-each>
							</tr>
							<xsl:variable name="name" select="@Name"/>
							
							<tr>
								<xsl:value-of select="enumeration"/>
							</tr>
							
							<tr>
								<xsl:for-each select=".">
									<dd><xsl:value-of select="."/></dd>
								</xsl:for-each>
							</tr>
							
							<!--
							<xsl:for-each select="/enumeration/">
								<tr>
									<th>
										<xsl:value-of select="$name" />
									</th>
									<xsl:for-each select="attribute">
											<td>
												<xsl:value-of select="."/>
											</td>
										
									</xsl:for-each>
								</tr>
							</xsl:for-each>
							-->
							<tr>
								<th style="background-color:black;"  >--</th>
								<xsl:for-each select="enumeration[1]/attribute">
									<th style="background-color:black;" >--</th>	
								</xsl:for-each>
							</tr>
						</xsl:for-each>
					</table>
				</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	<arguments type="user">
		<var name="SearchTerm" comment="the gene name you wish to search for" />
		<var  name="ColumnType" comment="the column types you want, * for all"  />
		<var  name="MeasurementNames" comment="the Name of the measurements you want, * for all"  />
	</arguments>

	<query id="1" name="SearchGene" comment="search table Gene" type="maxdBrowse" >
		<searchterm uref="user" name="SearchTerm" />
		<action>search</action>
		<table>Gene</table>
	</query>
	
	
	<query id="2" name="EnumerateGene" comment="enumerate some Genes" type="maxdBrowse" >
		<geneids xref="getGeneIds" processResultID="1" />
		
		<types uref="user" name="ColumnType" />
		<names uref="user" name="MeasurementNames" />
		
		<action>enumerate</action>
		<table>Measurement</table>
		<spots>Feature,Reporter,Gene</spots>
	</query>
	
	
	<query id="3" name="PresentMyResults" comment="now take the results and present them" type="export" >
		<h1>Query results</h1>
		<h2>Genes found</h2>
		<searchResult xref="formatSearchResult" processResultID="1" />
		<h2>Expression Values</h2>
		<enumerateResult pref="formatEnumerateResult" processResultID="2" />
	</query>

	
	
	
</maxdQL>
