| 1 |
# $Id: Seq.pm,v 1.4 2005/01/25 06:17:28 cmungall Exp $ |
| 2 |
# |
| 3 |
# This GO module is maintained by Chris Mungall <[email protected]> |
| 4 |
# |
| 5 |
# see also - http://www.geneontology.org |
| 6 |
# - http://www.godatabase.org/dev |
| 7 |
# |
| 8 |
# You may distribute this module under the same terms as perl itself |
| 9 |
|
| 10 |
package GO::Model::Seq; |
| 11 |
|
| 12 |
=head1 NAME |
| 13 |
|
| 14 |
GO::Model::Seq; |
| 15 |
|
| 16 |
=head1 SYNOPSIS |
| 17 |
|
| 18 |
print $gene_product->seq->seq; |
| 19 |
|
| 20 |
=head1 DESCRIPTION |
| 21 |
|
| 22 |
represents a biological sequence; uses the bioperl Bio::PrimarySeq class |
| 23 |
|
| 24 |
any call that you can do on a bioperl sequence object, you can do |
| 25 |
here, with the addition of the calls below |
| 26 |
|
| 27 |
to get bioperl, see http://www.bioperl.org |
| 28 |
|
| 29 |
=cut |
| 30 |
|
| 31 |
use Carp; |
| 32 |
use Exporter; |
| 33 |
use GO::Utils qw(rearrange); |
| 34 |
use GO::Model::Root; |
| 35 |
use strict; |
| 36 |
use vars qw(@ISA $AUTOLOAD); |
| 37 |
|
| 38 |
@ISA = qw(GO::Model::Root Exporter); |
| 39 |
|
| 40 |
sub _valid_params { |
| 41 |
return qw(id xref_list pseq description); |
| 42 |
} |
| 43 |
|
| 44 |
sub _initialize |
| 45 |
{ |
| 46 |
my $self = shift; |
| 47 |
my ($paramh) = @_; |
| 48 |
|
| 49 |
my @bpargs = @_; |
| 50 |
my %h = (); |
| 51 |
if (ref($paramh) eq "HASH") { |
| 52 |
@bpargs = (); |
| 53 |
foreach my $k (keys %$paramh) { |
| 54 |
if (grep {$k eq $_} $self->_valid_params) { |
| 55 |
$h{$k} = $paramh->{$k}; |
| 56 |
} |
| 57 |
else { |
| 58 |
push(@bpargs, "-".$k, $paramh->{$k}); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
require "Bio/PrimarySeq.pm"; |
| 63 |
my $pseq = Bio::PrimarySeq->new(@bpargs); |
| 64 |
$self->pseq($pseq); |
| 65 |
$self->SUPER::_initialize(\%h); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
=head2 pseq |
| 70 |
|
| 71 |
Usage - |
| 72 |
Returns - Bio::PrimarySeq |
| 73 |
Args - |
| 74 |
|
| 75 |
=cut |
| 76 |
|
| 77 |
sub pseq { |
| 78 |
my $self = shift; |
| 79 |
if (@_) { |
| 80 |
$self->{pseq} = shift; |
| 81 |
if ($self->{pseq}->isa("Bio::Seq::RichSeqI")) { |
| 82 |
my $annot = $self->{pseq}->annotation; |
| 83 |
# foreach my $link ( $annot->each_DBLink ) { |
| 84 |
foreach my $link ( $annot->get_Annotations('dblink') ) { |
| 85 |
my $xref = |
| 86 |
GO::Model::Xref->new; |
| 87 |
$xref->xref_key($link->primary_id); |
| 88 |
$xref->xref_dbname($link->database); |
| 89 |
$self->add_xref($xref); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $self->{pseq}; |
| 94 |
} |
| 95 |
|
| 96 |
sub id { |
| 97 |
my $self = shift; |
| 98 |
$self->{id} = shift if @_; |
| 99 |
return $self->{id}; |
| 100 |
} |
| 101 |
|
| 102 |
sub residues {shift->pseq->seq(@_)} |
| 103 |
|
| 104 |
=head2 md5checksum |
| 105 |
|
| 106 |
Usage - my $md5 = $seq->md5checksum() OR $seq->md5checksum($md5) |
| 107 |
Returns - 32 char hex string |
| 108 |
Args - 32 char hex string [optional] |
| 109 |
|
| 110 |
checksum for seq - easy way to check if it has been changed etc |
| 111 |
|
| 112 |
(requires Digest::MD5 module from CPAN) |
| 113 |
|
| 114 |
=cut |
| 115 |
|
| 116 |
sub md5checksum { |
| 117 |
my $self = shift; |
| 118 |
|
| 119 |
# we want to be able to manipulte the checksum |
| 120 |
# even if the actual residues are not in memory at this time |
| 121 |
if (@_) { |
| 122 |
$self->{md5checksum} = shift; |
| 123 |
} |
| 124 |
my $res = $self->pseq->seq(); |
| 125 |
if (!$res) { |
| 126 |
return $self->{md5checksum}; |
| 127 |
} |
| 128 |
require "Digest/MD5.pm"; |
| 129 |
my $md5 = Digest::MD5->new; |
| 130 |
$md5->add($self->residues); |
| 131 |
my $hex = $md5->hexdigest; |
| 132 |
|
| 133 |
$self->{md5checksum} = $hex; |
| 134 |
|
| 135 |
return $self->{md5checksum}; |
| 136 |
} |
| 137 |
|
| 138 |
=head2 to_fasta |
| 139 |
|
| 140 |
Usage - |
| 141 |
Returns - |
| 142 |
Args - |
| 143 |
|
| 144 |
=cut |
| 145 |
|
| 146 |
sub to_fasta { |
| 147 |
my $self = shift; |
| 148 |
|
| 149 |
my $res = $self->seq; |
| 150 |
$res =~ s/(.{50})/$1\n/g; |
| 151 |
my $hdr = $self->description || $self->display_id; |
| 152 |
# my $hdr = $self->display_id; |
| 153 |
|
| 154 |
return |
| 155 |
sprintf(">%s\n%s\n", |
| 156 |
$hdr, |
| 157 |
$res); |
| 158 |
} |
| 159 |
|
| 160 |
=head2 add_xref |
| 161 |
|
| 162 |
- Usage : $term->add_xref($xref); |
| 163 |
- Args : GO::Term::Xref |
| 164 |
|
| 165 |
|
| 166 |
=cut |
| 167 |
|
| 168 |
sub add_xref { |
| 169 |
my $self = shift; |
| 170 |
|
| 171 |
if (@_) { |
| 172 |
my $xref = shift; |
| 173 |
$self->xref_list([]) unless $self->xref_list; |
| 174 |
$xref->isa("GO::Model::Xref") || confess("Not an Xref"); |
| 175 |
push(@{$self->xref_list}, $xref); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
# delegate calls to Bio::Seq object |
| 180 |
sub AUTOLOAD { |
| 181 |
|
| 182 |
my $self = shift || confess; |
| 183 |
|
| 184 |
my $name = $AUTOLOAD; |
| 185 |
$name =~ s/.*://; # strip fully-qualified portion |
| 186 |
|
| 187 |
if ($name eq "DESTROY") { |
| 188 |
# we dont want to propagate this!! |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if (!$self->pseq) { confess("assertion error") } |
| 193 |
|
| 194 |
if ($self->pseq->can($name)) { |
| 195 |
return $self->pseq->$name(@_); |
| 196 |
} |
| 197 |
if ($self->is_valid_param($name)) { |
| 198 |
|
| 199 |
$self->{$name} = shift if @_; |
| 200 |
return $self->{$name}; |
| 201 |
} |
| 202 |
else { |
| 203 |
confess("can't do $name on $self"); |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
1; |