# 2010-07-08
# Function: generate decoy DB that proteins of target DB are shuffled
# Input: target DB
# Output: decoy DB with each entries start with '###SHF###'


BEGIN{
	push @INC, 'E:\Source\perl';
}
use liningIO;
use List::Util qw(shuffle);

($seq,$Ano) = liningIO::DBseq(@ARGV);
%seq = %$seq;
%Ano = %$Ano;
%SHF = ();

$target = $ARGV[0];
$target =~ s/fasta//g;
$decoy = $target."SHF.fasta";
open DECOY,">".$decoy or die "cannot open $decoy\n";

$ShfTag = '###SHF###';
foreach $ID (keys %seq){
	
	$SeqThisID = $seq{$ID};
	@SeqP = split //, $SeqThisID;

$iter = 0;
do{	
	
	@SeqPSHF = shuffle @SeqP;
	$SHF{$ID} = join '',@SeqPSHF;
	$iter++;
	
} until (($SHF{$ID} ne $SeqThisID) or ($iter>5));

	if ($SHF{$ID} eq $SeqThisID){
		print "god like!\n";
		print $ID,"\n";
	}
	
	$DecoyTitle = $Ano{$ID};
	$DecoyTitle =~ s/>//;
	$DecoyTitle = ">".$ShfTag.$DecoyTitle;
	
	print DECOY $DecoyTitle,"\n";
	print DECOY $SHF{$ID},"\n";
#	print $DecoyTitle,"\n";
#	print $ID,"\n";
#	print $RTP{$ID},"\n";
#	print length($SeqThisID)," ",length($RTP{$ID}),"\n";
	
}
close DECOY;
print "finished!\n";