File: HMM.pm

package info (click to toggle)
psortb 3.0.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,044 kB
  • sloc: perl: 3,652; ansic: 698; cpp: 338; sh: 27; makefile: 23
file content (160 lines) | stat: -rw-r--r-- 3,395 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package Algorithm::HMM;

use strict;
use 5.006;
use Carp;

require DynaLoader;
require Exporter;
use AutoLoader;

our @ISA = qw(Exporter DynaLoader);

our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );

our $VERSION = '0.02';

sub AUTOLOAD {
  my $constname;
  our $AUTOLOAD;
  ($constname = $AUTOLOAD) =~ s/.*:://;
  croak "& not defined" if $constname eq 'constant';
  my $val = constant($constname, @_ ? $_[0] : 0);
  if ($! != 0) {
    if ($! =~ /Invalid/ || $!{EINVAL}) {
      $AutoLoader::AUTOLOAD = $AUTOLOAD;
      goto &AutoLoader::AUTOLOAD;
    }
    else {
      croak "Your vendor has not defined Algorithm::HMM macro $constname";
    }
  }
  {
    no strict 'refs';
    # Fixed between 5.005_53 and 5.005_61
    if ($] >= 5.00561) {
      *$AUTOLOAD = sub () { $val };
    }
    else {
      *$AUTOLOAD = sub { $val };
    }
  }
  goto &$AUTOLOAD;
}

bootstrap Algorithm::HMM $VERSION;

use Algorithm::HMM::Hit::Domain;
use Algorithm::HMM::Hit::Global;
use Algorithm::HMM::Report;

=head1 NAME

Algorithm::HMM - Perl bindings for a Hidden Markov Model library.

=head1 SYNOPSIS

  use Algorithm::HMM;

  # Load a Hidden Markov Model.
  my $hmm = new Algorithm::HMM(Model => 'sample.hmm');

  # Run a sequence through the loaded model.
  my $rep = $hmm->search("AAIELKBPOWELKQJPASDLKJIGE");

  # Get all the global hits from the search.
  my @ghits = $rep->global_hits();

  # Get all the domain hits from the search.
  my @dhits = $rep->domain_hits();

  # Display information about the domain hits.
  for (@dhits) {
    print("pvalue = " . $_->pvalue . "\n");
    print("evalue = " . $_->evalue . "\n");
    print("score  = " . $_->score  . "\n");
  }

  # Save the model to a different file.
  $hmm->save("sample.hmm.0");

  # Load the saved model again.
  $hmm->load("sample.hmm.1")

=cut

sub new {
  my ($class, %args) = @_;
  my $self = bless({ }, $class);

  my $model = $args{Model} || '';
  my $df    = ($args{DoForward} || 0) + 0;
  my $n2    = ($args{DoNull2} || 0) + 0;

  $self->{hmm} = hmm_new($model, $df, $n2);
  croak("Couldn't create new HMM") if(! $self->{hmm});

  return $self;
}

sub load {
  my ($self, $file) = @_;

  croak("Can't load model because no filename provided") if(! $file);

  return $self->{hmm}->_load($file);
}

sub save {
  my ($self, $file, %args) = @_;
  my ($append, $binary);

  croak("Can't save model because no filename provided") if(! $file);
  croak("No model currently loaded") if(! $self->{hmm}->_modelLoaded());

  $append = $args{Append} + 0 || 0;
  $binary = $args{Binary} + 0 || 0;

  return $self->{hmm}->_save($file, $append, $binary);
}

sub search {
  my ($self, $seq) = @_;
  my ($rep, @global, @domain);

  $rep = $self->{hmm}->_search($seq);
  croak("Failed to create Algorithm::HMM::Report") if(! defined($rep));

  return $rep;
}

sub forward {
  my $self = shift;

  (@_) ? _setDoForward($self->{hmm},shift(@_)+0) : _getDoForward($self->{hmm});
}

sub null2 {
  my $self = shift;

  (@_) ? _setDoNull2($self->{hmm},shift(@_)+0) : _getDoNull2($self->{hmm});
}

sub train {
  my ($self, @seqs) = @_;
  my $max;

  # Determine the maxium sequence length.
  for(@seqs) { $max = length($_) if($max < length($_)) }

  # Pad the sequences to the same length.
  $_ = $_ . "-" x ($max - length($_)) for(@seqs);

  return _train($self->{hmm}, @seqs);
}

1;

__END__