Bladeren bron

Working to fix mapping of bundles to Chado tables

Stephen Ficklin 8 jaren geleden
bovenliggende
commit
2ad2077658

+ 20 - 14
tripal/api/tripal.entities.api.inc

@@ -165,29 +165,35 @@ function tripal_load_bundle_entity($values) {
  *
  * @param $bundle
  *   The newly created TripalBundle object.
+ * @param $args
+ *   Arguments used to create the TripalBundle, as well as
+ *   arugments from the form submission.
  */
-function hook_bundle_create(&$bundle) {
+function hook_bundle_create(&$bundle, $args) {
 
 }
 /**
  * Creates a new Tripal Entity type (i.e. bundle).
  *
- * @param $vocabulary
- *   The abbreviated vocabulary for the vocabulary (e.g. RO, SO, PATO).
- * @param $accession
- *   The unique term ID in the vocabulary $vocabulary (i.e. an accession).
- * @param $term_name
- *   A human-readable name for this term.  This will became the name that
- *   appears for the content type.  In practice, this should be the name
- *   of the term. (E.g. the name for SO:0000704 is gene).
+ * @param $args
+ *   An array of arguments that must include the following keys:
+ *     - vocabulary:  The abbreviated vocabulary for the vocabulary
+ *       (e.g. RO, SO, PATO).
+ *     - accession:  The unique term ID in the vocabulary $vocabulary
+ *       (i.e. an accession).
+ *     - term_name: A human-readable name for this term.  This will became
+ *       the name that appears for the content type.  In practice, this
+ *       should be the name of the term. (E.g. the name for SO:0000704 is gene).
  * @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_bundle($vocabulary, $accession, $term_name, &$error = '') {
+function tripal_create_bundle($args, &$error = '') {
+  $vocabulary = $args['vocabulary'];
+  $accession = $args['accession'];
+  $term_name = $args['term_name'];
 
   $transaction = db_transaction();
   try {
@@ -204,8 +210,8 @@ function tripal_create_bundle($vocabulary, $accession, $term_name, &$error = '')
       'accession' => $accession
     ));
     if (!$term) {
-      $args = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
-      $term = entity_get_controller('TripalTerm')->create($args);
+      $targs = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
+      $term = entity_get_controller('TripalTerm')->create($targs);
       $term = $term->save();
     }
 
@@ -229,7 +235,7 @@ function tripal_create_bundle($vocabulary, $accession, $term_name, &$error = '')
     $modules = module_implements('bundle_create');
     foreach ($modules as $module) {
       $function = $module . '_bundle_create';
-      $function($bundle);
+      $function($bundle, $args);
     }
 
     // Clear the entity cache so that Drupal will read our

+ 1 - 1
tripal/includes/TripalBundleController.inc

@@ -32,7 +32,7 @@ class TripalBundleController extends EntityAPIControllerExportable {
     $modules = module_implements('bundle_create');
     foreach ($modules as $module) {
       $function = $module . '_bundle_create';
-      $function($bundle);
+      $function($bundle, array());
     }
 
     return $bundle;

+ 17 - 10
tripal/includes/TripalBundleUIController.inc

@@ -73,10 +73,10 @@ class TripalBundleUIController extends EntityDefaultUIController {
  */
 function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
 
-  $entity_type = $form_state['build_info']['args'][0];
+  $bundle = $form_state['build_info']['args'][0];
   $term = NULL;
   $vocab = NULL;
-  if (preg_match('/bio_data_(\d+)/', $entity_type->name, $matches)) {
+  if (preg_match('/bio_data_(\d+)/', $bundle->name, $matches)) {
     $term = entity_load('TripalTerm', array('id' => $matches[1]));
     $term = reset($term);
     $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
@@ -86,6 +86,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
   // Add a validate and submit handler to save the data in this form.
   $form['#validate'] = array('tripal_tripal_bundle_form_validate');
   $form['#submit'] = array('tripal_tripal_bundle_form_submit');
+  $form['#bundle'] = $bundle;
 
   // @TODO: Move this into a css file.
   $form['#attached']['css'] = array(
@@ -149,7 +150,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
       displayed as part of the list on the <em>Add new content page</em>. It is recommended that
       this name begin with a capital letter and contain only letters, numbers, and spaces.
       This name must be unique.'),
-    '#default_value' => $entity_type->label,
+    '#default_value' => $bundle->label,
   );
 
   $form['description'] = array(
@@ -172,7 +173,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
 
   // Set Title Format.
   //-------------------------
-  $title_format = tripal_get_title_format($entity_type);
+  $title_format = tripal_get_title_format($bundle);
 
   $form['set_titles'] = array(
     '#type' => 'fieldset',
@@ -192,7 +193,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
       <p>Keep in mind that it might be confusing to users if more than
       one page has the same title. We recommend you <strong>choose a combination of tokens that
       will uniquely identify your content</strong>.</p>',
-      array('%type' => $entity_type->label)),
+      array('%type' => $bundle->label)),
   );
 
   $form['set_titles']['title_format'] = array(
@@ -214,7 +215,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
     '#collapsed' => TRUE
   );
 
-  $tokens = tripal_get_entity_tokens($entity_type);
+  $tokens = tripal_get_entity_tokens($bundle);
   $form['set_titles']['tokens'] = array(
     '#type' => 'hidden',
     '#value' => serialize($tokens)
@@ -227,7 +228,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
 
   // Set URL Alias Pattern.
   //-------------------------
-  $url_pattern = tripal_get_bundle_variable('url_format', $entity_type->id, '');
+  $url_pattern = tripal_get_bundle_variable('url_format', $bundle->id, '');
   if (!$url_pattern) $url_pattern = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
 
   $form['url'] = array(
@@ -245,7 +246,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
     This allows you to present more friendly, informative URLs to your user.</p>
     <p><strong>You must choose a combination of tokens that results in a unique path for
     each page!</strong></p>',
-    array('%type' => $entity_type->label)),
+    array('%type' => $bundle->label)),
   );
 
   $form['url']['url_pattern'] = array(
@@ -259,7 +260,7 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
     '#rows' => 1
   );
 
-  $tokens = tripal_get_entity_tokens($entity_type, array('required only' => TRUE));
+  $tokens = tripal_get_entity_tokens($bundle, array('required only' => TRUE));
   $form['url']['tokens'] = array(
     '#type' => 'hidden',
     '#value' => serialize($tokens)
@@ -463,7 +464,13 @@ function tripal_admin_add_type_form_submit($form, &$form_state) {
     $term = tripal_load_term_entity(array('vocabulary' => $vocabulary, 'accession' => $accession));
     if (!$term) {
       $error = '';
-      $success = tripal_create_bundle($vocabulary, $accession, $term_name, $error);
+      $args = array(
+        'vocabulary' => $vocabulary,
+        'accession' => $accession,
+        'term_name' => $term_name,
+        'form_values' => $form_state['values'],
+      );
+      $success = tripal_create_bundle($args, $error);
       if (!$success) {
         drupal_set_message($error, 'error');
         $form_state['redirect'] = "admin/structure/bio_data";

+ 1 - 1
tripal/includes/tripal.fields.inc

@@ -86,7 +86,7 @@ function tripal_field_formatter_info_alter(&$info) {
  * This is a Triapl defined hook and is called in the TripalBundle::create()
  * function to allow modules to perform tasks when a bundle is created.
  */
-function tripal_bundle_create($bundle) {
+function tripal_bundle_create($bundle, $args) {
   $field_type = 'rdfs__type';
   $field_name = 'rdfs__type';
 

+ 69 - 3
tripal_chado/includes/tripal_chado.fields.inc

@@ -10,9 +10,17 @@
  *  The TripalBundle object.
  */
 
-function tripal_chado_bundle_create($bundle) {
+function tripal_chado_bundle_create($bundle, $args) {
   $entity_type = $bundle->type;
 
+  //dpm($args);
+  // Before adding fields to the bundle we need to story the Chado table that
+  // this bundle maps to.  That information is in the $args['form_values']
+  // array.  The $args['form_values'] array contains values that indicate the
+  // Chado table.
+  // TODO: we need to store the chado table and type field used for this
+  // bundle.
+
   // Create/Add the new fields for this bundle.
   tripal_chado_bundle_create_fields($entity_type, $bundle);
 
@@ -1653,12 +1661,70 @@ function tripal_chado_field_instance_settings_form_alter(&$form, $form_state) {
     '#type' => 'fieldset',
     '#title' => 'Chado Mapping',
     '#description' => t('This field maps to data in Chado to the following table:'),
-    '#prefix' => '<div id = "tripal-field-term-fieldset">',
-    '#suffix' => '</div>',
   );
   $form['chado_mapping']['details'] = array(
     '#type' => 'item',
     '#markup' => theme_table($table),
   );
 
+}
+
+/**
+ * Implements hook_form_FROM_ID_alter()
+ */
+function tripal_chado_form_tripalbundle_form_alter(&$form, $form_state) {
+  global $language;
+  $bundle = $form['#bundle'];
+
+  $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
+  $term = reset($term);
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+  $mapped_table = chado_get_cvterm_mapping($params);
+  $chado_table = $mapped_table->chado_table;
+  $chado_column = $mapped_table->chado_field;
+
+  // Construct a table for the vocabulary information.
+  $headers = array();
+  $rows = array();
+  $rows[] = array(
+    array(
+      'data' => 'Chado Table',
+      'header' => TRUE,
+      'width' => '20%',
+    ),
+    $chado_table
+  );
+  $rows[] = array(
+    array(
+      'data' => 'Type Column',
+      'header' => TRUE,
+      'width' => '20%',
+    ),
+    $chado_column
+  );
+  $table = array(
+    'header' => $headers,
+    'rows' => $rows,
+    'attributes' => array(
+    ),
+    'sticky' => FALSE,
+    'caption' => '',
+    'colgroups' => array(),
+    'empty' => '',
+  );
+
+  $form['chado_mapping'] = array(
+    '#type' => 'item',
+    '#title' => 'Chado Mapping',
+    '#markup' => theme_table($table),
+    '#description' => t('This content type maps to the table in Chado
+        listed above.  Chado allows multiple data types to be housed
+        in a single table. Therefore, the column that is used to
+        differentiate between data types is also listed above.'),
+    '#weight' => 0,
+  );
 }

+ 4 - 4
tripal_chado/includes/tripal_chado.mapping.inc

@@ -100,10 +100,10 @@ function tripal_chado_map_cvterms() {
 function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
   // check if the record exists
   $record = db_select('tripal_cvterm_mapping', 'tcm')
-  ->fields('tcm', array('mapping_id'))
-  ->condition('cvterm_id', $cvterm_id)
-  ->execute()
-  ->fetchField();
+    ->fields('tcm', array('mapping_id'))
+    ->condition('cvterm_id', $cvterm_id)
+    ->execute()
+    ->fetchField();
   // insert records into the tripal_cvterm_mapping table.
   if (!$record) {
     db_insert('tripal_cvterm_mapping')

+ 24 - 0
tripal_chado/includes/tripal_chado.semweb.inc

@@ -182,6 +182,30 @@ function tripal_chado_populate_vocab_EDAM() {
     'definition' => 'Biological or biomedical data has been rendered into an image, typically for display on screen.',
   ));
   tripal_associate_chado_semweb_term(NULL, 'eimage_id', $term);
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'data:1274',
+    'name' => 'Map',
+    'cv_name' => 'EDAM',
+    'definition' => 'A map of (typically one) DNA sequence annotated with positional or non-positional features.',
+  ));
+  tripal_associate_chado_semweb_term(NULL, 'eimage_id', $term);
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'data:1278',
+    'name' => 'Genetic map',
+    'cv_name' => 'EDAM',
+    'definition' => 'A map showing the relative positions of genetic markers in a nucleic acid sequence, based on estimation of non-physical distance such as recombination frequencies.',
+  ));
+  tripal_associate_chado_semweb_term('featuremap', 'featuremap_id', $term);
+
+  $term = tripal_insert_cvterm(array(
+    'id' => 'data:1280',
+    'name' => 'Physical map',
+    'cv_name' => 'EDAM',
+    'definition' => 'A map of DNA (linear or circular) annotated with physical features or landmarks such as restriction sites, cloned DNA fragments, genes or genetic markers, along with the physical distances between them. Distance in a physical map is measured in base pairs. A physical map might be ordered relative to a reference map (typically a genetic map) in the process of genome sequencing.',
+  ));
+  tripal_associate_chado_semweb_term('featuremap', 'featuremap_id', $term);
 }
 
 /**

+ 6 - 0
tripal_chado/includes/tripal_chado.setup.inc

@@ -425,6 +425,12 @@ function tripal_chado_prepare_chado() {
       throw new Exception($error['!message']);
     }
 
+    // Create the 'Project' entity type. This uses the local:project term.
+    $error = '';
+    if (!tripal_create_bundle('data', '1278', 'Genetic map', $error)) {
+      throw new Exception($error['!message']);
+    }
+
     // Initialize the population of the tripal_cvterm_mapping table.
     tripal_chado_map_cvterms();