瀏覽代碼

Working on new chado_bio_data_xxx tables

Stephen Ficklin 8 年之前
父節點
當前提交
455a1942b5

+ 3 - 23
tripal/includes/TripalBundleController.inc

@@ -79,30 +79,10 @@ class TripalBundleController extends EntityAPIControllerExportable {
           }
         }
 
-        // TODO: this Chado sepcific code needs to be moved out of here!
-
-        // Remove the entries in the chado_entity and tripal_entity
-        $query = db_select('chado_entity', 'ce');
-        $query->join('tripal_entity', 'te', 'te.id = ce.entity_id');
-        $records = $query->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
-          ->condition('te.bundle', $bundle->name)
-          ->execute();
-        $num_removed = 0;
-        while ($record = $records->fetchObject()) {
-          db_delete('chado_entity')
-            ->condition('chado_entity_id', $record->chado_entity_id)
-            ->execute();
-          db_delete('tripal_entity')
-            ->condition('id', $record->chado_entity_id)
-            ->execute();
-          $num_removed++;
-        }
-        db_delete('chado_bundle')
-          ->condition('bundle_id', $bundle->id)
+        // Remove any entities from the tripal_entity table.
+        db_delete('tripal_entity')
+          ->condition('bundle', $bundle->name)
           ->execute();
-        if ($num_removed > 0) {
-          drupal_set_message(t('Removed %num records', array('%num' => $num_removed)));
-        }
 
         // Remove the terms for the bundles that are to be deleted.
         db_delete('tripal_term')

+ 32 - 9
tripal_chado/api/modules/tripal_chado.feature.api.inc

