Skip to content

Instantly share code, notes, and snippets.

@hryk
Created April 20, 2011 09:29
Show Gist options
  • Select an option

  • Save hryk/930826 to your computer and use it in GitHub Desktop.

Select an option

Save hryk/930826 to your computer and use it in GitHub Desktop.
Bio::Tools::Protscale
package Bio::Tools::Protscale;
use strict;
use warnings;
use base qw(Bio::Root::Root);
use LWP 5.64;
use HTML::TreeBuilder::XPath;
use Data::Dumper;
=head1 NAME
Bio::Tools::Protscale
=head1 SYNOPSYS
my $chart = Bio::Tools::Protscale->new(seq => $seq);
$chart->result('gif');
$chart->result_to_file('gif', 'path/to/file.gif');
=head1 METHOD
=head2 new(seq => Bio::SeqI)
=head2 result(FORMAT)
=head3 result_to_file(FORMAT, PATH)
=head1 AUTHOR
hryk
=head1 SEE ALSO
=cut
sub new {
my ($class, @args) = @_;
@args = (
'-base' => 'http://www.expasy.org',
'-url' => '/cgi-bin/protscale.pl?1',
'-form' => 'sequence',
'-scale' => 'Hphob. / Kyte & Doolittle',
'-window' => 3,
'-weight_variation' => 'linear',
'-normalize' => 0,
@args
);
my $self = $class->SUPER::new(@args);
my ($base, $url, $seq, $form,
$scale, $window, $weight_variation,
$normalize ) = $self->_rearrange([
qw/BASE URL SEQ FORM SCALE WINDOW WEIGHT_VARIATION NORMALIZE/],
@args);
$self->throw("Invalid argument : window size must be an odd number between 3 and 21") if ($window % 2 == 0) or ($window < 3);
$self->throw("Invalid argument : 'normalize' should be 0 or 1.") unless $normalize == 0 or $normalize == 1;
bless $self, $class;
$self->_ua(LWP::UserAgent->new());
$self->_base_uri($base);
my $res;
$res = $self->_ua->post($self->_base_uri.$url,
[
$form => $seq,
window => $window,
scale => $scale,
weight_var => $weight_variation,
norm => $normalize
],
'User-Agent' => 'Mozilla/4.76 [en] (Win2000; U)',
);
$self->throw("$url error: ".$res->status_line) unless $res->is_success;
$self->throw("Bad content type at $url ".$res->content_type) unless $res->content_type eq 'text/html';
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse($res->content);
$self->_tree($tree);
return $self;
}
sub chart {
my $self = shift;
my $nodes = $self->_tree->findnodes('//img[@alt="ProtScale graph"]');
if ($nodes->size > 0) {
my $src = $nodes->[0]->attr('src');
my $res = $self->_ua->get($self->_base_uri.$src);
if ($res->is_success) {
$self->_image($res->content);
return $self->_image;
}
else {
$self->throw($res->status_line);
}
}
else {
$self->throw('Couldnot find chart image tag.');
}
}
sub chart_to_file {
my $self = shift;
my $filename = shift;
$self->chart();
unless (defined $filename) {
$self->throw('Empty filename.');
}
open my $fh, ">$filename" || $self->throw("Cannt open file $filename: $!");
$fh->print($self->_image);
$fh->close;
}
sub _image {
my $self = shift;
if (scalar(@_) > 0) {
$self->{_image} = shift;
}
return $self->{_image};
}
sub _ua {
my $self = shift;
if (scalar(@_) > 0) {
$self->{_ua} = shift;
}
return $self->{_ua};
}
sub _tree {
my $self = shift;
if (scalar(@_) > 0) {
$self->{_tree} = shift;
}
return $self->{_tree};
}
sub _base_uri {
my $self = shift;
if (scalar(@_) > 0) {
$self->{_base_uri} = shift;
}
return $self->{_base_uri};
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment