Browse Source

Working through bugs with fields and web services

Stephen Ficklin 8 years ago
parent
commit
7ab777451c

+ 25 - 8
tripal/includes/tripal.fields.inc

@@ -142,14 +142,31 @@ function tripal_bundle_create($bundle, $args) {
 function tripal_field_formatter_view($entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
-   $element = array();
-   $formatter_class = $display['type'];
-   $is_loaded = tripal_load_include_field_class($formatter_class);
-   if ($is_loaded) {
-     $formatter = new $formatter_class($field, $instance);
-     $formatter->view($element, $entity_type, $entity, $langcode, $items, $display);
-   }
-   return $element;
+  // Don't show any fields that don't have a controlled vocabulary term in
+  // the database.
+  $vocabulary = $instance['settings']['term_vocabulary'];
+  $accession = $instance['settings']['term_accession'];
+  $term = tripal_get_term_details($vocabulary, $accession);
+  if (!$term) {
+    tripal_set_message(t("The controlled vocabulary term, ':term (:term_name)', assigned to the
+        field, ':field', is not in the database. The field cannot be shown.
+        Please add the term and the field will appear below. ",
+        array(':field' => $field['field_name'],
+              ':term' => $vocabulary . ":" . $accession,
+              ':term_name' => $instance['settings']['term_name']
+        )),
+        TRIPAL_WARNING);
+    return;
+  }
+
+  $element = array();
+  $formatter_class = $display['type'];
+  $is_loaded = tripal_load_include_field_class($formatter_class);
+  if ($is_loaded) {
+    $formatter = new $formatter_class($field, $instance);
+    $formatter->view($element, $entity_type, $entity, $langcode, $items, $display);
+  }
+  return $element;
 }
 
 /**

+ 3 - 3
tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop.inc

@@ -83,9 +83,9 @@ class chado_linker__prop extends ChadoField {
     $entity->{$field_name}['und'][0] = array(
       'value' => '',
       'chado-' . $field_table . '__' . $pkey => '',
-      'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
+      'chado-' . $field_table . '__' . $fkey_lcolumn => '',
       'chado-' . $field_table . '__value' => '',
-      'chado-' . $field_table . '__type_id' => $cvterm_id,
+      'chado-' . $field_table . '__type_id' => '',
       'chado-' . $field_table . '__rank' => '',
     );
 
@@ -104,7 +104,7 @@ class chado_linker__prop extends ChadoField {
       $property = $properties[$i];
       foreach ($schema['fields'] as $fname => $details) {
         $entity->{$field_name}['und'][$i] = array(
-          'value' => array(),
+          'value' => $property->value,
           'chado-' . $field_table . '__' . $pkey => $property->$pkey,
           'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
           'chado-' . $field_table . '__value' => $property->value,

+ 2 - 8
tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop_formatter.inc

@@ -24,16 +24,10 @@ class chado_linker__prop_formatter extends ChadoFieldFormatter {
     $field_name = $this->field['field_name'];
     $chado_table = $this->instance['settings']['chado_table'];
 
-    $properties = array();
     foreach ($items as $delta => $item) {
-      $properties[] = $item['chado-' . $chado_table . '__value'];
-    }
-    $content = implode(', ', $properties);
-
-    if (count($items) > 0) {
-      $element[0] = array(
+      $element[$delta] = array(
         '#type' => 'markup',
-        '#markup' => $content,
+        '#markup' => $item['value'],
       );
     }
   }

+ 3 - 11
tripal_chado/includes/TripalFields/data__accession/data__accession.inc

@@ -12,7 +12,7 @@ class data__accession extends ChadoField {
   // --------------------------------------------------------------------------
 
   // The default lable for this field.
-  public static $default_label = 'Accession';
+  public static $default_label = 'Site Accession';
 
   // The default description for this field.
   public static $description = 'The unique stable accession (ID) for
@@ -59,11 +59,7 @@ class data__accession extends ChadoField {
 
     // Set some defauls for the empty record
     $entity->{$field_name}['und'][0] = array(
-      'value' => array(
-        'vocabulary' => '',
-        'accession' => '',
-        'URL' => '',
-      ),
+      'value' => '',
       'chado-' . $field_table . '__' . $field_column => '',
       'db_id' => '',
       'accession' => '',
@@ -77,11 +73,7 @@ class data__accession extends ChadoField {
       $dbxref = $record->$field_column;
       $value = $dbxref->db_id->name . ':' . $dbxref->accession;
       $entity->{$field_name}['und'][0] = array(
-        'value' => array(
-          'vocabulary' => $dbxref->db_id->name,
-          'accession' => $dbxref->accession,
-          'URL' => tripal_get_dbxref_url($dbxref),
-        ),
+        'value' => $dbxref->accession,
         'chado-' . $field_table . '__' . $field_column => $record->$field_column->$field_column,
         'db_id'       => $dbxref->db_id->db_id,
         'accession'   => $dbxref->accession,

+ 2 - 5
tripal_chado/includes/TripalFields/data__accession/data__accession_formatter.inc

@@ -2,7 +2,7 @@
 
 class data__accession_formatter extends ChadoFieldFormatter {
   // The default lable for this field.
-  public static $default_label = 'Accession';
+  public static $default_label = 'Site Accession';
 
   // The list of field types for which this formatter is appropriate.
   public static $field_types = array('data__accession');
@@ -14,13 +14,10 @@ class data__accession_formatter extends ChadoFieldFormatter {
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
 
     foreach ($items as $delta => $item) {
-      if ($item['value']) {
-        $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
         $element[$delta] = array(
           '#type' => 'markup',
-          '#markup' => $content,
+          '#markup' => $item['value'],
         );
-      }
     }
   }
 }

+ 1 - 1
tripal_chado/includes/TripalFields/data__accession/data__accession_widget.inc

@@ -1,7 +1,7 @@
 <?php
 class data__accession_widget extends ChadoFieldWidget {
   // The default lable for this field.
-  public static $default_label = 'Accession';
+  public static $default_label = 'Site Accession';
 
   // The list of field types for which this formatter is appropriate.
   public static $field_types = array('data__accession');

+ 1 - 1
tripal_chado/includes/TripalFields/obi__organism/obi__organism.inc

@@ -21,7 +21,7 @@ class obi__organism extends ChadoField {
     // The name of the term.
     'term_name' => 'organism',
     // The unique ID (i.e. accession) of the term.
-    'term_accession' => 'organism',
+    'term_accession' => '0100026',
     // Set to TRUE if the site admin is allowed to change the term
     // type. This will create form elements when editing the field instance
     // to allow the site admin to change the term settings above.

+ 2 - 6
tripal_chado/includes/TripalFields/schema__alternate_name/schema__alternate_name.inc

@@ -79,7 +79,7 @@ class schema__alternate_name extends ChadoField {
 
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
-      'value' => array(),
+      'value' => '',
       'chado-' . $field_table . '__' . $pkey => '',
       'chado-' . $field_table . '__' . $fkey_lcolumn => '',
       'chado-' . $field_table . '__' . 'synonym_id' => '',
@@ -99,11 +99,7 @@ class schema__alternate_name extends ChadoField {
       foreach ($record->$linker_table as $index => $linker) {
         $synonym = $linker->synonym_id;
         $entity->{$field_name}['und'][$i] = array(
-          'value' => array(
-            '@type' => $synonym->type_id->dbxref_id->db_id->name . ':' . $synonym->type_id->dbxref_id->accession,
-            'type' => $synonym->type_id->name,
-            'name' => $synonym->name,
-          ),
+          'value' => $synonym->name,
           'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
           'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
           'chado-' . $field_table . '__' . 'synonym_id' => $synonym->synonym_id,

+ 4 - 13
tripal_chado/includes/TripalFields/schema__alternate_name/schema__alternate_name_formatter.inc

@@ -20,20 +20,11 @@ class schema__alternate_name_formatter extends ChadoFieldFormatter {
    * @see TripalFieldFormatter::view()
    */
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
-    $chado_table = $this->instance['settings']['chado_table'];
     foreach ($items as $delta => $item) {
-      if (array_key_exists('chado-' . $chado_table . '__synonym_id', $item) and
-          $item['chado-' . $chado_table . '__synonym_id']) {
-            $synonym = chado_generate_var('synonym', array('synonym_id' => $item['chado-' . $chado_table . '__synonym_id']));
-            $name = $synonym->name;
-            if ($synonym->type_id->name != 'exact') {
-              $name .= ' (<i>' . $synonym->type_id->name . '</i>)';
-            }
-            $element[$delta] = array(
-              '#type' => 'markup',
-              '#markup' => $name,
-            );
-          }
+      $element[$delta] = array(
+        '#type' => 'markup',
+        '#markup' => $item['value'],
+      );
     }
   }
 }

