Sfoglia il codice sorgente

Tripal: Bulk loader -Added insert_unique and made it default, allow insertion of non-unique records, bookeeping of what was inserted w/ revert capabilities; <br> Genetic -API to retrieve genotype experiments for a feature, feature genotypes template and added content to feature node page

laceysanderson 14 anni fa
parent
commit
f5bd65ba74

+ 31 - 0
theme_tripal/tripal_feature/tripal_feature_genotypes.tpl.php

@@ -0,0 +1,31 @@
+
+<?php
+$feature = $variables['node']->feature;
+$genotypes = $variables['tripal_feature']['genotype_experiments'];
+dpm($feature, 'feature');
+?>
+
+<?php if(count($genotypes) > 0){ ?>
+<div id="tripal_feature-genotypes-box" class="tripal_feature-info-box tripal-info-box">
+  <div class="tripal_feature-info-box-title tripal-info-box-title">Genotypes</div>
+  <div class="tripal_feature-info-box-desc tripal-info-box-desc">
+    Genotypes of this <?php print $feature->type_id->name; ?> in various germplasm
+  </div>
+  <table id="tripal_feature-genotypes-table" class="tripal_feature-table tripal-table tripal-table-horz">
+    <tr><th>Germplasm</th><th>Genotype</th></tr>
+    <?php foreach ($genotypes as $g) { 
+        $genotype = $g->nd_experiment_genotype->genotype_id->description;
+        if (preg_match('/no sequence data/',$genotype)) { $genotype = "<font colour='grey'>".$genotype.'</font>'; }
+        $stock_name = $g->nd_experiment_stock->stock_id->name;
+        if ($g->nd_experiment_stock->stock_id->nid) {
+          $stock_link = 'node/'.$g->nd_experiment_stock->stock_id->nid;
+          $stock = l($stock_name, $stock_link);
+        } else {
+          $stock = $stock_name;
+        }
+    ?>
+      <tr><td><?php print $stock; ?></td><td><?php print $genotype; ?></td></tr>
+    <?php } ?>
+  </table>
+</div>
+<?php } ?>

+ 3 - 3
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -149,7 +149,7 @@ function tripal_bulk_loader_modify_template_base_form ($form_state = NULL, $mode
           ),
           'mode' => array(
             '#type' => 'item',
-            '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert',
+            '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert_unique',
           ),
           'new_priority' => array(
             '#type' => 'select',
@@ -661,12 +661,12 @@ function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
     '#title' => 'Action to take when Loading Record',
     '#options' => array(
       'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
-      'insert' => 'INSERT: Insert the record if it doesn\'t already exist',
+      'insert' => 'INSERT: Insert the record',
       'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in',
       'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire spreadsheet',
       'insert_unique' => 'INSERT UNIQUE: Only insert record if there isn\'t a record with the same values',
     ),
-    '#default_value' => 'insert'
+    '#default_value' => 'insert_unique'
   );
   
   $form['edit_record']['submit-edit_record'] = array(

+ 9 - 5
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -106,7 +106,7 @@ function tripal_bulk_loader_load_data($nid) {
     
     foreach ($record_array['fields'] as $field_index => $field_array) {
       $default_data[$priority]['table'] = $record_array['table'];
-      $default_data[$priority]['mode'] = ($record_array['mode']) ? $record_array['mode'] : 'insert';
+      $default_data[$priority]['mode'] = ($record_array['mode']) ? $record_array['mode'] : 'insert_unique';
       $default_data[$priority]['record_id'] = $record_array['record_id'];
       $record2priority[$record_array['record_id']] = $priority;
       $default_data[$priority]['required'][$field_array['field']] = $field_array['required'];
@@ -216,6 +216,7 @@ function tripal_bulk_loader_load_data($nid) {
       // if insert unique then check to ensure unique
       if (preg_match('/insert_unique/',$table_data['mode'])) {
         $unique = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, array('has_record'=>TRUE));
+        //print 'Unique?'.print_r(array('table' => $table, 'columns' => array_keys($table_desc['fields']), 'values' => $values),TRUE).' returns '.$unique."\n";
         if ($unique > 0) {
           continue;
         }
@@ -262,10 +263,13 @@ function tripal_bulk_loader_load_data($nid) {
           
         } //end of if insert was successful
       } else {
-        // No record on select
-        $msg = $table_data['record_id'].' ('.$table_data['mode'].') No Matching record in '.$table.' where values:'.print_r($values,TRUE);
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING); 
-        $data[$priority]['error'] = TRUE;        
+        $exists = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, array('has_record'=>TRUE));
+        if (!$exists) {
+          // No record on select
+          $msg = $table_data['record_id'].' ('.$table_data['mode'].') No Matching record in '.$table.' where values:'.print_r($values,TRUE);
+          watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING); 
+          $data[$priority]['error'] = TRUE;        
+        }
       }
 
     } // end of foreach table in default data array

