| 1 | =head1 NAME - EEDB::FeatureSet | ||
| 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_featureset_global_counter = 1; | ||
| 46 | |||
| 47 | $VERSION = 0.953; | ||
| 48 | |||
| 49 | package EEDB::FeatureSet; | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 53 | use EEDB::Feature; | ||
| 54 | |||
| 55 | use MQdb::MappedQuery; | ||
| 56 | our @ISA = qw(MQdb::MappedQuery); | ||
| 57 | |||
| 58 | ################################################# | ||
| 59 | # Class methods | ||
| 60 | ################################################# | ||
| 61 | |||
| 62 | sub class { return "FeatureSet"; } | ||
| 63 | |||
| 64 | ################################################# | ||
| 65 | # Instance methods | ||
| 66 | ################################################# | ||
| 67 | |||
| 68 | sub init { | ||
| 69 | my $self = shift; | ||
| 70 | my %args = @_; | ||
| 71 | $self->SUPER::init(@_); | ||
| 72 | |||
| 73 | $self->{'features'} = {}; | ||
| 74 | $self->{'count'} = undef; | ||
| 75 | $self->primary_id($__riken_EEDB_featureset_global_counter++); | ||
| 76 | |||
| 77 | if($args{'farray'}) { $self->add_features($args{'farray'}); } | ||
| 78 | |||
| 79 | if($args{'db'}) { | ||
| 80 | if($args{'fsrc'}) { | ||
| 81 | my $fsrc = EEDB::FeatureSource->fetch_by_name($args{'db'}, $args{'fsrc'}); | ||
| 82 | my $features = EEDB::Feature->fetch_all_by_source($args{'db'}, $fsrc); | ||
| 83 | $self->add_features($features); | ||
| 84 | $self->name($fsrc->name); | ||
| 85 | } | ||
| 86 | if($args{'fsrc_id'}) { | ||
| 87 | my $fsrc = EEDB::FeatureSource->fetch_by_id($args{'db'}, $args{'fsrc_id'}); | ||
| 88 | my $features = EEDB::Edge->fetch_all_by_source($args{'db'}, $fsrc); | ||
| 89 | $self->add_features($features); | ||
| 90 | $self->name($fsrc->name); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub copy { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | my $copy = $self->SUPER::copy(); | ||
| 101 | |||
| 102 | $copy->primary_id($__riken_EEDB_featureset_global_counter++); | ||
| 103 | |||
| 104 | #don't want to share a pointer to the internal feature hash | ||
| 105 | #so create new hash and fill it. | ||
| 106 | $copy->{'features'} = {}; | ||
| 107 | $copy->add_features($self->features); | ||
| 108 | return $copy; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub name { | ||
| 112 | my $self = shift; | ||
| 113 | return $self->{'name'} = shift if(@_); | ||
| 114 | $self->{'name'}='' unless(defined($self->{'name'})); | ||
| 115 | return $self->{'name'}; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub description { | ||
| 119 | my $self = shift; | ||
| 120 | return $self->{'description'} = shift if(@_); | ||
| 121 | $self->{'description'}='' unless(defined($self->{'description'})); | ||
| 122 | return $self->{'description'}; | ||
| 123 | } | ||
| 124 | |||
| 125 | ################## | ||
| 126 | |||
| 127 | sub add_features { | ||
| 128 | my $self = shift; | ||
| 129 | my $farray = shift; | ||
| 130 | |||
| 131 | return unless($farray); | ||
| 132 | |||
| 133 | foreach my $feature (@$farray) { | ||
| 134 | next unless(defined($feature) && $feature->isa('EEDB::Feature')); | ||
| 135 | $self->{'features'}->{$feature->db_id} = $feature; | ||
| 136 | } | ||
| 137 | $self->{'count'} = undef; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub add_feature { | ||
| 141 | my $self = shift; | ||
| 142 | my @feature_list = @_; | ||
| 143 | |||
| 144 | return unless(@feature_list); | ||
| 145 | foreach my $feature (@feature_list) { | ||
| 146 | next unless(defined($feature) && $feature->isa('EEDB::Feature')); | ||
| 147 | $self->{'features'}->{$feature->db_id} = $feature; | ||
| 148 | } | ||
| 149 | $self->{'count'} = undef; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub count { | ||
| 153 | my $self = shift; | ||
| 154 | if(!defined($self->{'count'})) { | ||
| 155 | $self->{'count'} = scalar(keys(%{$self->{'features'}})); | ||
| 156 | } | ||
| 157 | return $self->{'count'}; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub features { | ||
| 161 | my $self = shift; | ||
| 162 | my $fsrc = shift; #optional FeatureSource; | ||
| 163 | |||
| 164 | my @feats = values(%{$self->{'features'}}); | ||
| 165 | if($fsrc and $fsrc->isa('EEDB::FeatureSource')) { | ||
| 166 | my @f2s; | ||
| 167 | foreach my $feature (@feats) { | ||
| 168 | next unless($feature and $feature->feature_source eq $fsrc); | ||
| 169 | push @f2s, $feature; | ||
| 170 | } | ||
| 171 | @feats = @f2s; | ||
| 172 | } | ||
| 173 | return \@feats; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub has_feature { | ||
| 177 | my $self = shift; | ||
| 178 | my $feature = shift; | ||
| 179 | |||
| 180 | return 0 unless(defined($feature) && $feature->isa('EEDB::Feature')); | ||
| 181 | return 1 if($self->{'features'}->{$feature->db_id}); | ||
| 182 | return 0; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | # id_list returns a string of ids which can be used as input to Edge::fetch_all_visible_with_feature_id_list | ||
| 187 | sub id_list { | ||
| 188 | my $self = shift; | ||
| 189 | my @ids; | ||
| 190 | foreach my $feature (@{$self->features}) { | ||
| 191 | push @ids, $feature->id; | ||
| 192 | } | ||
| 193 | my $id_list = join(',', @ids); | ||
| 194 | return $id_list; | ||
| 195 | } | ||
| 196 | |||
| 197 | #name_list returns a string of names white space separated, can be used in web display | ||
| 198 | sub name_list { | ||
| 199 | my $self = shift; | ||
| 200 | my @names; | ||
| 201 | foreach my $feature (@{$self->features}) { | ||
| 202 | push @names, $feature->primary_name; | ||
| 203 | } | ||
| 204 | return join(' ', @names);; | ||
| 205 | } | ||
| 206 | |||
| 207 | ################ | ||
| 208 | |||
| 209 | sub display_desc { | ||
| 210 | my $self = shift; | ||
| 211 | my $str = sprintf("FeatureSet(%s) %d features", $self->id, $self->count); | ||
| 212 | $str .= " :".$self->name if($self->name); | ||
| 213 | $str .= sprintf(" [%s]", $self->description) if($self->description); | ||
| 214 | return $str; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub simple_xml { | ||
| 218 | my $self = shift; | ||
| 219 | my $str = sprintf("<featureset name=\"%s\" count=\"%d\" />\n", $self->name, $self->count); | ||
| 220 | return $str; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub display_contents { | ||
| 224 | my $self = shift; | ||
| 225 | my $str = $self->display_desc . "\n"; | ||
| 226 | foreach my $feature (sort {($a->id <=> $b->id)} @{$self->features}) { | ||
| 227 | $str .= " " . $feature->simple_display_desc . "\n"; | ||
| 228 | } | ||
| 229 | return $str; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | 1; | ||
| 234 |