Browse Source

Added query/queryOrder to the annotation field. Added order_rank term with db update. Fixed bug in web services

Stephen Ficklin 7 years ago
parent
commit
23f7870887

+ 1 - 1
tripal/includes/TripalFields/TripalField.inc

@@ -208,7 +208,7 @@ class TripalField {
   /**
    * Describes this field to Tripal web services.
    *
-   * The child class need not implement this function has all of the details
+   * The child class need not implement this function. It has all of the details
    * provided for elements by the elementInfo() function are used to generate
    * the details needed for Views.
    *

+ 185 - 0
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation.inc

@@ -73,6 +73,187 @@ class sio__annotation extends ChadoField {
     }
   }
 
+  /**
+   * @see TripalField::elementInfo()
+   */
+  public function elementInfo() {
+    $field_table = $this->instance['settings']['chado_table'];
+    $schema = chado_get_schema($field_table);
+
+    $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
+    $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
+    $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
+
+    $field_term = $this->getFieldTermID();
+    $info = array(
+      $field_term => array(
+        'operations' => array(),
+        'sortable' => FALSE,
+        'searchable' => FALSE,
+        'type' => 'array',
+        'elements' => array(
+          $vocabulary_term => array(
+            'sortable' => TRUE,
+            'searchable' => TRUE,
+            'name' => 'vocabulary',
+            'label' => 'Annotation Term Vocabulary',
+            'operations' => array('eq', 'ne', 'contains', 'starts'),
+          ),
+          $accession_term => array(
+            'sortable' => TRUE,
+            'searchable' => TRUE,
+            'name' => 'accession',
+            'label' => 'Annotation Term Accession',
+            'operations' => array('eq', 'ne', 'contains', 'starts'),
+          ),
+          $definition_term = array(
+            'sortable' => TRUE,
+            'searchable' => TRUE,
+            'name' => 'description',
+            'label' => 'Annotation Term Description',
+            'operations' => array('eq', 'ne', 'contains', 'starts'),
+          ),
+        ),
+      ),
+    );
+
+    if (array_key_exists('is_not', $schema['fields'])) {
+      $negation_term = tripal_get_chado_semweb_term($field_table, 'is_not');
+      $info[$field_term]['elements'][$negation_term] = array(
+        'sortable' => TRUE,
+        'searchable' => TRUE,
+        'name' => 'negates',
+        'label' => 'Annotation Term Negates',
+        'operations' => array('eq', 'ne'),
+      );
+    }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $rank_term = tripal_get_chado_semweb_term($field_table, 'rank');
+      $info[$field_term]['elements'][$rank_term] = array(
+        'sortable' => TRUE,
+        'searchable' => TRUE,
+        'name' => 'rank',
+        'label' => 'Annotation Term Rank',
+        'operations' => array('eq', 'ne', 'lte', 'gte'),
+        'type' => numeric
+      );
+    }
+    if (array_key_exists('pub_id', $schema['fields'])) {
+    }
+    return $info;
+  }
+
+  /**
+   * @see ChadoField::query()
+   */
+  public function query($query, $condition) {
+    $alias = $this->field['field_name'];
+    $operator = $condition['operator'];
+
+    $field_table = $this->instance['settings']['chado_table'];
+    $base_table = $this->instance['settings']['base_table'];
+
+    $schema = chado_get_schema($field_table);
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    $field_term_id = $this->getFieldTermID();
+
+    $vocabulary_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'cv_id');
+    $accession_term = $field_term_id . ',' . tripal_get_chado_semweb_term('dbxref', 'accession');
+    $definition_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'definition');
+
+
+    // Join to the xxx_cvterm table for this field.
+    $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
+
+    if ($condition['column'] == $vocabulary_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
+      $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id");
+      $query->condition($alias . '_db.name', $condition['value'], $operator);
+    }
+    if ($condition['column'] == $accession_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
+      $query->condition($alias . '_dbxref.accession', $condition['value'], $operator);
+    }
+    if ($condition['column'] == $definition_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $query->condition($alias . '_cvterm.definition', $condition['value'], $operator);
+    }
+
+    if (array_key_exists('is_not', $schema['fields'])) {
+      $negation_term = $field_term_id . ',' . tripal_get_chado_semweb_term($field_table, 'is_not');
+      if ($condition['column'] == $negation_term) {
+        $query->condition($alias . '.is_not', $condition['value'], $operator);
+      }
+    }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $rank_term = $field_term_id . ',' . tripal_get_chado_semweb_term($field_table, 'rank');
+      if ($condition['column'] == $rank_term) {
+        $query->condition($alias . '.rank', $condition['value'], $operator);
+      }
+    }
+    if (array_key_exists('pub_id', $schema['fields'])) {
+    }
+  }
+
+  /**
+   * @see ChadoField::queryOrder()
+   */
+  public function queryOrder($query, $order) {
+    $alias = $this->field['field_name'];
+
+    $field_table = $this->instance['settings']['chado_table'];
+    $base_table = $this->instance['settings']['base_table'];
+
+    $schema = chado_get_schema($field_table);
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    $field_term_id = $this->getFieldTermID();
+
+    $vocabulary_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'cv_id');
+    $accession_term = $field_term_id . ',' . tripal_get_chado_semweb_term('dbxref', 'accession');
+    $definition_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'definition');
+
+    // Join to the xxx_cvterm table for this field.
+    $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
+
+    if ($order['column'] == $vocabulary_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
+      $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id");
+      $query->orderBy($alias . "_dbxref.name", $order['direction']);
+    }
+    if ($order['column'] == $accession_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
+      $query->orderBy($alias . "_dbxref.accession", $order['direction']);
+    }
+    if ($order['column'] == $definition_term) {
+      $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
+      $query->orderBy($alias . "_cvterm.definition", $order['direction']);
+    }
+    if (array_key_exists('is_not', $schema['fields'])) {
+      $negation_term = $field_term_id . ',' . tripal_get_chado_semweb_term($field_table, 'is_not');
+      if ($order['column'] == $negation_term) {
+        $query->orderBy("$alias.is_not", $order['direction']);
+      }
+    }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $rank_term = $field_term_id . ',' . tripal_get_chado_semweb_term($field_table, 'rank');
+      if ($order['column'] == $rank_term) {
+        $query->orderBy("$alias.rank", $order['direction']);
+      }
+    }
+    if (array_key_exists('pub_id', $schema['fields'])) {
+    }
+
+  }
   /**
    *
    * @see TripalField::load()
@@ -96,6 +277,9 @@ class sio__annotation extends ChadoField {
     if (array_key_exists('is_not', $schema['fields'])) {
       $negation = tripal_get_chado_semweb_term($field_table, 'is_not');
     }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $rank_term = tripal_get_chado_semweb_term($field_table, 'rank');
+    }
 
     // Set some defaults for the empty record.
     $chado_record = $entity->chado_record;
@@ -147,6 +331,7 @@ class sio__annotation extends ChadoField {
         $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__is_not'] = $linker->is_not;
       }
       if (array_key_exists('rank', $schema['fields'])) {
+        $entity->{$field_name}['und'][$i]['value'][$rank_term] = $linker->rank;
         $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $linker->rank;
       }
       if (array_key_exists('pub_id', $schema['fields'])) {

+ 2 - 1
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation_formatter.inc

@@ -16,6 +16,7 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
     $headers = array('Term', 'Definition', 'Is Not', 'Reference');
     $rows = array();
 
+
     $chado_table = $this->instance['settings']['chado_table'];
     foreach ($items as $delta => $item) {
       if ($item['chado-' . $chado_table . '__cvterm_id']) {
@@ -76,7 +77,7 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
     $headers = array('Term', 'Definition');
     $rows = array();
-
+    dpm($items);
     $field_table = $this->instance['settings']['chado_table'];
     $schema = chado_get_schema($field_table);
 

+ 1 - 0
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation_widget.inc

@@ -7,6 +7,7 @@ class sio__annotation_widget extends ChadoFieldWidget {
   // The list of field types for which this formatter is appropriate.
   public static $field_types = array('chado_linker__cvterm');
 
+
   /**
    *
    * @see TripalFieldWidget::form()

+ 2 - 2
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -621,8 +621,8 @@ function tripal_chado_field_storage_query($query) {
     } // end if ($sort['type'] == 'field') {
   } // end foreach ($query->order as $index => $sort) {
 
-  //dpm($cquery->__toString());
-  //dpm($cquery->getArguments());
+  dpm($cquery->__toString());
+  dpm($cquery->getArguments());
 
   $records = $cquery->execute();
 

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

@@ -27,6 +27,7 @@ function tripal_chado_populate_chado_semweb_table() {
   tripal_chado_populate_vocab_IAO();
   tripal_chado_populate_vocab_LOCAL();
   tripal_chado_populate_vocab_NCBITAXON();
+  tripal_chado_populate_vocab_OBCS();
   tripal_chado_populate_vocab_OBI();
   tripal_chado_populate_vocab_OGI();
   tripal_chado_populate_vocab_RDFS();
@@ -541,6 +542,29 @@ function tripal_chado_populate_vocab_ERO() {
   ));
 }
 
+/**
+ * Adds the Information Artifact Ontology database and terms.
+ */
+function tripal_chado_populate_vocab_OBCS() {
+  tripal_insert_db(array(
+    'name' => 'OBCS',
+    'description' => 'Ontology of Biological and Clinical Statistics.',
+    'url' => 'https://github.com/obcs/obcs',
+    'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
+  ));
+  tripal_insert_cv(
+    'OBCS',
+    'Ontology of Biological and Clinical Statistics.'
+  );
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'OBCS:0000117',
+    'name' => 'rank order',
+    'cv_name' => 'OBCS',
+    'definition' => 'A data item that represents an arrangement according to a rank, i.e., the position of a particular case relative to other cases on a defined scale.',
+  ));
+  tripal_associate_chado_semweb_term(NULL, 'rank', $term);
+}
 /**
  * Adds the Information Artifact Ontology database and terms.
  */
