#! /usr/bin/perl

use warnings;


$gene_name = "";
$last_transcript_id = "";
$last_cds_id = "";
$cds_transcript_id = "";
@exons = ();
@CDS_regions = ();
while (defined($line = <>)) {
    if ($line =~ /^\#/) {
	# Skip
    } elsif ($line !~ /\S/) {
	# Skip blank line
    } else {
	$line =~ s/\r\n/\n/;
	chop $line;
	@fields = split /\t/,$line;

	if ($fields[2] eq "gene") {
	    if ($#exons >= 0) {
		# Handle last mRNA of previous gene
		if ($strand eq "+") {
		    ($start,$end) = get_gene_bounds_plus(\@exons);
		    printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
		} elsif ($strand eq "-") {
		    ($start,$end) = get_gene_bounds_minus(\@exons);
		    printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
		} else {
		    die "strand $strand";
		}
		print "$gene_name\n";
		print_exons(\@exons,$gene_name,$last_transcript_id,$chr,$strand);

	    }
	    @exons = ();

	    if ($#CDS_regions >= 0) {
		# Handle last mRNA of previous gene
		if ($strand eq "+") {
		    ($start,$end) = get_gene_bounds_plus(\@CDS_regions);
		    printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
		} elsif ($strand eq "-") {
		    ($start,$end) = get_gene_bounds_minus(\@CDS_regions);
		    printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
		} else {
		    die "strand $strand";
		}
		print "$gene_name\n";
		print_exons(\@CDS_regions,$gene_name,$cds_transcript_id,$chr,$strand);
	    }
	    @CDS_regions = ();

	    ($gene_name) = $fields[8] =~ /ID=([^;]+)/;
	    $chr = $fields[0];
	    
	} elsif ($fields[2] eq "mRNA" || $fields[2] eq "transcript" || $fields[2] eq "ncRNA") {
	    if ($#exons >= 0) {
		if ($strand eq "+") {
		    ($start,$end) = get_gene_bounds_plus(\@exons);
		    printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
		} elsif ($strand eq "-") {
		    ($start,$end) = get_gene_bounds_minus(\@exons);
		    printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
		} else {
		    die "strand $strand";
		}
		print "$gene_name\n";
		print_exons(\@exons,$gene_name,$last_transcript_id,$chr,$strand);

	    }
	    @exons = ();

	    if ($#CDS_regions >= 0) {
		if ($strand eq "+") {
		    ($start,$end) = get_gene_bounds_plus(\@CDS_regions);
		    printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
		} elsif ($strand eq "-") {
		    ($start,$end) = get_gene_bounds_minus(\@CDS_regions);
		    printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
		} else {
		    die "strand $strand";
		}
		print "$gene_name\n";
		print_exons(\@CDS_regions,$gene_name,$cds_transcript_id,$chr,$strand);
	    }
	    @CDS_regions = ();

	    ($last_transcript_id) = $fields[8] =~ /ID=([^;]+)/;
	    $strand = $fields[6];
	    
	    # For GFF3 files without a gene line
	    if (!defined($gene_name) || $gene_name !~ /\S/) {
		$gene_name = $last_transcript_id;
	    }
	    if (!defined($chr) || $chr !~ /\S/) {
		$chr = $fields[0];
	    }

	} elsif ($fields[2] eq "exon") {
	    ($parent) = $fields[8] =~ /Parent=([^;]+)/;
	    if (!defined($parent) || $parent eq $last_transcript_id) {
		push @exons,"$fields[3] $fields[4]";
	    }

	} elsif ($fields[2] eq "CDS") {
	    ($cds_id) = $fields[8] =~ /ID=([^;]+)/;
	    if (!defined($cds_id)) {
		push @CDS_regions,"$fields[3] $fields[4]";
	    } else {		
		if ($cds_id ne $last_cds_id) {
		    if ($#CDS_regions > 0) {
			print_exons(\@CDS_regions,$gene_name,$cds_transcript_id,$chr,$strand);
		    }
		    @CDS_regions = (); # Need to clear single CDS regions
		}
		push @CDS_regions,"$fields[3] $fields[4]";
		$last_cds_id = $cds_id;
	    }
	    ($cds_transcript_id) = $fields[8] =~ /Parent=([^;]+)/;
	    if (!defined($cds_transcript_id)) {
		$cds_transcript_id = $last_transcript_id;
	    }
	}
    }
}

