Browse Source

Adding support for Chado v1.3

Stephen Ficklin 9 years ago
parent
commit
9ca5f175c0

+ 2 - 2
legacy/tripal_analysis/tripal_analysis.install

@@ -148,7 +148,7 @@ function tripal_analysis_add_cvterms() {
         . 'abbreviation for the analysis.',
       'cv_name' => 'tripal',
       'is_relationship' => 0,
-      'db_name' => 'tripal'
+      'db_name' => 'local'
     ),
     array('update_existing' => TRUE)
   );
@@ -162,7 +162,7 @@ function tripal_analysis_add_cvterms() {
       'definition' => 'The type of analysis that was performed.',
       'cv_name' => 'analysis_property',
       'is_relationship' => 0,
-      'db_name' => 'tripal'
+      'db_name' => 'local'
     ),
     array('update_existing' => TRUE)
   );

+ 22 - 0
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -857,6 +857,28 @@ function tripal_insert_obo($name, $path) {
   }
 }
 
+/**
+ * Retrieves an OBO record.
+ *
+ * @param $values
+ *   An associate array with the following allowed keys: obo_id, name
+ *
+ * @return
+ *   An instance of an OBO record object.
+ */
+function tripal_get_obo($values) {
+  $query = db_select('tripal_cv_obo', 'tco')
+    ->fields('tco');
+
+  if (array_key_exists('name', $values)) {
+    $query->condition('tco.name', $values['name']);
+  }
+  if (array_key_exists('obo_id', $values)) {
+    $query->condition('tco.obo_id', $values['obo_id']);
+  }
+  return $query->execute()->fetchObject();
+}
+
 /**
  * This function is intended to be used in autocomplete forms
  * for searching for CV terms that begin with the provided string

+ 1 - 1
tripal_chado/api/tripal_chado.schema.api.inc

@@ -289,7 +289,7 @@ function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
   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 and $effective_version != 'not installed')) {
+  if ($warn_if_unsupported and ($effective_version  < 1.11 and $effective_version != 'not installed')) {
     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

+ 32 - 0
tripal_chado/chado_schema/initialize-1.3.sql

@@ -0,0 +1,32 @@
+/* For load_gff3.pl */
+
+insert into contact (name,description) values ('null','null');
+insert into cv (name) values ('null');
+insert into cv (name,definition) values ('local','Locally created terms');
+insert into cv (name,definition) values ('Statistical Terms','Locally created terms for statistics');
+insert into db (name, description) values ('null', 'Use when a database is not available.');
+
+insert into dbxref (db_id,accession) values ((select db_id from db where name='null'), 'local:null');
+insert into cvterm (name,cv_id,dbxref_id) values ('null',(select cv_id from cv where name = 'null'),(select dbxref_id from dbxref where accession='local:null'));
+
+insert into pub (miniref,uniquename,type_id) values ('null','null',(select cvterm_id from cvterm where name = 'null'));
+
+insert into cv (name,definition) values ('chado_properties','Terms that are used in the chadoprop table to describe the state of the database');
+
+insert into dbxref (db_id,accession) values ((select db_id from db where name='null'), 'chado_properties:version');
+insert into cvterm (name,definition,cv_id,dbxref_id) values ('version','Chado schema version',(select cv_id from cv where name = 'chado_properties'),(select dbxref_id from dbxref where accession='chado_properties:version'));
+
+
+--this table will probably end up in general.sql
+ CREATE TABLE public.materialized_view   (       
+                                materialized_view_id SERIAL,
+                                last_update TIMESTAMP,
+                                refresh_time INT,
+                                name VARCHAR(64) UNIQUE,
+                                mv_schema VARCHAR(64),
+                                mv_table VARCHAR(128),
+                                mv_specs TEXT,
+                                indexed TEXT,
+                                query TEXT,
+                                special_index TEXT
+                                );

+ 504 - 0
tripal_chado/includes/tripal_chado.install.inc