@@ -597,7 +621,7 @@ function tripal_chado_populate_vocab_IAO() {
     'name' => 'IAO',
     'description' => 'The Information Artifact Ontology (IAO).',
     'url' => 'https://github.com/information-artifact-ontology/IAO/',
-    'urlprefix' => 'http://purl.obolibrary.org/obo/IAO_',
+    'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
   ));
   tripal_insert_cv(
     'IAO',

+ 29 - 0
tripal_chado/tripal_chado.install

@@ -1109,4 +1109,33 @@ function tripal_chado_update_7309() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
+}
+/**
+ * Adds the 'OBCS' and rank order term.
+ */
+function tripal_chado_update_7310() {
+  try {
+    tripal_insert_db(array(
+      'name' => 'OBCS',
+      'description' => 'Ontology of Biological and Clinical Statistics.',
+      'url' => 'https://github.com/obcs/obcs',
+      'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
+    ));
+    tripal_insert_cv(
+        'OBCS',
+        'Ontology of Biological and Clinical Statistics.'
+    );
+
+    $term = tripal_insert_cvterm(array(
+      'id' => 'OBCS:0000117',
+      'name' => 'rank order',
+      'cv_name' => 'OBCS',
+      'definition' => 'A data item that represents an arrangement according to a rank, i.e., the position of a particular case relative to other cases on a defined scale.',
+    ));
+    tripal_associate_chado_semweb_term(NULL, 'rank', $term);
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
 }

+ 45 - 39
tripal_ws/includes/TripalWebService/TripalEntityService_v0_1.inc

@@ -510,9 +510,11 @@ class TripalEntityService_v0_1 extends TripalWebService {
           $searchable_keys = $key_field->webServicesData();
           $criteria = implode('.', $subkeys);
           if (array_key_exists($criteria, $searchable_keys)) {
-            $new_params[$key_field_name]['value'] = $value;
-            $new_params[$key_field_name]['op'] = $op;
-            $new_params[$key_field_name]['column'] = $searchable_keys[$criteria];
+            $new_params[$key_field_name][] = array(
+              'value' => $value,
+              'op' => $op,
+              'column' => $searchable_keys[$criteria]
+            );
           }
           else {
             throw new Exception("The filter term, '$criteria', is not available for use.");
@@ -523,9 +525,11 @@ class TripalEntityService_v0_1 extends TripalWebService {
         else {
           $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
 
-          $new_params[$key_field_name]['value'] = $value;
-          $new_params[$key_field_name]['op'] = $op;
-          $new_params[$key_field_name]['column'] = $key_field_id;
+          $new_params[$key_field_name][] = array(
+            'value' => $value,
+            'op' => $op,
+            'column' => $key_field_id,
+          );
         }
       }
       else {
@@ -537,40 +541,42 @@ class TripalEntityService_v0_1 extends TripalWebService {
     $query = new TripalFieldQuery();
     $query->entityCondition('entity_type', 'TripalEntity');
     $query->entityCondition('bundle', $bundle->name);
-    foreach($new_params as $field_name => $details) {
-      $value = $details['value'];
-      $column_name = $details['column'];
-      switch ($details['op']) {
-        case 'eq':
-          $op = '=';
-          break;
-        case 'gt':
-          $op = '>';
-          break;
-        case 'gte':
-          $op = '>=';
-          break;
-        case 'lt':
-          $op = '<';
-          break;
-        case 'lte':
-          $op = '<=';
-          break;
-        case 'ne':
-          $op = '<>';
-          break;
-        case 'contains':
-          $op = 'CONTAINS';
-          break;
-        case 'starts':
-          $op = 'STARTS WITH';
-          break;
-        default:
-          $op = '=';
+    foreach($new_params as $field_name => $param_list) {
+      foreach ($param_list as $param_index => $details) {
+        $value = $details['value'];
+        $column_name = $details['column'];
+        switch ($details['op']) {
+          case 'eq':
+            $op = '=';
+            break;
+          case 'gt':
+            $op = '>';
+            break;
+          case 'gte':
+            $op = '>=';
+            break;
+          case 'lt':
+            $op = '<';
+            break;
+          case 'lte':
+            $op = '<=';
+            break;
+          case 'ne':
+            $op = '<>';
+            break;
+          case 'contains':
+            $op = 'CONTAINS';
+            break;
+          case 'starts':
+            $op = 'STARTS WITH';
+            break;
+          default:
+            $op = '=';
+        }
+        // We pass in the $column_name as an identifier for any sub fields
+        // that are present for the fields.
+        $query->fieldCondition($field_name, $column_name, $value, $op);
       }
-      // We pass in the $column_name as an identifier for any sub fields
-      // that are present for the fields.
-      $query->fieldCondition($field_name, $column_name, $value, $op);
     }
 
     // Perform the query just as a count first to get the number of records.