#!/usr/bin/php
____________________________________________________________________
++ _____  _____ ______  _____          _____ ______  _____  _____ ++
++/  ___||  _  || ___ \|_   _|        |_   _|| ___ \|  ___||  ___|++
++\ `--. | | | || |_/ /  | |   ______   | |  | |_/ /| |__  | |__  ++
++ `--. \| | | ||    /   | |  |______|  | |  |    / |  __| |  __| ++
++/\__/ /\ \_/ /| |\ \   | |            | |  | |\ \ | |___ | |___ ++
++\____/  \___/ \_| \_|  \_/            \_/  \_| \_|\____/ \____/ ++
++================================================================++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
##  This program will sort your Make_Table5 .TAB files according  ##
##  to a Phylogenetic tree file (.PH) & write the new file onto   ##
##  your desktop. (V1.0) Vamsee Reddy<Symphony.Dev@Gmail.com>     ##
####################################################################

<?
class ST
{
    var 
$raw_tree;
    var 
$raw_data;
    var 
$data;
    var 
$tree;
    var 
$output;
    var 
$div="\n>"// "\n>" for FASTA files "\n" for TAB files.
    
    
function __Construct()
    {
        
$tree=trim($this->getInput("\n>> Drag your Tree file Here"));
        
$data=trim($this->getInput("\n>> Drag MakeTable5 TAB File here"));
        
$output=$this->getInput("\n>> Enter an Output Filename");
        
$this->raw_tree=trim(file_get_contents($tree));
        
$this->raw_data=trim(file_get_contents($data));
        
$this->load_tree();
        
$this->load_data();
        
$this->sort();
        
$this->save($output); // Filename of output file. Make sure directory is writable.
        
$fp=getcwd();
        echo 
"\n++ $output has been written to $fp\n\n";
    }
    
    private function 
load_data()
    {
        
$data=explode($this->div,$this->raw_data);
        foreach(
$data as $line)
        {
            
//$dline=explode("\t",$line);
            
$dline=preg_split("/(\t)|( )+/",$line);
            
$key=preg_replace("/[^A-Z0-9]/i",NULL,$dline[0]);
            
$array[$key]=$line;
        }
        
$this->data=$array;
        return 
TRUE;
    }
    
    public function 
getInput($msg)
    {
        
fwrite(STDOUT"$msg: ");
        
$varin trim(fgets(STDIN));
        return 
$varin;
    }
    
    private function 
load_tree()
    {
        
$pattern="/[A-Za-z0-9]{3,7}/s";
        
preg_match_all($pattern,$this->raw_tree,$outPREG_PATTERN_ORDER);
        
$this->tree=$out[0];
        
//print_r($this->tree);
        
return TRUE;
    }
    
    public function 
sort()
    {
        foreach(
$this->tree as $key)
        {
            
$output[]=$this->data[$key];
        }
        
        
$this->output=array_filter($output);
        
$this->output=array_merge(array("\n"),$this->output);
        return 
TRUE;
    }
    
    public function 
save($filename=NULL)
    {
        
$data=implode($this->div,$this->output);
        
$data=preg_replace("/\n+/","\n",$data);
        
$data=trim($data);
        if(
$filename)
        {
            
$handle=fopen($filename,"w+");
            
fwrite($handle,$data);
            return 
TRUE;
        } else
        {
            return 
$data;
        } 
    }
}
$ST=new ST;
?>