<?xml version="1.0" encoding="UTF-8"?>
<maxdQL version="0.1" name="GeneHunterXHTML" >
	
	<description>Will take a list of Gene names, a list of Measurement names and a list of Column Types and produce a table. All values are coloured in relation to the a reference. In the list of Measurement names, the first Measurement listed is taken to be the reference.</description>
	
	<unitTest>
		php run.php -file=../../sequences/geneHunterPHP.xml -display=last -profile="The mouse microarray experiment" -GeneNames="Affy:Mouse430_2:1415670_at,Affy:Mouse430_2:1415671_at,Affy:Mouse430_2:1415672_at,Affy:Mouse430_2:1415673_at" -ColumnType="RMA Normalised" -MeasurementIDs="0,1"
	</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/browse/Gene"><xsl:value-of select="@ID"/>,</xsl:for-each>
			</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	<pfunction id="enumerateParser" comment="take a list of genes and make a geneids tag from their ids" method="xml" >
		<![CDATA[
			/*
			* 2 magic variables here: $input and $output
			* $input is the input object that needs parsing
			* it may be of 
			* - type: Array or 
			* - type: DOM
			* $output is the string that will be read by the engine at the end
			* also :
			* $ColumnType has been injected from the query that calls this function...
			*/
			
			
			// a variable to monitor first measurement's expression values, which will act as a baseline
			$firstTime = true;
			
			// create a new dom
			$domNew = new DOMDocument();
			$root = $domNew->createElement("result");
			$rootDom = $domNew->appendChild($root);
			
			// make a table
			$table = $domNew->createElement("table");
			$tableDom = $rootDom->appendChild($table);
			$tableDom->setAttribute("border", "1");
			
			$tr = $domNew->createElement("tr");
			$trDom = $tableDom->appendChild($tr);
			
			// if the user has supplied a list of specific measurements, 
			// make a list of measurement names from user input,
			if ($MeasurementNames != "*" ) {
				$MeasurementNamesArray = explode(",", $MeasurementNames );
			} else 
				// or if the user requested *, 
				// make the array from the keys of the $input array
			{
				$MeasurementNamesArray = array_keys($input);
			}
			
			// create table headings from this array
			foreach ($MeasurementNamesArray as $MeasurementName ) 
			{
				$th = $domNew->createElement("th");
				$thDom = $trDom->appendChild($th);
				$text = $domNew->createTextNode($MeasurementName);
				$text = $thDom->appendChild($text);
			}
			
			$tr1 = $domNew->createElement("tr");
			$tr1Dom = $tableDom->appendChild($tr1);
			
			// generate columns for each measurement
			foreach ($MeasurementNamesArray as $MeasurementName) 
			{
				$td = $domNew->createElement("td");
				$tdDom = $tr1Dom->appendChild($td);
			
				// some measurement names may not actually have data...
				if (isset($input[$MeasurementName]) )
				{
					$cols = $input[$MeasurementName];
					
					$colNames = array_keys($cols);
			
					// create a sub table
					$table2 = $domNew->createElement("table");
					$table2Dom = $tdDom->appendChild($table2);
					$table2Dom->setAttribute("border", "1");
				
					
					$tr = $domNew->createElement("tr");
					$trDom = $table2Dom->appendChild($tr);
				
					
					// assign headings to each column in the sub table
					foreach ($colNames as $colName)
					{
	
						$th = $domNew->createElement("th");
						$thDom = $trDom->appendChild($th);
				
						/* note createTextNode is better than createElement
						 * for inserting text as it escapes it
						 */
						$text = $domNew->createTextNode($colName);
						$text = $thDom->appendChild($text);
				
					}
				
					$featureIDs = array_keys($cols["Feature"]);
				
					// fill the columns
					foreach ($featureIDs as $featureID) {
						$tr = $domNew->createElement("tr");
						$trDom = $table2Dom->appendChild($tr);
				
						
						foreach ($colNames as $colName)
						{
							$td = $domNew->createElement("td");
							$tdDom = $trDom->appendChild($td);
					
							// colour the cells by comparing expression values with original
							if (is_numeric($cols[$colName][$featureID]) )
							{
								if ($firstTime == true)
								{
									$colour = "blue";
								} elseif ( $cols[$colName][$featureID] > $originalValues[$colName][$featureID] )
								{
									$colour = "red";
								} elseif ( $cols[$colName][$featureID] < $originalValues[$colName][$featureID] ) {
									$colour = "green";
								} else 
								{
									$colour = "black";
								}
				
								$tdDom->setAttribute("style", "color:white;background-color:$colour;");
							} 
				
							
							
				
							$text = $domNew->createTextNode($cols[$colName][$featureID]);
							$text = $tdDom->appendChild($text);
				
							if ($firstTime == true)
							{
								$originalValues[$colName][$featureID] = $cols[$colName][$featureID];
							}
				
				
						}
	
					}
				
					if ($firstTime == true)
					{
						$firstTime = false;
					}
				}
			
			}
			
			$output = $domNew;
			
		]]>
	</pfunction>
	
	<arguments type="user" >
		<var name="GeneNames" comment="the names of the genes you want, *for all"/>
		<var name="ColumnTypes" comment="the column names, comma-separated, * for all" />
		<var name="MeasurementNames" comment="the measurement names, * for all. The first Measurement listed is taken to be the reference." />
	</arguments>
	
	<query id="1" name="SearchGene" comment="search table Gene" type="maxdBrowse" >
		<names uref="user" name="GeneNames" />
		
		<action>browse</action>
		<table>Gene</table>
		<attributes>no</attributes>
	</query>
	
	<query id="2" name="EnumerateGenesForDesiredMeasurements" comment="enumerate some Genes" type="maxdBrowse">
		<geneids xref="getGeneIds" processResultID="1"/>
		
		<types uref="user" name="ColumnTypes"/>
		<names uref="user" name="MeasurementNames"/>
		
		<action>enumerate</action>
		<table>Measurement</table>
		<spots>Feature,Reporter,Gene</spots>
		<format>array</format>
	</query>
	
	<query id="3" name="FormatEnumeration" comment="digests the enumeration" type="export" >
		<h1>Results</h1>
		<table pref="enumerateParser" processResultID="2" >
			<var uref="user" name="MeasurementNames" />
		</table>
	</query>
	
</maxdQL>
