| 1 | package Bio::AutomatedAnnotation::ParseGenesFromGFF; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Parse a GFF file and efficiency extract the gene sequence. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Bio::Tools::GFF; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Bio::PrimarySeq; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Bio::SeqIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Bio::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | has 'gff_file' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 13 | 3 | has 'search_query' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | has '_awk_filter' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__awk_filter' ); | |||||||||||||||||||||||||||
| 16 | 3 | has '_gff_parser' => ( is => 'ro', isa => 'Bio::Tools::GFF', lazy => 1, builder => '_build__gff_parser' ); | |||||||||||||||||||||||||||
| 17 | 3 | has '_tags_to_filter' => ( is => 'ro', isa => 'Str', default => 'CDS' ); | |||||||||||||||||||||||||||
| 18 | 3 | has '_matching_features' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__matching_features' ); | |||||||||||||||||||||||||||
| 19 | 3 | has '_bio_seq_objects' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__bio_seq_objects' ); | |||||||||||||||||||||||||||
| 20 | 3 | has 'search_qualifiers' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_search_qualifiers' ); | |||||||||||||||||||||||||||
| 21 | 3 | has '_sequences' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__sequences' ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub _build_search_qualifiers | ||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_search_qualifiers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | return [ 'gene', 'product' ]; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub _build__sequences { | ||||||||||||||||||||||||||||
| 31 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__sequences: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 13 | my %seq_names_to_sequences; | |||||||||||||||||||||||||||
| 33 | 13 | my @sequences = $self->_gff_parser->get_seqs; | |||||||||||||||||||||||||||
| 34 | 13 | for my $sequence (@sequences) { | |||||||||||||||||||||||||||
| 35 | 13 | $seq_names_to_sequences{ $sequence->id } = $sequence; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 13 | return \%seq_names_to_sequences; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub _build__matching_features { | ||||||||||||||||||||||||||||
| 41 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__matching_features: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 42 | 20 | my @tag_names = @{$self->search_qualifiers}; | |||||||||||||||||||||||||||
| 43 | 20 | my @matching_features; | |||||||||||||||||||||||||||
| 44 | 20 | my $search_query = $self->search_query; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 20 | ▶ | while ( my $raw_feature = $self->_gff_parser->next_feature() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 55 | for my $tag_name (@tag_names) { | |||||||||||||||||||||||||||
| 48 | 92 | ▶ | if ( $raw_feature->has_tag($tag_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 48 | my @tag_values = $raw_feature->get_tag_values($tag_name); | |||||||||||||||||||||||||||
| 50 | 48 | my $values = join(',', @tag_values); | |||||||||||||||||||||||||||
| 51 | 48 | ▶ | if ( $values =~ /$search_query/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 26 | push( @matching_features, $raw_feature ); | |||||||||||||||||||||||||||
| 53 | 26 | last; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | #for my $tag_value (@tag_values) { | ||||||||||||||||||||||||||||
| 57 | # if ( $tag_value =~ /$search_query/ ) { | ||||||||||||||||||||||||||||
| 58 | # push( @matching_features, $raw_feature ); | ||||||||||||||||||||||||||||
| 59 | # last; | ||||||||||||||||||||||||||||
| 60 | # } | ||||||||||||||||||||||||||||
| 61 | #} | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 66 | ▶ | last if ( @matching_features > 0 && $raw_feature eq $matching_features[-1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@matching_features > 0 and $raw_feature eq $matching_features[-1]
MC/DC0%0/2
@matching_features > 0 and $raw_feature eq $matching_features[-1]
@matching_features > 0
$raw_feature eq $matching_features[-1]
Truth table66%2/3
A and B
A= @matching_features > 0B= $raw_feature eq $matching_features[-1]
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 20 | return \@matching_features; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub _build__gff_parser { | ||||||||||||||||||||||||||||
| 70 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__gff_parser: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 20 | open( my $fh, '-|', $self->_awk_filter." ".$self->gff_file ); | |||||||||||||||||||||||||||
| 72 | 20 | return Bio::Tools::GFF->new( -gff_version => 3, -fh => $fh, alphabet => 'dna'); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub _find_feature_id { | ||||||||||||||||||||||||||||
| 76 | 21 | ▶ | my ( $self, $feature ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_feature_id: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 77 | 21 | my $gene_id; | |||||||||||||||||||||||||||
| 78 | my @junk; | ||||||||||||||||||||||||||||
| 79 | 21 | my @tag_names = ( 'ID', 'locus_tag' ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 21 | for my $tag_name (@tag_names) { | |||||||||||||||||||||||||||
| 82 | 21 | ▶ | if ( $feature->has_tag($tag_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 21 | ( $gene_id, @junk ) = $feature->get_tag_values($tag_name); | |||||||||||||||||||||||||||
| 84 | 21 | return $gene_id; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | return $gene_id; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _build__bio_seq_objects { | ||||||||||||||||||||||||||||
| 91 | 19 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__bio_seq_objects: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 92 | 19 | my @bio_seq_objects; | |||||||||||||||||||||||||||
| 93 | 19 | ▶ | return \@bio_seq_objects if(!defined($self->_matching_features) || @{ $self->_matching_features } == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $self->_matching_features or @{$self->_matching_features;} == 0
MC/DC50%1/2
not defined $self->_matching_features or @{$self->_matching_features;} == 0
not defined $self->_matching_features
@{$self->_matching_features;} == 0
Truth table66%2/3
not(A) or B
A= not defined $self->_matching_featuresB= @{$self->_matching_features;} == 0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 13 | for my $feature ( @{ $self->_matching_features } ) { | |||||||||||||||||||||||||||
| 96 | 21 | my $sequence_name = join( '_', ( $feature->seq_id, $self->_find_feature_id($feature) ) ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 21 | my $feature_sequence = $self->_sequences->{ $feature->seq_id }->subseq( $feature->start, $feature->end ); | |||||||||||||||||||||||||||
| 99 | 21 | ▶ | if ( $feature->strand == -1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 3 | $feature_sequence = revcom($feature_sequence)->seq; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 21 | push( @bio_seq_objects, Bio::Seq->new( -display_id => $sequence_name, -seq => $feature_sequence ) ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 13 | return \@bio_seq_objects; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Parsing a GFF file with perl is slow, so filter out the bits we dont need first | ||||||||||||||||||||||||||||
| 108 | sub _build__awk_filter { | ||||||||||||||||||||||||||||
| 109 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__awk_filter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | return | ||||||||||||||||||||||||||||
| 111 | 20 | 'awk \'BEGIN {FS="\t"};{ IGNORECASE = 1; if ($3 ~/' | |||||||||||||||||||||||||||
| 112 | . $self->_tags_to_filter | ||||||||||||||||||||||||||||
| 113 | . '/ && $9 ~ /' | ||||||||||||||||||||||||||||
| 114 | . $self->search_query | ||||||||||||||||||||||||||||
| 115 | . '/) print $0;else if ($3 ~/' | ||||||||||||||||||||||||||||
| 116 | . $self->_tags_to_filter | ||||||||||||||||||||||||||||
| 117 | . '/) ; else print $0;}\' '; | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =pod | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 NAME | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Bio::AutomatedAnnotation::ParseGenesFromGFF - Parse a GFF file and efficiency extract the gene sequence. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 VERSION | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | version 1.182685 | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Automated annotation of assemblies. | ||||||||||||||||||||||||||||
| 142 | use Bio::AutomatedAnnotation::ParseGenesFromGFF; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $obj = Bio::AutomatedAnnotation::ParseGenesFromGFF->new( | ||||||||||||||||||||||||||||
| 145 | gff_file => 'abc.gff', | ||||||||||||||||||||||||||||
| 146 | search_query => 'mecA' | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | $obj->matching_features; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Andrew J. Page <ap13@sanger.ac.uk> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||