#! /usr/bin/perl

use strict;
use  Getopt::Std      qw(getopt);

getopt( 'io' );

my $name = $Getopt::Std::opt_i; 
my $output = $Getopt::Std::opt_o; 

if (($name eq "")||($output eq ""))
{
	print "Parameters to the program are as follows:\n";
	print "\t-i\t Name of input file.\n";
	print "\t-o\t Name of output FASTA file.\n";

	exit;
}

#GO:0033279 - ribosome

my $go_term = "GO:0005840";

open (FILE, "< $name") or die "NO $name file found!\n";
open (OUTPUT, "> $output");

print "Reading $name file!\n";
my $counter_1 = 0;
my $counter_2 = 0;
my $total = 0;

while (my $temp = <FILE>)
{
	chomp($temp);
	$total++;

	my @array = split(/\s+/, $temp);

	my $weight = $array[1];	
	my $ID = $array[7];
	my $AC = $array[8];

	print OUTPUT ">$ID|$AC|$weight\n";

	system "entret sw:$AC dasha -auto";

	open (DASHA, "< dasha");
	
	my $already_cellular = "N";
	my $already_ribosome = "N";
	my $reading_seq = "N";

	while (my $string = <DASHA>)
	{
		if (($string =~ /C\:/)&&($string =~ /GO\;/))
		{
			#print "GOT 'Cellular component' $AC -> $weight\n";
			if ($already_cellular ne "Y")
			{
				$counter_1++;
				$already_cellular = "Y";	
			}
		}
		if ($string =~ /$go_term/)
		{
			#print "GOT 'ribosome' $go_term: $AC -> $weight\n";
			if ($already_ribosome ne "Y")
			{
				$counter_2++;
				$already_ribosome = "Y";	
			}
		}
		if ($string =~ /^SQ/)
		{
			$reading_seq = "Y";
			next;
		}
		if ($reading_seq eq "Y")
		{
			if ($string =~ /^\//)
			{
				last;
			}
			$string =~ s/\s+//g;
			print OUTPUT "$string\n";
		}
	}

	close DASHA;
	system "rm dasha";
}

close OUTPUT;
close FILE;

print "FILE = $name\n";
print "TOTAL = $total\n";
print "Cellular_component = $counter_1\n";
print "Ribosome = $counter_2\n";