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

	<arguments type="user">
		<var 
			name="ArrayTypeName" 
			comment="the name of the array type you want" 
		/>
		
		<var name="GeneAccessionLinkPre"  
			comment="the initialpart of a URL that ends with the gene accession number. for example, the url 'http://streptomyces.org.uk/cgi-bin/sco/external.pl?name=SCO001' has a LINK_PRE of 'http://streptomyces.org.uk/cgi-bin/sco/external.pl?name=' "
		/>
		<var name="GeneDatabaseName"  
			comment="the name of the database"
		/>
		
	</arguments>
	
	
	<pfunction id="arrayTypeHeader" comment="this prints the platform header" method="text" >
		<![CDATA[
			$map = array (
				"!Platform_coating" => "ArrayType.Surface Type",
				"!Platform_support" => "ArrayType.Substrate Type",
				"!Platform_technology" => "ArrayType.Technology Type",
				"!Platform_manufacture_protocol" => "ArrayType.Spotting Method",
				"!Platform_manufacturer" => "ArrayType.Manufacturer.Name",
				"!Platform_distribution" => "ArrayType.Manufacturer.Origin",
				"!Platform_title" => "Name",
				"^PLATFORM" => "Name",
				"!Platform_organism" => "ArrayType.Species.Standard Ontology.Value",
				"!Platform_web_link" => "ArrayType.Manufacturer.Website" 
			);
			
			$required = array(
				"^PLATFORM",
				"!Platform_title", 
				"!Platform_distribution", 
				"!Platform_technology", 
				"!Platform_organism", 
				"!Platform_manufacturer", 
				"!Platform_manufacture_protocol",
				"!Platform_support",
				"!Platform_coating",
				"!Platform_web_link"
			);
			
			
			$protocols = array ("!Platform_manufacture_protocol");
			
			// note only obvious equivalents are translated here... 
			// or else the value will be kept as in maxdLoad2
			$translations = array (
				"!Platform_technology" => array (
					"spotted DNA/cDNA" => "spotted_ds_DNA_features",
					"spotted oligonucleotide" => "spotted_ss_oligo_features",
					"in situ oligonucleotide" => "in_situ_oligo_features",
					"antibody" => "spotted_antibody_features",
					"RT-PCR" => "spotted_ss_PCR_amplicon_features"
				),
				"!Platform_distribution" => array (
					"commercial" =>  "Commercial",
					"non-commercial" => "Non-commercial"
				)
			);
			
			// assumes linkedResults come from only one original entry... shoddy!
			foreach ($input as $linkedKey => $linkedResult)
			{
				$newInput = $linkedResult;	
			}
			
			$ArrayType = $newInput["ArrayType"];
			//print_r($ArrayType["Name"]);
			$columns = array_keys($ArrayType);
			
			$foundArrayTypeID = false;
			foreach ($ArrayType["Name"] as $id => $name)
			{
				//print "\n '$name' == '$ArrayTypeName' \n";
				if ($name == $ArrayTypeName)
				{
					$ArrayTypeID = $id;
					$foundArrayTypeID = true;
				}
			}
			
			if ($foundArrayTypeID != true)
			{
				throw new exception ("Sorry, but the ArrayType ID was not found for $ArrayTypeName.");
			}
			
			$string = "";
			
			foreach ($required as $req)
			{
				$reqMapped = $map[$req];

				if (! in_array($reqMapped, $columns) )
				{
					throw new exception ("'ArrayType.$reqMapped' not found ...  though this field may not required by maxdLoad2, it is essential for GEO export. It's the equivalent field to GEO's '$req'. Please annotate this database with this field.");
				} else {
					
					$value = $ArrayType[$reqMapped][$ArrayTypeID];
					
					if (isset($translations[$req]) ) {
					
						$string .= "\n" . $req . " = " . array_search ($value, $translations[$req]);
						
					} 
					elseif (in_array($req, $protocols) )
					{
					
						$split = explode("\u000A", $value);
						
						foreach ($split as $n => $s)
						{
							$string .= "\n" . $req . " = " . ($n+1) . ". " . $s;
						}
					
					} 
					else 
					{
						$string .= "\n" . $req . " = " . $value;
					}
					
					
				}
			}
			
		$output = $string;
		
		//print "'" . $output . "'";
		//die();
			
		]]>
	</pfunction>
	
	<pfunction id="platformTabber" comment="this tabs intensities" method="text" >
		<![CDATA[
				
				//print_r($input);die();
				
				// assumes linkedResults come from only one original entry... shoddy!
				foreach ($input as $linkedKey => $linkedResult)
				{
					$newInput = $linkedResult;	
				}
				
				$maxGeneNumber = 0;
				$tableString = "";
				
				$input2sort = $newInput["Feature"]["Name"];
				ksort($input2sort);
				
				foreach ($input2sort as $id => $name)
				{
					$tableString .= "\n" . $id . "\t" . $name;
					
					$reporters =& $newInput['Feature']['Reporter->'][$id];

					if (count($reporters) > 0) 
					{
						foreach ($reporters as $reporterId => $reporterName)
						{
							$tableString .= "\t" . $reporterName;
							
							$genes =& $newInput['Reporter']['Gene->'][$reporterId];
							
							$geneCount = count($genes);
							
							if ($geneCount > $maxGeneNumber)
							{
								$maxGeneNumber = $geneCount;
							}
							
							if ($geneCount > 0)
							{ 
								foreach ($genes as $geneId => $geneName)
								{
									$tableString .= "\t" . $geneName;
								}
							}
						}
					}
				}
				
				$headerString = "\nID\tFeature\tReporter$maxGeneNumer";
				//$output = "\n" . $maxGeneNumber;
				$headerCommentString = "\n#ID = the feature ID";
				$headerCommentString .= "\n#Feature = the name of the Feature";
				$headerCommentString .= "\n#Reporter = the name of the Reporter";
				
				for ($i=1;$i<=$maxGeneNumber;$i++)
				{
					$headerString .= "\tSCO_ACC (#$i)";
					$headerCommentString .= "\n#SCO_ACC (#$i) = the $GeneDatabaseName accession of a gene picked up by the probe LINK_PRE:\"$GeneAccessionLinkPre\" ";
				}
				
				$output = $headerCommentString . "\n!platform_table_begin" . $headerString . $tableString . "\n!platform_table_end";

				
				
					
		]]>
	</pfunction>
		
	
	<query id="1" 
	       name="Linker" 
	       comment= "link ArrayType to Gene"
	       type="maxdBrowse" 
	       >
		<names uref="user" name="ArrayTypeName"  />
		<action>link</action>
		<table>ArrayType</table>
		<desiredTable>Gene</desiredTable>
		<!--<displayTable>ArrayType,Feature,Reporter,Gene</displayTable>-->
		<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" 
	       >
		
		<arraytypeannotations pref="arrayTypeHeader" processResultID="1"  >
			<var uref="user" name="ArrayTypeName"  />
		</arraytypeannotations>
		<featureannotations pref="platformTabber" processResultID="1"  >
			<var uref="user" name="GeneAccessionLinkPre"  />
			<var uref="user" name="GeneDatabaseName"  />
		</featureannotations>



		
	</query>
	

</maxdQL>
