use EnsEMBL::Web::RegObj;
use EnsEMBL::Web::ExtIndex;
use Data::Dumper;
use CGI;

my $T = new CGI;
my $indexer = new EnsEMBL::Web::ExtIndex( $ENSEMBL_WEB_REGISTRY->species_defs );
my $result_ref;
my @RES;
print "Content-type: text/plain\n\n";
eval {
  if( $T->param('res') eq 'seq' ) {
    $result_ref = $indexer->get_seq_by_id({ DB => 'EMBL', ID => $T->param('ID'), OPTIONS => 'seq'});
    my $seq = join '', map { s/\W//g;$_ } grep { !/^>/ } @{$result_ref||[]};
    if( $T->param('start') && $T->param('end') ) {
      $seq = substr($seq,$T->param('start')-1,$T->param('end')); 
    }
    print $seq;
  } else {
    $result_ref = $indexer->get_seq_by_id({ DB  => 'EMBL', ID  => $T->param('ID'), OPTIONS => 'desc' });
    print @{$result_ref||['NO DATA']};
  }

  if (0) {
  if( $T->param('res') eq 'seq' ) {
    $result_ref = $indexer->get_seq_by_id({ DB => 'EMBL', ID => $T->param('ID'), OPTIONS => 'seq'});
    my $seq = join '', map { s/\W//g;$_ } grep { !/^>/ } @{$result_ref||[]};
    if( $T->param('start') && $T->param('end') ) {
      $seq = substr($seq,$T->param('start')-1,$T->param('end')); 
    }
    if( $T->param('strand') && $T->param('strand') < 0 ) {
      $seq = reverse $seq;
      $seq=~tr/acgtACGT/tgcaTGCA/;
    }
    print $seq;
  } else {
    $result_ref = $indexer->get_seq_by_id({ DB  => 'EMBL', ID  => $T->param('ID'), OPTIONS => 'desc' });
    print @{$result_ref||['NO DATA']};
  }
  }
};

1;
