Browse Source

Added a field to support synonym linker tables

Stephen Ficklin 9 years ago
parent
commit
241ac2efc3

+ 7 - 2
tripal/api/tripal.entities.api.inc

@@ -74,8 +74,10 @@ function tripal_load_vocab_entity($values) {
  * Retrieves a TripalBundle entity that matches the given arguments.
  *
  * @param $values
- *   An associative array used to match a bundle.  Valid keys may be 'name' or
- *   'label' (e.g. array('name' => 'bio-data_234').
+ *   An associative array used to match a bundle.  Valid keys may:
+ *     - name:  the bundle name (e.g. 'bio-data_234')
+ *     - label: the bundle label (e.g. 'Organism')
+ *     - term_id: the term ID to which the bundle belongs
  *
  * @return
  *   A TripalBundle entity object or NULL if not found.
@@ -90,6 +92,9 @@ function tripal_load_bundle_entity($values) {
   if (array_key_exists('label', $values)) {
     $query->condition('tb.label', $values['label']);
   }
+  if (array_key_exists('term_id', $values)) {
+    $query->condition('tb.term_id', $values['term_id']);
+  }
   $bundle = $query->execute()->fetchObject();
 
   if ($bundle) {

+ 1 - 1
tripal/tripal.module

@@ -862,7 +862,7 @@ function tripal_import_api() {
 function tripal_field_storage_info() {
   return array(
     'tripal_no_storage' => array(
-      'label' => t(''),
+      'label' => t('Tripal'),
       'description' => t('The NULL storage is a placeholder for field values
           that are not stored in any storage backend (e.g. entity types).'),
       'settings' => array(),

+ 2 - 2
tripal_chado/includes/fields/cvterm.inc

@@ -121,7 +121,7 @@ function tripal_chado_cvterm_widget(&$widget, $form, $form_state, $field,
   }
 
   // Check $form_state['values'] to see if an AJAX call set the values.
-  if (array_key_exists('values', $form_state)) {
+  if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
     $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
     $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
     $is_not = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_not');
@@ -263,7 +263,7 @@ function theme_tripal_chado_cvterm_widget($variables) {
 
   $layout = "
     <div class=\"annotation-cvterm-widget\">
-      <div class=\"secondary-dbxref-widget-item\">" .
+      <div class=\"annotation-cvterm-widget-item\">" .
         drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
       </div>
       <div class=\"annotation-cvterm-widget-item\">" .

+ 1 - 1
tripal_chado/includes/fields/dbxref.inc

@@ -77,7 +77,7 @@ function tripal_chado_dbxref_widget(&$widget, $form, $form_state, $field,
   }
 
   // Check $form_state['values'] to see if an AJAX call set the values.
-  if (array_key_exists('values', $form_state)) {
+  if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
     $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name);
     $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__' . $fkey);
     $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__dbxref_id');

+ 17 - 17
tripal_chado/includes/fields/dbxref_id.inc

@@ -73,14 +73,14 @@ function tripal_chado_dbxref_id_widget(&$widget, $form, $form_state, $field, $in
   // If we are here because our parent was triggered in a form submit
   // then that means an ajax call was made and we don't want the fieldset to
   // be closed when it returns from the ajax call.
-  $collapsed = TRUE;
-  if (array_key_exists('triggering_element', $form_state) and
-      $form_state['triggering_element']['#parents'][0] == $field_name) {
-    $collapsed = FALSE;
-  }
-  if ($dbxref_id) {
-    $collapsed = FALSE;
-  }
+//   $collapsed = TRUE;
+//   if (array_key_exists('triggering_element', $form_state) and
+//       $form_state['triggering_element']['#parents'][0] == $field_name) {
+//     $collapsed = FALSE;
+//   }
+//   if ($dbxref_id) {
+//     $collapsed = FALSE;
+//   }
 
   $schema = chado_get_schema('dbxref');
   $options = tripal_get_db_select_options();
@@ -96,8 +96,8 @@ function tripal_chado_dbxref_id_widget(&$widget, $form, $form_state, $field, $in
     '#description' =>  $element['#description'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
     '#theme' => 'tripal_chado_dbxref_id_widget',
-    '#collapsible' => FALSE,
-    '#collapsed' => $collapsed,
+    //'#collapsible' => TRUE,
+    //'#collapsed' => $collapsed,
   );
 
   $widget['value'] = array(
@@ -254,18 +254,18 @@ function theme_tripal_chado_dbxref_id_widget($variables) {
     </div>
   ";
 
-  $classes = array();
-  $classes[] = 'collapsible';
-  $theme_settings = $element['#theme_settings'];
-  if ($theme_settings['#collapsed'] == FALSE) {
-    $classes[] = 'collapsed';
-  }
+//   $classes = array();
+//   $classes[] = 'collapsible';
+//   $theme_settings = $element['#theme_settings'];
+//   if ($theme_settings['#collapsed'] == FALSE) {
+//     $classes[] = 'collapsed';
+//   }
   $fieldset = array(
     '#title' => $element['#title'],
     '#value' => '',
     '#description' => $element['#description'],
     '#children' => $layout,
-    '#attributes' => array('class' => $classes),
+//    '#attributes' => array('class' => $classes),
   );
 
   return theme('fieldset', array('element' => $fieldset));

+ 97 - 24
tripal_chado/includes/tripal_chado.setup.inc

@@ -265,6 +265,7 @@ function tripal_chado_prepare_chado() {
     // to warn the user if the current version is not compatible
     $version = chado_get_version(FALSE, FALSE);
 
+
     ///////////////////////////////////////////////////////////////////////////
     //                          Chado DB Module
     ///////////////////////////////////////////////////////////////////////////
@@ -293,7 +294,7 @@ function tripal_chado_prepare_chado() {
     // Add the synonym_type vocabulary.
     tripal_insert_cv(
       'synonym_type',
-      'A local vocabulary added for synonym types.'
+      'A vocabulary used for synonym types (e.g. exact, broad, narrow, related).'
     );
 
     // Add the Chado ontology CV.
@@ -303,6 +304,19 @@ function tripal_chado_prepare_chado() {
     //tripal_submit_obo_job(array('obo_id' => $obo_id));
 
 
+    ///////////////////////////////////////////////////////////////////////////
+    //                          Chado Misc
+    ///////////////////////////////////////////////////////////////////////////
+    tripal_insert_cv(
+      'synonym_type',
+      'A vocabulary for storing synonym types.'
+    );
+    // set the default vocabularies
+    tripal_set_default_cv('synonym', 'type_id', 'synonym_type');
+
+    // Add cvterms.
+    tripal_insert_misc_cvterms();
+
 
     /////////////////////////////////////////////////////////////////////////////
     //                          Chado Organism Module
@@ -312,7 +326,7 @@ function tripal_chado_prepare_chado() {
       'Contains properties for organisms'
     );
 
-    // set the default vocabularies
+    // Set the default vocabularies.
     tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
 
 
@@ -483,11 +497,11 @@ function tripal_chado_prepare_chado() {
       '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).',
+        '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',
     ));
 
@@ -496,9 +510,9 @@ function tripal_chado_prepare_chado() {
       '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).',
+        'or of determining its essential features and their relations. ' .
+        '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
+        'Dictionaries Ltd).',
       'cv_name' => 'local',
     ));
 
@@ -506,17 +520,26 @@ function tripal_chado_prepare_chado() {
       'id' => 'local:project',
       'name' => 'project',
       'definition' => 'A plan or proposal for accomplishing something. ' .
-      '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
-      'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
+        '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
+        'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
       'cv_name' => 'local',
     ));
 
     // For the TripalBundle entities we will want to associate the cvterm_id,
     // and the chado table and field that it maps to.  We will use a few
     // variables to do this:
-    tripal_insert_variable('chado_cvterm_id', 'The cvterm_id that a TripalBundle maps to.');
-    tripal_insert_variable('chado_table', 'The name of the table to which a TripalBundle maps.');
-    tripal_insert_variable('chado_column', 'The name of the column within the table that a TripalBundle maps to.');
+    tripal_insert_variable(
+      'chado_cvterm_id',
+      'The cvterm_id that a TripalBundle maps to.'
+    );
+    tripal_insert_variable(
+      'chado_table',
+      'The name of the table to which a TripalBundle maps.'
+    );
+    tripal_insert_variable(
+      'chado_column',
+      'The name of the column within the table that a TripalBundle maps to.'
+    );
 
     // 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
@@ -1773,29 +1796,28 @@ function tripal_contact_add_cvs() {
   // Add the cv for contact properties. This is a default vocabulary in the event
   // that a user does not want to use the tripal_contact vocabulary
   tripal_insert_cv(
-      'contact_property',
-      'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+    'contact_property',
+    'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
   );
 
   // add the cv for the contact type. This is a default vocabulary in the event
   // that a user does not want to use the tripal_contact vocabulary
   tripal_insert_cv(
-      'contact_type',
-      'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+    'contact_type',
+    'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
   );
 
   // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
   tripal_insert_cv(
-      'tripal_contact',
-      'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
+    'tripal_contact',
+    'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
   );
 
   // add the cv for contact relationships
   tripal_insert_cv(
-      'contact_relationship',
-      'Contains types of relationships between contacts.'
+    'contact_relationship',
+    'Contains types of relationships between contacts.'
   );
-
 }
 /**
  * Add any custom tables needed by this module.
@@ -2096,4 +2118,55 @@ function tripal_cv_create_tripal_obo_temp() {
     $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
     chado_query($sql);
   }
+}
+
+/**
+ * Adds generic CVterms.
+ */
+function tripal_insert_misc_cvterms() {
+  tripal_insert_cvterm(
+    array(
+      'name' => 'exact',
+      'id' => "local:exact",
+      'definition' => 'An exact equivalent; interchangeable with the term name.',
+      'is_obsolete' => 0,
+      'cv_name' => 'synonym_type',
+      'is_relationship' => FALSE
+    ),
+    array('update_existing' => TRUE)
+  );
+
+  tripal_insert_cvterm(
+    array(
+      'name' => 'broad',
+      'id' => "local:broad",
+      'definition' => 'The synonym is broader than the name.',
+      'is_obsolete' => 0,
+      'cv_name' => 'synonym_type',
+      'is_relationship' => FALSE
+    ),
+    array('update_existing' => TRUE)
+  );
+  tripal_insert_cvterm(
+    array(
+      'name' => 'narrow',
+      'id' => "local:narrow",
+      'definition' => 'The synonym is narrower or more precise than the name.',
+      'is_obsolete' => 0,
+      'cv_name' => 'synonym_type',
+      'is_relationship' => FALSE
+    ),
+    array('update_existing' => TRUE)
+  );
+  tripal_insert_cvterm(
+    array(
+      'name' => 'related',
+      'id' => "local:related",
+      'definition' => 'The synonym is related in some way.',
+      'is_obsolete' => 0,
+      'cv_name' => 'synonym_type',
+      'is_relationship' => FALSE
+    ),
+    array('update_existing' => TRUE)
+  );
 }

+ 6 - 12
tripal_chado/theme/css/tripal_chado.css

@@ -1,23 +1,17 @@
 @CHARSET "UTF-8";
 
-.primary-dbxref-widget-item {
-   float: left;
-   margin-right: 10px;
-}
-.primary-dbxref-widget-links {
-   clear: both;
-}
-.secondary-dbxref-widget-item {
+.synonym-widget-item,
+.primary-dbxref-widget-item,
+.secondary-dbxref-widget-item,
+.kvproperty-adder-widget-item {
    float: left;
    margin-right: 10px;
 }
+
+.primary-dbxref-widget-links,
 .secondary-dbxref-widget-links {
    clear: both;
 }
-.kvproperty-adder-widget-item {
-   float: left;
-   margin-right: 10px;
-}
 
 .residues-formatter {
    border: 1px solid #dddddd;

+ 93 - 1
tripal_chado/tripal_chado.module

@@ -550,6 +550,19 @@ function tripal_chado_field_info() {
         'active' => TRUE
       ),
     ),
+
+    'synonym' => array(
+      'label' => t('Synonyms'),
+      'description' => t('Adds an alternative name (synonym or alias) to this record..'),
+      'default_widget' => 'tripal_chado_synonym_widget',
+      'default_formatter' => 'tripal_chado_synonym_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
   );
   return $fields;
 }
@@ -609,6 +622,10 @@ function tripal_chado_field_widget_info() {
       'label' => t('Property'),
       'field types' => array('kvproperty'),
     ),
+    'tripal_chado_synonym_widget' => array(
+      'label' => t('Synonyms'),
+      'field types' => array('synonym'),
+    ),
   );
 }
 /**
@@ -656,6 +673,10 @@ function tripal_chado_field_formatter_info() {
       'label' => t('Property'),
       'field types' => array('kvproperty')
     ),
+    'tripal_chado_synonym_formatter' => array(
+      'label' => t('Synonyms'),
+      'field types' => array('synonym')
+    ),
   );
 }
 
@@ -750,6 +771,11 @@ function tripal_chado_field_formatter_view($entity_type, $entity, $field,
       tripal_chado_kvproperty_formatter($element, $entity_type, $entity, $field,
           $instance, $langcode, $items, $display);
       break;
+    case 'tripal_chado_synonym_formatter':
+      module_load_include('inc', 'tripal_chado', 'includes/fields/synonym');
+      tripal_chado_synonym_formatter($element, $entity_type, $entity, $field,
+          $instance, $langcode, $items, $display);
+      break;
   }
   return $element;
 }
@@ -814,6 +840,11 @@ function tripal_chado_field_widget_form(&$form, &$form_state, $field,
       module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
       tripal_chado_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
+    case 'tripal_chado_synonym_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/synonym');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/synonym');
+      tripal_chado_synonym_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
   }
   return $widget;
 }
@@ -961,6 +992,10 @@ function tripal_chado_theme($existing, $type, $theme, $path) {
       'render element' => 'element',
       'file' => 'includes/fields/cvterm.inc',
     ),
+    'tripal_chado_synonym_widget' => array(
+      'render element' => 'element',
+      'file' => 'includes/fields/synonym.inc',
+    ),
     'tripal_chado_kvproperty_addr_widget' => array(
       'render element' => 'element',
       'file' => 'includes/fields/dbxref_id.inc',
@@ -1097,6 +1132,18 @@ function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
   if (chado_table_exists($cvterm_table)) {
     tripal_chado_add_bundle_cvterm_class_adder_field($entity_type, $bundle_name, $cvterm_table, $bundle_data['data_table']);
   }
+
+
+  ////
+  //
+  // Synonym table fields.
+  //
+  // Check to see if there are any cvterm tables with FKs to this
+  // base table. If so, add the fields for that type of table.
+  $syn_table = $bundle_data['data_table'] . '_synonym';
+  if (chado_table_exists($cvterm_table)) {
+    tripal_chado_add_bundle_synonym_field($entity_type, $bundle_name, $syn_table, $bundle_data['data_table']);
+  }
 }
 
 /**
@@ -1150,6 +1197,51 @@ function tripal_chado_add_bundle_dbxref_field($entity_type_name, $bundle_name, $
   }
   tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
 }
+/**
+ * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
+ *
+ * @param $entity_type
+ * @param $bundle_name
+ * @param $base_table
+ * @param $dbxref_table
+ */
+function tripal_chado_add_bundle_synonym_field($entity_type_name, $bundle_name, $syn_table, $base_table) {
+  // We already have a dbxref_id field.
+  $field_name = $syn_table;
+  $schema = chado_get_schema($syn_table);
+  $pkey = $schema['primary key'][0];
+
+  // Initialize the field array.
+  $field_info = array(
+    'field_type' => 'synonym',
+    'widget_type' => 'tripal_fields_synonym_widget',
+    'widget_settings' => array('display_label' => 1),
+    'description' => '',
+    'label' => 'Synonyms',
+    'is_required' => 0,
+    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+    'storage' => 'field_chado_storage',
+    'field_settings' => array(
+      // The Chado table that this field maps to.
+      'chado_table' => $syn_table,
+      // The column in the chado table that this field maps to.
+      'chado_column' => $pkey,
+      // The base table that this field is connected to.
+      'base_table' => $base_table,
+      'semantic_web' => array(
+        // The type is the term from a vocabulary that desribes this field..
+        'type' => '',
+        // The namepsace for the vocabulary (e.g. 'foaf').
+        'ns' => '',
+        // The URL for the namespace.  It must be that the type can be
+        // appended to the URL.
+        'nsurl' => '',
+      ),
+    ),
+  );
+
+  tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
+}
 /**
  * Adds the fields for managing properties that are stored in a prop table.
  *
@@ -1420,7 +1512,7 @@ function tripal_chado_get_table_column_field_default($table_name, $schema, $colu
     $field['field_type'] = 'md5checksum';
     $field['widget_type'] = 'tripal_chado_md5checksum_checkbox_widget';
     $field['label'] = 'MD5 Checksum';
-    $field['description'] = 'Generating MD5 checksum for the sequence.';
+    $field['description'] = 'Generate an MD5 checksum for the sequence.';
   }
   elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
     $field['field_type'] = 'seqlen';