Browse Source

Fixing some problems with properties and the api for retrieving stocks

laceysanderson 14 years ago
parent
commit
1602b81ec5

+ 39 - 2
tripal_stock/other_module_api_functions.inc

@@ -13,7 +13,7 @@
  * @params where_options: array(
  *													<column_name> => array(
  *														'type' => <type of column: INT/STRING>,
- *														'value' => <the vlaue you want to filter on>,
+ *														'value' => <the value you want to filter on>,
  *														'exact' => <if TRUE use =; if FALSE use ~>,
  *													)
  *				)
@@ -44,7 +44,7 @@ function get_max_chado_rank ($tablename, $where_options) {
     implode(' AND ',$where)
   ));
   tripal_db_set_active($previous_db);
-
+	//drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
 	if ($result->count > 0) {
 	  return $result->max_rank;
 	} else {
@@ -189,6 +189,43 @@ function get_chado_cvterm_options($cv_id = 0) {
 // Module: tripal_db
 ///////////////////////////////////////////////////////////////////////////
 
+/*************************************************************************
+ * Purpose: To retrieve a chado db object
+ *
+ * @params where_options: array(
+ *													<column_name> => array(
+ *														'type' => <type of column: INT/STRING>,
+ *														'value' => <the vlaue you want to filter on>,
+ *														'exact' => <if TRUE use =; if FALSE use ~>,
+ *													)
+ *				)
+ * @return chado db object with all fields from the chado db table
+ */
+function get_chado_db ($where_options) {
+	$previous_db = tripal_db_set_active('chado');
+
+	$where= array();
+	//generate the where clause from supplied options
+	// the key is the column name
+	foreach ($where_options as $key => $val_array) {
+		if (preg_match('/INT/', $val_array['type'])) {
+			$where[] = $key."=".$val_array['value'];
+		} else {
+			if ($val_array['exact']) { $operator='='; }
+			else { $operator='~'; }
+			$where[] = $key.$operator."'".$val_array['value']."'";
+		}
+	}
+	
+  $r = db_fetch_object(db_query(
+    "SELECT * FROM db WHERE ".implode(' AND ',$where)
+  ));
+  
+  tripal_db_set_active($previous_db);
+
+  return $r;
+}
+
 /*************************************************************************
  * Purpose: Create an options array to be used in a form element
  *   which provides a list of all chado dbs

+ 23 - 8
tripal_stock/tripal_stock-api_functions.inc

@@ -49,7 +49,11 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		$unknown_provided = TRUE;
 		$new_criteria = array();
 		foreach ($criteria['unknown']['columns'] as $column_name) {
-			$new_criteria[$column_name] = array('value' => $criteria['unknown']['value'], 'regex'=>TRUE);
+			if (in_array($column_name, array('stock_id','dbxref_id','organism_id','type_id') )) {
+				$new_criteria[$column_name] = array('type'=>'INT','value' => $criteria['unknown']['value']);
+			} else {
+				$new_criteria[$column_name] = array('type'=>'STRING','value' => $criteria['unknown']['value'], 'regex'=>TRUE);
+			}
 		}
 		$unknown_stocks = get_chado_stocks($new_criteria, 'ANY', $organism_id);
 	}
@@ -81,9 +85,9 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 			else { $operator = '='; }
 			
 			if (preg_match('/INT/', $v['type'])) {
-				$where[] = $column_name.$operator.$v['value'];
+				$where[] = 'stock.'.$column_name.'='.$v['value'];
 			} else {
-				$where[] = $column_name.$operator."'".$v['value']."'";
+				$where[] = 'stock.'.$column_name.$operator."'".$v['value']."'";
 			}		
 		}
 	}
@@ -91,12 +95,19 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 	//Build query-----------------------------------------
 	if (preg_match('/ANY/', $match_type)) {
 		$where_string = implode(' OR ',$where);
+		if ($organism_id) {
+			$where_string = '('.$where_string.') AND organism_id='.$organism_id;
+		}
 	} else {
 		$where_string = implode(' AND ',$where);
+		if ($organism_id) {
+			$where_string .= ' AND organism_id='.$organism_id; }
 	}
+	
 	if (sizeof($where) >= 1) {
 		$execute_query = TRUE;
 		$sql_query = 'SELECT stock.stock_id FROM stock '.implode(' ',$joins).' WHERE '.$where_string;
+		drupal_set_message('Query='.$sql_query);
 	} elseif (!$unknown_provided) {	
 		$execute_query = TRUE;
 		$sql_query = 'SELECT stock.stock_id FROM stock';
@@ -111,6 +122,8 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		$resource = db_query($sql_query);
 		tripal_db_set_active($previous_db);
 	
+		//drupal_set_message($sql_query);
+	
 		$stock_ids = array();	
 		while ($r = db_fetch_object($resource)) {
 			$stock_ids[] = $r->stock_id;
@@ -118,10 +131,12 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		$stock_ids = array_unique($stock_ids);
 	
 		//Get Stocks------------------------------------------
-		$resource = db_query("SELECT nid FROM {chado_stock} WHERE stock_id IN (%s)",implode(',',$stock_ids));
-		$main_stocks = array();
-		while ($r = db_fetch_object($resource)) {
-			$main_stocks[] = node_load($r->nid);
+		if (!empty($stock_ids)) {
+			$resource = db_query("SELECT nid FROM {chado_stock} WHERE stock_id IN (%s)",implode(',',$stock_ids));
+			$main_stocks = array();
+			while ($r = db_fetch_object($resource)) {
+				$main_stocks[] = node_load($r->nid);
+			}
 		}
 	}
 	
@@ -135,7 +150,7 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		if(!empty($unknown_stocks)){
 			return $unknown_stocks;
 		} else {
-			drupal_set_message('No Stocks matched the given criteria','warning');
+			//drupal_set_message('No Stocks matched the given criteria','warning');
 			return array();
 		}
 	}

+ 6 - 2
tripal_stock/tripal_stock-properties.inc

@@ -79,6 +79,11 @@ function tripal_stock_add_ONE_property_form_validate($form, &$form_state) {
   // Only Require if Adding Property
   if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
 
+		// Check that there is a stock
+		if ( $form_state['values']['prop_stock_id'] <= 0 ) {
+			form_set_error('prop_stock_id', 'There is no associated stock.');
+		}
+		
     // Check that Selected a type
     if ( $form_state['values']['prop_type_id'] == 0) {
       form_set_error('prop_type_id', 'Please select a type of property.');
@@ -106,7 +111,7 @@ function tripal_stock_add_ONE_property_form_validate($form, &$form_state) {
 }
 
 function tripal_stock_add_ONE_property_form_submit($form, &$form_state) {
-
+	
   // if there is a property add it (only won't be a property if clicked next step w/ no property)
   if ($form_state['values']['prop_type_id'] != 0) {
     //determine the rank for this property
@@ -269,7 +274,6 @@ function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $prefer
 	$previous_db = tripal_db_set_active('chado');
 	$old_obj = db_fetch_object(db_query("SELECT * FROM stockprop WHERE stockprop_id=%d",$stockprop_id));
 	tripal_db_set_active($previous_db);
-	dpm($old_obj);
 	
 	// if they changed the type need to check rank
 	//   (if there is another property of the same type then rank needs to be increased to prevent collisions)

+ 12 - 11
tripal_stock/tripal_stock.module

@@ -630,19 +630,20 @@ function chado_stock_insert($node) {
         $node->accession,
 				$node->db_description	
       );
-    }
-  }
 
+  		$dbxref_options['db_id'] = array(
+  			'value' =>$node->database,
+  			'type' => 'INT',
+  		);
+  		$dbxref_options['accession'] = array(
+  			'value' =>$node->accession,
+  			'type' => 'STRING',
+  		);
+  		$dbxref = get_chado_dbxref($dbxref_options);
+  	}
+  }
+  
   // create stock
-  $dbxref_options['db_id'] = array(
-  	'value' =>$node->database,
-  	'type' => 'INT',
-  );
-  $dbxref_options['accession'] = array(
-  	'value' =>$node->accession,
-  	'type' => 'STRING',
-  );
-  $dbxref = get_chado_dbxref($dbxref_options);
   if($dbxref->dbxref_id != 0) {
     db_query(
       "INSERT INTO stock (dbxref_id, organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, %d, '%s', '%s', '%s', %d, 'f')",