| 1 | #$Id: OntologyServer.pm,v 1.3 2008/09/02 13:14:18 kawas Exp $ | ||
| 2 | # this module needs to talk to the 'real' ontology | ||
| 3 | # server as well as the MOBY Central database | ||
| 4 | # in order to ensure that they are both in sync | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | MOBY::OntologyServer - A way for MOBY Central to query the | ||
| 9 | object, service, namespace, and relationship ontologies | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use MOBY::OntologyServer; | ||
| 16 | my $OS = MOBY::OntologyServer->new(ontology => "object"); | ||
| 17 | |||
| 18 | my ($success, $message, $existingURI) = $OS->objectExists(term => "Object"); | ||
| 19 | |||
| 20 | if ($success){ | ||
| 21 | print "object exists and it has the LSID $existingURI\n"; | ||
| 22 | } else { | ||
| 23 | print "object does not exist; additional message from server: $message\n"; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Swappable interface to ontologies. It should deal with LSID's 100% | ||
| 32 | of the time, and also deal with MOBY-specific common names for objects, | ||
| 33 | services, namespaces, and relationship types. | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | =head1 AUTHORS | ||
| 38 | |||
| 39 | Mark Wilkinson (markw@illuminae.com) | ||
| 40 | |||
| 41 | BioMOBY Project: http://www.biomoby.org | ||
| 42 | |||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | |||
| 49 | =head2 new | ||
| 50 | |||
| 51 | Title : new | ||
| 52 | Usage : my $OS = MOBY::OntologyServer->new(%args) | ||
| 53 | Function : | ||
| 54 | Returns : MOBY::OntologyServer object | ||
| 55 | Args : ontology => [object || service || namespace || relationship] | ||
| 56 | database => mysql databasename that holds the ontologies | ||
| 57 | host => mysql hostname | ||
| 58 | username => mysql username | ||
| 59 | password => mysql password | ||
| 60 | port => mysql port | ||
| 61 | dbh => pre-existing database handle to a mysql database | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | package MOBY::OntologyServer; | ||
| 66 | |||
| 67 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 68 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 69 | 1 | ▶ | use vars qw($AUTOLOAD); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | 1 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 71 | 0 | ▶ | use DBD::mysql; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 72 | use MOBY::Config; | ||
| 73 | |||
| 74 | use vars qw /$VERSION/; | ||
| 75 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 76 | |||
| 77 | my $debug = 0; | ||
| 78 | { | ||
| 79 | |||
| 80 | #Encapsulated class data | ||
| 81 | #___________________________________________________________ | ||
| 82 | #ATTRIBUTES | ||
| 83 | my %_attr_data = # DEFAULT ACCESSIBILITY | ||
| 84 | ( | ||
| 85 | ontology => [ undef, 'read/write' ], | ||
| 86 | database => [ undef, 'read/write' ], | ||
| 87 | host => [ undef, 'read/write' ], | ||
| 88 | username => [ undef, 'read/write' ], | ||
| 89 | password => [ undef, 'read/write' ], | ||
| 90 | port => [ undef, 'read/write' ], | ||
| 91 | dbh => [ undef, 'read/write' ], | ||
| 92 | ); | ||
| 93 | |||
| 94 | #_____________________________________________________________ | ||
| 95 | # METHODS, to operate on encapsulated class data | ||
| 96 | # Is a specified object attribute accessible in a given mode | ||
| 97 | sub _accessible { | ||
| 98 | my ( $self, $attr, $mode ) = @_; | ||
| 99 | $_attr_data{$attr}[1] =~ /$mode/; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Classwide default value for a specified object attribute | ||
| 103 | sub _default_for { | ||
| 104 | my ( $self, $attr ) = @_; | ||
| 105 | $_attr_data{$attr}[0]; | ||
| 106 | } | ||
| 107 | |||
| 108 | # List of names of all specified object attributes | ||
| 109 | sub _standard_keys { | ||
| 110 | keys %_attr_data; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | sub new { | ||
| 115 | my ( $caller, %args ) = @_; | ||
| 116 | my $caller_is_obj = ref($caller); | ||
| 117 | my $class = $caller_is_obj || $caller; | ||
| 118 | my $self = bless {}, $class; | ||
| 119 | foreach my $attrname ( $self->_standard_keys ) { | ||
| 120 | if ( exists $args{$attrname} && defined $args{$attrname} ) { | ||
| 121 | $self->{$attrname} = $args{$attrname}; | ||
| 122 | } elsif ($caller_is_obj) { | ||
| 123 | $self->{$attrname} = $caller->{$attrname}; | ||
| 124 | } else { | ||
| 125 | $self->{$attrname} = $self->_default_for($attrname); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | $self->ontology eq 'object' && $self->database('mobyobject'); | ||
| 129 | $self->ontology eq 'namespace' && $self->database('mobynamespace'); | ||
| 130 | $self->ontology eq 'service' && $self->database('mobyservice'); | ||
| 131 | $self->ontology eq 'relationship' && $self->database('mobyrelationship'); | ||
| 132 | |||
| 133 | #print STDERR "\n\nCONFIG object is $CONFIG\n\n"; | ||
| 134 | $CONFIG ||= MOBY::Config->new; | ||
| 135 | |||
| 136 | #print STDERR "got username ",($CONFIG->{mobycentral}->{username})," for mobycentral\n"; | ||
| 137 | $self->username( $CONFIG->{ $self->database }->{username} ) | ||
| 138 | unless $self->username; | ||
| 139 | $self->password( $CONFIG->{ $self->database }->{password} ) | ||
| 140 | unless $self->password; | ||
| 141 | $self->port( $CONFIG->{ $self->database }->{port} ) unless $self->port; | ||
| 142 | $self->host( $CONFIG->{ $self->database }->{url} ) unless $self->host; | ||
| 143 | my $host = $self->host ? $self->host : $ENV{MOBY_CENTRAL_URL}; | ||
| 144 | chomp $host; | ||
| 145 | my $username = | ||
| 146 | $self->username ? $self->username : $ENV{MOBY_CENTRAL_DBUSER}; | ||
| 147 | chomp $username; | ||
| 148 | my $password = | ||
| 149 | $self->password ? $self->password : $ENV{MOBY_CENTRAL_DBPASS}; | ||
| 150 | chomp $password if $password; | ||
| 151 | $password =~ s/\s//g if $password; | ||
| 152 | my $port = $self->port ? $self->port : $ENV{MOBY_CENTRAL_DBPORT}; | ||
| 153 | chomp $port; | ||
| 154 | my ($dsn) = | ||
| 155 | "DBI:mysql:" | ||
| 156 | . ( $CONFIG->{ $self->database }->{dbname} ) . ":" | ||
| 157 | . ($host) . ":" | ||
| 158 | . ($port); | ||
| 159 | |||
| 160 | #print STDERR "\n\nDSN was $dsn\n\n"; | ||
| 161 | my $dbh; | ||
| 162 | |||
| 163 | # $debug && &_LOG("connecting to db with params ",$self->database, $self->username, $self->password,"\n"); | ||
| 164 | if ( defined $password ) { | ||
| 165 | $dbh = DBI->connect( $dsn, $username, $password, { RaiseError => 1 } ) | ||
| 166 | or die "can't connect to database"; | ||
| 167 | } else { | ||
| 168 | $dbh = DBI->connect( $dsn, $username, undef, { RaiseError => 1 } ) | ||
| 169 | or die "can't connect to database"; | ||
| 170 | } | ||
| 171 | |||
| 172 | # $debug && &_LOG("CONNECTED!\n"); | ||
| 173 | if ($dbh) { | ||
| 174 | $self->dbh($dbh); | ||
| 175 | return $self; | ||
| 176 | } else { | ||
| 177 | return undef; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 objectExists | ||
| 182 | |||
| 183 | moby:newterm will return (0, $message, $MOBYLSID) | ||
| 184 | newterm will return (0, $message, $MOBYLSID | ||
| 185 | oldterm will return (1, $message, undef) | ||
| 186 | newLSID will return (0, $desc, $lsid) | ||
| 187 | |||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub objectExists { | ||
| 192 | my ( $self, %args ) = @_; | ||
| 193 | |||
| 194 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 195 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 196 | |||
| 197 | my $term = $args{term}; | ||
| 198 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 199 | my $sth; | ||
| 200 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' ); | ||
| 201 | return (0, undef, undef) unless $term; | ||
| 202 | |||
| 203 | my $result; | ||
| 204 | |||
| 205 | $result = $adaptor->query_object(type => $term); | ||
| 206 | |||
| 207 | my $row = shift(@$result); | ||
| 208 | my $lsid = $row->{object_lsid}; | ||
| 209 | my $type = $row->{object_type}; | ||
| 210 | my $desc = $row->{description}; | ||
| 211 | my $auth = $row->{authority}; | ||
| 212 | my $email = $row->{contact_email}; | ||
| 213 | |||
| 214 | if ($lsid) | ||
| 215 | { # if it is in there, then it has been discovered regardless of being foreign or not | ||
| 216 | return ( 1, $desc, $lsid ); | ||
| 217 | } elsif ( _isForeignLSID($term) ) | ||
| 218 | { # if not in our ontology, but is a foreign LSID, then pass it back verbatim | ||
| 219 | return ( | ||
| 220 | 0, | ||
| 221 | "LSID $term does not exist in the biomoby.org Object Class system\n", | ||
| 222 | $term | ||
| 223 | ); | ||
| 224 | } else { # under all other circumstances (i.e. not a term, or a non-existent biomoby LSID) then fail | ||
| 225 | return ( | ||
| 226 | 0, | ||
| 227 | "Object type $term does not exist in the biomoby.org Object Class system\n", | ||
| 228 | '' | ||
| 229 | ); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 objectInfo | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub objectInfo{ | ||
| 238 | my ( $self, %args ) = @_; | ||
| 239 | |||
| 240 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 241 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 242 | |||
| 243 | my $term = $args{term}; | ||
| 244 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 245 | my $sth; | ||
| 246 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' ); | ||
| 247 | return (0, undef, undef) unless $term; | ||
| 248 | |||
| 249 | my $result; | ||
| 250 | |||
| 251 | $result = $adaptor->query_object(type => $term); | ||
| 252 | my $row = shift(@$result); | ||
| 253 | #my $lsid = $row->{object_lsid}; | ||
| 254 | #my $type = $row->{object_type}; | ||
| 255 | #my $desc = $row->{description}; | ||
| 256 | #my $auth = $row->{authority}; | ||
| 257 | #my $email = $row->{contact_email}; | ||
| 258 | # | ||
| 259 | if ($row->{object_lsid}) | ||
| 260 | { # if it is in there, then it has been discovered regardless of being foreign or not | ||
| 261 | return $row; | ||
| 262 | } elsif ( _isForeignLSID($term) ) { # if not in our ontology, but is a foreign LSID, then pass it back verbatim | ||
| 263 | return {object_lsid => $term, | ||
| 264 | object_type => $term, | ||
| 265 | description => "LSID $term does not exist in the biomoby.org Object Class system\n", | ||
| 266 | authority => "", | ||
| 267 | contact_email => "", | ||
| 268 | }; | ||
| 269 | } else { # under all other circumstances (i.e. not a term, or a non-existent biomoby LSID) then fail | ||
| 270 | return {object_lsid => "", | ||
| 271 | object_type => "", | ||
| 272 | description => "LSID $term does not exist in the biomoby.org Object Class system\n", | ||
| 273 | authority => "", | ||
| 274 | contact_email => "", | ||
| 275 | }; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | =head2 serviceInfo | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub serviceInfo{ | ||
| 285 | my ( $self, %args ) = @_; | ||
| 286 | |||
| 287 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 288 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 289 | |||
| 290 | my $term = $args{term}; | ||
| 291 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 292 | my $sth; | ||
| 293 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' ); | ||
| 294 | return (0, undef, undef) unless $term; | ||
| 295 | |||
| 296 | my $result; | ||
| 297 | |||
| 298 | $result = $adaptor->query_service(type => $term); | ||
| 299 | my $row = shift(@$result); | ||
| 300 | |||
| 301 | if ($row->{service_lsid}) | ||
| 302 | { # if it is in there, then it has been discovered regardless of being foreign or not | ||
| 303 | return $row; | ||
| 304 | } elsif ( _isForeignLSID($term) ) { # if not in our ontology, but is a foreign LSID, then pass it back verbatim | ||
| 305 | return {service_lsid => $term, | ||
| 306 | service_type => $term, | ||
| 307 | description => "LSID $term does not exist in the biomoby.org Object Class system\n", | ||
| 308 | authority => "", | ||
| 309 | contact_email => "", | ||
| 310 | }; | ||
| 311 | } else { # under all other circumstances (i.e. not a term, or a non-existent biomoby LSID) then fail | ||
| 312 | return {service_lsid => "", | ||
| 313 | service_type => "", | ||
| 314 | description => "LSID $term does not exist in the biomoby.org Object Class system\n", | ||
| 315 | authority => "", | ||
| 316 | contact_email => "", | ||
| 317 | }; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | sub _isMOBYLSID { | ||
| 322 | my ($lsid) = @_; | ||
| 323 | return 1 if $lsid =~ /^urn\:lsid\:biomoby.org/; | ||
| 324 | return 0; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub _isForeignLSID { | ||
| 328 | my ($lsid) = @_; | ||
| 329 | return 0 if $lsid =~ /^urn\:lsid\:biomoby.org/; | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | =head2 createObject | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub createObject { | ||
| 338 | my ( $self, %args ) = @_; | ||
| 339 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 340 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 341 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' ); | ||
| 342 | return ( 0, "requires a object type node", '' ) unless ( $args{node} ); | ||
| 343 | return ( 0, "requires an authURI ", '' ) unless ( $args{authority} ); | ||
| 344 | return ( 0, "requires a contact email address", '' ) | ||
| 345 | unless ( $args{contact_email} ); | ||
| 346 | return ( 0, "requires a object description", '' ) | ||
| 347 | unless ( $args{description} ); | ||
| 348 | my $term = $args{node}; | ||
| 349 | |||
| 350 | my $result; | ||
| 351 | $result = $adaptor->query_object(type => $term); | ||
| 352 | my $row = shift(@$result); | ||
| 353 | my $lsid = $row->{object_lsid}; | ||
| 354 | my $type = $row->{object_type}; | ||
| 355 | my $desc = $row->{description}; | ||
| 356 | my $auth = $row->{authority}; | ||
| 357 | my $email = $row->{contact_email}; | ||
| 358 | |||
| 359 | if ($lsid) { # if it is in there, then the object exists | ||
| 360 | return ( 0, "This term already exists: $lsid", $lsid ); | ||
| 361 | } | ||
| 362 | my $LSID = $self->setURI( $term ); | ||
| 363 | unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) } | ||
| 364 | $args{description} =~ s/^\s+(.*?)\s+$/$1/s; | ||
| 365 | $args{node} =~ s/^\s+(.*?)\s+$/$1/s; | ||
| 366 | $args{contact_email} =~ s/^\s+(.*?)\s+$/$1/s; | ||
| 367 | $args{authority} =~ s/^\s+(.*?)\s+$/$1/s; | ||
| 368 | |||
| 369 | my $insertid = $adaptor->insert_object(object_type => $args{'node'}, | ||
| 370 | object_lsid => $LSID, | ||
| 371 | description => $args{'description'}, | ||
| 372 | authority => $args{'authority'}, | ||
| 373 | contact_email => $args{'contact_email'}); | ||
| 374 | unless ( $insertid ) { | ||
| 375 | return ( 0, "Object creation failed for unknown reasons", '' ); | ||
| 376 | } | ||
| 377 | return ( 1, "Object creation succeeded", $LSID ); | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 retrieveObject | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | sub retrieveObject { | ||
| 385 | my ( $self, %args ) = @_; | ||
| 386 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 387 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 388 | my $term = $args{'type'}; | ||
| 389 | $term ||=$args{'node'}; | ||
| 390 | |||
| 391 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' ); | ||
| 392 | return ( 0, "requires a object type node as an argument", '' ) | ||
| 393 | unless ( $term ); | ||
| 394 | my $LSID = | ||
| 395 | ( $term =~ /urn\:lsid/ ) | ||
| 396 | ? $term | ||
| 397 | : $self->getObjectURI($term); | ||
| 398 | unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) } | ||
| 399 | my $result = $adaptor->query_object(type => $LSID); | ||
| 400 | my $row = shift(@$result); | ||
| 401 | my $type = $row->{object_type}; | ||
| 402 | my $lsid = $row->{object_lsid}; | ||
| 403 | my $desc = $row->{description}; | ||
| 404 | my $auth = $row->{authority}; | ||
| 405 | my $contact = $row->{contact_email}; | ||
| 406 | |||
| 407 | unless ($lsid) { return ( 0, "Object doesn't exist in ontology", "" ) } | ||
| 408 | |||
| 409 | $result = $adaptor->get_object_relationships(type => $lsid); | ||
| 410 | my %rel; | ||
| 411 | foreach my $row (@$result) | ||
| 412 | { | ||
| 413 | my $relationship_type = $row->{relationship_type}; | ||
| 414 | my $objectlsid = $row->{object_lsid}; | ||
| 415 | my $article = $row->{object2_articlename}; | ||
| 416 | my $contact = $row->{contact_email}; | ||
| 417 | my $def = $row->{definition}; | ||
| 418 | my $auth = $row->{authority}; | ||
| 419 | my $type = $row->{object_type}; | ||
| 420 | |||
| 421 | push @{ $rel{$relationship_type} }, [ $objectlsid, $article, $type, $def, $auth, $contact ]; | ||
| 422 | } | ||
| 423 | return { | ||
| 424 | objectType => $type, | ||
| 425 | objectLSID => $lsid, | ||
| 426 | description => $desc, | ||
| 427 | contactEmail => $contact, | ||
| 428 | authURI => $auth, | ||
| 429 | Relationships => \%rel | ||
| 430 | }; | ||
| 431 | } | ||
| 432 | |||
| 433 | =head2 deprecateObject | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub deprecateObject { | ||
| 438 | my ( $self, %args ) = @_; | ||
| 439 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 440 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 441 | |||
| 442 | return ( 0, "WRONG ONTOLOGY", '' ) unless ( $self->ontology eq 'object' ); | ||
| 443 | my $term = $args{term}; | ||
| 444 | |||
| 445 | # if ($term =~ /^urn:lsid/ && !($term =~ /^urn:lsid:biomoby.org:objectclass/)){ | ||
| 446 | # return (0, "can't delete from external ontology", $term); | ||
| 447 | # } | ||
| 448 | my $LSID; | ||
| 449 | unless ( $term =~ /urn\:lsid/ ) { $LSID = $self->getObjectURI($term) } else { $LSID = $term } | ||
| 450 | return ( 0, "Object type $term cannot be resolved to an LSID", "" ) | ||
| 451 | unless $LSID; | ||
| 452 | |||
| 453 | my $result = $adaptor->query_object(type => $LSID); | ||
| 454 | my $row = shift(@$result); | ||
| 455 | my $id = $row->{object_id}; | ||
| 456 | my $lsid = $row->{object_lsid}; | ||
| 457 | |||
| 458 | # object1_id ISA object2_id? | ||
| 459 | my $isa = $adaptor->query_object_term2term(type => $lsid); | ||
| 460 | my $isas = shift @$isa; | ||
| 461 | if ( $isas->{object1_id}) { | ||
| 462 | return ( 0, | ||
| 463 | qq{Object type $term has object dependencies in the ontology}, | ||
| 464 | $lsid ); | ||
| 465 | } | ||
| 466 | |||
| 467 | my ($err, $errstr) = $adaptor->delete_object(type => $lsid); | ||
| 468 | if ( $err ) { | ||
| 469 | return ( 0, "Delete from Object Class table failed: $errstr", | ||
| 470 | $lsid ); | ||
| 471 | } | ||
| 472 | return ( 1, "Object $term Deleted", $lsid ); | ||
| 473 | } | ||
| 474 | |||
| 475 | =head2 deleteObject | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub deleteObject { | ||
| 480 | my $self = shift; | ||
| 481 | $self->deprecateObject(@_); | ||
| 482 | } | ||
| 483 | |||
| 484 | =head2 relationshipExists | ||
| 485 | |||
| 486 | =cut | ||
| 487 | |||
| 488 | sub relationshipExists { | ||
| 489 | |||
| 490 | # term => $term | ||
| 491 | # ontology => $ontology | ||
| 492 | my ( $self, %args ) = @_; | ||
| 493 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 494 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyrelationship' ); | ||
| 495 | return ( 0, "WRONG ONTOLOGY!", '' ) | ||
| 496 | unless ( $self->ontology eq 'relationship' ); | ||
| 497 | my $term = lc( $args{term} ); | ||
| 498 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 499 | my $ont = $args{ontology}; | ||
| 500 | return ( 0, "requires both term and ontology arguments\n", '' ) | ||
| 501 | unless ( defined($term) && defined($ont) ); | ||
| 502 | my $result; | ||
| 503 | if ( $term =~ /^urn\:lsid/ ) { | ||
| 504 | |||
| 505 | $result = $adaptor->query_relationship( | ||
| 506 | type => $term, | ||
| 507 | ontology => $ont); | ||
| 508 | |||
| 509 | } else { | ||
| 510 | |||
| 511 | $result = $adaptor->query_relationship(type => $term, ontology => $ont); | ||
| 512 | |||
| 513 | } | ||
| 514 | my $row = shift(@$result); | ||
| 515 | my $lsid = $row->{relationship_lsid}; | ||
| 516 | my $type = $row->{relationship_type}; | ||
| 517 | my $desc = $row->{description}; | ||
| 518 | my $auth = $row->{authority}; | ||
| 519 | my $email = $row->{contact_email}; | ||
| 520 | if ($lsid) { | ||
| 521 | return ( 1, $desc, $lsid, $type, $auth, $email ); | ||
| 522 | } else { | ||
| 523 | return ( | ||
| 524 | 0,"Relationship Type $term does not exist in the biomoby.org Relationship Type system\n", | ||
| 525 | '', '', '', '' | ||
| 526 | ); | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | =head2 addObjectRelationship | ||
| 531 | |||
| 532 | =cut | ||
| 533 | |||
| 534 | sub addObjectRelationship { | ||
| 535 | |||
| 536 | # adds a relationship | ||
| 537 | #subject_node => $term, | ||
| 538 | #relationship => $reltype, | ||
| 539 | #object_node => $objectType, | ||
| 540 | #articleName => $articleName, | ||
| 541 | #authority => $auth, | ||
| 542 | #contact_email => $email | ||
| 543 | my ( $self, %args ) = @_; | ||
| 544 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 545 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 546 | |||
| 547 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'object' ); | ||
| 548 | |||
| 549 | my $result = $adaptor->query_object(type => $args{subject_node}); | ||
| 550 | my $row = shift(@$result); | ||
| 551 | my $subj_lsid = $row->{object_lsid}; | ||
| 552 | return ( 0, qq{Object type $args{subject_node} does not exist in the ontology}, '' ) | ||
| 553 | unless defined $subj_lsid; | ||
| 554 | |||
| 555 | $result = $adaptor->query_object(type => $args{object_node}); | ||
| 556 | $row = shift(@$result); | ||
| 557 | my $obj_lsid = $row->{object_lsid}; | ||
| 558 | return ( 0,qq{Object type $args{object_node} does not exist in the ontology},'' ) | ||
| 559 | unless defined $obj_lsid; | ||
| 560 | my $isa = $adaptor->query_object_term2term(type => $subj_lsid); | ||
| 561 | my $isarow = shift @$isa; | ||
| 562 | if ( $isarow->{object_lsid} ) { | ||
| 563 | return ( | ||
| 564 | 0, | ||
| 565 | qq{Object type $args{subject_node} has existing object dependencies in the ontology. It cannot be changed.}, | ||
| 566 | $subj_lsid | ||
| 567 | ); | ||
| 568 | } | ||
| 569 | my $OE = MOBY::OntologyServer->new( ontology => 'relationship' ); | ||
| 570 | my ( $success, $desc, $rel_lsid ) = $OE->relationshipExists( | ||
| 571 | term => $args{relationship}, | ||
| 572 | ontology => 'object' ); | ||
| 573 | ($success) || return ( 0, | ||
| 574 | qq{Relationship $args{relationship} does not exist in the ontology}, | ||
| 575 | '' ); | ||
| 576 | |||
| 577 | # need to ensure that identical article names dont' end up at the same level | ||
| 578 | my $articleNameInvalid = &_testIdenticalArticleName(term => $subj_lsid, articleName => $args{articleName}); | ||
| 579 | return (0, "Object will have conflicting articleName ".($args{articleName}), '') if $articleNameInvalid; | ||
| 580 | |||
| 581 | my $insertid = $adaptor->insert_object_term2term(relationship_type => $rel_lsid, | ||
| 582 | object1_type => $subj_lsid, | ||
| 583 | object2_type => $obj_lsid, | ||
| 584 | object2_articlename => $args{articleName}); | ||
| 585 | |||
| 586 | |||
| 587 | if ($insertid ) { | ||
| 588 | return ( 1, "Object relationsihp created successfully", '' ); | ||
| 589 | } else { | ||
| 590 | return ( 0, "Object relationship creation failed for unknown reasons", | ||
| 591 | '' ); | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | sub _testIdenticalArticleName { | ||
| 596 | my (%args)= @_; | ||
| 597 | my $term = $args{term}; | ||
| 598 | my $articleName = $args{articleName}; | ||
| 599 | my $foundCommonArticleNameFlag = 0; | ||
| 600 | # need to first traverse down the ISA pathway to root | ||
| 601 | # then for each ISA test the hAS and HASA's for their articlenames and see if they are the same | ||
| 602 | # case insensitive? | ||
| 603 | my $OS = MOBY::OntologyServer->new(ontology => 'object'); | ||
| 604 | my $OSrel = MOBY::OntologyServer->new(ontology => 'relationship'); | ||
| 605 | my ($exists1, $desc, $isalsid) = $OSrel->relationshipExists(term => 'isa', ontology => 'object'); | ||
| 606 | my ($exists2, $desc2, $hasalsid) = $OSrel->relationshipExists(term => 'hasa', ontology => 'object'); | ||
| 607 | my ($exists3, $desc3, $haslsid) = $OSrel->relationshipExists(term => 'has', ontology => 'object'); | ||
| 608 | |||
| 609 | return 1 unless ($exists1 && $exists2 && $exists3); # this is bad, since it returns boolean suggesting that it found a common articlename rather than finding that a given relationship doesn't exist, but... hey.... | ||
| 610 | # check the hasa relationships for common articleName | ||
| 611 | $foundCommonArticleNameFlag += _compareArticleNames(OS => $OS, type => $args{term}, relationship => $hasalsid, targetArticleName => $articleName); | ||
| 612 | # check the has relationships for common articleName | ||
| 613 | $foundCommonArticleNameFlag += _compareArticleNames(OS => $OS, type => $args{term}, relationship => $haslsid, targetArticleName => $articleName); | ||
| 614 | |||
| 615 | # now get all of its inherited parents | ||
| 616 | my $relationships = $OS->Relationships( | ||
| 617 | ontology => 'object', | ||
| 618 | term => $args{term}, | ||
| 619 | relationship => $isalsid, | ||
| 620 | direction => 'root', | ||
| 621 | expand => 1); | ||
| 622 | #relationships{relationship} = [[lsid1,articleNmae], [lsid2, articleName], [lsid3, articleName]] | ||
| 623 | my ($isa) = keys(%$relationships); # can only be one key returned, and must be isa in this case | ||
| 624 | my @ISAlist; | ||
| 625 | (@ISAlist = @{$relationships->{$isa}}) if ($relationships->{$isa}) ; | ||
| 626 | # for each of the inherited parents, check their articleNames | ||
| 627 | foreach my $ISA(@ISAlist){ # $ISA = [lsid, articleName] (but articleName shuld be null anyway in this case) | ||
| 628 | my $what_it_is = $ISA->{lsid}; | ||
| 629 | # check the hasa relationships for common articleName | ||
| 630 | $foundCommonArticleNameFlag += _compareArticleNames(OS => $OS, type => $what_it_is, relationship => $hasalsid, targetArticleName => $articleName); | ||
| 631 | # check the has relationships for common articleName | ||
| 632 | $foundCommonArticleNameFlag += _compareArticleNames(OS => $OS, type => $what_it_is, relationship => $haslsid, targetArticleName => $articleName); | ||
| 633 | } | ||
| 634 | return $foundCommonArticleNameFlag; | ||
| 635 | } | ||
| 636 | |||
| 637 | sub _compareArticleNames { | ||
| 638 | my (%args) = @_; | ||
| 639 | my $OS = $args{OS}; | ||
| 640 | my $what_it_is = $args{type}; | ||
| 641 | my $lsid = $args{relationship}; | ||
| 642 | my $targetArticleName = $args{targetArticleName}; | ||
| 643 | my $foundCommonArticleNameFlag = 0; | ||
| 644 | my $contents = $OS->Relationships( | ||
| 645 | ontology => 'object', | ||
| 646 | term => $what_it_is, | ||
| 647 | relationship => $lsid, | ||
| 648 | direction => 'root', | ||
| 649 | ); | ||
| 650 | if ($contents){ | ||
| 651 | #$hasarelationships{relationship} = [[lsid1,articleNmae], [lsid2, articleName], [lsid3, articleName]] | ||
| 652 | my ($content) = keys(%$contents); | ||
| 653 | if ($contents->{$content}){ | ||
| 654 | my @CONTENTlist = @{$contents->{$content}}; | ||
| 655 | foreach my $CONTAINED(@CONTENTlist){ | ||
| 656 | $foundCommonArticleNameFlag = 1 if ($CONTAINED->{articleName} eq $targetArticleName); #->[1] is the articleName field | ||
| 657 | } | ||
| 658 | } | ||
| 659 | } | ||
| 660 | return $foundCommonArticleNameFlag; | ||
| 661 | } | ||
| 662 | |||
| 663 | =head2 addServiceRelationship | ||
| 664 | |||
| 665 | =cut | ||
| 666 | |||
| 667 | sub addServiceRelationship { | ||
| 668 | |||
| 669 | # adds an ISA relationship | ||
| 670 | # fail if another object is in relation to this objevt | ||
| 671 | #subject_node => $term, | ||
| 672 | #relationship => $relationship, | ||
| 673 | #predicate_node => $pred | ||
| 674 | #authority => $auth, | ||
| 675 | #contact_email => $email); | ||
| 676 | my ( $self, %args ) = @_; | ||
| 677 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 678 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 679 | |||
| 680 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' ); | ||
| 681 | |||
| 682 | my $result = $adaptor->query_service(type => $args{subject_node}); | ||
| 683 | my $row = shift(@$result); | ||
| 684 | my $sbj_lsid = $row->{service_lsid}; | ||
| 685 | |||
| 686 | return (0, | ||
| 687 | qq{Service type $args{subject_node} has object dependencies in the ontology. It can not be changed}, | ||
| 688 | $sbj_lsid | ||
| 689 | ) unless defined $sbj_lsid; | ||
| 690 | |||
| 691 | my $isa = $adaptor->query_service_term2term(service2_id => $sbj_lsid); | ||
| 692 | my $isarow = shift @$isa; | ||
| 693 | if ( $isarow->{service_lsid} ) { | ||
| 694 | return ( | ||
| 695 | 0, | ||
| 696 | qq{Service type $args{subject_node} has object dependencies in the ontology. It can not be changed}, | ||
| 697 | $sbj_lsid | ||
| 698 | ); | ||
| 699 | } | ||
| 700 | $result = $adaptor->query_service(type => $args{object_node}); | ||
| 701 | $row = shift(@$result); | ||
| 702 | my $obj_lsid = $row->{service_lsid}; | ||
| 703 | # get ID of the related service | ||
| 704 | |||
| 705 | defined $obj_lsid | ||
| 706 | || return ( 0, | ||
| 707 | qq{Service $args{object_node} does not exist in the service ontology}, | ||
| 708 | '' ); | ||
| 709 | my $OE = MOBY::OntologyServer->new( ontology => 'relationship' ); | ||
| 710 | my ( $success, $desc, $rel_lsid ) = $OE->relationshipExists( | ||
| 711 | term => $args{relationship}, | ||
| 712 | ontology => 'service' ); | ||
| 713 | ($success) | ||
| 714 | || return ( 0, | ||
| 715 | qq{Relationship $args{relationship} does not exist in the ontology}, | ||
| 716 | '' ); | ||
| 717 | |||
| 718 | my $insertid = $adaptor->insert_service_term2term(relationship_type => $rel_lsid, | ||
| 719 | service1_type => $sbj_lsid, | ||
| 720 | service2_type => $obj_lsid); | ||
| 721 | if ( defined($insertid)) { | ||
| 722 | return ( 1, "Service relationship created successfully", '' ); | ||
| 723 | } else { | ||
| 724 | return ( 0, "Service relationship creation failed for unknown reasons", | ||
| 725 | '' ); | ||
| 726 | } | ||
| 727 | } | ||
| 728 | |||
| 729 | =head2 serviceExists | ||
| 730 | |||
| 731 | =cut | ||
| 732 | |||
| 733 | sub serviceExists { | ||
| 734 | my ( $self, %args ) = @_; | ||
| 735 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 736 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 737 | |||
| 738 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' ); | ||
| 739 | my $term = $args{term}; | ||
| 740 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 741 | if ( $term =~ /^urn:lsid/ | ||
| 742 | && !( $term =~ /^urn:lsid:biomoby.org:servicetype/ ) ) | ||
| 743 | { | ||
| 744 | return ( 1, "external ontology", $term ); | ||
| 745 | } | ||
| 746 | return (0, undef, undef) unless $term; | ||
| 747 | |||
| 748 | my $result; | ||
| 749 | $result = $adaptor->query_service(type => $term); | ||
| 750 | my $row = shift(@$result); | ||
| 751 | my $id = $row->{service_id}; | ||
| 752 | my $type = $row->{service_type}; | ||
| 753 | my $lsid = $row->{service_lsid}; | ||
| 754 | my $desc = $row->{description}; | ||
| 755 | my $auth = $row->{authority}; | ||
| 756 | my $email = $row->{contact_email}; | ||
| 757 | |||
| 758 | if ($id) { | ||
| 759 | return ( 1, $desc, $lsid ); | ||
| 760 | } else { | ||
| 761 | return ( | ||
| 762 | 0, | ||
| 763 | "Service Type $term does not exist in the biomoby.org Service Type ontology\n", | ||
| 764 | '' | ||
| 765 | ); | ||
| 766 | } | ||
| 767 | } | ||
| 768 | |||
| 769 | =head2 createServiceType | ||
| 770 | |||
| 771 | =cut | ||
| 772 | |||
| 773 | sub createServiceType { | ||
| 774 | my ( $self, %args ) = @_; | ||
| 775 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 776 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 777 | |||
| 778 | #node => $term, | ||
| 779 | #descrioption => $desc, | ||
| 780 | #authority => $auth, | ||
| 781 | #contact_email => $email); | ||
| 782 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' ); | ||
| 783 | return ( 0, "requires a object type node", '' ) unless ( $args{node} ); | ||
| 784 | return ( 0, "requires an authURI ", '' ) unless ( $args{authority} ); | ||
| 785 | return ( 0, "requires a contact email address", '' ) | ||
| 786 | unless ( $args{contact_email} ); | ||
| 787 | return ( 0, "requires a object description", '' ) | ||
| 788 | unless ( $args{description} ); | ||
| 789 | my $term = $args{node}; | ||
| 790 | if ( $term =~ /^urn:lsid/ | ||
| 791 | && !( $term =~ /^urn:lsid:biomoby.org:servicetype/ ) ) | ||
| 792 | { # if it is an LSID, but not a MOBY LSID, than barf | ||
| 793 | return ( 0, "can't create a term in a non-MOBY ontology!", $term ); | ||
| 794 | } | ||
| 795 | |||
| 796 | my $LSID =$self->setURI( $args{'node'} ); | ||
| 797 | unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) } | ||
| 798 | |||
| 799 | my $insertid = $adaptor->insert_service(service_type => $args{'node'}, | ||
| 800 | service_lsid => $LSID, | ||
| 801 | description => $args{'description'}, | ||
| 802 | authority => $args{'authority'}, | ||
| 803 | contact_email => $args{'contact_email'}); | ||
| 804 | |||
| 805 | unless ( $insertid ) { | ||
| 806 | return ( 0, "Service creation failed for unknown reasons", '' ); | ||
| 807 | } | ||
| 808 | return ( 1, "Service creation succeeded", $LSID ); | ||
| 809 | } | ||
| 810 | |||
| 811 | =head2 deleteServiceType | ||
| 812 | |||
| 813 | =cut | ||
| 814 | |||
| 815 | sub deleteServiceType { | ||
| 816 | my ( $self, %args ) = @_; | ||
| 817 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 818 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 819 | |||
| 820 | return ( 0, "WRONG ONTOLOGY!", '' ) unless ( $self->ontology eq 'service' ); | ||
| 821 | my $term = $args{term}; | ||
| 822 | if ( $term =~ /^urn:lsid/ | ||
| 823 | && !( $term =~ /^urn:lsid:biomoby.org:servicetype/ ) ) | ||
| 824 | { | ||
| 825 | return ( 0, "can't delete from external ontology", $term ); | ||
| 826 | } | ||
| 827 | my $LSID; | ||
| 828 | unless ( $term =~ /^urn:lsid:biomoby.org:servicetype/ ) { | ||
| 829 | $LSID = $self->getServiceURI($term); | ||
| 830 | } else { | ||
| 831 | $LSID = $term; | ||
| 832 | } | ||
| 833 | return ( | ||
| 834 | 0, q{Service type $term cannot be resolved to an LSID in the MOBY ontologies},"" | ||
| 835 | ) unless $LSID; | ||
| 836 | |||
| 837 | my $result = $adaptor->query_service(type => $LSID); | ||
| 838 | my $row = shift(@$result); | ||
| 839 | my $lsid = $row->{service_lsid}; | ||
| 840 | |||
| 841 | if ( !defined $lsid ) { | ||
| 842 | return ( 0, q{Service type $term does not exist in the ontology}, | ||
| 843 | $lsid ); | ||
| 844 | } | ||
| 845 | |||
| 846 | # service1_id ISA service2_id? | ||
| 847 | my $isa = $adaptor->query_service_term2term(type => $lsid); | ||
| 848 | my $isas = shift(@$isa); | ||
| 849 | |||
| 850 | if ( $isas->{service1_id} ) { | ||
| 851 | return ( 0, qq{Service type $term has dependencies in the ontology}, | ||
| 852 | $lsid ); | ||
| 853 | } | ||
| 854 | my ($err, $errstr) = $adaptor->delete_service(type => $lsid); | ||
| 855 | |||
| 856 | if ( $err ) { | ||
| 857 | return ( 0, "Delete from Service Type table failed: $errstr", | ||
| 858 | $lsid ); | ||
| 859 | } | ||
| 860 | |||
| 861 | return ( 1, "Service Type $term Deleted", $lsid ); | ||
| 862 | } | ||
| 863 | |||
| 864 | =head2 namespaceExists | ||
| 865 | |||
| 866 | =cut | ||
| 867 | |||
| 868 | sub namespaceExists { | ||
| 869 | my ( $self, %args ) = @_; | ||
| 870 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 871 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 872 | |||
| 873 | return ( 0, "WRONG ONTOLOGY!", '' ) | ||
| 874 | unless ( $self->ontology eq 'namespace' ); | ||
| 875 | my $term = $args{term}; | ||
| 876 | return (0, undef, undef) unless $term; | ||
| 877 | $term =~ s/^moby://; # if the term is namespaced, then remove that | ||
| 878 | if ( $term =~ /^urn:lsid/ | ||
| 879 | && !( $term =~ /^urn:lsid:biomoby.org:namespacetype/ ) ) | ||
| 880 | { | ||
| 881 | return ( 1, "external ontology", $term ); | ||
| 882 | } | ||
| 883 | my $result; | ||
| 884 | $result = $adaptor->query_namespace(type => $term); | ||
| 885 | my $row = shift(@$result); | ||
| 886 | my $id = $row->{namespace_id}; | ||
| 887 | my $type = $row->{namespace_type}; | ||
| 888 | my $lsid = $row->{namespace_lsid}; | ||
| 889 | my $desc = $row->{description}; | ||
| 890 | my $auth = $row->{authority}; | ||
| 891 | my $email = $row->{contact_email}; | ||
| 892 | |||
| 893 | if ($id) { | ||
| 894 | return ( 1, $desc, $lsid ); | ||
| 895 | } else { | ||
| 896 | return ( | ||
| 897 | 0, | ||
| 898 | "Namespace Type $term does not exist in the biomoby.org Namespace Type ontology\n", | ||
| 899 | '' | ||
| 900 | ); | ||
| 901 | } | ||
| 902 | } | ||
| 903 | |||
| 904 | =head2 createNamespace | ||
| 905 | |||
| 906 | =cut | ||
| 907 | |||
| 908 | sub createNamespace { | ||
| 909 | my ( $self, %args ) = @_; | ||
| 910 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 911 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 912 | #node => $term, | ||
| 913 | #descrioption => $desc, | ||
| 914 | #authority => $auth, | ||
| 915 | #contact_email => $email); | ||
| 916 | return ( 0, "WRONG ONTOLOGY!", '' ) | ||
| 917 | unless ( $self->ontology eq 'namespace' ); | ||
| 918 | return ( 0, "requires a namespace type node", '' ) unless ( $args{node} ); | ||
| 919 | return ( 0, "requires an authURI ", '' ) unless ( $args{authority} ); | ||
| 920 | return ( 0, "requires a contact email address", '' ) | ||
| 921 | unless ( $args{contact_email} ); | ||
| 922 | return ( 0, "requires a object description", '' ) | ||
| 923 | unless ( $args{description} ); | ||
| 924 | my $term = $args{node}; | ||
| 925 | if ( $term =~ /^urn:lsid/){ # if it is an LSID, barf | ||
| 926 | return ( 0, "can't create a term from an lsid!", $term ); | ||
| 927 | } | ||
| 928 | my $LSID = $self->setURI( $term ); | ||
| 929 | unless ($LSID) { return ( 0, "Failed during creation of an LSID", '' ) } | ||
| 930 | |||
| 931 | my $insertid = $adaptor->insert_namespace(namespace_type => $args{'node'}, | ||
| 932 | namespace_lsid => $LSID, | ||
| 933 | description => $args{'description'}, | ||
| 934 | authority => $args{'authority'}, | ||
| 935 | contact_email => $args{'contact_email'}); | ||
| 936 | |||
| 937 | unless ( $insertid ) { | ||
| 938 | return ( 0, "Namespace creation failed for unknown reasons", '' ); | ||
| 939 | } | ||
| 940 | return ( 1, "Namespace creation succeeded", $LSID ); | ||
| 941 | } | ||
| 942 | |||
| 943 | =head2 deleteNamespace | ||
| 944 | |||
| 945 | =cut | ||
| 946 | |||
| 947 | sub deleteNamespace { | ||
| 948 | my ( $self, %args ) = @_; | ||
| 949 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 950 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 951 | return ( 0, "WRONG ONTOLOGY!", '' ) | ||
| 952 | unless ( $self->ontology eq 'namespace' ); | ||
| 953 | my $term = $args{term}; | ||
| 954 | my $LSID; | ||
| 955 | unless ( $term =~ /urn\:lsid/ ) { $LSID = $self->getNamespaceURI($term) } else { $LSID = $term } | ||
| 956 | return ( 0, q{Namespace type $term cannot be resolved to an LSID}, "" ) | ||
| 957 | unless $LSID; | ||
| 958 | if ( $term =~ /^urn:lsid/ | ||
| 959 | && !( $term =~ /^urn:lsid:biomoby.org:namespacetype/ ) ) | ||
| 960 | { | ||
| 961 | return ( 0, "cannot delete a term from an external ontology", $term ); | ||
| 962 | } | ||
| 963 | |||
| 964 | my $result = $adaptor->query_namespace(type => $LSID); | ||
| 965 | my $row = shift(@$result); | ||
| 966 | my $lsid = $row->{namespace_lsid}; | ||
| 967 | |||
| 968 | unless ($lsid) { | ||
| 969 | return ( 0, q{Namespace type $term does not exist in the ontology}, | ||
| 970 | $lsid ); | ||
| 971 | } | ||
| 972 | |||
| 973 | # service1_id ISA service2_id? | ||
| 974 | my $isa = $adaptor->query_namespace_term2term(type => $lsid); | ||
| 975 | my $isas = shift @$isa; | ||
| 976 | |||
| 977 | if ($isas->{namespace1_id} ) { | ||
| 978 | return ( 0, qq{Namespace type $term has dependencies in the ontology}, | ||
| 979 | $lsid ); | ||
| 980 | } | ||
| 981 | |||
| 982 | my ($err, $errstr) = $adaptor->delete_namespace(type => $lsid); | ||
| 983 | |||
| 984 | if ( $err ) { | ||
| 985 | return ( 0, "Delete from namespace table failed: $errstr", | ||
| 986 | $lsid ); | ||
| 987 | } | ||
| 988 | |||
| 989 | #($err, $errstr) = $adaptor->delete_namespace_term2term(namespace1_id => $lsid); | ||
| 990 | # | ||
| 991 | #if ( $err ) { | ||
| 992 | # return ( | ||
| 993 | # 0, | ||
| 994 | # "Delete from namespace term2term table failed: $errstr", | ||
| 995 | # $lsid | ||
| 996 | # ); | ||
| 997 | #} | ||
| 998 | return ( 1, "Namespace Type $term Deleted", $lsid ); | ||
| 999 | } | ||
| 1000 | |||
| 1001 | =head2 retrieveAllServiceTypes | ||
| 1002 | |||
| 1003 | =cut | ||
| 1004 | |||
| 1005 | sub retrieveAllServiceTypes { | ||
| 1006 | my ($self) = @_; | ||
| 1007 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1008 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 1009 | my $types = $adaptor->new_query_service(); | ||
| 1010 | |||
| 1011 | my %response; | ||
| 1012 | foreach (@$types) { | ||
| 1013 | $response{ $_->{'service_type'} } = [$_->{'description'}, $_->{'service_lsid'}, $_->{'contact_email'}, $_->{'authority'}, $_->{'parent_type'}, $_->{'parent_lsid'}]; #UNCOMMENT | ||
| 1014 | } | ||
| 1015 | return \%response; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | =head2 retrieveAllNamespaceTypes | ||
| 1019 | |||
| 1020 | =cut | ||
| 1021 | |||
| 1022 | sub retrieveAllNamespaceTypes { | ||
| 1023 | my ($self) = @_; | ||
| 1024 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1025 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 1026 | my $types = $adaptor->query_namespace(); | ||
| 1027 | |||
| 1028 | my %response; | ||
| 1029 | foreach (@$types) { | ||
| 1030 | $response{ $_->{namespace_type} } = [$_->{description}, $_->{namespace_lsid}, $_->{authority}, $_->{contact_email}]; | ||
| 1031 | } | ||
| 1032 | return \%response; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | =head2 retrieveAllObjectClasses | ||
| 1036 | |||
| 1037 | =cut | ||
| 1038 | |||
| 1039 | sub retrieveAllObjectClasses { | ||
| 1040 | my ($self) = @_; | ||
| 1041 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1042 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 1043 | my $types = $adaptor->query_object(); | ||
| 1044 | |||
| 1045 | my %response; | ||
| 1046 | foreach (@$types) { | ||
| 1047 | $response{ $_->{object_type} } = [$_->{description}, $_->{object_lsid}]; | ||
| 1048 | } | ||
| 1049 | return \%response; | ||
| 1050 | } | ||
| 1051 | *retrieveAllObjectTypes = \&retrieveAllObjectClasses; | ||
| 1052 | *retrieveAllObjectTypes = \&retrieveAllObjectClasses; | ||
| 1053 | |||
| 1054 | =head2 getObjectCommonName | ||
| 1055 | |||
| 1056 | =cut | ||
| 1057 | |||
| 1058 | sub getObjectCommonName { | ||
| 1059 | my ( $self, $URI ) = @_; | ||
| 1060 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1061 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 1062 | return undef unless $URI =~ /urn\:lsid/; | ||
| 1063 | my $result = $adaptor->query_object(type => $URI); | ||
| 1064 | my $row = shift(@$result); | ||
| 1065 | my $name = $row->{object_type}; | ||
| 1066 | |||
| 1067 | return $name ? $name : $URI; | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | =head2 getNamespaceCommonName | ||
| 1071 | |||
| 1072 | =cut | ||
| 1073 | |||
| 1074 | sub getNamespaceCommonName { | ||
| 1075 | my ( $self, $URI ) = @_; | ||
| 1076 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1077 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 1078 | return undef unless $URI =~ /urn\:lsid/; | ||
| 1079 | my $result = $adaptor->query_namespace(type => $URI); | ||
| 1080 | my $row = shift(@$result); | ||
| 1081 | my $name = $row->{namespace_type}; | ||
| 1082 | |||
| 1083 | return $name ? $name : $URI; | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | =head2 getServiceCommonName | ||
| 1087 | |||
| 1088 | =cut | ||
| 1089 | |||
| 1090 | sub getServiceCommonName { | ||
| 1091 | my ( $self, $URI ) = @_; | ||
| 1092 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1093 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 1094 | return undef unless $URI =~ /urn\:lsid/; | ||
| 1095 | my $result = $adaptor->query_service(type => $URI); | ||
| 1096 | my $row = shift(@$result); | ||
| 1097 | my $name = $row->{service_type}; | ||
| 1098 | |||
| 1099 | return $name ? $name : $URI; | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | =head2 getServiceURI | ||
| 1103 | |||
| 1104 | =cut | ||
| 1105 | |||
| 1106 | sub getServiceURI { | ||
| 1107 | my ( $self, $term ) = @_; | ||
| 1108 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1109 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyservice' ); | ||
| 1110 | return $term if $term =~ /urn\:lsid/; | ||
| 1111 | |||
| 1112 | my $result = $adaptor->query_service(type => $term); | ||
| 1113 | my $row = shift(@$result); | ||
| 1114 | my $id = $row->{service_lsid}; | ||
| 1115 | |||
| 1116 | return $id; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | =head2 getObjectURI | ||
| 1120 | |||
| 1121 | =cut | ||
| 1122 | |||
| 1123 | sub getObjectURI { | ||
| 1124 | my ( $self, $term ) = @_; | ||
| 1125 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1126 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyobject' ); | ||
| 1127 | return $term if $term =~ /urn\:lsid/; | ||
| 1128 | |||
| 1129 | my $result = $adaptor->query_object(type => $term); | ||
| 1130 | my $row = shift(@$result); | ||
| 1131 | my $id = $row->{object_lsid}; | ||
| 1132 | |||
| 1133 | return $id; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | =head2 getNamespaceURI | ||
| 1137 | |||
| 1138 | =cut | ||
| 1139 | |||
| 1140 | sub getNamespaceURI { | ||
| 1141 | my ( $self, $term ) = @_; | ||
| 1142 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1143 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobynamespace' ); | ||
| 1144 | |||
| 1145 | return $term if $term =~ /urn\:lsid/; | ||
| 1146 | |||
| 1147 | my $result = $adaptor->query_namespace(type => $term); | ||
| 1148 | my $row = shift(@$result); | ||
| 1149 | my $id = $row->{namespace_lsid}; | ||
| 1150 | |||
| 1151 | return $id; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | =head2 getRelationshipURI | ||
| 1155 | |||
| 1156 | consumes ontology (object/service) | ||
| 1157 | consumes relationship term as term or LSID | ||
| 1158 | |||
| 1159 | =cut | ||
| 1160 | |||
| 1161 | sub getRelationshipURI { | ||
| 1162 | my ( $self, $ontology, $term ) = @_; | ||
| 1163 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1164 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyrelationship' ); | ||
| 1165 | |||
| 1166 | return $term if $term =~ /urn\:lsid/; | ||
| 1167 | |||
| 1168 | my $result = $adaptor->query_relationship(type => $term, ontology => $ontology); | ||
| 1169 | my $row = shift(@$result); | ||
| 1170 | my $id = $row->{relationship_lsid}; | ||
| 1171 | |||
| 1172 | return $id; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | =head2 getRelationshipTypes | ||
| 1176 | |||
| 1177 | =cut | ||
| 1178 | |||
| 1179 | sub getRelationshipTypes { | ||
| 1180 | my ( $self, %args ) = @_; | ||
| 1181 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1182 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobyrelationship' ); | ||
| 1183 | |||
| 1184 | my $ontology = $args{'ontology'}; | ||
| 1185 | my $OS = MOBY::OntologyServer->new( ontology => "relationship" ); | ||
| 1186 | |||
| 1187 | my $defs = $adaptor->query_relationship(ontology => $ontology); | ||
| 1188 | |||
| 1189 | my %result; | ||
| 1190 | foreach ( @$defs ) { | ||
| 1191 | $result{ $_->{relationship_lsid} } = [ $_->{relationship_type}, $_->{authority}, $_->{description} ]; | ||
| 1192 | } | ||
| 1193 | return \%result; | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | =head2 RelationshipsDEPRECATED | ||
| 1197 | |||
| 1198 | =cut | ||
| 1199 | |||
| 1200 | sub RelationshipsDEPRECATED { | ||
| 1201 | |||
| 1202 | # this entire subroutine assumes that there is NOT multiple parenting!! | ||
| 1203 | my ( $self, %args ) = @_; | ||
| 1204 | my $ontology = $args{ontology} ? $args{ontology} : $self->ontology; | ||
| 1205 | my $term = $args{term}; | ||
| 1206 | my $relationship = $args{relationship}; | ||
| 1207 | my $direction = $args{direction} ? $args{direction} : 'root'; | ||
| 1208 | my $expand = $args{expand} ? 1 : 0; | ||
| 1209 | return | ||
| 1210 | unless ( $ontology | ||
| 1211 | && $term | ||
| 1212 | && ( ( $ontology eq 'service' ) || ( $ontology eq 'object' ) ) ); | ||
| 1213 | |||
| 1214 | # convert $term into an LSID if it isn't already | ||
| 1215 | if ( $ontology eq 'service' ) { | ||
| 1216 | $term = $self->getServiceURI($term); | ||
| 1217 | $relationship ||="isa"; | ||
| 1218 | my $OS = MOBY::OntologyServer->new(ontology => 'relationship'); | ||
| 1219 | $relationship = $OS->getRelationshipURI("service", $relationship); | ||
| 1220 | } elsif ( $ontology eq 'object' ) { | ||
| 1221 | $term = $self->getObjectURI($term); | ||
| 1222 | $relationship ||="isa"; | ||
| 1223 | my $OS = MOBY::OntologyServer->new(ontology => 'relationship'); | ||
| 1224 | $relationship = $OS->getRelationshipURI("object", $relationship); | ||
| 1225 | } | ||
| 1226 | my %results; | ||
| 1227 | while ( ( $term ne 'urn:lsid:biomoby.org:objectclass:Object' ) | ||
| 1228 | && ( $term ne 'urn:lsid:biomoby.org:servicetype:Service' ) ) | ||
| 1229 | { | ||
| 1230 | my $defs = $self->_doRelationshipsQuery( | ||
| 1231 | $ontology, | ||
| 1232 | $term, | ||
| 1233 | $relationship, | ||
| 1234 | $direction ); | ||
| 1235 | return {[]} unless $defs; # somethig has gone terribly wrong! | ||
| 1236 | my $lsid; | ||
| 1237 | my $rel; | ||
| 1238 | my $articleName; | ||
| 1239 | foreach ( @{$defs} ) { | ||
| 1240 | $lsid = $_->[0]; | ||
| 1241 | $rel = $_->[1]; | ||
| 1242 | $articleName = $_->[2]; | ||
| 1243 | $articleName ||=""; | ||
| 1244 | $debug | ||
| 1245 | && _LOG("\t\tADDING RELATIONSHIP $_ : $lsid to $rel\n"); | ||
| 1246 | push @{ $results{$rel} }, [$lsid, $articleName]; | ||
| 1247 | } | ||
| 1248 | last unless ($expand); | ||
| 1249 | last unless ( $direction eq "root" ); # if we aren't going to root, then be careful or we'll loop infnitely | ||
| 1250 | $term = $lsid; # this entire subroutine assumes that there is NOT multiple parenting... | ||
| 1251 | } | ||
| 1252 | return \%results; #results(relationship} = [[lsid1,articleNmae], [lsid2, articleName], [lsid3, articleName]] | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | |||
| 1256 | =head2 Relationships | ||
| 1257 | |||
| 1258 | =cut | ||
| 1259 | |||
| 1260 | sub Relationships { | ||
| 1261 | my ($self, %args) = @_; | ||
| 1262 | my %results; | ||
| 1263 | |||
| 1264 | my $term = $args{term}; | ||
| 1265 | my $ontology = $args{ontology} ? $args{ontology} : $self->ontology; | ||
| 1266 | my $direction = $args{direction} ? $args{direction} : 'root'; | ||
| 1267 | $direction = $direction eq 'root'? 'root' : 'leaves'; # map anything else to 'leaves' | ||
| 1268 | my $relationship = $args{relationship}; | ||
| 1269 | my $expand = $args{expand} ? 1 : 0; | ||
| 1270 | |||
| 1271 | # in order to make this function also usable for 'traverseDAG' | ||
| 1272 | # we need a more precise definition what to expand. Note that | ||
| 1273 | # the default settings assure the behaviour of the old 'expand' param. | ||
| 1274 | # 1. expand along the isa relationship? | ||
| 1275 | my $isaExpand = $args{isaExpand} ? $args{isaExpand} : $expand; | ||
| 1276 | # 2. expand along the inclusion relationship types (has/hasa), | ||
| 1277 | # i.e. get inclusions of inclusions? | ||
| 1278 | # (Note: this is set when called by 'traverseDAG') | ||
| 1279 | my $incExpand = $args{incExpand} ? $args{incExpand} : 0; | ||
| 1280 | # 3. explore inclusion relationships for complete isa hierarchy? | ||
| 1281 | # (Note: this was fix behaviour of the old 'expand', | ||
| 1282 | # but is not used by traverseDAG) | ||
| 1283 | my $mapIncToIsa = $args{mapIncToIsa} ? $args{mapIncToIsa} : $expand; | ||
| 1284 | |||
| 1285 | # first of all, get ID of query entity, | ||
| 1286 | # internally, we will operate on pure IDs | ||
| 1287 | # as long as possible... | ||
| 1288 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 1289 | my $datasource = "moby$ontology"; # like mobyobject, or mobyservice | ||
| 1290 | my $adaptor = $CONFIG->getDataAdaptor( datasource => $datasource ); | ||
| 1291 | my $queryId; | ||
| 1292 | my $query_method = "query_$ontology"; | ||
| 1293 | my $result = $adaptor->$query_method(type => $term); | ||
| 1294 | my $row = shift @$result; | ||
| 1295 | $queryId = $row->{"${ontology}_id"}; | ||
| 1296 | |||
| 1297 | return {} unless $queryId; | ||
| 1298 | |||
| 1299 | # get all relationships in the database in one query | ||
| 1300 | my $relHash = $adaptor->get_all_relationships(direction=>$direction,ontology=>$ontology); | ||
| 1301 | |||
| 1302 | # find out which relationships to return | ||
| 1303 | # use keys of %$relHash, because these are lsids: | ||
| 1304 | |||
| 1305 | # initialize to return all relationships (becomes effective if eg. 'all' was used) | ||
| 1306 | my @relList = keys %$relHash; | ||
| 1307 | if ( (not $relationship) or # ISA (and nothing else) is the default if nothing specified | ||
| 1308 | ($relationship =~ /isa$/i) ) { | ||
| 1309 | @relList = grep { /isa$/i } @relList; | ||
| 1310 | } | ||
| 1311 | elsif ( $relationship =~ /has(a)?$/i ) { | ||
| 1312 | # if either has or hasa was specified, use only that | ||
| 1313 | @relList = grep { /$relationship$/i } @relList; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | # build the isa hierarchy, it's needed in any case... | ||
| 1317 | my ($isaLsid) = grep { /isa$/i } keys %$relHash; # we need the lsid... | ||
| 1318 | my $isa_hierarchy = $self->_getIsaHierarchy($relHash->{$isaLsid}, $queryId, $direction, $isaExpand); | ||
| 1319 | |||
| 1320 | # prepare the hash for storing HAS/HASA relationship details | ||
| 1321 | my $hasRelDetails; | ||
| 1322 | |||
| 1323 | # table fields needed to get entity details: | ||
| 1324 | my @fields = ("${ontology}_lsid","${ontology}_type"); | ||
| 1325 | |||
| 1326 | # nodes to check for has/hasa relationship | ||
| 1327 | my @checkNodes = ($queryId); | ||
| 1328 | # mapIncToIsa means that has/hasa has to be checked | ||
| 1329 | # not only for the query object alone but also for all | ||
| 1330 | # isa ancestors/descendants | ||
| 1331 | push @checkNodes, @$isa_hierarchy if $mapIncToIsa; | ||
| 1332 | |||
| 1333 | # the result hash will consist of one list for each included relationship type... | ||
| 1334 | foreach my $rel ( @relList ) { | ||
| 1335 | my @entityQueryList = (); # this collects the unique object ids | ||
| 1336 | my @entityResultList = (); # this collects ids of objects to add to the result, maybe not unique | ||
| 1337 | # the latter one is not essential to have, the only benefit is | ||
| 1338 | # a somehow predictable order in the output... | ||
| 1339 | |||
| 1340 | # find out which entities we have to include in the result | ||
| 1341 | # and how these are related to each other; | ||
| 1342 | # Note: all needed information is present in the relationship hash %$relHash! | ||
| 1343 | |||
| 1344 | if ( $rel ne $isaLsid ) { | ||
| 1345 | # either HAS or HASA | ||
| 1346 | foreach my $node ( @checkNodes ) { | ||
| 1347 | my $incls = $self->_getInclusions($relHash,$node,[$rel], $incExpand); | ||
| 1348 | foreach my $triplet ( @$incls ) { | ||
| 1349 | my ($inclId, $inclArtName, $inclAssert) = @$triplet; | ||
| 1350 | $hasRelDetails->{$inclId}->{$inclAssert} = $inclArtName; # can be more than one articleName for each included Object | ||
| 1351 | push @entityResultList, $inclId; | ||
| 1352 | } | ||
| 1353 | } | ||
| 1354 | # we have the following structure now for the HAS and HASA... | ||
| 1355 | # DB<35> x $hasRelDetails | ||
| 1356 | # 0 HASH(0x95cd1bc) | ||
| 1357 | # 5371 => HASH(0x95f7fd8) # object type | ||
| 1358 | # 10795 => 'Tiny' # related to parent by $ rel relationship | ||
| 1359 | # 10796 => 'Small' | ||
| 1360 | # 10797 => 'Aliphatic' | ||
| 1361 | # 10798 => 'Aromatic' | ||
| 1362 | # 10799 => 'Non-polar' | ||
| 1363 | # 10800 => 'Polar' | ||
| 1364 | # 10801 => 'Charged' | ||
| 1365 | # 10802 => 'Positive' | ||
| 1366 | # 10803 => 'Negative' | ||
| 1367 | # 10804 => 'Hydropathy_KD' | ||
| 1368 | # 10805 => 'Hydropathy_OHM' | ||
| 1369 | # 10806 => 'Consensus' | ||
| 1370 | |||
| 1371 | # set up list of unique object ids for the database lookup | ||
| 1372 | @entityQueryList = keys %$hasRelDetails; | ||
| 1373 | } | ||
| 1374 | else { | ||
| 1375 | # ISA | ||
| 1376 | @entityQueryList = @$isa_hierarchy; # isa hierarchy is guaranteed to be unique... | ||
| 1377 | @entityResultList = @$isa_hierarchy; # ... but still both variables have to be set | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | # now it's time to move away from pure ids, retrieve details from database: | ||
| 1381 | my $details = $adaptor->get_details_for_id_list($ontology, \@fields, \@entityQueryList); | ||
| 1382 | my $newstructure; | ||
| 1383 | # enhance details with information about relationships and build result hash | ||
| 1384 | foreach my $entityId (@entityResultList) { | ||
| 1385 | # add articleName slot if necessary | ||
| 1386 | next if $details->{$entityId}->{'articleName'}; # we've already processed this one | ||
| 1387 | if ( exists $hasRelDetails->{$entityId} ) { # the only things that have RelDetails are HASA/HAS EntityIDs | ||
| 1388 | foreach my $assert ( keys %{$hasRelDetails->{$entityId}} ) { | ||
| 1389 | # THIS DATA STRUCTURE IS WRONG - IT ASSUMES ONE ARTICLE NAME FOR EACH CONTAINED OBJECT TYPE | ||
| 1390 | # NEEDS TO BE REVERSED! | ||
| 1391 | my $articleName = $hasRelDetails->{$entityId}->{$assert}; | ||
| 1392 | my $objectTypeLSID = $details->{entityId}->{object_lsid}; | ||
| 1393 | $details->{$entityId}->{'articleName'}->{$articleName} = "Related_by"; # I know, this is a very goofy data structure. What we really | ||
| 1394 | # want are keys $details->{entitId}->{articleName} | ||
| 1395 | # so taht we can see how often that object is included | ||
| 1396 | # by a has or hasa relationship | ||
| 1397 | } | ||
| 1398 | } | ||
| 1399 | elsif ( $ontology eq 'object') { # if it doesn't have a RelDetail, and it is the object ontology we are querying, then its an ISA | ||
| 1400 | # for isa, articleName is the empty string | ||
| 1401 | $details->{$entityId}->{'articleName'} = ''; | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | # map ontology specific field names to commons slots: | ||
| 1405 | # 1. 'object_lsid'/'service_lsid' -> 'lsid' | ||
| 1406 | $details->{$entityId}->{'lsid'} = $details->{$entityId}->{"${ontology}_lsid"} | ||
| 1407 | unless exists $details->{$entityId}->{'lsid'}; # do just once foreach object! | ||
| 1408 | delete $details->{$entityId}->{"${ontology}_lsid"}; # remove redundant slot | ||
| 1409 | # 2. 'object_type'/'service_type' -> 'term' | ||
| 1410 | $details->{$entityId}->{'term'} = $details->{$entityId}->{"${ontology}_type"} | ||
| 1411 | unless exists $details->{$entityId}->{'term'}; # do just once foreach object! | ||
| 1412 | delete $details->{$entityId}->{"${ontology}_type"}; # remove redundant slot | ||
| 1413 | |||
| 1414 | # finally, add record to the result hash | ||
| 1415 | push @{ $results{$rel} }, $details->{$entityId}; | ||
| 1416 | } | ||
| 1417 | } | ||
| 1418 | return \%results; | ||
| 1419 | } | ||
| 1420 | |||
| 1421 | sub _getIsaHierarchy { | ||
| 1422 | # Finds out the isa hierarchy for the query entity, that is | ||
| 1423 | # the parent (the one which it inherits from) if direction is 'root' or | ||
| 1424 | # the children (one or more which inherit from it) if direction is 'leaves'. | ||
| 1425 | # If 'expand' is set all deeper levels (ancestors or descendants if you like) | ||
| 1426 | # are also included. | ||
| 1427 | # Note 1: this implementation relies on pure single inheritance! | ||
| 1428 | # Note 2: we can use the same method for both directions only because the | ||
| 1429 | # provided isaHash is built with the direction in mind, make sure | ||
| 1430 | # to have direction consistent! | ||
| 1431 | |||
| 1432 | # returned is a reference to a flat list | ||
| 1433 | |||
| 1434 | my ($self, $isaHash, $query, $direction, $expand) = @_; | ||
| 1435 | |||
| 1436 | my @hierarchy = (); | ||
| 1437 | if ( exists $isaHash->{$query} ) { | ||
| 1438 | if ( $direction eq 'root' ) { | ||
| 1439 | # push the parent entity | ||
| 1440 | push @hierarchy, $isaHash->{$query}; # relies on single inheritance! | ||
| 1441 | } | ||
| 1442 | elsif ( $direction eq 'leaves' ) { | ||
| 1443 | # push the direct children | ||
| 1444 | push @hierarchy, @{$isaHash->{$query}}; | ||
| 1445 | } | ||
| 1446 | else { | ||
| 1447 | # it has to be either 'root' or 'leaves' | ||
| 1448 | warn "_getIsaHierarchy was called with wrong direction indicator, | ||
| 1449 | use either 'root' or 'leaves'!\n"; | ||
| 1450 | return []; | ||
| 1451 | } | ||
| 1452 | if ( $expand ) { | ||
| 1453 | my @firstLevel = @hierarchy; | ||
| 1454 | foreach my $entity ( @firstLevel ) { | ||
| 1455 | my $deeperLevels = $self->_getIsaHierarchy($isaHash, $entity, $direction, 1); | ||
| 1456 | push @hierarchy, @$deeperLevels; | ||
| 1457 | } | ||
| 1458 | } | ||
| 1459 | return \@hierarchy; | ||
| 1460 | } | ||
| 1461 | else { | ||
| 1462 | # important: anchor the recursion! | ||
| 1463 | return []; | ||
| 1464 | } | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | sub _getInclusions { | ||
| 1468 | |||
| 1469 | # Finds out the objects related to the query by one of the inclusion | ||
| 1470 | # relationships (HAS or HASA). This is the HAS/HASA-analogue to | ||
| 1471 | # _getIsaHierarchy, but is more complicated, because the values in | ||
| 1472 | # the provided relationship hash ($relHash) are not simple ids but | ||
| 1473 | # triplets ("relationship records") in the format of: | ||
| 1474 | # [id of relationship partner, articleName, assertion id] | ||
| 1475 | # On the other hand, direction does not matter here, because | ||
| 1476 | # we have to deal with multi relationships in any case. | ||
| 1477 | # Like for ISA, be aware that the relationship hash '$relHash' | ||
| 1478 | # is built direction dependant. Make sure to use it consistently! | ||
| 1479 | |||
| 1480 | # Note: third argument is a listref of relationship types, that is | ||
| 1481 | # it could be called with HAS and HASA (expected are lsids) at | ||
| 1482 | # the same time and in this way merge both inclusion relationship | ||
| 1483 | # types. However, this usage is not used currently and not tested! | ||
| 1484 | |||
| 1485 | # Returned is a reference to a list with each element being | ||
| 1486 | # a triplet (listref to a relationship record) as explained above. | ||
| 1487 | |||
| 1488 | my ($self, $relHash, $query, $relList, $expand) = @_; | ||
| 1489 | |||
| 1490 | my %nodeCheckDone; # for avoiding multiple check of one node (if expand is set) | ||
| 1491 | my @allInclusions = (); | ||
| 1492 | foreach my $relType ( @$relList ) { | ||
| 1493 | # 'root' means: include all relationships where query is the | ||
| 1494 | # containing (outer) object; | ||
| 1495 | # eg. if A HAS B, and A is query, include this record | ||
| 1496 | if ( exists $relHash->{$relType}->{$query} ) { | ||
| 1497 | my $relRecords = $relHash->{$relType}->{$query}; | ||
| 1498 | foreach my $record ( @$relRecords ) { | ||
| 1499 | push @allInclusions, $record; | ||
| 1500 | if ( $expand ) { | ||
| 1501 | my ($incId, $artName, $assert) = @$record; | ||
| 1502 | if ( not exists $nodeCheckDone{$incId} ) { | ||
| 1503 | my $deeperInclusions = $self->_getInclusions($relHash, $incId, $relList, 1); | ||
| 1504 | push @allInclusions, @$deeperInclusions; | ||
| 1505 | $nodeCheckDone{$incId}++; | ||
| 1506 | } | ||
| 1507 | } | ||
| 1508 | } | ||
| 1509 | } | ||
| 1510 | } | ||
| 1511 | return \@allInclusions; # empty if nothing found, this anchors the recursion | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | =head2 setURI | ||
| 1515 | |||
| 1516 | =cut | ||
| 1517 | |||
| 1518 | sub setURI { | ||
| 1519 | my ( $self, $id ) = @_; | ||
| 1520 | my $URI; | ||
| 1521 | |||
| 1522 | my ($sec,$min,$hour,$mday,$month,$year, $wday,$yday,$dst) =gmtime(time); | ||
| 1523 | my $date = sprintf ("%04d-%02d-%02dT%02d-%02d-%02dZ",$year+1900,$month+1,$mday,$hour,$min,$sec); | ||
| 1524 | |||
| 1525 | # $id = lc($id); | ||
| 1526 | if ( $self->ontology eq 'object' ) { | ||
| 1527 | $URI = "urn:lsid:biomoby.org:objectclass:$id:$date"; | ||
| 1528 | } elsif ( $self->ontology eq 'namespace' ) { | ||
| 1529 | $URI = "urn:lsid:biomoby.org:namespacetype:$id:$date"; | ||
| 1530 | } elsif ( $self->ontology eq 'service' ) { | ||
| 1531 | $URI = "urn:lsid:biomoby.org:servicetype:$id:$date"; | ||
| 1532 | } elsif ( $self->ontology eq 'relationship' ) { | ||
| 1533 | $URI = "urn:lsid:biomoby.org:relationshiptype:$id"; # dont' add version info here | ||
| 1534 | } else { | ||
| 1535 | $URI = 0; | ||
| 1536 | } | ||
| 1537 | return $URI; | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | =head2 traverseDAG | ||
| 1541 | |||
| 1542 | =cut | ||
| 1543 | |||
| 1544 | sub traverseDAG { | ||
| 1545 | my ( $self, $term, $direction ) = @_; | ||
| 1546 | my $ontology = $self->ontology; | ||
| 1547 | return {} unless $ontology; | ||
| 1548 | return {} unless $term; | ||
| 1549 | $direction = "root" unless ($direction); | ||
| 1550 | return {} unless ( ( $direction eq 'root' ) || ( $direction eq 'leaves' ) ); | ||
| 1551 | if ( $ontology eq 'service' ) { | ||
| 1552 | $term = $self->getServiceURI($term); | ||
| 1553 | } elsif ( $ontology eq 'object' ) { | ||
| 1554 | $term = $self->getObjectURI($term); | ||
| 1555 | } | ||
| 1556 | return {} unless $term; # search term not in db! | ||
| 1557 | return {} unless $term =~ /^urn\:lsid/; # now its a URI | ||
| 1558 | |||
| 1559 | my $result = {}; | ||
| 1560 | # get the types of relationships for the object/service ontology | ||
| 1561 | my $relTypeHash = $self->getRelationshipTypes( ontology => $ontology ); | ||
| 1562 | my $relHash = $self->Relationships( term => $term, | ||
| 1563 | direction => $direction, | ||
| 1564 | ontology => $ontology, | ||
| 1565 | isaExpand => 1, | ||
| 1566 | incExpand => 1, | ||
| 1567 | mapIncToIsa => 0, | ||
| 1568 | relationship => 'all'); | ||
| 1569 | foreach my $relType ( keys %$relTypeHash ) { | ||
| 1570 | $result->{$relType} = []; | ||
| 1571 | my %tmpHash; # avoid doubles! | ||
| 1572 | my $relList = $relHash->{$relType}; | ||
| 1573 | foreach my $rel ( @$relList ) { | ||
| 1574 | $tmpHash{$rel->{'lsid'}}++; | ||
| 1575 | } | ||
| 1576 | @{$result->{$relType}} = keys %tmpHash; | ||
| 1577 | } | ||
| 1578 | return $result; | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | sub _LOG { | ||
| 1582 | return unless $debug; | ||
| 1583 | |||
| 1584 | #print join "\n", @_; | ||
| 1585 | #print "\n---\n"; | ||
| 1586 | #return; | ||
| 1587 | open LOG, ">>/tmp/OntologyServer.txt" or die "can't open logfile $!\n"; | ||
| 1588 | print LOG join "\n", @_; | ||
| 1589 | print LOG "\n---\n"; | ||
| 1590 | close LOG; | ||
| 1591 | } | ||
| 1592 | sub DESTROY { } | ||
| 1593 | |||
| 1594 | sub AUTOLOAD { | ||
| 1595 | no strict "refs"; | ||
| 1596 | my ( $self, $newval ) = @_; | ||
| 1597 | $AUTOLOAD =~ /.*::(\w+)/; | ||
| 1598 | my $attr = $1; | ||
| 1599 | if ( $self->_accessible( $attr, 'write' ) ) { | ||
| 1600 | *{$AUTOLOAD} = sub { | ||
| 1601 | if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] } | ||
| 1602 | return $_[0]->{$attr}; | ||
| 1603 | }; ### end of created subroutine | ||
| 1604 | ### this is called first time only | ||
| 1605 | if ( defined $newval ) { | ||
| 1606 | $self->{$attr} = $newval; | ||
| 1607 | } | ||
| 1608 | return $self->{$attr}; | ||
| 1609 | } elsif ( $self->_accessible( $attr, 'read' ) ) { | ||
| 1610 | *{$AUTOLOAD} = sub { | ||
| 1611 | return $_[0]->{$attr}; | ||
| 1612 | }; ### end of created subroutine | ||
| 1613 | return $self->{$attr}; | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | # Must have been a mistake then... | ||
| 1617 | croak "No such method: $AUTOLOAD"; | ||
| 1618 | } | ||
| 1619 | 1; | ||