EEDB/Edge.pm untested

filter: Statementstmt 0.0% 0 / 440 Branchbran 0.0% 0 / 166 Conditioncond 0.0% 0 / 99 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 54 Podpod 0.0% 0 / 48 totaltotal 0.0% 0 / 868 CC 84 SCAR 31.5
CC
84
Cov
0%
stream_all
90.0
mapRow
42.0
fetch_all_with_metadata
42.0
CRAP 23.3
1 =head1 NAME - EEDB::Edge
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 =head1 CONTACT
8  
9 Jessica Severin <severin@gsc.riken.jp>
10  
11 =head1 LICENSE
12  
13 * Software License Agreement (BSD License)
14 * EdgeExpressDB [eeDB] system
15 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
16 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * Neither the name of Jessica Severin RIKEN OSC nor the
25 * names of its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 =head1 APPENDIX
40  
41 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
42  
43 =cut
44  
45 my $__riken_EEDB_edge_global_should_cache = 0;
46 my $__riken_EEDB_edge_global_id_cache = {};
47 my $__riken_EEDB_edge_global_featureid_cache = {};
48  
49 $VERSION = 0.953;
50  
51 package EEDB::Edge;
52  
53 use strict;
54 use Time::HiRes qw(time gettimeofday tv_interval);
55 use EEDB::Feature;
56 use EEDB::EdgeSource;
57 use EEDB::MetadataSet;
58  
59 use MQdb::MappedQuery;
60 our @ISA = qw(MQdb::MappedQuery);
61  
62 #################################################
63 # Class methods
64 #################################################
65  
66 sub class { return "Edge"; }
67  
68 sub set_cache_behaviour {
69 my $class = shift;
70 my $mode = shift;
71
72 $__riken_EEDB_edge_global_should_cache = $mode;
73
74 if(defined($mode) and ($mode eq '0')) {
75 #if turning off caching, then flush the caches
76 $__riken_EEDB_edge_global_id_cache = {};
77 $__riken_EEDB_edge_global_featureid_cache = {};
78 }
79 }
80  
81 sub get_cache_size {
82 return scalar(keys(%$__riken_EEDB_edge_global_id_cache));
83 }
84  
85  
86 #################################################
87 # Instance methods
88 #################################################
89  
90 sub init {
91 my $self = shift;
92 $self->SUPER::init;
93
94 return $self;
95 }
96  
97 sub feature1_id {
98 my $self = shift;
99 return $self->{'feature1_id'};
100 }
101 sub feature2_id {
102 my $self = shift;
103 return $self->{'feature2_id'};
104 }
105  
106 sub fed_feature1_id {
107 my $self = shift;
108 my $fid = $self->feature1_id;
109 if($self->edge_source->db1_is_external) { $fid = $self->edge_source->peer1->alias . "::" . $self->feature1_id; }
110 return $fid;
111 }
112  
113 sub fed_feature2_id {
114 my $self = shift;
115 my $fid = $self->feature2_id;
116 if($self->edge_source->db2_is_external) { $fid = $self->edge_source->peer2->alias . "::" . $self->feature2_id; }
117 return $fid;
118 }
119  
120 sub feature1 {
121 my ($self, $feature) = @_;
122 if($feature) {
123 unless(defined($feature) && $feature->isa('EEDB::Feature')) {
124 die('feature1 param must be a EEDB::Feature');
125 }
126 $self->{'feature1'} = $feature;
127 $self->{'feature1_id'} = $feature->id;
128 }
129
130 #lazy load from database if possible
131 if(!defined($self->{'feature1'}) and
132 defined($self->database) and
133 defined($self->{'feature1_id'}))
134 {
135 #printf("LAZY LOAD chrom_id=%d\n", $self->{'_chrom_id'});
136 my $feature = EEDB::Feature->fetch_by_id($self->edge_source->database1, $self->{'feature1_id'});
137 if(defined($feature)) { $self->{'feature1'} = $feature; }
138 }
139 return $self->{'feature1'};
140 }
141  
142  
143 sub feature2 {
144 my ($self, $feature) = @_;
145 if($feature) {
146 unless(defined($feature) && $feature->isa('EEDB::Feature')) {
147 die('feature2 param must be a EEDB::Feature');
148 }
149 $self->{'feature2'} = $feature;
150 $self->{'feature2_id'} = $feature->id;
151 }
152
153 #lazy load from database if possible
154 if(!defined($self->{'feature2'}) and
155 defined($self->database) and
156 defined($self->{'feature2_id'}))
157 {
158 #printf("LAZY LOAD chrom_id=%d\n", $self->{'_chrom_id'});
159 my $feature = EEDB::Feature->fetch_by_id($self->edge_source->database2, $self->{'feature2_id'});
160 if(defined($feature)) { $self->{'feature2'} = $feature; }
161 }
162 return $self->{'feature2'};
163 }
164  
165  
166 sub edge_source {
167 my ($self, $source) = @_;
168 if($source) {
169 unless(defined($source) && $source->isa('EEDB::EdgeSource')) {
170 die('edge_source param must be a EEDB::EdgeSource');
171 }
172 $self->{'edge_source'} = $source;
173 }
174
175 #lazy load from database if possible
176 if(!defined($self->{'edge_source'}) and
177 defined($self->database) and
178 defined($self->{'edge_source_id'}))
179 {
180 #printf("LAZY LOAD chrom_id=%d\n", $self->{'_chrom_id'});
181 my $source = EEDB::EdgeSource->fetch_by_id($self->database, $self->{'edge_source_id'});
182 if(defined($source)) {
183 $self->{'edge_source'} = $source;
184 $self->{'edge_source_id'} = undef;
185 }
186 }
187 return $self->{'edge_source'};
188 }
189  
190  
191 sub sub_type {
192 my $self = shift;
193 return $self->{'sub_type'} = shift if(@_);
194 $self->{'sub_type'}='' unless(defined($self->{'sub_type'}));
195 return $self->{'sub_type'};
196 }
197  
198 sub direction {
199 my $self = shift;
200 return $self->{'direction'} = shift if(@_);
201 $self->{'direction'}='' unless(defined($self->{'direction'}));
202 return $self->{'direction'};
203 }
204  
205 sub weight {
206 my $self = shift;
207 return $self->{'weight'} = shift if(@_);
208 $self->{'weight'}=0.0 unless(defined($self->{'weight'}));
209 return $self->{'weight'};
210 }
211 ##################
212  
213 sub metadataset {
214 my $self = shift;
215
216 if(!defined($self->{'metadataset'})) {
217 $self->{'metadataset'} = new EEDB::MetadataSet;
218  
219 if($self->database) {
220 my $symbols = EEDB::Symbol->fetch_all_by_edge_id($self->database, $self->id);
221 $self->{'metadataset'}->add_metadata(@$symbols);
222  
223 my $mdata = EEDB::Metadata->fetch_all_by_edge_id($self->database, $self->id);
224 $self->{'metadataset'}->add_metadata(@$mdata);
225 }
226 }
227 return $self->{'metadataset'};
228 }
229  
230 ################
231  
232 sub display_desc {
233 my $self = shift;
234 my $str = sprintf("Edge(%s) %s(%s) => %s(%s) : %f %s",
235 $self->id,
236 $self->feature1->primary_name,
237 $self->feature1->id,
238 $self->feature2->primary_name,
239 $self->feature2->id,
240 $self->weight,
241 $self->edge_source->uqname
242 );
243 my $mdata_list = $self->metadataset->metadata_list;
244 my $first=1;
245 if(defined($mdata_list) and (scalar(@$mdata_list))) {
246 $str .= ' (';
247 foreach my $mdata (@$mdata_list) {
248 if($first) { $first=0; }
249 else { $str .= ','; }
250 $str .= sprintf("(%s,\"%s\")", $mdata->type, $mdata->data);
251 }
252 $str .= ')';
253 }
254 return $str;
255 }
256  
257  
258 sub display_contents {
259 my $self = shift;
260 my $str = sprintf("Edge(%s) %s(%s) => %s(%s) : %f %s",
261 $self->id,
262 $self->feature1->primary_name,
263 $self->feature1->id,
264 $self->feature2->primary_name,
265 $self->feature2->id,
266 $self->weight,
267 $self->edge_source->uqname
268 );
269 $str .= "\n". $self->metadataset->display_contents;
270 return $str;
271 }
272  
273  
274 sub xml {
275 my $self = shift;
276
277 my $str = sprintf("<edge source=\"%s\" weight=\"%f\" dir=\"%s\" eid=\"%s\">\n",
278 $self->edge_source->uqname,
279 $self->weight,
280 $self->direction,
281 $self->id
282 );
283
284 my $feature1 = $self->feature1;
285 $str .= sprintf(" <feature1 id=\"%s\" name=\"%s\" source=\"%s\" category=\"%s\" ",
286 $self->fed_feature1_id,
287 $feature1->primary_name,
288 $feature1->feature_source->name,
289 $feature1->feature_source->category);
290 if($feature1->chrom) { $str .= sprintf("loc=\"%s\" ", $feature1->chrom_location); }
291 $str .= "/>\n";
292
293 my $feature2 = $self->feature2;
294 $str .= sprintf(" <feature2 id=\"%s\" name=\"%s\" source=\"%s\" category=\"%s\" ",
295 $self->fed_feature2_id,
296 $feature2->primary_name,
297 $feature2->feature_source->name,
298 $feature2->feature_source->category);
299 if($feature2->chrom) { $str .= sprintf("loc=\"%s\" ", $feature2->chrom_location); }
300 $str .= "/>\n";
301  
302 $str .= $self->metadataset->xml;
303
304 $str .= "</edge>\n";
305 return $str;
306 }
307  
308 sub simple_xml {
309 my $self = shift;
310 my $str = sprintf("<edge source=\"%s\" f1id=\"%d\" f2id=\"%d\" name1=\"%s\" name2=\"%s\" weight=\"%f\" dir=\"%s\" edge_id=\"%s\" />",
311 $self->edge_source->uqname,
312 $self->feature1_id,
313 $self->feature2_id,
314 $self->feature1->primary_name,
315 $self->feature2->primary_name,
316 $self->weight,
317 $self->direction,
318 $self->id
319 );
320 return $str;
321 }
322  
323 #######################
324  
325 sub get_neighbor {
326 my $self = shift;
327 my $feature = shift;
328  
329 my $dir = "";
330 my $neighbor = undef;
331  
332 if($self->feature1_id == $feature->id) {
333 $dir = "link_to";
334 $neighbor = $self->feature2;
335 } elsif($self->feature2_id == $feature->id) {
336 $dir = "link_from";
337 $neighbor = $self->feature1;
338 } elsif($self->feature1_id == $self->feature2_id) {
339 $dir = "link_self";
340 $neighbor = $self->feature1;
341 }
342 return ($dir, $neighbor);
343 }
344  
345  
346 #################################################
347 #
348 # DBObject override methods
349 #
350 #################################################
351  
352 sub store {
353 my $self = shift;
354 my $db = shift;
355
356 if($db) { $self->database($db); }
357 my $dbh = $self->database->get_connection;
358 my $sql = "INSERT ignore INTO edge (
359 feature1_id,
360 feature2_id,
361 sub_type,
362 direction,
363 edge_source_id,
364 weight
365 )
366 VALUES(?,?,?,?,?,?)";
367 my $sth = $dbh->prepare($sql);
368 $sth->execute($self->feature1->id,
369 $self->feature2->id,
370 $self->sub_type,
371 $self->direction,
372 $self->edge_source->id,
373 $self->weight);
374  
375 my $dbID = $sth->{'mysql_insertid'};
376 #my $dbID = $dbh->last_insert_id(undef, undef, qw(edge edge_id));
377 $sth->finish;
378 $self->primary_id($dbID);
379  
380 return $self;
381 }
382  
383 sub store_metadata {
384 my $self = shift;
385 die("error no database to store metadata\n") unless($self->database);
386  
387 my $mdata_list = $self->metadataset->metadata_list;
388 foreach my $mdata (@$mdata_list) {
389 if(!defined($mdata->primary_id)) {
390 $mdata->store($self->database);
391 $mdata->store_link_to_feature_link($self);
392 }
393 }
394 }
395  
396 sub check_exists_db {
397 my $self = shift;
398
399 unless($self->edge_source) { return undef; }
400 unless($self->edge_source->database) { return undef; }
401 if(defined($self->primary_id)) { return $self; }
402
403 #check if it is already in the database
404 my $dbc = $self->edge_source->database->get_connection;
405 my $sth = $dbc->prepare("SELECT edge_id FROM edge where feature1_id=? and feature2_id=? and edge_source_id=?");
406 $sth->execute($self->feature1->id, $self->feature2->id, $self->edge_source->id);
407 my ($edge_id) = $sth->fetchrow_array();
408 $sth->finish;
409
410 if($edge_id) {
411 $self->primary_id($edge_id);
412 $self->database($self->edge_source->database);
413 return $self;
414 } else {
415 return undef;
416 }
417 }
418  
419 ##### DBObject instance override methods #####
420  
421 sub mapRow {
422 my $self = shift;
423 my $rowHash = shift;
424 my $dbh = shift;
425  
426 my $dbID = $rowHash->{'edge_id'};
427 if($__riken_EEDB_edge_global_should_cache != 0) {
428 my $cached_self = $__riken_EEDB_edge_global_id_cache->{$self->database() . $dbID};
429 if(defined($cached_self)) {
430 #printf("link already loaded in cache, reuse\n");
431 #$cached_self->display_info;
432 #printf(" db_id :: %s\n", $cached_self->db_id);
433 return $cached_self;
434 }
435 }
436  
437 $self->primary_id($rowHash->{'edge_id'});
438 $self->direction($rowHash->{'direction'});
439 $self->sub_type($rowHash->{'sub_type'});
440 $self->weight($rowHash->{'weight'});
441  
442 $self->{'feature1_id'} = $rowHash->{'feature1_id'};
443 $self->{'feature2_id'} = $rowHash->{'feature2_id'};
444 $self->{'edge_source_id'} = $rowHash->{'edge_source_id'};
445  
446 if($__riken_EEDB_edge_global_should_cache != 0) {
447 #first do the edge_id caching
448 $__riken_EEDB_edge_global_id_cache->{$self->database() . $self->id} = $self;
449  
450 #then do the feature->[array of edges] caching
451 my $center_id = $rowHash->{'center_id'};
452 if(defined($center_id)) {
453 my $link_hash = $__riken_EEDB_edge_global_featureid_cache;
454
455 if(!defined($link_hash->{$self->database() . $center_id})) {
456 $link_hash->{$self->database() . $center_id} = {$self->id => $self};
457 } else {
458 $link_hash->{$self->database() . $center_id}->{$self->id} = $self;
459 }
460 }
461 }
462  
463 return $self;
464 }
465  
466  
467 ##### public class methods for fetching by utilizing DBObject framework methods #####
468  
469 sub fetch_by_id {
470 my $class = shift;
471 my $db = shift;
472 my $id = shift;
473  
474 if($__riken_EEDB_edge_global_should_cache != 0) {
475 my $edge = $__riken_EEDB_edge_global_id_cache->{$db . $id};
476 if(defined($edge)) { return $edge; }
477 }
478  
479 my $sql = "SELECT * FROM edge WHERE edge_id=?";
480 return $class->fetch_single($db, $sql, $id);
481 }
482  
483 sub fetch_all_by_source {
484 my $class = shift;
485 my $db = shift;
486 my $link_source = shift; #EdgeSource object
487  
488 my $sql = "SELECT * FROM edge WHERE edge_source_id=?";
489 return $class->fetch_multiple($db, $sql, $link_source->id);
490 }
491  
492 sub stream_all_by_source {
493 my $class = shift;
494 my $source = shift; #EdgeSource object with database connection
495 my $sort = shift; #optional flag for sorting
496
497 return undef unless($source and $source->database);
498  
499 my $sql = "SELECT * FROM edge WHERE edge_source_id=? ";
500 if(defined($sort) and ($sort eq 'f1')) { $sql .= "ORDER by feature1_id "; }
501 if(defined($sort) and ($sort eq 'f2')) { $sql .= "ORDER by feature2_id "; }
502 return $class->stream_multiple($source->database, $sql, $source->id);
503 }
504  
505 sub fetch_all_from_sourceid_list {
506 my $class = shift;
507 my $db = shift;
508 my $source_ids = shift;
509  
510 my $sql = sprintf("SELECT * FROM edge WHERE edge_source_id in (%s)", $source_ids);
511 return $class->fetch_multiple($db, $sql);
512 }
513  
514 sub fetch_all_from_feature_id {
515 my $class = shift;
516 my $db = shift;
517 my $id = shift;
518 my $edge_source_ids = shift; #optional
519
520 my $sql = sprintf("SELECT * FROM edge fl JOIN edge_source using(edge_source_id) WHERE is_active='y' AND feature1_id='%s' ", $id);
521 if(defined($edge_source_ids)) {
522 $sql .= sprintf("AND fl.edge_source_id in(%s)", $edge_source_ids);
523 }
524 return $class->fetch_multiple($db, $sql);
525 }
526  
527 sub fetch_all_to_feature_id {
528 my $class = shift;
529 my $db = shift;
530 my $id = shift;
531 my $edge_source_ids = shift; #optional
532  
533 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) WHERE is_active='y' AND feature2_id='%s' ", $id);
534 if(defined($edge_source_ids)) {
535 $sql .= sprintf("AND fl.edge_source_id in(%s)", $edge_source_ids);
536 }
537 return $class->fetch_multiple($db, $sql);
538 }
539  
540 sub fetch_all_with_feature {
541 my $class = shift;
542 my $feature = shift;
543 my %options = @_; #like category=>"subfeature", sources=>[$esrc1, $esrc2,$esrc3]
544
545 unless(defined($feature) && $feature->isa('EEDB::Feature')) {
546 die('fetch_all_with_feature param1 must be a EEDB::Feature');
547 }
548 my $list1 = $class->fetch_all_with_feature1($feature, %options);
549 my $list2 = $class->fetch_all_with_feature2($feature, %options);
550 my @rtnlist = (@$list1, @$list2);
551 return \@rtnlist;
552 }
553  
554 sub fetch_all_with_feature1 {
555 my $class = shift;
556 my $feature1 = shift;
557 my %options = @_; #like category=>"subfeature", sources=>[$esrc1, $esrc2,$esrc3]
558
559 unless(defined($feature1) && $feature1->isa('EEDB::Feature')) {
560 die('fetch_all_with_feature1 param1 must be a EEDB::Feature');
561 }
562
563 my $sql = "SELECT * FROM edge fl JOIN edge_source using(edge_source_id) WHERE feature1_id=? ";
564 if(%options) {
565 if($options{'category'}) {
566 $sql .= sprintf(" AND category='%s'", $options{'category'});
567 }
568 if($options{'sources'}) {
569 my @lsrc_ids;
570 foreach my $lsrc (@{$options{'sources'}}) { push @lsrc_ids, $lsrc->id; }
571 $sql .= sprintf(" AND fl.edge_source_id in(%s)", join(',', @lsrc_ids));
572 }
573 }
574 return $class->fetch_multiple($feature1->database, $sql, $feature1->id);
575 }
576  
577 sub fetch_all_with_feature2 {
578 my $class = shift;
579 my $feature2 = shift;
580 my %options = @_; #like category=>"subfeature", sources=>[$esrc1, $esrc2,$esrc3]
581
582 unless(defined($feature2) && $feature2->isa('EEDB::Feature')) {
583 die('fetch_all_with_feature2 param1 must be a EEDB::Feature');
584 }
585  
586 my $sql = "SELECT * FROM edge fl JOIN edge_source using(edge_source_id) WHERE feature2_id= ?";
587 if(%options) {
588 if($options{'category'}) {
589 $sql .= sprintf(" AND category='%s'", $options{'category'});
590 }
591 if($options{'sources'}) {
592 my @lsrc_ids;
593 foreach my $lsrc (@{$options{'sources'}}) { push @lsrc_ids, $lsrc->id; }
594 $sql .= sprintf(" AND fl.edge_source_id in(%s)", join(',', @lsrc_ids));
595 }
596 }
597 return $class->fetch_multiple($feature2->database, $sql, $feature2->id);
598 }
599  
600 sub fetch_all_with_feature_id {
601 my $class = shift;
602 my $db = shift;
603 my $id = shift;
604 my $edge_source_ids = shift; #optional
605  
606 my $sql = sprintf("SELECT %s center_id, fl.* FROM edge fl WHERE (feature1_id='%s' OR feature2_id='%s') ", $id, $id, $id);
607 if(defined($edge_source_ids)) {
608 $sql .= sprintf("AND fl.edge_source_id in (%s)", $edge_source_ids);
609 }
610 return $class->fetch_multiple($db, $sql);
611 }
612  
613 sub fetch_all_visible_with_feature_id {
614 my $class = shift;
615 my $db = shift;
616 my $id = shift;
617 my $edge_source_ids = shift; #optional
618  
619 if($__riken_EEDB_edge_global_should_cache != 0) {
620 my $edge_hash = $__riken_EEDB_edge_global_featureid_cache->{$db . $id};
621 if(defined($edge_hash)) {
622 my @edges = values(%$edge_hash);
623 return \@edges;
624 }
625 }
626  
627 my $sql = sprintf("SELECT %s center_id, fl.* FROM edge fl JOIN edge_source using(edge_source_id) WHERE is_visible ='y' AND(feature1_id='%s' OR feature2_id='%s') ", $id, $id, $id);
628 if(defined($edge_source_ids)) {
629 $sql .= sprintf("AND fl.edge_source_id in (%s)", $edge_source_ids);
630 }
631 return $class->fetch_multiple($db, $sql);
632 }
633  
634 sub fetch_all_active_with_feature_id {
635 my $class = shift;
636 my $db = shift;
637 my $id = shift;
638  
639 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
640 "WHERE is_active ='y' AND(feature1_id='%s' OR feature2_id='%s') ", $id, $id);
641 return $class->fetch_multiple($db, $sql);
642 }
643  
644 sub fetch_all_visible_with_feature_id_list {
645 my $class = shift;
646 my $db = shift;
647 my $id_list = shift;
648  
649 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
650 "WHERE is_visible ='y' AND feature1_id in(%s) AND feature2_id in(%s)", $id_list, $id_list);
651 return $class->fetch_multiple($db, $sql);
652 }
653  
654 sub fetch_all_active_with_feature_id_list {
655 my $class = shift;
656 my $db = shift;
657 my $id_list = shift;
658  
659 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
660 "WHERE is_active ='y' AND feature1_id in(%s) AND feature2_id in(%s)", $id_list, $id_list);
661 return $class->fetch_multiple($db, $sql);
662 }
663  
664 sub fetch_all_visible_between_feature_id_list {
665 my $class = shift;
666 my $db = shift;
667 my $id_list = shift;
668  
669 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
670 "WHERE is_visible ='y' AND feature1_id in(%s) AND feature2_id in(%s)", $id_list, $id_list);
671 return $class->fetch_multiple($db, $sql);
672 }
673  
674 sub fetch_all_active_between_feature_id_list {
675 my $class = shift;
676 my $db = shift;
677 my $id_list = shift;
678  
679 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
680 "WHERE is_active ='y' AND feature1_id in(%s) AND feature2_id in(%s)", $id_list, $id_list);
681 return $class->fetch_multiple($db, $sql);
682 }
683  
684 sub fetch_all_active_expand_from_feature_id_list {
685 my $class = shift;
686 my $db = shift;
687 my $id_list = shift;
688  
689 my $sql = sprintf("SELECT fl.* FROM edge fl JOIN edge_source using(edge_source_id) ".
690 "WHERE is_active ='y' AND feature1_id in(%s) OR feature2_id in(%s)", $id_list, $id_list);
691 return $class->fetch_multiple($db, $sql);
692 }
693  
694 sub fetch_all_visible {
695 my $class = shift;
696 my $db = shift;
697  
698 my $sql = sprintf("SELECT * FROM edge JOIN edge_source using(edge_source_id) WHERE is_visible ='y' ");
699 return $class->fetch_multiple($db, $sql);
700 }
701  
702 sub stream_all_visible {
703 my $class = shift;
704 my $db = shift;
705  
706 my $sql = sprintf("SELECT * FROM edge JOIN edge_source using(edge_source_id) WHERE is_visible ='y' ");
707 return $class->stream_multiple($db, $sql);
708 }
709  
710 sub fetch_all_like_link {
711 my $class = shift;
712 my $db = shift;
713 my $link = shift; #Edge object
714
715 #'like' means same feature1, feature2, and source
716  
717 my $sql = "SELECT * FROM edge ".
718 "WHERE feature1_id=? and feature2_id=? and edge_source_id=?";
719 return $class->fetch_multiple($db, $sql, $link->feature1_id, $link->feature2_id, $link->edge_source->id);
720 }
721  
722  
723 sub fetch_all_with_metadata {
724 my $class = shift;
725 my $source = shift; #EdgeSource object uses database of source for fetching
726 my @mdata_array = @_; #Metadata object(s)
727
728 if(defined($source) && !($source->isa('EEDB::EdgeSource'))) {
729 die('second parameter [source] must be a EEDB::EdgeSource');
730 }
731
732 if(!defined($source->database) or !defined($source->primary_id)) { return []; }
733
734 my @mdata_ids;
735 foreach my $mdata (@mdata_array) {
736 unless(defined($mdata) && ($mdata->class eq 'Metadata')) {
737 die("$mdata is not a EEDB::Metadata");
738 }
739 if(defined($mdata->primary_id)) {push @mdata_ids, $mdata->id; }
740 }
741 if(scalar(@mdata_ids) == 0) { return []; } #if mdata objects not stored then return []
742
743 my $sql = sprintf("SELECT e.* FROM edge e JOIN edge_2_metadata using(edge_id) WHERE metadata_id in(%s) ",
744 join(',', @mdata_ids));
745 $sql .= sprintf(" AND edge_source_id=%d", $source->id);
746 $sql .= sprintf(" GROUP BY e.edge_id");
747  
748 #print($sql, "\n", );
749 return $class->fetch_multiple($source->database, $sql);
750 }
751  
752 sub fetch_all_with_symbol {
753 my $class = shift;
754 my $db = shift;
755 my $source = shift; #EdgeSource object
756 my $symbol = shift; #Symbol object
757 my $response_limit = shift; #optional
758
759 unless(defined($source) && $source->isa('EEDB::EdgeSource')) {
760 die('second parameter [source] must be a EEDB::EdgeSource');
761 }
762 unless(defined($symbol) && ($symbol->class eq 'Symbol')) {
763 die('third parameter [symbol] must be a EEDB::Symbol');
764 }
765  
766 my $sql = sprintf("SELECT e.* FROM edge f ".
767 "JOIN edge_2_symbol using(edge_id) ".
768 "WHERE symbol_id = %d ",
769 $symbol->id);
770 if(defined($source)) {
771 $sql .= sprintf("AND edge_source_id=%d", $source->id);
772 }
773 $sql .= sprintf(" GROUP BY e.edge_id"); #to make sure the edge is not sent more than once
774 if($response_limit) {
775 $sql .= sprintf(" LIMIT %d", $response_limit);
776 }
777  
778 #print($sql, "\n", );
779 return $class->fetch_multiple($db, $sql);
780 }
781  
782  
783 ###############################################################################################
784 #
785 # streaming API section
786 #
787 ###############################################################################################
788  
789  
790 =head2 stream_all
791  
792 Description: stream all edges out of database with a given set of source filters
793 Arg (1) : $database (MQdb::Database)
794 Arg (2...) : hash named filter parameters.
795 sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource
796 Returntype : a DBStream instance
797 Exceptions : none
798  
799 =cut
800  
801 sub stream_all {
802 my $class = shift;
803 my $db = shift; #database
804 my %options = @_; #like sources=>[$esrc1, $esrc2,$esrc3]
805
806 return [] unless($db);
807  
808 my $sql = "SELECT * FROM edge e ";
809 if(%options and $options{'visible'} eq 'y') {
810 $sql .= "JOIN edge_source es on(e.edge_source_id = es.edge_source_id and es.is_visible='y') ";
811 }
812 $sql .= "WHERE 1=1 ";
813  
814 if(%options and $options{'sources'}) {
815 my @esrc_ids;
816 foreach my $source (@{$options{'sources'}}) {
817 if($source->class eq 'EdgeSource') { push @esrc_ids, $source->id; }
818 }
819 $sql .= sprintf("AND e.edge_source_id in(%s) ", join(',', @esrc_ids)) if(@esrc_ids);
820 }
821 if(%options and $options{'feature1'} and ($options{'feature1'}->class eq "Feature")) {
822 $sql .= sprintf("AND feature1_id=%s ", $options{'feature1'}->id);
823 }
824 if(%options and $options{'feature2'} and ($options{'feature2'}->class eq "Feature")) {
825 $sql .= sprintf("AND feature2_id=%s ", $options{'feature2'}->id);
826 }
827  
828 $sql .= "ORDER by e.edge_source_id, weight desc";
829 if(%options and $options{'end'} eq 'f2') { $sql .= ",feature2_id, feature1_id "; }
830 else { $sql .= ",feature1_id, feature2_id "; }
831  
832 #printf("<sql>%s</sql>\n", $sql);
833 return $class->stream_multiple($db, $sql);
834 }
835  
836  
837 sub stream_all_with_feature { #used by EEDB::Tools::EdgeCompare (experimental approach)
838 my $class = shift;
839 my $db = shift;
840 my $feature = shift;
841 my %options = @_; #optional
842
843 my $id = $feature->id;
844 my $sql = "SELECT * FROM edge e ";
845 if(%options and $options{'visible'} eq 'y') {
846 $sql .= " JOIN edge_source es on(e.edge_source_id = es.edge_source_id and es.is_visible='y') ";
847 }
848  
849 $sql .= sprintf("WHERE (feature1_id='%s' OR feature2_id='%s') ", $id, $id);
850
851 if(%options and $options{'sources'}) {
852 my @lsrc_ids;
853 foreach my $lsrc (@{$options{'sources'}}) { push @lsrc_ids, $lsrc->id; }
854 $sql .= sprintf("AND e.edge_source_id in(%s) ", join(',', @lsrc_ids));
855 }
856
857 $sql .= "ORDER by weight desc";
858 if(%options and $options{'end'} eq 'f1') { $sql .= ",feature1_id, feature2_id "; }
859 if(%options and $options{'end'} eq 'f2') { $sql .= ",feature2_id, feature1_id "; }
860  
861 #printf("<sql>%s</sql>\n", $sql);
862 return $class->stream_multiple($db, $sql);
863 }
864  
865  
866  
867 1;
868