Bläddra i källkod

Fixed bug with transation not committing beetween OBO term loading and cvtermpath loading

Stephen Ficklin 7 år sedan
förälder
incheckning
eb2eed105b
2 ändrade filer med 85 tillägg och 80 borttagningar
  1. 65 31
      tripal/api/tripal.importer.api.inc
  2. 20 49
      tripal_chado/includes/TripalImporter/OBOImporter.inc

+ 65 - 31
tripal/api/tripal.importer.api.inc

@@ -91,18 +91,60 @@ function tripal_load_include_importer_class($class) {
 function tripal_run_importer($import_id, TripalJob $job = NULL) {
 
   $loader = NULL;
+  $loader = TripalImporter::byID($import_id);
+  $loader->setJob($job);
+  $loader->prepareFiles();
+
+  print "\nNOTE: Loading of this OBO is performed using a database transaction. \n" .
+        "The transaction occurs in two parts. First the ontology is loaded. Second, \n" .
+        "the paths between all terms are loaded. If either of the parts fail \n" .
+        "or is terminated prematurely then only the part that failed will have all \n" .
+        "insertions and updates rolled back and will not be found in the database\n\n";
+
+  try {
+    // Run the loader
+    tripal_run_importer_run($loader, $job);
+
+    // Handle the post run.
+    tripal_run_importer_post_run($loader, $job);
+
+    // Check for tables with new cvterms
+    tripal_chado_map_cvterms();
+
+    // Check for new fields and notify the user.
+    tripal_tripal_cron_notification();
+
+    // Clear the Drpual chace
+    cache_clear_all();
+
+    print "\nDone\n";
+  }
+  catch (Exception $e) {
+    if ($job) {
+      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
+    }
+    if ($loader) {
+      $loader->cleanFile();
+    }
+  }
+}
+
+/**
+ * First step of the tripal_run_importer.
+ *
+ * @param $loader
+ *   The TripalImporter object.
+ * @param $job
+ *   The TripalJob object.$this
+ * @throws Exception
+ */
+function tripal_run_importer_run($loader, $job) {
+
   try {
     // begin the transaction
     $transaction = db_transaction();
-    print "\nNOTE: Loading of this 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";
 
-    $loader = TripalImporter::byID($import_id);
-    $loader->setJob($job);
-    $loader->prepareFiles();
     $loader->run();
-    $loader->cleanFile();
 
     if ($job) {
       $job->logMessage("Done");
@@ -115,38 +157,30 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
     }
   }
   catch (Exception $e) {
+    // Rollback and re-throw the error.
     $transaction->rollback();
-    if ($job) {
-      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
-    }
-    if ($loader) {
-      $loader->cleanFile();
-    }
+    throw $e;
   }
-  unset($transaction);
+}
+/**
+ * Second step of the tripal_run_importer.
+ *
+ * @param $loader
+ *   The TripalImporter object.
+ * @param $job
+ *   The TripalJob object.
+ * @throws Exception
+ */
+function tripal_run_importer_post_run($loader, $job) {
   try {
     // the transaction
     $transaction = db_transaction();
     $loader->postRun();
-  } 
+  }
   catch (Exception $e) {
+    // Rollback and re-throw the error.
     $transaction->rollback();
-    if ($job) {
-      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
-    }
-    if ($loader) {
-      $loader->cleanFile();
-    }
+    throw $e;
   }
-  // Check for tables with new cvterms
-  tripal_chado_map_cvterms();
-  
-  // Check for new fields and notify the user.
-  tripal_tripal_cron_notification();
-  
-  // Clear the Drpual chace
-  cache_clear_all();
-
-  print "\nDone\n";
 }
 

+ 20 - 49
tripal_chado/includes/TripalImporter/OBOImporter.inc

