EEDB/EdgeSet.pm untested

filter: Statementstmt 0.0% 0 / 293 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 500 CC 47 SCAR 34.4
CC
47
Cov
0%
find_edges_with_feature1
90.0
find_edges_with_feature2
90.0
init
56.0
CRAP 31.3
1 =head1 NAME - EEDB::EdgeSet
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_linkset_global_counter = 1;
46  
47 $VERSION = 0.953;
48  
49 package EEDB::EdgeSet;
50  
51 use strict;
52 use Time::HiRes qw(time gettimeofday tv_interval);
53  
54 use EEDB::Feature;
55 use EEDB::Edge;
56 use EEDB::FeatureSet;
57  
58 use MQdb::MappedQuery;
59 our @ISA = qw(MQdb::MappedQuery);
60  
61 #################################################
62 # Class methods
63 #################################################
64  
65 sub class { return "EdgeSet"; }
66  
67 #################################################
68 # Instance methods
69 #################################################
70  
71 sub init {
72 my $self = shift;
73 my %args = @_;
74 $self->SUPER::init(@_);
75
76 $self->{'edges'} = {};
77 $self->{'count'} = undef;
78 $self->{'_feature1_hash'} = {};
79 $self->{'_feature2_hash'} = {};
80  
81 $self->{'_links_sort1'} = [];
82 $self->{'_links_sort2'} = [];
83 $self->{'_links_sort1_count'} = undef;
84 $self->{'_links_sort2_count'} = undef;
85  
86 $self->primary_id($__riken_EEDB_linkset_global_counter++);
87
88 if($args{'larray'}) {
89 $self->add_edges($args{'larray'});
90 }
91
92 if($args{'linksource'}) {
93 my $lsrc = $args{'linksource'};
94 my $links = EEDB::Edge->fetch_all_by_source($lsrc->database, $lsrc);
95 $self->add_edges($links);
96 } elsif($args{'db'}) {
97 if($args{'lsname'}) {
98 my $ls = EEDB::EdgeSource->fetch_by_name($args{'db'}, $args{'lsname'});
99 my $links = EEDB::Edge->fetch_all_by_source($args{'db'}, $ls);
100 $self->add_edges($links);
101 $self->name($ls->name);
102 }
103 if($args{'ls_id'}) {
104 my $ls = EEDB::EdgeSource->fetch_by_id($args{'db'}, $args{'ls_id'});
105 my $links = EEDB::Edge->fetch_all_by_source($args{'db'}, $ls);
106 $self->add_edges($links);
107 $self->name($ls->name);
108 }
109 if($args{'subnet_fset'}) {
110 my $id_list = $args{'subnet_fset'}->id_list;
111 my $links = EEDB::Edge->fetch_all_visible_with_feature_id_list($args{'db'}, $id_list);
112 $self->add_edges($links);
113 }
114 }
115 return $self;
116 }
117  
118 sub copy {
119 my $self = shift;
120
121 my $copy = $self->SUPER::copy();
122
123 $copy->primary_id($__riken_EEDB_linkset_global_counter++);
124  
125 #don't want to share a pointer to the internal feature hash
126 #so create new hash and fill it.
127 $copy->{'edges'} = {};
128 $copy->add_edges($self->edges);
129 return $copy;
130 }
131  
132 ##################
133 sub name {
134 my $self = shift;
135 return $self->{'name'} = shift if(@_);
136 $self->{'name'}='' unless(defined($self->{'name'}));
137 return $self->{'name'};
138 }
139  
140 sub description {
141 my $self = shift;
142 return $self->{'description'} = shift if(@_);
143 $self->{'description'}='' unless(defined($self->{'description'}));
144 return $self->{'description'};
145 }
146  
147 ##################
148  
149 sub clear_all_links {
150 my $self = shift;
151 $self->{'edges'} = {};
152 $self->{'count'} = undef;
153 $self->{'_feature1_hash'} = {};
154 $self->{'_feature2_hash'} = {};
155 $self->{'_links_sort1'} = [];
156 $self->{'_links_sort2'} = [];
157 $self->{'_links_sort1_count'} = undef;
158 $self->{'_links_sort2_count'} = undef;
159 }
160  
161 sub add_edges {
162 my $self = shift;
163 my $larray = shift;
164
165 return unless($larray);
166
167 foreach my $edge (@$larray) {
168 next unless(defined($edge) && $edge->isa('EEDB::Edge'));
169 $self->{'edges'}->{$edge->db_id} = $edge;
170 }
171 $self->{'count'} = undef;
172 $self->{'_links_sort1'} = [];
173 $self->{'_links_sort2'} = [];
174 $self->{'_links_sort1_count'} = undef;
175 $self->{'_links_sort2_count'} = undef;
176 }
177  
178 sub add_edge {
179 my $self = shift;
180 my $edge = shift;
181
182 return unless($edge);
183 return unless(defined($edge) && $edge->isa('EEDB::Edge'));
184 $self->{'edges'}->{$edge->db_id} = $edge;
185 $self->{'count'} = undef;
186 $self->{'_links_sort1'} = [];
187 $self->{'_links_sort2'} = [];
188 $self->{'_links_sort1_count'} = undef;
189 $self->{'_links_sort2_count'} = undef;
190 }
191  
192 sub add_link_to_internal_leftright_hash {
193 my $self = shift;
194 my $edge = shift;
195
196 my $lset = $self->{'_feature1_hash'}->{$edge->feature1->db_id};
197 if(!defined($lset)) {
198 $lset = new EEDB::EdgeSet;
199 $self->{'_feature1_hash'}->{$edge->feature1->db_id} = $lset;
200 }
201 $lset->add_edge($edge);
202
203 $lset = $self->{'_feature2_hash'}->{$edge->feature2->db_id};
204 if(!defined($lset)) {
205 $lset = new EEDB::EdgeSet;
206 $self->{'_feature2_hash'}->{$edge->feature2->db_id} = $lset;
207 }
208 $lset->add_edge($edge);
209 }
210  
211 sub count {
212 my $self = shift;
213 if(!defined($self->{'count'})) {
214 $self->{'count'} = scalar(keys(%{$self->{'edges'}}));
215 }
216 return $self->{'count'};
217 }
218  
219 sub edges {
220 my $self = shift;
221 my @edges = values(%{$self->{'edges'}});
222 return \@edges;
223 }
224  
225 #returns a FeatureSet object of unique nodes in this set of edges
226 sub feature_set {
227 my $self = shift;
228
229 my $fset = new EEDB::FeatureSet;
230 foreach my $edge (@{$self->edges}) {
231 $fset->add_feature($edge->feature1);
232 $fset->add_feature($edge->feature2);
233 }
234 return $fset;
235 }
236  
237 sub feature1_set {
238 my $self = shift;
239
240 my $fset = new EEDB::FeatureSet;
241 foreach my $edge (@{$self->edges}) {
242 $fset->add_feature($edge->feature1);
243 }
244 return $fset;
245 }
246  
247 sub feature2_set {
248 my $self = shift;
249
250 my $fset = new EEDB::FeatureSet;
251 foreach my $edge (@{$self->edges}) {
252 $fset->add_feature($edge->feature2);
253 }
254 return $fset;
255 }
256  
257 ###########################
258 # Set manipulation routines
259 #
260 sub remove_edgesource {
261 #this method will create new EdgeSet removing edges of specified source and returning all others
262 my $self = shift;
263 my $edge_source = shift; #EEDB::EdgeSource
264
265 my $lset = new EEDB::EdgeSet;
266 foreach my $edge (@{$self->edges}) {
267 next if(defined($edge_source) and ($edge->edge_source->id == $edge_source->id));
268 $lset->add_edge($edge);
269 }
270 return $lset;
271 }
272  
273 sub extract_edgesource {
274 #this method will create new EdgeSet with only those edges of specified source
275 my $self = shift;
276 my $edge_source = shift; #EEDB::EdgeSource
277
278 my $lset = new EEDB::EdgeSet;
279 if(!defined($edge_source) or !($edge_source->isa('EEDB::EdgeSource'))) { return $lset; }
280
281 foreach my $edge (@{$self->edges}) {
282 next if($edge->edge_source->id != $edge_source->id);
283 $lset->add_edge($edge);
284 }
285 return $lset;
286 }
287  
288 sub extract_category {
289 #this method will create new EdgeSet with only those edge sources of the specified category
290 my $self = shift;
291 my $category = shift;
292
293 my $lset = new EEDB::EdgeSet;
294
295 foreach my $edge (@{$self->edges}) {
296 next unless($edge->edge_source->category eq $category);
297 $lset->add_edge($edge);
298 }
299 return $lset;
300 }
301  
302 sub remove_local_leaves {
303 my $self = shift;
304
305 # OK this is where I will do the leaf filter step (on the edges)
306 # then the remaining code will work just fine...
307 # feature_hash has not been built yet...
308 my $intnodes = {};
309 foreach my $edge (@{$self->edges}) {
310 if($edge->direction eq "=") {
311 $intnodes->{$edge->feature1_id} = 1;
312 $intnodes->{$edge->feature2_id} = 1;
313 } else {
314 $intnodes->{$edge->feature1_id} = 1;
315 }
316 }
317 my $lset = new EEDB::EdgeSet;
318 foreach my $edge (@{$self->edges}) {
319 if($edge->direction eq "=") {
320 $lset->add_edge($edge);
321 }
322 elsif($intnodes->{$edge->feature1_id} and $intnodes->{$edge->feature2_id}) {
323 $lset->add_edge($edge);
324 }
325 }
326 return $lset;
327 }
328  
329 sub find_edges_with_feature1_hash_based {
330 my $self = shift;
331 my $feature1 = shift; #EEDB::Feature
332
333 my $lset = $self->{'_feature1_hash'}->{$feature1->db_id};
334 if(!defined($lset)) {
335 $lset = new EEDB::EdgeSet;
336 foreach my $edge (@{$self->edges}) {
337 if($edge->feature1_id == $feature1->id) { $lset->add_edge($edge); }
338 }
339 $self->{'_feature1_hash'}->{$feature1->db_id} = $lset;
340 }
341 return $lset;
342 }
343  
344 sub find_edges_with_feature2_hash_based {
345 my $self = shift;
346 my $feature2 = shift; #EEDB::Feature
347
348 my $lset = $self->{'_feature2_hash'}->{$feature2->db_id};
349 if(!defined($lset)) {
350 $lset = new EEDB::EdgeSet;
351 foreach my $edge (@{$self->edges}) {
352 if($edge->feature2_id == $feature2->id) { $lset->add_edge($edge); }
353 }
354 $self->{'_feature2_hash'}->{$feature2->db_id} = $lset;
355 }
356 return $lset;
357 }
358  
359 sub find_linksource_name {
360 my $self = shift;
361 my $edge_source_name = shift;
362
363 my $lset = new EEDB::EdgeSet;
364 foreach my $edge (@{$self->edges}) {
365 if($edge->edge_source->name eq $edge_source_name) { $lset->add_edge($edge); }
366 }
367 return $lset;
368 }
369  
370 sub _build_sorted_lists {
371 my $self = shift;
372
373 return if(defined($self->{'_links_sort1_count'}));
374 my $time2 = time();
375  
376 my @slist1 = sort {($a->feature1_id <=> $b->feature1_id)} values(%{$self->{'edges'}});
377 $self->{'_links_sort1'} = \@slist1;
378  
379 my @slist2 = sort {($a->feature2_id <=> $b->feature2_id)} values(%{$self->{'edges'}});
380 $self->{'_links_sort2'} = \@slist2;
381  
382 $self->{'_links_sort1_count'} = scalar(@slist1);
383 $self->{'_links_sort2_count'} = scalar(@slist2);
384
385 #printf("_build_sorted_lists %1.3f secs\n", (time() - $time2));
386 }
387  
388  
389 sub find_edges_with_feature1 {
390 my $self = shift;
391 my $feature1 = shift; #EEDB::Feature
392
393 #printf("search for %s\n", $feature1->simple_display_desc);
394 my $lset = new EEDB::EdgeSet;
395 $self->_build_sorted_lists();
396 return $lset unless($self->{'_links_sort1_count'}>0);
397
398 my $idx1 = 0;
399 my $idx2 = $self->{'_links_sort1_count'} - 1;
400 my $search = 1;
401  
402 while($search == 1) {
403 my $idx = int(($idx1 + $idx2) / 2);
404 my $search_link = $self->{'_links_sort1'}->[$idx];
405 #printf(" %d [%d..%d] :: f1_id %d\n", $idx, $idx1, $idx2, $search_link->feature1_id);
406  
407 if($search_link->feature1_id == $feature1->id) {
408 $idx2 = $idx;
409 } elsif(($idx==$idx1) and ($idx1 == $idx2-1)) {
410 $idx1++;
411 } elsif($feature1->id < $search_link->feature1_id) {
412 $idx2 = $idx;
413 } elsif($feature1->id > $search_link->feature1_id) {
414 $idx1 = $idx;
415 }
416 if(($idx<0) or ($idx>$self->{'_links_sort1_count'})) { $search = -1; }
417 if($idx1 == $idx2) { $search= 0; }
418 }
419
420 return $lset unless($search == 0);
421
422 my $search_link = $self->{'_links_sort1'}->[$idx1++];
423 while(defined($search_link) and ($search_link->feature1_id == $feature1->id)) {
424 $lset->add_edge($search_link);
425 #printf(" found %s\n", $search_link->display_desc);
426 $search_link = $self->{'_links_sort1'}->[$idx1++];
427 }
428
429 return $lset;
430 }
431  
432 sub find_edges_with_feature2 {
433 my $self = shift;
434 my $feature2 = shift; #EEDB::Feature
435
436 #printf("search for %s\n", $feature2->simple_display_desc);
437 my $lset = new EEDB::EdgeSet;
438 $self->_build_sorted_lists();
439 return $lset unless($self->{'_links_sort2_count'}>0);
440
441 my $idx1 = 0;
442 my $idx2 = $self->{'_links_sort2_count'} - 1;
443 my $search = 1;
444  
445 while($search == 1) {
446 my $idx = int(($idx1 + $idx2) / 2);
447 my $search_link = $self->{'_links_sort2'}->[$idx];
448 #printf(" %d [%d..%d] :: f2_id %d\n", $idx, $idx1, $idx2, $search_link->feature2_id);
449  
450 if($search_link->feature2_id == $feature2->id) {
451 $idx2 = $idx;
452 } elsif(($idx==$idx1) and ($idx1 == $idx2-1)) {
453 $idx1++;
454 } elsif($feature2->id < $search_link->feature2_id) {
455 $idx2 = $idx;
456 } elsif($feature2->id > $search_link->feature2_id) {
457 $idx1 = $idx;
458 }
459 if(($idx<0) or ($idx>$self->{'_links_sort2_count'})) { $search = -1; }
460 if($idx1 == $idx2) { $search= 0; }
461 }
462
463 return $lset unless($search == 0);
464
465 my $search_link = $self->{'_links_sort2'}->[$idx1++];
466 while(defined($search_link) and ($search_link->feature2_id == $feature2->id)) {
467 $lset->add_edge($search_link);
468 #printf(" found %s\n", $search_link->display_desc);
469 $search_link = $self->{'_links_sort2'}->[$idx1++];
470 }
471
472 return $lset;
473 }
474  
475 ################
476  
477 sub display_desc {
478 my $self = shift;
479 my $str = sprintf("EdgeSet(%s) %s :: %d edges",
480 $self->id,
481 $self->name,
482 $self->count
483 );
484 return $str;
485 }
486  
487 sub simple_xml {
488 my $self = shift;
489 my $str = sprintf("<EdgeSet name=\"%s\" count=\"%d\" />\n", $self->name, $self->count);
490 return $str;
491 }
492  
493 sub display_contents {
494 my $self = shift;
495 my $str = $self->display_desc . "\n";
496 foreach my $edge (@{$self->edges}) {
497 $str .= " " . $edge->display_desc . "\n";
498 }
499 return $str;
500 }
501  
502 1;
503