#!/usr/bin/perl 
##############################################################################
#
# Author:Jack Min, 

# Name: separate_sub_locations.pl 
# retri secreted, mito, vacuole, ER, Golgi, cytoskeleton, cytoplasm (cytosol), nuc, lysosome, peroxisome, . remove entries with terms "Probable" "Potential" "By similarity"
############################################ Body of the code ################################

if ($#ARGV != 0)
{
	print "Usage: separate_sub_locations.pl input \n";
	print "input - generated by uniprotParse.pl\n";
	exit;
}

print "In progress ...\n";

$inFile = $ARGV[0];
#$outFile = $ARGV[1];
  
unless( open(IN, $inFile))
{
	print STDERR "Cannot open file $inFile1\n\n";
   	exit;
}
@inA = <IN>;
close IN;

$int = 0; 
$inLine = '';
$flag = 0;
foreach $line (@inA)
{
	chomp($line);
	print $int++."\n"; 
	$line =~ s/[\n\f\r]//;
	if ($line =~ /(Potential)/ ||$line =~ /(Probable)/ ||$line =~ /(By similarity)/)
	{
		next;
	}
	else
	{ 
		if ($line =~ /Secreted/i || $line =~ /extracellular space/i || $line =~ /extracellular matrix/i ) 
		{
			if ($line !~ /membrane/i && $line !~ /mito/i && $line !~ /nuc/i && $line !~ /vac/i && $line !~ /Endo/ && $line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA, $line."\n");
			}
		}
		elsif ($line =~ /mitoch/i) 
		{
			if ($line !~ /cell/i && $line !~ /nuc/i && $line !~ /vac/i && $line !~ /Endo/ && $line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA2, $line."\n");
			}
		}
		elsif ($line =~ /nucl/i) #$line !~ /cell/i
		{
			if ($line !~ /cell/i && $line !~ /vac/i && $line !~ /Endo/ && $line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA3, $line."\n");
			}
		}
		elsif ($line =~ /vac/i) #$line !~ /cell/i
		{
			if ($line !~ /cell/i && $line !~ /Endo/ && $line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA4, $line."\n");
			}
		}
		elsif ($line =~ /endoplasmic reticulum/i) #$line !~ /cell/i
		{
			if ($line !~ /cell/i && $line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA5, $line."\n");
			}
		}
		elsif ($line =~ /cell membrane/i) 
		{
			if ($line !~ /cyto/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA6, $line."\n");
			}
		}
		elsif ($line =~ /cytoskeleton/i) 
		{
			if ($line !~ /membrane/i && $line !~ /vesicle/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA7, $line."\n");
			}
		}
		elsif ($line =~ /cytoplasm/i || $line =~ /cytosol/i) 
		{
			if ($line !~ /myofibril/i && $line !~ /membrane/i && $line !~ /vesicle/i && $line !~ /golgi/i && $line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA8, $line."\n");
			}
			
		}
		elsif ($line =~ /golgi/i) 
		{
			if ($line !~ /lysosome/i && $line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA9, $line."\n");
			}
		}
		elsif ($line =~ /lysosome/i) 
		{
			if ($line !~ /perox/i && $line !~ /Melanosome/i)
			{			
				push (@outA10, $line."\n");
			}
		}
		elsif ($line =~ /perox/i) 
		{
			if ($line !~ /Melanosome/i)
			{			
				push (@outA11, $line."\n");
			}
		}

	}
}
$f1 = "sec.txt";
open (OUT, ">$f1");
print OUT (@outA);
close OUT;
$f2 = "mito.txt";
open (OUT, ">$f2");
print OUT (@outA2);
close OUT;
$f3 = "nuc.txt";
open (OUT, ">$f3");
print OUT (@outA3);
close OUT;
$f4 = "vac.txt";
open (OUT, ">$f4");
print OUT (@outA4);
close OUT;
$f5 = "ER.txt";
open (OUT, ">$f5");
print OUT (@outA5);
close OUT;
$f6 = "cell_membrane.txt";
open (OUT, ">$f6");
print OUT (@outA6);
close OUT;
$f7 = "cytoskeleton.txt";
open (OUT, ">$f7");
print OUT (@outA7);
close OUT;
$f8 = "cytoplasm.txt";
open (OUT, ">$f8");
print OUT (@outA8);
close OUT;
$f9 = "golgi.txt";
open (OUT, ">$f9");
print OUT (@outA9);
close OUT;
$f10 = "lyso.txt";
open (OUT, ">$f10");
print OUT (@outA10);
close OUT;
$f11 = "perox.txt";
open (OUT, ">$f11");
print OUT (@outA11);
close OUT;


print "The job is done!!\n\n";
exit;