+ 62 - 45
tripal_core/tripal_core.api.inc

@@ -858,50 +858,52 @@ function tripal_core_generate_chado_var($table, $values) {
     }
     
     // recursively follow foreign key relationships nesting objects as we go------------------------
-    foreach ($table_desc['foreign keys'] as $foreign_key_array) {
-      $foreign_table = $foreign_key_array['table'];
-      foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
-        // Note: Foreign key is the field in the current table whereas primary_key is the field in 
-        // the table referenced by the foreign key
-        
-        //Dont do anything if the foreign key is empty
-        if (empty($object->{$foreign_key})) {
-          break;
-        }
-        
-        // get the record from the foreign table
-        $foreign_values = array($primary_key => $object->{$foreign_key});
-        $foreign_object = tripal_core_generate_chado_var($foreign_table, $foreign_values);
-  
-        // add the foreign record to the current object in a nested manner
-        $object->{$foreign_key} = $foreign_object;
-        
-        // Flatten expandable_x arrays so only in the bottom object
-        if (is_array($object->{$foreign_key}->expandable_fields)) {
-          $object->expandable_fields = array_merge(
-            $object->expandable_fields, 
-            $object->{$foreign_key}->expandable_fields
-          );
-          unset($object->{$foreign_key}->expandable_fields);
-        }
-        if (is_array($object->{$foreign_key}->expandable_tables)) {
-          $object->expandable_tables = array_merge(
-            $object->expandable_tables, 
-            $object->{$foreign_key}->expandable_tables
-          );
-          unset($object->{$foreign_key}->expandable_tables);
-        }
-        if (is_array($object->{$foreign_key}->expandable_nodes)) {
-          $object->expandable_nodes = array_merge(
-            $object->expandable_nodes, 
-            $object->{$foreign_key}->expandable_nodes
-          );
-          unset($object->{$foreign_key}->expandable_nodes);
-        }
-      }
-    }    
+    if ($table_desc['foreign keys']) {
+      foreach ($table_desc['foreign keys'] as $foreign_key_array) {
+        $foreign_table = $foreign_key_array['table'];
+        foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
+          // Note: Foreign key is the field in the current table whereas primary_key is the field in 
+          // the table referenced by the foreign key
+          
+          //Dont do anything if the foreign key is empty
+          if (empty($object->{$foreign_key})) {
+            break;
+          }
+          
+          // get the record from the foreign table
+          $foreign_values = array($primary_key => $object->{$foreign_key});
+          $foreign_object = tripal_core_generate_chado_var($foreign_table, $foreign_values);
     
-    $results[$key] = $object;
+          // add the foreign record to the current object in a nested manner
+          $object->{$foreign_key} = $foreign_object;
+          
+          // Flatten expandable_x arrays so only in the bottom object
+          if (is_array($object->{$foreign_key}->expandable_fields)) {
+            $object->expandable_fields = array_merge(
+              $object->expandable_fields, 
+              $object->{$foreign_key}->expandable_fields
+            );
+            unset($object->{$foreign_key}->expandable_fields);
+          }
+          if (is_array($object->{$foreign_key}->expandable_tables)) {
+            $object->expandable_tables = array_merge(
+              $object->expandable_tables, 
+              $object->{$foreign_key}->expandable_tables
+            );
+            unset($object->{$foreign_key}->expandable_tables);
+          }
+          if (is_array($object->{$foreign_key}->expandable_nodes)) {
+            $object->expandable_nodes = array_merge(
+              $object->expandable_nodes, 
+              $object->{$foreign_key}->expandable_nodes
+            );
+            unset($object->{$foreign_key}->expandable_nodes);
+          }
+        }
+      }    
+      
+      $results[$key] = $object;
+    }
   }
   
     // check only one result returned
