<?xml version="1.0" encoding="UTF-8"?>
<maxdQL version="0.1" name="GEOseries" >
	<description>Will describe an experiment in softSOFTtext format</description>

	<arguments type="user">
		<var 
			name="ExperimentName" 
			comment="the name of the experiment" 
		/>
		
		
		
	</arguments>


	<pfunction id="seriesTabber" comment="this prints the platform header" method="text" >
		<![CDATA[
			
			//print_r($input);die();
			
			$map = array (
				"^SERIES" => "Experiment.Name",
				"!Series_title" => "Publication.Title",
				"!Series_type" => "Experiment.Design Type.*",
				"!Series_overall_design" => array ("Experiment.Number Of Conditions", "Experiment.Number Of Hybridisations", "Experiment.Quality Control.Description", "Experiment.Replicates.Description"),
				"!Series_summary" => "Experiment.Description",	
				"!Series_contributor" => "Submitter.Name",
				"!Series_sample_id" => "Measurement.Name"
				
			);
			
			$cardinality = array (
				"^SERIES" => "1",
				"!Series_title" => "1",
				"!Series_type" => "1",
				"!Series_overall_design" => "1",
				"!Series_summary" => "1",	
				"!Series_contributor" => "1*",
				"!Series_sample_id" => "1*"
				
			);
			
			foreach ($cardinality as $thing => $card)
			{
				if ( ($card == "1") || ($card == "1*") )
				{
					$required[] = $thing;
				}
			}
			
			// assuming there is only one entry linked to...
			// shoddy!
			foreach ($input as $linkKey => $linked)
			{
				$newInput = $linked;
			}
			
			//print_r($input);die();
			
			foreach ($newInput as $table => $columns)
			{
				//print "\n" . $table;
				foreach ($columns as $colName => $col)
				{
					// i don't think you need to strip the -> because 
					// only Names and Attributes needed...
					
					//$colNameStripped = str_replace ("<-","",$colName);
					//$colNameStripped = str_replace ("->","",$colNameStripped);
					//print "\n\t" . $colNameStripped;
					//$string .= "\n$colName";
					$mapKeys = array();
					$mappedName = "";
					if (in_array ($colName, $map) )
					{
						$mapKeys = array_keys ( $map, $colName);
						$mappedName = $colName;
						//print "\n$mapKey\t" . $colName;
						
					} elseif (in_array ($table . "." . $colName, $map) ) 
					{
						$mapKeys = array_keys ($map, $table . "." . $colName);
						$mappedName = $table . "." . $colName;
						//print "\n$mapKey\t" . $table . "." . $colName;
					} 
					
					if (count($mapKeys) > 0) 
					{
						foreach ($mapKeys as $mapKey)
						{
							$mappedValues[$mapKey][$mappedName] = $col;
						}

					} else {	// if nothing found chech to see if there's a wildcard
						foreach ($map as $mk => $mv)
						{
							if (preg_match("/$mv/", $colName) )
							{
								//$string .= "\n$mv ...";
								$mappedValues[$mk][$colName] = $col;
								
							} elseif (is_array($mv) )
							{
								if (in_array($colName, $mv) )
								{
									
									$mapKeys = array_keys ($mv, $colName);
									$mappedName = $colName;
									foreach ($mapKeys as $mapKey)
									{
										
										$mappedValues[$mk][$mappedName] = $col;
									}
								}	
							}
						}
						
						
						
					}
					
				}
			}
			
			//print_r($mappedValues);
			
			foreach ($mappedValues as $mapKey => $cols)
			{
				foreach ($cols as $colName => $col)
				{
					foreach ($col as $rowKey => $rowValue)
					{
						$strings[$mapKey][] = "$rowValue";
					}
				}
			}
			
			$found = array();
			
			foreach ($strings as $mapKey => $stringSet)
			{
				if ($cardinality[$mapKey] == "1" ) {
					$separator ="";
					$string .= "\n$mapKey = ";
					foreach ($stringSet as $stringSetString)
					{
						$string .= $separator . $stringSetString;
						$separator = ". ";
					}
				} 
				
				else {
					foreach ($stringSet as $stringSetString)
					{
						$string .= "\n$mapKey = $stringSetString";
					}
				}
				
				$found[$mapKey] = true;
				
			}
			
			foreach ($required as $req)
			{
				if (! isset ($found[$req] ) )
				{
					throw new exception ("sorry, but the value '" . $map[$req] . "' is required for GEO's '$req' so you must add this to the database entry.");
				}
			}
			
			$string .= "\n";
			
			$output = $string;
			
			
			
		]]>
	</pfunction>

	<query id="1" 
	       name="linkExperiment" 
	       comment= "link experiment"
	       type="maxdBrowse" 
	       >
		<names uref="user" name="ExperimentName"  />
		<action>link</action>
		<table>Experiment</table>
		<desiredTable>Submitter,Measurement</desiredTable>
		<attributes>yes</attributes>
		<format>array</format>
		<linkVersion>02</linkVersion>
	</query>

	<query id="2" 
	       name="tabIt" 
	       comment="makes a tab-delimmited text version" 
	       type="export" 
	       format="text" 
	       >
	       <seriesHeader pref="seriesTabber" processResultID="1" />
	</query>
</maxdQL>

