Browse Source

Added a chado base table dropdown so when adding a Tripal Content Type, a default table can be selected

Chun-Huai Cheng 8 years ago
parent
commit
563e9efe75

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

@@ -1868,4 +1868,34 @@ function tripal_get_schema_name($schema = 'chado') {
   drupal_alter('tripal_get_schema_name', $schema_name, $context);
 
   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;
 }

+ 54 - 28
tripal_chado/includes/tripal_chado.mapping.inc

@@ -5,9 +5,8 @@
  * data in the database.
  */
 function tripal_chado_map_cvterms() {
-  // Get the cvterm table and look for all of the tables that link to it.
-  $schema = chado_get_schema('cvterm');
-  $referring = $schema['referring_tables'];
+  // Get chado base tables
+  $base_tables = chado_get_base_tables();
 
   // Perform this action in a transaction
   $transaction = db_transaction();
@@ -17,19 +16,13 @@ function tripal_chado_map_cvterms() {
   try {
 
     // Iterate through the referring tables to see what records are there.
-    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;
-      }
-
+    foreach ($base_tables as $tablename) {
       print "Examining $tablename...\n";
       $ref_schema = chado_get_schema($tablename);
       $fkeys = $ref_schema['foreign keys'];
+      if (!isset($fkeys['cvterm']['columns'])) {
+        continue;
+      }
       foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
 
         // Get the list of cvterm_ids from existing records in the table.
@@ -40,21 +33,7 @@ function tripal_chado_map_cvterms() {
         ";
         $results = chado_query($sql);
         while ($cvterm_id = $results->fetchField()) {
-
-          // Get the CV term details and add it to the tripal_vocabulary table if
-          // it doesn't already exist.
-          $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
-
-          // TODO insert records into the tripal_cvterm_mapping table.
-          db_insert('tripal_cvterm_mapping')
-            ->fields(
-              array(
-                'cvterm_id' => $cvterm->cvterm_id,
-                'chado_table' => $tablename,
-                'chado_field' => $local_id
-              )
-            )
-            ->execute();
+          tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $local_id);
         }
       }
     }
@@ -66,4 +45,51 @@ function tripal_chado_map_cvterms() {
     print "FAILED: Rolling back database changes...\n";
   }
   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 
+ * table
+ */
+function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
+  // check if the record exists
+  $record = db_select('tripal_cvterm_mapping', 'tcm')
+  ->fields('tcm', array('mapping_id'))
+  ->condition('cvterm_id', $cvterm_id)
+  ->condition('chado_table', $tablename)
+  ->condition('chado_field', $chado_field)
+  ->execute()
+  ->fetchField();
+  
+  // insert records into the tripal_cvterm_mapping table.
+  if (!$record) {
+    db_insert('tripal_cvterm_mapping')
+    ->fields(
+        array(
+          'cvterm_id' => $cvterm_id,
+          'chado_table' => $tablename,
+          'chado_field' => $chado_field
+        )
+        )
+        ->execute();
+  }
 }

+ 31 - 1
tripal_chado/includes/tripal_chado.vocab_storage.inc

@@ -145,6 +145,23 @@ function tripal_chado_vocab_select_term_form($form, &$form_state) {
              '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '.  ' .
              '<br><b>Definition:</b>  ' . $term->definition,
        );
+       $base_tables = chado_get_base_tables();
+       $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;
+       }
+       $form['terms_list']['default_table-' . $term->cvterm_id] = array(
+         '#type' => 'select',
+         '#title' => 'Default table',
+         '#options' => $options,
+         '#default_value' => $default_table
+       );
        $num_terms++;
     }
     if ($num_terms == 0) {
@@ -205,6 +222,19 @@ function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
       $form_state['storage']['vocabulary'] = $term->dbxref_id->db_id->name;
       $form_state['storage']['accession'] = $term->dbxref_id->accession;
       $form_state['storage']['term_name'] = $term->name;
+      // Make sure a default table is selected
+      $default_table = $form_state['values']['default_table-' . $cvterm_id];
+      if (!$default_table) {
+        form_set_error('default_table-' . $cvterm_id, 'Please select a default table.');
+      }
+      else {
+        $schema = chado_get_schema($default_table);
+        if (isset($schema['foreign keys']['cvterm']['columns'])) {
+          $fkey = array_keys($schema['foreign keys']['cvterm']['columns']);
+          $chado_field = $fkey[0];
+          tripal_chado_add_cvterm_mapping($cvterm_id, $default_table, $chado_field);
+        }
+      }
     }
   }
   // For any other button click it's an AJAX call and we just want to reubild
@@ -212,4 +242,4 @@ function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
   else {
     $form_state['rebuild'] = TRUE;
   }
-}
+}

+ 1 - 0
tripal_chado/tripal_chado.module

@@ -38,6 +38,7 @@ require_once "includes/tripal_chado.schema.inc";
 require_once "includes/tripal_chado.vocab_storage.inc";
 require_once "includes/tripal_chado.field_storage.inc";
 require_once "includes/tripal_chado.fields.inc";
+require_once "includes/tripal_chado.mapping.inc";
 
 tripal_chado_set_globals();