« blib/lib/Bio/AutomatedAnnotation/External/Cmscan.pm
↑ Summary
blib/lib/Bio/AutomatedAnnotation/CommandLine/GeneNameOccurances.pm »
| 1 | package Bio::AutomatedAnnotation::GeneNamesFromGFF; | ||
| 2 | |||
| 3 | # ABSTRACT: Parse a GFF and efficiently extract out the Gene Names | ||
| 4 | |||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use Bio::Tools::GFF; | ||
| 8 | |||
| 9 | has 'gff_file' => ( is => 'ro', isa => 'Str', required => 1 ); | ||
| 10 | |||
| 11 | has '_tags_to_filter' => ( is => 'ro', isa => 'Str', default => 'CDS' ); | ||
| 12 | has '_tags_to_ignore' => ( is => 'ro', isa => 'Str', default => 'rRNA|tRNA|ncRNA|tmRNA' ); | ||
| 13 | has '_gff_parser' => ( is => 'ro', isa => 'Bio::Tools::GFF', lazy => 1, builder => '_build__gff_parser' ); | ||
| 14 | has '_awk_filter' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__awk_filter' ); | ||
| 15 | has '_remove_sequence_filter' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__remove_sequence_filter' ); | ||
| 16 | |||
| 17 | has 'gene_names' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_gene_names' ); | ||
| 18 | |||
| 19 | sub _build_gene_names { | ||
| 20 | my ($self) = @_; | ||
| 21 | my %gene_names; | ||
| 22 | |||
| 23 | while ( my $raw_feature = $self->_gff_parser->next_feature() ) { | ||
| 24 | last unless defined($raw_feature); # No more features | ||
| 25 | next if !( $raw_feature->primary_tag eq 'CDS' ); | ||
| 26 | |||
| 27 | if ( $raw_feature->has_tag('gene') ) { | ||
| 28 | my ( $gene_name, @junk ) = $raw_feature->get_tag_values('gene'); | ||
| 29 | $gene_name =~ s!"!!g; | ||
| 30 | next if ( $gene_name eq "" ); | ||
| 31 | $gene_names{$gene_name} = 1; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | $self->_gff_parser->close(); | ||
| 35 | return \%gene_names; | ||
| 36 | } | ||
| 37 | |||
| 38 | # Bio::Tools::GFF->ignore_sequence(1) doesnt work with our data, triggers an infinite loop | ||
| 39 | sub _build__gff_parser { | ||
| 40 | my ($self) = @_; | ||
| 41 | open( my $fh, '-|', $self->_gff_fh_input_string ) or die "Couldnt open GFF file"; | ||
| 42 | my $gff_parser = Bio::Tools::GFF->new( -fh => $fh, gff_version => 3 ); | ||
| 43 | return $gff_parser; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _gff_fh_input_string { | ||
| 47 | my ($self) = @_; | ||
| 48 | return $self->_awk_filter . " " . $self->gff_file . " | " . $self->_remove_sequence_filter; | ||
| 49 | } | ||
| 50 | |||
| 51 | # Parsing a GFF file with perl is slow, so filter out CDSs which dont contain a gene name | ||
| 52 | sub _build__awk_filter { | ||
| 53 | my ($self) = @_; | ||
| 54 | return | ||
| 55 | 'awk \'BEGIN {FS="\t"};{ if ($3 ~/' | ||
| 56 | . $self->_tags_to_filter | ||
| 57 | . '/ && $9 ~ /gene=/) print $0;else if ($3 ~/' | ||
| 58 | . $self->_tags_to_filter . '|' | ||
| 59 | . $self->_tags_to_ignore | ||
| 60 | . '/) ; else print $0;}\' '; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Cut out the FASTA sequence at the bottom of the file | ||
| 64 | sub _build__remove_sequence_filter { | ||
| 65 | my ($self) = @_; | ||
| 66 | return 'sed -n \'/##gff-version 3/,/##FASTA/p\' | grep -v \'##FASTA\''; | ||
| 67 | } | ||
| 68 | |||
| 69 | no Moose; | ||
| 70 | __PACKAGE__->meta->make_immutable; | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ | ||
| 75 | |||
| 76 | =pod | ||
| 77 | |||
| 78 | =encoding UTF-8 | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | Bio::AutomatedAnnotation::GeneNamesFromGFF - Parse a GFF and efficiently extract out the Gene Names | ||
| 83 | |||
| 84 | =head1 VERSION | ||
| 85 | |||
| 86 | version 1.182685 | ||
| 87 | |||
| 88 | =head1 SYNOPSIS | ||
| 89 | |||
| 90 | Parse a GFF and efficiently extract out the Gene Names | ||
| 91 | use Bio::AutomatedAnnotation::GeneNamesFromGFF; | ||
| 92 | |||
| 93 | my $obj = Bio::AutomatedAnnotation::GeneNamesFromGFF->new( | ||
| 94 | gff_file => 'abc.gff' | ||
| 95 | ); | ||
| 96 | $obj->gene_names; | ||
| 97 | |||
| 98 | =head1 AUTHOR | ||
| 99 | |||
| 100 | Andrew J. Page <ap13@sanger.ac.uk> | ||
| 101 | |||
| 102 | =head1 COPYRIGHT AND LICENSE | ||
| 103 | |||
| 104 | This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute. | ||
| 105 | |||
| 106 | This is free software, licensed under: | ||
| 107 | |||
| 108 | The GNU General Public License, Version 3, June 2007 | ||
| 109 | |||
| 110 | =cut |