use strict;

my %seqs;
open CHART,$ARGV[0] || die "$!";
     $/=">";<CHART>;$/="\n";
     while(<CHART>){
       chomp;
       my $seqhead = $_;
       my $seqname;
       if($seqhead=~/Oryza_sativa_Japonica_Group_(\S+)/) {
          $seqname = $1;
       }
       $/=">";
       my $seq=<CHART>;
       chomp($seq);
       $seq=~s/\s+//g;
       $seq=~s/\n+//g;
       $seqs{$seqname} = $seq;
       $/="\n";
}
close CHART;

open IN,$ARGV[1] || die "$!";
while(<IN>){
   chomp;
   next if(/^$/);
   my @tem = split(/\t/);
   $tem[1] = $tem[1]+1;
   my $maturelen;
   next if(not exists $seqs{$tem[3]});
   if($tem[9] == 1) {
      $maturelen = length($seqs{$tem[3]})+3;
      print ">".$tem[3]."|$tem[0]:$tem[1]-$tem[2]:$tem[5]|$maturelen\n".$seqs{$tem[3]}."CCA"."\n";
   } else {
      $tem[10] =~ s/,$//;
      $tem[11] =~ s/,$//;
      my @tem2 = split(/,/,$tem[10]);
      my @tem3 = split(/,/,$tem[11]);      
      my $mature;
      my $seq = $seqs{$tem[3]};
      if($tem[5] eq "-") {
         $seq = reverse $seq;
      }
      for(my $j=0;$j<@tem2;$j++) {
          $mature .= substr($seq,$tem3[$j],$tem2[$j]);
      }
      if($tem[5] eq "-") {
         $mature = reverse $mature;
      }
      $maturelen = length($mature)+3;
      print ">".$tem[3]."|$tem[0]:$tem[1]-$tem[2]:$tem[5]|$maturelen\n".$mature."CCA"."\n";
   }
}
close IN;


sub revdnacomp {
  my $dna = shift;
  my $revcomp = reverse($dna);
  $revcomp =~ tr/ACGTacgt/TGCAtgca/;
  return $revcomp;
}
