----bioajax.cgi----
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI qw(:standard); # then only CGI.pm defines a head()
use CGI::Carp 'fatalsToBrowser';
use CGI::Ajax;
use IO::String;
use Bio::SeqIO;
use Bio::DB::GenBank;
my $cgi = CGI->new();
my $ajax = CGI::Ajax->new( get_sequence => \&get_sequence);
print $ajax->build_html( $cgi, \&main,{-expires=>'now'});
sub get_sequence{
my ( $id ) = @_;
my $str;
my $io = IO::String->new(\$str);
my $genBank = new Bio::DB::GenBank;
my $seq = $genBank->get_Seq_by_id($id);
my $seqOut = Bio::SeqIO->new(-format => 'fasta',-fh => $io );
$seqOut->write_seq($seq);
return $str;
}
sub main {
my $html = <
Bioinformatics CGI-Ajax Example
Bioinformatics CGI-Ajax Example
HTML
my $url = $cgi->url(-relative => 1);
$html .= <
HTML
return $html;
}
exit 0;
__END__
------------------------------------------------------------
-------------bioajax.js------------------------------------
// Run code when the page loads. From
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
// Set up functions to run when events occur.
function installHandlers() {
if (!document.getElementById) return;
var idbox = document.getElementById('fetch');
var tarea = document.getElementById('sequence');
if (idbox) {
// When the user leaves this element, call the server.
idbox.onclick = function() {
document.getElementById('message').innerHTML = 'Fetching Sequence...';
document.getElementById('message').style.visibility = 'visible';
get_sequence(['gi'], ['sequence']);
document.getElementById('sequence').style.backgroundColor = '#FFBE4A';
setTimeout("document.getElementById('message').style.visibility = 'hidden';",2000);
return false; // Continue with default action.
}
}
}
addLoadEvent( installHandlers );
--------------------------------------------------------