use Data::Dumper;

my %rf2type;
open GENOMESEQ, "family.txt" || die "$!";
while(<GENOMESEQ>) {
   chomp;
   my @temp = split(/\t/);
   $temp[18] =~ s/Gene; //;
   if($temp[18] =~ /snoRNA/) {
	  $temp[18] = "snoRNA";    
   } elsif($temp[18] =~ /snRNA/) {
	  $temp[18] = "snRNA";    
   } elsif($temp[18] =~ /Cis-reg/) {
	  $temp[18] = "Cis-reg";    
   } elsif($temp[18] =~ /sRNA/) {
	  $temp[18] = "sRNA";    
   } 
   $temp[18] =~ s/;//;
   
   $rf2type{$temp[0]} = $temp[18];
}
close GENOMESEQ;

my %acc2name;
my %acc2annotation;
open GENOMESEQ, "acc2name.txt" || die "$!";
while(<GENOMESEQ>) {
   chomp;
   my @temp = split(/\t/);
   $acc2name{$temp[1]} = $temp[0];
   $acc2annotation{$temp[1]} = $temp[2];
}
close GENOMESEQ;



my %id2acc;
open GENOMESEQ, "Rfam.full_region" || die "$!";
while(<GENOMESEQ>) {
   chomp;
   my @temp = split(/\t/);
   my ($start,$end);
   $start = $temp[2]; 
      $end = $temp[3]; 	  
   
   $id2acc{$temp[1]."/".$start."-".$end} = $temp[0];
}
close GENOMESEQ;


open GENOMESEQ, "osa.fa" || die "$!";
$/=">";<GENOMESEQ>;$/="\n";
while(<GENOMESEQ>) {
   chomp;
   my $seqname = $_;  
   $/=">";
   my $seq=<GENOMESEQ>;
   chomp($seq);
   if($seqname =~ /Oryza sativa/ and $seqname =~ /Japonica/) {
	   my @temp = split(/ /,$seqname);
	   my $pfid = $id2acc{$temp[0]};
	   if(exists $rf2type{$pfid} and $rf2type{$pfid} !~ /miRNA|lncRNA|tRNA/ and $acc2name{$pfid} !~ /bacteria/) {
			print ">".$temp[0]."|".$acc2name{$pfid}."|".$acc2annotation{$pfid}."|".$rf2type{$pfid}."\n".$seq;
	   }
       
   }
   $/="\n";
}
close GENOMESEQ;
