/
PathAdaptorDriver.pm
318 lines (267 loc) · 10.9 KB
/
PathAdaptorDriver.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# $Id$
#
# BioPerl module for Bio::DB::BioSQL::mysql::PathAdaptorDriver
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Hilmar Lapp <hlapp at gmx.net>
#
# Copyright Hilmar Lapp
#
#
# (c) Hilmar Lapp, hlapp at gmx.net, 2003.
# (c) GNF, Genomics Institute of the Novartis Research Foundation, 2003.
#
# You may distribute this module under the same terms as perl itself.
# Refer to the Perl Artistic License (see the license accompanying this
# software package, or see http://www.perl.com/language/misc/Artistic.html)
# for the terms under which you may use, modify, and redistribute this module.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# POD documentation - main docs before the code
=head1 NAME
Bio::DB::BioSQL::mysql::PathAdaptorDriver - DESCRIPTION of Object
=head1 SYNOPSIS
Give standard usage here
=head1 DESCRIPTION
Describe the object here
=head1 FEEDBACK
=head2 Mailing Lists
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.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
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.
=head2 Reporting Bugs
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:
http://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Hilmar Lapp
Email hlapp at gmx.net
=head1 CONTRIBUTORS
Additional contributors names and emails here
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::DB::BioSQL::mysql::PathAdaptorDriver;
use vars qw(@ISA);
use strict;
# Object preamble - inherits from Bio::Root::Root
use Bio::DB::BioSQL::mysql::BasePersistenceAdaptorDriver;
@ISA = qw(Bio::DB::BioSQL::mysql::BasePersistenceAdaptorDriver);
=head2 compute_transitive_closure
Title : compute_transitive_closure
Usage :
Function: Compute the transitive closure over a given ontology
and populate the respective path table in the relational
schema.
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).
=cut
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 t.$pkname, ".$idpred->primary_key
. ", t.$pkname, t.$ontfkname, 0\n"
. "FROM $term_table t LEFT OUTER JOIN $rel_table ta "
. "ON (ta.".$self->foreign_key_name($termcl."::predicate")
." = t.$pkname AND ta.$ontfkname = t.$ontfkname) "
. "WHERE t.$ontfkname = ? AND ta.$ontfkname IS NULL\n";
$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: $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
#
# unfortunately, in MySQL we can't insert into a table we're also selecting
# from - which will make things a bit slower, and requires us to set up
# separate INSERT and SELECT statements, looping over each individual
# select result set
#
# here's the insert statement, pretty simple
$sql = "INSERT INTO $path_table (".
join(", ", @fks, $colmap->{"distance"}).
") VALUES (?, ?, ?, ?, ?)";
$adp->debug("INSERT TC ONTOLOGY #2.1: preparing: $sql\n");
my $inssth = $adp->dbh->prepare($sql);
$self->throw("failed to prepare statement ($sql): ".$adp->errstr)
unless $inssth;
# now the select statement
$sql = "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("SELECT PATH ONTOLOGY #2.2: preparing: $sql\n");
$sth = $adp->dbh->prepare($sql);
$self->throw("failed to prepare statement ($sql): ".$adp->dbh->errstr)
unless $sth;
my $dist = 0;
my $nrows = 1;
# this is the main loop: while the select statement returned any rows
while($rv && ($nrows > 0)) {
# try the next higher distance
$dist++;
if($adp->verbose) {
$adp->debug("SELECT PATH ONTOLOGY #2.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);
$self->throw("failed to execute INSERT TC #2.2: ".$sth->errstr)
unless $rv;
$nrows = 0;
# insert all rows returned
while(my $row = $sth->fetchrow_arrayref()) {
$nrows++;
$adp->debug("INSERT TC ONTOLOGY #2.1: executing: ".
"binding columns to (".join(";", @$row).")\n");
$rv = $inssth->execute(@$row);
$self->throw("failed to execute INSERT TC #2.1: ".$inssth->errstr)
unless $rv;
}
}
# done.
return $rv;
}
1;