Browse Source

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

Lacey Sanderson 12 years ago
parent
commit
4c06e42886

+ 61 - 24
tripal_core/api/tripal_core.api.inc

@@ -990,7 +990,7 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
     // TODO: what do we do if we get to this point and we have a fk
     // relationship expected but we don't have any definition for one in the
     // table schema??
-    $version = variable_get('chado_version', '');
+    $version = tripal_core_get_chado_version(TRUE);
     $message = t("There is no foreign key relationship defined for " . $field . ".
        To define a foreign key relationship, determine the table this foreign
        key referrs to (<foreign table>) and then implement
@@ -2330,8 +2330,8 @@ function tripal_core_get_chado_tables($include_custom = NULL) {
   return $tables;
 }
 /**
- * Queries the database to detrmine the Chado version and sets
- * a Drupal variable named 'chado_version'.  
+ * Sets a Drupal variable with the current version of Chado.  This variable
+ * can then be queried later using the tripal_core_get_chado_Version   
  *
  * @returns
  *   The version of Chado
@@ -2351,11 +2351,7 @@ function tripal_core_set_chado_version() {
   $previous_db = tripal_db_set_active('chado');
   $prop_exists = db_table_exists('chadoprop');
   tripal_db_set_active($previous_db);
-  if (!$prop_exists) {
-     drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11.  If you are certain this is v1.11 
-       of if Chado was installed using Tripal v0.3.1b then all is well. If not please check the version and either upgrade to 
-       v1.11 or a later version"),'warning');
-     variable_set('chado_version', "1.11 or older");
+  if (!$prop_exists) {     
      return "1.11 or older";
   }
   
@@ -2374,19 +2370,68 @@ function tripal_core_set_chado_version() {
   // if we don't have a version in the chadoprop table then it must be 
   // v1.11 or older 
   if (!$v->value) {
-     drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11.  If you are certain this is v1.11 
-       of if Chado was installed using Tripal v0.3.1b then all is well. If not please check the version and either upgrade to 
-       v1.11 or a later version"),'warning');
      variable_set('chado_version', "1.11 or older");  
      return "1.11 or older";
   }
   
-  if($v->value != '1.11' and $v->value != '1.2'){  
-     drupal_set_message(t("WARNING: This currently installed version of Chado is not fully supported."),'warning');  
-  }
-  variable_set('chado_version', "1.11 or older");
+  variable_set('chado_version', $v->value);
   return $v->value;
 }
+/**
+ * Returns the version number of the currently installed Chado instance.
+ * It can return the real or effective version.
+ *
+ * @param $exact
+ *   Set this argument to 1 to retrieve the exact version that is installed.
+ *   Otherwise, this function will set the version to the nearest 'tenth'.
+ *   Chado versioning numbers in the hundreds represent changes to the 
+ *   software and not the schema.  Changes in the tenth's represent changes
+ *   in the schema.
+ *
+ * @param $warn_if_unsupported
+ *   If the currently installed version of Chado is not supported by Tripal
+ *   the generatea a Drupal warning.
+ *
+ * @returns
+ *   The version of Chado
+ *
+ * @ingroup tripal_core_api
+ */
+function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FALSE) {
+   // first get the chado version that is installed
+   $exact_version = variable_get('chado_version', '');
+   if (!$exact_version) {
+     $exact_version = tripal_core_set_chado_version();
+   }
+     
+   // Tripal only supports v1.11 or newer.. really this is the same as v1.1
+   // but at the time the v1.11 schema API was written we didn't know that so
+   // we'll return the version 1.11 so the schema API will work.
+   if (strcmp($exact_version, '1.11 or older') == 0) {
+     $exact_version = "1.11";
+     if($warn_if_unsupported){
+       drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.1.  If you are certain this is v1.1 
+         of if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.1 or later"),
+         'warning');
+     }
+   }
+      
+   // if not returing an exact version, return the version to the nearest 10th.
+   // return 1.2 for all versions of 1.2x
+   $effective_version = $exact_version;
+   if (preg_match('/^1\.2\d+$/', $effective_version)){
+      $effective_version = "1.2";
+   }
+   if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2)) {
+     drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."),'warning');
+   }
+   // if the callee has requested the exact version then return it
+   if ($exact) {
+      return $exact_version;
+   }
+
+   return $effective_version;
+}
 /**
  * Retrieves the chado tables Schema API array.  
  *
@@ -2402,16 +2447,8 @@ function tripal_core_set_chado_version() {
 function tripal_core_get_chado_table_schema($table) {
 
    // first get the chado version that is installed
-   $v = variable_get('chado_version', '');
-   if (!$v) {
-     $v = tripal_core_set_chado_version();
-   }
+   $v = tripal_core_get_chado_version();
    
-   // Tripal only supports v1.11 or newer
-   if (strcmp($v, '1.11 or older') == 0) {
-      $v = "1.11";
-   }
-
    // get the table array from the proper chado schema
    $v = preg_replace("/\./", "_", $v); // reformat version for hook name
    $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);

+ 65 - 20
tripal_core/includes/chado_install.php

@@ -6,17 +6,23 @@
  */
 
 /**
- * Load Chado Schema 1.11 Form
+ * Load Chado Schema Form
  *
  * @ingroup tripal_core
  */
 function tripal_core_chado_load_form() {
 
-  $version = tripal_core_set_chado_version();
+  // we want to force the version of Chado to be set properly
+  $real_version = tripal_core_set_chado_version();
+  
+  // get the effective version.  Pass true as second argument
+  // to warn the user if the current version is not compatible
+  $version = tripal_core_get_chado_version(FALSE, TRUE);
+  
   $form['current_version'] = array(
     '#type' => 'item',
     '#title' => t("Current installed version of Chado"),
-    '#value' => $version,
+    '#value' => $real_version,
   );
 
   $form['action_to_do'] = array(
@@ -73,19 +79,31 @@ function tripal_core_chado_load_form_submit($form, &$form_state) {
  */
 function tripal_core_install_chado($action) {
 
-  $vsql = "INSERT INTO chadoprop (type_id, value) VALUES  "
-        ."((SELECT cvterm_id "
-        ."FROM cvterm CVT "
-        ." INNER JOIN cv CV on CVT.cv_id = CV.cv_id "
-        ."WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), "
-        ."'%s') ";
+  $vsql = "INSERT INTO chadoprop (type_id, value) VALUES  " .
+          "((SELECT cvterm_id " .
+          "FROM cvterm CVT " .
+          " INNER JOIN cv CV on CVT.cv_id = CV.cv_id " .
+          "WHERE CV.name = 'chado_properties' AND CVT.name = 'version'), " .
+          "'%s') ";
 
   if($action == 'Install Chado v1.2'){
     $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.2.sql';
     $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.2.sql';
     if (tripal_core_reset_chado_schema()) {
-      tripal_core_install_sql($schema_file);
-      tripal_core_install_sql($init_file);
+      $success = tripal_core_install_sql($schema_file);
+      if ($success) {
+        print "Installation Complete!\n";
+      }
+      else {
+        print "Installation Problems!  Please check output above for errors.\n";
+      }
+      $success = tripal_core_install_sql($init_file);
+      if ($success) {
+        print "Installation Complete!\n";
+      }
+      else {
+        print "Installation Problems!  Please check output above for errors.\n";
+      }
       db_query($vsql,'1.2'); # set the version
     }
     else {
@@ -96,16 +114,40 @@ function tripal_core_install_chado($action) {
   elseif($action == 'Upgrade Chado v1.11 to v1.2') {
     $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
     $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/upgrade-1.11-1.2.sql';
-    tripal_core_install_sql($schema_file);
-    tripal_core_install_sql($init_file);
+    $success = tripal_core_install_sql($schema_file);
+    if ($success) {
+      print "Installation Complete!\n";
+    }
+    else {
+      print "Installation Problems!  Please check output above for errors.\n";
+    }
+    $success = tripal_core_install_sql($init_file);
+    if ($success) {
+      print "Installation Complete!\n";
+    }
+    else {
+      print "Installation Problems!  Please check output above for errors.\n";
+    }
     db_query($vsql,'1.2'); # set the version
   }
   elseif($action == 'Install Chado v1.11'){
     $schema_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/default_schema-1.11.sql';
     $init_file = drupal_get_path('module', 'tripal_core') . '/chado_schema/initialize-1.11.sql';
     if (tripal_core_reset_chado_schema()) {
-      tripal_core_install_sql($schema_file);
-      tripal_core_install_sql($init_file);
+      $success = tripal_core_install_sql($schema_file);
+      if ($success) {
+        print "Installation Complete!\n";
+      }
+      else {
+        print "Installation Problems!  Please check output above for errors.\n";
+      }
+      $success = tripal_core_install_sql($init_file);
+      if ($success) {
+        print "Installation Complete!\n";
+      }
+      else {
+        print "Installation Problems!  Please check output above for errors.\n";
+      }
     }
     else {
       print "ERROR: cannot install chado.  Please check database permissions\n";
@@ -201,6 +243,7 @@ function tripal_core_install_sql($sql_file) {
   $in_string = 0;
   $query = '';
   $i = 0;
+  $success = 1;
   foreach ($lines as $line_num => $line) {
     $i++;
     $type = '';
@@ -314,11 +357,13 @@ function tripal_core_install_sql($sql_file) {
     }
     else {
       print "UNHANDLED $i, $in_string: $line";
-      return tripal_core_chado_install_done();
+      tripal_core_chado_install_done();
+      return FALSE;
     }
     if (preg_match_all("/\n/", $query, $temp) > 100) {
       print "SQL query is too long.  Terminating:\n$query\n";
-      return tripal_core_chado_install_done();
+      tripal_core_chado_install_done();
+      return FALSE;
     }
     if ($type and sizeof($stack) == 0) {
       print "Adding $type: line $i\n";
@@ -329,15 +374,15 @@ function tripal_core_install_sql($sql_file) {
       $result = pg_query($active_db, $query);
       if (!$result) {
         $error  = pg_last_error();
-        print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
+        print "FAILED!!\nError Message:\nSQL $i, $in_string: $query\n$error\n";
         pg_query($active_db, "set search_path to public,chado");
-        return tripal_core_chado_install_done();
+        $success = 0;
       }
       $query = '';
     }
   }
-  print "Installation Complete!\n";
   tripal_core_chado_install_done();
+  return $success;    
 }
 
 /**

+ 3 - 0
tripal_core/tripal_core.module

@@ -61,6 +61,9 @@ function tripal_core_init() {
   // otherwise PostgreSQL version that may have a different datestyle setting
   // will fail when inserting or updating a date column in a table.
   db_query("SET DATESTYLE TO '%s'", 'MDY');
+  
+  // make sure the current version of chado is set
+  tripal_core_set_chado_version();
 }
 
 /**

+ 6 - 18
tripal_feature/api/tripal_feature.api.inc

@@ -643,20 +643,8 @@ function trpial_feature_get_formatted_sequence($feature_id, $feature_name,
                        WHEN FL.fmin - $2 <= 0 THEN FL.fmin
                        ELSE $2
                     END                   
-                END as downstream,            
-                              
-                CASE 
-                  WHEN FL.strand >= 0 THEN 
-                    CASE 
-                       WHEN FL.fmin - $1 <= 0 THEN substring(OF.residues from 1 for ((FL.fmax - FL.fmin) + $1 + $2)) 
-                       ELSE substring(OF.residues from (FL.fmin + 1 - $1) for ((FL.fmax - FL.fmin) + $1 + $2))
-                    END
-                  WHEN FL.strand < 0 THEN
-                    CASE 
-                      WHEN FL.fmin - $2 <= 0 THEN substring(OF.residues from 1 for ((FL.fmax - FL.fmin) + $1 + $2)) 
-                      ELSE substring(OF.residues from (FL.fmin + 1 - $2) for ((FL.fmax - FL.fmin) + $1 + $2))      
-                    END
-                END as residues
+                END as downstream,                                          
+                substring(OF.residues from (adjfmin + 1) for (upstream + (FL.fmax - FL.fmin) + downstream)  as residues
               FROM featureloc FL 
                 INNER JOIN feature SF on FL.feature_id = SF.feature_id
                 INNER JOIN cvterm SCVT on SF.type_id = SCVT.cvterm_id
@@ -736,26 +724,26 @@ function trpial_feature_get_formatted_sequence($feature_id, $feature_name,
           $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
 
           // if the first sub feature we need to include the upstream bases
-          if ($i == 0 and $parent->strand >= 0) {
+          if ($i == 0 and $parent->strand >= 0) {  // forward direction
             // -------------------------- ref
             //    ....---->  ---->        
             //     up    1       2         
             $q = chado_query($sql, $upstream, 0, $child->feature_id);
           }
-          elseif ($i == 0 and $parent->strand < 0) {
+          elseif ($i == 0 and $parent->strand < 0) { // reverse direction
             // -------------------------- ref
             //    ....<----  <----
             //    down  1       2
             $q = chado_query($sql, 0, $downstream, $child->feature_id);
           }          
           // if the last sub feature we need to include the downstream bases
-          elseif ($i == $num_children->num_children - 1 and $parent->strand >= 0) {
+          elseif ($i == $num_children->num_children - 1 and $parent->strand >= 0) {  // forward direction
             // -------------------------- ref
             //        ---->  ---->....
             //          1       2 down
             $q = chado_query($sql, 0, $downstream, $child->feature_id);
           }
-          elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) {
+          elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) { // reverse direction
             // -------------------------- ref
             //        <----  <----....
             //          1       2  up