+ 11 - 3
tripal_chado/includes/tripal_chado.fields.inc

@@ -1231,11 +1231,19 @@ function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bund
   // PROPERTIES
   $prop_table = $table_name . 'prop';
   if (chado_table_exists($prop_table)) {
-     // Get the list of existing property types for this table.
-     $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
-     $props = chado_query($sql);
+     $tschema = chado_get_schema($table_name);
      $schema = chado_get_schema($prop_table);
+     $tpkey = $tschema['primary key'][0];
      $pkey = $schema['primary key'][0];
+     // Get the list of existing property types for this table.
+     $sql = "
+       SELECT DISTINCT P.type_id
+       FROM {" . $prop_table . "} P
+         INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
+       WHERE T.type_id = :cvterm_id
+     ";
+     $args = array(':cvterm_id' => $cvterm_id);
+     $props = chado_query($sql, $args);
      while ($prop = $props->fetchObject()) {
        $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
        $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));

+ 39 - 1
tripal_chado/includes/tripal_chado.semweb.inc

@@ -25,6 +25,7 @@ function tripal_chado_populate_chado_semweb_table() {
   tripal_chado_populate_vocab_LOCAL();
   tripal_chado_populate_vocab_NCBITAXON();
   tripal_chado_populate_vocab_OBI();
+  tripal_chado_populate_vocab_OGI();
   tripal_chado_populate_vocab_RDFS();
   tripal_chado_populate_vocab_SBO();
   tripal_chado_populate_vocab_SCHEMA();
@@ -69,7 +70,7 @@ function tripal_chado_populate_vocab_RDFS() {
   ));
   $name = tripal_insert_cvterm(array(
     'id' => 'rdfs:label',
-    'name' => 'type',
+    'name' => 'label',
     'cv_name' => 'rdfs',
     'definition' => 'A human-readable version of a resource\'s name.',
   ));
