Browse Source

Some small bug fixes, plus working back in code for mapping all terms to tables where used

Stephen Ficklin 8 years ago
parent
commit
c760ae1122

+ 8 - 8
legacy/tripal_pub/tripal_pub.module

@@ -287,8 +287,8 @@ function tripal_pub_permission() {
  * @ingroup tripal_pub
  */
 function tripal_pub_mail($key, &$message, $params) {
+  $site_name = variable_get('site_name');
   $language = $message['language'];
-  $variables = user_mail_tokens($params['account'], $language);
   switch($key) {
     case 'import_report':
       $headers = array(
@@ -296,13 +296,13 @@ function tripal_pub_mail($key, &$message, $params) {
         'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
         'Content-Transfer-Encoding' => '8Bit',
         'X-Mailer' => 'Drupal'
-        );
-        foreach ($headers as $key => $value) {
-          $message['headers'][$key] = $value;
-        }
-        $message['subject'] = t('Publication import from !site', $variables, $language->language);
-        $message['body'][] = $params['message'];
-        break;
+      );
+      foreach ($headers as $key => $value) {
+        $message['headers'][$key] = $value;
+      }
+      $message['subject'] = t('Publication import from !site', array('!site' => $site_name));
+      $message['body'][] = $params['message'];
+      break;
   }
 }
 

+ 43 - 1
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -1155,7 +1155,19 @@ function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
 }
 
 /**
- * Retreives the default vocabulary for a given table and field
+ * Retreives the default vocabulary for a given table and field.
+ *
+ * Each table in Chado that has a 'type_id' (or foreign key constraint to
+ * the cvterm table) will have a default vocabulary assigned. This indicates to
+ * Tripal that terms in that vocabulary are used to set the type_id for that
+ * table. An example where this is used is the
+ * tripal_get_cvterm_select_options() function which generates a list of options
+ * for a select box used in a Drupal form.  The select box will list the terms
+ * from the default vocabulary in the drop down.
+ *
+ * This function uses the Chado table and field name (e.g. 'type_id') to
+ * retreive the vocabulary assgined.
+ *
  * @param $table
  *   The name of the table that contains a field with a foreign key
  *   relationship to the cvterm table
@@ -1178,6 +1190,36 @@ function tripal_get_default_cv($table, $field) {
   return tripal_get_cv(array('cv_id' => $cv_id));
 }
 
+/**
+ * Retrieves the Chado table to which a vocbulary is set as default.
+ *
+ * Each table in Chado that has a 'type_id' (or foreign key constraint to
+ * the cvterm table) will have a default vocabulary assigned. This indicates to
+ * Tripal that terms in that vocabulary are used to set the type_id for that
+ * table. An example where this is used is the
+ * tripal_get_cvterm_select_options() function which generates a list of options
+ * for a select box used in a Drupal form.  The select box will list the terms
+ * from the default vocabulary in the drop down.
+ *
+ * This function uses the vocabulary ID to get the Chado table to which it
+ * is assigned.
+ *
+ * @param $cv_id
+ *  The ID of the vocabulary.
+ *
+ * @return
+ *   If an assignment is present, an object containing the 'table_name' and
+ *   'field_name' is returned.
+ */
+function tripal_get_default_cv_table($cv_id) {
+  $default = db_select('tripal_cv_defaults', 't')
+    ->fields('t', array('table_name', 'field_name'))
+    ->condition('cv_id', $cv_id)
+    ->execute()
+    ->fetchObject();
+  return $default;
+}
+
 /**
  * Create an options array to be used in a form element
  * which provides a list of all chado cvterms. Unlike the

+ 3 - 5
tripal_chado/includes/tripal_chado.entity.inc

@@ -43,6 +43,8 @@ function tripal_chado_set_bundle_vars($bundle) {
   );
   $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') {
@@ -78,11 +80,7 @@ function tripal_chado_set_bundle_vars($bundle) {
     // 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 = db_select('tripal_cv_defaults', 't')
-      ->fields('t')
-      ->condition('cv_id', $cvterm->cv_id->cv_id)
-      ->execute()
-      ->fetchObject();
+    $default = tripal_get_default_cv_table($cvterm->cv_id->cv_id);
     if ($default) {
       $details = array(
         'chado_cv_id' => $cvterm->cv_id->cv_id,

+ 104 - 0
tripal_chado/includes/tripal_chado.mapping.inc

@@ -0,0 +1,104 @@
+<?php
+
+/**
+ *
+ */
+function tripal_chado_tripal_cvterm_mapping_schema() {
+
+  $schema = array (
+    'table' => 'tripal_cvterm_mapping',
+    'fields' => array (
+      'mapping_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE
+      ),
+      'cvterm_id' => array (
+        'type' => 'int',
+        'not null' => TRUE
+      ),
+      'chado_table' => array (
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE
+      ),
+      'chado_field' => array (
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE
+      ),
+    ),
+    'primary key' => array (
+      0 => 'mapping_id'
+    ),
+    'indexes' => array(
+      'tripal_cvterm2table_idx1' => array('cvterm_id'),
+      'tripal_cvterm2table_idx2' => array('chado_table'),
+      'tripal_cvterm2table_idx3' => array('chado_table', 'chado_field'),
+    ),
+  );
+  return $schema;
+}
+
+/**
+ * This function populates the Tripal entity tables using existing
+ * 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'];
+
+  // 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_vocabulary table if
+          // it doesn't already exist.
+          $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
+
+          $values = array(
+            'cvterm_id' => $cvterm->cvterm_id,
+            'chado_table' => $tablename,
+            'chado_field' => $remote_id
+          );
+          // TODO insert records into the tripal_cvterm_mapping table.
+        }
+      }
+    }
+  }
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    $transaction->rollback();
+    watchdog_exception('tripal_chado', $e);
+    print "FAILED: Rolling back database changes...\n";
+  }
+  print "\nDone.\n";
+}

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

@@ -472,8 +472,7 @@ function tripal_chado_semweb_form($form, &$form_state, $chado_table = NULL) {
   if ($chado_table) {
     $schema = chado_get_schema($chado_table);
     $pk = $schema['primary key'][0];
-    $cv_default =
-      db_select('tripal_cv_defaults', 'tc')
+    $cv_default = db_select('tripal_cv_defaults', 'tc')
       ->fields('tc', array('field_name'))
       ->condition('table_name', $chado_table)
       ->execute()