Browse Source

Updated loader to handle compressed files. fixed bug in the database editor.
"

Stephen Ficklin 7 years ago
parent
commit
534f0b3040

+ 1 - 1
legacy/tripal_cv/theme/templates/tripal_cv_help.tpl.php

@@ -15,7 +15,7 @@
    users access to view content and create a special role for creating, editing and other administrative tasks.</p></li>
 
       <li><p><b>Loading of Ontologies/Controlled Vocabularies</b>: You can access this loader at <?php
-        print l('Admin->Tripal Management->Tripal CV->Load Ontology With OBO File', 'admin/tripal/tripal_cv/obo_loader')
+        print l('Admin->Tripal Management->Tripal CV->Load Ontology With OBO File', 'admin/tripal/loaders/chado_obo_loader')
         ?>. This loader allows you to choose from a list of common ontologies or
         enter the URL or location to an OBO file. Even the list of common
         ontologies is using a URL ensuring you get the most up to date ontology.</p>

+ 38 - 2
tripal/includes/TripalImporter.inc

@@ -389,8 +389,16 @@ class TripalImporter {
         $file_remote = $this->arguments['file']['file_remote'];
         $this->logMessage('Download file: !file_remote...', array('!file_remote' => $file_remote));
 
+        // If this file is compressed then keepthe .gz extension so we can
+        // uncompress it.
+        $ext = '';
+        if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_remote'])) {
+          $ext = '.gz';
+        }
         // Create a temporary file.