@@ -118,6 +119,14 @@ function tripal_chado_populate_vocab_SCHEMA() {
   ));
   tripal_associate_chado_semweb_term(NULL, 'description', $term);
   tripal_associate_chado_semweb_term('organism', 'comment', $term);
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'schema:publication',
+    'name' => 'publication',
+    'cv_name' => 'schema',
+    'definition' => 'A publication event associated with the item.',
+  ));
+  tripal_associate_chado_semweb_term(NULL, 'pub_id', $term);
 }
 /**
  * Adds the EDAM database and terms.
@@ -208,6 +217,14 @@ function tripal_chado_populate_vocab_EDAM() {
     'definition' => 'A map of DNA (linear or circular) annotated with physical features or landmarks such as restriction sites, cloned DNA fragments, genes or genetic markers, along with the physical distances between them. Distance in a physical map is measured in base pairs. A physical map might be ordered relative to a reference map (typically a genetic map) in the process of genome sequencing.',
   ));
   tripal_associate_chado_semweb_term('featuremap', 'featuremap_id', $term);
+
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'data:2012',
+    'name' => 'Sequence coordinates',
+    'cv_name' => 'EDAM',
+    'definition' => 'A position in a map (for example a genetic map), either a single position (point) or a region / interval.',
+  ));
 }
 
 /**
@@ -230,6 +247,27 @@ function tripal_chado_populate_vocab_OBI() {
   ));
   tripal_associate_chado_semweb_term(NULL, 'organism_id', $term);
 }
+
+/**
+ * Adds the Ontology for genetic interval database and terms.
+ */
+function tripal_chado_populate_vocab_OGI() {
+  tripal_insert_db(array(
+    'name' => 'OGI',
+    'description' => 'Ontology for genetic interval.',
+    'url' => 'http://purl.bioontology.org/ontology/OGI',
+    'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
+  ));
+  tripal_insert_cv('obi','Ontology for Biomedical Investigation. The Ontology for Biomedical Investigations (OBI) is build in a collaborative, international effort and will serve as a resource for annotating biomedical investigations, including the study design, protocols and instrumentation used, the data generated and the types of analysis performed on the data. This ontology arose from the Functional Genomics Investigation Ontology (FuGO) and will contain both terms that are common to all biomedical investigations, including functional genomics investigations and those that are more domain specific.');
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'OGI:0000021',
+    'name' => 'location on map',
+    'cv_name' => 'ogi',
+    'definition' => '',
+  ));
+  tripal_associate_chado_semweb_term(NULL, 'organism_id', $term);
+}
 /**
  * Adds the Information Artifact Ontology database and terms.
  */

