#!/usr/bin/perl -w 

open TXT1 , "<./mito_metazoa_flat.txt";

$out_file_1 = "./cds_rRNA_table.txt";

system "rm $out_file_1";
open OUT, ">>./$out_file_1";


print OUT"accession_num\tfeature\tposition\tgene\tproduct\n";

while(<TXT1>)
{
	chomp;
	if($_=~/^LOCUS.*/)
	{
		$acc = "";
		$posi = "";
		$rna_posi = "";
		$e = 0;
		$i = 0;
		$j = 0;
		$k = 0;
		$c = 0;
		$h = 0;
		###	rRNA control
		$n = 0;
		$p = 0;
		$y = 0;
		$z = 0;
		$cds_gene = "";
		$cds_prod = "";
		@cds_product = ();		

	}

	if($_=~/^\s+ACCESSION\s+(\S+).*/)
	{
		$acc = $1;

	}

	if($_=~/^\s+FEATURES\s+.*/)
	{
		$i = 1;


	}

	###	if shows "FEATURE"
	if($i == 1)
	{
		###	process next feature of CDS
		if($z == 1)
		{

			###	ex: accession number:Y11796 only has /gene and does not have /product /translation

			if($_ =~ /^\s{6}(\S+)\s+\S+.*/)
			{
				$feat = $1;
				if($feat ne 'CDS') 
				{	
					print OUT"\n";
					$z = 0;
					$j = 0;	

				}

			}

			if($_=~/\s{1}ORIGIN.*/)
			{
				print OUT"\n";
				$z = 0;

			}


		}

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

		###	process of rRNA region
		if($_=~ /^\s{6}(\S+)\s+(.*)/)
		{
			$rna_feature = $1; 
			$rna_posi = $2;
	
			if($rna_feature eq 'rRNA' )
			{	
				$r = 0;

				if($n == 0)
				{
					print OUT "$acc\trRNA\t$rna_posi\t";	
				}

				if($n > 0)
				{
					print OUT "\n$acc\trRNA\t$rna_posi\t";	
				}
	
				$n++;
			
				$p = 1;
				$y = 1;
			}else{$n = 0;}

		}

		if($p == 1)
		{

			if($_=~ /^\s+\/gene=\"(.*)\".*/)			
			{
				$rna_gene = $1;
				$r = 1; 
				print OUT"$rna_gene\t";

			}

			if($_=~ /^\s+\/product=\"(.*)\".*/)			
			{
						
				if($r == 0)
				{
					print OUT"\t";
				}
				
				$rna_prod = $1;
				print OUT"$rna_prod";
			}
			

		}

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






		###	process next feature of rRNA
		if($y == 1)
		{
			
			if($_ =~ /^\s{6}(\S+)\s+\S+.*/)
			{
				$rna_feat = $1;
				if($rna_feat ne 'rRNA') 
				{	
					print OUT"\n";
					$p = 0;	
					$y = 0;

				}

			}

			if($_=~/\s{1}ORIGIN.*/)
			{
				print OUT"\n";
				$y = 0;
			}



		}
		#####################################




		###	if appears "CDS"	
		if($_=~ /^\s{6}(\S+)\s+(.*)/)
		{
			$feature = $1;
			$posi = $2;
	
			if($feature eq 'CDS') 
			{			
				
				$a = 0;
				$b = 0;
				$j = 1;
				$e = 1;
				###	position switch
				$h = 1;
				$g = 0;
				$f = 0;	
				$k = 0;
				$z = 1;
				###	if there are multiple CDS
				$cds_gene = "";
				$cds_prod = "";
				@cds_product = ();
				@posi = ();
				###	if there are repeated CDS
				if($c == 0)
				{
					print OUT"$acc\tCDS\t";
					$posi =~ s/^\s+|\s+$//g;
					push @posi , $posi; 
				}			

				if($c > 0)
				{
					print OUT"\n$acc\tCDS\t";
					$posi =~ s/^\s+|\s+$//g;	
					push @posi , $posi;
				}

				$c++;
			
			}else{$c = 0;}

		}
		##############################



		###	process of CDS's position	
		if($h == 1)
		{	
			if($_=~ /\s+\/.*/)
			{
				print OUT"@posi\t";
				$h = 0;
				$g = 0;

			}
			$g++;
			if($g > 1)
			{
				$_ =~ s/^\s+|\s+$//g;
				push @posi , $_;
			}	
			
							

		}		
		###############################








		###	process of CDS region
		if($j == 1)
		{
			if($_=~ /^\s+\/gene=\"?(.*)\".*/)
			{
				$cds_gene = $1;
				if($f == 0)
				{
					print OUT"$cds_gene\t";
				}	
				$a = 1;
				$f++;	

			}

			if($_=~ /^\s+\/product=\"?(.*)\"?.*/)
			{
				$cds_prod = $1;

				###	if there is no "gene" before "product" 
				if($a == 0)
				{
					print OUT"\t";
				}

				$k = 1;	

				if($_=~ /^\s+\/product=\"(.*)\".*/)
				{
					print OUT"$1";
					$k = 0;

				}	

				if($k == 1)
				{

					push @cds_product , $cds_prod;	

				}				

			}
			

			###	if CDS has product note with multi-lines  
			if($k == 1)
			{
				$b++;
				if($b > 1)
				{
					if($_=~ /\s+(.*)\".*/)
					{	
						push @cds_product , $1;
						print OUT"@cds_product"; 
						$k = 0;
					}else{print OUT "$_";}	
				}						
				
			}	
							

		}

	}

}
