Browse Source

Fixed errors in tables and corrected populate function

Stephen Ficklin 9 năm trước cách đây
mục cha
commit
907e1ad74b

+ 112 - 0
tripal_entities/includes/tripal_entities.admin.inc

@@ -0,0 +1,112 @@
+<?php
+
+/**
+ * This function populates the Tripal entity tables using existing
+ * data in the database.
+ */
+function tripal_entities_populate_entity_tables() {
+  // Get the cvterm table and look for all of the tables that link to it.
+  $schema = chado_get_schema('cvterm');
+  $referring = $schema['referring_tables'];
+
+  // Perform this action in a transaction
+  $transaction = db_transaction();
+  print "\nNOTE: Populating of tripal entity tables is performed using a database transaction. \n" .
+    "If the load fails or is terminated prematurely then the entire set of \n" .
+    "insertions/updates is rolled back and will not be found in the database\n\n";
+  try {
+
+    // Iterate through the referring tables to see what records are there.
+    foreach ($referring as $tablename) {
+
+      // We only want to look at base tables.
+      if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
+          $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
+          $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
+          preg_match('/_cvterm$/', $tablename)) {
+        continue;
+      }
+
+      print "Examining $tablename...\n";
+      $ref_schema = chado_get_schema($tablename);
+      $fkeys = $ref_schema['foreign keys'];
+      foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
+
+        // Get the list of cvterm_ids from existing records in the table.
+        $sql = "
+          SELECT $local_id
+          FROM { " . $tablename . "}
+          GROUP BY $local_id
+        ";
+        $results = chado_query($sql);
+        while ($cvterm_id = $results->fetchField()) {
+
+          // Get the CV term details and add it to the tripal_entity_type table if
+          // it doesn't already exist.
+          $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
+
+          // First add a record to the tripal_entity_type table.
+          $values = array(
+            'cv_id' => $cvterm->cv_id->cv_id,
+            'db_id' => $cvterm->dbxref_id->db_id->db_id,
+            'publish' => 0,
+          );
+          $entity_type_id = 0;
+          $entity_type = chado_select_record('tripal_entity_type', array('entity_type_id'), $values);
+          if (count($entity_type) == 0) {
+            $entity_type = chado_insert_record('tripal_entity_type', $values);
+            $entity_type_id = $entity_type['entity_type_id'];
+          }
+          else {
+            $entity_type_id = $entity_type[0]->entity_type_id;
+          }
+
+          // Next add a record to the tripal_entity_bundle table.
+          $values = array(
+            'entity_type_id' => $entity_type_id,
+            'cvterm_id' => $cvterm_id,
+            'publish' => 0
+          );
+          $bundle_id = 0;
+          $bundle = chado_select_record('tripal_entity_bundle', array('bundle_id'), $values);
+          if (count($bundle) == 0) {
+            $bundle = chado_insert_record('tripal_entity_bundle', $values);
+            $bundle_id = $bundle['bundle_id'];
+          }
+          else {
+            $bundle_id = $bundle[0]->bundle_id;
+          }
+
+          // Add the table where the records are found.
+          $values = array(
+            'bundle_id' => $bundle_id,
+            'data_table' => $tablename,
+            'type_table' => $tablename,
+            'field' => $local_id
+          );
+          if (!chado_select_record('tripal_entity_bundle_source', array('bundle_source_id'), $values, array('has_record' => TRUE))) {
+            chado_insert_record('tripal_entity_bundle_source', $values);
+          }
+
+          // Add the table where the records are found.
+          $values = array(
+            'entity_type_id' => $entity_type_id,
+            'data_table' => $tablename,
+            'type_table' => $tablename,
+            'field' => $local_id
+          );
+          if (!chado_select_record('tripal_entity_type_source', array('entity_type_id'), $values, array('has_record' => TRUE))) {
+            chado_insert_record('tripal_entity_type_source', $values);
+          }
+        }
+      }
+    }
+  }
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    $transaction->rollback();
+    watchdog_exception('tripal_ws', $e);
+    print "FAILED: Rolling back database changes...\n";
+  }
+  print "\nDone.\n";
+}

+ 16 - 25
tripal_entities/tripal_entities.install

