<?php

$precision = 3;
$max_seqs = 500;


?><html>
<head>
<style>
body {
	font-family: arial;
	color: #003965;
	font-size: 14px;
}
textarea { 	
	width: 600px;
	height: 120px;
}
</style>
</head>
<body>
<?php if( isset( $_POST['sequence'] )) : ?>

	<?php

	  require_once 'util.php';

	  $file =  '/tmp/cwpred' . uniqid();

	  $seqs = readSeqs($_POST['sequence']); 
	  $organism = $_POST['org'];
	?>

	<?php if( count($seqs) > $max_seqs ) : ?>

		<em>You can input no more than <?= $max_seqs ?> sequences at a time.</em>

	<?php else: ?>

		<?php

		  $input = '';

		  foreach($seqs as $v) {
			$input .= ">" . $v['name'] . "\n" . substr($v['seq'], -60) ."\n"; 
		  }
			echo $input;

		  file_put_contents( $file, $input );
		
		chdir("./juchmme/bin");
		
		#Arxaia
		if($organism=='arc')
		{
			exec ("java hmm/Juchmme -a ../tables/A_archaea_trained -e ../tables/E_archaea_trained -c ../conf/conf.cwpred_archaea -m ../models/cwpred_archaea.mdel -f $file -p", $out, $ret);
		}

		#Gram-negative
		elseif ($organism=='neg') 
		{
		    exec("java hmm/Juchmme -a ../tables/A_negative_trained -e ../tables/E_negative_trained -c ../conf/conf.cwpred_negative -m ../models/cwpred_negative.mdel -f $file -p", $out, $ret);
		}

		#Gram-posiive
		elseif ($organism=='pos') 
		{
		    exec("java hmm/Juchmme -a ../tables/A_positive_trained -e ../tables/E_positive_trained -c ../conf/conf.cwpred_positive -m ../models/cwpred_positive.mdel -f ../input/positive_training.txt -f $file -p", $out, $ret);
		}		
		unlink( $file ); 
		chdir("../../");
		?>

		<?php if( $ret == 0  ) : ?>

			<?php

			    $out = implode( "\n", $out );
print "<pre>$out</pre>";
			    preg_match_all( '/Preparing Sequences.*?LP: \w*/s', $out, $matches );

			    $results = $matches[0];
#print "<br/ ><br/ ><pre>LALAKIS<br/>$results</pre>";
		?>

			<?php foreach ( $results as $r ) : ?>

				<?php

					print "<pre>$r</pre><br/><br/>";
				      preg_match( '/^ID: >(.*)$/m', $r, $matches );

				      $seq_name = $matches[1]; 

				      preg_match( '/^VP: (.*)$/m', $r, $matches );

				      $reliability = $matches[1]; 

				      preg_match_all( '/\d+\s*\((.)\)\s*.:\s*VI:\s*(.)\s*PVI:\s*.\s*PVI:\s*.\s*\|.*\|.*\|.*\|.*\|.*\|\s*([\d\.E-]*)\s*([\d\.E-]*)\s*([\d\.E-]*)\s*([\d\.E-]*)\s*([\d\.E-]*)/', $r, $matches );

				      for( $i=3; $i<=7; $i++ ) {

					$arr = array();

					foreach( $matches[$i] as $v ) {
					
						$arr[] = round($v, $precision);
					}

					$paths[] = $arr;
									      }

				       $aa = $matches[1];
				       $vpath = $matches[2];
					
					?>

					<?php if( count($results) == 1 ) : ?>

						<img src='http://chart.apis.google.com/chart?<?= "chs=600x200" . 
						"&cht=lc" . 
						"&chco=FF0000,00FF00,0000FF" . // ",FF00FF,00FFFF" . 
						"&chds=0,1,0,1,0,1" . // ",0,1,0,1" . 
						"&chdl=Lipo signal peptide|Sec signal peptide|TM segment" . 
						"&chxt=x,x,y,y".
						"&chxr=2,0,1|0,1,".count($aa).
						"&chxl=3:|Posterior probability|1:|Residue no". //implode('|',$aa) . "" . 
						"&chxp=3,50|1,50".
						"&chd=t:".
							//implode(',',$paths[0])."|". // I
							implode(',',$paths[3])."|". // L
							implode(',',$paths[2])."|". // S
							implode(',',$paths[1])."". // M
							//implode(',',$paths[4])."".  // O
						"" ?>' />
						<br />
						<br />
						<br />

					<?php endif ?>

					<?php

				       $previous = '';

					$type = null;
					$start = null;
					$end = null;

				       foreach( $vpath as $k => $v ) {

					  if( $v != $previous ) {
						
						if( $v == 'S' || $v == 'M') {
							$start = $k+1; 
							if( $v== 'S' )
								$type = 'Sec signal peptide';
							elseif( $v== 'M' )
								$type = 'TM segment';
						} else {
							$end = $k;
						}
					  }
					

					  $previous = $v; 
				       }
					
				       #$cleavage = $aa[$end-3] . $aa[$end-2] . $aa[$end-1] ."-". $aa[$end-0] . $aa[$end+1];
				?>
				
				<?= htmlspecialchars( $seq_name ) ?> <?= count($results) == 1? '<br /><br />': ' - ' ?>  

				<b>

				<?php if ($type == 'Sec signal peptide' || $type == 'Lipo signal peptide') : ?>

					<?= $type ?> predicted.

				<?php else : ?>

					No cell-wall sorting signal detected

				<?php endif ?>	

				</b>

				<?= count($results) == 1? '<br /><br />': ' - ' ?>
				
				Reliability score: <?= sprintf( "%.3f", $reliability ) ?>

				<br />

			<?php endforeach // end foreach sequence ?>

		<?php else : // return is not zero  ?>

			<em>Program execution error. Please contact us for help.</em>

		<?php endif // end if return is zero ?>

	<?php endif // end if sequences < max_seqs ?>

	<br />
	<br />
	<a href="index.php">Run again</a>

<?php else: // form has not been posted ?>

	<p> Please paste up to <?php print $max_seqs ?> sequences in the textarea below:</p>
	<form method=post>
	<textarea name=sequence></textarea>
	<br />
	<p>Select source organism type:
        <input type="radio" name="org" value="arc" checked /> Archaea
        &nbsp;<input type="radio" name="org" value="neg" /> Gram-positive bacteria
        &nbsp;<input type="radio" name="org" value="pos" /> Gram-negative bacteria
	<br /><br />
	<input type=submit value=Run />
	</form>
	<hr>
    If you find CW-PRED useful in your research, please consider citing the reference that describes this work: <br /> <br />
    <b>A Hidden Markov Model for the prediction of C-terminal cell-wall sorting signals in Bacteria and Archaea.</b><br/>
    Aikaterini G. Chatziargyri¶ , Evangelia A. Stasi¶ , Eleni Nydrioti, Konstantinos D. Tsirigos, Zoi I. Litou, Vassiliki A. Iconomidou and Pantelis G. Bagos. <br/>
    <i>under review  <u></u></i>

<?php endif // end if form has been posted ?>

</body>
</html>
