| 1 | =head1 NAME - EEDB::MetadataSet | ||
| 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_metadataset_global_counter = 1; | ||
| 46 | my $__riken_EEDB_metadataset_global_localcount = 1; | ||
| 47 | |||
| 48 | $VERSION = 0.953; | ||
| 49 | |||
| 50 | package EEDB::MetadataSet; | ||
| 51 | |||
| 52 | use strict; | ||
| 53 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 54 | |||
| 55 | use EEDB::Metadata; | ||
| 56 | use EEDB::Symbol; | ||
| 57 | |||
| 58 | use MQdb::MappedQuery; | ||
| 59 | our @ISA = qw(MQdb::MappedQuery); | ||
| 60 | |||
| 61 | ################################################# | ||
| 62 | # Class methods | ||
| 63 | ################################################# | ||
| 64 | |||
| 65 | sub class { return "MetadataSet"; } | ||
| 66 | |||
| 67 | ################################################# | ||
| 68 | # Instance methods | ||
| 69 | ################################################# | ||
| 70 | |||
| 71 | sub init { | ||
| 72 | my $self = shift; | ||
| 73 | my %args = @_; | ||
| 74 | $self->SUPER::init(@_); | ||
| 75 | |||
| 76 | $self->{'metadata'} = []; | ||
| 77 | #$self->primary_id($__riken_EEDB_metadataset_global_counter++); | ||
| 78 | |||
| 79 | if($args{'metadata'}) { $self->add_metadata(@{$args{'metadata'}}); } | ||
| 80 | |||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub copy { | ||
| 85 | my $self = shift; | ||
| 86 | |||
| 87 | my $copy = $self->SUPER::copy(); | ||
| 88 | |||
| 89 | $copy->primary_id($__riken_EEDB_metadataset_global_counter++); | ||
| 90 | |||
| 91 | #don't want to share a pointer to the internal symbol hash | ||
| 92 | #so create new hash and fill it. | ||
| 93 | $copy->{'metadata'} = []; | ||
| 94 | $copy->add_metadata(@{$self->metadata_list}); | ||
| 95 | return $copy; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub name { | ||
| 99 | my $self = shift; | ||
| 100 | return $self->{'name'} = shift if(@_); | ||
| 101 | return $self->{'name'}; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub description { | ||
| 105 | my $self = shift; | ||
| 106 | return $self->{'description'} = shift if(@_); | ||
| 107 | return $self->{'description'}; | ||
| 108 | } | ||
| 109 | |||
| 110 | ################## | ||
| 111 | |||
| 112 | sub merge_metadataset { | ||
| 113 | #merges the contents of the external set into this one | ||
| 114 | #also performs a 'remove_duplicates' after the merge | ||
| 115 | my $self = shift; | ||
| 116 | my $mdataset = shift; | ||
| 117 | |||
| 118 | return unless($mdataset); | ||
| 119 | $self->add_metadata(@{$mdataset->metadata_list}); | ||
| 120 | $self->remove_duplicates; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub add_metadata { | ||
| 124 | my $self = shift; | ||
| 125 | my @mdata_list = @_; | ||
| 126 | |||
| 127 | return unless(@mdata_list); | ||
| 128 | foreach my $mdata (@mdata_list) { | ||
| 129 | next unless(defined($mdata) && $mdata->isa('EEDB::Metadata')); | ||
| 130 | push @{$self->{'metadata'}}, $mdata; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub remove_metadata { | ||
| 135 | my $self = shift; | ||
| 136 | my @mdata_list = @_; | ||
| 137 | |||
| 138 | return unless(@mdata_list); | ||
| 139 | my @new_list; | ||
| 140 | foreach my $mdata1 (@{$self->{'metadata'}}) { | ||
| 141 | my $match=0; | ||
| 142 | foreach my $mdata2 (@mdata_list) { | ||
| 143 | next unless(defined($mdata2) && $mdata2->isa('EEDB::Metadata')); | ||
| 144 | if($mdata1->equals($mdata2)) { $match=1; } | ||
| 145 | } | ||
| 146 | if(!$match) { push @new_list, $mdata1}; | ||
| 147 | } | ||
| 148 | $self->{'metadata'} = \@new_list; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub add_tag_symbol { | ||
| 152 | #create new Symbol from tag/value | ||
| 153 | my $self = shift; | ||
| 154 | my $tag = shift; | ||
| 155 | my $value = shift; | ||
| 156 | |||
| 157 | my $symbol = EEDB::Symbol->new($tag, $value); | ||
| 158 | push @{$self->{'metadata'}}, $symbol; | ||
| 159 | return $symbol; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub add_tag_data { | ||
| 163 | #create new Metadata from tag/value | ||
| 164 | my $self = shift; | ||
| 165 | my $tag = shift; | ||
| 166 | my $data = shift; | ||
| 167 | |||
| 168 | my $mdata = EEDB::Metadata->new($tag, $data); | ||
| 169 | push @{$self->{'metadata'}}, $mdata; | ||
| 170 | return $mdata; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub remove_duplicates { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | my @newlist; | ||
| 177 | my $last = undef; | ||
| 178 | foreach my $mdata (sort {($b->class cmp $a->class) or | ||
| 179 | (lc($a->type) cmp lc($b->type)) or | ||
| 180 | (lc($a->data) cmp lc($b->data)) or | ||
| 181 | ($b->id cmp $a->id)} @{$self->metadata_list}) { | ||
| 182 | if(!defined($last)) { | ||
| 183 | $last = $mdata; | ||
| 184 | push @newlist, $mdata; | ||
| 185 | } else { | ||
| 186 | unless(($last->class eq $mdata->class) and | ||
| 187 | (lc($last->type) eq lc($mdata->type)) and | ||
| 188 | (lc($mdata->data) eq lc($last->data))) { | ||
| 189 | $last = $mdata; | ||
| 190 | push @newlist, $mdata; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | $self->{'metadata'} = \@newlist; | ||
| 195 | } | ||
| 196 | |||
| 197 | =head2 convert_bad_symbols | ||
| 198 | |||
| 199 | Description : Symbol is a special subclass of metadata which is supposed to be keyword-like | ||
| 200 | Symbols should have no whitespace and should be short enough to fit in the table | ||
| 201 | This method will double check the metadata and any improper Symbol is | ||
| 202 | converted into EEDB::Metadata and unlinked from database. | ||
| 203 | This is useful when mirroring data or prior to bulk loading. | ||
| 204 | Returntype : $self | ||
| 205 | Exceptions : none | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub convert_bad_symbols { | ||
| 210 | my $self = shift; | ||
| 211 | #since Symbol is a special subclass of Metadata and uses the same | ||
| 212 | #instance variables, I can just recast if there is a problem | ||
| 213 | |||
| 214 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 215 | my $value = $mdata->data; | ||
| 216 | if (($mdata->class eq "Symbol") and (($value =~ /\s/) or (length($value)>120))) { | ||
| 217 | #if has whitespace or it is too long then this is Metadata | ||
| 218 | bless $mdata, "EEDB::Metadata"; | ||
| 219 | $mdata->primary_id(undef); | ||
| 220 | $mdata->database(undef); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | return $self; | ||
| 224 | } | ||
| 225 | |||
| 226 | ################################################ | ||
| 227 | # | ||
| 228 | # list access methods | ||
| 229 | # | ||
| 230 | ################################################ | ||
| 231 | |||
| 232 | sub count { | ||
| 233 | my $self = shift; | ||
| 234 | return scalar(@{$self->{'metadata'}}); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub metadata_list { | ||
| 238 | my $self = shift; | ||
| 239 | return $self->{'metadata'}; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub id_list { | ||
| 243 | my $self = shift; | ||
| 244 | my @ids; | ||
| 245 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 246 | push @ids, $mdata->id; | ||
| 247 | } | ||
| 248 | my $id_list = join(',', @ids); | ||
| 249 | return $id_list; | ||
| 250 | } | ||
| 251 | |||
| 252 | #name_list returns a string of names white space separated, can be used in web display | ||
| 253 | sub value_list { | ||
| 254 | my $self = shift; | ||
| 255 | my @names; | ||
| 256 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 257 | push @names, $mdata->data; | ||
| 258 | } | ||
| 259 | return join(' ', @names);; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub tag_data_list { | ||
| 263 | my $self = shift; | ||
| 264 | my @symlist; | ||
| 265 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 266 | push @symlist, [$mdata->type, $mdata->data]; | ||
| 267 | } | ||
| 268 | return \@symlist; | ||
| 269 | } | ||
| 270 | |||
| 271 | ################################################ | ||
| 272 | # | ||
| 273 | # search methods | ||
| 274 | # | ||
| 275 | ################################################ | ||
| 276 | |||
| 277 | sub find_metadata { | ||
| 278 | #returns first occurance matching search pattern | ||
| 279 | my $self = shift; | ||
| 280 | my $tag = shift; | ||
| 281 | my $value = shift; | ||
| 282 | |||
| 283 | return undef unless(defined($tag) or defined($value)); | ||
| 284 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 285 | if(defined($tag) and defined($value)) { | ||
| 286 | if(($mdata->type eq $tag) and ($mdata->data eq $value)) { return $mdata; } | ||
| 287 | } else { | ||
| 288 | if(defined($tag) and ($mdata->type eq $tag)) { return $mdata; } | ||
| 289 | if(defined($value) and ($mdata->data eq $value)) { return $mdata; } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | return undef; | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | sub find_all_metadata_like { | ||
| 297 | #returns simple array reference of mdata | ||
| 298 | #finds all occurance matching search pattern | ||
| 299 | #tag (if specified) must match exactly, but value(if specified) is allowed to be a 'prefix' | ||
| 300 | my $self = shift; | ||
| 301 | my $tag = shift; # can be undef | ||
| 302 | my $value = shift; #optional | ||
| 303 | |||
| 304 | my @rtn_mdata; | ||
| 305 | |||
| 306 | return undef unless(defined($tag) or defined($value)); | ||
| 307 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 308 | if(defined($tag) and defined($value)) { | ||
| 309 | if(($mdata->type eq $tag) and ($mdata->data =~ /^$value/)) { push @rtn_mdata, $mdata; } | ||
| 310 | } else { | ||
| 311 | if(defined($tag) and ($mdata->type eq $tag)) { push @rtn_mdata, $mdata; } | ||
| 312 | if(defined($value) and ($mdata->data =~ /^$value/)) { push @rtn_mdata, $mdata; } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | return \@rtn_mdata; | ||
| 316 | } | ||
| 317 | |||
| 318 | ################ | ||
| 319 | |||
| 320 | sub display_desc { | ||
| 321 | my $self = shift; | ||
| 322 | my $str = sprintf("MetadataSet(%s) %d symbols", $self->id, $self->count); | ||
| 323 | $str .= " :".$self->name if($self->name); | ||
| 324 | $str .= sprintf(" [%s]", $self->description) if($self->description); | ||
| 325 | return $str; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub display_contents { | ||
| 329 | my $self = shift; | ||
| 330 | my $str = ""; | ||
| 331 | foreach my $mdata (sort {($b->class cmp $a->class) or ($a->type cmp $b->type) or ($a->data cmp $b->data)} @{$self->metadata_list}) { | ||
| 332 | $str .= " " . $mdata->display_contents . "\n"; | ||
| 333 | } | ||
| 334 | return $str; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub xml { | ||
| 338 | my $self = shift; | ||
| 339 | my $str = ""; | ||
| 340 | my $mdata_list = $self->metadata_list; | ||
| 341 | foreach my $mdata (sort {($b->class cmp $a->class) or ($a->type cmp $b->type)} @$mdata_list) { | ||
| 342 | next if($mdata->type eq 'keyword'); #always hide the 'keywords' from the xml output | ||
| 343 | $str .= " " . $mdata->xml; | ||
| 344 | } | ||
| 345 | return $str; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub gff_description { | ||
| 349 | #the GFF2 format is not very robust so some metadata can not be outputed in this format | ||
| 350 | #instead of manipulating the data, this data is skipped in the gff output | ||
| 351 | my $self = shift; | ||
| 352 | |||
| 353 | my $str =''; | ||
| 354 | my $sym_hash ={}; | ||
| 355 | foreach my $mdata (sort {($b->class cmp $a->class) or ($a->type cmp $b->type) or ($a->data cmp $b->data)} @{$self->metadata_list}) { | ||
| 356 | next unless($mdata->class eq 'Symbol'); | ||
| 357 | next if($mdata->type eq 'keyword'); #always hide the 'keywords' from the gff metadata output | ||
| 358 | next if($mdata->data =~ /[\s\"\",]/); | ||
| 359 | next if($mdata->data =~ /,/); | ||
| 360 | #my $data = $mdata->data; | ||
| 361 | #$data =~ s/\"/\\\"/g; | ||
| 362 | #$str .= sprintf("%s=\"%s\"", $mdata->type, $data); | ||
| 363 | my $data = $sym_hash->{$mdata->type}; | ||
| 364 | if(defined($data)) { $data .= ","; } | ||
| 365 | $data .= $mdata->data; | ||
| 366 | $sym_hash->{$mdata->type} = $data; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | my $first=1; | ||
| 371 | foreach my $key (sort keys(%$sym_hash)) { | ||
| 372 | unless($first) { $str .= ";"; } | ||
| 373 | $first=0; | ||
| 374 | my $data = $sym_hash->{$key}; | ||
| 375 | $str .= sprintf("_%s=\"%s\"", $key, $data); | ||
| 376 | } | ||
| 377 | return $str; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | ################################ | ||
| 382 | |||
| 383 | sub store { | ||
| 384 | #just makes sure the Metadata/Symbols are stored/synched with database | ||
| 385 | #used prior to doing linkage | ||
| 386 | my $self = shift; | ||
| 387 | my $db = shift; | ||
| 388 | |||
| 389 | unless($db) { return undef; } | ||
| 390 | foreach my $mdata (@{$self->metadata_list}) { | ||
| 391 | if($mdata->id eq '') { $mdata->store($db); } | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | |||
| 396 | 1; | ||
| 397 |