#!/usr/bin/php
<?php
error_reporting(0);
Class HMMGap
{
	var $seq; // Full Sequence
	var $gapfile; // Partial Sequence from GAP
	var $tms;
	var $pos;
	var $omark='<b><u>';
	var $emark='</u></b>';
	var $aligned;
	var $orig;
	var $totaltms;
	var $tms_index;
	
	public function __Construct($seq,$gap,$out=NULL)
	{
		$this->seq=$seq;
		$this->orig=$gap;
		$this->gapfile=$gap;
		$this->gapfile=preg_replace("/(\.+)/",NULL,$this->gapfile);		
		$this->hmmtop($this->seq);
		$this->getPos();
		$this->count_tms();
		$this->findTMS();
		if($out)
		{
			$handle=fopen($out,"w+");
			fwrite($handle,"<pre>{$this->aligned}</pre>");
		} else
		{
			echo "{$this->aligned}\n";
		}
	}
	
	
	public function get()
	{
		return $this->aligned;
	}
	
	public function hmmtop($seq)
	{
		$seq=urlencode($seq);
		$url="http://www.enzim.hu/hmmtop/server/hmmtop.cgi";
		$data="if=$seq&button2=HMM+TOP";
		$c=$this->post_data($url,$data);
		if($c)
		{
			$pattern='/seq.{1,}<\/pre>/is';
			preg_match_all($pattern,$c,$out,PREG_PATTERN_ORDER);
			$trim='/([0-9])|(seq)|(pred)|(<\/pre>)/is';
			$clean=trim(preg_replace($trim,NULL,$out[0][0]));
			$clean=explode("\n\n",$clean);
			foreach($clean as $row)
			{
				$row=explode("\n",$row);
				$seqn[]=preg_replace("/(\t+)|(\s+)/",NULL,$row[0]);
				$tms[]=preg_replace("/(\t+)|(\s+)/",NULL,$row[1]);
			}
			$seqn=implode("",$seqn);
			$tms=implode("",$tms);
			return $this->tms=array($seq,$tms);
		}
	}
	
	public function count_tms()
	{
		preg_match_all("/H+/",$this->tms[1],$total,PREG_PATTERN_ORDER);
		$tms=count($total[0]);
		$this->totaltms=$tms;
		$tc=0;
		for($i=0;$i<=strlen($this->tms[1])-1;$i++)
		{
			if(($this->tms[1][$i]=='H')&&($this->tms[1][$i-1]!="H"))
			{
				$tc++;
				$rc=$tc;
			}
			if(($this->tms[1][$i]=='H')&&($this->tms[1][$i-1]=='H'))
			{
				$rc="-";
			}
			if($this->tms[1][$i]!='H')
			{
				$rc='&nbsp;';
			}
			$index[]="$rc";
		} 
		$this->tms_index=$index;
		
	}
	
	public function post_data($url,$data)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		$result = curl_exec($ch);
		return $result;
	}
	
	public function getPos()
	{
		$pos=explode($this->gapfile,$this->tms[0]);
		if($pos[0]) // String was NOT in the beginning..
		{
			$start=strlen($pos[0])+1;
			$end=strlen($this->gapfile)+$start-1;
		} else // String was at found @ start...
		{
			$start=1;
			$end=strlen($this->gapfile);
		}
		$this->pos=array($start,$end);
	}
	
	public function findTMS()
	{
		for($i=$this->pos[0]-1;$i<=$this->pos[1]-1;$i++)
		{
			$string[]=($this->tms[1][$i]=='H')?strtolower($this->tms[0][$i]):$this->tms[0][$i];
			$tindex[]=$this->tms_index[$i];
		}
		$x=0;
		for($i=0;$i<=strlen($this->orig)-1;$i++)
		{
			if($this->orig[$i]!='.')
			{
				$gaps[]=$string[$x];
				$index[]=$tindex[$x];
				$x++;
			} else
			{
				$gaps[]='.';
				$index[]="&nbsp;";
			}
		}
		//print_r($string);
		//print_r($index);
		$string=implode("",$gaps);
		$string=preg_replace("/([a-z\.]+)/","<b>$1</b>",$string);
		$string=strtoupper($string);
		$string=preg_replace('/<b>(\.+)<\/b>/i','$1',$string);
		$index=implode("",$index);
		
		return $this->aligned="<i>$index</i><br>$string [{$this->totaltms} TMSs]";
	}
}
$hmmgap=new HMMGap($argv[1],$argv[2],$argv[3]);
?>