Browse Source

Fixing API function calls and related bugs

Stephen Ficklin 10 years ago
parent
commit
5a637e17a7

+ 4 - 2
tripal_core/api/get_FKs.php

@@ -37,8 +37,10 @@ if (isset($arguments['v'])) {
 }
 
 /**
- * This function does the actual work of determining the foreign key relationships from
- * the database and creating the schema file.
+ * Builds the FK relationships array in the database.
+ * 
+ * This function does the actual work of determining the foreign key 
+ * relationships from the database and creating the schema file.
  */
 function get_chado_fk_relationships($version) {
 

+ 33 - 4
tripal_core/api/tripal_core.DEPRECATED.inc

@@ -796,7 +796,7 @@ function tripal_job_set_progress($job_id, $percentage) {
  * Function was deprecated in Tripal 2.0 and will be removed 2 releases from now.
  * This function has been replaced by tripal_update_mview().
  *
- * @see tripal_update_mview().
+ * @see tripal_refresh_mview() or tripal_delete_mview().
  */
 function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
 
@@ -807,12 +807,16 @@ function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
       "DEPRECATED: %old_function has been replaced with %new_function. Please update your code.",
       array(
         '%old_function'=>'tripal_mviews_action',
-        '%new_function' => 'tripal_add_populate_mview'
+        '%new_function' => 'tripal_refresh_mview'
       )
     );
 
     //New API Function
-    return tripal_add_job_populate_mview($mview_id, $redirect);
+    $ret = tripal_refresh_mview($mview_id, $redirect);
+    if ($redirect) {
+      drupal_goto("admin/tripal/schema/mviews");
+    }
+    return $ret;
   }
   if ($op == 'delete') {
     tripal_report_error(
@@ -826,7 +830,11 @@ function tripal_mviews_action($op, $mview_id, $redirect = FALSE) {
     );
 
     //New API Function
-    return tripal_delete_mview($mview_id, $redirect);
+    $ret = tripal_delete_mview($mview_id, $redirect);
+    if ($redirect) {
+      drupal_goto("admin/tripal/schema/mviews");
+    }
+    return $ret;
   }
 }
 
@@ -871,6 +879,27 @@ function tripal_update_mview($mview_id) {
   return tripal_populate_mview($mview_id);
 }
 
+/**
+ * @deprecated Restructured API to make naming more readable and consistent.
+ * Function was deprecated in Tripal 2.0 and will be removed 2 releases from now.
+ * This function is considered to be no easier to use than tripal_get_mview_id directly
+ * tripal_get_mview_id().
+ *
+ * @see tripal_get_mview_id().
+ */
+function tripal_mviews_get_mview_id($view_name) {
+  tripal_report_error(
+    'tripal_deprecated', TRIPAL_NOTICE,
+    "DEPRECATED: %old_function has been replaced with %new_function. Please update your code.",
+    array(
+      '%old_function'=>'tripal_mviews_get_mview_id',
+      '%new_function' => 'tripal_get_mview_id'
+    )
+  );
+  
+  //New API Function
+  return tripal_get_mview_id($view_name);
+}
 /**
  * @deprecated Restructured API to make naming more readable and consistent.
  * Function was deprecated in Tripal 2.0 and will be removed 2 releases from now.

+ 1 - 1
tripal_core/api/tripal_core.chado_general.api.inc

@@ -199,7 +199,7 @@ function chado_get_property($basetable, $record_id, $property, $cv_name, $proper
  * @ingroup tripal_chado_api
  */
 function chado_insert_property($basetable, $record_id, $property,
-$cv_name, $value, $update_if_present = 0) {
+  $cv_name, $value, $update_if_present = 0) {
 
   // first see if the property already exists, if the user want's to update
   // then we can do that, but otherwise we want to increment the rank and

+ 2 - 2
tripal_core/api/tripal_core.chado_nodes.api.inc

@@ -723,7 +723,7 @@ function chado_cleanup_orphaned_nodes($table, $job_id = NULL) {
       $sql = "DELETE FROM {chado_" . $table . "} WHERE " . $table . "_id = :" . $table . "_id";
       db_query($sql, array(":" . $table . "_id" => $nid->$table_id));
       $message = "chado_$table missing $table.... DELETING entry.";
-      watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
+      watchdog('tripal_core', $message, array(), WATCHDOG_NOTICE);
     }
     $i++;
   }
