| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SearchIO::hmmer | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Kai Blin <kai.blin@biotech.uni-tuebingen.de> | ||
| 7 | # | ||
| 8 | # Copyright Kai Blin | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | # | ||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::SearchIO::hmmer - A parser for HMMER2 and HMMER3 output (hmmscan, hmmsearch, hmmpfam) | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # do not use this class directly it is available through FAST::Bio::SearchIO | ||
| 21 | use FAST::Bio::SearchIO; | ||
| 22 | my $in = FAST::Bio::SearchIO->new(-format => 'hmmer', | ||
| 23 | -file => 't/data/L77119.hmmer'); | ||
| 24 | while( my $result = $in->next_result ) { | ||
| 25 | # this is a FAST::Bio::Search::Result::HMMERResult object | ||
| 26 | print $result->query_name(), " for HMM ", $result->hmm_name(), "\n"; | ||
| 27 | while( my $hit = $result->next_hit ) { | ||
| 28 | print $hit->name(), "\n"; | ||
| 29 | while( my $hsp = $hit->next_hsp ) { | ||
| 30 | print "length is ", $hsp->length(), "\n"; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | This object implements a parser for HMMER output. It works with both HMMER2 and HMMER3 | ||
| 38 | |||
| 39 | =head1 FEEDBACK | ||
| 40 | |||
| 41 | =head2 Mailing Lists | ||
| 42 | |||
| 43 | User feedback is an integral part of the evolution of this and other | ||
| 44 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 45 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 46 | |||
| 47 | bioperl-l@bioperl.org - General discussion | ||
| 48 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 49 | |||
| 50 | =head2 Support | ||
| 51 | |||
| 52 | Please direct usage questions or support issues to the mailing list: | ||
| 53 | |||
| 54 | I<bioperl-l@bioperl.org> | ||
| 55 | |||
| 56 | rather than to the module maintainer directly. Many experienced and | ||
| 57 | reponsive experts will be able look at the problem and quickly | ||
| 58 | address it. Please include a thorough description of the problem | ||
| 59 | with code and data examples if at all possible. | ||
| 60 | |||
| 61 | =head2 Reporting Bugs | ||
| 62 | |||
| 63 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 64 | of the bugs and their resolution. Bug reports can be submitted via the | ||
| 65 | web: | ||
| 66 | |||
| 67 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 68 | |||
| 69 | =head1 AUTHOR - Kai Blin | ||
| 70 | |||
| 71 | Email kai.blin-at-biotech.uni-tuebingen.de | ||
| 72 | |||
| 73 | =head1 APPENDIX | ||
| 74 | |||
| 75 | The rest of the documentation details each of the object methods. | ||
| 76 | Internal methods are usually preceded with a _ | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | # Let the code begin... | ||
| 81 | |||
| 82 | package FAST::Bio::SearchIO::hmmer; | ||
| 83 | |||
| 84 | use strict; | ||
| 85 | |||
| 86 | use FAST::Bio::Factory::ObjectFactory; | ||
| 87 | |||
| 88 | use base qw(FAST::Bio::SearchIO); | ||
| 89 | |||
| 90 | sub new { | ||
| 91 | my ( $caller, @args ) = @_; | ||
| 92 | my $class = ref($caller) || $caller; | ||
| 93 | |||
| 94 | if( $class =~ /FAST::Bio::SearchIO::hmmer(\d)/ ) { | ||
| 95 | my ($self) = $class->SUPER::new(@args); | ||
| 96 | $self->_initialize(@args); | ||
| 97 | return $self; | ||
| 98 | } else { | ||
| 99 | # Try to guess the hmmer format version if it's not specified. | ||
| 100 | my $version; | ||
| 101 | my $fh; | ||
| 102 | my $fh_needs_close = 0; | ||
| 103 | my %param = @args; | ||
| 104 | |||
| 105 | @param{ map { lc $_ } keys %param } = values %param; # lowercase keys | ||
| 106 | |||
| 107 | # If the caller specified a version, go for that | ||
| 108 | if (defined($param{"-version"})) { | ||
| 109 | $version = $param{"-version"}; | ||
| 110 | } else { | ||
| 111 | # Check the contents of the file for the hmmer version mentioned in there | ||
| 112 | if (defined($param{"-fh"})) { | ||
| 113 | $fh = $param{"-fh"}; | ||
| 114 | } elsif (defined($param{"-file"})) { | ||
| 115 | open($fh, $param{"-file"}) || return; | ||
| 116 | $fh_needs_close = 1; | ||
| 117 | } | ||
| 118 | |||
| 119 | # read second line of the file | ||
| 120 | <$fh>; | ||
| 121 | $_ = <$fh>; | ||
| 122 | |||
| 123 | if ( m/HMMER\s3/ ) { | ||
| 124 | $version = "3"; | ||
| 125 | } else { | ||
| 126 | $version = "2"; | ||
| 127 | } | ||
| 128 | |||
| 129 | if ($fh_needs_close) { | ||
| 130 | close($fh); | ||
| 131 | } else { | ||
| 132 | seek($fh, 0, 0); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | my $format = "hmmer$version"; | ||
| 137 | return unless( $class->_load_format_module($format) ); | ||
| 138 | return "FAST::Bio::SearchIO::${format}"->new(@args); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _initialize { | ||
| 143 | my ( $self, @args ) = @_; | ||
| 144 | $self->SUPER::_initialize(@args); | ||
| 145 | my $handler = $self->_eventHandler; | ||
| 146 | $handler->register_factory( | ||
| 147 | 'result', | ||
| 148 | FAST::Bio::Factory::ObjectFactory->new( | ||
| 149 | -type => 'FAST::Bio::Search::Result::HMMERResult', | ||
| 150 | -interface => 'FAST::Bio::Search::Result::ResultI' | ||
| 151 | ) | ||
| 152 | ); | ||
| 153 | |||
| 154 | $handler->register_factory( | ||
| 155 | 'hit', | ||
| 156 | FAST::Bio::Factory::ObjectFactory->new( | ||
| 157 | -type => 'FAST::Bio::Search::Hit::HMMERHit', | ||
| 158 | -interface => 'FAST::Bio::Search::Hit::HitI' | ||
| 159 | ) | ||
| 160 | ); | ||
| 161 | |||
| 162 | $handler->register_factory( | ||
| 163 | 'hsp', | ||
| 164 | FAST::Bio::Factory::ObjectFactory->new( | ||
| 165 | -type => 'FAST::Bio::Search::HSP::HMMERHSP', | ||
| 166 | -interface => 'FAST::Bio::Search::HSP::HSPI' | ||
| 167 | ) | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; |