@@ -0,0 +1,504 @@
+<?php
+/**
+ * @file
+ * Functions to install chado schema through Drupal
+ */
+
+/**
+ * Load Chado Schema Form
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_install_form() {
+
+  // we want to force the version of Chado to be set properly
+  $real_version = chado_get_version(TRUE);
+
+  // get the effective version.  Pass true as second argument
+  // to warn the user if the current version is not compatible
+  $version = chado_get_version(FALSE, TRUE);
+
+  $form['current_version'] = array(
+    '#type' => 'item',
+    '#title' => t("Current installed version of Chado:"),
+    '#description' => $real_version,
+  );
+
+  $form['action_to_do'] = array(
+     '#type' => 'radios',
+     '#title' => 'Installation/Upgrade Action',
+     '#options' => array(
+        'Install Chado v1.3' => t('New Install of Chado v1.3 (erases all existing Chado data if Chado already exists)'),
+        'Upgrade Chado v1.2 to v1.3' => t('Upgrade existing Chado v1.2 to v1.3 (no data is lost)'),
+        'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
+        'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
+        'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)'),
+     ),
+     '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
+     '#required' => TRUE,
+  );
+
+  $form['warning'] = array(
+    '#markup' => "<div><font color=\"red\">WARNING:</font>" . t('A new install of
+      Chado will remove and recreate the Chado database if it already exists.') . '</div>',
+  );
+
+  $form['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Install/Upgrade Chado'),
+  );
+
+  return $form;
+}
+
+function tripal_chado_install_form_validate($form, &$form_state) {
+
+  if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
+    // Make sure we are already not at v1.2
+    $real_version = chado_get_version(TRUE);
+    if ($real_version == "1.2") {
+      form_set_error("action_to_do", "You are already at v1.2.  There is no need to upgrade.");
+    }
+  }
+  if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
+    // Make sure we are already not at v1.2
+    $real_version = chado_get_version(TRUE);
+    if ($real_version == "1.3") {
+      form_set_error("action_to_do", "You are already at v1.3.  There is no need to upgrade.");
+    }
+  }
+}
+/**
+ * Submit Load Chado Schema Form
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_install_form_submit($form, &$form_state) {
+  global $user;
+  $action_to_do = trim($form_state['values']['action_to_do']);
+  $args = array($action_to_do);
+  $includes = array(module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.install'));
+  tripal_add_job($action_to_do, 'tripal_chado',
+      'tripal_chado_install_chado', $args, $user->uid, 10, $includes);
+}
+
+/**
+ * Install Chado Schema
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_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'),
+       :version)
+  ";
+
+  try {
+    if ($action == 'Install Chado v1.3') {
+      tripal_chado_install_chado_1_3();
+      chado_query($vsql, array(':version' => '1.3'));
+    }
+    elseif ($action == 'Upgrade Chado v1.2 to v1.3') {
+      tripal_chado_upgrade_chado_1_2_to_1_3();
+      chado_query($vsql, array(':version' => '1.3'));
+    }
+    elseif ($action == 'Install Chado v1.2') {
+      tripal_chado_install_chado_1_2();
+      chado_query($vsql, array(':version' => '1.2'));
+    }
+    elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
+      tripal_chado_upgrade_chado_1_11_to_1_2();
+      chado_query($vsql, array(':version' => '1.2'));
+    }
+    elseif ($action == 'Install Chado v1.11') {
+      tripal_chado_install_chado_1_11();
+    }
+  }
+  catch (Exception $e) {
+    tripal_chado_install_done();
+    tripal_log($e->getMessage(), 'error');
+    return FALSE;
+  }
+
+  // Set a variable to indicate the site is prepared.
+  variable_set('tripal_chado_is_prepared', FALSE);
+
+  module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup');
+
+  tripal_chado_set_globals();
+  tripal_chado_init();
+  tripal_chado_prepare_chado();
+
+  return TRUE;
+}
+
+/**
+ * Installs Chado v1.3.
+ */
+function tripal_chado_install_chado_1_3() {
+  // Get the path to the schema and init SQL files.
+  $schema_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/default_schema-1.3.sql';
+  $init_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/initialize-1.3.sql';
+
+  // Erase the Chado schema if it exists and perform the install.
+  if (tripal_chado_reset_chado_schema()) {
+    $success = tripal_chado_install_sql($schema_file);
+    if ($success) {
+      print "Install of Chado v1.3 (Step 1 of 2) Successful!\n";
+    }
+    else {
+      throw new Exception("Installation (Step 1 of 2) Problems!  Please check output above for errors.");
+    }
+    $success = tripal_chado_install_sql($init_file);
+    if ($success) {
+      print "Install of Chado v1.3 (Step 2 of 2) Successful.\nInstallation Complete\n";
+    }
+    else {
+      throw new Exception("Installation (Step 2 of 2) Problems!  Please check output above for errors.");
+    }
+  }
+  else {
+    throw new Exception("ERROR: cannot install chado.  Please check database permissions");
+  }
+}
+/**
+ * Installs Chado v1.2.
+ */
+function tripal_chado_install_chado_1_2() {
+  // Get the path to the schema and init SQL files.
+  $schema_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/default_schema-1.2.sql';
+  $init_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/initialize-1.2.sql';
+
+  // Erase the Chado schema if it exists and perform the install.
+  if (tripal_chado_reset_chado_schema()) {
+    $success = tripal_chado_install_sql($schema_file);
+    if ($success) {
+      print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
+    }
+    else {
+      throw new Exception("Installation (Step 1 of 2) Problems!  Please check output above for errors.");
+    }
+    $success = tripal_chado_install_sql($init_file);
+    if ($success) {
+      print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
+    }
+    else {
+      throw new Exception("Installation (Step 2 of 2) Problems!  Please check output above for errors.");
+    }
+  }
+  else {
+    throw new Exception("ERROR: cannot install chado.  Please check database permissions");
+  }
+}
+/**
+ *
+ */
+function tripal_chado_install_chado_1_11() {
+
+  // Get the path to the schema and init SQL files.
+  $schema_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/default_schema-1.11.sql';
+  $init_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/initialize-1.11.sql';
+
+  // Erase the Chado schema if it exists and perform the install.
+  if (tripal_chado_reset_chado_schema()) {
+    $success = tripal_chado_install_sql($schema_file);
+    if ($success) {
+      print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
+    }
+    else {
+      throw new Exception("Installation (Step 1 of 2) Problems!  Please check output above for errors.");
+    }
+    $success = tripal_chado_install_sql($init_file);
+    if ($success) {
+      print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
+    }
+    else {
+      throw new Exception("Installation (Step 2 of 2) Problems!  Please check output above for errors.");
+    }
+  }
+  else {
+    throw new Exception("ERROR: cannot install chado.  Please check database permissions");
+  }
+}
+/**
+ * Upgrades Chado from v1.2 to v1.3
+ */
+function tripal_chado_upgrade_chado_1_2_to_1_3() {
+
+  // Get the path to the diff schema and upgrade SQL files.
+  $diff_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/default_schema-1.2-1.3-diff.sql';
+
+  $success = tripal_chado_install_sql($diff_file);
+  if ($success) {
+    print "Upgrade from v1.2 to v1.3 Successful!\n";
+  }
+  else {
+    throw new Exception("Upgrade problems!  Please check output above for errors.");
+  }
+}
+/**
+ * Upgrades Chado from v1.11 to v1.2
+ */
+function tripal_chado_upgrade_chado_1_11_to_1_2() {
+
+  // Get the path to the schema diff and upgarde SQL files.
+  $schema_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/default_schema-1.11-1.2-diff.sql';
+  $init_file = drupal_get_path('module', 'tripal_chado') .
+    '/chado_schema/upgrade-1.11-1.2.sql';
+
+  $success = tripal_chado_install_sql($schema_file);
+  if ($success) {
+    print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
+  }
+  else {
+    throw new Exception("Upgrade (Step 1 of 2) problems!  Please check output above for errors.");
+  }
+  $success = tripal_chado_install_sql($init_file);
+  if ($success) {
+    print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
+  }
+  else {
+    throw new Exception("Upgrade (Step 2 of 2) problems!  Please check output above for errors.");
+  }
+}
+
+/**
+ * Reset the Chado Schema
+ * This drops the current chado and chado-related schema and re-creates it
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_reset_chado_schema() {
+
+  // drop current chado and chado-related schema
+  if (chado_dbschema_exists('genetic_code')) {
+    print "Dropping existing 'genetic_code' schema\n";
+    db_query("drop schema genetic_code cascade");
+  }
+  if (chado_dbschema_exists('so')) {
+    print "Dropping existing 'so' schema\n";
+    db_query("drop schema so cascade");
+  }
+  if (chado_dbschema_exists('frange')) {
+    print "Dropping existing 'frange' schema\n";
+    db_query("drop schema frange cascade");
+  }
+  if (chado_dbschema_exists('chado')) {
+    print "Dropping existing 'chado' schema\n";
+    db_query("drop schema chado cascade");
+  }
+
+  // create the new chado schema
+  print "Creating 'chado' schema\n";
+  db_query("create schema chado");
+  if (chado_dbschema_exists('chado')) {
+    // before creating the plpgsql language let's check to make sure
+    // it doesn't already exists
+    $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
+    $results = db_query($sql);
+    $count = $results->fetchObject();
+    if (!$count or $count->count == 0) {
+      db_query("create language plpgsql");
+    }
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+ * Execute the provided SQL
+ *
+ * @param $sql_file
+ *   Contains SQL statements to be executed
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_install_sql($sql_file) {
+
+  $chado_local = chado_dbschema_exists('chado');
+
+  if ($chado_local) {
+    db_query("set search_path to chado");
+  }
+  print "Loading $sql_file...\n";
+  $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
+
+  if (!$lines) {
+    return 'Cannot open $schema_file';
+  }
+
+  $stack = array();
+  $in_string = 0;
+  $query = '';
+  $i = 0;
+  $success = 1;
+  foreach ($lines as $line_num => $line) {
+    $i++;
+    $type = '';
+    // find and remove comments except when inside of strings
+    if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
+      $line = preg_replace('/--.*$/', '', $line);  // remove comments
+    }
+    if (preg_match('/\/\*.*?\*\//', $line)) {
+      $line = preg_replace('/\/\*.*?\*\//', '', $line);  // remove comments
+    }
+    // skip empty lines
+    if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
+      continue;
+    }
+    // Find SQL for new objects
+    if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
+      $stack[] = 'table';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
+      $stack[] = 'alter table';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*SET/i', $line) and !$in_string) {
+      $stack[] = 'set';
+    }
+    if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
+      $stack[] = 'schema';
+    }
+    if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
+      $stack[] = 'sequence';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
+      $stack[] = 'view';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
+      $stack[] = 'comment';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
+      $stack[] = 'function';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
+      $stack[] = 'index';
+    }
+    if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
+      $stack[] = 'insert';
+      $line = preg_replace("/public\./", "chado.", $line);
+    }
+    if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
+      $stack[] = 'type';
+    }
+    if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
+      $stack[] = 'grant';
+    }
+    if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
+      $stack[] = 'aggregate';
+    }
+
+    // determine if we are in a string that spans a line
+    $matches = preg_match_all("/[']/i", $line, $temp);
+    $in_string = $in_string - ($matches % 2);
+    $in_string = abs($in_string);
+
+    // if we've reached the end of an object the pop the stack
+    if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+      $type = array_pop($stack);
+    }
+    if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
+      $type = array_pop($stack);
+    }
+    // if we're in a recognized SQL statement then let's keep track of lines
+    if ($type or sizeof($stack) > 0) {
+      $query .= "$line";
+    }
+    else {
+      print "UNHANDLED $i, $in_string: $line";
+      tripal_chado_install_done();
+      return FALSE;
+    }
+    if (preg_match_all("/\n/", $query, $temp) > 100) {
+      print "SQL query is too long.  Terminating:\n$query\n";
+      tripal_chado_install_done();
+      return FALSE;
+    }
+    if ($type and sizeof($stack) == 0) {
+      //print "Adding $type: line $i\n";
+      // rewrite the set search_path to make 'public' be 'chado', but only if the
+      // chado schema exists
+      if (strcmp($type, 'set') == 0 and $chado_local) {
+        $query = preg_replace("/public/m", "chado", $query);
+      }
+
+      // execute the statement
+      $result = db_query($query);
+
+      if (!$result) {
+        $error  = pg_last_error();
+        print "FAILED. Line  $i, $in_string\n$error:\n$query\n\n";
+        tripal_chado_install_done();
+        $success = 0;
+        return $success;
+      }
+      $query = '';
+    }
+  }
+  tripal_chado_install_done();
+  return $success;
+}
+
+/**
+ * Finish the Chado Schema Installation
+ *
+ * @ingroup tripal_chado
+ */
+function tripal_chado_install_done() {
+
+  db_query("set search_path to default");
+}