if ($#exons >= 0) {
    if ($strand eq "+") {
	($start,$end) = get_gene_bounds_plus(\@exons);
	printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
    } elsif ($strand eq "-") {
	($start,$end) = get_gene_bounds_minus(\@exons);
	printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
    } else {
	die "strand $strand";
    }
    print "$gene_name\n";
    print_exons(\@exons,$gene_name,$last_transcript_id,$chr,$strand);

}
@exons = ();
if ($#CDS_regions >= 0) {
    if ($strand eq "+") {
	($start,$end) = get_gene_bounds_plus(\@CDS_regions);
	printf ">$last_transcript_id $chr:%u..%u\n",$start,$end;
    } elsif ($strand eq "-") {
	($start,$end) = get_gene_bounds_minus(\@CDS_regions);
	printf ">$last_transcript_id $chr:%u..%u\n",$end,$start;
    } else {
	die "strand $strand";
    }
    print "$gene_name\n";
    print_exons(\@CDS_regions,$gene_name,$last_transcript_id,$chr,$strand);
}
@CDS_regions = ();


exit;


sub ascending_cmp {
    ($starta) = $a =~ /(\d+) \d+/;
    ($startb) = $b =~ /(\d+) \d+/;
    return $starta <=> $startb;
}

sub get_gene_bounds_plus {
    my ($exons) = @_;
    my ($start,$end);
    my @sorted;

    @sorted = sort ascending_cmp (@ {$exons});
    ($start) = $sorted[0] =~ /(\d+) \d+/;
    ($end) = $sorted[$#sorted] =~ /\d+ (\d+)/;
    return ($start,$end);
}

sub get_gene_bounds_minus {
    my ($exons) = @_;
    my ($start,$end);
    my @sorted;

    @sorted = reverse sort ascending_cmp (@ {$exons});
    ($end) = $sorted[0] =~ /\d+ (\d+)/;
    ($start) = $sorted[$#sorted] =~ /(\d+) \d+/;
    return ($start,$end);
}


sub get_exon_bounds_plus {
    my ($exons) = @_;
    my @starts = ();
    my @ends = ();

    foreach $exon (sort ascending_cmp (@ {$exons})) {
	($start,$end) = $exon =~ /(\d+) (\d+)/;
	push @starts,$start;
	push @ends,$end;
    }

    return (\@starts,\@ends);
}

sub get_exon_bounds_minus {
    my ($exons) = @_;
    my @starts = ();
    my @ends = ();

    foreach $exon (reverse sort ascending_cmp (@ {$exons})) {
	($start,$end) = $exon =~ /(\d+) (\d+)/;
	push @starts,$start;
	push @ends,$end;
    }

    return (\@starts,\@ends);
}

sub print_exons {
    my ($exons, $gene_name, $transcript_id, $chr, $strand) = @_;

    $nexons = $#{$exons} + 1;
    if ($strand eq "+") {
	($starts,$ends) = get_exon_bounds_plus($exons);
	for ($i = 0; $i < $nexons; $i++) {
	    printf "%u %u\n",$ {$starts}[$i],$ {$ends}[$i];
	}
    } elsif ($strand eq "-") {
	($starts,$ends) = get_exon_bounds_minus($exons);
	for ($i = 0; $i < $nexons; $i++) {
	    printf "%u %u\n",$ {$ends}[$i],$ {$starts}[$i];
	}
    }
    
    return;
}

