Bio::DB::BioSQL::Pg PathAdaptorDriver
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Bio::DB::BioSQL::Pg::PathAdaptorDriver - DESCRIPTION of Object
Package variables
No package variables defined.
Included modules
Give standard usage here
Describe the object here
Methods description
compute_transitive_closurecode    nextTop
 Title   : compute_transitive_closure
Usage :
Function: Compute the transitive closure over a given ontology
and populate the respective path table in the relational
Example : Returns : TRUE on success, and FALSE otherwise Args : The calling adaptor. The ontology to compute the transitive closure over (a persistent Bio::Ontology::OntologyI compliant object). The predicate indicating identity between a term and itself, to be used as the predicate for the paths of distance zero between a term and itself. If undef the zero distance paths will not be created. A scalar that if evaluating to TRUE means truncate the paths for the respective ontology first (optional; even though the default is FALSE this parameter should usually be given as TRUE unless you know what you are doing).
Methods code
sub compute_transitive_closure {
    my ($self,$adp,$ont,$idpred,$trunc) = @_;
    # if we only got three arguments and the third is not a term then we
# probably got called in the old signature
if ((! ref($idpred)) && (@_ == 4)) { $trunc = $idpred; $idpred = undef; } # the ontology needs to be persistent (we'll need its primary key)
if(! $ont->isa("Bio::DB::PersistentObjectI")) { $self->throw("$ont is not a persistent object. Bummer."); } # and obviously needs to have a primary key
if(! ($ont->primary_key || ($ont = $ont->adaptor->find_by_unique_key($ont)) || ($ont = $ont->adaptor->create($ont)))) { $self->throw("failed to look-up or insert ontology\" ". $ont->name().", can't continue without the foreign key"); } # the identity predicate needs to be persistent too, if given
if (ref($idpred)) { if(! $idpred->isa("Bio::DB::PersistentObjectI")) { $idpred = $adp->db->create_persistent($idpred); } # and obviously needs to have a primary key as well
if(! ($idpred->primary_key || ($idpred = ($idpred->adaptor->find_by_unique_key($idpred) || $idpred->adaptor->create($idpred))))) { $self->throw("failed to look-up or insert ID predicate '". $idpred->name() ."', can't continue without the foreign key"); } } # we'll need the name of the path table, and that of the relationship
# table
my $path_table = $self->table_name($adp); my $rel_table = $self->table_name("Bio::Ontology::RelationshipI"); # truncate existing path table content?
if($trunc) { my $sth = $self->prepare_delete_query_sth($adp, -fkobjs => [$ont]); $adp->debug("DELETE $path_table: execute, binding column 1 to ". $ont->primary_key(). " (PK to ".ref($ont->obj).")\n"); my $rv = $sth->execute($ont->primary_key()); if(! $rv) { $self->throw("failed to execute DELETE $path_table statement ". "(bound PK ".$ont->primary_key()."): ". $sth->errstr); } $adp->debug("DELETE $path_table: deleted $rv rows\n"); } # initialize the path table with all relationships under the ontology.
# for this we need to map a number of objects to foreign keys, so gather
# keys, and obtain an attribute map
my $termcl = "Bio::Ontology::TermI"; my @fks = map { $self->foreign_key_name($_); } ($termcl."::subject", $termcl."::predicate", $termcl."::object", $ont); my $colmap = $self->slot_attribute_map($path_table); # initialize with paths of distance zero between all non-predicate
# terms and themselves if the identity predicate was provided
if (ref($idpred)) { my $term_table = $self->table_name($termcl); my $pkname = $self->primary_key_name($term_table); my $ontfkname = $self->foreign_key_name($ont); my $sql = "INSERT INTO $path_table (" . join(", ", @fks, $colmap->{"distance"}).")\n" . "SELECT $pkname, ".$idpred->primary_key . ", $pkname, $ontfkname, 0\n" . "FROM $term_table t WHERE $ontfkname = ?\n" . "AND NOT EXISTS (\n" . "SELECT 1 FROM $rel_table ta WHERE ta." . $self->foreign_key_name($termcl."::predicate")." = t.$pkname " . "AND ta.$ontfkname = t.$ontfkname)"; $adp->debug("INSERT TC ONTOLOGY #0: preparing: $sql\n"); my $sth = $adp->dbh->prepare($sql); $self->throw("failed to prepare statement ($sql): ".$adp->dbh->errstr) unless $sth; $adp->debug("INSERT TC ONTOLOGY #0: executing: binding column 1 to ", $ont->primary_key(), " (FK to ".ref($ont->obj).")\n"); my $rv = $sth->execute($ont->primary_key()); if($rv) { $adp->debug("INSERT TC ONTOLOGY #0: $rv rows inserted\n"); } else { $self->throw("failed to execute statement ($sql) with parameter ". $ont->primary_key()." (FK to ".ref($ont->obj)."): ". $sth->errstr); } } # now the distance one paths as the relationships in the
# Term_Relationship table
my $sql = "INSERT INTO $path_table (". join(", ", @fks, $colmap->{"distance"}).")\n". "SELECT ". join(", ", @fks, "1")."\n". "FROM $rel_table WHERE ".$self->foreign_key_name($ont)." = ?"; $adp->debug("INSERT TC ONTOLOGY #1: preparing: $sql\n"); my $sth = $adp->dbh->prepare($sql); $self->throw("failed to prepare statement ($sql): ".$adp->dbh->errstr) unless $sth; $adp->debug("INSERT TC ONTOLOGY #1: executing: binding column 1 to ", $ont->primary_key(), " (FK to ".ref($ont->obj).")\n"); my $rv = $sth->execute($ont->primary_key()); if($rv) { $adp->debug("INSERT TC ONTOLOGY #1: executed, $rv rows inserted\n"); } else { $self->throw("failed to execute statement ($sql) with parameter ". $ont->primary_key()." (FK to ".ref($ont->obj)."): ". $sth->errstr); } # now build the transitive closure in a loop
$sql = "INSERT INTO $path_table (". join(", ", @fks, $colmap->{"distance"}).")\n". "SELECT DISTINCT ". join(", ", "tr.".$colmap->{"subject"}, "trp1.".$colmap->{"object"}, "tp.".$colmap->{"object"}, "tr.".$colmap->{"ontology"}, "tp.".$colmap->{"distance"}."+1")."\n". "FROM ". join(", ", "$rel_table tr", "$path_table tp", "$rel_table trp1", "$rel_table trp2")."\n". "WHERE ". join("\nAND ", "tp.".$colmap->{"ontology"}." = tr.".$colmap->{"ontology"}, "tr.".$colmap->{"object"}." = tp.".$colmap->{"subject"}, "tr.".$colmap->{"ontology"}." = ?", "tp.".$colmap->{"distance"}." = ?", "trp1.".$colmap->{"subject"}." = tp.".$colmap->{"predicate"}, "trp2.".$colmap->{"subject"}." = tr.".$colmap->{"predicate"}, "trp1.".$colmap->{"object"}." = trp2.".$colmap->{"object"}); $adp->debug("INSERT TC ONTOLOGY #2: preparing: $sql\n"); $sth = $adp->dbh->prepare($sql); $self->throw("failed to prepare statement ($sql): ".$adp->dbh->errstr) unless $sth; my $dist = 0; $rv = 1; # dummy value in order to enter the while loop at least once
while($rv && ($rv > 0)) { $dist++; if($adp->verbose) { $adp->debug("INSERT TC ONTOLOGY #2: executing: ". "binding columns (". join(";", "FK to ".ref($ont->obj), "distance"). ") to (". join(";", $ont->primary_key(), $dist). ")\n"); } $rv = $sth->execute($ont->primary_key(), $dist); $adp->debug("INSERT TC ONTOLOGY #2: executed, $rv rows inserted\n") if $rv; } if(! $rv) { $self->throw("failed to execute statement ($sql) with parameters (". join(";", $ont->primary_key(), $dist). "): ".$sth->errstr); } # done.
return $rv; } 1;
General documentation
Mailing ListsTop
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.                  - General discussion - About the mailing lists
Support Top
Please direct usage questions or support issues to the mailing list:

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
Reporting BugsTop
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
AUTHOR - Hilmar LappTop
Email hlapp at
Additional contributors names and emails here
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _