JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Profile.pm
1 package Profile;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 use base qw(Root Read Write);
8
9 =head1 NAME
10
11 Profile - Base object for various types of profile
12
13 =head1 SYNOPSIS
14
15   use Profile;
16
17   my $prof = Profile->new;
18
19 =head1 DESCRIPTION
20
21 Basic methods for storing information sequence profiles. The model of this object is for each sequence position to have an array of data associated with it.
22
23 =head1 METHODS
24
25 Inherits from Root, Read and Write.
26
27 =head2 $prof->add_pos(@data)
28
29 Add data to the end position of the profile.
30
31 =cut
32
33 sub add_pos {
34         my $self = shift;
35         @_ or croak "No data passed to ".__PACKAGE__."\n";
36         push @{ $self->{__PACKAGE__."lines"} }, pack "d*", @_;
37 }
38
39 sub get_aoa {
40         my ($self) = @_;
41         map { [ $self->get_pos ] } 0.. $self->get_size - 1;
42 }
43
44 =head2 $prof->add_line(@data)
45
46 Alias for add_pos().
47
48 =cut
49
50 *add_line = *add_pos;
51
52 =head2 @data $prof->get_pos
53
54 Itterator for getting all the positions in a profile. Returns the same information as get_num_pos, or undef when it reaches the end of the profile. When this happens, a new call starts at the begining of the profile.
55
56 =cut
57
58 sub get_pos {
59         my ($self) = @_;
60         $self->{__PACKAGE__."counter"} ||= 0;
61
62         if ((my @foo = $self->get_num_pos( $self->{__PACKAGE__."counter"}++ )) > 0) {
63                 return @foo;
64         }
65         else {
66                 $self->{__PACKAGE__."counter"} = 0;
67                 return undef;
68         }
69 }
70
71 =head2 @data = $prof->get_num_pos($position)
72
73 Access the profile information for a particular position in the profile.
74
75 =cut
76
77 sub get_num_pos {
78         my ($self, $pos) = @_;
79
80         confess "get_num_pos didn't receive position" unless defined $pos;
81
82         #use Data::Dumper;
83         #print $pos, "\n";
84         #print Dumper unpack "d*", @{ $self->{__PACKAGE__."lines"}->[$pos]};
85
86         confess "No positions present" unless exists $self->{__PACKAGE__."lines"};
87         return undef unless defined $self->{__PACKAGE__."lines"}->[$pos];
88         return unpack "d*", $self->{__PACKAGE__."lines"}->[$pos];
89 }
90
91 =head2 $prof->get_size
92
93 Returns the number of positions in the profile.
94
95 =cut
96
97 sub get_size {
98         my ($self) = @_;
99         return @{ $self->{__PACKAGE__."lines"} };
100 }
101
102 =head2 $prof->get_line($position)
103
104 Alias for get_num_pos().
105
106 =cut
107
108 *get_line = *get_num_pos;
109
110 1;