Browse Source

Adjusted entity creation to use the tripal_cvterm_mapping table

Stephen Ficklin 8 years ago
parent
commit
8af937f449

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

@@ -72,11 +72,17 @@ function tripal_chado_publish_records($values, $job_id = NULL) {
   // Load the bundle entity so we can get information about which Chado
   // table/field this entity belongs to.
   $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
-
   $bundle_id = $bundle->id;
-  $table = tripal_get_bundle_variable('chado_table', $bundle_id);
-  $column = tripal_get_bundle_variable('chado_type_column', $bundle_id);
-  $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
+  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+  $mapped_table = chado_get_cvterm_mapping($params);
+  $table = $mapped_table->chado_table;
+  $column = $mapped_table->chado_field;
+  $cvterm_id = $mapped_table->cvterm->cvterm_id;
 
   // Get the table information for the Chado table.
   $table_schema = chado_get_schema($table);

+ 0 - 29
tripal_chado/api/tripal_chado.query.api.inc

@@ -1870,32 +1870,3 @@ function tripal_get_schema_name($schema = 'chado') {
   return $schema_name;
 }
 
-/**
- *  Returns all chado base tables
- *  
- *   Thie function returns all tables that are referred to by the cvterm table. In addition,
- *   organism, project, and analysis tables are considered base tables and also returned
- */
-function chado_get_base_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'];
-  
-  $base_tables = array('organism', 'project', 'analysis');
-  
-  foreach ($referring as $tablename) {
-  
-    // Ignore the cvterm tables, relationships, chadoprop 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;
-    }
-    else {
-      array_push($base_tables, $tablename);
-    }
-  }
-  sort($base_tables);
-  return $base_tables;
-}

+ 121 - 0
tripal_chado/api/tripal_chado.schema.api.inc

@@ -505,4 +505,125 @@ function chado_get_custom_table_schema($table) {
   else {
     return unserialize($custom->schema);
   }
+}
+
+/**
+ *  Returns all chado base tables.
+ *
+ *  Base tables are those that contain the primary record for a data type. For
+ *  example, feature, organism, stock, are all base tables.  Other tables
+ *  include linker tables (which link two or more base tables), property tables,
+ *  and relationship tables.  These provide additional information about
+ *  primary data records and are therefore not base tables.  This function
+ *  retreives only the list of tables that are considered 'base' tables.
+ *
+ *  @return
+ *    An array of base table names.
+ *
+ *  @ingroup tripal_chado_schema_api
+ */
+function chado_get_base_tables() {
+
+  // Initialize the base tables with those tables that are missing a type.
+  // Ideally they should have a type, but that's for a future version of Chado.
+  $base_tables = array('organism', 'project', 'analysis', 'biomaterial');
+
+  // We'll use the cvterm table to guide which tables are base tables. Typically
+  // base tables (with a few exceptions) all have a type.  Iterate through the
+  // referring tables.
+  $schema = chado_get_schema('cvterm');
+  $referring = $schema['referring_tables'];
+  foreach ($referring as $tablename) {
+
+    // Ignore the cvterm tables, relationships, chadoprop tables.
+    if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
+        $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
+        $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
+        preg_match('/_cvterm$/', $tablename) ||
+        // Ignore prop tables
+        preg_match('/prop$/', $tablename) || preg_match('/prop_.+$/', $tablename) ||
+        // Ignore nd_tables
+        preg_match('/^nd_/', $tablename)) {
+      continue;
+    }
+    else {
+      array_push($base_tables, $tablename);
+    }
+  }
+
+  // Remove any linker tables that have snuck in.  Linker tables are those
+  // whose foreign key constraints link to two or more base table.
+  $final_list = array();
+  foreach ($base_tables as $i => $tablename) {
+    $num_links = 0;
+    $schema = chado_get_schema($tablename);
+    $fkeys = $schema['foreign keys'];
+    foreach ($fkeys as $fkid => $details) {
+      $fktable = $details['table'];
+      if (in_array($fktable, $base_tables)) {
+        $num_links++;
+      }
+    }
+    if ($num_links < 2) {
+      $final_list[] = $tablename;
+    }
+  }
+
+  // Sort the tables and return the list.
+  sort($final_list);
+  return $final_list;
+}
+
+/**
+ * Get information about which Chado base table a cvterm is mapped to.
+ *
+ * Vocbulary terms that represent content types in Tripal must be mapped to
+ * Chado tables.  A cvterm can only be mapped to one base table in Chado.
+ * This function will return an object that contains the chado table and
+ * foreign key field to which the cvterm is mapped.  The 'chado_table' property
+ * of the returned object contains the name of the table, and the 'chado_field'
+ * property contains the name of the foreign key field (e.g. type_id), and the
+ * 'cvterm' property contains a cvterm object.
+ *
+ * @params
+ *   An associative array that contains the following keys:
+ *     - cvterm_id:  the cvterm ID value for the term.
+ *     - vocabulary: the short name for the vocabulary (e.g. SO, GO, PATO)
+ *     - accession:  the accession for the term.
+ *     - bundle_id:  the ID for the bundle to which a term is associated.
+ *   The 'vocabulary' and 'accession' must be used together, the 'cvterm_id' can
+ *   be used on it's own.
+ * @return
+ *   An object containing the chado_table and chado_field properties or NULL if
+ *   if no mapping was found for the term.
+ */
+function chado_get_cvterm_mapping($params) {
+  $cvterm_id = array_key_exists('cvterm_id', $params) ? $params['cvterm_id'] : NULL;
+  $vocabulary = array_key_exists('vocabulary', $params) ? $params['vocabulary'] : NULL;
+  $accession = array_key_exists('accession', $params) ? $params['accession'] : NULL;
+
+  if ($cvterm_id) {
+    $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
+  }
+  else if ($vocabulary and $accession) {
+    $match = array(
+      'dbxref_id' => array(
+        'db_id' => array(
+          'name' => $vocabulary,
+        ),
+        'accession' => $accession,
+      ),
+    );
+    $cvterm = chado_generate_var('cvterm', $match);
+  }
+
+  $result = db_select('tripal_cvterm_mapping', 'tcm')
+    ->fields('tcm')
+    ->condition('cvterm_id', $cvterm->cvterm_id)
+    ->execute();
+  $result = $result->fetchObject();
+  if ($result) {
+    $result->cvterm = $cvterm;
+  }
+  return $result;
 }

