Browse Source

Web services now supports linking to entities, and data in Chado from FKs

Stephen Ficklin 9 years ago
parent
commit
86433df0e8

+ 11 - 7
tripal/api/tripal.entities.api.inc

@@ -45,23 +45,27 @@ function tripal_load_term_entity($values) {
  * Retrieves a TripalVocab entity that maches the given arguments.
  *
  * @param $values
- *   An associative array used to match a term.  Currently, the only valid keys
- *   is 'namespace'.
+ *   An associative array used to match a vocabulary.  The valid keys are
+ *   'vocab_id' and 'namespace'.
  *
  * @return
  * A TripalVocab entity object or NULL if not found.
  */
 function tripal_load_vocab_entity($values) {
   $namespace = array_key_exists('namespace', $values) ? $values['namespace'] : '';
+  $vocab_id = array_key_exists('vocab_id', $values) ? $values['vocab_id'] : '';
   $vocab = NULL;
 
+  $query= db_select('tripal_vocab', 'tv')
+    ->fields('tv');
+
   if ($namespace) {
-    $vocab = db_select('tripal_vocab', 'tv')
-      ->fields('tv')
-      ->condition('tv.namespace', $namespace)
-      ->execute()
-      ->fetchObject();
+    $query->condition('tv.namespace', $namespace);
+  }
+  if ($namespace) {
+    $query->condition('tv.vocab_id', $vocab_id);
   }
+  $vocab = $query->execute()->fetchObject();
 
   if ($vocab) {
     $entity = entity_load('TripalVocab', array($vocab->id));

+ 21 - 7
tripal_chado/includes/fields/chado_base__dbxref_id.inc

@@ -47,7 +47,7 @@ function chado_base__dbxref_id_attach_info($entity_type, $bundle, $target) {
   // Check the schema for the data table if it does not have
   // a 'dbxref_id' column then we don't want to attach this field.
   $schema = chado_get_schema($table_name);
-  if (!array_key_exists('organism_id', $schema['fields'])) {
+  if (!array_key_exists('dbxref_id', $schema['fields'])) {
     return $field_info;
   }
 
@@ -71,8 +71,8 @@ function chado_base__dbxref_id_attach_info($entity_type, $bundle, $target) {
       'chado_table' => $table_name,
       'chado_column' => 'dbxref_id',
       'semantic_web' => array(
-        'type' => 'organism',
-        'ns' => 'local',
+        'type' => '',
+        'ns' => '',
         'nsurl' => '',
       ),
     ),
@@ -293,7 +293,7 @@ function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_chado_organism_select_widget.
+ * Callback function for validating the tripal_chado_dbxref_select_widget.
  */
 function chado_base__dbxref_id_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
@@ -421,9 +421,9 @@ function chado_base__dbxref_id_load($field, $entity, $base_table, $record) {
     $columns = array('*');
     $match = array('dbxref_id' => $record->$field_column->$field_column);
     $options = array('return_array' => TRUE);
-    $records = chado_select_record('dbxref', $columns, $match, $options);
-    if (count($records) > 0) {
-      $dbxref = $records[0];
+    $dbxrefs = chado_select_record('dbxref', $columns, $match, $options);
+    if (count($dbxrefs) > 0) {
+      $dbxref = $dbxrefs[0];
       $entity->{$field_name}['und'][0] = array(
         'value' => $dbxref->dbxref_id,
         'dbxref__db_id' => $dbxref->db_id,
@@ -435,3 +435,17 @@ function chado_base__dbxref_id_load($field, $entity, $base_table, $record) {
   }
 }
 
+
+/**
+ * Implements hook_ws_formatter().
+ */
+function chado_base__dbxref_id_ws_formatter($entity_type, $entity,
+    $field, $instance, $items) {
+
+  $dbxref = $entity->chado_record->dbxref_id;
+
+  unset($dbxref->tablename);
+  unset($dbxref->db_id->tablename);
+  return $dbxref;
+}
+

+ 12 - 12
tripal_chado/includes/fields/chado_base__organism_id.inc

@@ -258,18 +258,6 @@ function chado_base__organism_id_formatter(&$element, $entity_type, $entity,
     }
   }
 }
-/**
- * Implements hook_ws_formatter().
- */
-function chado_base__organism_id_ws_formatter($entity_type, $entity,
-    $field, $instance, $items) {
-
-  $values = array();
-  foreach ($items as $delta => $item) {
-
-  }
-  return $values;
-}
 
 /**
  *  Implements hook_widget().
@@ -311,3 +299,15 @@ function chado_base__organism_id_widget_validate($element, &$form_state) {
   }
 }
 
+
+/**
+ * Implements hook_ws_formatter().
+ */
+function chado_base__organism_id_ws_formatter($entity_type, $entity,
+    $field, $instance, $items) {
+
+  $organism = (array) $entity->chado_record->organism_id;
+  unset($organism->tablename);
+
+  return $organism;
+}

+ 1 - 1
tripal_chado/includes/fields/chado_linker__cvterm.inc

@@ -271,7 +271,7 @@ function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_chado_organism_select_widget.
+ * Callback function for validating the chado_linker__cvterm_widget.
  */
 function chado_linker__cvterm_widget_validate($element, &$form_state) {
 

+ 25 - 1
tripal_chado/includes/fields/chado_linker__dbxref.inc

@@ -310,7 +310,7 @@ function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_chado_organism_select_widget.
+ * Callback function for validating the chado_linker__dbxref_widget.
  */
 function chado_linker__dbxref_widget_validate($element, &$form_state) {
 
@@ -460,3 +460,27 @@ function chado_linker__dbxref_load($field, $entity, $base_table, $record) {
   }
 }
 
+/**
+ * Implements hook_ws_formatter().
+ */
+function chado_linker__dbxref_ws_formatter($entity_type, $entity,
+    $field, $instance, $items) {
+
+  $field_name = $field['field_name'];
+  $field_type = $field['type'];
+  $chado_table = $field['settings']['chado_table'];
+  $record = $entity->chado_record;
+
+  $values = array();
+  $options = array('return_array' => 1);
+  $record = chado_expand_var($record, 'table', $chado_table, $options);
+  $linker_dbxrefs = $record->$chado_table;
+  foreach ($linker_dbxrefs as $linker_dbxref) {
+    $dbxref = $linker_dbxref->dbxref_id;
+    unset($dbxref->tablename);
+    unset($dbxref->db_id->tablename);
+    $values[] = $dbxref;
+  }
+
+  return $values;
+}

+ 1 - 1
tripal_chado/includes/fields/chado_linker__pub.inc

@@ -236,7 +236,7 @@ function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_chado_organism_select_widget.
+ * Callback function for validating the chado_linker__pub_widget.
  */
 function chado_linker__pub_widget_validate($element, &$form_state) {
 

+ 1 - 1
tripal_chado/includes/fields/chado_linker__synonym.inc

@@ -246,7 +246,7 @@ function chado_linker__synonym_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_chado_organism_select_widget.
+ * Callback function for validating the chado_linker__synonym_widget.
  */
 function chado_linker__synonym_widget_validate($element, &$form_state) {
 

+ 1 - 1
tripal_chado/includes/fields/tripal_chado_field.inc

@@ -106,7 +106,7 @@ class TripalChadoField {
 
   }
   /**
-   * Callback function for validating the chado_base__organism_id_widget.
+   * Callback function for validating the widget_.
    */
   static function widget_validate($element, &$form_state) {
 

+ 1 - 3
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -326,7 +326,7 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
 
         // Allow the creating module to alter the value if desired.  The
         // module should do this if the field has any other form elements
-        // that need populationg besides the default value.
+        // that need populationg besides the value which was set above.
         $load_function = $field_type . '_load';
         module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
         if (function_exists($load_function)) {
@@ -339,8 +339,6 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
       // loading of these fields to the field itself.
       if ($field_table != $base_table) {
 
-
-
         // Set an empty value by default, and let the hook function update it.
         $entity->{$field_name}['und'][0]['value'] = '';
         $load_function = $field_type . '_load';

+ 21 - 53
tripal_chado/includes/tripal_chado.fields.inc

@@ -352,29 +352,6 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
     $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
   }
 
-  //
-  // GENERIC COLUMNS
-  //
-//   if ($field['field_settings']['chado_column'] == 'organism_id') {
-//     $field['field_type'] = 'chado_base__organism_id';
-//     $field['widget_type'] = 'chado_base__organism_id_widget';
-//     $field['label'] = 'Organism';
-//     $field['description'] = 'Select an organism.';
-//     $field['field_settings']['semantic_web']['type'] = 'organism';
-//     $field['field_settings']['semantic_web']['ns'] = 'local';
-//     $field['field_settings']['semantic_web']['nsurl'] = '';
-
-//   }
-//   elseif ($field['field_settings']['chado_column'] =='dbxref_id') {
-//     $field['field_type'] = 'chado_base__dbxref_id';
-//     $field['widget_type'] = 'chado_base_dbxref_id_widget';
-//     $field['label'] = 'Cross Reference';
-//     $field['description'] = 'This record can be cross referenced with a ' .
-//         'record in another online database. The primary reference is for the ' .
-//         'most prominent reference.  At a minimum, the database and accession ' .
-//         'must be provided.  To remove a set reference, change the database ' .
-//         'field to "Select a Database".';
-//   }
   if ($field['label'] == 'Timeaccessioned') {
     $field['label'] = 'Time Accessioned';
     $field['description'] = 'Please enter the time that this record was first added to the database.';
@@ -400,24 +377,7 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
     $field['field_settings']['semantic_web']['ns'] = 'foaf';
     $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
   }
-  /* elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'md5checksum') {
-    $field['field_type'] = 'chado_feature__md5checksum';
-    $field['widget_type'] = 'chado_feature__md5checksum_widget';
-    $field['label'] = 'MD5 Checksum';
-    $field['description'] = 'Generate an MD5 checksum for the sequence.';
-  }
-  elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
-    $field['field_type'] = 'chado_feature__seqlen';
-    $field['widget_type'] = 'chado_feature__seqlen_widget';
-    $field['label'] = 'Seqlen';
-    $field['description'] = 'The length of the residues.';
-  }
-  elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
-    $field['field_type'] = 'chado_feature__residues';
-    $field['widget_type'] = 'chado_feature__residues_widget';
-    $field['label'] = 'Residues';
-    $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
-  } */
+
   //
   // ANALYSIS TABLE
   //
@@ -517,27 +477,35 @@ function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_sta
 function tripal_chado_field_ws_formatter($entity_type, $entity, $field_info,
     $field, $items){
 
-  $values = array();
+  $field_name = $field_info['field_name'];
+  $field_type = $field_info['type'];
+
+  $chado_table = $field_info['settings']['chado_table'];
+  $chado_column  = $field_info['settings']['chado_column'];
 
-  // Only deal with fields that were created by this module.
-  if ($field_info['storage']['type'] != 'field_chado_storage') {
-    return $values;
+  // Check to see if this field is associated with a different entity. If so
+  // we want to return the entity_id and not let the field customize the
+  // display as we need to allow the tripal_ws module to link to the other
+  // entity.
+  if (property_exists($entity->chado_record, $chado_column) and
+      property_exists($entity->chado_record->$chado_column, 'entity_id')) {
+     $fk_entity_id = $entity->chado_record->$chado_column->entity_id;
+     $value = array(
+       'entity_id' => $fk_entity_id,
+       'entity_type' => 'TripalEntity'
+     );
+     return $value;
   }
 
   // See if the field file defines a formatter.
-  $field_type = $field_info['type'];
   $function = $field_type . '_ws_formatter';
   module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
-
+  $value = '';
   if (function_exists($function)) {
-    $values = $function($entity_type, $entity, $field_info, $field, $items);
+    $value = $function($entity_type, $entity, $field_info, $field, $items);
   }
 
-  // If no customizations exist then perform some default formatting
-  if (count($values) == 0) {
-  }
-
-  return $values;
+  return $value;
 }
 
 /**

+ 24 - 9
tripal_ws/includes/tripal_ws.rest.inc

@@ -489,34 +489,49 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
       continue;
     }
 
+    $value = '';
+
+
     // We want to allow the field to format itself for web services if it
     // wants to.  If the module implements the hook_field_ws_formatter()
     // then we'll retrieve the values from it.
     $field_module = $field_info['storage']['module'];
     $function = $field_module . '_field_ws_formatter';
-    $values = array();
 
     if (function_exists($function)) {
-      $values = $function($entity->type, $entity, $field_info, $field, $items);
+      $value = $function($entity->type, $entity, $field_info, $field, $items);
     }
 
-    if (count($values) == 0) {
+    if (!$value) {
       // Get the values based on cardinality
       if (count($items) == 1) {
-        $values = $items[0]['value'];
+        $value = $items[0]['value'];
       }
       // If cardinality is greater than 1 then the value should be an array
       else {
-        $values = array();
+        $value = array();
         for ($i = 0; $i < count($items); $i++) {
-          $values[] = $items[$i]['value'];
+          $value[] = $items[$i]['value'];
         }
       }
     }
-    $response[$key] = $values;
-  }
 
- //$response['fields'] = $fields;
+    // If the value for this key is an array with an 'entity_id' key then
+    // we want to do a substitution.
+    if (is_array($value) and array_key_exists('entity_id', $value)) {
+      $lentity = entity_load($value['entity_type'], array($value['entity_id']));
+      $lentity = reset($lentity);
+      $lterm = tripal_load_term_entity(array('term_id' => $lentity->term_id));
+      $lvocab = tripal_load_vocab_entity(array('vocab_id' => $lterm->vocab_id));
+      $value = array(
+        '@id'   => $api_url . '/content/' . $lterm->name . '/' . $value['entity_id'],
+        '@type' => $lvocab->namespace . ':' . $lterm->accession,
+        'label' => $lentity->title,
+      );
+    }
+
+    $response[$key] = $value;
+  }
 
   // Lastly, add in the terms used into the @context section.
   $response['@context']['label'] = 'rdfs:label';