@@ -747,7 +747,7 @@ function chado_cleanup_orphaned_nodes($table, $job_id = NULL) {
       if (node_access('delete', $node)) {
         $deleted++;
         $message = "Node missing in chado_$table table.... DELETING node $node->nid";
-        watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
+        watchdog("tripal_core", $message, array(), WATCHDOG_NOTICE);
         node_delete($node->nid);
       }
       else {

+ 4 - 4
tripal_core/api/tripal_core.mviews.api.inc

@@ -288,7 +288,7 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  *
  * @ingroup tripal_mviews_api
  */
-function tripal_mviews_get_mview_id($view_name) {
+function tripal_get_mview_id($view_name) {
   if (db_table_exists('tripal_mviews')) {
     $sql = "SELECT * FROM {tripal_mviews} WHERE name = :name";
     $results = db_query($sql, array(':name' => $view_name));
@@ -302,14 +302,14 @@ function tripal_mviews_get_mview_id($view_name) {
 }
 
 /**
- * Does the specified action for the specified Materialized View
+ * Populates the specified Materialized View
  *
  * @param $mview_id
  *   The unique ID of the materialized view for the action to be performed on
  *
  * @ingroup tripal_mviews_api
  */
-function tripal_add_job_populate_mview($mview_id) {
+function tripal_refresh_mview($mview_id) {
   global $user;
 
   if (!$mview_id) {
@@ -321,7 +321,7 @@ function tripal_add_job_populate_mview($mview_id) {
   $results = db_query($sql, array(':mview_id' => $mview_id));
   $mview = $results->fetchObject();
 
-  // add a job or perform the action based on the given operation
+  // add a job to populate the mview
   $args = array("$mview_id");
   tripal_add_job("Populate materialized view '$mview->name'", 'tripal_core',
      'tripal_populate_mview', $args, $user->uid);

+ 2 - 3
tripal_core/includes/tripal_core.mviews.inc

@@ -609,14 +609,13 @@ function tripal_mviews_delete_form_submit($form, &$form_state) {
 }
 
 /**
- * A wrapper for the tripal_add_job_populate_mview() API function, which
+ * A wrapper for the tripal_refresh_mview() API function, which
  * then redirects back to the admin page for mviews.
  * 
  * @param $mview_id
  */
-
 function tripal_mviews_add_populate_job($mview_id) {
   
-  tripal_add_job_populate_mview($mview_id);
+  tripal_refresh_mview($mview_id);
   drupal_goto("admin/tripal/schema/mviews");
 }

+ 18 - 23
tripal_cv/api/tripal_cv.api.inc

@@ -873,12 +873,13 @@ function tripal_autocomplete_cvterm($cv_id, $string = '') {
  * @param $record_id
  *   The primary key of the basetable to associate the cvterm with. This should be in integer.
  * @param $cvterm
- *   An associative array describing the cvterm. Valid keys include: 'name' => the
- *   name for the cvterm, 'cv_name' => the name of the cv the cvterm belongs to;
- *   'cv_id' => the primary key of the cv the cvterm belongs to.
+ *   An associative array describing the cvterm. Valid keys include: 
+ *     - name: the name for the cvterm, 
+ *     - cv_name: the name of the cv the cvterm belongs to.
+ *     - cv_id: the primary key of the cv the cvterm belongs to.
  * @param $options
  *   An associative array of options. Valid keys include:
- *    - insert_cvterm: Insert the cvterm if it doesn't already exist. FALSE is the default
+ *     - insert_cvterm: Insert the cvterm if it doesn't already exist. FALSE is the default
  *
  * @ingroup tripal_db_api
  */
@@ -889,23 +890,21 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
   // Default Values
   $options['insert_cvterm'] = (isset($options['insert_cvterm'])) ? $options['insert_cvterm'] : FALSE;
 
-  // If the cvterm_id is set then we know it already exists
-  // Otherwise, select to check
+  // If the cvterm_id is not set then find the cvterm record using the name and cv_id
   if (!isset($cvterm['cvterm_id'])) {
     $values = array(
       'name' => $cvterm['name'],
     );
     if (isset($cvterm['cv_id'])) {
       $values['cv_id'] = $cvterm['cv_id'];
-    } elseif (isset($cvterm['cv_name'])) {
+    } 
+    elseif (isset($cvterm['cv_name'])) {
       $values['cv_id'] = array(
         'name' => $cvterm['cv_name']
       );
     }
     else {
-      tripal_report_error(
-        'tripal_cv_api',
-        TRIPAL_WARNING,
+      tripal_report_error('tripal_cv_api', TRIPAL_WARNING,
         "tripal_associate_cvterm: The cvterm needs to have either the cv_name or cv_id
           supplied. You were trying to associate a cvterm with the %base %record_id
           and supplied the cvterm values: %cvterm.",
@@ -913,7 +912,10 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
       );
       return FALSE;
     }
-    $select = chado_select_record('cvterm',array('*'), $values);
+
+    // Get the cvterm. If it doesn't exist then add it if the option
+    // 'insert_cvterm' is set.
+    $select = chado_select_record('cvterm', array('*'), $values);
     if ($select) {
       $cvterm['cvterm_id'] = $select[0]->cvterm_id;
     }
@@ -924,9 +926,7 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
         $cvterm['cvterm_id'] = $insert->cvterm_id;
       }
       else {
-        tripal_report_error(
-          'tripal_cv_api',
-          TRIPAL_WARNING,
+        tripal_report_error('tripal_cv_api', TRIPAL_WARNING,
           "tripal_associate_cvterm: Unable to insert the cvterm using the cvterm values: %cvterm.",
           array('%cvterm' => print_r($cvterm,TRUE))
         );
@@ -934,16 +934,14 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
       }
     }
     else {
-      tripal_report_error(
-        'tripal_api',
-        TRIPAL_WARNING,
+      tripal_report_error('tripal_api', TRIPAL_WARNING,
         "tripal_associate_cvterm: The cvterm doesn't already exist. You supplied the cvterm values: %cvterm.",
         array('%cvterm' => print_r($cvterm,TRUE))
       );
       return FALSE;
     }
   }
-
+  
   // Now add the link between the record & cvterm
   if ($cvterm['cvterm_id'] > 0) {
     $values = array(
@@ -952,15 +950,12 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
       'pub_id' => 1,
     );
 
+    // Check if the cvterm is already associated. If so, don't re-add it.
     $result = chado_select_record($linking_table, array('*'), $values);
-
-    // if it doesn't already exist then add it
     if (!$result) {
       $success = chado_insert_record($linking_table, $values);
       if (!$success) {
-        tripal_report_error(
-          'tripal_api',
-          TRIPAL_WARNING,
+        tripal_report_error('tripal_api', TRIPAL_WARNING,
           "Failed to insert the %base record %term",
           array('%base' => $linking_table, '%term' => $cvterm['name'])
         );

+ 66 - 38
tripal_feature/includes/tripal_feature.delete.inc

@@ -81,11 +81,11 @@ function tripal_feature_delete_form() {
 function tripal_feature_delete_form_validate($form, &$form_state) {
   $organism_id   = $form_state['values']['organism_id'];
   $seq_type      = trim($form_state['values']['seq_type']);
-  $analysis_id   = $form_state['values']['analysis_id'];
+  //$analysis_id   = $form_state['values']['analysis_id'];
   $is_unique     = $form_state['values']['is_unique'];
   $feature_names = $form_state['values']['feature_names'];
 
-  if (!$organism_id and !$anaysis_id and !$seq_type and !$feature_names) {
+  if (!$organism_id and !$seq_type and !$feature_names) { // !$anaysis_id and
     form_set_error('feature_names', t("Please select at least one option"));
   }
 
@@ -117,9 +117,10 @@ function tripal_feature_delete_form_submit($form, &$form_state) {
 
   $organism_id   = $form_state['values']['organism_id'];
   $seq_type      = trim($form_state['values']['seq_type']);
-  $analysis_id   = $form_state['values']['analysis_id'];
+  //$analysis_id   = $form_state['values']['analysis_id'];
   $is_unique     = $form_state['values']['is_unique'];
   $feature_names = $form_state['values']['feature_names'];
+  $analysis_id = NULL;
 
   $args = array($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names);
 
@@ -152,47 +153,74 @@ function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
   global $user;
   $match = array();
 
-  // if feature names have been provided then handle that separately
-  if ($feature_names) {
-    $names = preg_split('/\s+/', $feature_names);
-    if (sizeof($names) == 1) {
-      $names = $names[0];
+  // Deleting of features will cause a cascade delete on the
+  // featureloc table which in turn will wind up calling create_point
+  // function which is not prefix with the schema, and an error occurs.
+  // Therefore, we set the active database to chado to get around that
+  // problem.
+  $previous_db = chado_set_active('chado');
+  
+  // begin the transaction
+  $transaction = db_transaction();
+  print "\nNOTE: Loading of this GFF file 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 {
+    
+    
+    // if feature names have been provided then handle that separately
+    if ($feature_names) {
+      $names = preg_split('/\s+/', $feature_names);
+      if (sizeof($names) == 1) {
+        $names = $names[0];
+      }
+      if ($is_unique) {
+        $match['uniquename'] = $names;
+      }
+      else {
+        $match['name'] = $names;
+      }
+      $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
+      print "Deleting " . $num_deletes[0]->cnt . " features\n";
+      
+      chado_delete_record('feature', $match);
     }
-    if ($is_unique) {
-      $match['uniquename'] = $names;
+  
+    // if the user has provided an analysis_id then handle that separately
+    elseif ($analysis_id) {
+      tripal_feature_delete_by_analysis();
     }
     else {
-      $match['name'] = $names;
+  
+      if ($organism_id) {
+        $match['organism_id'] = $organism_id;
+      }
+      if ($seq_type) {
+        $match['type_id'] = array(
+          'name' => $seq_type,
+          'cv_id' => array(
+            'name' => 'sequence'
+          ),
+        );
+      }
+      $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
+      print "Deleting " . $num_deletes[0]->cnt . " features\n";
+      chado_delete_record('feature', $match);
     }
-    $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
-    print "Deleting " . $num_deletes[0]->cnt . " features\n";
-    chado_delete_record('feature', $match);
+  
+    print "Removing orphaned feature pages\n";
+    chado_cleanup_orphaned_nodes('feature');
   }
-
-  // if the user has provided an analysis_id then handle that separately
-  elseif ($analysis_id) {
-    tripal_feature_delete_by_analysis();
-  }
-  else {
-
-    if ($organism_id) {
-      $match['organism_id'] = $organism_id;
-    }
-    if ($seq_type) {
-      $match['type_id'] = array(
-        'name' => $seq_type,
-        'cv_id' => array(
-          'name' => 'sequence'
-        ),
-      );
-    }
-    $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
-    print "Deleting " . $num_deletes[0]->cnt . " features\n";
-    chado_delete_record('feature', $match);
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    $transaction->rollback();
+    print "FAILED: Rolling back database changes...\n";
+    watchdog_exception('tripal_feature', $e);
+    return 0;
   }
-
-  print "Removing orphaned feature pages\n";
-  tripal_features_cleanup(array(), $user->uid);
+  chado_set_active($previous_db);
+  print "\nDone\n";
 }
 
 /**

+ 1 - 2
tripal_feature/theme/templates/tripal_feature_terms.tpl.php

@@ -30,9 +30,8 @@ if (count($s_terms) > 0) { ?>
     // can be found here:
     // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
     $rows = array();
-    
-    foreach ($terms as $term) { 
 
+    foreach ($terms as $term) { 
       $accession = $term->cvterm_id->dbxref_id->accession;
       if (is_numeric($term->cvterm_id->dbxref_id->accession)) {
         $accession = $term->cvterm_id->dbxref_id->db_id->name . ":" . $term->cvterm_id->dbxref_id->accession;