1#!/usr/local/bin/perl -W 2 3use strict; 4use Bio::Perl; 5use Bio::Tools::GFF; 6 7my $gffio = Bio::Tools::GFF->new(-fh => \*STDIN, -gff_version => 2); 8my $out = Bio::Tools::GFF->new(-fh => \*STDOUT, -gff_version => 2); 9my $feature; 10my @features = (); 11 12#Record entries 13while($feature = $gffio->next_feature()) { 14 push @features, $feature; 15} 16$gffio->close(); 17 18#Sort them 19my @sorted_features = sort { 20 if ($a->seq_id gt $b->seq_id) {return 1;} 21 elsif ($b->seq_id gt $a->seq_id) {return -1;} 22 else {return $a->start <=> $b->start;} 23} @features; 24 25#Merge redundant elements 26my $current_index = 0; 27my $next_index = 1; 28while ($next_index <= $#sorted_features) { 29 my $current_feature = $sorted_features[$current_index]; 30 my $next_feature = $sorted_features[$next_index]; 31 32 if ($next_feature->seq_id gt $current_feature->seq_id 33 || $next_feature->start > $current_feature->end) { 34 $current_index = $next_index; 35 $next_index++; 36 next; 37 } 38 39 if ($next_feature->end > $current_feature->end) { 40 $current_feature->end($next_feature->end); 41 } 42 43 undef $sorted_features[$next_index]; 44 $next_index++; 45} 46 47#Output 48my $index; 49for ($index = 0; $index < @sorted_features; $index++) { 50 if (defined $sorted_features[$index]) { 51 $out->write_feature($sorted_features[$index]); 52 } 53} 54 55