@@ -1091,6 +1091,9 @@ function chado_get_aggregate_feature_relationships($feature_id, $substitute=1,
  *
  */
 function chado_get_feature_relationships($feature_id, $side = 'as_subject') {
+
+  $feature = chado_generate_var('feature', array('feature_id' => $feature_id));
+
   // get the relationships for this feature.  The query below is used for both
   // querying the object and subject relationships
   $sql = "
@@ -1119,20 +1122,24 @@ function chado_get_feature_relationships($feature_id, $side = 'as_subject') {
   // get the relationships
   $results = chado_query($sql, array(':feature_id' => $feature_id));
 
+  // Get the bundle for this feature type, if one exists.
+  $term = tripal_load_term_entity(array(
+    'vocabulary' => $feature->type_id->dbxref_id->db_id->name,
+    'accession' => $feature->type_id->dbxref_id->accession,
+  ));
+  $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
+
   // iterate through the relationships, put these in an array and add
   // in the Drupal node id if one exists
   $i=0;
-  $esql = "
-    SELECT entity_id
-    FROM {chado_entity}
-    WHERE record_id = :feature_id";
   $relationships = array();
   while ($rel = $results->fetchObject()) {
-    $entity = db_query($esql, array(':feature_id' => $rel->subject_id))->fetchObject();
+
+    $entity = tripal_chado_get_record_entity($bundle, $rel->subject_id);
     if ($entity) {
       $rel->subject_entity_id = $entity->entity_id;
     }
-    $entity = db_query($esql, array(':feature_id' => $rel->object_id))->fetchObject();
+    $entity = tripal_chado_get_record_entity($bundle, $rel->object_id);
     if ($entity) {
       $rel->object_entity_id = $entity->entity_id;
     }
@@ -1181,11 +1188,27 @@ function chado_get_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1)
   $i=0;
   $featurelocs = array();
   while ($loc = $flresults->fetchObject()) {
-    // if a drupal node exists for this feature then add the nid to the
+    // if a drupal entity exists for this feature then add the nid to the
     // results object
 
-    $loc->feid = tripal_get_chado_entity_id('feature', $loc->feature_id);
-    $loc->seid = tripal_get_chado_entity_id('feature', $loc->src_feature_id);
+
+    // Get the bundle for this feature type, if one exists.
+    $ffeature = chado_generate_var('feature', array('feature_id' => $loc->feature_id));
+    $sfeature = chado_generate_var('feature', array('feature_id' => $loc->src_feature_id));
+    $fterm = tripal_load_term_entity(array(
+      'vocabulary' => $ffeature->type_id->dbxref_id->db_id->name,
+      'accession' => $ffeature->type_id->dbxref_id->accession,
+    ));
+    $sterm = tripal_load_term_entity(array(
+      'vocabulary' => $sfeature->type_id->dbxref_id->db_id->name,
+      'accession' => $sfeature->type_id->dbxref_id->accession,
+    ));
+    $fbundle = tripal_load_bundle_entity(array('term_id' => $fterm->id));
+    $sbundle = tripal_load_bundle_entity(array('term_id' => $sterm->id));
+
+    $loc->feid = tripal_chado_get_record_entity($fbundle, $loc->feature_id);
+    $loc->seid = tripal_chado_get_record_entity($sbundle, $loc->src_feature_id);
+
     // add the result to the array
     $featurelocs[$i++] = $loc;
   }

+ 4 - 44
tripal_chado/api/tripal_chado.api.inc

@@ -1,44 +1,5 @@
 <?php
 
-/**
- * Retrieves an entity that matches the given table and record id.
- *
- * @param $table
- *   The name of the Chado table.
- * @param $record_id
- *   The record's primary key in the table specified by $table.
- *
- * @return
- *   A chado_entity object.
- */
-function tripal_load_chado_entity($table, $record_id) {
-   $entity_id = db_select('chado_entity', 'ce')
-     ->fields('ce', array('entity_id'))
-     ->condition('ce.record_id', $record_id)
-     ->condition('ce.data_table', $table)
-     ->execute()
-     ->fetchField();
-   if ($entity_id) {
-     $entity = entity_load('TripalEntity', array($entity_id));
-     return reset($entity);
-   }
-   return NULL;
-}
-
-/**
- * Retrieves the entity ID using a record ID.
- *
- * @param unknown $data_table
- * @param unknown $record_id
- */
-function tripal_get_chado_entity_id($data_table, $record_id) {
-   return db_select('chado_entity', 'ce')
-     ->fields('ce', array('entity_id'))
-     ->condition('data_table', $data_table)
-     ->condition('record_id', $record_id)
-     ->execute()
-     ->fetchField();
-}
 
 /**
  * Publishes content in Chado as a new TripalEntity entity.
@@ -79,6 +40,7 @@ function tripal_chado_publish_records($values, $job_id = NULL) {
         array('@error' => 'The bundle name must be provided'));
     return FALSE;
   }
+  $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
 
   $table = $bundle->data_table;
   $type_column = $bundle->type_column;
@@ -94,8 +56,7 @@ function tripal_chado_publish_records($values, $job_id = NULL) {
   $select = "SELECT $pkey_field as record_id ";
   $from = "
     FROM {" . $table . "} T
-      LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
-         AND CE.data_table = :table
+      LEFT JOIN public.$chado_entity_table CE on CE.record_id = T.$pkey_field
   ";
 
   // For migration of Tripal v2 nodes to entities we want to include the
@@ -196,11 +157,10 @@ function tripal_chado_publish_records($values, $job_id = NULL) {
         throw new Exception('Could not create entity.');
       }
 
-      // Next save the chado_entity record.
+      // Next save the chado entity record.
       $entity_record = array(
         'entity_id' => $entity->id,
         'record_id' => $record_id,
-        'data_table' => $table,
       );
 
       // For the Tv2 to Tv3 migration we want to add the nid to the
@@ -208,7 +168,7 @@ function tripal_chado_publish_records($values, $job_id = NULL) {
       if (property_exists($record, 'nid')) {
         $entity_record['nid'] = $record->nid;
       }
-      $success = drupal_write_record('chado_entity', $entity_record);
+      $success = drupal_write_record($chado_entity_table, $entity_record);
 
       $i++;
     }

+ 1 - 1
tripal_chado/api/tripal_chado.variables.api.inc

@@ -309,7 +309,7 @@ function chado_generate_var($table, $values, $base_options = array()) {
         }
       }
 
-      // Check to see if the current table maps to an entity
+      // Check to see if the current record maps to an entity
       $entity_id = db_select('chado_entity', 'ce')
         ->fields('ce', array('entity_id'))
         ->condition('data_table', $table)

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

@@ -6,7 +6,7 @@ class obi__organism extends ChadoField {
   public static $default_label = 'Organism';
 
   // The default description for this field.
-  public static $description = 'The organism to which this resource is sssociated.';
+  public static $description = 'The organism to which this resource is associated.';
 
   // Provide a list of instance specific settings. These can be access within
   // the instanceSettingsForm.  When the instanceSettingsForm is submitted

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

@@ -112,7 +112,7 @@ class so__transcript extends ChadoField {
         'data:0842' => $transcript->uniquename,
         'SO:0000735' => $loc,
       );
-      $entity_id = tripal_get_chado_entity_id($field_table, $record->feature_id);
+      $entity_id = tripal_chado_get_record_entity($entity->bundle, $record_id);
       if ($entity_id) {
          $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $entity_id;
        }

+ 64 - 1
tripal_chado/includes/tripal_chado.bundle.inc

@@ -58,4 +58,67 @@ function tripal_chado_bundle_create($bundle, $storage_args) {
       throw new Exception('Cannot create content type. Problem associating type with Chado.');
     }
   }
-}
+
+  tripal_chado_create_bundle_table($bundle);
+}
+
+
+/**
+ * Creates the table that tripal_chado uses to link Chado records with entities.
+ *
+ * @param $bundle
+ *   A bundle object (as retreieved from tripal_load_bundle_entity().
+ */
+function tripal_chado_create_bundle_table($bundle) {
+
+  // Now create the table where the bundle's records will go
+  $schema = array(
+    'description' => 'The linker table that associates TripalEntities with Chado records for entities of type ' . $bundle->name . '.',
+    'fields' => array(
+      'entity_id' => array(
+        'description' => 'The unique entity id.',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'record_id' => array(
+        'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'nid' => array(
+        'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
+        'type' => 'int',
+      )
+    ),
+    'indexes' => array(
+      'record_id' => array('record_id'),
+      'entity_id' => array('entity_id'),
+      'nid' => array('nid'),
+    ),
+    'unique keys' => array(
+      'table_record' => array('record_id'),
+      'entity_id' => array('entity_id'),
+    ),
+  );
+  $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+  db_create_table($chado_entity_table, $schema);
+}
+
+/**
+ * Implements hook_bundle_delete().
+ *
+ */
+function tripal_chado_bundle_delete($bundle) {
+
+  // Remove the entries in the appropriate chado entity table
+  // and tripal_entity
+  $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+  $sql = "DROP TABLE {$chado_entity_table}";
+  db_query($sql);
+
+  // Remove the entry from the chado_bundle table.
+  db_delete('chado_bundle')
+    ->condition('bundle_id', $bundle->id)
+    ->execute();
+}
+

+ 2 - 1
tripal_chado/includes/tripal_chado.entity.inc

@@ -87,7 +87,8 @@ function tripal_chado_entity_load($entities, $type) {
         $entity->chado_table = $bundle->data_table;
         $entity->chado_column = $bundle->type_column;
 
-        $chado_entity = db_select('chado_entity' ,'ce')
+        $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+        $chado_entity = db_select($chado_entity_table, 'ce')
           ->fields('ce')
           ->condition('ce.entity_id', $entity->id)
           ->execute()

+ 16 - 5
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -68,6 +68,16 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
     if (!$success) {
       drupal_set_message('Unable to insert new Chado entity.', 'error');
     }
+    // Add the record to the proper chado entity table too
+    $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+    $record = array(
+      'entity_id' => $entity->id,
+      'record_id' => $base_record_id,
+    );
+    $success = drupal_write_record($chado_entity_table, $record);
+    if (!$success) {
+      drupal_set_message('Unable to insert new Chado entity.', 'error');
+    }
   }
 
   // Now that we have handled the base table, we need to handle linking tables.
@@ -201,8 +211,9 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
       $base_table = $bundle->data_table;
       $type_field = $bundle->type_column;
 
-      // Get the base table and record id for the fields of this entity.
-      $details = db_select('chado_entity', 'ce')
+      // Get the record id for the fields of this entity.
+      $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+      $details = db_select($chado_entity_table, 'ce')
         ->fields('ce')
         ->condition('entity_id', $entity->id)
         ->execute()
@@ -501,12 +512,12 @@ function tripal_chado_field_storage_query($query) {
     }
   } // end foreach ($query->fieldConditions as $index => $condition) {
 
-  // Now join with the chado_entity table to get published records only.
-  $cquery->join('chado_entity', 'CE', "CE.record_id = base.$pkey");
+  // Now join with the chado entity table to get published records only.
+  $chado_entity_table = tripal_chado_get_bundle_entity_table($bundle);
+  $cquery->join($chado_entity_table, 'CE', "CE.record_id = base.$pkey");
   $cquery->join('tripal_entity', 'TE', "CE.entity_id = TE.id");
   $cquery->fields('CE', array('entity_id'));
   $cquery->fields('TE', array('bundle'));
-  $cquery->condition('CE.data_table', $data_table);
   if (array_key_exists('start', $query->range)) {
     $cquery->range($query->range['start'], $query->range['length']);
   }

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

@@ -1261,7 +1261,7 @@ function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bund
       'field_name' =>  $field_name,
       'entity_type' => $entity_type,
       'bundle' => $bundle->name,
-      'label' => 'Database Cross Reference',
+      'label' => 'Cross Reference',
       'description' => 'The IDs where this record may be available in other external online databases.',
       'required' => FALSE,
       'settings' => array(

+ 7 - 0
tripal_chado/includes/tripal_chado.semweb.inc

@@ -316,6 +316,13 @@ function tripal_chado_populate_vocab_EDAM() {
     'cv_name' => 'EDAM',
     'definition' => 'The name of a biological or bioinformatics database.',
   ));
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'data:1048',
+    'name' => 'Database ID',
+    'cv_name' => 'EDAM',
+    'definition' => 'An identifier of a biological or bioinformatics database.',
+  ));
   tripal_associate_chado_semweb_term('db', 'name', $term);
 
   $term = tripal_insert_cvterm(array(

+ 51 - 1
tripal_chado/tripal_chado.install

@@ -729,7 +729,7 @@ function tripal_chado_chado_cvterm_mapping_schema() {
 }
 
 /**
- * Fixes the phase on the tripal_gffcds_temp table used for importing GFF files.
+ * Fixes the phase on the tripal_gffcds_temp table used for importing GFF files, and fixes the db.name term mapping.
  *
  */
 function tripal_chado_update_7200() {
@@ -737,9 +737,59 @@ function tripal_chado_update_7200() {
     if (chado_table_exists('tripal_gffcds_temp')) {
       chado_query("ALTER TABLE {tripal_gffcds_temp} ALTER COLUMN phase DROP NOT NULL;");
     }
+
+    $term = tripal_insert_cvterm(array(
+      'id' => 'data:1048',
+      'name' => 'Database ID',
+      'cv_name' => 'EDAM',
+      'definition' => 'An identifier of a biological or bioinformatics database.',
+    ));
+    tripal_associate_chado_semweb_term('db', 'name', $term);
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Could not fix phase on tripal_gffcds_temp table: '. $error);
   }
+}
+
+/**
+ * Divides chado_entity table for better integration with views.
+ */
+function tripal_chado_update_7201() {
+
+  module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.bundle');
+  try {
+    $transaction = db_transaction();
+    $query = db_select('chado_bundle', 'CB');
+    $query->join('tripal_bundle', 'TB', 'TB.id = CB.bundle_id');
+    $query->fields('CB', array('data_table'));
+    $query->fields('TB', array('name'));
+    $cbundles = $query->execute();
+
+    // If the table for the bundle doesn't exist then create one, and then
+    // move all of the records from the chado_entity table to it.
+    while($cbundle = $cbundles->fetchObject()) {
+      $cbundle_table = tripal_chado_get_bundle_entity_table($cbundle);
+
+      if (!db_table_exists($cbundle_table)) {
+        // Create the bundle table.
+        tripal_chado_create_bundle_table($cbundle);
+
+        // Now move the records over.
+        $sql = "
+          INSERT INTO {$cbundle_table}
+           SELECT CE.entity_id, CE.record_id, CE.nid
+            FROM {chado_entity} CE
+              INNER JOIN {tripal_entity} TE ON CE.entity_id = TE.id
+            WHERE TE.bundle = :bundle
+        ";
+        db_query($sql, array(':bundle' => $cbundle->name));
+      }
+    }
+  }
+  catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
 }

+ 1 - 0
tripal_chado/tripal_chado.module

@@ -6,6 +6,7 @@
 
 // Generic Chado API functions
 require_once "api/tripal_chado.api.inc";
+require_once "api/tripal_chado.entity.api.inc";
 require_once 'api/tripal_chado.property.api.inc';
 require_once 'api/tripal_chado.query.api.inc';
 require_once 'api/tripal_chado.variables.api.inc';

+ 2 - 2
tripal_chado/tripal_chado.views.inc

@@ -5,8 +5,8 @@
  */
 
 /**
- * Describe various Tripal Core systems to Views
- *   for the creation of administrative views.
+ * Describe various Tripal Core systems to Views for the creation of
+ * administrative views.
  *
  * @ingroup tripal
  */

+ 7 - 7
tripal_chado/views_handlers/chado_views_handler_filter.inc

@@ -27,7 +27,7 @@ class chado_views_handler_filter_boolean extends views_handler_filter_boolean_op
    */
   function query() {
 
-    // Adds joins to chado_entity and the chado table this field is from.
+    // Adds joins to chado entity and the chado table this field is from.
     $alias = _chado_views_add_table_joins($this);
 
     // Booleans in chado are stored t/f.
@@ -48,7 +48,7 @@ class chado_views_handler_filter_date extends views_handler_filter_date {
    */
   function query() {
 
-    // Adds joins to chado_entity and the chado table this field is from.
+    // Adds joins to chado entity and the chado table this field is from.
     $alias = _chado_views_add_table_joins($this);
 
     // Then allow the parent handler to add the where.
@@ -99,7 +99,7 @@ class chado_views_handler_filter_fk extends views_handler_filter {
    */
   function query() {
 
-    // Adds joins to chado_entity and the chado table this field is from.
+    // Adds joins to chado entity and the chado table this field is from.
     $alias = _chado_views_add_table_joins($this);
 
     // We need to do a quick fix for multiple values selected.
@@ -391,7 +391,7 @@ class chado_views_handler_filter_string extends views_handler_filter_string {
 
   function query() {
 
-    // Adds joins to chado_entity and the chado table this field is from.
+    // Adds joins to chado entity and the chado table this field is from.
     $alias = _chado_views_add_table_joins($this);
 
     // Finally add the restriction on the chado table including the value entered by the filter.
@@ -414,16 +414,16 @@ class chado_views_handler_filter_string extends views_handler_filter_string {
  */
 function _chado_views_add_table_joins(&$handler) {
 
-  // First we need to join to the chado_entity table where the link between an
+  // First we need to join to the chado entity table where the link between an
   // entity and it's chado record is stored.
   $join = new views_join();
   $join->construct('chado_entity', $handler->table, 'id', 'chado_entity_id');
   $alias = $handler->query->add_relationship('chado_entity', $join, $handler->table_alias, $handler->relationship);
 
-  // Restrict the chado_entity join to only return the table this field is from.
+  // Restrict the chado entity join to only return the table this field is from.
   $handler->query->add_where(0, $alias . '.data_table', $handler->definition['chado_table'], '=');
 
-  // Now, we need to join from chado_entity to the chado table needed by this field.
+  // Now, we need to join from chado entity to the chado table needed by this field.
   // This only works if the field is from the base table.
   // @todo: fix handler to work with non-base tables.
   $join = new views_join();