Răsfoiți Sursa

Merge branch '7.x-2.x' into 7.x-2.x-phylogeny

Stephen Ficklin 8 ani în urmă
părinte
comite
77062152c2

+ 97 - 4
tripal_core/api/tripal_core.chado_schema.api.inc

@@ -26,8 +26,9 @@
  */
 
 /**
- * Check that any given Chado table exists.  This function
- * is necessary because Drupa's db_table_exists will not
+ * Check that any given Chado table exists.
+ *
+ * This function is necessary because Drupal's db_table_exists will not
  * look in any other schema but the one were Drupal is installed
  *
  * @param $table
@@ -50,7 +51,12 @@ function chado_table_exists($table) {
       table_schema = :chado AND
       table_catalog = :default_db
   ";
-  $results = db_query($sql, array(':table_name' => $table, ':chado' => tripal_get_schema_name('chado'), ':default_db' => $default_db));
+  $args = array(
+    ':table_name' => $table,
+    ':chado' => tripal_get_schema_name('chado'),
+    ':default_db' => $default_db
+  );
+  $results = db_query($sql, $args);
   $exists = $results->fetchObject();
   if (!$exists) {
     return FALSE;
@@ -58,6 +64,89 @@ function chado_table_exists($table) {
   return TRUE;
 }
 
+/**
+ * Check that any given column in a Chado table exists.
+ *
+ * This function is necessary because Drupal's db_field_exists() will not
+ * look in any other schema but the one were Drupal is installed
+ *
+ * @param $table
+ *   The name of the chado table.
+ * @param $column
+ *   The name of the column in the chado table.
+
+ * @return
+ *   TRUE if the column exists for the table in the chado schema and
+ *   FALSE if it does not.
+ *
+ * @ingroup tripal_chado_schema_api
+ */
+function chado_column_exists($table, $column) {
+  global $databases;
+
+  $default_db = $databases['default']['default']['database'];
+
+  $sql = "
+    SELECT 1
+    FROM information_schema.columns
+    WHERE
+      table_name = :table_name AND
+      column_name = :column_name AND
+      table_schema = :chado AND
+      table_catalog = :default_db
+  ";
+  $args = array(
+    ':table_name' => $table,
+    ':column_name' => $column,
+    ':chado' => tripal_get_schema_name('chado'),
+    ':default_db' => $default_db
+  );
+  $results = db_query($sql, $args);
+  $exists = $results->fetchField();
+  if (!$exists) {
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/**
+ * Check that any given column in a Chado table exists.
+ *
+ * This function is necessary because Drupal's db_field_exists() will not
+ * look in any other schema but the one were Drupal is installed
+ *
+ * @param sequence
+ *   The name of the sequence
+ * @return
+ *   TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
+ *
+ * @ingroup tripal_chado_schema_api
+ */
+function chado_sequence_exists($sequence) {
+  global $databases;
+
+  $default_db = $databases['default']['default']['database'];
+
+  $sql = "
+    SELECT 1
+    FROM information_schema.sequences
+    WHERE
+      sequence_name = :sequence_name AND
+      sequence_schema = :sequence_schema AND
+      sequence_catalog = :sequence_catalog
+  ";
+  $args = array(
+    ':sequence_name' => $sequence,
+    ':sequence_schema' => tripal_get_schema_name('chado'),
+    ':sequence_catalog' => $default_db
+  );
+  $results = db_query($sql, $args);
+  $exists = $results->fetchField();
+  if (!$exists) {
+    return FALSE;
+  }
+  return TRUE;
+}
 /**
  * A Chado-aware replacement for the db_index_exists() function.
  *
@@ -73,7 +162,11 @@ function chado_index_exists($table, $name) {
 
   $default_db = $databases['default']['default']['database'];
 
-  $sql = "SELECT 1 as exists FROM pg_indexes WHERE indexname = :indexname";
+  $sql = "
+      SELECT 1 as exists
+      FROM pg_indexes
+      WHERE indexname = :indexname
+  ";
 
   $result = db_query($sql, array(':indexname' => $indexname));
   $exists = $result->fetchObject();

Fișier diff suprimat deoarece este prea mare
+ 245 - 242
tripal_core/chado_schema/default_schema-1.2-1.3-diff.sql


+ 220 - 29
tripal_core/includes/tripal_core.chado_install.inc

@@ -9,7 +9,7 @@
  *
  * @ingroup tripal_core
  */
-function tripal_core_chado_load_form() {
+function tripal_core_chado_load_form($form, $form_state) {
 
   // we want to force the version of Chado to be set properly
   $real_version = chado_get_version(TRUE);
@@ -17,23 +17,64 @@ function tripal_core_chado_load_form() {
   // 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);
-
-  if ($real_version == '1.2') {
-    drupal_set_message('Please note: the upgrade of Chado from v1.2 to v1.3 may
-        require three fixes to your database. All of the primary keys
-        in Chado were changed from integers to big integers to support larger
-        tables.  First, if your site has custom materialized views that will hold
-        data derived from fields changed to big integers then you may need to
-        alter the views to change the fields from integers to big integers
-        and repopulate those views.  Second, if you have made
-        any custom PL/pgSQL functions that expect primary and foreign key fields
-        to be integers, then those functions will need to be altered to accept
-        big integers.  Third, if you have PostgreSQL Views that use fields
-        that are converted to big integers then most likely this upgrade will
-        fail.  You must first remove those views, perform the upgrade and then
-        recreate them with the appropriate fields change to big integers.
-        The Tripal upgrader is not able to fix these problems automatically',
-        'warning');
+  if (array_key_exists('values', $form_state)) {
+    if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
+      $tables_list = implode(', ', array('analysis_cvterm', 'analysis_dbxref', 'analysis_pub',
+        'analysis_relationship', 'contactprop', 'dbprop', 'feature_contact',
+        'featuremap_contact', 'featuremap_dbxref', 'featuremap_organism', 'featuremapprop',
+        'featureposprop', 'library_contact', 'library_expression', 'library_expressionprop',
+        'library_featureprop', 'library_relationship', 'library_relationship_pub', 'nd_experiment_analysis',
+        'organism_cvterm', 'organism_cvtermprop', 'organism_pub', 'organism_relationship',
+        'organismprop_pub', 'phenotypeprop', 'phylotreeprop', 'project_analysis',
+        'project_dbxref', 'project_feature', 'project_stock', 'pubauthor_contact',
+        'stock_feature', 'stock_featuremap', 'stock_library', 'stockcollection_db'));
+      $items = array(
+        'PostgreSQL version 9.1 is required to perform this upgrade. If your Tripal
+         site uses an older version please upgrade before proceeding.',
+        'A major change between Chado v1.2 and v1.3 is that primary and foreign
+         keys were upgraded from integers to big integers. If your site has custom
+         materialized views that will hold data derived from fields changed to
+         big integers then you may need to alter the views to change the fields
+         from integers to big integers and repopulate those views. If you have not
+         added any materialized views you can ignore this issue.',
+        'Custom PL/pgSQL functions that expect primary and
+         foreign key fields to be integers will not work after the upgrade.
+         Those functions will need to be altered to accept big integers. If you
+         do not have any custom PL/pgSQL functions you can ignore this issue.',
+        'PostgreSQL Views that use fields that are converted to big
+         integers will cause this upgrade to fail.  You must first remove
+         those views, perform the upgrade and then recreate them with the
+         appropriate fields change to big integers. If you do not have custom
+         PostgreSQL Views you can ignore this issue.',
+        'Several new tables were added to Chado v1.3.  However, some groups have
+         added these tables to their Chado v1.2 installation.  The Tripal upgrader
+         will alter the primary and foreign keys of those tables to be "bigints"
+         if they already exist but will otherwise leave them the same.  You should
+         verify that any tables with Chado v1.3 names correctly match the v1.3 schema.
+         Otherwise you may have problems using Tripal. If you have not added any
+         Chado v1.3 tables to your Chado v1.2 database you can ignore this issue.
+         These are the newly added tables:  ' .
+         $tables_list  . '.'
+      );
+      $list = theme_item_list(array(
+        'items' => $items,
+        'title' => '',
+        'type' => 'ul',
+        'attributes' => array(),
+      ));
+      drupal_set_message('Please note: the upgrade of Chado from v1.2 to v1.3 may
+          require several fixes to your database. Please review the following
+          list to ensure a safe upgrade. The Tripal upgrader is
+          not able to fix these problems automatically: ' . $list, 'warning');
+    }
+    if ($form_state['values']['action_to_do'] == "Install Chado v1.3" or
+        $form_state['values']['action_to_do'] == "Install Chado v1.2" or
+        $form_state['values']['action_to_do'] == "Install Chado v1.11") {
+      drupal_set_message('Please note: if Chado is already installed it will
+          be removed and recreated and all data will be lost. If this is
+          desired or if this is the first time Chado has been installed
+          you can ignore this issue.', 'warning');
+    }
   }
 
   $form['current_version'] = array(
@@ -43,17 +84,23 @@ function tripal_core_chado_load_form() {
   );
 
   $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,
+    '#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,
+    '#ajax' => array(
+      'callback' => "tripal_core_chado_load_form_ajax_callback",
+      'wrapper'  => 'tripal_core_chado_load_form',
+      'effect'   => 'fade',
+      'method'   => 'replace',
+    ),
   );
 
   $form['warning'] = array(
@@ -66,6 +113,20 @@ function tripal_core_chado_load_form() {
     '#value' => t('Install/Upgrade Chado'),
   );
 
+  $form['#prefix'] = '<div id="tripal_core_chado_load_form">';
+  $form['#suffix'] = '</div>';
+  return $form;
+}
+
+/**
+ * Ajax callback function for the gensas_job_view_panel_form.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function tripal_core_chado_load_form_ajax_callback($form, $form_state) {
+  // drupal_debug($form['tree_settings']['org_table']['num_orgs']);
+
   return $form;
 }
 
@@ -281,7 +342,14 @@ function tripal_core_install_chado_1_11() {
  */
 function tripal_core_upgrade_chado_1_2_to_1_3() {
 
+  // Upgrade some of the custom tables that Tripal created that are now in
+  // Chado v1.3.  We'll do this ahead of time because the upgrade script won't
+  // upgrade tables if they already exist.
+  print "Checking for existing v1.3 tables in v1.2 and fixing bigints...\n";
+  tripal_core_upgrade_chado_1_2_to_1_3_pre_alter();
+
   // Get the path to the diff schema and upgrade SQL files.
+  print "Incorporating additional changes...\n";
   $diff_file = drupal_get_path('module', 'tripal_core') .
     '/chado_schema/default_schema-1.2-1.3-diff.sql';
 
@@ -293,6 +361,129 @@ function tripal_core_upgrade_chado_1_2_to_1_3() {
     throw new Exception("Upgrade problems!  Please check output above for errors.");
   }
 }
+
+/**
+ * Upgrade custom tables that may match the tables now in Chado v1.3.
+ *
+ * There were many new tables that were added to Chado v1.3 that were
+ * suggested by the Chado user community.  Some of those were Tripal users.
+ * Therefore, to help these Tripal users upgrade more seemlessly this function
+ * checks if those custom tables already exists, and if so updates them as
+ * best it can to match.  At a minimum it will create the table if it doesn't
+ * exist and if it does it will change the primary keys and foreign keys to
+ * be big ints.
+ */
+function tripal_core_upgrade_chado_1_2_to_1_3_pre_alter() {
+
+  // Include the Chado v1.3 schema definitions.
+  module_load_include('inc', 'tripal_core', '/api/tripal_core.schema_v1.3.api');
+
+  // The list of new tables in Chado v1.3
+  $new_tables = array('analysis_cvterm', 'analysis_dbxref', 'analysis_pub', 'analysis_relationship',
+    'contactprop', 'dbprop', 'feature_contact', 'featuremap_contact', 'featuremap_dbxref',
+    'featuremap_organism', 'featuremapprop', 'featureposprop', 'library_contact',
+    'library_expression', 'library_expressionprop', 'library_featureprop',
+    'library_relationship', 'library_relationship_pub', 'nd_experiment_analysis',
+    'organism_cvterm', 'organism_cvtermprop', 'organism_pub', 'organism_relationship',
+    'organismprop_pub', 'phenotypeprop', 'phylotreeprop', 'project_analysis',
+    'project_dbxref', 'project_feature', 'project_stock', 'pubauthor_contact',
+    'stock_feature', 'stock_featuremap', 'stock_library', 'stockcollection_db',
+  );
+
+  // Get the name of the chado schema.
+  $chado_schema = tripal_get_schema_name('chado');
+
+  // Iterate through the new Chado tables and create them or if they already
+  // exist then update them.
+  foreach ($new_tables as $table) {
+
+    // Get the schema for this table.
+    $function = 'tripal_core_chado_schema_v1_3_' . $table;
+    $schema = $function();
+
+    // If the table exists then fix the pkeys and fkeys.
+    if (chado_table_exists($table)) {
+
+      // Update the primary key fields to be bigints.
+      $fields = $schema['fields'];
+      foreach ($fields as $field_name => $field) {
+        if ($field['type'] == 'serial') {
+          if (chado_column_exists($table, $field_name)) {
+            $sql = 'ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field_name . ' TYPE bigint';
+            chado_query($sql);
+          }
+          else {
+            throw new Exception('Could not alter primary key to bigint: ' . $table . '.' . $field_name);
+          }
+        }
+      }
+
+      // Update the foreign key fields to be bigints.
+      $fkeys = $schema['foreign keys'];
+      foreach ($fkeys as $fktable => $details) {
+        foreach ($details['columns'] as $leftkey => $rightkey) {
+          if (chado_column_exists($table, $leftkey)) {
+            $sql = 'ALTER TABLE {' . $table . '} ALTER COLUMN ' . $leftkey . ' TYPE bigint';
+            chado_query($sql);
+          }
+          else {
+            throw new Exception('Could not alter foreign key to bigint: ' . $table . '.' . $leftkey);
+          }
+        }
+      }
+
+    }
+  }
+  // Now create the sequences if they don't already exist.
+  $sequences = array(
+    'analysis_cvterm_analysis_cvterm_id_seq',
+    'analysis_dbxref_analysis_dbxref_id_seq',
+    'analysis_pub_analysis_pub_id_seq',
+    'analysis_relationship_analysis_relationship_id_seq',
+    'contactprop_contactprop_id_seq',
+    'dbprop_dbprop_id_seq',
+    'feature_contact_feature_contact_id_seq',
+    'featuremap_contact_featuremap_contact_id_seq',
+    'featuremap_dbxref_featuremap_dbxref_id_seq',
+    'featuremap_organism_featuremap_organism_id_seq',
+    'featuremapprop_featuremapprop_id_seq',
+    'featureposprop_featureposprop_id_seq',
+    'library_contact_library_contact_id_seq',
+    'library_expression_library_expression_id_seq',
+    'library_expressionprop_library_expressionprop_id_seq',
+    'library_featureprop_library_featureprop_id_seq',
+    'library_relationship_library_relationship_id_seq',
+    'library_relationship_pub_library_relationship_pub_id_seq',
+    'nd_experiment_analysis_nd_experiment_analysis_id_seq',
+    'organism_cvterm_organism_cvterm_id_seq',
+    'organism_cvtermprop_organism_cvtermprop_id_seq',
+    'organism_pub_organism_pub_id_seq',
+    'organism_relationship_organism_relationship_id_seq',
+    'organismprop_pub_organismprop_pub_id_seq',
+    'phenotypeprop_phenotypeprop_id_seq',
+    'phylotreeprop_phylotreeprop_id_seq',
+    'project_analysis_project_analysis_id_seq',
+    'project_dbxref_project_dbxref_id_seq',
+    'project_feature_project_feature_id_seq',
+    'project_stock_project_stock_id_seq',
+    'pubauthor_contact_pubauthor_contact_id_seq',
+    'stock_feature_stock_feature_id_seq',
+    'stock_featuremap_stock_featuremap_id_seq',
+    'stock_library_stock_library_id_seq',
+    'stockcollection_db_stockcollection_db_id_seq'
+  );
+  foreach ($sequences as $sequence) {
+
+    // Now add in the sequences if they don't already exist. There is no
+    // PostgreSQL 'CREATE SEQUENCE IF NOT EXIST' so we're forced to do it here
+    // and these create statements were removed from the diff upgrade file.
+    if (!chado_sequence_exists($sequence)) {
+      $sql = "CREATE SEQUENCE {" . $sequence . "} START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1";
+      chado_query($sql);
+    }
+  }
+}
+
 /**
  * Upgrades Chado from v1.11 to v1.2
  */

+ 185 - 188
tripal_cv/api/tripal_cv.api.inc

@@ -379,55 +379,70 @@ function tripal_insert_cv($name, $definition) {
 }
 
 /**
- *  Add's a controlled vocabulary term to the cvterm table.
+ *  Add's a controlled vocabulary term to Chado.
  *
- *  If the parent CV does not exist then
- *  that too is added to the CV table.  If the cvterm is a relationship term
- *  then the $is_relationship argument should be set.  The function will try
- *  to first find the relationship in the relationship ontology for updating and
- *  if it can't be found will add the relationship to the __global CV.  All terms
- *  must also have a corresponding database.  This is specified in the term's
- *  ID just before the colon (e.g. GO:003824).  If the database does not exist
- *  in the DB table then it will be added automatically.  The accession (the
- *  value just after the colon in the term's ID) will be added to the dbxref
- *  table.  If the CVterm already exists and $update is set (default) then the
- *  cvterm is updated.  If the CVTerm already exists and $update is not set, then
- *  no changes are made and the CVTerm object is returned.
+ *  This function will add a cvterm record (and a dbxref record if appropriate
+ *  values are provided). If the parent vocabulary does not exist then
+ *  that also is added to the cv table.  If the cvterm is a relationship term
+ *  then the 'is_relationship' value should be set.  All
+ *  terms must also have a corresponding database.  This is specified in the
+ *  term's ID just before the colon (e.g. GO:003824).  If the database does not
+ *  exist in the DB table then it will be added automatically.  The accession
+ *  (the value just after the colon in the term's ID) will be added to the
+ *  dbxref table.  If the CVterm already exists and $update is set (default)
+ *  then the cvterm is updated.  If the CVTerm already exists and $update is
+ *  not set, then no changes are made and the CVTerm object is returned.
  *
  * @param $term
  *   An associative array with the following keys:
- *    - id: the term accession. must be of the form <DB>:<ACCESSION>, where <DB> is the
- *        name of the database to which the cvterm belongs and the <ACCESSION> is the
- *        term's accession number in the database.
+ *    - id: the term accession. must be of the form <DB>:<ACCESSION>, where
+ *      <DB> is the name of the database to which the cvterm belongs and the
+ *      <ACCESSION> is the term's accession number in the database.
  *    - name: the name of the term. usually meant to be human-readable.
- *    - namespace: the CV name for the term. DEPRECATED. Please use cv_name instead.
  *    - is_obsolete: is present and set to 1 if the term is defunct
  *    - definition: the definition of the term
- *    - cv_name: The CV name to which the term belongs.  If this arugment is null or not
- *        provided then the function tries to find a record in the CV table with the same
- *        name provided in the $term[namespace].  If this field is provided then it
- *        overrides what the value in $term[namespace]
- *    - is_relationship: If this term is a relationship term then this value should be 1.
- *    - db_name: In some cases the database name will not be part of the $term['id'] and it
- *        needs to be explicitly set.  Use this argument only if the database name
- *        cannot be specififed in the term ID (e.g. <DB>:<ACCESSION>).
+ *    - cv_name: The CV name to which the term belongs.  If this arugment is
+ *        null or not provided then the function tries to find a record in the
+ *        CV table with the same name provided in the $term[namespace].  If
+ *        this field is provided then it overrides what the value in
+ *        $term[namespace]
+ *    - is_relationship: If this term is a relationship term then this value
+ *        should be 1.
+ *    - db_name: In some cases the database name will not be part of the
+ *        $term['id'] and it needs to be explicitly set.  Use this argument
+ *        only if the database name cannot be specififed in the term ID
+ *        (e.g. <DB>:<ACCESSION>).
  * @param $options
  *   An associative array with the following keys:
- *    - update_existing: By default this is TRUE.  If the term exists it is automatically updated.
+ *    - update_existing: By default this is TRUE.  If the term exists it is
+ *      automatically updated.
  *
  * @return
- *   A CVTerm object
+ *   A cvterm object
  *
- * @ingroup tripal_cv_api
+ * @ingroup tripal_chado_api
  */
 function tripal_insert_cvterm($term, $options = array()) {
 
-  // Set Defaults.
+  // get the term properties
+  $id = (isset($term['id'])) ? $term['id'] : '';
+  $name = '';
+  $cvname = '';
+  $definition = '';
+  $is_obsolete = 0;
+  $accession = '';
+
+  // Set Defaults
   if (isset($term['cv_name'])) {
-    $defaultcv = $term['cv_name'];
+    $cvname = $term['cv_name'];
   }
   else {
-    $defaultcv = '_global';
+    $cvname = 'local';
+  }
+  // Namespace is deprecated but must be supported for backwards
+  // compatability
+  if (array_key_exists('namespace', $term)) {
+    $cvname = $term['namespace'];
   }
 
   if (isset($term['is_relationship'])) {
@@ -451,14 +466,6 @@ function tripal_insert_cvterm($term, $options = array()) {
     $update = 1;
   }
 
-  // Get the term properties.
-  $id = (isset($term['id'])) ? $term['id'] : '';
-  $name = '';
-  $cvname = '';
-  $definition = '';
-  $is_obsolete = 0;
-  $accession = '';
-
   if (array_key_exists('name', $term)) {
     $name = $term['name'];
   }
@@ -466,12 +473,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     $name = $id;
   }
 
-  if (array_key_exists('namespace', $term)) {
-    $cvname = $term['namespace'];
-  }
-  else {
-    $cvname = $defaultcv;
-  }
+
   if (array_key_exists('definition', $term)) {
     $definition = preg_replace('/^\"(.*)\"/', '\1', $term['definition']);
   }
@@ -524,9 +526,9 @@ function tripal_insert_cvterm($term, $options = array()) {
   }
 
   // This SQL statement will be used a lot to find a cvterm so just set it
-  // here for easy reference below.  The following SQL tries to cover both the following:
-  // 1) The cvterm name may have changed but the accession remains constant
-  // 2) The cvterm name remains constant but the daccession/db may have changed (rare).
+  // here for easy reference below.  Because CV terms can change their names
+  // but accessions don't change, the following SQL finds cvterms based on
+  // their accession rather than the name.
   $cvtermsql = "
     SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
       DB.name as dbname, DB.db_id, DBX.accession
@@ -534,7 +536,7 @@ function tripal_insert_cvterm($term, $options = array()) {
       INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
       INNER JOIN {db} DB on DBX.db_id = DB.db_id
       INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
-    WHERE (DBX.accession = :accession and DB.name = :dbname) OR (CVT.name = :term and CV.name = :cvname)
+    WHERE DBX.accession = :accession and DB.name = :name
   ";
 
   // Add the database. The function will just return the DB object if the
@@ -544,13 +546,12 @@ function tripal_insert_cvterm($term, $options = array()) {
     $db = tripal_insert_db(array('name' => $dbname));
   }
   if (!$db) {
-    tripal_report_error('tripal_cv', TRIPAL_WARNING,
-      "Cannot find database '$dbname' in Chado.", NULL);
+    tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find database '$dbname' in Chado.", NULL);
     return 0;
   }
 
   // The cvterm table has two unique dependencies. We need to check both.
-  // first check the (name, cv_id, is_obsolete) constraint
+  // first check the (name, cv_id, is_obsolete) constraint.
   $values = array(
     'name' => $name,
     'is_obsolete' => $is_obsolete,
@@ -567,11 +568,9 @@ function tripal_insert_cvterm($term, $options = array()) {
     $result = chado_select_record('dbxref', array('*'), $values);
     $dbxref = $result[0];
     if (!$dbxref) {
-      tripal_report_error(
-        'tripal_cv',
-        TRIPAL_ERROR,
-        'Unable to access the dbxref record for the :term cvterm. Term Record: !record',
-        array(':term' => $name, '!record' => print_r($cvterm, TRUE))
+      tripal_report_error('tripal_cv', TRIPAL_ERROR,
+          'Unable to access the dbxref record for the :term cvterm. Term Record: !record',
+          array(':term' => $name, '!record' => print_r($cvterm, TRUE))
       );
       return FALSE;
     }
@@ -581,41 +580,40 @@ function tripal_insert_cvterm($term, $options = array()) {
     $result = chado_select_record('db', array('*'), $values);
     $db_check = $result[0];
 
-//     // The database name for this existing term does not match that of the
-//     // one provided to this function.  The CV name matches otherwise we
-//     // wouldn't have made it this far. So, let's swap the database for
-//     // this term
-//     if ($db_check->name != $db->name) {
-
-//       // Look to see if the correct dbxref record already exists for this
-//       // database.
-//       $values = array(
-//         'db_id' => $db->db_id,
-//         'accession' => $accession,
-//       );
-//       $result = chado_select_record('dbxref', array('*'), $values);
-
-//       // If we already have a good dbxref then we want to update our cvterm
-//       // to use this dbxref.
-//       if (count($result) > 0) {
-//         $dbxref = $result[0];
-//         $match = array('cvterm_id' => $cvterm->cvterm_id);
-//         $values = array('dbxref_id' => $dbxref->dbxref_id);
-//         $success = chado_update_record('cvterm', $match, $values);
-//         if (!$success) {
-//           tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to correct the dbxref id for the cvterm " .
-//             "'$name' (id: $accession), for database $dbname", NULL);
-//           return 0;
-//         }
-//       }
-//       // If we don't have the record then we want to delete our cvterm and let
-//       // the code below recreate it with the correct info.
-//       else {
-//         $match = array('cvterm_id' => $cvterm->cvterm_id);
-//         $options = array('statement_name' => 'del_cvterm_cv');
-//         chado_delete_record('cvterm', $match, $options);
-//       }
-//     }
+    //     // The database name for this existing term does not match that of the
+    //     // one provided to this function.  The CV name matches otherwise we
+    //     // wouldn't have made it this far. So, let's swap the database for
+    //     // this term.
+    //     if ($db_check->name != $db->name) {
+
+    //       // Look to see if the correct dbxref record already exists for this
+    //       // database.
+    //       $values = array(
+    //         'db_id' => $db->db_id,
+    //         'accession' => $accession,
+    //       );
+    //       $result = chado_select_record('dbxref', array('*'), $values);
+
+    //       // If we already have a good dbxref then we want to update our cvterm
+    //       // to use this dbxref.
+    //       if (count($result) > 0) {
+    //         $dbxref = $result[0];
+    //         $match = array('cvterm_id' => $cvterm->cvterm_id);
+    //         $values = array('dbxref_id' => $dbxref->dbxref_id);
+    //         $success = chado_update_record('cvterm', $match, $values);
+    //         if (!$success) {
+    //           tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to correct the dbxref id for the cvterm " .
+    //             "'$name' (id: $accession), for database $dbname", NULL);
+    //           return 0;
+    //         }
+    //       }
+    //       // If we don't have the dbxref then we want to delete our cvterm and let
+    //       // the code below recreate it with the correct info.
+    //       else {
+    //         $match = array('cvterm_id' => $cvterm->cvterm_id);
+    //         chado_delete_record('cvterm', $match);
+    //       }
+    //     }
 
     // Check that the accession matches.  Sometimes an OBO can define a term
     // multiple times but with different accessions.  If this is the case we
@@ -630,7 +628,7 @@ function tripal_insert_cvterm($term, $options = array()) {
       ));
       if (!$dbxref_new) {
         tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
-          "$name (id: $accession), for database $dbname", NULL);
+            "$name (id: $accession), for database $dbname", NULL);
         return 0;
       }
 
@@ -640,142 +638,141 @@ function tripal_insert_cvterm($term, $options = array()) {
         'dbxref_id' => $dbxref_new->dbxref_id,
         'is_for_definition' => 1,
       );
-      $options = array('statement_name' => 'sel_cvtermdbxref_cvdbis');
-      $result = chado_select_record('cvterm_dbxref', array('*'), $values, $options);
+      $result = chado_select_record('cvterm_dbxref', array('*'), $values);
 
       // if the cvterm_dbxref record does not exists then add it
       if (count($result)==0) {
         $options = array(
-          'statement_name' => 'ins_cvtermdbxref_cvdbis',
           'return_record' => FALSE,
         );
         $success = chado_insert_record('cvterm_dbxref', $values, $options);
         if (!$success) {
           tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the cvterm_dbxref record for a " .
-            "duplicated cvterm:  $name (id: $accession), for database $dbname", NULL);
+              "duplicated cvterm:  $name (id: $accession), for database $dbname", NULL);
           return 0;
         }
       }
       // get the original cvterm with the same name and return that.
-      $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
+      $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':name' => $dbname));
       $cvterm = $result->fetchObject();
       return $cvterm;
     }
 
     // Continue on, we've fixed the record if the db_id did not match.
     // We can now perform and updated if we need to.
-  }
-
-  if (!$result) {
-
-    // check to see if the dbxref exists if not, add it.
-    $dbxref =  tripal_insert_dbxref(array(
-      'db_id' => $db->db_id,
-      'accession' => $accession
-    ));
-    if (!$dbxref) {
-      tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
-        "$name (id: $accession), for database $dbname", NULL);
-      return 0;
     }
 
-    // Check to see if the dbxref already has an entry in the cvterm table.
-    $values = array('dbxref_id' => $dbxref->dbxref_id);
-    $options = array('statement_name' => 'sel_cvterm_db');
-    $check = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
-    if (count($check) == 0) {
-      // now add the cvterm
-      $ins_values = array(
-        'cv_id'                => $cv->cv_id,
-        'name'                 => $name,
-        'definition'           => $definition,
-        'dbxref_id'            => $dbxref->dbxref_id,
-        'is_obsolete'          => $is_obsolete,
-        'is_relationshiptype'  => $is_relationship,
-      );
-      $ins_options = array('statement_name' => 'ins_cvterm_all');
-      $success = chado_insert_record('cvterm', $ins_values, $ins_options);
-      if (!$success) {
-        if (!$is_relationship) {
-          tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the term: $name ($dbname)", NULL);
-          return 0;
-        }
-        else {
-          tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL);
-          return 0;
-        }
-      }
-    }
-    // This dbxref already exists in the cvterm table.
-    else {
-      tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
-      return 0;
-    }
-    $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
+    // get the CVterm record
+    $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
     $cvterm = $result->fetchObject();
-  }
-  // Update the cvterm.
-  elseif ($update) {
-
-    // First, basic update of the term.
-    $match = array('cvterm_id' => $cvterm->cvterm_id);
-    $upd_values = array(
-      'name'                => $name,
-      'definition'          => $definition,
-      'is_obsolete'         => $is_obsolete,
-      'is_relationshiptype' => $is_relationship,
-    );
-    $upd_options = array('statement_name' => 'upd_cvterm_nadeisis');
-    $success = chado_update_record('cvterm', $match, $upd_values, $upd_options);
-    if (!$success) {
-      tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
-      return 0;
-    }
+    if (!$cvterm) {
 
-    // Second, check that the dbxref has not changed and if it has then update it.
-    $checksql = "
-      SELECT cvterm_id
-      FROM {cvterm} CVT
-        INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
-        INNER JOIN {db} DB on DBX.db_id = DB.db_id
-        INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
-      WHERE DBX.accession = :accession and DB.name = :dbname and CVT.name = :term and CV.name = :cvname
-    ";
-    $check = chado_query($checksql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
-    if (!$check) {
-
-      // check to see if the dbxref exists if not, add it.
+      // Check to see if the dbxref exists if not, add it.
       $dbxref =  tripal_insert_dbxref(array(
         'db_id' => $db->db_id,
         'accession' => $accession
       ));
       if (!$dbxref) {
         tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
-          "$name (id: $accession), for database $dbname", NULL);
+            "$name (id: $accession), for database $dbname", NULL);
         return 0;
       }
 
+      // Check to see if the dbxref already has an entry in the cvterm table.
+      $values = array('dbxref_id' => $dbxref->dbxref_id);
+      $check = chado_select_record('cvterm', array('cvterm_id'), $values);
+      if (count($check) == 0) {
+        // now add the cvterm
+        $ins_values = array(
+          'cv_id'                => $cv->cv_id,
+          'name'                 => $name,
+          'definition'           => $definition,
+          'dbxref_id'            => $dbxref->dbxref_id,
+          'is_obsolete'          => $is_obsolete,
+          'is_relationshiptype'  => $is_relationship,
+        );
+        $success = chado_insert_record('cvterm', $ins_values);
+        if (!$success) {
+          if (!$is_relationship) {
+            tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the term: $name ($dbname)", NULL);
+            return 0;
+          }
+          else {
+            tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL);
+            return 0;
+          }
+        }
+      }
+      // This dbxref already exists in the cvterm table.
+      else {
+        tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
+        return 0;
+      }
+      $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
+      $cvterm = $result->fetchObject();
+    }
+    // Update the cvterm.
+    elseif ($update) {
+
+      // First, basic update of the term.
       $match = array('cvterm_id' => $cvterm->cvterm_id);
       $upd_values = array(
-        'dbxref_id' => $dbxref->dbxref_id,
+        'name'                => $name,
+        'definition'          => $definition,
+        'is_obsolete'         => $is_obsolete,
+        'is_relationshiptype' => $is_relationship,
       );
       $success = chado_update_record('cvterm', $match, $upd_values);
       if (!$success) {
-        tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term $name with new accession $db:$accession", NULL);
+        tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
         return 0;
       }
-    }
 
-    // Finally grab the updated details.
-    $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
-    $cvterm = $result->fetchObject();
-  }
-  else {
-     // do nothing, we have the cvterm but we don't want to update
+      // Second, check that the dbxref has not changed and if it has then update it.
+      $checksql = "
+        SELECT cvterm_id
+        FROM {cvterm} CVT
+          INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
+          INNER JOIN {db} DB on DBX.db_id = DB.db_id
+          INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
+        WHERE DBX.accession = :accession and DB.name = :dbname and CVT.name = :term and CV.name = :cvname
+      ";
+      $check = chado_query($checksql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
+      if (!$check) {
+
+        // check to see if the dbxref exists if not, add it.
+        $dbxref =  tripal_insert_dbxref(array(
+          'db_id' => $db->db_id,
+          'accession' => $accession
+        ));
+        if (!$dbxref) {
+          tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
+              "$name (id: $accession), for database $dbname", NULL);
+          return 0;
+        }
+
+        $match = array('cvterm_id' => $cvterm->cvterm_id);
+        $upd_values = array(
+          'dbxref_id' => $dbxref->dbxref_id,
+        );
+        $success = chado_update_record('cvterm', $match, $upd_values);
+        if (!$success) {
+          tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the term $name with new accession $db:$accession", NULL);
+          return 0;
+        }
+      }
+
+      // Finally grab the updated details.
+      $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
+      $cvterm = $result->fetchObject();
+    }
+    else {
+      // do nothing, we have the cvterm but we don't want to update
+    }
+    // return the cvterm
+    return $cvterm;
   }
-  // return the cvterm
-  return $cvterm;
-}
+
 
 /**
  * This function allows other modules to programatically

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff