@tRF5=(18,22);
@tRF3=(20,24);
@p5_half_length=(32,36);
@misc=(18,40);
$misc_start=8;
$tRF1_min=18;
$tRF1_max=40;

%subst=("A"=>"T","T"=>"A","G"=>"C","C"=>"G");

open(TESTSET,">test_tRF_dataset-Homo_sapiens.fas")||die print$!;
open(GTRNADB,"gtRNAdb_homo_sapiens.fasta")||die print$!;
open(TRF1,"homo_sapiens.tRF-1.fas")||die print$!;

while(<GTRNADB>)
	{
	$_=~s/\s*$//;
	if($_=~s/^>Homo_sapiens_//)
		{
		$_=~s/ .+$//;
		$head=$_;
		}
	else
		{
		$tRNA_mature{$head}.=uc$_;
		}
	}
close GTRNADB;

foreach$tRNA_name(sort{$a cmp $b}keys%tRNA_mature)
	{
	foreach$l($tRF5[0]..$tRF5[1])
		{
		if($tRNA_mature{$tRNA_name}=~/^.{$l}/)
			{
			$seq=$&;
			print TESTSET">tRF-5|$tRNA_name\n$seq\n";
			substr($seq,9,1)=$subst{substr($seq,9,1)};
			print TESTSET">tRF-5_(1mm)|$tRNA_name\n$seq\n";
			$n_tRF5++;
			}
		}
	foreach$l($p5_half_length[0]..$p5_half_length[1])
		{
		if($tRNA_mature{$tRNA_name}=~/^.{$l}/)
			{
			$seq=$&;
			print TESTSET">5'-tRNA-half|$tRNA_name\n$seq\n";
			substr($seq,9,1)=$subst{substr($seq,9,1)};
			print TESTSET">5'-tRNA-half_(1mm)|$tRNA_name\n$seq\n";
			$n_tRF5half++;
			}
		}
	foreach$l($tRF3[0]..$tRF3[1])
		{
		if($tRNA_mature{$tRNA_name}=~/.{$l}$/)
			{
			$seq=$&;
			print TESTSET">tRF-3|$tRNA_name\n$seq\n";
			substr($seq,9,1)=$subst{substr($seq,9,1)};
			print TESTSET">tRF-3_(1mm)|$tRNA_name\n$seq\n";
			$n_tRF3++;
			}
		}
	foreach$i(1..5)
		{
		$tRNA_half=substr($tRNA_mature{$tRNA_name},31+$i);
		next if(length$tRNA_half<18);
		print TESTSET">3'-tRNA-half|$tRNA_name\n$tRNA_half\n";
		substr($tRNA_half,9,1)=$subst{substr($tRNA_half,9,1)};
		print TESTSET">3'-tRNA-half_(1mm)|$tRNA_name\n$tRNA_half\n";
		$n_tRF3half++;
		}
	foreach$l($misc[0]..$misc[1])
		{
		$misc=substr($tRNA_mature{$tRNA_name},$misc_start,$l);
		next if(length$misc<18);
		print TESTSET">miscellaneous-tRF|$tRNA_name\n$misc\n";
		substr($misc,9,1)=$subst{substr($misc,9,1)};
		print TESTSET">miscellaneous-tRF_(1mm)|$tRNA_name\n$misc\n";
		$n_misc++;
		}
	}

while(<TRF1>)
	{
	$_=~s/\s*$//;
	if($_=~s/^>[^\|]+\|//)
		{
		$head=$_;
		}
	elsif(length$_>=$tRF1_min&&length$_<=$tRF1_max)
		{
		print TESTSET">tRF-1|$head\n$_\n";
		substr($_,9,1)=$subst{substr($_,9,1)};
		print TESTSET">tRF-1_(1mm)|$head\n$_\n";
		$n_tRF1++;
		}
	}
close TRF1;
close TESTSET;
print"
tRF-5\t$n_tRF5
tRF-3\t$n_tRF3
5'-half\t$n_tRF5half
3'-half\t$n_tRF3half
tRF-1\t$n_tRF1
misc\t$n_misc

";