JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / PSIBLAST / PSSM.pm
1 package PSIBLAST::PSSM;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 use base qw(Root Read Write);
8
9 sub read {
10         my ($self, $fh) = @_;
11
12         local $/ = "\n";
13
14         my $xsum = 0;
15         while (my $line = <$fh>) {
16                 if ($line =~ /^\s{11}A  R  N  D  C  Q  E/) {
17                         while ($line = <$fh>) {
18                                 chomp;
19                                 last if $line =~ /^\s*$/;
20
21                                 my @pssm = split /\s+/, $line;
22
23                                 my @data;
24                                 for (@pssm[3..22]) {
25                                         push @data, sprintf "%2.8f", 1 / (1 + (exp(- $_)));
26                                         $xsum += $_;
27                                 }
28
29                                 $self->add_pos(@data);
30                         }
31                 }
32         }
33
34 }
35
36 sub add_pos {
37         my ($self, @data) = @_;
38
39         @data == 20 or croak "add_pos passed wrong length array";
40
41         push @{ $self->{__PACKAGE__."pos"} }, \@data;
42 }
43
44 {
45         my $i = 0;
46
47         sub get_pos {
48                 my ($self) = @_;
49
50                 if (defined (my $foo = $self->get_num_pos($i++))) {
51                         return $foo
52                 }
53                 else {
54                         $i = 0;
55                         return undef;
56                 }
57         }
58 }
59
60 sub seq {
61         my ($self, $data) = @_;
62
63         if ($data) {
64                 confess "Not passed ARRAY" unless ref $data eq 'ARRAY';
65                 $self->add_pos(@{ $data });
66         }
67         else {
68                 my $size = $self->get_pos_size - 1;
69                 $size or return ();
70                 return map { $self->get_num_pos($_) } 0..$size;
71         }
72 }
73
74 sub get_num_pos {
75         my ($self, $pos) = @_;
76         confess "No positions passed to get_num_pos" unless defined $pos;
77         return undef unless exists $self->{__PACKAGE__."pos"};
78         return ${ $self->{__PACKAGE__."pos"} }[$pos];
79 }
80
81 sub get_pos_size {
82         my ($self) = @_;
83         exists $self->{__PACKAGE__."pos"} ?
84                 return scalar @{ $self->{__PACKAGE__."pos"} } :
85                 0;
86 }
87
88 sub write {
89         my ($self, $fh) = @_;
90         while (my $pos = $self->get_pos) {
91                 print $fh join(" ", @{$pos}), "\n"
92         }
93 }
94
95 1;
96
97 __END__
98
99 =head1 NAME
100
101 PSIBLAST::PSSM - Read PSIBLAST PSSM files
102
103 =head1 EXAMPLE
104
105  my $pssm = PSIBLAST::PSSM->new;
106  $pssm->read_file("data.pssm");
107
108  for ($pssm->seq) {
109         print join(" ", @{$_}), "\n";
110  }
111
112 =head1 DESCRIPTION
113
114 Class for reading PSIBLAST PSSM files, subclasses the Root, Read and Write classes.
115
116 =head1 METHODS
117
118 =head2 my $pssm = PSIBLAST::PSSM->new()
119
120 Create the object. See Root module for details.
121
122 =head2 $pssm->read_file("path_to_file");
123
124 Read in a PSIBLAST PSSM file. See Read module for other methods.
125
126 =head2 $pssm->add_pos(@data);
127
128 Adds data from PSIBLAST PSSM.
129
130 =head2 $pssm->get_pos;
131
132 Itterator for returning the data for each position loaded in the object.
133
134 =head2 $pssm->write;
135
136 Prints the PSSM file for use by Jnet.
137
138 =head2 $pssm->seq([ @data ] );
139   $data = $pssm->seq;
140
141 Gah! Deprecated.
142
143 =head1 SEE ALSO
144
145 Root, Read and Write modules.
146
147 =head1 AUTHOR
148
149 Jonathan Barber <jon@compbio.dundee.ac.uk>