@@ -953,9 +955,18 @@ function tripal_core_generate_chado_var($table, $values) {
  *
  * @ingroup tripal_api
  */
-function tripal_core_expand_chado_vars ($object, $type, $to_expand) {
+function tripal_core_expand_chado_vars ($object, $type, $to_expand, $values = NULL) {
   $base_table = $object->tablename;
   
+  // check to see if they are expanding an array of objects
+  if (is_array($object)) {
+    foreach ($object as $index => $o) {
+      $object[$index] = tripal_core_expand_chado_vars($o,$type,$to_expand);
+    }
+    return $object;
+  }
+  
+  
   switch ($type) {
     case "field": //--------------------------------------------------------------------------------
       if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
@@ -1007,9 +1018,14 @@ function tripal_core_expand_chado_vars ($object, $type, $to_expand) {
         foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
           if (!$object->{$right}) { break; }
           
+          if (is_array($values)) { 
+            $values = array_merge($values, array($left => $object->{$right}) );
+          } else {
+            $values = array($left => $object->{$right});
+          }
           $foreign_object = tripal_core_generate_chado_var(
             $foreign_table,
-            array($left => $object->{$right})
+            $values
           );
   
           if ($foreign_object) {
@@ -1020,6 +1036,7 @@ function tripal_core_expand_chado_vars ($object, $type, $to_expand) {
       } else {
         //We need to recurse -the table has a relationship to one of the nested objects
         foreach ((array) $object as $field_name => $field_value) {
+          // if we have a nested object ->expand the table in it
           if (is_object($field_value)) {
             $object->{$field_name} = tripal_core_expand_chado_vars(
               $field_value,

+ 85 - 22
tripal_core/views/handlers/views_handler_filter_chado_select_cvterm_name.inc

@@ -26,9 +26,14 @@ class views_handler_filter_chado_select_cvterm_name extends views_handler_filter
 			}    	
     } else {
     	//get a list of cvs currently used
-    	$sql = 'select distinct(cv.cv_id) FROM '.$this->view->base_table
-    		.' LEFT JOIN cvterm cvterm ON cvterm.cvterm_id='.$this->view->base_table.'.type_id '
-    		.'LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
+    	if ($this->view->base_table == 'cvterm') {
+      	$sql = 'SELECT distinct(cv.cv_id) FROM '.$this->view->base_table
+      		.' LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';	
+    	} else {
+      	$sql = 'SELECT distinct(cv.cv_id) FROM '.$this->view->base_table
+      		.' LEFT JOIN cvterm cvterm ON cvterm.cvterm_id='.$this->view->base_table.'.type_id '
+      		.'LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
+      }
     	$previous_db = tripal_db_set_active('chado');
     	$resource = db_query($sql);
     	tripal_db_set_active($previous_db);
@@ -48,14 +53,35 @@ class views_handler_filter_chado_select_cvterm_name extends views_handler_filter
     $this->cvterm_options = $cvterms;
   }
 
+ /**
+  * Defines options for the option forms
+  */
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
 
+    $form['values_form_type'] = array(
+      '#type' => 'radios',
+      '#title' => 'Filter Type',
+      '#options' => array(
+        'textfield' => 'Text Field',
+        'select' => 'Drop-Down Box',
+      ),
+      '#default_value' => $this->options['values_form_type'],
+    );
+  }
+  
  /**
   * Alters the query so that WHERE cvterm.cvterm_id=212 is used
   */
   function query() {
     $this->ensure_table;
     
-    $where = 'cvterm.cvterm_id=%d';
+    if (preg_match('/^\d+$/', $this->value)) {
+      $where = 'cvterm.cvterm_id=%d';
+    } else {
+      $where = "cvterm.name" . $this->operator . "'%s'";
+    }
+
     $this->query->add_where($this->options['group'], $where, $this->value);
   }
   
@@ -66,27 +92,35 @@ class views_handler_filter_chado_select_cvterm_name extends views_handler_filter
   function value_form(&$form, &$form_state) {
     parent::value_form(&$form, &$form_state);
     
-    // Get Options
-    if ($this->options['exposed']) {    
-      $options['All'] = '<Any>';
-    }
-    $options['<select '.$this->table.'>'] = '<None>';
-    $max_length = 40;
-    foreach ($this->cvterm_options as $cvterm_id => $cvterm_name) {
-      if (strlen($cvterm_name) > $max_length) {
-        $options[$cvterm_id] = substr($cvterm_name,0,$max_length) . '...';
-      } else {
-        $options[$cvterm_id] = $cvterm_name;    
+    if (preg_match('/select/', $this->options['values_form_type'])) {
+      // Get Options
+      if ($this->options['exposed']) {    
+        $options['All'] = '<Any>';
       }
-    }
-    
-    //Select List
-    $form['value'] = array(
-        '#type' => 'select',
+      $options['<select '.$this->table.'>'] = '<None>';
+      $max_length = 40;
+      foreach ($this->cvterm_options as $cvterm_id => $cvterm_name) {
+        if (strlen($cvterm_name) > $max_length) {
+          $options[$cvterm_id] = substr($cvterm_name,0,$max_length) . '...';
+        } else {
+          $options[$cvterm_id] = $cvterm_name;    
+        }
+      }
+      
+      //Select List
+      $form['value'] = array(
+          '#type' => 'select',
+          '#title' => $this->options['label'],
+          '#options' => $options,
+          '#default_value' => $this->value,
+      );
+    } else {
+      $form['value'] = array(
+        '#type' => 'textfield',
         '#title' => $this->options['label'],
-        '#options' => $options,
         '#default_value' => $this->value,
-    );
+      );
+    }
   }
 
  /**
@@ -121,4 +155,33 @@ class views_handler_filter_chado_select_cvterm_name extends views_handler_filter
 
   }
 
+  /**
+   * This kind of construct makes it relatively easy for a child class
+   * to add or remove functionality by overriding this function and
+   * adding/removing items from this array.
+   */
+  function operators() {
+    $operators = array(
+      '=' => array(
+        'title' => t('Is equal to'),
+        'short' => t('='),
+        'method' => 'op_equal',
+        'values' => 1,
+      ),
+      '!=' => array(
+        'title' => t('Is not equal to'),
+        'short' => t('!='),
+        'method' => 'op_equal',
+        'values' => 1,
+      ),
+      '~' => array(
+        'title' => t('Contains'),
+        'short' => t('contains'),
+        'method' => 'op_contains',
+        'values' => 1,
+      ),
+    );
+    
+    return $operators;
+  }
 }

+ 18 - 0
tripal_core/views/handlers/views_handler_filter_chado_select_string.inc

@@ -76,4 +76,22 @@ class views_handler_filter_chado_select_string extends views_handler_filter_stri
 
   }
 
+ /**
+  *
+  */
+  function query() {
+    $this->ensure_my_table();
+    $field = "$this->table_alias.$this->real_field";
+    $upper = $this->case_transform();
+
+    // Deal with All/Any as value
+    if (preg_match('/All/', $this->value)) {
+      // Don't do anything    
+    } else {
+      $info = $this->operators();
+      if (!empty($info[$this->operator]['method'])) {
+        $this->{$info[$this->operator]['method']}($field, $upper);
+      }
+    }
+  }
 }

+ 22 - 0
tripal_genetic/tripal_genetic.api.inc

@@ -1,5 +1,27 @@
 <?php 
 
+/**
+ *
+ */
+function tripal_genetic_get_genotypes_by_feature_id ($feature_id) {
+
+  $sql = 'SELECT nd_experiment_id FROM nd_experiment_genotype WHERE genotype_id IN (SELECT genotype_id FROM feature_genotype WHERE feature_id=%d)';
+  $resource = db_query($sql, $feature_id);
+
+  $values['nd_experiment_id'] = array();
+  while ($r = db_fetch_object($resource)) {
+    $values['nd_experiment_id'][] = $r->nd_experiment_id;
+  }
+  
+  $object = tripal_core_generate_chado_var('nd_experiment',$values);
+  $object = tripal_core_expand_chado_vars($object, 'table', 'nd_experiment_genotype');
+  $object = tripal_core_expand_chado_vars($object, 'table', 'feature_genotype');
+  $object = tripal_core_expand_chado_vars($object, 'table', 'nd_experiment_stock');
+  
+  return $object;
+}
+
+
 /**
  * Implements hook_chado_genotype_schema()
  * Purpose: To add descriptions and foreign keys to default table description

+ 31 - 0
tripal_genetic/tripal_genetic.module

@@ -21,4 +21,35 @@ function tripal_genetic_views_api() {
    return array(
       'api' => 2.0,
    );
+}
+
+function tripal_genetic_theme() {
+  return array(
+    'tripal_feature_genotypes' => array (
+       'arguments' => array('node'=> null),
+       'template' => 'tripal_feature_genotypes',
+    ),  
+  );
+}
+
+/**
+ *
+ */
+function tripal_genetic_nodeapi (&$node, $op, $teaser, $page) {
+  switch ($op) {
+    case 'view':
+      if ($node->type == 'chado_feature') {
+        $node->content['tripal_feature_genotypes'] = array(
+           '#value' => theme('tripal_feature_genotypes', $node),
+        );      
+      }
+  }
+}
+
+/**
+ *
+ */
+function tripal_genetic_preprocess_tripal_feature_genotypes(&$variables){
+    $feature = $variables['node']->feature;   
+    $variables['tripal_feature']['genotype_experiments'] = tripal_genetic_get_genotypes_by_feature_id($feature->feature_id);
 }

+ 18 - 18
tripal_natural_diversity/tripal_natural_diversity.api.inc

@@ -36,7 +36,7 @@ function tripal_natural_diversity_chado_nd_experiment_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
       'nd_experiment_contact',
       'nd_experiment_dbxref',
       'nd_experiment_genotype',
@@ -88,7 +88,7 @@ function tripal_natural_diversity_chado_nd_experiment_contact_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -132,7 +132,7 @@ function tripal_natural_diversity_chado_nd_experiment_dbxref_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -176,7 +176,7 @@ function tripal_natural_diversity_chado_nd_experiment_genotype_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -220,7 +220,7 @@ function tripal_natural_diversity_chado_nd_experiment_phenotype_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -264,7 +264,7 @@ function tripal_natural_diversity_chado_nd_experiment_project_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -308,7 +308,7 @@ function tripal_natural_diversity_chado_nd_experiment_protocol_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -352,7 +352,7 @@ function tripal_natural_diversity_chado_nd_experiment_pub_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -406,7 +406,7 @@ function tripal_natural_diversity_chado_nd_experiment_stock_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
       'nd_experiment_stock_dbxref',
       'nd_experiment_stockprop',
     ),
@@ -459,7 +459,7 @@ function tripal_natural_diversity_chado_nd_experiment_stockprop_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     ),
   );
   
@@ -502,7 +502,7 @@ function tripal_natural_diversity_chado_nd_experiment_stock_dbxref_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
 
     ),
   );
@@ -554,7 +554,7 @@ function tripal_natural_diversity_chado_nd_experimentprop_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     ),
   );
   
@@ -590,7 +590,7 @@ function tripal_natural_diversity_chado_nd_geolocation_schema () {
     'primary key' => array(
       '0' => 'nd_geolocation_id',
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
       'nd_experiment',
       'nd_geolocationprop'
     ),
@@ -643,7 +643,7 @@ function tripal_natural_diversity_chado_nd_geolocationprop_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     ),
   );
   
@@ -668,7 +668,7 @@ function tripal_natural_diversity_chado_nd_protocol_schema () {
     'primary key' => array(
      '0' => 'nd_protocol_id',
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
       'nd_experiment_protocol',
       'nd_protocol_reagent',
       'nd_protocolprop'  
@@ -724,7 +724,7 @@ function tripal_natural_diversity_chado_nd_protocol_reagent_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     
     ),
   );
@@ -776,7 +776,7 @@ function tripal_natural_diversity_chado_nd_protocolprop_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     ),
   );
   
@@ -913,7 +913,7 @@ function tripal_natural_diversity_chado_nd_reagentprop_schema () {
         ),
       ),
     ),
-    'referring tables' => array(
+    'referring_tables' => array(
     ),
   );