#! /usr/bin/perl

# Terminator builder.
$inp_file="terminator.inp";
open(IN,$inp_file);

$_=<IN>;
$_=<IN>;

@par=();
while(<IN>){
   chop($_);
   $_ =~ s/^.+://;
   $_ =~ s/ //g;
   push(@par,$_);
}

#for($i=0;$i<$#par+1;$i++){
#   print "$par[$i] \n";
#}

@def=("terminator",'n','n','n','n',31.25,0.5);

# Setting file/class name.
if($par[0] !~ /^\s*$/){
   $t_name = $par[0];
}else{
   $t_name = $def[0];
}

# Setting forward readthrough flag (y/n):
if($par[1] !~ /^\s*$/){
   $frt_flag= $par[1];
}else{
   $frt_flag= $def[1];
}

# Setting backwards readthrough flag
if($par[2] !~ /^\s*$/){
   $brt_flag= $par[2];
}else{
   $brt_flag= $def[2];
}

# Setting terminator flag
if($par[3] !~ /^\s*$/){
   $p_flag= $par[3];
}else{
   $p_flag= $def[3];
}

# Setting RNA flag (y/n):
if($par[4] !~ /^\s*$/){
   $RNA_flag= $par[4];
}else{
   $RNA_flag= $def[4];
}

# Setting zeta value
if($par[5] !~ /^\s*$/){
   $zeta= $par[5];
}else{
   $zeta= $def[5];
}

# Setting eta value
if($par[6] !~ /^\s*$/){
   $eta= $par[6];
}else{
   $eta= $def[6];
}

#####################################################################
# Checking parameters

if($RNA_flag eq 'y' && $brt_flag eq 'y'){
   die "sRNA production can not be affected by readthrough. You set \"backwards readthrough\" to 'y' \n";
}

if($frt_flag eq 'n' && $brt_flag eq 'y'){
   die "A backwards readthrough cannot exist without a forward one. You set \"backwards readthrough\" to 'y' and \"forward readthrough\" to 'n' \n";
}

if($RNA_flag eq 'y' && $p_flag eq 'y'){
   die "A terminator can be preceded only by one part. You set to 'y' both \"promoter flag\" and \"RNA flag\". \n"; 
}

####################################################################

open(OUT,">$t_name.mdl");

print OUT "
(define-module
  :class \"$t_name\"
  :super-classes (\"module\")
  :icon \"terminator.png\"
  :terminals((\"in_pol\"
	      :geometry-side \"LEFT\"\
	      :geometry-position \"0.9\"
	      :variables
	      ((\"pops\"
	        :documentation \"Incoming PoPS\"
	        :system-theoretic \"input\"
	        :value \"0.0d0\"
	        :unit \"m/s\")";

if($RNA_flag eq 'y' || $p_flag eq 'y'){ 
print OUT "
	       (\"pops_lk\"
	        :documentation \"Incoming PoPS_lk\"
	        :system-theoretic \"input\"
	        :value \"0.0d0\"
	        :unit \"m/s\")";
}

print OUT "))";

print OUT "
	      (\"out_pol\"
	       :geometry-side \"LEFT\"
	       :geometry-position \"0.1\"
	       :variables
	       ((\"pops\"
	        :is-eq-to \"parent.pops_out\")))";

if($frt_flag eq 'y'){
 print OUT "
	      (\"out_rt\"
	       :geometry-side \"RIGHT\"
	       :geometry-position \"0.9\"
	       :variables
	       ((\"pops\"
	         :is-eq-to \"parent.pops_rt\")))";
}

if($brt_flag eq 'y'){
 print OUT "
	      (\"out_rbs\"
	       :geometry-side \"BOTTOM\"
	       :geometry-position \"0.5\"
	       :variables
	       ((\"pops\"
	         :is-eq-to \"parent.pops_rbs\")))";
}

if($RNA_flag eq 'y'){
print OUT "
              (\"out_rna\"
               :geometry-side \"RIGHT\"\
	       :geometry-position \"0.1\"
	       :variables
	       ((\"rnaps\"
	         :is-eq-to \"parent.rnaps_out\")))";
$rnaps_out="zeta*polt";
	      }

print OUT ")";

print OUT "
  :variables((\"pops_in\"
	      :is-eq-to \"in_pol.pops\")
	     (\"pops_out\"
	      :documentation \"Outgoing PoPS\"
	      :system-theoretic \"output\"
	      :value \"0.0d0\"
	      :unit \"m/s\")
	     (\"polt\"
              :documentation \"[PolT] (polymerase-terminator) complex\"
	      :system-theoretic \"state\"
	      :value \"0.0d0\"
	      :unit \"m/s\")
	     (\"zeta\"
	      :documentation \"[PolT] dissociation rate\"
	      :system-theoretic \"real-parameter\"
	      :value \"${zeta}d0\"
	      :unit \"s ^ (-1)\")";

if($frt_flag eq 'y'){
print OUT "
             (\"pops_rt\"
	      :documentation \"Outgoing readthrough PoPS\"
	      :system-theoretic \"output\"
	      :value \"0.0d0\"
	      :unit \"m/s\")
	     (\"eta\"
	      :documentation \"Readthrough rate\"
	      :system-theoretic \"real-parameter\"
	      :value \"${eta}d0\"
	      :unit \"s ^ (-1)\")";
}

if($brt_flag eq 'y'){
print OUT "
             (\"pops_rbs\"
	      :documentation \"Readthrough PoPS to the previous RBS\"
	      :system-theoretic \"output\"
	      :value \"0.0d0\"
	      :unit \"m/s\")";
}

if($RNA_flag eq 'y'){
print OUT "
             (\"rnaps_out\"
	      :documentation \"Outgoing RNAPS\"
	      :system-theoretic \"output\"
	      :value \"0.0d0\"
	      :unit \"m/s\")
	     (\"pops_lk\"
	      :is-eq-to \"in_pol.pops_lk\")";
}
	   
if($p_flag eq 'y'){
print OUT "		
	     (\"pops_lk\"
	      :is-eq-to \"in_pol.pops_lk\")";
}	      

print OUT ")";

$pops_rt="eta*polt";
$pops_rbs="eta*polt";

print OUT "
  :equations((\"pops_out_eq\"
              :relation \"pops_out == zeta*polt\")";

if($RNA_flag eq 'y'){
print OUT "
	     (\"rnaps_out_eq\"
	      :relation \"rnaps_out == $rnaps_out + pops_lk\")";
	   }

if($frt_flag eq 'y'){
 print OUT "
             (\"polt_eq\"
              :relation \":diff<t> polt == pops_in-zeta*polt-$pops_rt\")
	     (\"pops_rt_eq\"
	      :relation \"pops_rt == $pops_rt\")";
}
   
if($brt_flag eq 'y'){
 print OUT "
	     (\"pops_rbs_eq\"
	      :relation \"pops_rbs == $pops_rbs\")";
}

if($frt_flag eq 'n'){
 print OUT "
	     (\"polt_eq\"
              :relation \":diff<t> polt == pops_in-zeta*polt\")";
}

print OUT "))\n";
