#!/usr/bin/php
+------------------------------------------------------------------+
| _______ _______ _______      _______        _____  ______ __   _ |
|    |    |  |  | |______      |_____| |        |   |  ____ | \  | |
|    |    |  |  | ______|      |     | |_____ __|__ |_____| |  \_| |
|------------------------------------------------------------------|
| This program will run ClustalW and generate a multiple           |
| alignment sequence or use an existing .ALN file to               |
| generate a report with highlighted and numbered TMS              |
| regions in the aligned sequences. Uses HMMTOP to predict.        |
|                                                                  |
| By Vamsee Reddy <symphony.dev@gmail.com>                         |
|__________________________________________________________________|

<?php
Class TMSAlign
{
    var 
$aln;
    var 
$file;
    var 
$out='aligned.html';
    var 
$fastafile;
    
    public function 
__Construct()
    {
        
$type=$this->getInput("Do you want to analyze a\n[1] Alignment File (.aln)\n[2] FASTA File (.faa)\n(Pick a number)");
        if(
$type==1)
        {
            
$this->aln=file_get_contents($this->getInput("Drag your ALN file here >>"));
        } else
        {
            
$this->fastafile=trim($this->getInput("Drag your FASTA file here >>"));
            
$this->unique_fastas($this->fastafile);
        }
        
$outdir=$this->getInput("Name your output directory (will be saved to desktop)");
        @
mkdir($outdir);
        
chdir($outdir);
        if(
$type==2)
        {
            
            
system("clustalw2 -INFILE='/tmp/unique.faa' -ALIGN -OUTFILE='aligned.aln'");
            
$this->aln=file_get_contents('aligned.aln');
        }
        
$this->build();
        
$this->write();
        
chdir('../');
        
system("mv ./$outdir ~/Desktop/$outdir");
        
system("open ~/Desktop/$outdir/{$this->out}");
    }
    
    public function 
build()
    {
        
$pattern='/[A-Z0-9]{1,7}\s+[A-Z,-]{1,60}/i';
        
$this->aln=preg_replace("/CLUSTAL [0-9,\.]{1,10} multiple sequence alignment/",NULL,$this->aln);
        
preg_match_all($pattern,$this->aln,$out,PREG_PATTERN_ORDER);
        foreach(
$out[0] as $res)
        {
            
$res=preg_split('/\s+/',$res);
            
$seq[$res[0]][]=$res[1];
        }
        foreach(
$seq as $symbol=>$s)
        {
            
$final[$symbol]=implode(NULL,$s);
        }
        
$this->aln=$final;
    }
    
    public function 
unique_fastas($in)
    {
        
$res=file_get_contents($in);
        
$res=explode(">",$res);
        unset(
$res[0]);
        foreach(
$res as $row)
        {
            
$fastas[]=">$row";
        }
        
$index=array();
        foreach(
$fastas as $fasta)
        {
            
$hash=md5(trim($fasta));
            if(!
in_array($hash,$index))
            {
                
$index[]=$hash;
                
$unique[]=$fasta;
            }
        }
        
$fastas=array_unique($unique);
        
$fastas=trim(implode("\n",$fastas));
        
$handle=fopen("/tmp/unique.faa","w+");
        
fwrite($handle,$fastas);
    }
    
    public function 
getInput($msg)
    {
        
fwrite(STDOUT"$msg: ");
        
$varin trim(fgets(STDIN));
        return 
$varin;
    }
    
    public function 
write()
    {
        
//unlink($out);
        
$handle=fopen($this->out,'w+');
        
$css='<style type="text/css">
        body,td,th {
            font-family: "Courier New", Courier, monospace;
            font-size: 14px;
            color: #0F0;
        }
        body {
            background-color: #000;
        }
        b{
            color:#F00;
        }
        </style>
        (TMS Align V.2 by Vamsee Reddy #symphony.dev@gmail.com)<br><br>
        '
;
        
fwrite($handle,$css);
        foreach(
$this->aln as $symbol=>$seq)
        {
            
$clean=preg_replace('/[^A-Z]/',NULL,$seq);
            
$seq=preg_replace('/-/','.',$seq);
            
ob_start();
            
$line=system("hmmgap $clean $seq");
            
ob_get_clean();
            
$write=sprintf("%s<pre>%s</pre>",$symbol,$line);
            
fwrite($handle,$write);
            echo 
'.';
        }
        echo 
"DONE! \n\n";
    }
}
new 
TMSAlign;
?>