Browse Source

Added support for Organism and Analysis base tables

Stephen Ficklin 9 years ago
parent
commit
1f97dd2fca

+ 214 - 44
tripal_entities/api/tripal_entities.api.inc

@@ -1,49 +1,5 @@
 <?php
 
-
-/**
- * Deletes a tripal_entity.
- */
-function tripal_entity_delete(TripalEntity $tripal_entity) {
-  $tripal_entity->delete();
-}
-
-/**
- * Saves a tripal_entity to the database.
- *
- * @param $tripal_entity
- *   The tripal_entity object.
- */
-function tripal_entity_save(TripalEntity $entity) {
-  return $entity->save();
-}
-
-
-/**
- * Saves a tripal_entity type to the db.
- */
-function tripal_bundle_save(TripalBundle $entity) {
-  $entity->save();
-}
-
-
-/**
- * Deletes a tripal_entity type from the db.
- */
-function tripal_bundle_delete(TripalBundle $type) {
-  $type->delete();
-}
-
-
-/**
- * URI callback for tripal_entitys
- */
-function tripal_entity_uri(TripalEntity $entity){
-  return array(
-    'path' => 'BioData/' . $entity->id,
-  );
-}
-
 /**
  * TODO: The code for creating the title needs to be updated to not
  * use nodes but rather entities.
@@ -95,3 +51,217 @@ function chado_get_entity_title($entity) {
 
   return $title;
 }
+
+/**
+ * Creates a new Tripal Entity type (i.e. bundle).
+ *
+ * An entity is created by first adding a record to the tripal_term and
+ * tripal_vocabulary tables.  This helps Tripal keep track of which terms
+ * are used in which tables.  Second, records are added to the tripal_bundle
+ * table which is where Tripal keeps track of all of the entity_types. Finally,
+ * the hook_add_bundle_fields() is called that allows other modules to
+ * add fields to the entity.
+ *
+ * @param $cvterm
+ *  A cvterm object created using the chado_generate_var() function.
+ * @param $error
+ *  A string, passed by reference, that is filled with the error message
+ *  if the function fails.
+ *
+ * @return
+ *  TRUE if the entity type (bundle) was succesfully created.  FALSE otherwise.
+ */
+function tripal_create_entity_type($cvterm, &$error = '') {
+
+  // Before creating the entity we must add records to the tripal_vocabulary
+  // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
+  $match = array('cv_id' => $cvterm->cv_id->cv_id);
+  $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
+  if (count($vocab) == 0) {
+    $values = array(
+      'cv_id' => $cvterm->cv_id->cv_id,
+      'db_id' => $cvterm->dbxref_id->db_id->db_id,
+      'publish' => 1,
+    );
+    $values = chado_insert_record('tripal_vocabulary', $values);
+    if (!$values) {
+      $error = 'Could not add vocabulary to tripal_vocabluary table.';
+      return FALSE;
+    }
+    // Convert the values array into an object.
+    $vocab = new stdClass();
+    $vocab->vocabulary_id = $values['vocabulary_id'];
+    $vocab->cv_id = $values['cv_id'];
+  }
+  else {
+    // Make sure the vocabulary is set to publish
+    $values = array('publish' => 1);
+    chado_update_record('tripal_vocabulary', $match, $values);
+    $vocab = $vocab[0];
+  }
+
+  // Look to see if this vocabulary is used as a default for any table. If
+  // so then we can use that to populate the tripal_vocabulary_usage table.
+  $default = db_select('tripal_cv_defaults', 't')
+    ->fields('t')
+    ->condition('cv_id', $vocab->cv_id)
+    ->execute()
+    ->fetchObject();
+  if ($default) {
+    $values = array(
+      'vocabulary_id' => $vocab->vocabulary_id,
+      'data_table' => $default->table_name,
+      'type_table' => $default->table_name,
+      'field' =>  $default->field_name,
+    );
+    $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
+    if (count($vocab_usage) == 0) {
+      $values = chado_insert_record('tripal_vocabulary_usage', $values);
+      if (!$values) {
+        $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
+        return FALSE;
+      }
+      $vocab_usage = new stdClass();
+      $vocab_usage->vocabulary_id = $values['vocabulary_id'];
+      $vocab_usage->data_table = $values['data_table'];
+      $vocab_usage->type_table = $values['type_table'];
+      $vocab_usage->field = $values['field'];
+    }
+  }
+  // The organism table does not have a type_id so we won't ever find
+  // a record for it in the tripal_cv_defaults table.
+  else if ($cvterm->name == 'organism') {
+    $values = array(
+      'vocabulary_id' => $vocab->vocabulary_id,
+      'data_table' => 'organism',
+      'type_table' => 'organism',
+      'field' =>  '',
+    );
+    $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
+    if (count($vocab_usage) == 0) {
+      $values = chado_insert_record('tripal_vocabulary_usage', $values);
+      if (!$values) {
+        $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
+        return FALSE;
+      }
+      $vocab_usage = new stdClass();
+      $vocab_usage->vocabulary_id = $values['vocabulary_id'];
+      $vocab_usage->data_table = $values['data_table'];
+      $vocab_usage->type_table = $values['type_table'];
+      $vocab_usage->field = $values['field'];
+    }
+  }
+  // The analysis table does not have a type_id so we won't ever find
+  // a record for it in the tripalcv_defaults table.
+  else if ($cvterm->name == 'analysis') {
+    $values = array(
+      'vocabulary_id' => $vocab->vocabulary_id,
+      'data_table' => 'analysis',
+      'type_table' => 'analysis',
+      'field' =>  '',
+    );
+    $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
+    if (count($vocab_usage) == 0) {
+      $values = chado_insert_record('tripal_vocabulary_usage', $values);
+      if (!$values) {
+        $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
+        return FALSE;
+      }
+      $vocab_usage = new stdClass();
+      $vocab_usage->vocabulary_id = $values['vocabulary_id'];
+      $vocab_usage->data_table = $values['data_table'];
+      $vocab_usage->type_table = $values['type_table'];
+      $vocab_usage->field = $values['field'];
+    }
+  }
+  // If there is no default table then we have an error, and we should
+  // set a variable so that the form can help the user deal with the problem.
+  else {
+    $error = t('There is no default mapping of this term\'s ' .
+        'vocabulary to a table in Chado.  Therefore, it is not possible to ' .
+        'determine how to store data of this type.');
+    return FALSE;
+  }
+
+  // Now add the tripal_term record if it doesn't already exist.
+  $match = array(
+    'vocabulary_id' => $vocab->vocabulary_id,
+    'cvterm_id' => $cvterm->cvterm_id,
+  );
+  $term = chado_select_record('tripal_term', array('*'), $match);
+  if (count($term) == 0) {
+    $values = array(
+      'vocabulary_id' => $vocab->vocabulary_id,
+      'cvterm_id' => $cvterm->cvterm_id,
+      'publish' => 1
+    );
+    $values = chado_insert_record('tripal_term', $values);
+    if (!$values) {
+      $error =  'Could not add term to tripal_term table.';
+      return FALSE;
+    }
+    $term = new stdClass();
+    $term->term_id = $values['term_id'];
+  }
+  else {
+    $values = array('publish' => 1);
+    chado_update_record('tripal_term', $match, $values);
+    $term = $term[0];
+  }
+
+  // Finally, add the tripal_term_usage record if it doesn't already exist.
+  $match = array('term_id' => $term->term_id);
+  $options = array('has_record' => TRUE);
+  if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
+    $values = array(
+      'term_id' => $term->term_id,
+      'data_table' => $vocab_usage->data_table,
+      'type_table' => $vocab_usage->type_table,
+      'field' =>  $vocab_usage->field,
+    );
+    $values = chado_insert_record('tripal_term_usage', $values);
+    if (!$values) {
+      $error = 'Could not add term to tripal_term table.';
+      return FALSE;
+    }
+  }
+
+  // Clear the entity cache so that Drupal will read our
+  // hook_entity_info() implementation which now will have the entities
+  // described because we set the publish column to 1 in the tripal_term
+  // table.
+  global $language;
+  $langcode = $language->language;
+  cache_clear_all("entity_info:$langcode", 'cache');
+
+  // Create the bundle name and entity type name.  The bundle name is the
+  // dbxref ID.  This isn't very human readable, but the alternative is to
+  // use the accession which may not always be alpha-numeric.
+  $bundle_name = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
+
+  // Check to see if this bundle exists. If not then create it
+  $bundle = db_select('tripal_bundle', 't')
+    ->fields('t')
+    ->condition('type', 'BioData')
+    ->condition('bundle', $bundle_name)
+    ->execute()
+    ->fetchObject();
+
+  if (!$bundle) {
+    // The TripalBundle Entity manages the bundles we have available.
+    // Therefore, we need to add a new entity for each bundle "type".
+    $vals = array(
+      'label' => $cvterm->name,
+      'type' => 'BioData',
+      'bundle' => $bundle_name,
+      'data' => serialize(array()),
+      'module' => 'tripal_entities'
+    );
+    $tripal_bundle = new TripalBundle($vals, 'BioData_bundles');
+    $tripal_bundle->save();
+  }
+  // Allow modules to now add fields to the bundle
+  module_invoke_all('add_bundle_fields', 'BioData', $bundle_name, $cvterm);
+
+  return TRUE;
+}

