#Script developped by group 9 of the Bioinformatics course at Universitat Pompeu Fabra. Autumn-Winter of 2012.
#You are allowed to use and modify this sript.

#Script to split a multifasta containing all selenoproteines from SelenoDB into a single fasta for each protein
#The fastas are named this way: proteincode.aa.fa
#It also creates an index containing the following information: #Protein code
								#Protein name
								#Species name in this format: genre_species


#!/usr/bin/perl -w

use strict;
use warnings;

print "Enter the multifasta you want to split:\n";
my $multifasta = <STDIN>;
chomp($multifasta);

open(my $allseleno, "<", "$multifasta");
open(my $protein_index, ">", "./selenoprotein_index.txt");
open(my $protein_index, ">>", "./selenoprotein_index.txt");
print $protein_index "Prot_code\tProt_name\tSpecies\n";

my @prot_code; my $pc;
my @prot_name; my $pn;
my @species; my $s;
my @splittedline;
my @name;
my $file;

system ("mkdir ./selenoproteins");


while (<$allseleno>) {
	@splittedline = split /#/, $_;
	if ($splittedline [0] =~ m/^\>/) {		
		@prot_code = $splittedline [0] =~ m/\>([A-Za-z0-9_\.]+)/;
		@prot_name = $splittedline [2] =~ m/\(([A-Za-z0-9_]+)\)/;
		$splittedline [3] =~ s/\s/_/g;
		@species = $splittedline [3] =~ m/_([A-Za-z0-9_]+)_/;
		$pc = join "", @prot_code;
		$pn = join "", @prot_name;
		$s = join "", @species;
		@name = $pc . ".aa.fa";
		open($file, ">>", "./selenoproteins/@name");
		print $protein_index "$pc\t$pn\t$s\n";
	}
	print $file "$_";
	

}