-        $temp = tempnam("temporary://", 'import_');
+        $temp = tempnam("temporary://", 'import_') . $ext;
+        $this->logMessage("Saving as: !file", array('!file' => $temp));
+
         $url_fh = fopen($file_remote, "r");
         $tmp_fh = fopen($temp, "w");
         if (!$url_fh) {
@@ -402,11 +410,39 @@ class TripalImporter {
         while (!feof($url_fh)) {
           fwrite($tmp_fh, fread($url_fh, 255), 255);
         }
-
         // Set the path to the file for the importer to use.
         $this->arguments['file']['file_path'] = $temp;
         $this->is_prepared = TRUE;
       }
+
+      // Is this file compressed?  If so, then uncompress it
+      $matches = array();
+      if (preg_match('/^(.*?)\.gz$/', $this->arguments['file']['file_path'], $matches)) {
+        $this->logMessage("Uncompressing: !file", array('!file' => $this->arguments['file']['file_path']));
+        $buffer_size = 4096;
+        $new_file_path = $matches[1];
+        $gzfile = gzopen($this->arguments['file']['file_path'], 'rb');
+        $out_file = fopen($new_file_path, 'wb');
+        if (!$out_file) {
+          throw new Exception("Cannot uncompress file: new temporary file, '$new_file_path', cannot be created.");
+        }
+
+        // Keep repeating until the end of the input file
+        while (!gzeof($gzfile)) {
+          // Read buffer-size bytes
+          // Both fwrite and gzread and binary-safe
+          fwrite($out_file, gzread($gzfile, $buffer_size));
+        }
+
+        // Files are done, close files
+        fclose($out_file);
+        gzclose($gzfile);
+
+        // Now remove the .gz file and reset the file_path to the new
+        // uncompressed version.
+        unlink($this->arguments['file']['file_path']);
+        $this->arguments['file']['file_path'] = $new_file_path;
+      }
     }
     catch (Exception $e){
       throw new Exception('Cannot prepare the importer: ' .  $e->getMessage());

+ 1 - 1
tripal/tripal.module

@@ -1096,7 +1096,7 @@ function tripal_field_display_TripalEntity_alter(&$display, $context){
  * @param $element
  *   The field group object.
  * @param $children
- *   An array of field names that are cpntained in the field group
+ *   An array of field names that are contained in the field group.
  */
 function tripal_field_group_table_rows_alter(&$element, &$children) {
 

+ 6 - 2
tripal_chado/api/tripal_chado.query.api.inc

@@ -1321,8 +1321,12 @@ function chado_select_record($table, $columns, $values, $options = NULL) {
     // Require the field be in the table description.
     if (!array_key_exists($field, $table_desc['fields'])) {
       tripal_report_error('tripal_chado', TRIPAL_ERROR,
-        'chado_select_record: The field "%field" does not exist for the table "%table".  Cannot perform query. Values: %array',
-        array('%field' => $field, '%table' => $table, '%array' => print_r($values, 1)),
+        'chado_select_record: The field "%field" does not exist in the table "%table".  Cannot perform query. Values: %array. Fields: %fields',
+        array(
+          '%field' => $field,
+          '%table' => $table,
+          '%array' => print_r($values, 1),
+          '%fields' => print_r($table_desc['fields'], 1)),
         array('print' => $print_errors)
       );
       return array();

+ 19 - 25
tripal_chado/includes/TripalImporter/OBOImporter.inc

@@ -553,11 +553,9 @@ class OBOImporter extends TripalImporter {
   private function loadOBO_v1_2($file, &$newcvs) {
 
     $header = array();
-
-    // make sure our temporary table exists
     $ret = array();
 
-    // empty the temp table
+    // Empty the temp table.
     $sql = "DELETE FROM {tripal_obo_temp}";
     chado_query($sql);
 
@@ -566,7 +564,7 @@ class OBOImporter extends TripalImporter {
     // parse the obo file
     $default_db = $this->parse($file, $header);
 
-    // add the CV for this ontology to the database.  The v1.2 definition
+    // Add the CV for this ontology to the database.  The v1.2 definition
     // specifies a 'default-namespace' to be used if a 'namespace' is not
     // present for each stanza.  Some ontologies have adopted the v1.4 method
     // in their v1.2 files and not including it.
@@ -577,10 +575,10 @@ class OBOImporter extends TripalImporter {
       }
       $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
     }
-    // if the 'default-namespace' is missing
+    // If the 'default-namespace' is missing.
     else {
 
-      // look to see if an 'ontology' key is present.  It is part of the v1.4
+      // Look to see if an 'ontology' key is present.  It is part of the v1.4
       // specification so it shouldn't be in the file, but just in case
       if (array_key_exists('ontology', $header)) {
         $defaultcv = tripal_insert_cv(strtoupper($header['ontology'][0]), '');
@@ -597,11 +595,11 @@ class OBOImporter extends TripalImporter {
           "should go.  Instead, those terms will be placed in the '!vocab' vocabulary.",
           array('!vocab' => $defaultcv->name), TRIPAL_WARNING);
     }!
-    // add any typedefs to the vocabulary first
+    // Add any typedefs to the vocabulary first.
     $this->logMessage("Step 2: Loading type defs...");
     $this->loadTypeDefs($defaultcv, $newcvs, $default_db);
 
-    // next add terms to the vocabulary
+    // Next add terms to the vocabulary.
     $this->logMessage("Step 3: Loading terms...");
     if (!$this->processTerms($defaultcv, $newcvs, $default_db)) {
       throw new Exception('Cannot add terms from this ontology');
@@ -668,7 +666,7 @@ class OBOImporter extends TripalImporter {
 
     $i = 0;
 
-    // iterate through each term from the OBO file and add it
+    // Iterate through each term from the OBO file and add it.
     $sql = "
       SELECT * FROM {tripal_obo_temp}
       WHERE type = 'Term'
@@ -691,7 +689,7 @@ class OBOImporter extends TripalImporter {
       $term = unserialize(base64_decode($t->stanza));
       $this->setItemsHandled($i);
 
-      // add/update this term
+      // Add/update this term.
       if (!$this->processTerm($term, $defaultcv->name, 0, $newcvs, $default_db)) {
         throw new Exception("Failed to process terms from the ontology");
       }
@@ -752,6 +750,14 @@ class OBOImporter extends TripalImporter {
       throw new Exception("Cannot add the term " . $term['id'][0]);
     }
 
+    // Remove any relationships that this term already has (in case it was
+    // updated) and we'll re-add them.
+    $sql = "
+      DELETE FROM {cvterm_relationship} CVTR
+      WHERE CVTR.subject_id = :cvterm_id
+    ";
+    chado_query($sql, array(':cvterm_id' => $cvterm->cvterm_id));
+
     if (array_key_exists('namespace', $term)) {
       $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
     }
@@ -863,18 +869,6 @@ class OBOImporter extends TripalImporter {
       foreach ($term['relationship'] as $value) {
         $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
         $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
-        // The Gene Ontology uses 'has_part' for transitive relationships, but
-        // it specifically indicates that 'has_part' should not be used for
-        // grouping annotations.  Unfortunately, this means that when we
-        // try to popoulate the cvtermpath table a 'has_part' relationships
-        // will be used for exactly that purpose: to group annotations.  This
-        // doesn't seem to the be the case for other vocabularies such as the
-        // sequence ontology that uses has_part as primary relationship between
-        // terms. So, when loading the GO, we'll not include has_part
-        // relationships.
-        /*if ($rel == 'has_part' and $cvterm->dbxref_id->db_id->name == 'GO') {
-        continue;
-        }*/
         if (!$this->addRelationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
           throw new Exception("Cannot add relationship $rel: $object");
         }
@@ -917,7 +911,7 @@ class OBOImporter extends TripalImporter {
   private function addRelationship($cvterm, $defaultcv, $rel,
       $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
 
-    // make sure the relationship cvterm exists
+    // Make sure the relationship cvterm exists.
     $term = array(
       'name' => $rel,
       'id' => "$default_db:$rel",
@@ -930,7 +924,7 @@ class OBOImporter extends TripalImporter {
     $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
 
     if (!$relcvterm) {
-      // if the relationship term couldn't be found in the default_db provided
+      // If the relationship term couldn't be found in the default_db provided
       // then do on more check to find it in the relationship ontology
       $term = array(
         'name' => $rel,
@@ -947,7 +941,7 @@ class OBOImporter extends TripalImporter {
       }
     }
 
-    // get the object term
+    // Get the object term.
     $oterm = $this->getTerm($objname);
     if (!$oterm) {
       throw new Exception("Could not find object term $objname\n");

+ 5 - 0
tripal_chado/includes/tripal_chado.db.inc

@@ -151,6 +151,11 @@ function tripal_chado_add_db_form_fields(&$form, $form_state, $dbid = NULL) {
     $default_desc = $db->description;
     $default_url = $db->url;
     $default_urlprefix = $db->urlprefix;
+
+    $form['dbid'] = array(
+      '#type' => 'value',
+      '#value' => $dbid,
+    );
   }
 
   // add a fieldset for the Drupal Schema API

+ 1 - 1
tripal_chado/tripal_chado.module

@@ -644,7 +644,7 @@ function tripal_chado_menu() {
     'title' => 'Edit a Database Reference',
     'description' => 'Edit existing Database References.',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_chado_db_edit_form',6),
+    'page arguments' => array('tripal_chado_db_edit_form', 5),
     'access callback' => 'user_access',
     'access arguments' => array('administer db cross-references'),
     'file' => 'includes/tripal_chado.db.inc',

+ 0 - 4
tripal_chado/tripal_chado.views_default.inc

@@ -506,10 +506,6 @@ function tripal_chado_defaultview_admin_cvs_listing() {
     'label-1' => 'Add Vocabulary',
     'path-1' => 'admin/tripal/loaders/chado_cv/add',
   );
-  $handler->display->display_options['header']['action_links_area']['link-2'] = array(
-    'label-2' => 'Load Ontology',
-    'path-2' => 'admin/tripal/vocab/obo_loader',
-  );
   /* No results behavior: Global: Text area */
   $handler->display->display_options['empty']['text']['id'] = 'area';
   $handler->display->display_options['empty']['text']['table'] = 'views';