#!usr/bin/perl
# rename_UniProt_toTaxName.pl

### this script is used to rename  sequences from Uniprot

#use strict;
#use warnings;
unless(@ARGV==1) {die "please provide name of the file in the command line!!\n";}

open ( IN , "<$ARGV[0]");
my@temp= split ( /\./ , $ARGV[0] );
my$outname=$temp[0]."\.renamedTax\.seq";
open (OUT , ">$outname");

while ( my $line = <IN> ) {
	
	chomp $line;
	
	if ( $line =~ s/^>/_/ ) {
	    $count += 1;
	    $line =~ s/^_\s/_/;
		
		#my @info = split ( /\|/ , $line );
		
		#print "@info\n";
		
		# match only the species name at array position 4 and save it 
		# into the variable $1
		
		$line =~ /Tax=(.*?)TaxID/;			 
		
		my $species_strain = $1;
		#print "$1\n";
		
		# replace all whitespace with "_" characters
		$species_strain =~ s/\s/_/g;
		
		#print "$species_strain\n";
		
		#print OUT (">"."$count"."_"."$species_strain"." "."$line\n"); #this guaranties unique names
		
		print OUT ("\n>"."$count"."$species_strain"."$line\n"); # the newline at the begining is needed because the sequence itself is stripped of the \n
		print (">"."$species_strain"." "."\n");
		
	}
	
	else {print OUT ("$line"); #prints to file without newlines.  
		print ("$line\n")}#prints to screen with newlines
		
}
		
		
