Browse Source

Changes to Chado Select: array, Some basic fixesto stock as well as add regex option to tripal_stock_get_stock_by_name_identifier()

laceysanderson 14 years ago
parent
commit
af0cbc329c
3 changed files with 93 additions and 85 deletions
  1. 64 33
      tripal_core/tripal_core.api.inc
  2. 19 43
      tripal_stock/tripal_stock.api.inc
  3. 10 9
      tripal_stock/tripal_stock.module

+ 64 - 33
tripal_core/tripal_core.api.inc

@@ -368,10 +368,22 @@ function tripal_core_chado_update($table,$match,$values){
 *  An associative array containing the values for filtering the results. In the 
 *  case where multiple values for the same time are to be selected an additional
 *  entry for the field should appear for each value
-* @param $has_record
-*  Set this argument to 'true' to have this function return a numeric 
-*  value for the number of recrods rather than the array of records.  this
-*  can be useful in 'if' statements to check the presence of particula records.
+* @param $options
+*  An associative array of additional options where the key is the option 
+*  and the value is the value of that option.
+*
+* Additional Options Include:
+*  - has_record
+*     Set this argument to 'true' to have this function return a numeric 
+*     value for the number of recrods rather than the array of records.  this
+*     can be useful in 'if' statements to check the presence of particula records.
+*  - return_sql
+*     Set this to 'true' to have this function return an array where the first element is the sql 
+*     that would have been run and the second is an array of arguments.
+*  - case_insensitive_columns
+*     An array of columns to do a case insensitive search on.
+*  - regex_columns
+*     An array of columns where the value passed in should be treated as a regular expression
 * 
 * @return
 *  A database query result resource, FALSE if the query was not executed 
@@ -406,8 +418,11 @@ function tripal_core_chado_update($table,$match,$values){
 *
 * @ingroup tripal_api
 */
-function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$return_sql = 0){
-
+function tripal_core_chado_select($table,$columns,$values,$options = null){
+    if (!is_array($options)) { $options = array(); }
+    if (!$options['case_insensitive_columns']) { $options['case_insensitive_columns'] = array(); }
+    if (!$options['regex_columns']) { $options['regex_columns'] = array(); }
+    
    if (!is_array($columns)){
       watchdog('tripal_feature', 'the $columns argument for tripal_core_chado_select must be an array.');
       return false;
@@ -435,7 +450,11 @@ function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$retur
             $where[$field] = $value;
          } else {
             // select the value from the foreign key relationship for this value
-            $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+            $foreign_options = array(
+              'regex_columns' => $options['regex_columns'],
+              'case_insensitive_columns' => $options['case_insensitive_columns']
+            );
+            $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value, $foreign_options);
             if (sizeof($results) < 1) {
               // foreign key records are required
               // thus if none matched then return false and alert the admin through watchdog
@@ -472,8 +491,17 @@ function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$retur
          $sql .= "$field IN (".db_placeholders($value,'varchar').") AND ";
          foreach ($value as $v) { $args[] = $v; }
        } else {
-         $sql .= "$field = '%s' AND ";
-         $args[] = $value[0];
+         $operator = '=';
+         if (in_array($field, $options['regex_columns'])) {
+           $operator = '~*';
+         }
+         if (in_array($field, $options['case_insensitive_columns'])) {
+           $sql .= "lower($field) $operator lower('%s') AND ";
+           $args[] = $value[0];
+         }  else {
+           $sql .= "$field $operator '%s' AND ";
+           $args[] = $value[0];
+         }
        }
      }
      $sql = substr($sql,0,-4);  // get rid of the trailing 'AND'
@@ -481,7 +509,7 @@ function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$retur
 
    // if the caller has requested the SQL rather than the results...
    // which happens in the case of wanting to use the Drupal pager, then do so
-   if($return_sql){
+   if($options['return_sql']){
       return array('sql'=> $sql, 'args' => $args);
    }
 
@@ -494,7 +522,7 @@ function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$retur
      $results[] = $r;    
    }
    
-   if(!$has_record){
+   if(!$options['has_record']){
       return $results;
    } else{
       return count($results);
@@ -539,8 +567,11 @@ function tripal_core_chado_select($table,$columns,$values,$has_record = 0,$retur
 *
 * @ingroup tripal_api
 */
-function tripal_core_chado_get_foreign_key($table_desc,$field,$values){
-
+function tripal_core_chado_get_foreign_key($table_desc,$field,$values, $options = null){
+    if (!is_array($options)) { $options = array(); }
+    if (!$options['case_insensitive_columns']) { $options['case_insensitive_columns'] = array(); }
+    if (!$options['regex_columns']) { $options['regex_columns'] = array(); }
+    
    // get the list of foreign keys for this table description and
    // iterate through those until we find the one we're looking for
    $fkeys = $table_desc['foreign keys'];
@@ -562,7 +593,7 @@ function tripal_core_chado_get_foreign_key($table_desc,$field,$values){
                // the column name of the foreign key matches the field we want 
                // so this is the right relationship.  Now we want to select
                $select_cols = array($right);
-               $result = tripal_core_chado_select($table,$select_cols,$values);
+               $result = tripal_core_chado_select($table,$select_cols,$values, $options);
                $fields = array();
                foreach ($result as $obj) {
                  $fields[] = $obj->$right;
@@ -627,10 +658,10 @@ function tripal_core_chado_get_foreign_key($table_desc,$field,$values){
  *      This hook allows you to exclude fields from all tables that are of a given postgresql field 
  *      type. Simply implement this hook to return an array of postgresql types mapped to criteria.
  *      Then all fields of that type where the criteria supplied returns TRUE will be excluded from 
- *      any table. Tokens available in criteria are <field_value> and <field_name>. For example:
+ *      any table. Tokens available in criteria are &gt;field_value&lt;  and &gt;field_name&lt; . For example:
  * @code
           mymodule_exclude_type_by_default() {
-            return array('text' => 'length(<field_value>) > 50');
+            return array('text' => 'length(&gt;field_value&lt; ) > 50');
           }
  * @endcode
  *      will exclude all text fields with a length > 50. Thus if $feature.residues is longer than 50 *      it will be excluded, otherwise it will be added.
@@ -656,11 +687,11 @@ function tripal_core_generate_chado_var($table, $values) {
   // Get fields to be removed by name.................................
   $fields_to_remove = module_invoke_all('exclude_field_from_'.$table.'_by_default');
   foreach ($fields_to_remove as $field_name => $criteria) {
-    //replace <field_name> with the current field name & 
-    $criteria = preg_replace('/<field_name>/', $field_name, $criteria);
+    //replace &gt;field_name&lt;  with the current field name & 
+    $criteria = preg_replace('/&gt;field_name&lt; /', $field_name, $criteria);
 
     // if field_value needed we can't deal with this field yet
-    if (preg_match('/<field_value>/', $criteria)) { break; }
+    if (preg_match('/&gt;field_value&lt; /', $criteria)) { break; }
 
     //if criteria then remove from query
     $success = drupal_eval('<?php return '.$criteria.'; ?>');
@@ -685,18 +716,18 @@ function tripal_core_generate_chado_var($table, $values) {
   foreach ($types_to_remove as $field_type => $criteria) {
     // if there are fields of that type to remove
     if (is_array($field_types[$field_type])) {
-      //replace <field_name> with the current field name & 
-      $criteria = preg_replace('/<field_name>/', $field_name, $criteria);
+      //replace &gt;field_name&lt;  with the current field name & 
+      $criteria = preg_replace('/&gt;field_name&lt; /', $field_name, $criteria);
       
       foreach ($field_types[$field_type] as $field_name) {
         // if field_value needed we can't deal with this field yet
-        if (preg_match('/<field_value>/', $criteria)) { 
+        if (preg_match('/&gt;field_value&lt; /', $criteria)) { 
           $fields_to_remove[$field_name] = $criteria;
           continue; 
         }
 
         // if field_value needed we can't deal with this field yet
-        if (preg_match('/<field_value>/', $criteria)) { break; }
+        if (preg_match('/&gt;field_value&lt; /', $criteria)) { break; }
 
         //if criteria then remove from query
         $success = drupal_eval('<?php return '.$criteria.'; ?>');
@@ -747,7 +778,7 @@ function tripal_core_generate_chado_var($table, $values) {
     // remove any fields where criteria need to be evalulated---------------------------------------
     foreach ($fields_to_remove as $field_name => $criteria) {
       if (!isset($object->{$field_name})) { break; }
-      $criteria = preg_replace('/<field_value>/', $object->{$field_name}, $criteria);
+      $criteria = preg_replace('/&gt;field_value&lt; /', $object->{$field_name}, $criteria);
       //if criteria then remove from query
       $success = drupal_eval('<?php return '.$criteria.'; ?>');
       watchdog('tripal_core', 
@@ -1032,21 +1063,21 @@ function tripal_core_expand_chado_vars ($object, $type, $to_expand) {
  * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are 
  * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can 
  * contain the following tokens:
- *   - <field_name>
+ *   - &gt;field_name&lt; 
  *       Replaced by the name of the field to be excluded
- *   - <field_value>
+ *   - &gt;field_value&lt; 
  *       Replaced by the value of the field in the current record
- * Also keep in mind that if your criteria doesn't contain the <field_value> token then it will be 
+ * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt;  token then it will be 
  * evaluated before the query is executed and if the field is excluded it won't be included in the 
  * query.
  *
  * @return
  *   An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  *
- * @ingroupt tripal_api
+ * @ingroup tripal_api
  */
 function tripal_core_exclude_type_by_default() {
-  return array('text' => "strlen('<field_value>') > 100");
+  return array('text' => "strlen('&gt;field_value&lt; ') > 100");
 }
 
 /**
@@ -1061,18 +1092,18 @@ function tripal_core_exclude_type_by_default() {
  * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are 
  * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can 
  * contain the following tokens:
- *   - <field_name>
+ *   - &gt;field_name&lt; 
  *       Replaced by the name of the field to be excluded
- *   - <field_value>
+ *   - &gt;field_value&lt; 
  *       Replaced by the value of the field in the current record
- * Also keep in mind that if your criteria doesn't contain the <field_value> token then it will be 
+ * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt;  token then it will be 
  * evaluated before the query is executed and if the field is excluded it won't be included in the 
  * query.
  *
  * @return
  *   An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  *
- * @ingroupt tripal_api
+ * @ingroup tripal_api
  */
 function tripal_core_exclude_field_from_feature_by_default() {
   return array();

+ 19 - 43
tripal_stock/tripal_stock.api.inc

@@ -39,49 +39,6 @@ function tripal_stock_get_stock_by_stock_id ($stock_id) {
 	
 }
 
-/**
- * Purpose:
- *
- * @param $stock_id
- *   the unique identifier for the stock to load properties of
- *
- * @return
- *   an array of properties where each element is a property object with
- *   the following fields:
- *     - stockprop_id (stockprop):
- *     - value (stockprop):
- *     - rank (stockprop):
- *     - type_id (stockprop):
- *     - type_name (cvterm):
- *     - type_definition (cvterm):
- *     - type_is_obsolete (cvterm):
- *     - type_is_relationshiptype (cvterm):
- *     - type_cv_id (cvterm):
- *     - type_db_reference_id (cvterm):
- *     - type_cv_name (cv):
- *     - type_cv_definition (cv):
- *     - type_db_reference_acession (dbxref):
- *     - type_db_reference_version (dbxref):
- *     - type_db_reference_description (dbxref):
- *     - type_db_reference_db_id (dbxref):
- *     - type_db_reference_db_name (db):
- *     - type_db_reference_db_description (db):
- *     - type_db_reference_db_url (db):
- *     - type_db_reference_db_urlprefix (db):
- *
- * @ingroup tripal_api
- */
-function tripal_stock_load_properties($stock_id) {
-
-  $properties = tripal_core_chado_select(
-    'stockprop',
-    array('stockprop_id', 'value', 'rank','type_id'),
-    array('stock_id' => $stock_id)
-  );
-  
-  return $properties;  
-}
-
 /**
  * Purpose: Returns all stocks currently sync'd with drupal
  *
@@ -241,11 +198,18 @@ function tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values)
 function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
   $stock_ids = array();
   
+  $options = array(
+    'regex_columns' => array('name', 'uniquename', 'accession', 'value')
+  );
+  
   // where name_identifier = stock.name-------------------------------
   $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
     array(
       'name' => $name,
       'organism_id' => $organism_id,
+    ),
+    array(
+      'regex_columns' => array('name'),
     )
   );
   if (!empty($current_stocks)) {
@@ -257,6 +221,9 @@ function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
     array(
       'uniquename' => $name,
       'organism_id' => $organism_id,
+    ),
+    array(
+      'regex_columns' => array('uniquename'),
     )
   );
   if (!empty($current_stocks)) {
@@ -271,6 +238,9 @@ function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
         'accession' => $name,
       ),
       'organism_id' => $organism_id,
+    ),
+    array(
+      'regex_columns' => array('accession'),
     )
   );
   if (!empty($current_stocks)) {
@@ -286,6 +256,9 @@ function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
       'stock_id' => array(
         'organism_id' => $organism_id,
       ),
+    ),
+    array(
+      'regex_columns' => array('accession'),
     )
   );
   if (!empty($current_stocks)) {
@@ -304,6 +277,9 @@ function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
         'name' => 'synonym',
       ),
       'value' => $name,
+    ),
+    array(
+      'regex_columns' => array('value'),
     )
   );
   if (!empty($current_stocks)) {

+ 10 - 9
tripal_stock/tripal_stock.module

@@ -336,12 +336,12 @@ function theme_tripal_stock_stock_table (&$stocks) {
   $output .= "<th>Description</th>";
   $output .= "</tr>";
 
-  foreach($stocks as $stock){
+  foreach($stocks as $node){
 		$output .= "<tr>";
-		$output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
-		$output .= "<td>".$stock->stock_type."</td>";
-		$output .= "<td nowrap>".$stock->organism->common_name."</td>";
-		$output .= "<td>".$stock->description."</td>";
+		$output .= "<td>".l($node->stock->name, "node/".$node->nid)."</td>";
+		$output .= "<td>".$node->stock->type_id->name."</td>";
+		$output .= "<td nowrap>".$node->stock->organism_id->common_name."</td>";
+		$output .= "<td>".$node->stock->description."</td>";
 		$output .= "</tr>";
 	}
 	$output .= "</table>";
@@ -427,15 +427,16 @@ function chado_stock_load($node) {
  * @ingroup tripal_stock
  */
 function chado_stock_form($node, $form_state) {
-  $type = node_get_types('type', $node);
 
   // Expand all fields needed
   $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
   foreach ($fields_needed as $field_name) {
     // Check to see if it's excluded and expand it if so
-    if (in_array($field_name, $node->expandable_fields)) {
-      $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
-    }    
+    if ($node->expandable_fields) {
+      if (in_array($field_name, $node->expandable_fields)) {
+        $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
+      }
+    }
   }
 
   // This defines the path for the next step in a simulated multipart form