« blib/lib/Bio/AutomatedAnnotation/ParseGenesFromGFFs.pm
↑ Summary
lib/Bio/AutomatedAnnotation/CommandLine/ParseGenesFromGFFs.pm »
| 1 | package Bio::AutomatedAnnotation::ParseGenesFromGFF; | ||
| 2 | |||
| 3 | # ABSTRACT: Parse a GFF file and efficiency extract the gene sequence. | ||
| 4 | |||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use Bio::Tools::GFF; | ||
| 8 | use Bio::PrimarySeq; | ||
| 9 | use Bio::SeqIO; | ||
| 10 | use Bio::Perl; | ||
| 11 | |||
| 12 | has 'gff_file' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 13 | has 'search_query' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 14 | |||
| 15 | has '_awk_filter' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__awk_filter' ); | ||
| 16 | has '_gff_parser' => ( is => 'ro', isa => 'Bio::Tools::GFF', lazy => 1, builder => '_build__gff_parser' ); | ||
| 17 | has '_tags_to_filter' => ( is => 'ro', isa => 'Str', default => 'CDS' ); | ||
| 18 | has '_matching_features' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__matching_features' ); | ||
| 19 | has '_bio_seq_objects' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__bio_seq_objects' ); | ||
| 20 | has 'search_qualifiers' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_search_qualifiers' ); | ||
| 21 | has '_sequences' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__sequences' ); | ||
| 22 | |||
| 23 | |||
| 24 | sub _build_search_qualifiers | ||
| 25 | { | ||
| 26 | my ($self) = @_; | ||
| 27 | return [ 'gene', 'product' ]; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _build__sequences { | ||
| 31 | my ($self) = @_; | ||
| 32 | my %seq_names_to_sequences; | ||
| 33 | my @sequences = $self->_gff_parser->get_seqs; | ||
| 34 | for my $sequence (@sequences) { | ||
| 35 | $seq_names_to_sequences{ $sequence->id } = $sequence; | ||
| 36 | } | ||
| 37 | return \%seq_names_to_sequences; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _build__matching_features { | ||
| 41 | my ($self) = @_; | ||
| 42 | my @tag_names = @{$self->search_qualifiers}; | ||
| 43 | my @matching_features; | ||
| 44 | my $search_query = $self->search_query; | ||
| 45 | |||
| 46 | while ( my $raw_feature = $self->_gff_parser->next_feature() ) { | ||
| 47 | for my $tag_name (@tag_names) { | ||
| 48 | if ( $raw_feature->has_tag($tag_name) ) { | ||
| 49 | my @tag_values = $raw_feature->get_tag_values($tag_name); | ||
| 50 | my $values = join(',', @tag_values); | ||
| 51 | if ( $values =~ /$search_query/ ){ | ||
| 52 | push( @matching_features, $raw_feature ); | ||
| 53 | 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 | last if ( @matching_features > 0 && $raw_feature eq $matching_features[-1] ); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | return \@matching_features; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _build__gff_parser { | ||
| 70 | my ($self) = @_; | ||
| 71 | open( my $fh, '-|', $self->_awk_filter." ".$self->gff_file ); | ||
| 72 | return Bio::Tools::GFF->new( -gff_version => 3, -fh => $fh, alphabet => 'dna'); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _find_feature_id { | ||
| 76 | my ( $self, $feature ) = @_; | ||
| 77 | my $gene_id; | ||
| 78 | my @junk; | ||
| 79 | my @tag_names = ( 'ID', 'locus_tag' ); | ||
| 80 | |||
| 81 | for my $tag_name (@tag_names) { | ||
| 82 | if ( $feature->has_tag($tag_name) ) { | ||
| 83 | ( $gene_id, @junk ) = $feature->get_tag_values($tag_name); | ||
| 84 | return $gene_id; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | return $gene_id; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _build__bio_seq_objects { | ||
| 91 | my ($self) = @_; | ||
| 92 | my @bio_seq_objects; | ||
| 93 | return \@bio_seq_objects if(!defined($self->_matching_features) || @{ $self->_matching_features } == 0); | ||
| 94 | |||
| 95 | for my $feature ( @{ $self->_matching_features } ) { | ||
| 96 | my $sequence_name = join( '_', ( $feature->seq_id, $self->_find_feature_id($feature) ) ); | ||
| 97 | |||
| 98 | my $feature_sequence = $self->_sequences->{ $feature->seq_id }->subseq( $feature->start, $feature->end ); | ||
| 99 | if ( $feature->strand == -1 ) { | ||
| 100 | $feature_sequence = revcom($feature_sequence)->seq; | ||
| 101 | } | ||
| 102 | push( @bio_seq_objects, Bio::Seq->new( -display_id => $sequence_name, -seq => $feature_sequence ) ); | ||
| 103 | } | ||
| 104 | 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 | my ($self) = @_; | ||
| 110 | return | ||
| 111 | '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 | no Moose; | ||
| 121 | __PACKAGE__->meta->make_immutable; | ||
| 122 | |||
| 123 | 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.182680 | ||
| 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 |