+ 15 - 16
tripal_ws/includes/tripal_ws.rest_v0.1.inc

@@ -564,18 +564,20 @@ function tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_u
     $vocabulary = $instance['settings']['term_vocabulary'];
     $accession = $instance['settings']['term_accession'];
     $term = tripal_get_term_details($vocabulary, $accession);
-    $term = tripal_get_term_details($vocabulary, $accession);
     if ($term) {
       $key = $term['name'];
       $key_adj = strtolower(preg_replace('/ /', '_', $key));
       $response['@context'][$key_adj] = $term['url'];
     }
+    else {
+      continue;
+    }
 
     // If this field should not be attached by default then just add a link
     // so that the caller can get the information separately.
     $instance_settings = $instance['settings'];
     if (array_key_exists('auto_attach', $instance['settings']) and
-        $instance_settings['auto_attach'] != TRUE) {
+        !$instance_settings['auto_attach']) {
       $response['@context'][$key_adj] = array(
         '@id' => $response['@context'][$key_adj],
         '@type' => '@id'
@@ -753,36 +755,33 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
   if (!$items) {
     return;
   }
+
   $values = array();
   for ($i = 0; $i < count($items); $i++) {
 
     // See if the keys in the values array have been mapped to semantic
     // web terms.
-    if (array_key_exists('semantic_web', $items[$i])) {
-      foreach ($items[$i]['semantic_web'] as $k => $v) {
+    if (is_array($items[$i]['value'])) {
+      $temp = array();
+      foreach ($items[$i]['value'] as $k => $v) {
         $matches = array();
-        if (preg_match('/^(.+):(.+)$/', $v, $matches)) {
+        if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
           $vocabulary = $matches[1];
           $accession = $matches[2];
           $term = tripal_get_term_details($vocabulary, $accession);
-          if ($k == 'type') {
-            $items[$i]['value']['@type'] = $items[$i]['value']['type'];
-            unset($items[$i]['value']['type']);
-            $response['@context'][$term['name']] = $term['url'];
-          }
-          else {
-            $response['@context'][$k] = $term['url'];
-          }
+          $temp[$term['name']] = $v;
+          $response['@context'][$term['name']] = $term['url'];
         }
       }
+      $values[] = $temp;
+    }
+    else {
+      $values[] = $items[$i]['value'];
     }
 
     // Recurse through the values array and set the entity elemetns
     // and add the fields to the context.
     tripal_ws_services_v0_1_get_content_add_field_context($items[$i], $response, $api_url);
-
-    // Add the remaining values to the $values array.
-    $values[] = $items[$i]['value'];
   }
 
   // If we only have one value then set the response with just the value.

+ 1 - 3
tripal_ws/tripal_ws.module

@@ -134,9 +134,7 @@ function tripal_ws_services() {
  */
 function tripal_ws_entity_load($entities, $type) {
   foreach ($entities as $entity) {
-    //if ($entiy->type = 'TripalEntity') {
-    //  dpm($entity);
-    //}
+
   }
 }