如何从Perl中的GFF3文件获取范围内的所有功能?

时间:2010-09-08 14:33:54

标签: perl parsing bioperl

我想编写一个获取GFF3文件名和范围(即100000 ... 2000000)的Perl函数。并返回对包含该范围内所有基因名称/种质的数组的引用。

我想使用bioperl会有意义,但我对它的经验很少。我可以写一个用我自己解析GFF3的脚本,但是如果使用bioperl(或其他packagae)并不太复杂 - 我宁愿重用它们的代码。

3 个答案:

答案 0 :(得分:3)

use Bio::Tools::GFF;

my $range_start = 100000;
my $range_end   = 200000;

my @features_in_range = ( );


my $gffio = Bio::Tools::GFF->new(-file => $gff_file, -gff_version => 3);

while (my $feature = $gffio->next_feature()) {

    ## What about features that are not contained within the coordinate range but
    ## do overlap it?  Such features won't be caught by this check.            
    if (
        ($feature->start() >= $range_start)
        &&
        ($feature->end()   <= $range_end)
       ) {

        push @features_in_range, $feature;

    }

}

$gffio->close();

免责声明:天真的实施。我刚刚说出来,它没有测试。我甚至不保证它会编译。

答案 1 :(得分:2)

您确实希望使用BioPerl,可能使用Bio::Tools::GFF模块。

你应该问BioPerl mailing list。这是非常友好的,订阅者知识渊博 - 他们肯定能够帮助你。一旦你得到答案(如果你没有得到答案),我建议在这里回答你自己的问题,这样我们都可以受益!

答案 2 :(得分:0)

以下函数采用目标和范围的哈希值,并返回一个函数,该函数将遍历与任何范围重叠的所有目标。目标应该是对引用数组的引用:

my $targets =    
[
  [
    $start,
    $end,
  ],
  ...,
]

范围应该是对哈希数组的引用:

my $ranges =
[
  {
    seqname   => $seqname,
    source    => $source,
    feature   => $feature,
    start     => $start,
    end       => $end,
    score     => $score,
    strand    => $strand,
    frame     => $frame,
    attribute => $attribute,
  },
  ...,
]

当然,您只需传递一个目标。

my $brs_iterator
= binary_range_search( targets => $targets, ranges => $ranges );

while ( my $gff_line = $brs_iterator->() ) {
   # do stuff
}

sub binary_range_search {
    my %options = @_;

    my $targets = $options{targets}  || croak 'Need a targets parameter';
    my $ranges  = $options{ranges} || croak 'Need a ranges parameter';

    my ( $low, $high ) = ( 0, $#{$ranges} );
    my @iterators = ();

TARGET:
    for my $range (@$targets) {

    RANGE_CHECK:
        while ( $low <= $high ) {

            my $try = int( ( $low + $high ) / 2 );

            $low = $try + 1, next RANGE_CHECK
                if $ranges->[$try]{end} < $range->[0];
            $high = $try - 1, next RANGE_CHECK
                if $ranges->[$try]{start} > $range->[1];

            my ( $down, $up ) = ($try) x 2;
            my %seen = ();

            my $brs_iterator = sub {

                if (    $ranges->[ $up + 1 ]{end} >= $range->[0]
                    and $ranges->[ $up + 1 ]{start} <= $range->[1]
                    and !exists $seen{ $up + 1 } )
                {
                    $seen{ $up + 1 } = undef;
                    return $ranges->[ ++$up ];
                }
                elsif ( $ranges->[ $down - 1 ]{end} >= $range->[0]
                    and $ranges->[ $down - 1 ]{start} <= $range->[1]
                    and !exists $seen{ $down - 1 }
                    and $down > 0 )
                {
                    $seen{ $down - 1 } = undef;
                    return $ranges->[ --$down ];
                }
                elsif ( !exists $seen{$try} ) {
                    $seen{$try} = undef;
                    return $ranges->[$try];
                }
                else {
                    return;
                }
            };
            push @iterators, $brs_iterator;
            next TARGET;
        }
    }

# In scalar context return master iterator that iterates over the list of range iterators.
# In list context returns a list of range iterators.
    return wantarray
        ? @iterators
        : sub {
        while (@iterators) {
            if ( my $range = $iterators[0]->() ) {
                return $range;
            }
            shift @iterators;
        }
        return;
        };
}