#!/usr/bin/perl
use strict;
use warnings;
use Bio::DB::GFF;
use Bio::Graphics;
use Bio::SeqFeature::Generic;

my $dmdb = Bio::DB::GFF->new( -adaptor => 'dbi::mysql',
			      -dsn => 'test',
			      -aggregators=> 'processed_transcript{exon,five_prime_UTR,three_prime_UTR,intron/mRNA}',
			    );
my @genes = qw(CG17131 CG7486);
foreach my $gene (@genes){

  my $tg = $dmdb->segment(-name => $gene);

  if ($tg){
    my $panel = Bio::Graphics::Panel->new(
					  -length => $tg->length,
					  -width  => 800,
					  -pad_left => 10,
					  -pad_right => 10,
					 );
    my $full_length =
      Bio::SeqFeature::Generic->new(-start=>1,-end=>$tg->length);

    $panel->add_track($full_length,
		      -glyph   => 'arrow',
		      -tick    => 2,
		      -fgcolor => 'black',
		      -double  => 1,
                            );

    my @transcripts = $tg->features(-types =>'processed_transcript');

    $panel->add_track(processed_transcript=>\@transcripts,
		      -label=>1,
		      -implied_utrs=>1,
		      -key=>'transcripts');

    for my $tc (@transcripts){
      next unless $tc->attributes('Gene') eq $gene;

      my @exon   = $tc->get_SeqFeatures('exon');

      $panel->add_track(    generic => \@exon,
			    -bgcolor => 'blue',
			    -label  => 1,
			    -bump => 1,
			    -key => "$tc exons",
		       );

    }

    open FH, ">$gene.png" || die "Can't create file $gene.png\n";
    print FH $panel->png;
    $panel->finished;
    close FH;
  }
}
1;