+ 39 - 97
tripal_chado/includes/tripal_chado.entity.inc

@@ -4,103 +4,17 @@
  * Implements hook_chado_bundle_create().
  *
  * This is a Tripal hook. It allows any module to perform tasks after
- * a bundle has been craeted.
+ * a bundle has been created.
  *
  * @param $bundle
  *  The TripalBundle object.
  */
 
 function tripal_chado_bundle_create($bundle) {
-  tripal_chado_set_bundle_vars($bundle);
-}
 
-/**
- * Associates the Chado table/column with the bundle.
- *
- * @param $bundle
- *   The TripalBundle object.
- */
-function tripal_chado_set_bundle_vars($bundle) {
-  // The details array is used to pass to the TripalEntity->create_info()
-  // function to provide more details about how the bundle is used by this
-  // module.
-  $details = array();
-
-  // Get the term.
-  $term = entity_load('TripalTerm', array($bundle->term_id));
-  $term = reset($term);
-
-  // Get the cvterm that corresponds to this TripalTerm object.
-  $vocab = entity_load('TripalVocab', array($term->vocab_id));
-  $vocab = reset($vocab);
-  $match = array(
-    'dbxref_id' => array(
-      'db_id' => array(
-        'name' => $vocab->vocabulary,
-      ),
-      'accession' => $term->accession
-    ),
-  );
-  $cvterm = chado_generate_var('cvterm', $match);
-
-  // TODO: these cvterm names should not be hardcoded. A better solution
-  // should be found.
-  // The organism table does not have a type_id so we won't ever find
-  // a record for it in the tripal_cv_defaults table.
-  if ($cvterm->name == 'organism') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'organism',
-      'chado_type_table' => 'organism',
-      'chado_type_column' =>  '',
-    );
-  }
-  // The analysis table does not have a type_id so we won't ever find
-  // a record for it in the tripalcv_defaults table.
-  else if ($cvterm->name == 'analysis') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'analysis',
-      'chado_type_table' => 'analysis',
-      'chado_type_column' =>  '',
-    );
-  }
-  else if ($cvterm->name == 'project') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'project',
-      'chado_type_table' => 'project',
-      'chado_type_column' =>  '',
-    );
-  }
-  else {
-    // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
-    // TABLES.
-    // Look to see if this vocabulary is used as a default for any table.
-    $default = tripal_get_default_cv_table($cvterm->cv_id->cv_id);
-    if ($default) {
-      $details = array(
-        'chado_cv_id' => $cvterm->cv_id->cv_id,
-        'chado_cvterm_id' => $cvterm->cvterm_id,
-        'chado_table' => $default->table_name,
-        'chado_type_table' => $default->table_name,
-        'chado_type_column' =>  $default->field_name,
-      );
-    }
-  }
-
-  // Save the mapping information so that we can reuse it when we need to
-  // look things up for later (such as the hook_create_instance_info() function.
-  tripal_set_bundle_variable('chado_cv_id', $bundle->id, $details['chado_cv_id']);
-  tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $details['chado_cvterm_id']);
-  tripal_set_bundle_variable('chado_table', $bundle->id, $details['chado_table']);
-  tripal_set_bundle_variable('chado_type_table', $bundle->id, $details['chado_type_table']);
-  tripal_set_bundle_variable('chado_type_column', $bundle->id, $details['chado_type_column']);
 }
 
