This Wiki section is an edited version of Brian O’Connor’s presentation.
Turnkey is a package that auto-generates Web sites given a relational schema, based on SQL::Translator
Convert SQL Queries/Inserts/Deletes -> Object Calls
INSERT INTO feature (organism_id, name)
VALUES (1, 'foo');
To:
my $feature = Turnkey::Model::Feature->find_or_create({
organism_id => $organism,
name => 'xfile', uniquename => 'xfile',
type_id => $mrna_cvterm,
is_analysis => 'f', is_obsolete => 'f'
});
use base qw(Class::DBI::Pg);
my ($dsn, $name, $pass);
$dsn = "dbi:Pg:host=localhost;dbname=chado;port=5432";
$name = "postgres";
$pass = "";
Turnkey::Model::DBI->set_db('Main', $dsn, $name, $pass, {AutoCommit => 1});
package Turnkey::Model::Feature;
use base 'Turnkey::Model::DBI';
Turnkey::Model::Feature->set_up_table('feature');
#
# Primary key accessors
#
sub id { shift->feature_id }
sub feature { shift->feature_id }
#
# has_a
#
Turnkey::Model::Feature->has_a( type_id => "Turnkey::Model::Cvterm" );
sub cvterm { return shift->type_id; }
#
# has_many
#
Turnkey::Model::Feature->has_many('feature_synonym_feature_id',
'Turnkey::Model::Feature_Synonym' => 'feature_id');
sub feature_synonyms { return shift->feature_synonym_feature_id; }
Turnkey::Model::Feature->has_many('featureprop_feature_id',
'Turnkey::Model::Featureprop' => 'feature_id');
sub featureprops { return shift->featureprop_feature_id; }
# skip over feature_synonym table
#
# method 1
#
sub synonyms { my $self = shift; return map $_->synonym_id, $self->feature_synonyms; }
#
# method 2
#
Turnkey::Model::Feature->has_many( synonyms2 =>
['Turnkey::Model::Feature_Synonym' => 'synonym_id']);
sub do_transaction {
my $class = shift;
my ( $code ) = @_;
# Turn off AutoCommit for this scope.
# A commit will occur at the exit of this block automatically,
# when the local AutoCommit goes out of scope.
local $class->db_Main->{ AutoCommit };
# Execute the required code inside the transaction.
eval { $code->() };
if ( $@ ) {
my $commit_error = $@;
eval { $class->dbi_rollback }; # might also die!
die $commit_error;
}
}
Turnkey::Model::Feature->columns( Primary => qw/feature_id/ );
Turnkey::Model::Feature->columns( Essential => qw/name organism_id type_id/ );
Turnkey::Model::Feature->columns( Others => qw/residues .../ );
Typically:
Turnkey::Model::Feature->set_up_table('feature');
# now create mRNA feature
my $feature = Turnkey::Model::Feature->find_or_create({
organism_id => $organism,
name => 'xfile', uniquename => 'xfile',
type_id => $mrna_cvterm,
is_analysis => 'f', is_obsolete => 'f'
});
# create description
my $featureprop = Turnkey::Model::Featureprop->find_or_create({
value => 'A test gene for GMOD meeting',
feature_id => $feature,
type_id => $note_cvterm,
});
# objects for global use
# the organism for our new feature
my $organism = Turnkey::Model::Organism->search(abbreviation => "S.cerevisiae")->next;
# the cvterm for a "Note"
my $note_cvterm = Turnkey::Model::Cvterm->retrieve(2);
# searching name by wildcard
my @results = Turnkey::Model::Feature->search_like(name => 'x-%');
# update the xfile gene name
$feature->name("x-file");
$feature->update();
# now delete the x-file feature
$feature->delete();
Note that speed is relative: one can find bad performance using the wrong SQL and Chado%253A%253AAutoDBI approach will be speedier.