Browse Source

Updated TripalImporter

Stephen Ficklin 8 years ago
parent
commit
0018be0880

+ 26 - 64
tripal/api/tripal.importer.api.inc

@@ -89,77 +89,39 @@ function tripal_load_include_importer_class($class) {
  * @throws Exception
  */
 function tripal_run_importer($import_id, TripalJob $job = NULL) {
-  $details = db_select('tripal_import', 'ti')
-    ->fields('ti')
-    ->condition('ti.import_id', $import_id)
-    ->execute()
-    ->fetchObject();
 
-  if ($details) {
-    $details->arguments = unserialize($details->arguments);
-    if ($details->fid) {
-      $details->file = file_load($details->fid);
-    }
-
-    $class = $details->class;
-    tripal_load_include_importer_class($class);
-    if (class_exists($class)) {
-
-      $loader_name = $class::$name;
-      $loader = new $class($job);
-      try {
-
-        // If the file is provided via a URL then download the file.
-        if (!empty($details->arguments['file_url'])) {
-          // 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->logMessage('Downloading file from remote location: !url', array('!url' => $details->arguments['file_url']));
+  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";
 
-          // Create a temporary file.
-          $temp = tempnam("temporary://", 'import_');
-          $url_fh = fopen($details->arguments['file_url'], "r");
-          $tmp_fh = fopen($temp, "w");
-          if (!$url_fh) {
-            throw new Exception(t("Unable to download the remote file at !url. Could a firewall be blocking outgoing connections?",
-                array('!url', $details->arguments['file_url'])));
-          }
-          // Write the contents of the remote file to the temp file.
-          while (!feof($url_fh)) {
-            fwrite($tmp_fh, fread($url_fh, 255), 255);
-          }
-          // Set the path to the file for the importer to use.
-          $details->arguments['file_path'] = $temp;
-        }
+    $loader = TripalImporter::byID($import_id);
+    $loader->setJob($job);
+    $loader->prepareFile();
+    $loader->run();
 
-        $loader->preRun($details);
-        $loader->run($details);
-        $loader->postRun($details);
-        if ($job) {
-          $job->logMessage("Done");
-        }
+    if ($job) {
+      $job->logMessage("Done");
+    }
 
-        // Remove the temp file
-        if (!empty($details->arguments['file_url'])) {
-          $loader->logMessage('Removing downloaded file...');
-          unlink($temp);
-        }
+    // Remove the temp file
+    if (!empty($details->arguments['file_url'])) {
+      $loader->logMessage('Removing downloaded file...');
+      unlink($temp);
+    }
 
-        print "\nDone\n";
+    print "\nDone\n";
 
-        // Check for new fields and notify the user.
-        tripal_tripal_cron_notification();
+    // Check for new fields and notify the user.
+    tripal_tripal_cron_notification();
 
-      }
-      catch (Exception $e) {
-        $transaction->rollback();
-        if ($job) {
-          $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
-        }
-      }
+  }
+  catch (Exception $e) {
+    $transaction->rollback();
+    if ($job) {
+      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
     }
   }
 }

+ 277 - 23
tripal/includes/TripalImporter.inc

@@ -89,27 +89,29 @@ class TripalImporter {
   public static $file_required = TRUE;
 
 
+  /**
+   * The array of arguments used for this loader.  Each argument should
+   * be a separate array containing a machine_name, name, and description
+   * keys.  This information is used to build the help text for the loader.
+   */
+  public static $argument_list = array();
+
   // --------------------------------------------------------------------------
   //                  PRIVATE MEMBERS -- DO NOT EDIT or OVERRIDE
   // --------------------------------------------------------------------------
-  /**
-   * The job that this importer is associated with.  This is needed for
-   * updating the status of the job.
-   */
-  private $job = NULL;
 
   /**
    * The number of items that this importer needs to process. A progress
    * can be calculated by dividing the number of items process by this
    * number.
    */
-  private $total_items = 0;
+  private $total_items;
 
   /**
    * The number of items that have been handled so far.  This must never
    * be below 0 and never exceed $total_items;
    */
-  private $num_handled = 0;
+  private $num_handled;
 
   /**
    * The interval when the job progress should be updated. Updating the job
@@ -118,13 +120,40 @@ class TripalImporter {
    * 0 and 100 to indicate a percent interval (e.g. 1 means update the
    * progress every time the num_handled increases by 1%).
    */
-  private $interval = 1;
+  private $interval;
 
   /**
    * Each time the job progress is updated this variable gets set.  It is
    * used to calculate if the $interval has passed for the next update.
    */
-  private $prev_update = 0;
+  private $prev_update;
+
+  /**
+   * The job that this importer is associated with.  This is needed for
+   * updating the status of the job.
+   */
+  protected $job;
+
+  /**
+   * The arguments needed for the importer. This is a list of key/value
+   * pairs in an associative array.
+   */
+  protected $arguments;
+
+  /**
+   * The ID for this import record.
+   */
+  protected $import_id;
+
+  /**
+   * Prior to running an importer it must be prepared to make sure the file
+   * is available.  Preparing the importer will download all the necessary
+   * files.  This value is set to TRUE after the importer is prepared for
+   * funning.
+   */
+  protected $is_prepared;
+
+
 
   // --------------------------------------------------------------------------
   //                          CONSTRUCTORS
@@ -136,9 +165,247 @@ class TripalImporter {
    *   An optional TripalJob object that this loader is associated with.
    */
   public function __construct(TripalJob $job = NULL) {
+
+    // Intialize the private member variables.
+    $this->is_prepared = FALSE;
+    $this->import_id = NULL;
+    $this->arguments = array();
+    $this->job = NULL;
+    $this->total_items = 0;
+    $this->interval = 1;
+    $this->num_handled = 0;
+    $this->prev_update = 0;
+  }
+
+  /**
+   * Instantiates a new TripalImporter object using the import record ID.
+   *
+   * This function will automatically instantiate the correct TripalImporter
+   * child class that is appropriate for the provided ID.
+   *
+   * @param $import_id
+   *   The ID of the import recrod.
+   * @return
+   *   An TripalImporter object of the appropriate child class.
+   */
+  static public function byID($import_id) {
+    // Get the importer.
+    $import = db_select('tripal_import', 'ti')
+      ->fields('ti')
+      ->condition('ti.import_id', $import_id)
+      ->execute()
+      ->fetchObject();
+
+    if (!$import) {
+      throw new Exception('Cannot find an importer that matches the given import ID.');
+    }
+
+    $class = $import->class;
+    tripal_load_include_importer_class($class);
+    if (class_exists($class)) {
+      $loader = new $class();
+      $loader->load($import_id);
+      return $loader;
+    }
+    else {
+      throw new Exception('Cannot find the matching class for this import record.');
+    }
+  }
+
+  /**
+   * Associate this importer with the Tripal job that is running it.
+   *
+   * Associating an import with a job will allow the importer to log messages
+   * to the job log.
+   *
+   * @param TripalJob $job
+   *   An instnace of a TripalJob.
+   */
+  public function setJob(TripalJob $job) {
     $this->job = $job;
   }
 
+  /**
+   * Creates a new importer record.
+   *
+   * @param $run_args
+   *   An associative array of the arguments needed to run the importer. Each
+   *   importer will have its own defined set of arguments.
+   *
+   * @param $file_details
+   *   An associative array with one of the following keys:
+   *   -fid: provides the Drupal managed File ID for the file.
+   *   -file_local: provides the full path to the file on the server.
+   *   -file_remote: provides the remote URL for the file.
+   *
+   * @throws Exception
+   */
+  public function create($run_args, $file_details) {
+    global $user;
+    $class = get_called_class();
+
+    try {
+      // Build the values for the tripal_importer table insert.
+      $values = array(
+        'uid' => $user->uid,
+        'class' => $class,
+        'submit_date' => time(),
+      );
+
+      // Build the arguments array, which consists of the run arguments
+      // and the file.
+      $arguments = array(
+        'run_args' => $run_args,
+        'file' => array(
+          'file_path' => '',
+          'file_local' => '',
+          'file_remote' => '',
+          'fid' => NULL,
+        ),
+      );
+
+      // Get the file argument.
+      $has_file = 0;
+      if (array_key_exists('file_local', $file_details)) {
+        $arguments['file']['file_local'] = $file_details['file_local'];
+        $arguments['file']['file_path'] = $file_details['file_local'];
+        $has_file++;
+      }
+      if (array_key_exists('file_remote', $file_details)) {
+        $arguments['file']['file_remote'] = $file_details['file_remote'];
+        $has_file++;
+      }
+      if (array_key_exists('fid', $file_details)) {
+        $fid = $file_details['fid'];
+        $file = file_load($fid);
+        $arguments['file']['file_path'] =  base_path() . drupal_realpath($file->uri);
+        $arguments['file']['fid'] = $fid;
+        $values['fid'] = $fid;
+        $has_file++;
+      }
+
+      // Validate the $file_details argument.
+      if ($has_file == 0 and $this->file_required == TRUE) {
+        throw new Exception("Must provide a proper file identifier for the \$file_details argument.");
+      }
+
+      // Store the arguments in the class and serialize for table insertion.
+      $this->arguments = $arguments;
+      $values['arguments'] = serialize($arguments);
+
+      // Insert the importer record.
+      $import_id = db_insert('tripal_import')
+        ->fields($values)
+        ->execute();
+
+      $this->import_id = $import_id;
+    }
+    catch (Exception $e){
+      throw new Exception('Cannot create importer: ' .  $e->getMessage());
+    }
+  }
+
+  /**
+   * Loads an existing import record into this object.
+   *
+   * @param $import_id
+   *   The ID of the import record.
+   */
+  public function load($import_id) {
+    $class = get_called_class();
+
+    // Get the importer.
+    $import = db_select('tripal_import', 'ti')
+      ->fields('ti')
+      ->condition('ti.import_id', $import_id)
+      ->execute()
+      ->fetchObject();
+
+    if (!$import) {
+      throw new Exception('Cannot find an importer that matches the given import ID.');
+    }
+
+    if ($import->class != $class) {
+      throw new Exception('The importer specified by the given ID does not match this importer class.');
+    }
+
+    $this->arguments = unserialize($import->arguments);
+    $this->import_id = $import_id;
+
+  }
+
+
+  /**
+   * Submits the importer for execution as a job.
+   *
+   * @return
+   *   The ID of the newly submitted job.
+   */
+  public function submitJob() {
+    global $user;
+
+    $class = get_called_class();
+
+    if (!$this->import_id) {
+      throw new Exception('Cannot submit an importer job without an import record. Please run create() first.');
+    }
+
+    // Add a job to run the importer.
+    try {
+      $args = array($this->import_id);
+      $includes = array(
+        module_load_include('inc', 'tripal', 'api/tripal.importer.api'),
+      );
+      $job_id = tripal_add_job($class::$button_text, 'tripal',
+          'tripal_run_importer', $args, $user->uid, 10, $includes);
+
+      return $job_id;
+    }
+    catch (Exception $e){
+      throw new Exception('Cannot create importer job: ' .  $e->getMessage());
+    }
+  }
+
+  /**
+   * Prepares the importer files for execution.
+   *
+   * This function must be run prior to the run() function to ensure that
+   * the import file is ready to go.
+   */
+  public function prepareFile() {
+    $class = get_called_class();
+
+    // If no file is required then just indicate that all is good to go.
+    if ($class::$file_required == FALSE) {
+      $this->is_prepared = TRUE;
+      return;
+    }
+
+    try {
+      if (!empty($this->arguments['file']['file_remote'])) {
+        // Create a temporary file.
+        $temp = tempnam("temporary://", 'import_');
+        $url_fh = fopen($this->arguments['remote_file'], "r");
+        $tmp_fh = fopen($temp, "w");
+        if (!$url_fh) {
+          throw new Exception(t("Unable to download the remote file at !url. Could a firewall be blocking outgoing connections?",
+              array('!url', $details->arguments['file_url'])));
+        }
+        // Write the contents of the remote file to the temp file.
+        while (!feof($url_fh)) {
+          fwrite($tmp_fh, fread($url_fh, 255), 255);
+        }
+        // Set the path to the file for the importer to use.
+        $details->arguments['file_path'] = $temp;
+
+        $this->is_prepared = TRUE;
+      }
+    }
+    catch (Exception $e){
+      throw new Exception('Cannot prepare the importer: ' .  $e->getMessage());
+    }
+  }
+
   // --------------------------------------------------------------------------
   //                     OVERRIDEABLE FUNCTIONS
   // --------------------------------------------------------------------------
@@ -176,25 +443,12 @@ class TripalImporter {
 
   }
 
-  /**
-   * Executes tasks that should be run prior to execution of the run() function.
-   */
-  public function preRun($details) {
-
-  }
   /**
    * Performs the import.
-   *
-   * This function must be overrriden by a child class to perform the import.
    */
-  public function run($details) {
+  public function run() {
   }
-  /**
-   * Executes tasks that should be run after execution of the run() function.
-   */
-  public function postRun($details) {
 
-  }
   /**
    * Logs a message for the importer.
    *

+ 15 - 49
tripal/includes/tripal.importer.inc

@@ -144,19 +144,19 @@ function tripal_get_importer_form_validate($form, &$form_state) {
 function tripal_get_importer_form_submit($form, &$form_state) {
   global $user;
 
-  $arguments = $form_state['values'];
+  $run_args = $form_state['values'];
   $class = $form_state['values']['importer_class'];
   tripal_load_include_importer_class($class);
 
   // Remove the file_local and file_upload args. We'll add in a new
   // full file path and the fid instead.
-  unset($arguments['file_local']);
-  unset($arguments['file_upload']);
-  unset($arguments['form_build_id']);
-  unset($arguments['form_token']);
-  unset($arguments['form_id']);
-  unset($arguments['op']);
-  unset($arguments['button']);
+  unset($run_args['file_local']);
+  unset($run_args['file_upload']);
+  unset($run_args['form_build_id']);
+  unset($run_args['form_token']);
+  unset($run_args['form_id']);
+  unset($run_args['op']);
+  unset($run_args['button']);
 
 
   $file_local = NULL;
@@ -177,24 +177,16 @@ function tripal_get_importer_form_submit($form, &$form_state) {
   // Sumbit a job for this loader.
   $fname = '';
   $fid = NULL;
+  $file_details = array();
   if ($file_local) {
     $fname = preg_replace("/.*\/(.*)/", "$1", $file_local);
-    $arguments['file_path'] = $file_local;
-    $arguments['file_url'] = '';
-    $arguments['fid'] = NULL;
+    $file_details['file_local'] = $file_local;
   }
   if ($file_upload) {
-    $fid = $file_upload;
-    $file = file_load($fid);
-    $fname = $file->filename;
-    $arguments['file_path'] =  base_path() . drupal_realpath($file->uri);
-    $arguments['file_url'] = '';
-    $arguments['fid'] = $file_upload;
+    $file_details['fid'] = $file_upload;
   }
   if ($file_remote) {
-    $arguments['file_path'] = '';
-    $arguments['file_url'] = $file_remote;
-    $arguments['fid'] = NULL;
+    $file_details['file_remote'] = $file_remote;
   }
   try {
 
@@ -208,35 +200,9 @@ function tripal_get_importer_form_submit($form, &$form_state) {
       return;
     }
 
-    $values = array(
-      'uid' => $user->uid,
-      'class' => $class,
-      'arguments' => serialize($arguments),
-      'submit_date' => time(),
-    );
-    if ($fid) {
-      $values['fid'] = $fid;
-    }
-    // Add the importer to the tripal_import table.
-    $import_id = db_insert('tripal_import')
-      ->fields($values)
-      ->execute();
-
-    // Add a job to run the importer.
-    $args = array($import_id);
-    $includes = array(
-      module_load_include('inc', 'tripal', 'api/tripal.importer.api'),
-    );
-    $job_id = tripal_add_job("Import " . $class::$upload_title . ": $fname", 'tripal',
-        'tripal_run_importer', $args, $user->uid, 10, $includes);
-
-    // Now associate the job_id with the import.
-    db_update('tripal_import')
-      ->fields(array(
-        'job_id' => $job_id,
-      ))
-      ->condition('import_id', $import_id)
-      ->execute();
+    $importer->create($run_args, $file_details);
+    $importer->submitJob();
+
   }
   catch (Exception $e) {
     drupal_set_message('Cannot submit import: ' . $e->getMessage(), 'error');

+ 58 - 57
tripal_chado/includes/TripalImporter/FASTAImporter.inc

@@ -31,7 +31,7 @@ class FASTAImporter extends TripalImporter {
   public static $upload_description = 'Please provide the FASTA file. The file must have a .fasta extension.';
 
   /**
-   * The title that should appear above the upload button.
+   * The title that should appear above the file upload section.
    */
   public static $upload_title = 'FASTA Upload';
 
@@ -308,10 +308,11 @@ class FASTAImporter extends TripalImporter {
   /**
    * @see TripalImporter::run()
    */
-  public function run($details) {
-    $arguments = $details->arguments;
+  public function run() {
+
+    $arguments = $this->arguments['run_args'];
+    $file_path = $this->arguments['file']['file_path'];
 
-    $dfile = $arguments['file_path'];
     $organism_id = $arguments['organism_id'];
     $type = $arguments['seqtype'];
     $method = $arguments['method'];
@@ -346,14 +347,14 @@ class FASTAImporter extends TripalImporter {
       $match_type = 'Unique name';
     }
 
-    $this->loadFasta($dfile, $organism_id, $type, $re_name, $re_uname, $re_accession,
+    $this->loadFasta($file_path, $organism_id, $type, $re_name, $re_uname, $re_accession,
       $db_id, $rel_type, $re_subject, $parent_type, $method, $analysis_id,
       $match_type);
   }
   /**
    * Load a fasta file.
    *
-   * @param $dfile
+   * @param $file_path
    *   The full path to the fasta file to load.
    * @param $organism_id
    *   The organism_id of the organism these features are from.
@@ -384,7 +385,7 @@ class FASTAImporter extends TripalImporter {
    * @param $match_type
    *  Whether to match existing features based on the 'Name' or 'Unique name'.
    */
-  private function loadFasta($dfile, $organism_id, $type, $re_name, $re_uname, $re_accession,
+  private function loadFasta($file_path, $organism_id, $type, $re_name, $re_uname, $re_accession,
       $db_id, $rel_type, $re_subject, $parent_type, $method, $analysis_id, $match_type) {
 
       // First get the type for this sequence.
@@ -397,7 +398,7 @@ class FASTAImporter extends TripalImporter {
     ";
     $cvterm = chado_query($cvtermsql, array(':cvname' => 'sequence',':name' => $type,':synonym' => $type))->fetchObject();
     if (!$cvterm) {
-      $this->logMessage("Cannot find the term type: '%type'", array('%type' => $type), TRIPAL_ERROR);
+      $this->logMessage("Cannot find the term type: '!type'", array('!type' => $type), TRIPAL_ERROR);
       return 0;
     }
 
@@ -405,8 +406,8 @@ class FASTAImporter extends TripalImporter {
     if ($parent_type) {
       $parentcvterm = chado_query($cvtermsql, array(':cvname' => 'sequence', ':name' => $parent_type,':synonym' => $parent_type))->fetchObject();
       if (!$parentcvterm) {
-        $this->logMessage("Cannot find the paretne term type: '%type'", array(
-          '%type' => $parentcvterm
+        $this->logMessage("Cannot find the paretne term type: '!type'", array(
+          '!type' => $parentcvterm
         ), TRIPAL_ERROR);
         return 0;
       }
@@ -416,8 +417,8 @@ class FASTAImporter extends TripalImporter {
     if ($rel_type) {
       $relcvterm = chado_query($cvtermsql, array(':cvname' => 'sequence',':name' => $rel_type,':synonym' => $rel_type))->fetchObject();
       if (!$relcvterm) {
-        $this->logMessage("Cannot find the relationship term type: '%type'", array(
-          '%type' => $relcvterm
+        $this->logMessage("Cannot find the relationship term type: '!type'", array(
+          '!type' => $relcvterm
         ), TRIPAL_ERROR);
         return 0;
       }
@@ -429,10 +430,10 @@ class FASTAImporter extends TripalImporter {
     $dbxref_tbl = chado_get_schema('dbxref');
 
     $this->logMessage(t("Step 1: Finding sequences..."));
-    $filesize = filesize($dfile);
-    $fh = fopen($dfile, 'r');
+    $filesize = filesize($file_path);
+    $fh = fopen($file_path, 'r');
     if (!$fh) {
-      throw new Exception(t("Cannot open file: %dfile", array('%dfile' => $dfile)));
+      throw new Exception(t("Cannot open file: !dfile", array('!dfile' => $file_path)));
     }
     $num_read = 0;
 
@@ -455,12 +456,12 @@ class FASTAImporter extends TripalImporter {
         // Get the feature name if a regular expression is provided.
         if ($re_name) {
           if (!preg_match("/$re_name/", $defline, $matches)) {
-            $this->logMessage("Regular expression for the feature name finds nothing. Line %line.",
-              array('%line' => $i), TRIPAL_ERROR);
+            $this->logMessage("Regular expression for the feature name finds nothing. Line !line.",
+              array('!line' => $i), TRIPAL_ERROR);
           }
           elseif (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
-            $this->logMessage("Regular expression retrieves a value too long for the feature name. Line %line.",
-              array('%line' => $i), TRIPAL_WARNING);
+            $this->logMessage("Regular expression retrieves a value too long for the feature name. Line !line.",
+              array('!line' => $i), TRIPAL_WARNING);
           }
           else {
             $name = trim($matches[1]);
@@ -472,23 +473,23 @@ class FASTAImporter extends TripalImporter {
         elseif (strcmp($match_type, 'Name') == 0) {
           if (preg_match("/^\s*(.*?)[\s\|].*$/", $defline, $matches)) {
             if (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
-              $this->logMessage("Regular expression retrieves a feature name too long for the feature name. Line %line.",
-                array('%line' => $i), TRIPAL_WARNING);
+              $this->logMessage("Regular expression retrieves a feature name too long for the feature name. Line !line.",
+                array('!line' => $i), TRIPAL_WARNING);
             }
             else {
               $name = trim($matches[1]);
             }
           }
           else {
-            $this->logMessage("Cannot find a feature name. Line %line.", array('%line' => $i), TRIPAL_WARNING);
+            $this->logMessage("Cannot find a feature name. Line !line.", array('!line' => $i), TRIPAL_WARNING);
           }
         }
 
         // Get the feature uniquename if a regular expression is provided.
         if ($re_uname) {
           if (!preg_match("/$re_uname/", $defline, $matches)) {
-            $this->logMessage("Regular expression for the feature unique name finds nothing. Line %line.",
-              array('%line' => $i), TRIPAL_ERROR);
+            $this->logMessage("Regular expression for the feature unique name finds nothing. Line !line.",
+              array('!line' => $i), TRIPAL_ERROR);
           }
           $uname = trim($matches[1]);
         }
@@ -500,8 +501,8 @@ class FASTAImporter extends TripalImporter {
             $uname = trim($matches[1]);
           }
           else {
-            $this->logMessage("Cannot find a feature unique name. Line %line.",
-              array('%line' => $i), TRIPAL_ERROR);
+            $this->logMessage("Cannot find a feature unique name. Line !line.",
+              array('!line' => $i), TRIPAL_ERROR);
           }
         }
 
@@ -509,7 +510,7 @@ class FASTAImporter extends TripalImporter {
         preg_match("/$re_accession/", $defline, $matches);
         if (strlen($matches[1]) > $dbxref_tbl['fields']['accession']['length']) {
           $this->logMessage("Regular expression retrieves an accession too long for the feature name. " .
-            "Cannot add cross reference. Line %line.", array('%line' => $i), TRIPAL_WARNING);
+            "Cannot add cross reference. Line !line.", array('!line' => $i), TRIPAL_WARNING);
         }
         else {
           $accession = trim($matches[1]);
@@ -577,8 +578,8 @@ class FASTAImporter extends TripalImporter {
       $results = chado_select_record('feature', array('feature_id'
       ), $values);
       if (count($results) > 1) {
-        $this->logMessage("Multiple features exist with the name '%name' of type '%type' for the organism.  skipping",
-          array('%name' => $name,'%type' => $type), TRIPAL_ERROR);
+        $this->logMessage("Multiple features exist with the name '!name' of type '!type' for the organism.  skipping",
+          array('!name' => $name,'!type' => $type), TRIPAL_ERROR);
         return 0;
       }
       if (count($results) == 1) {
@@ -596,8 +597,8 @@ class FASTAImporter extends TripalImporter {
 
       $results = chado_select_record('feature', array('feature_id'), $values);
       if (count($results) > 1) {
-        $this->logMessage("Multiple features exist with the name '%name' of type '%type' for the organism.  skipping",
-          array('%name' => $name,'%type' => $type), TRIPAL_WARNING);
+        $this->logMessage("Multiple features exist with the name '!name' of type '!type' for the organism.  skipping",
+          array('!name' => $name,'!type' => $type), TRIPAL_WARNING);
         return 0;
       }
       if (count($results) == 1) {
@@ -606,8 +607,8 @@ class FASTAImporter extends TripalImporter {
 
       // If the feature exists but this is an "insert only" then skip.
       if ($feature and (strcmp($method, 'Insert only') == 0)) {
-        $this->logMessage("Feature already exists '%name' ('%uname') while matching on %type. Skipping insert.",
-          array('%name' => $name,'%uname' => $uname,'%type' => drupal_strtolower($match_type)), TRIPAL_WARNING);
+        $this->logMessage("Feature already exists '!name' ('!uname') while matching on !type. Skipping insert.",
+          array('!name' => $name,'!uname' => $uname,'!type' => drupal_strtolower($match_type)), TRIPAL_WARNING);
         return 0;
       }
     }
@@ -632,8 +633,8 @@ class FASTAImporter extends TripalImporter {
       );
       $success = chado_insert_record('feature', $values);
       if (!$success) {
-        $this->logMessage("Failed to insert feature '%name (%uname)'", array(
-          '%name' => $name,'%uname' => $numane), TRIPAL_ERROR);
+        $this->logMessage("Failed to insert feature '!name (!uname)'", array(
+          '!name' => $name,'!uname' => $numane), TRIPAL_ERROR);
         return 0;
       }
 
@@ -649,8 +650,8 @@ class FASTAImporter extends TripalImporter {
         $feature = $results[0];
       }
       else {
-        $this->logMessage("Failed to retreive newly inserted feature '%name (%uname)'", array(
-          '%name' => $name,'%uname' => $numane), TRIPAL_ERRORR);
+        $this->logMessage("Failed to retreive newly inserted feature '!name (!uname)'", array(
+          '!name' => $name,'!uname' => $numane), TRIPAL_ERRORR);
         return 0;
       }
 
@@ -660,8 +661,8 @@ class FASTAImporter extends TripalImporter {
 
     // if we don't have a feature and the user wants to do an update then fail
     if (!$feature and (strcmp($method, 'Update only') == 0 or strcmp($method, 'Insert and update') == 0)) {
-      $this->logMessage("Failed to find feature '%name' ('%uname') while matching on " . drupal_strtolower($match_type) . ".",
-          array('%name' => $name,'%uname' => $uname), TRIPAL_ERROR);
+      $this->logMessage("Failed to find feature '!name' ('!uname') while matching on " . drupal_strtolower($match_type) . ".",
+          array('!name' => $name,'!uname' => $uname), TRIPAL_ERROR);
       return 0;
     }
 
@@ -686,9 +687,9 @@ class FASTAImporter extends TripalImporter {
           );
           $results = chado_select_record('feature', array('feature_id'), $values);
           if (count($results) > 0) {
-            $this->logMessage("Cannot update the feature '%name' with a uniquename of '%uname' and type of '%type' as it " .
+            $this->logMessage("Cannot update the feature '!name' with a uniquename of '!uname' and type of '!type' as it " .
               "conflicts with an existing feature with the same uniquename and type.",
-              array('%name' => $name,'%uname' => $uname,'%type' => $type), TRIPAL_ERROR);
+              array('!name' => $name,'!uname' => $uname,'!type' => $type), TRIPAL_ERROR);
             return 0;
           }
 
@@ -703,8 +704,8 @@ class FASTAImporter extends TripalImporter {
           // perform the update
           $success = chado_update_record('feature', $match, $values);
           if (!$success) {
-            $this->logMessage("Failed to update feature '%name' ('%name')",
-              array('%name' => $name,'%uiname' => $uname), TRIPAL_ERROR);
+            $this->logMessage("Failed to update feature '!name' ('!name')",
+              array('!name' => $name,'!uiname' => $uname), TRIPAL_ERROR);
             return 0;
           }
         }
@@ -724,8 +725,8 @@ class FASTAImporter extends TripalImporter {
           );
           $success = chado_update_record('feature', $match, $values);
           if (!$success) {
-           $this->logMessage("Failed to update feature '%name' ('%name')",
-             array('%name' => $name,'%uiname' => $uname), TRIPAL_ERROR);
+           $this->logMessage("Failed to update feature '!name' ('!name')",
+             array('!name' => $name,'!uiname' => $uname), TRIPAL_ERROR);
            return 0;
           }
         }
@@ -746,8 +747,8 @@ class FASTAImporter extends TripalImporter {
       if (count($results) == 0) {
         $success = chado_insert_record('analysisfeature', $values);
         if (!$success) {
-          $this->logMessage("Failed to associate analysis and feature '%name' ('%name')",
-            array('%name' => $name,'%uname' => $uname), TRIPAL_ERROR);
+          $this->logMessage("Failed to associate analysis and feature '!name' ('!name')",
+            array('!name' => $name,'!uname' => $uname), TRIPAL_ERROR);
           return 0;
         }
       }
@@ -766,8 +767,8 @@ class FASTAImporter extends TripalImporter {
       if (count($results) == 0) {
         $results = chado_insert_record('dbxref', $values);
         if (!$results) {
-          $this->logMessage("Failed to add database accession '%accession'",
-            array('%accession' => $accession), TRIPAL_ERROR);
+          $this->logMessage("Failed to add database accession '!accession'",
+            array('!accession' => $accession), TRIPAL_ERROR);
             return 0;
           }
           $results = chado_select_record('dbxref', array('dbxref_id'), $values);
@@ -775,8 +776,8 @@ class FASTAImporter extends TripalImporter {
             $dbxref = $results[0];
           }
         else {
-          $this->logMessage("Failed to retreive newly inserted dbxref '%name (%uname)'",
-            array('%name' => $name,'%uname' => $numane), TRIPAL_ERROR);
+          $this->logMessage("Failed to retreive newly inserted dbxref '!name (!uname)'",
+            array('!name' => $name,'!uname' => $numane), TRIPAL_ERROR);
             return 0;
         }
       }
@@ -793,8 +794,8 @@ class FASTAImporter extends TripalImporter {
       if (count($results) == 0) {
         $success = chado_insert_record('feature_dbxref', $values);
         if (!$success) {
-          $this->logMessage("Failed to add associate database accession '%accession' with feature",
-            array('%accession' => $accession), TRIPAL_ERROR);
+          $this->logMessage("Failed to add associate database accession '!accession' with feature",
+            array('!accession' => $accession), TRIPAL_ERROR);
           return 0;
         }
       }
@@ -805,8 +806,8 @@ class FASTAImporter extends TripalImporter {
       $values = array('organism_id' => $organism_id,'uniquename' => $parent, 'type_id' => $parentcvterm->cvterm_id);
       $results = chado_select_record('feature', array('feature_id'), $values);
       if (count($results) != 1) {
-        $this->logMessage("Cannot find a unique fature for the parent '%parent' of type '%type' for the feature.",
-          array('%parent' => $parent,'%type' => $parent_type), TRIPAL_ERROR);
+        $this->logMessage("Cannot find a unique fature for the parent '!parent' of type '!type' for the feature.",
+          array('!parent' => $parent,'!type' => $parent_type), TRIPAL_ERROR);
           return 0;
       }
       $parent_feature = $results[0];
@@ -821,8 +822,8 @@ class FASTAImporter extends TripalImporter {
       if (count($results) == 0) {
         $success = chado_insert_record('feature_relationship', $values);
         if (!$success) {
-          $this->logMessage("Failed to add associate database accession '%accession' with feature",
-            array('%accession' => $accession), TRIPAL_ERROR);
+          $this->logMessage("Failed to add associate database accession '!accession' with feature",
+            array('!accession' => $accession), TRIPAL_ERROR);
           return 0;
         }
       }

+ 6 - 5
tripal_chado/includes/TripalImporter/GFF3Importer.inc

@@ -294,10 +294,11 @@ class GFF3Importer extends TripalImporter {
   /**
    * @see TripalImporter::run()
    */
-  public function run($details) {
-    $arguments = $details->arguments;
+  public function run() {
+
+    $arguments = $this->arguments['run_args'];
+    $file_path = $this->arguments['file']['file_path'];
 
-    $gff_file = $arguments['file_path'];
     $organism_id = $arguments['organism_id'];
     $analysis_id = $arguments['analysis_id'];
     $add_only = $arguments['add_only'];
@@ -315,7 +316,7 @@ class GFF3Importer extends TripalImporter {
     $re_mrna = $arguments['re_mrna'];
     $re_protein = $arguments['re_protein'];
 
-    $this->load($gff_file, $organism_id, $analysis_id,
+    $this->load($file_path, $organism_id, $analysis_id,
         $add_only, $update, $refresh, $remove, $use_transaction,
         $target_organism_id, $target_type,  $create_target,
         $start_line, $landmark_type, $alt_id_attr,  $create_organism,
@@ -530,7 +531,7 @@ class GFF3Importer extends TripalImporter {
       // get the columns
       $cols = explode("\t", $line);
       if (sizeof($cols) != 9) {
-        throw new Excpetion(t('Improper number of columns on line %line_num', array('%line_num' => $line_num)));
+        throw new Exception(t('Improper number of columns on line %line_num', array('%line_num' => $line_num)));
       }
 
       // get the column values

+ 47 - 40
tripal_chado/includes/TripalImporter/OBOImporter.inc

@@ -366,12 +366,23 @@ class OBOImporter extends TripalImporter {
    *   The following arguments are supported:
    *     - obo_id:  (required) The ID of the ontology to be imported.
    */
-  public function run($details) {
+  public function run() {
+
+    $arguments = $this->arguments['run_args'];
 
-    $arguments = $details->arguments;
     $obo_id = $arguments['obo_id'];
 
-    $this->loadOBO_v1_2_id($obo_id);
+    // Make sure the $obo_id is valid
+    $obo = db_select('tripal_cv_obo', 'tco')
+      ->fields('tco')
+      ->condition('obo_id', $obo_id)
+      ->execute()
+      ->fetchObject();
+    if (!$obo) {
+      throw new Exception("Invalid OBO ID provided: '$obo_id'.");
+    }
+
+    $this->loadOBO_v1_2_id($obo);
   }
 
   /**
@@ -384,11 +395,7 @@ class OBOImporter extends TripalImporter {
    *   An obo_id from the tripal_cv_obo file that specifies which OBO file to import
    * @ingroup tripal_obo_loader
    */
-  private function loadOBO_v1_2_id($obo_id) {
-
-    // Get the OBO reference.
-    $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
-    $obo = db_query($sql, array(':obo_id' => $obo_id))->fetchObject();
+  private function loadOBO_v1_2_id($obo) {
 
     // Convert the module name to the real path if present
     if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
@@ -480,7 +487,7 @@ class OBOImporter extends TripalImporter {
     $url_fh = fopen($url, "r");
     $obo_fh = fopen($temp, "w");
     if (!$url_fh) {
-      throw new Excpetion("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
+      throw new Exception("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
           " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
           " specify the location of the file on your server.");
 
@@ -566,7 +573,7 @@ class OBOImporter extends TripalImporter {
     if (array_key_exists('default-namespace', $header)) {
       $defaultcv = tripal_insert_cv($header['default-namespace'][0], '');
       if (!$defaultcv) {
-        throw new Excpetion('Cannot add namespace ' . $header['default-namespace'][0]);
+        throw new Exception('Cannot add namespace ' . $header['default-namespace'][0]);
       }
       $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
     }
@@ -578,12 +585,12 @@ class OBOImporter extends TripalImporter {
       if (array_key_exists('ontology', $header)) {
         $defaultcv = tripal_insert_cv(strtoupper($header['ontology'][0]), '');
         if (!$defaultcv) {
-          throw new Excpetion('Cannot add namespace ' . strtoupper($header['ontology'][0]));
+          throw new Exception('Cannot add namespace ' . strtoupper($header['ontology'][0]));
         }
         $newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
       }
       else {
-        throw new Excpetion("Could not find a namespace for this OBO file.");
+        throw new Exception("Could not find a namespace for this OBO file: $file");
       }
       $this->logMessage("This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go.  Instead, those terms will be placed in the '%vocab' vocabulary.",
           array('%vocab' => $defaultcv->name), TRIPAL_WARNING);
@@ -685,7 +692,7 @@ class OBOImporter extends TripalImporter {
 
       // add/update this term
       if (!$this->processTerm($term, $defaultcv->name, 0, $newcvs, $default_db)) {
-        throw new Excpetion("Failed to process terms from the ontology");
+        throw new Exception("Failed to process terms from the ontology");
       }
 
       $i++;
@@ -713,7 +720,7 @@ class OBOImporter extends TripalImporter {
 
     // make sure we have a namespace for this term
     if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
-      throw new Excpetion("Cannot add the term: no namespace defined. " . $term['id'][0]);
+      throw new Exception("Cannot add the term: no namespace defined. " . $term['id'][0]);
     }
 
     // construct the term array for sending to the tripal_chado_add_cvterm function
@@ -741,7 +748,7 @@ class OBOImporter extends TripalImporter {
     // add the cvterm
     $cvterm = tripal_insert_cvterm($t, array('update_existing' => TRUE));
     if (!$cvterm) {
-      throw new Excpetion("Cannot add the term " . $term['id'][0]);
+      throw new Exception("Cannot add the term " . $term['id'][0]);
     }
 
     if (array_key_exists('namespace', $term)) {
@@ -755,7 +762,7 @@ class OBOImporter extends TripalImporter {
     if (array_key_exists('alt_id', $term)) {
       foreach ($term['alt_id'] as $alt_id) {
         if (!$this->addCvtermDbxref($cvterm, $alt_id)) {
-          throw new Excpetion("Cannot add alternate id $alt_id");
+          throw new Exception("Cannot add alternate id $alt_id");
         }
       }
     }
@@ -766,7 +773,7 @@ class OBOImporter extends TripalImporter {
     // add synonyms for this cvterm
     if (array_key_exists('synonym', $term)) {
       if (!$this->addSynonym($term, $cvterm)) {
-        throw new Excpetion("Cannot add synonyms");
+        throw new Exception("Cannot add synonyms");
       }
     }
 
@@ -793,7 +800,7 @@ class OBOImporter extends TripalImporter {
       }
 
       if (!$this->addSynonym($term, $cvterm)) {
-        throw new Excpetion("Cannot add/update synonyms");
+        throw new Exception("Cannot add/update synonyms");
       }
     }
 
@@ -803,7 +810,7 @@ class OBOImporter extends TripalImporter {
       $j = 0;
       foreach ($comments as $comment) {
         if (!$this->addCvtermProp($cvterm, 'comment', $comment, $j)) {
-          throw new Excpetion("Cannot add/update cvterm property");
+          throw new Exception("Cannot add/update cvterm property");
         }
         $j++;
       }
@@ -813,7 +820,7 @@ class OBOImporter extends TripalImporter {
     if (array_key_exists('xref', $term)) {
       foreach ($term['xref'] as $xref) {
         if (!$this->addCvtermDbxref($cvterm, $xref)) {
-          throw new Excpetion("Cannot add/update cvterm database reference (dbxref).");
+          throw new Exception("Cannot add/update cvterm database reference (dbxref).");
         }
       }
     }
@@ -821,14 +828,14 @@ class OBOImporter extends TripalImporter {
     if (array_key_exists('xref_analog', $term)) {
       foreach ($term['xref_analog'] as $xref) {
         if (!$this->addCvtermDbxref($cvterm, $xref)) {
-          throw new Excpetion("Cannot add/update cvterm database reference (dbxref).");
+          throw new Exception("Cannot add/update cvterm database reference (dbxref).");
         }
       }
     }
     if (array_key_exists('xref_unk', $term)) {
       foreach ($term['xref_unk'] as $xref) {
         if (!$this->addCvtermDbxref($cvterm, $xref)) {
-          throw new Excpetion("Cannot add/update cvterm database reference (dbxref).");
+          throw new Exception("Cannot add/update cvterm database reference (dbxref).");
         }
       }
     }
@@ -837,7 +844,7 @@ class OBOImporter extends TripalImporter {
     if (array_key_exists('is_a', $term)) {
       foreach ($term['is_a'] as $is_a) {
         if (!$this->addRelationship($cvterm, $defaultcv, 'is_a', $is_a, $is_relationship, $default_db)) {
-          throw new Excpetion("Cannot add relationship is_a: $is_a");
+          throw new Exception("Cannot add relationship is_a: $is_a");
         }
       }
     }
@@ -868,7 +875,7 @@ class OBOImporter extends TripalImporter {
         continue;
         }*/
         if (!$this->addRelationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
-          throw new Excpetion("Cannot add relationship $rel: $object");
+          throw new Exception("Cannot add relationship $rel: $object");
         }
       }
     }
@@ -935,14 +942,14 @@ class OBOImporter extends TripalImporter {
       );
       $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
       if (!$relcvterm) {
-        throw new Excpetion("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
+        throw new Exception("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
       }
     }
 
     // get the object term
     $oterm = $this->getTerm($objname);
     if (!$oterm) {
-      throw new Excpetion("Could not find object term $objname\n");
+      throw new Exception("Could not find object term $objname\n");
     }
 
     $objterm = array();
@@ -967,7 +974,7 @@ class OBOImporter extends TripalImporter {
 
     $objcvterm = tripal_insert_cvterm($objterm, array('update_existing' => TRUE));
     if (!$objcvterm) {
-      throw new Excpetion("Cannot add cvterm " . $oterm['name'][0]);
+      throw new Exception("Cannot add cvterm " . $oterm['name'][0]);
     }
 
     // check to see if the cvterm_relationship already exists, if not add it
@@ -981,7 +988,7 @@ class OBOImporter extends TripalImporter {
       $options = array('return_record' => FALSE);
       $success = chado_insert_record('cvterm_relationship', $values, $options);
       if (!$success) {
-        throw new Excpetion("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
+        throw new Exception("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
       }
     }
 
@@ -1057,7 +1064,7 @@ class OBOImporter extends TripalImporter {
           );
           $syntype = tripal_insert_cvterm($term, array('update_existing' => TRUE));
           if (!$syntype) {
-            throw new Excpetion("Cannot add synonym type: internal:$scope");
+            throw new Exception("Cannot add synonym type: internal:$scope");
           }
         }
 
@@ -1076,7 +1083,7 @@ class OBOImporter extends TripalImporter {
           $options = array('return_record' => FALSE);
           $success = chado_insert_record('cvtermsynonym', $values, $options);
           if (!$success) {
-            throw new Excpetion("Failed to insert the synonym for term: $name ($def)");
+            throw new Exception("Failed to insert the synonym for term: $name ($def)");
           }
         }
 
@@ -1207,7 +1214,7 @@ class OBOImporter extends TripalImporter {
       );
       chado_insert_record('tripal_obo_temp', $values);
       if (!$success) {
-        throw new Excpetion("Cannot insert stanza into temporary table.");
+        throw new Exception("Cannot insert stanza into temporary table.");
       }
       $this->setItemsHandled($num_read);
     }
@@ -1232,7 +1239,7 @@ class OBOImporter extends TripalImporter {
     $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
 
     if (!$accession) {
-      throw new Excpetion("Cannot add a dbxref without an accession: '$xref'");
+      throw new Exception("Cannot add a dbxref without an accession: '$xref'");
     }
 
     // if the xref is a database link, handle that specially
@@ -1244,13 +1251,13 @@ class OBOImporter extends TripalImporter {
     // add the database
     $db = tripal_insert_db(array('name' => $dbname));
     if (!$db) {
-      throw new Excpetion("Cannot find database '$dbname' in Chado.");
+      throw new Exception("Cannot find database '$dbname' in Chado.");
     }
 
     // now add the dbxref
     $dbxref = $this->addDbxref($db->db_id, $accession, '', $description);
     if (!$dbxref) {
-      throw new Excpetion("Cannot find or add the database reference (dbxref)");
+      throw new Exception("Cannot find or add the database reference (dbxref)");
     }
 
     // finally add the cvterm_dbxref but first check to make sure it exists
@@ -1263,7 +1270,7 @@ class OBOImporter extends TripalImporter {
       $ins_options = array('return_record' => FALSE);
       $result = chado_insert_record('cvterm_dbxref', $values, $ins_options);
       if (!$result) {
-        throw new Excpetion("Cannot add cvterm_dbxref: $xref");
+        throw new Exception("Cannot add cvterm_dbxref: $xref");
       }
     }
 
@@ -1289,7 +1296,7 @@ class OBOImporter extends TripalImporter {
     // make sure the 'cvterm_property_type' CV exists
     $cv = tripal_insert_cv('cvterm_property_type', '');
     if (!$cv) {
-      throw new Excpetion("Cannot add/find cvterm_property_type cvterm");
+      throw new Exception("Cannot add/find cvterm_property_type cvterm");
     }
 
     // get the property type cvterm.  If it doesn't exist then we want to add it
@@ -1309,7 +1316,7 @@ class OBOImporter extends TripalImporter {
       );
       $cvproptype = tripal_insert_cvterm($term, array('update_existing' => FALSE));
       if (!$cvproptype) {
-        throw new Excpetion("Cannot add cvterm property: internal:$property");
+        throw new Exception("Cannot add cvterm property: internal:$property");
       }
     }
     else {
@@ -1321,7 +1328,7 @@ class OBOImporter extends TripalImporter {
       $values = array('cvterm_id' => $cvterm->cvterm_id);
       $success = chado_delete_record('cvtermprop', $values);
       if (!$success) {
-        throw new Excpetion("Could not remove existing properties to update property $property for term\n");
+        throw new Exception("Could not remove existing properties to update property $property for term\n");
       }
     }
 
@@ -1335,7 +1342,7 @@ class OBOImporter extends TripalImporter {
     $options = array('return_record' => FALSE);
     $result = chado_insert_record('cvtermprop', $values, $options);
     if (!$result) {
-      throw new Excpetion("Could not add property $property for term\n");
+      throw new Exception("Could not add property $property for term\n");
     }
     return TRUE;
   }
@@ -1372,7 +1379,7 @@ class OBOImporter extends TripalImporter {
       $ins_options = array('return_record' => FALSE);
       $result = chado_insert_record('dbxref', $ins_values, $ins_options);
       if (!$result) {
-        throw new Excpetion("Failed to insert the dbxref record $accession");
+        throw new Exception("Failed to insert the dbxref record $accession");
       }
       $result = chado_select_record('dbxref', array('dbxref_id'), $values);
     }

+ 18 - 10
tripal_chado/includes/setup/tripal_chado.setup.inc

@@ -74,37 +74,43 @@ function tripal_chado_load_ontologies() {
       'name' => 'Relationship Ontology (legacy)',
       'path' => '{tripal_chado}/files/legacy_ro.obo',
       'auto_load' => FALSE,
-      'cv_name' => 'ro'
+      'cv_name' => 'ro',
+      'db_name' => 'RO',
     ),
     array(
       'name' => 'Gene Ontology',
       'path' => 'http://purl.obolibrary.org/obo/go.obo',
       'auto_load' => FALSE,
-      'cv_name' => 'cellualar_component'
+      'cv_name' => 'cellualar_component',
+      'db_name' => 'GO',
     ),
     array(
       'name' => 'Taxonomic Rank',
       'path' => 'http://purl.obolibrary.org/obo/taxrank.obo',
       'auto_load' => TRUE,
-      'cv_name' => 'taxonomic_rank'
+      'cv_name' => 'taxonomic_rank',
+      'db_name' => 'TAXRANK'
     ),
     array(
       'name' => 'Tripal Contact',
       'path' => '{tripal_chado}/files/tcontact.obo',
       'auto_load' => TRUE,
-      'cv_name' => 'tripal_contact'
+      'cv_name' => 'tripal_contact',
+      'db_name' => 'TCONTACT'
     ),
     array(
       'name' => 'Tripal Publication',
       'path' => '{tripal_chado}/files/tpub.obo',
       'auto_load' => TRUE,
-      'cv_name' => 'tripal_pub'
+      'cv_name' => 'tripal_pub',
+      'db_name' => 'TPUB',
     ),
      array(
        'name' => 'Sequence Ontology',
        'path' => 'http://purl.obolibrary.org/obo/so.obo',
        'auto_load' => TRUE,
-       'cv_name' => 'sequence'
+       'cv_name' => 'sequence',
+       'db_name' => 'SO',
      ),
 
   );
@@ -115,10 +121,12 @@ function tripal_chado_load_ontologies() {
     if ($ontologies[$i]['auto_load'] == TRUE) {
       // Only load ontologies that are not already in the cv table.
       $cv = tripal_get_cv(array('name' => $ontologies[$i]['cv_name']));
-      if (!$cv) {
-        print "Loading ontology: " . $ontologies[$i]['name'] . "...\n";
-        $obo = new OBOImporter();
-        $obo->run(array('obo_id' => $obo_id));
+      $db = tripal_get_db(array('name' => $ontologies[$i]['db_name']));
+      if (!$cv or !$db) {
+        print "Loading ontology: " . $ontologies[$i]['name'] . " ($obo_id)...\n";
+        $obo_importer = new OBOImporter();
+        $obo_importer->create(array('obo_id' => $obo_id));
+        $obo->run();
       }
       else {
         print "Ontology already loaded (skipping): " . $ontologies[$i]['name'] . "...\n";