@@ -18,6 +18,9 @@ function tripal_entities_install() {
   // add any custom tables. For this case we will add an 'file' table to the
   // chado schema
   tripal_entities_add_custom_tables();
+
+  drupal_set_message('The current Entity Example (Genes) requires the Sequence Ontology.', 'warning');
+
 }
 
 /**
@@ -38,7 +41,7 @@ function tripal_entities_add_tripal_entity_type_table() {
   $schema = array (
     'table' => 'tripal_entity_type',
     'fields' => array (
-      'vocab_id' => array(
+      'entity_type_id' => array(
         'type' => 'serial',
         'not null' => TRUE
       ),
@@ -57,7 +60,7 @@ function tripal_entities_add_tripal_entity_type_table() {
       ),
     ),
     'primary key' => array (
-      0 => 'vocab_id'
+      0 => 'entity_type_id'
     ),
     'foreign keys' => array (
       'cv' => array (
@@ -74,7 +77,7 @@ function tripal_entities_add_tripal_entity_type_table() {
       ),
     ),
     'unique keys' => array (
-      'vocab_cvdb' => array (
+      'tripal_entity_type_cvdb' => array (
         'cv_id', 'db_id'
       ),
     ),
@@ -96,7 +99,7 @@ function tripal_entities_add_tripal_entity_bundle_table() {
         'type' => 'serial',
         'not null' => TRUE
       ),
-      'vocab_id' => array (
+      'entity_type_id' => array (
         'type' => 'int',
         'not null' => TRUE
       ),
@@ -123,17 +126,17 @@ function tripal_entities_add_tripal_entity_bundle_table() {
       'tripal_entity_type' => array (
         'table' => 'tripal_entity_type',
         'columns' => array (
-          'vocab_id' => 'vocab_id'
+          'entity_type_id' => 'entity_type_id'
         )
       ),
     ),
     'unique keys' => array (
       'tripal_entity_bundle_unq' => array (
-        'vocab_id', 'cvterm_id'
+        'entity_type_id', 'cvterm_id'
       ),
     ),
     'indexes' => array(
-      'tripal_entity_bundle_vocab_id' => array('vocab_id'),
+      'tripal_entity_bundle_entity_type_id' => array('entity_type_id'),
       'tripal_entity_bundle_cvterm_id' => array('cvterm_id'),
     ),
   );
@@ -146,11 +149,11 @@ function tripal_entities_add_tripal_entity_type_source_table(){
   $schema = array (
     'table' => 'tripal_entity_type_source',
     'fields' => array (
-      'vocab_table_id' => array(
+      'entity_type_source_id' => array(
         'type' => 'serial',
         'not null' => TRUE
       ),
-      'vocab_id' => array (
+      'entity_type_id' => array (
         'type' => 'int',
         'not null' => TRUE
       ),
@@ -171,23 +174,23 @@ function tripal_entities_add_tripal_entity_type_source_table(){
       ),
     ),
     'primary key' => array (
-      0 => 'vocab_table_id'
+      0 => 'entity_type_source_id'
     ),
     'foreign keys' => array (
       'tripal_entity_type' => array (
         'table' => 'tripal_entity_type',
         'columns' => array (
-          'vocab_id' => 'vocab_id'
+          'entity_type_id' => 'entity_type_id'
         ),
       ),
     ),
     'unique keys' => array (
       'tripal_entity_type_ridbase' => array (
-        'vocab_id', 'data_table'
+        'entity_type_id', 'data_table'
       ),
     ),
     'indexes' => array(
-      'tripal_entity_type_vocab_id' => array('vocab_id'),
+      'tripal_entity_type_entity_type_id' => array('entity_type_id'),
       'tripal_entity_type_data_table' => array('data_table'),
       'tripal_entity_type_type_table' => array('type_table'),
     ),
@@ -434,18 +437,6 @@ function tripal_entities_schema() {
   return $schema;
 }
 
-/**
- * Implements hook_install().
- *
- * The current implementation assumes we are using the sequence ontology so we should
- * warn people trying this out that they need to first load the sequence ontology.
- */
-function tripal_entities_install() {
-  
-  drupal_set_message('The current Entity Example (Genes) requires the Sequence Ontology.', 'warning');
-  
-}
-
 /**
  * Implements hook_uninstall().
  *

+ 1 - 0
tripal_entities/tripal_entities.module

@@ -1,6 +1,7 @@
 <?php
 
 require_once "api/tripal_entities.api.inc";
+require_once "includes/tripal_entities.admin.inc";
 require_once "includes/tripal_entities.field_storage.inc";
 require_once "includes/tripal_entities.fields.inc";
 require_once "includes/TripalData.inc";