@@ -396,7 +396,13 @@ class OBOImporter extends TripalImporter {
    *
    */
   public function postRun() {
-    $this->load_cvtermpath($this->newcvs);
+
+    // Upate the cvtermpath table for each newly added CV.
+    $this->logMessage("Updating cvtermpath table.  This may take a while...");
+    foreach ($this->newcvs as $namespace => $cvid) {
+      $this->logMessage("- Loading paths for @vocab", array('@vocab' => $namespace));
+      tripal_update_cvtermpath($cvid);
+    }
   }
   /**
    * A wrapper function for importing the user specified OBO file into Chado by
@@ -464,7 +470,7 @@ class OBOImporter extends TripalImporter {
       tripal_insert_obo($obo_name, $file);
     }
 
-    $success = $this->loadOBO_v1_2($file, $this->newcvs);
+    $success = $this->loadOBO_v1_2($file);
   }
 
   /**
@@ -485,8 +491,6 @@ class OBOImporter extends TripalImporter {
    */
   private function loadOBO_v1_2_url($obo_name, $url, $is_new = TRUE) {
 
-    $newcvs = array();
-
     // first download the OBO
     $temp = tempnam(sys_get_temp_dir(), 'obo_');
     print "Downloading URL $url, saving to $temp\n";
@@ -509,34 +513,12 @@ class OBOImporter extends TripalImporter {
     }
 
     // second, parse the OBO
-    $this->loadOBO_v1_2($temp, $newcvs);
-
-    // update the cvtermpath table
-    $this->load_cvtermpath($newcvs);
+    $this->loadOBO_v1_2($temp);
 
     // now remove the temp file
     unlink($temp);
   }
 
-  /**
-   * A function for executing the cvtermpath function of Chado.  This function
-   * populates the cvtermpath table of Chado for quick lookup of term
-   * relationships
-   *
-   * @param $newcvs
-   *   An associative array of controlled vocabularies to update.  The key must be
-   *   the name of the vocabulary and the value the cv_id from the cv table of chado.
-   *
-   * @ingroup tripal_obo_loader
-   */
-  private function load_cvtermpath($newcvs) {
-
-    print "\nUpdating cvtermpath table.  This may take a while...\n";
-    foreach ($newcvs as $namespace => $cvid) {
-      tripal_update_cvtermpath($cvid);
-    }
-  }
-
   /**
    * Imports a given OBO file into Chado.  This function is usually called by
    * one of three wrapper functions:  loadOBO_v1_2_id,
@@ -546,15 +528,10 @@ class OBOImporter extends TripalImporter {
    *
    * @param $flie
    *   The full path to the OBO file on the file system
-   * @param $newcvs
-   *   An empty array passed by reference that upon return will contain the list
-   *   of newly added vocabularies.  The key will contain the CV name and the
-   *   value the new cv_id
-   *
    *
    * @ingroup tripal_obo_loader
    */
-  private function loadOBO_v1_2($file, &$newcvs) {
+  private function loadOBO_v1_2($file) {
 
     $header = array();
     $ret = array();
@@ -577,7 +554,7 @@ class OBOImporter extends TripalImporter {
       if (!$defaultcv) {
         throw new Exception('Cannot add namespace ' . $header['default-namespace'][0]);
       }
-      $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
+      $this->newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
     }
     // If the 'default-namespace' is missing.
     else {
@@ -589,7 +566,7 @@ class OBOImporter extends TripalImporter {
         if (!$defaultcv) {
           throw new Exception('Cannot add namespace ' . strtoupper($header['ontology'][0]));
         }
-        $newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
+        $this->newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
       }
       else {
         throw new Exception("Could not find a namespace for this OBO file: $file");
@@ -601,11 +578,11 @@ class OBOImporter extends TripalImporter {
     }!
     // Add any typedefs to the vocabulary first.
     $this->logMessage("Step 2: Loading type defs...");
-    $this->loadTypeDefs($defaultcv, $newcvs, $default_db);
+    $this->loadTypeDefs($defaultcv, $default_db);
 
     // Next add terms to the vocabulary.
     $this->logMessage("Step 3: Loading terms...");
-    if (!$this->processTerms($defaultcv, $newcvs, $default_db)) {
+    if (!$this->processTerms($defaultcv, $default_db)) {
       throw new Exception('Cannot add terms from this ontology');
     }
 
@@ -618,15 +595,12 @@ class OBOImporter extends TripalImporter {
    * @param $defaultcv
    *   A database object containing a record from the cv table for the
    *   default controlled vocabulary
-   * @param $newcvs
-   *   An associative array of controlled vocabularies for this OBO.  The key must be
-   *   the name of the vocabulary and the value the cv_id from the cv table of chado.
    * @param $default_db
    *   The name of the default database.
    *
    * @ingroup tripal_obo_loader
    */
-  private function loadTypeDefs($defaultcv, $newcvs, $default_db) {
+  private function loadTypeDefs($defaultcv, $default_db) {
     $sql = "SELECT * FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
     $typedefs = chado_query($sql);
 
@@ -644,7 +618,7 @@ class OBOImporter extends TripalImporter {
     foreach ($typedefs as $typedef) {
       $this->setItemsHandled($i);
       $term = unserialize(base64_decode($typedef->stanza));
-      $this->processTerm($term, $defaultcv->name, 1, $newcvs, $default_db);
+      $this->processTerm($term, $defaultcv->name, 1, $default_db);
       $i++;
     }
 
@@ -660,13 +634,10 @@ class OBOImporter extends TripalImporter {
    * @param $defaultcv
    *   A database object containing a record from the cv table for the
    *   default controlled vocabulary
-   * @param $newcvs
-   *   An associative array of controlled vocabularies for this OBO.  The key must be
-   *   the name of the vocabulary and the value the cv_id from the cv table of chado.
    * @param $default_db
    *   The name of the default database.
    */
-  private function processTerms($defaultcv, &$newcvs, $default_db) {
+  private function processTerms($defaultcv, $default_db) {
     $i = 0;
 
     // Iterate through each term from the OBO file and add it.
@@ -693,7 +664,7 @@ class OBOImporter extends TripalImporter {
       $this->setItemsHandled($i);
 
       // Add/update this term.
-      if (!$this->processTerm($term, $defaultcv->name, 0, $newcvs, $default_db)) {
+      if (!$this->processTerm($term, $defaultcv->name, 0, $default_db)) {
         throw new Exception("Failed to process terms from the ontology");
       }
 
@@ -718,7 +689,7 @@ class OBOImporter extends TripalImporter {
    *
    * @ingroup tripal_obo_loader
    */
-  private function processTerm($term, $defaultcv, $is_relationship = 0, &$newcvs, $default_db) {
+  private function processTerm($term, $defaultcv, $is_relationship = 0, $default_db) {
 
     // make sure we have a namespace for this term
     if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
@@ -762,7 +733,7 @@ class OBOImporter extends TripalImporter {
     chado_query($sql, array(':cvterm_id' => $cvterm->cvterm_id));
 
     if (array_key_exists('namespace', $term)) {
-      $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
+      $this->newcvs[$term['namespace'][0]] = $cvterm->cv_id;
     }
 
     // now handle other properites