+
 /**
  * Implements hook_entity_create().
  *
@@ -117,8 +31,15 @@ function tripal_chado_entity_create(&$entity, $type) {
 
       // Add in the Chado table information for this entity type.
       $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
-      $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
-      $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
+      $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+      $vocab = $term->vocab;
+      $params = array(
+        'vocabulary' => $vocab->vocabulary,
+        'accession' => $term->accession,
+      );
+      $mapped_table = chado_get_cvterm_mapping($params);
+      $chado_table = $mapped_table->chado_table;
+      $chado_column = $mapped_table->chado_field;
       if ($chado_table) {
         $entity->chado_table = $chado_table;
         $entity->chado_column = $chado_column;
@@ -165,8 +86,15 @@ function tripal_chado_entity_load($entities, $type) {
         if (!$bundle) {
           continue;
         }
-        $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
-        $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
+        $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+        $vocab = $term->vocab;
+        $params = array(
+          'vocabulary' => $vocab->vocabulary,
+          'accession' => $term->accession,
+        );
+        $mapped_table = chado_get_cvterm_mapping($params);
+        $chado_table = $mapped_table->chado_table;
+        $chado_column = $mapped_table->chado_field;
         if ($chado_table) {
           $entity->chado_table = $chado_table;
           $entity->chado_column = $chado_column;
@@ -265,9 +193,16 @@ function tripal_chado_tripal_default_title_format($entity, $available_tokens) {
   // Load the bundle
   $bundle = tripal_load_bundle_entity(array('term_id' => $entity->term_id));
   $bundle_id = $bundle->id;
-  $table = tripal_get_bundle_variable('chado_table', $bundle_id);
-  $column = tripal_get_bundle_variable('chado_column', $bundle_id);
-  $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
+  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+  $mapped_table = chado_get_cvterm_mapping($params);
+  $table = $mapped_table->chado_table;
+  $column = $mapped_table->chado_field;
+  $cvterm_id = $mapped_table->cvterm->cvterm_id;
 
   // For organism titles  we want the genus and species with no comma separation.
   if ($table == 'organism') {
@@ -377,8 +312,15 @@ function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
 
       // Get the Chado table for this data type.
       $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
-      $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
-      $chado_field = tripal_get_bundle_variable('chado_column', $bundle->id);
+      $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+      $vocab = $term->vocab;
+      $params = array(
+        'vocabulary' => $vocab->vocabulary,
+        'accession' => $term->accession,
+      );
+      $mapped_table = chado_get_cvterm_mapping($params);
+      $chado_table = $mapped_table->chado_table;
+      $chado_field = $mapped_table->chado_field;
 
       // Get the list of templates that should be used for entities and generatte
       // the key in the array for this entity type (using the chado table the

+ 27 - 10
tripal_chado/includes/tripal_chado.fields.inc

@@ -33,13 +33,22 @@ function tripal_chado_field_info() {
  */
 function tripal_chado_field_create_info($entity_type, $bundle) {
 
+  // Get the table this bundle is mapped to.
+  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+  $mapped_table = chado_get_cvterm_mapping($params);
+
   // Get the details about the mapping of this bundle to the Chado table:
   $details = array(
-    'chado_cv_id' => tripal_get_bundle_variable('chado_cv_id', $bundle->id),
-    'chado_cvterm_id' => tripal_get_bundle_variable('chado_cvterm_id', $bundle->id),
-    'chado_table' => tripal_get_bundle_variable('chado_table', $bundle->id),
-    'chado_type_table' => tripal_get_bundle_variable('chado_type_table', $bundle->id),
-    'chado_type_column' => tripal_get_bundle_variable('chado_type_column', $bundle->id),
+    'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
+    'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
+    'chado_table' => $mapped_table->chado_table,
+    'chado_type_table' => $mapped_table->chado_table,
+    'chado_type_column' => $mapped_table->chado_field,
   );
 
   $base_fields = tripal_chado_field_create_base('create_info', $entity_type, $bundle, $details);
@@ -262,13 +271,21 @@ function tripal_chado_field_create_info_base_defaults($field_name, $table_name,
  */
 function tripal_chado_field_create_instance_info($entity_type, $bundle) {
 
+  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+  $mapped_table = chado_get_cvterm_mapping($params);
+
   // Get the details about the mapping of this bundle to the Chado table:
   $details = array(
-    'chado_cv_id' => tripal_get_bundle_variable('chado_cv_id', $bundle->id),
-    'chado_cvterm_id' => tripal_get_bundle_variable('chado_cvterm_id', $bundle->id),
-    'chado_table' => tripal_get_bundle_variable('chado_table', $bundle->id),
-    'chado_type_table' => tripal_get_bundle_variable('chado_type_table', $bundle->id),
-    'chado_type_column' => tripal_get_bundle_variable('chado_type_column', $bundle->id),
+    'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
+    'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
+    'chado_table' => $mapped_table->chado_table,
+    'chado_type_table' => $mapped_table->chado_table,
+    'chado_type_column' => $mapped_table->chado_field,
   );
 
   $base_fields = tripal_chado_field_create_base('create_instance_info', $entity_type, $bundle, $details);

+ 3 - 20
tripal_chado/includes/tripal_chado.mapping.inc

@@ -47,27 +47,10 @@ function tripal_chado_map_cvterms() {
   print "\nDone.\n";
 }
 
-/*
- * Return mapped tables for a cvterm
- * 
- * Query the tripal_cvterm_mapping table and return tables used by a cvterm
- */
-function tripal_chado_get_cvterm_mapped_tables($cvterm_id) {
-  $tables = array();
-  $results = db_select('tripal_cvterm_mapping', 'tcm')
-    ->fields('tcm')
-    ->condition('cvterm_id', $cvterm_id)
-    ->execute();
-  while($table = $results->fetchObject()) {
-    array_push($tables, $table);
-  }
-  return $tables;
-}
-
 /*
  * Add a cvterm mapping record
- * 
- * Check if the cvterm mapping record exists. If not, add it to the tripal_cvterm_mapping 
+ *
+ * Check if the cvterm mapping record exists. If not, add it to the tripal_cvterm_mapping
  * table
  */
 function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
@@ -79,7 +62,7 @@ function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
   ->condition('chado_field', $chado_field)
   ->execute()
   ->fetchField();
-  
+
   // insert records into the tripal_cvterm_mapping table.
   if (!$record) {
     db_insert('tripal_cvterm_mapping')

+ 7 - 6
tripal_chado/includes/tripal_chado.vocab_storage.inc

@@ -146,20 +146,21 @@ function tripal_chado_vocab_select_term_form($form, &$form_state) {
              '<br><b>Definition:</b>  ' . $term->definition,
        );
        $base_tables = chado_get_base_tables();
+       $default_table = 0;
        $options = array(0 => '-- Select table --');
        foreach ($base_tables AS $tablename) {
          $options[$tablename] = $tablename;
        }
-       $mapped_tables = tripal_chado_get_cvterm_mapped_tables($term->cvterm_id);
-       $default_table = 0;
-       if (count($mapped_tables) > 0) {
-         $first_table = $mapped_tables[0];
-         $default_table = $first_table->chado_table;
+       $mapped_table = chado_get_cvterm_mapping(array('cvterm_id' => $term->cvterm_id));
+       if ($mapped_table) {
+         $default_table = $mapped_table->chado_table;
        }
        $form['terms_list']['default_table-' . $term->cvterm_id] = array(
          '#type' => 'select',
-         '#title' => 'Default table',
+         '#title' => 'Chado table',
          '#options' => $options,
+         '#description' => 'Select the table in Chado where data of this type
+           will be stored.',
          '#default_value' => $default_table
        );
        $num_terms++;

+ 9 - 1
tripal_chado/tripal_chado.module

@@ -652,7 +652,15 @@ function tripal_chado_theme($existing, $type, $theme, $path) {
     // map to tables that used had Tripal v2 nodes.  For those, if the
     // legacy support is turned on then we want to use the legacy template.
     while ($bundle = $bundles->fetchObject()) {
-      $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
+      $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+      $vocab = $term->vocab;
+      $params = array(
+        'vocabulary' => $vocab->vocabulary,
+        'accession' => $term->accession,
+      );
+      $mapped_table = chado_get_cvterm_mapping($params);
+
+      $chado_table = $mapped_table->chado_table;
       $legacy_template = 'legacy_template--chado_' . $chado_table;
       if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
         $themes['TripalEntity__' . $bundle->name] = array(