+ 65 - 208
tripal_entities/includes/tripal_entities.admin.inc

@@ -198,27 +198,48 @@ function tripal_entities_admin_bundles_form_submit($form, $form_state) {
 /**
  * Form for creating biological data types (ie: tripal entity types).
  *
+ * This form is available on the menu at Admin >> Structure >> Biological Data
+ * Types
+ *
  * @param array $form
  * @param array $form_state
+ *
  * @return
  *    An array describing this form to the Form API.
  */
 function tripal_entities_admin_add_type_form($form, &$form_state) {
-
   $term_name = '';
   $num_terms = 0;
   $cv_id = '';
 
   // Set defaults using the form state.
-  if (array_key_exists('storage', $form_state) and
-      array_key_exists('terms', $form_state['storage'])) {
-    $terms = $form_state['storage']['terms'];
+  if (array_key_exists('input', $form_state)) {
+    if (array_key_exists('term_name', $form_state['input'])) {
+      $term_name = $form_state['input']['term_name'];
+    }
+    if (array_key_exists('cv_id', $form_state['input'])) {
+      $cv_id = $form_state['input']['cv_id'];
+    }
+
+    // If a term and cv_id are provided then we can look for the term using
+    // both and we should find a unique term. If only ther term is provided
+    // we can still look for a unique term but there must only be one.
+    if ($term_name and !$cv_id) {
+      $match = array(
+        'name' => $term_name,
+      );
+    }
+    else {
+      $match = array(
+        'name' => $term_name,
+        'cv_id' => $cv_id,
+      );
+    }
+
+    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
+    $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
     $num_terms = count($terms);
   }
-  if (array_key_exists('values', $form_state) and
-      array_key_exists('term_name', $form_state['values'])) {
-    $term_name = $form_state['values']['term_name'];
-  }
 
   // If no term has been selected yet then provide the auto complete field.
   if ($num_terms == 0) {
@@ -233,32 +254,33 @@ function tripal_entities_admin_add_type_form($form, &$form_state) {
       '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
     );
   }
-
+  else {
+    $form['term_name'] = array(
+      '#type' => 'hidden',
+      '#value' => $term_name,
+    );
+  }
 
   // If the term belongs to more than one vocabulary then add additional fields
   // to let the user select the vocabulary.
   if ($num_terms > 1) {
     $cvs = array();
     foreach ($terms as $term) {
-      $cvs[$term->cv_id->cv_id] = $term->cv_id->name;
+      $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
     }
     $form['cv_id'] = array(
       '#type' => 'radios',
       '#title' => t('Select the appropriate vocabulary'),
       '#options' => $cvs,
-      '#description' => t('The term belongs to more than one vocabulary. Please
-          indicate the proper vocabulary for the term.')
     );
   }
 
   // Add in the button for the cases of no terms or too many.
-  if ($num_terms != 1) {
-    $form['select_button'] = array(
-      '#type' => 'submit',
-      '#value' => t('Use this term'),
-      '#name' => 'select_cvterm'
-    );
-  }
+  $form['select_button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Use this term'),
+    '#name' => 'select_cvterm'
+  );
 
   return $form;
 }
@@ -268,15 +290,22 @@ function tripal_entities_admin_add_type_form($form, &$form_state) {
  *
  */
 function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
+
   // Check if this term and vocabulary is in the tripal_vocabulary usage tables.
   // If not then add it.
   if (array_key_exists('clicked_button', $form_state) and
       $form_state['clicked_button']['#name'] =='select_cvterm') {
 
     // First, make sure the term is unique. If not then we can't check it.
-    $term_name = $form_state['values']['term_name'];
-    $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+    $term_name = NULL;
+    $cv_id = NULL;
     $cvterm = NULL;
+    if (array_key_exists('term_name', $form_state['values'])) {
+      $term_name = $form_state['input']['term_name'];
+    }
+    if (array_key_exists('cv_id', $form_state['input'])) {
+      $cv_id = $form_state['input']['cv_id'];
+    }
 
     // If a term and cv_id are provided then we can look for the term using
     // both and we should find a unique term. If only ther term is provided
@@ -293,18 +322,8 @@ function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
       );
     }
     $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
-
-    // Add the cvterm to the storage element so we don't have to keep
-    // looking it up in the submit function or on a rebuild of the form.
     $form_state['storage']['terms'] = $terms;
 
-    // If we only have one term then we found a unique match and we can do
-    // some further checking.
-    if (count($terms) == 1) {
-      $cvterm = $terms[0];
-      // Make sure the term is set as published.
-      tripal_entities_add_term_usage($cvterm, $form_state);
-    }
     // If we do not have any terms then the term provided by the user does not
     // exists and we need to provide an error message.
     if (count($terms) == 0) {
@@ -313,7 +332,8 @@ function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
     // If we have more than one term then we need to set an error so that the
     // form can provide a list of vocabularies to select from.
     if (count($terms) > 1) {
-      form_set_error('', t('The term is not unique. A list of vocabularies has been provided where this term is present. Please select the appropriate one.'));
+      form_set_error('term_name', t('The term is not unique. A list of vocabularies
+          that contain this term. Please select the most appropriate vocabulary.'));
     }
   }
 }
@@ -322,61 +342,33 @@ function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
  *
  */
 function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
+
   if ($form_state['clicked_button']['#name'] =='select_cvterm') {
     $cvterm = $form_state['storage']['terms'][0];
+
     $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
 
     // Before we try to add this type, check to see if it already exists
     // as a bundle.
     $einfo = entity_get_info('BioData');
     if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
-      tripal_entities_add_term_usage($cvterm, $form_state);
-      tripal_entities_add_bundle($cvterm);
-      drupal_set_message('New biological data type created.  Fields are added automatically to this type.');
-      $form_state['redirect'] = "admin/structure/BioData";
+      $error = '';
+      $success = tripal_create_entity_type($cvterm, $error);
+      if (!$success) {
+        drupal_set_message($error, 'error');
+        $form_state['redirect'] = "admin/structure/BioData";
+      }
+      else {
+        drupal_set_message('New biological data type created.  Fields are added automatically to this type.');
+        $form_state['redirect'] = "admin/structure/BioData";
+      }
     }
     else {
       drupal_set_message('This type already exists.', 'warning');
     }
   }
 }
-/**
- * Implements hook_submit() for the tripal_entities_admin_publish_form.
- *
- */
-function tripal_entities_add_bundle($cvterm) {
-
-  // Create the bundle name and entity type name.  The bundle name is the
-  // dbxref ID.  This isn't very human readable, but the alternative is to
-  // use the accession which may not always be alpha-numeric.
-  $bundle_name = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
-
-  // Check to see if this bundle exists. If not then create it
-  $bundle = db_select('tripal_bundle', 't')
-  ->fields('t')
-  ->condition('type', 'BioData')
-  ->condition('bundle', $bundle_name)
-  ->execute()
-  ->fetchObject();
-
-  if (!$bundle) {
-    // The TripalBundle Entity manages the bundles we have available.
-    // Therefore, we need to add a new entity for each bundle "type".
-    $vals = array(
-      'label' => $cvterm->name,
-      'type' => 'BioData',
-      'bundle' => $bundle_name,
-      'data' => serialize(array()),
-      'module' => 'tripal_entities'
-    );
-    $tripal_bundle = new TripalBundle($vals, 'BioData_bundles');
-    $tripal_bundle->save();
-  }
-
-  // Allow modules to now add fields to the bundle
-  module_invoke_all('add_bundle_fields', 'BioData', $bundle_name, $cvterm);
 
-}
 /**
  * Implements hook_add_bundle_fields().
  *
@@ -385,7 +377,6 @@ function tripal_entities_add_bundle($cvterm) {
  * @param $cvterm
  */
 function tripal_entities_add_bundle_fields($entity_type_name, $bundle_name, $cvterm) {
-
   // Adds the fields for the base table to the entity.
   tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm);
 
@@ -422,6 +413,7 @@ function tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $b
  * Adds the fields for the base table to the entity.
  */
 function tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm) {
+
   // Get the list of tables where this cvterm is used.
   $match = array('cvterm_id' => $cvterm->cvterm_id);
   $term = chado_select_record('tripal_term', array('*'), $match);
@@ -576,141 +568,6 @@ function tripal_entities_get_table_column_field_default($table_name, $schema, $c
 
   return $field_info;
 }
-/**
- * Adds a vocabulary and term to the Tripal term usage tables.
- *
- * This function is meant to be called only by the
- * tripal_entities_entity_form_validate() function. This code is
- * separated to simplify that function.  Therefore, if errors occur with adding
- * of terms then the form_set_error() is called.
- *
- * @param $cvterm
- */
-function tripal_entities_add_term_usage($cvterm, &$form_state) {
-
-  // Before creating the entity we mut add records to the tripal_vocabulary
-  // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
-  $match = array('cv_id' => $cvterm->cv_id->cv_id);
-  $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
-  if (count($vocab) == 0) {
-    $values = array(
-      'cv_id' => $cvterm->cv_id->cv_id,
-      'db_id' => $cvterm->dbxref_id->db_id->db_id,
-      'publish' => 1,
-    );
-    $values = chado_insert_record('tripal_vocabulary', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add vocabulary to tripal_vocabluary table.');
-      return FALSE;
-    }
-    // Convert the values array into an object.
-    $vocab = new stdClass();
-    $vocab->vocabulary_id = $values['vocabulary_id'];
-    $vocab->cv_id = $values['cv_id'];
-  }
-  else {
-    // Make sure the vocabulary is set to publish
-    $values = array('publish' => 1);
-    chado_update_record('tripal_vocabulary', $match, $values);
-    $vocab = $vocab[0];
-  }
-
-  // Does this vocabulary have a record in the tripal_vocabulary_usage
-  // table? If not then add one.
-  $match = array('vocabulary_id' => $vocab->vocabulary_id);
-  $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
-  if (count($vocab_usage) == 0) {
-    // Look to see if this vocabulary is used as a default for any table. If
-    // so then we can use that to populate the tripal_vocabulary_usage table.
-    $default = db_select('tripal_cv_defaults', 't')
-    ->fields('t')
-    ->condition('cv_id', $vocab->cv_id)
-    ->execute()
-    ->fetchObject();
-    if ($default) {
-      $values = array(
-        'vocabulary_id' => $vocab->vocabulary_id,
-        'data_table' => $default->table_name,
-        'type_table' => $default->table_name,
-        'field' =>  $default->field_name,
-      );
-      $values = chado_insert_record('tripal_vocabulary_usage', $values);
-      if (!$values) {
-        form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
-        return FALSE;
-      }
-    }
-    // If there is no default table then we have an error, and we should
-    // set a variable so that the form can help the user deal with the problem.
-    else {
-      $form_state['storage']['cvterm_has_default'] = FALSE;
-      form_set_error('', t('There is no default mapping of this term\'s
-          vocabulary to a table in Chado.  Therefore, it is not possible to
-          determine how to store data of this type.'));
-      return FALSE;
-    }
-    $vocab_usage = new stdClass();
-    $vocab_usage->vocabulary_id = $values['vocabulary_id'];
-    $vocab_usage->data_table = $values['data_table'];
-    $vocab_usage->type_table = $values['type_table'];
-    $vocab_usage->field = $values['field'];
-  }
-  else {
-    $vocab_usage = $vocab_usage[0];
-  }
-
-  // Now add the tripal_term record if it doesn't already exist.
-  $match = array(
-    'vocabulary_id' => $vocab->vocabulary_id,
-    'cvterm_id' => $cvterm->cvterm_id,
-  );
-  $term = chado_select_record('tripal_term', array('*'), $match);
-  if (count($term) == 0) {
-    $values = array(
-      'vocabulary_id' => $vocab->vocabulary_id,
-      'cvterm_id' => $cvterm->cvterm_id,
-    );
-    $values = chado_insert_record('tripal_term', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add term to tripal_term table..');
-      return FALSE;
-    }
-    $term = new stdClass();
-    $term->term_id = $values['term_id'];
-  }
-  else {
-    $values = array('publish' => 1);
-    chado_update_record('tripal_term', $match, $values);
-    $term = $term[0];
-  }
-
-  // Finally, add the tripal_term_usage record if it doesn't already exist.
-  $match = array('term_id' => $term->term_id);
-  $options = array('has_record' => TRUE);
-  if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
-    $values = array(
-      'term_id' => $term->term_id,
-      'data_table' => $vocab_usage->data_table,
-      'type_table' => $vocab_usage->type_table,
-      'field' =>  $vocab_usage->field,
-    );
-    $values = chado_insert_record('tripal_term_usage', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add term to tripal_term table..');
-      return FALSE;
-    }
-  }
-
-  // Clear the entity cache so that Drupal will read our
-  // hook_entity_info() implementation which now will have the entities
-  // described because we set the publish column to 1 in the tripal_term
-  // table.
-  global $language;
-  $langcode = $language->language;
-  cache_clear_all("entity_info:$langcode", 'cache');
-
-  return TRUE;
-}
 
 /**
  *

+ 88 - 36
tripal_entities/tripal_entities.install

@@ -13,30 +13,82 @@ function tripal_entities_install() {
   // that are available for use with entities.
   // @TODO: Ask Stephen why these are chado instead of drupal tables...
   chado_create_custom_table(
-    'tripal_vocabulary', 
-    tripal_entities_tripal_vocabulary_schema(), 
+    'tripal_vocabulary',
+    tripal_entities_tripal_vocabulary_schema(),
     TRUE
   );
   chado_create_custom_table(
-    'tripal_vocabulary_usage', 
-    tripal_entities_tripal_vocabulary_usage_schema(), 
+    'tripal_vocabulary_usage',
+    tripal_entities_tripal_vocabulary_usage_schema(),
     TRUE
   );
   chado_create_custom_table(
-    'tripal_term', 
-    tripal_entities_tripal_term_schema(), 
+    'tripal_term',
+    tripal_entities_tripal_term_schema(),
     TRUE
   );
   chado_create_custom_table(
-    'tripal_term_usage', 
-    tripal_entities_tripal_term_usage_schema(), 
+    'tripal_term_usage',
+    tripal_entities_tripal_term_usage_schema(),
     TRUE
   );
   chado_create_custom_table(
-    'tripal_term_relationship', 
-    tripal_entities_tripal_term_relationship_schema(), 
+    'tripal_term_relationship',
+    tripal_entities_tripal_term_relationship_schema(),
     TRUE
   );
+
+  // Unfortunately, some Chado base tables do not have a type_id, so we must
+  // take special action for those tables.  These include: organism and
+  // analysis. Until we can find an appropriate controlled vocabulary
+  // that is well supported by the community with types for these tables we
+  // will have to use in-house terms.
+
+  // Add a term to be used for an inherent 'type_id' for the organism table.
+  tripal_insert_cvterm(array(
+    'id' => 'local:organism',
+    'name' => 'organism',
+    'definition' => 'An individual form of life, such as a bacterium, protist, ' .
+      'fungus, plant, or animal, composed of a single cell or a complex of cells  ' .
+      'in which organelles or organs work together to carry out the various  ' .
+      'processes of life. (American Heritage® Dictionary of the English ' .
+      'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
+      'Harcourt Publishing Company).',
+    'cv_name' => 'local',
+  ));
+
+  // Add a term to be used for an inherent 'type_id' for the organism table.
+  tripal_insert_cvterm(array(
+    'id' => 'local:analysis',
+    'name' => 'analysis',
+    'definition' => 'A process as a method of studying the nature of something ' .
+      'or of determining its essential features and their relations. ' .
+      '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
+      'Dictionaries Ltd).',
+    'cv_name' => 'local',
+  ));
+
+  // We want to provide a set of commonly used entity types by default. This
+  // way when a user first installs Tripal there are some commonly used
+  // formats.
+  module_load_include('inc', 'tripal_entities', 'api/tripal_entities.api');
+  module_load_include('inc', 'tripal_entities', 'includes/tripal_entities.admin');
+
+  // Create the 'Organism' entity type. This uses the local:organism term.
+  $error = '';
+  $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
+  $cvterm = chado_generate_var('cvterm', $term);
+  if (!tripal_create_entity_type($cvterm, $error)) {
+    throw new Exception($error);
+  }
+
+  // Create the 'Organism' entity type. This uses the local:organism term.
+  $error = '';
+  $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
+  $cvterm = chado_generate_var('cvterm', $term);
+  if (!tripal_create_entity_type($cvterm, $error)) {
+    throw new Exception($error);
+  }
 }
 
 /**
@@ -158,8 +210,8 @@ function tripal_entities_tripal_entity_schema() {
 
 /**
  * The base table for Biological Data Type Entites.
- * This table contains a list of Biological Data Types. 
- * For the example above (5 genes and 10 mRNAs), there would only be two records in 
+ * This table contains a list of Biological Data Types.
+ * For the example above (5 genes and 10 mRNAs), there would only be two records in
  * this table one for "gene" and another for "mRNA".
  */
 function tripal_entities_tripal_bundle_schema() {
@@ -281,10 +333,10 @@ function tripal_entities_chado_entity_schema() {
 /**
  * A list of published vocabularies.
  *
- * Usage: This table will be used by the Entity type admin page that lets the site admin 
- *   specify which vocabularies should be used as entity types.  This table will only be 
- *   populated with vocabularies that are actually used within the Chado database.  This 
- *   table will also be used by web services to provide a list of all of the entity types 
+ * Usage: This table will be used by the Entity type admin page that lets the site admin
+ *   specify which vocabularies should be used as entity types.  This table will only be
+ *   populated with vocabularies that are actually used within the Chado database.  This
+ *   table will also be used by web services to provide a list of all of the entity types
  *   that are available for access.
  */
 function tripal_entities_tripal_vocabulary_schema() {
@@ -343,9 +395,9 @@ function tripal_entities_tripal_vocabulary_schema() {
 /**
  * A list of published terms.
  *
- * Usage: This table is used by web services to provide a list of all of the bundles 
- *   (i.e. vocabulary terms) that have data in the site.   It is also used by the Entity 
- *   administrative pages to allow the site admin to specify which terms should be 
+ * Usage: This table is used by web services to provide a list of all of the bundles
+ *   (i.e. vocabulary terms) that have data in the site.   It is also used by the Entity
+ *   administrative pages to allow the site admin to specify which terms should be
  *   publishable (i.e. used as bundles).
  */
 function tripal_entities_tripal_term_schema() {
@@ -402,13 +454,13 @@ function tripal_entities_tripal_term_schema() {
 }
 
 /**
- * 
  *
- * Specifies the source table in Chado where this entity will pull data.  Because 
- * vocabularies can be used in multiple tables there could be many entries here for each 
+ *
+ * Specifies the source table in Chado where this entity will pull data.  Because
+ * vocabularies can be used in multiple tables there could be many entries here for each
  * vocabulary.
  *
- * Usage: This table is used by web services when querying for all of the records of a 
+ * Usage: This table is used by web services when querying for all of the records of a
  * given type.  Web services must know where to look for records of a given term.
  */
 function tripal_entities_tripal_vocabulary_usage_schema(){
@@ -468,11 +520,11 @@ function tripal_entities_tripal_vocabulary_usage_schema(){
 /**
  *
  *
- * Specifies the source table in Chado where this bundle will pull data.  Because terms 
- * can be used in multiple tables there could be many entries here for each term. 
- * 
- * Note: this table contains the list of tables where a particular cvterm is used, 
- * whereas, the tripal_entity_type_source just provides a list of where cvterms from a 
+ * Specifies the source table in Chado where this bundle will pull data.  Because terms
+ * can be used in multiple tables there could be many entries here for each term.
+ *
+ * Note: this table contains the list of tables where a particular cvterm is used,
+ * whereas, the tripal_entity_type_source just provides a list of where cvterms from a
  * particular vocabulary might be found.
  */
 function tripal_entities_tripal_term_usage_schema() {
@@ -532,14 +584,14 @@ function tripal_entities_tripal_term_usage_schema() {
  *
  * Specifies the predicates used for the semantic web for all properties of a bundle.
  *
- * Usage: When fields are added to an entity then there must be some “relationship” term 
- * (i.e. predicate) that indicates the meaning of the relationship.  This predicate must 
- * itself be a cvterm from a vocabulary.  For all fields that are automatically added to 
- * bundles by tripal there should be a record here.  The site admin should be able to 
- * change these if desired, but there should be some sort of default set by Tripal 
- * itself.  This will require that all fields for all tables in Chado have some default 
- * predicate value.  Also, relationship between two different bundles (whether published 
- * or not) should also have a relationship predicate.  See the section in the 
+ * Usage: When fields are added to an entity then there must be some “relationship” term
+ * (i.e. predicate) that indicates the meaning of the relationship.  This predicate must
+ * itself be a cvterm from a vocabulary.  For all fields that are automatically added to
+ * bundles by tripal there should be a record here.  The site admin should be able to
+ * change these if desired, but there should be some sort of default set by Tripal
+ * itself.  This will require that all fields for all tables in Chado have some default
+ * predicate value.  Also, relationship between two different bundles (whether published
+ * or not) should also have a relationship predicate.  See the section in the
  * specification for how default predicates are set.
  */
 function tripal_entities_tripal_term_relationship_schema() {

+ 11 - 6
tripal_feature/tripal_feature.install

@@ -49,10 +49,13 @@ function tripal_feature_requirements($phase) {
  */
 function tripal_feature_install() {
 
-  // Add the feature properties vocabulary provided by Chado.
-  $obo_path = '{tripal_feature}/files/feature_property.obo';
-  $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
-  tripal_submit_obo_job(array('obo_id' => $obo_id));
+  // Note: the feature_property OBO that came with Chado v1.2 should not
+  // be automatically installed.  Some of the terms are duplicates of
+  // others in better maintained vocabularies.  New Tripal sites should
+  // use those.
+  // $obo_path = '{tripal_feature}/files/feature_property.obo';
+  // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
+  // tripal_submit_obo_job(array('obo_id' => $obo_id));
 
   // Add the materialized view.
   tripal_feature_add_organism_count_mview();
@@ -315,8 +318,10 @@ function tripal_feature_add_cvs() {
     'Contains types of relationships between features.'
   );
 
-  // the feature_property CV already exists... it comes with chado, but we need to
-  // add it just in case it doesn't get added before the feature module is installed
+  // The feature_property CV may already exists. It comes with Chado, but
+  // we need to  add it just in case it doesn't get added before the feature
+  // module is installed. But as of Tripal v3.0 the Chado version of this
+  // vocabulary is no longer loaded by default.
   tripal_insert_cv(
     'feature_property',
     'Stores properties about features'