<?xml version="1.0" encoding="UTF-8"?>
<maxdQL version="0.1" name="GeneHunterTabTextSLOW" >
	
	<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. Formatted as text.</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="text" >
		<![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...
			*/
			
			
			
			// 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);
			}
			
			$firstMeasurementName = $MeasurementNamesArray[0];
			
			$FeatureIDs = array_keys($input[$firstMeasurementName]["Feature"]);
			
			//print_r($input);die();
			
			
			$firstMeasurement = true;
			// first row: the measurement names
			foreach ($MeasurementNamesArray as $MeasurementName)
			{
				foreach ($input[$MeasurementName] as $columnName => $column) {
					if (
						($firstMeasurement == true) || 
						( 
							($columnName != "Feature") && 
							($columnName != "Reporter") && 
							($columnName != "Gene") 
						)
					) {
						$output .= "\t" . $MeasurementName . $columnName ;	
					}
				}
				$firstMeasurement = false;
			}
			
			$firstMeasurement = true;
			// second row the column names:
			$output .= "\nFeatureIDs";
			foreach ($MeasurementNamesArray as $MeasurementName)
			{
				foreach ($input[$MeasurementName] as $columnName => $column) {
					if (
						($firstMeasurement == true) || 
						( 
							($columnName != "Feature") && 
							($columnName != "Reporter") && 
							($columnName != "Gene") 
						)
					) {
						$output .= "\t" . $columnName ;	
					}
				}
				$firstMeasurement = false;
			}
			
			
			// now list the expression values...
			// similar loop as before, except that it's wrapped in a featureid loop as well
			foreach ($FeatureIDs as $FeatureID)
			{
				$firstMeasurement = true;
				$output .= "\n" . $FeatureID;
				foreach ($MeasurementNamesArray as $MeasurementName)
				{
					foreach ($input[$MeasurementName] as $columnName => $column) 
					{
						if (
						($firstMeasurement == true) || 
						( 
							($columnName != "Feature") && 
							($columnName != "Reporter") && 
							($columnName != "Gene") 
						)
						) {
							$output .= "\t" . $column[$FeatureID];
						}
					}
					$firstMeasurement = false;
				}		
			}
			
			
			
			
			
		]]>
	</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" format="text" >
		<h1>Results</h1>
		<table pref="enumerateParser" processResultID="2" >
			<var uref="user" name="MeasurementNames" />
		</table>
	</query>
	
</maxdQL>