+ 50 - 7
tripal_chado/includes/tripal_chado.schema.inc

@@ -1,5 +1,16 @@
 <?php
 
+/**
+ * @file
+ * Adds to the tripal schema description.
+ *
+ * This file contains hooks for specifying the natural diversity tables
+ * when Chado v1.11 is installed.  This is only useful for some specific
+ * sites that required the ND tables before they were released.  These
+ * files should not be needed for most sites as the ND tables are now part
+ * of Chado v1.2. However these hooks remain here for backwards compatibility.
+ */
+
 /**
  * Implements hook_chado_schema_v1_11_analysisfeatureprop()
  * Purpose: To describe the structure of 'analysisfeatureprop' to tripal
@@ -111,15 +122,47 @@ function tripal_chado_chado_schema_v1_2_tripal_obo_temp() {
 }
 
 /**
- * @file
- * Adds to the tripal schema description.
+ * Implements hook_chado_schema_v1_3_tripal_obo_temp()
+ * Purpose: To describe the structure of 'tripal_obo_temp' to tripal
+ * @see chado_insert_record()
+ * @see chado_update_record()
+ * @see chado_select_record()
  *
- * This file contains hooks for specifying the natural diversity tables
- * when Chado v1.11 is installed.  This is only useful for some specific
- * sites that required the ND tables before they were released.  These
- * files should not be needed for most sites as the ND tables are now part
- * of Chado v1.2. However these hooks remain here for backwards compatibility.
+ * @return
+ *    An array describing the 'tripal_obo_temp' table
+ *
+ * @ingroup tripal_chado_v1.3_schema_api
  */
