| Sanger Home | Acedb | YourGenome | Ensembl | Trace Server | Library |
|
|
| Data Release Policy | Conditions of Use | |
AcePerl is an excellent Perl module which provides access to ACeDB databases, and is written by Lincoln Stein, who also wrote CGI.pm.
AcePerl is an object-oriented Perl module, so
you need to be happy with object-oriented Perl to
use it. AcePerl can be used to access ACeDB
databases either by contacting an
aceserver directly, or by wrapping a
tace process opened on an ACeDB
database directory. Access through the server is
usually faster and more reliable.
There are two principal types of object in AcePerl, Database handle objects and Data objects.
These are connections to ACeDB databases. A
conection is made with the connect()
method. This example script opens a connection,
and stores it in the variable
$db:
#!/usr/local/bin/perl -w
use strict;
use Ace; # Import the AcePerl library
my $host = 'humsrv1';
my $port = 310000;
my $db = Ace->connect( -host => $host, -port => $port )
or die "Can't connect to '$host' on port '$port' : ", Ace->error;
Once you have a connection to the ACeDB database,
objects can be retrieved from it using the
fetch() method:
my $seq = $db->fetch(Sequence => 'bK390B3')
or die "Can't fetch sequence 'bK390B3'";
The object's data tree can be accessed with
the at() method. It is very helpful
to have xace open on the model or
example data at this stage!
my $author = $seq->at('Origin.From_Author[1]');
print "Author of $seq is $author\n";
This call fetches the tag one hop to the right
(specified by '[1]') of the
From_Author tag. The full path to
the tag through the object (ie: via
Origin) must be specified with the
at() method.
Everything which you get from a database with
AcePerl is an object. These objects
automatically convert themselves into a string
when called in a string context (in the print
statement above for example). What actually
happens internally is that the
name() method is called on the
object.
This can trap you when you store data
retrieved with AcePerl in your Perl script (in a
hash for example), because you can rapidly run
out of memory as you accumulate numbers of large
AcePerl objects. If, for instance, you stored
the $author object above, then what
you actually store is a full AcePerl object,
which knows which Class it belongs to and
contains a handle to its database.
To store just the string, you need to call
name() directly, or put it in string
context:
my( %seq_authors ); # For storing the author of each sequence
# These two lines are equivalent:
$seq_authors{$seq} = "$author";
$seq_authors{$seq} = $author->name;
# ($seq gets stringified because hash keys
# force string context.)
Called in array context, the at()
method returns a list of objects. The following
piece of code gets the column of sequence object
tags to the right of the Subsequence
tag, and then uses the row() method
to get the two tags one hop to the right:
my @sub_list = $seq->at('Structure.Subsequence[1]');
foreach my $subseq (@sub_list) {
my($left, $right) = $subseq->row(1);
print "Subsequence: $subseq\t$left\t$right\n";
}
| Last Modified Fri Oct 19 11:36:50 2001 | jgrg@sanger.ac.uk |