[MOBY-guts] biomoby commit

Dirk Haase d.haase at dev.open-bio.org
Tue Jan 30 15:08:33 UTC 2007


d.haase
Tue Jan 30 10:08:33 EST 2007
Update of /home/repository/moby/moby-live/Perl/MOBY/Adaptor/moby/queryapi
In directory dev.open-bio.org:/tmp/cvs-serv2183

Modified Files:
	mysql.pm 
Log Message:
Added new methods for usage in MOBY::OntologyServer::Relationships:
get_all_relationships and get_details_for_id_list.
Also fixed a bug in get_relationship (select statement for 'leaves'
direction), but I guess this method isn't used any longer due to
re-implementation of MOBY::OntologyServer::Relationships anyway...

moby-live/Perl/MOBY/Adaptor/moby/queryapi mysql.pm,1.81,1.82
===================================================================
RCS file: /home/repository/moby/moby-live/Perl/MOBY/Adaptor/moby/queryapi/mysql.pm,v
retrieving revision 1.81
retrieving revision 1.82
diff -u -r1.81 -r1.82
--- /home/repository/moby/moby-live/Perl/MOBY/Adaptor/moby/queryapi/mysql.pm	2007/01/30 00:26:18	1.81
+++ /home/repository/moby/moby-live/Perl/MOBY/Adaptor/moby/queryapi/mysql.pm	2007/01/30 15:08:33	1.82
@@ -1371,7 +1371,7 @@
 	} else {
 		unless ( defined $relationship ) {
 			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid $extra_columns from
+            select distinct s1.${ontology}_lsid $extra_columns from
                 ${ontology}_term2term as t2t,
                 $ontology as s1,
                 $ontology as s2  
@@ -1381,7 +1381,7 @@
                 s2.${ontology}_lsid = ?", undef, $lsid);                   # ")
 		} else {
 			$defs = $self->dbh->selectall_arrayref( "
-            select distinct s2.${ontology}_lsid $extra_columns from
+            select distinct s1.${ontology}_lsid $extra_columns from
                 ${ontology}_term2term as t2t,
                 $ontology as s1,
                 $ontology as s2  
@@ -1395,6 +1395,93 @@
 	return $defs;
 }
 
+# Get all relationships in the queried database in one go.  The
+# complete table ${ontology}_term2term is transferred into a hash
+# whose reference is finally returned.  Important: note that the hash
+# is built 'direction aware', that is for objects 'object1_id' is used
+# as key when direction is 'root' and 'object2_id' as value. Vice
+# versa for the 'leaves' direction.  Likewise for services.
+# Returns a hash reference.
+sub get_all_relationships {
+
+  my ($self, %args) = @_;
+  my $direction = $args{'direction'};
+  my $ontology = $args{'ontology'};
+  # my $relationship = $args{'relationship'}; # has to be lsid!
+
+  my $relHash;
+  my $dbh = _getDBHandle("moby$ontology");
+
+  my $statement = "select ${ontology}1_id, ${ontology}2_id, relationship_type";
+  $statement .= ", object2_articlename, assertion_id " if $ontology eq 'object';
+  $statement .= " from ${ontology}_term2term";
+  # my $relationship_lsid = "urn:lsid:biomoby.org:${ontology}relation:isa";
+  my $defs = $dbh->selectall_arrayref($statement);
+
+  return {} unless @$defs;
+  foreach my $def (@$defs) {
+    my $relationship = $def->[2];
+    if ( $relationship =~ /has/i ) {
+      # HAS or HASA
+      # >1 has/hasa child possible; also store articlename and assertion_id
+      # hash structure: $relHash->{has/a-lsid}->{object1_id}->[object2_id,articlename,assertion_id]
+      push @{$relHash->{$relationship}->{$def->[0]}}, [$def->[1],$def->[3],$def->[4]] if $direction eq 'root';
+      push @{$relHash->{$relationship}->{$def->[1]}}, [$def->[0],$def->[3],$def->[4]] if $direction eq 'leaves';
+    }
+    elsif ( $relationship =~ /isa/i ) {
+      # ISA
+      push @{$relHash->{$relationship}->{$def->[1]}}, $def->[0] if $direction eq 'leaves'; # >1 child possible!
+      $relHash->{$relationship}->{$def->[0]} = $def->[1] if $direction eq 'root'; # no multi parents!
+    }
+    else { return {}; }
+  }
+  return $relHash;
+}
+
+# retrieve details for a number of entities from table $ontology
+# represented by a list of ${ontology}_id's;
+# used in MOBY::OntologyServer::Relationships
+sub get_details_for_id_list {
+  my ($self, $ontology, $fields, $idList) = @_;
+
+  return {} unless @$idList;
+  return {} unless @$fields;
+
+  my $dbh = _getDBHandle("moby$ontology");
+  my $result = {};
+
+  # avoid errors due to wrong field names:
+  my %existingFields;
+  my @queryFields = ();
+  my $resArray = $dbh->selectall_arrayref("SHOW COLUMNS FROM $ontology");
+  foreach my $row ( @$resArray ) {
+    $existingFields{$row->[0]}++;
+  }
+  foreach my $field ( @$fields ) {
+    next if $field eq "${ontology}_id";
+    if ( exists $existingFields{$field} ) {
+      push @queryFields, $field;
+    }
+    else {
+      warn "Requested field $field does not exist in table $ontology!";
+    }
+  }
+
+  #
+  my $statement = "select ${ontology}_id, ". join(",", @queryFields). 
+    " from $ontology where ${ontology}_id in (" .
+      join(",", @$idList) . ")";
+  $resArray = $dbh->selectall_arrayref($statement);
+  foreach my $row ( @$resArray ) {
+    my $entityId = shift @$row;
+    foreach my $field (@queryFields) {
+      my $value = shift @$row;
+      $result->{$entityId}->{$field} = $value ? $value : '';
+    }
+  }
+  return $result;
+}
+
 sub _checkURI {
 	
 #	my $uri = "http://www.ics.uci.edu/pub/ietf/uri/#Related";




More information about the MOBY-guts mailing list