+function tripal_chado_chado_schema_v1_3_tripal_obo_temp() {
+  $schema = array(
+    'table' => 'tripal_obo_temp',
+    'fields' => array(
+      'id' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+      'stanza' => array(
+        'type' => 'text',
+        'not null' => TRUE,
+      ),
+      'type' => array(
+        'type' => 'varchar',
+        'length' => '50',
+        'not null' => TRUE,
+      ),
+    ),
+    'indexes' => array(
+      'tripal_obo_temp_idx0' => array('id'),
+      'tripal_obo_temp_idx0' => array('type'),
+    ),
+    'unique keys' => array(
+      'tripal_obo_temp_uq0' => array('id'),
+    ),
+  );
+  return $schema;
+}
+
 
 /**
  * Implements hook_chado_schema_v1_11_table()

+ 7 - 421
tripal_chado/includes/tripal_chado.setup.inc

@@ -62,200 +62,6 @@ function tripal_chado_prepare_form_submit($form, $form_state) {
      variable_set('tripal_chado_is_prepared', TRUE);
    }
 }
-/**
- * Load Chado Schema Form
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_install_form($form, $form_state) {
-
-  // we want to force the version of Chado to be set properly
-  $real_version = chado_get_version(TRUE);
-
-  // get the effective version.  Pass true as second argument
-  // to warn the user if the current version is not compatible
-  $version = chado_get_version(FALSE, TRUE);
-
-  $form['current_version'] = array(
-    '#type' => 'item',
-    '#title' => t("Current installed version of Chado:"),
-    '#description' => $real_version,
-  );
-
-  $form['action_to_do'] = array(
-     '#type' => 'radios',
-     '#title' => 'Installation/Upgrade Action',
-     '#options' => array(
-        'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
-        'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
-        'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)')
-     ),
-     '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
-     '#required' => TRUE,
-  );
-
-  $form['warning'] = array(
-    '#markup' => "<div><font color=\"red\">WARNING:</font>" . t('A new install of
-      Chado will remove and recreate the Chado database if it already exists.') . '</div>',
-  );
-
-  $form['button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Install/Upgrade Chado'),
-  );
-
-  return $form;
-}
-
-/**
- *
- * @param $form
- * @param $form_state
- */
-function tripal_chado_install_form_validate($form, &$form_state) {
-  // We do not want to allow re-installation of Chado if other
-  // Tripal modules are installed.  This is because the install files
-  // of those modules may add content to Chado and reinstalling Chado
-  // removes that content which may break the modules.
-
-  if ($form_state['values']['action_to_do'] == "Install Chado v1.2" or
-      $form_state['values']['action_to_do'] == "Install Chado v1.11") {
-
-    $modules = system_get_info('module');
-    $list = array();
-    foreach ($modules as $mname => $module) {
-      if (array_key_exists('dependencies', $module) and in_array('tripal_chado', $module['dependencies'])) {
-        $list[] = $module['name'] . " ($mname)";
-      }
-    }
-    if (count($list) > 0) {
-      form_set_error("action_to_do", "Chado cannot be installed while other Tripal modules
-          are enabled.  You must fully uninstall the following modules if you
-          would like to install or re-install chado.<br>" .
-          implode("<br>", $list));
-    }
-  }
-  if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
-    // Make sure we are already not at v1.2
-    $real_version = chado_get_version(TRUE);
-    if ($real_version == "1.2") {
-      form_set_error("action_to_do", "You are already at v1.2.  There is no need to upgrade.");
-    }
-  }
-}
-/**
- * Submit Load Chado Schema Form
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_install_form_submit($form, &$form_state) {
-  global $user;
-  $action_to_do = trim($form_state['values']['action_to_do']);
-  $args = array($action_to_do);
-  $includes = array(
-    module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup'),
-    module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
-  );
-  tripal_add_job($action_to_do, 'tripal_chado', 'tripal_chado_install_chado',
-      $args, $user->uid, 10, $includes);
-}
-
-/**
- * Install Chado Schema
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_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'),
-       :version)
-  ";
-
-  if ($action == 'Install Chado v1.2') {
-    $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.2.sql';
-    $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/initialize-1.2.sql';
-    if (tripal_chado_reset_chado_schema()) {
-      $success = tripal_chado_install_sql($schema_file);
-      if ($success) {
-        print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
-      }
-      else {
-        print "Installation (Step 1 of 2) Problems!  Please check output above for errors.\n";
-        exit;
-      }
-      $success = tripal_chado_install_sql($init_file);
-      if ($success) {
-        print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
-      }
-      else {
-        print "Installation (Step 2 of 2) Problems!  Please check output above for errors.\n";
-        exit;
-      }
-      chado_query($vsql, array(':version' => '1.2')); # set the version
-    }
-    else {
-      print "ERROR: cannot install chado.  Please check database permissions\n";
-      exit;
-    }
-  }
-  elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
-    $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
-    $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/upgrade-1.11-1.2.sql';
-    $success = tripal_chado_install_sql($schema_file);
-    if ($success) {
-      print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
-    }
-    else {
-      print "Upgrade (Step 1 of 2) problems!  Please check output above for errors.\n";
-      exit;
-    }
-    $success = tripal_chado_install_sql($init_file);
-    if ($success) {
-      print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
-    }
-    else {
-      print "Upgrade (Step 2 of 2) problems!  Please check output above for errors.\n";
-      exit;
-    }
-    chado_query($vsql, array(':version' => '1.2')); # set the version
-  }
-  elseif ($action == 'Install Chado v1.11') {
-    $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.11.sql';
-    $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/initialize-1.11.sql';
-    if (tripal_chado_reset_chado_schema()) {
-      $success = tripal_chado_install_sql($schema_file);
-      if ($success) {
-        print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
-      }
-      else {
-        print "Installation (Step 1 of 2) Problems!  Please check output above for errors.\n";
-        exit;
-      }
-      $success = tripal_chado_install_sql($init_file);
-      if ($success) {
-        print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
-      }
-      else {
-        print "Installation (Step 2 of 2) Problems!  Please check output above for errors.\n";
-        exit;
-      }
-    }
-    else {
-      print "ERROR: cannot install chado.  Please check database permissions\n";
-      exit;
-    }
-  }
-
-  tripal_chado_set_globals();
-  tripal_chado_init();
-  tripal_chado_prepare_chado();
-}
 
 /**
  * Prepares Chado for use by Tripal.
@@ -395,6 +201,13 @@ function tripal_chado_prepare_chado() {
     // tripal_chado_load_obo_v1_2_id($obo_id);
     //// tripal_submit_obo_job(array('obo_id' => $obo_id));
 
+    // Load the sequence ontology.
+    $obo = tripal_get_obo(array('obo_name' => 'Sequence Ontology'));
+    if ($obo) {
+      print "Installing the Sequence Ontology... This may take a while...\n";
+      tripal_chado_load_obo_v1_2_id($obo->obo_id);
+    }
+
     // Add the materialized view.
     tripal_feature_add_organism_count_mview();
 
@@ -592,233 +405,6 @@ function tripal_chado_prepare_chado() {
   }
 }
 
-/**
- * Reset the Chado Schema
- * This drops the current chado and chado-related schema and re-creates it
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_reset_chado_schema() {
-
-  // drop current chado and chado-related schema
-  if (chado_dbschema_exists('genetic_code')) {
-    print "Dropping existing 'genetic_code' schema\n";
-    db_query("drop schema genetic_code cascade");
-  }
-  if (chado_dbschema_exists('so')) {
-    print "Dropping existing 'so' schema\n";
-    db_query("drop schema so cascade");
-  }
-  if (chado_dbschema_exists('frange')) {
-    print "Dropping existing 'frange' schema\n";
-    db_query("drop schema frange cascade");
-  }
-  if (chado_dbschema_exists('chado')) {
-    print "Dropping existing 'chado' schema\n";
-    db_query("drop schema chado cascade");
-  }
-
-  // create the new chado schema
-  print "Creating 'chado' schema\n";
-  db_query("create schema chado");
-  if (chado_dbschema_exists('chado')) {
-    // before creating the plpgsql language let's check to make sure
-    // it doesn't already exists
-    $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
-    $results = db_query($sql);
-    $count = $results->fetchObject();
-    if (!$count or $count->count == 0) {
-      db_query("create language plpgsql");
-    }
-    return TRUE;
-  }
-
-  return FALSE;
-}
-
-/**
- * Execute the provided SQL
- *
- * @param $sql_file
- *   Contains SQL statements to be executed
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_install_sql($sql_file) {
-
-  $chado_local = chado_dbschema_exists('chado');
-
-  if ($chado_local) {
-    db_query("set search_path to chado");
-  }
-  print "Loading $sql_file...\n";
-  $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
-
-  if (!$lines) {
-    return 'Cannot open $schema_file';
-  }
-
-  $stack = array();
-  $in_string = 0;
-  $query = '';
-  $i = 0;
-  $success = 1;
-  foreach ($lines as $line_num => $line) {
-    $i++;
-    $type = '';
-    // find and remove comments except when inside of strings
-    if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
-      $line = preg_replace('/--.*$/', '', $line);  // remove comments
-    }
-    if (preg_match('/\/\*.*?\*\//', $line)) {
-      $line = preg_replace('/\/\*.*?\*\//', '', $line);  // remove comments
-    }
-    // skip empty lines
-    if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
-      continue;
-    }
-    // Find SQL for new objects
-    if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
-      $stack[] = 'table';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
-      $stack[] = 'alter table';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*SET/i', $line) and !$in_string) {
-      $stack[] = 'set';
-    }
-    if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
-      $stack[] = 'schema';
-    }
-    if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
-      $stack[] = 'sequence';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
-      $stack[] = 'view';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
-      $stack[] = 'comment';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
-      $stack[] = 'function';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
-      $stack[] = 'index';
-    }
-    if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
-      $stack[] = 'insert';
-      $line = preg_replace("/public\./", "chado.", $line);
-    }
-    if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
-      $stack[] = 'type';
-    }
-    if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
-      $stack[] = 'grant';
-    }
-    if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
-      $stack[] = 'aggregate';
-    }
-
-    // determine if we are in a string that spans a line
-    $matches = preg_match_all("/[']/i", $line, $temp);
-    $in_string = $in_string - ($matches % 2);
-    $in_string = abs($in_string);
-
-    // if we've reached the end of an object the pop the stack
-    if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
-      $type = array_pop($stack);
-    }
-    elseif (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
-      $type = array_pop($stack);
-    }
-    // if we're in a recognized SQL statement then let's keep track of lines
-    if ($type or sizeof($stack) > 0) {
-      $query .= "$line";
-    }
-    else {
-      print "UNHANDLED $i, $in_string: $line";
-      tripal_chado_chado_install_done();
-      return FALSE;
-    }
-    if (preg_match_all("/\n/", $query, $temp) > 100) {
-      print "SQL query is too long.  Terminating:\n$query\n";
-      tripal_chado_chado_install_done();
-      return FALSE;
-    }
-    if ($type and sizeof($stack) == 0) {
-      //print "Adding $type: line $i\n";
-      // rewrite the set search_path to make 'public' be 'chado', but only if the
-      // chado schema exists
-      if (strcmp($type, 'set') == 0 and $chado_local) {
-        $query = preg_replace("/public/m", "chado", $query);
-      }
-
-      // execute the statement
-      $result = db_query($query);
-
-      if (!$result) {
-        $error  = pg_last_error();
-        print "FAILED. Line  $i, $in_string\n$error:\n$query\n\n";
-        tripal_chado_chado_install_done();
-        $success = 0;
-        return $success;
-      }
-      $query = '';
-    }
-  }
-  tripal_chado_chado_install_done();
-  return $success;
-}
-
-/**
- * Finish the Chado Schema Installation
- *
- * @ingroup tripal_chado
- */
-function tripal_chado_chado_install_done() {
-
-  db_query("set search_path to default");
-}
-
 /**
  * Creates a materialized view that stores the type & number of stocks per organism
  *

+ 1 - 1
tripal_chado/tripal_chado.module

@@ -118,7 +118,7 @@ function tripal_chado_menu() {
     'page arguments' => array('tripal_chado_install_form'),
     'type' => MENU_NORMAL_ITEM,
     'access arguments' => array('install chado'),
-    'file' => 'includes/tripal_chado.setup.inc',
+    'file' => 'includes/tripal_chado.install.inc',
     'file path' => drupal_get_path('module', 'tripal_chado'),
     'weight' => -100
   );