Browse Source

Added new select/insert/delete/update for featureprop and analysisfeatureprop tables. Also added update_property_by_id and delete_property_by_id functions to the core API. Added a new drush command to launch jobs

Stephen Ficklin 13 years ago
parent
commit
87a16fbff7

+ 112 - 17
tripal_core/tripal_core.api.inc

@@ -1376,7 +1376,7 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
    $table_desc = module_invoke_all('chado_'.$basetable.'prop_schema');
    $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
 
-   // construct the array of values to be inserted  
+   // construct the array of values to be selected  
    $values = array (
       $fkcol => $record_id,
       'type_id' => array ( 
@@ -1392,7 +1392,10 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
    return $results;
 }
 /**
- * Insert a property for a given basetable record
+ * Insert a property for a given base table.  By default if the property already
+ * exists a new property is added with the next available rank.  If 
+ * $update_if_present argument is specified then the record will be updated if it
+ * exists rather than adding a new property.
  *
  * @param $basetable
  *   The base table for which the property should be inserted. Thus to insert a property
@@ -1406,7 +1409,9 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
  * @param $value
  *   The value of the property to be inserted (can be empty)
  * @param $update_if_present
- *   A boolean indicating whether an existing record should be updated or an error thrown
+ *   A boolean indicating whether an existing record should be updated. If the
+ *   property already exists and this value is not specified or is zero then
+ *   a new property will be added with the next largest rank.
  *
  * @return
  *   Return True on Insert/Update and False otherwise
@@ -1416,13 +1421,23 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
 function tripal_core_insert_property($basetable, $record_id, $property, 
    $cv_name, $value, $update_if_present = 0)
 {
-   // first see if the property already exists, if so we can't insert
+   // first see if the property already exists, if the user want's to update
+   // then we can do that, but otherwise we want to increment the rank and
+   // insert
    $prop = tripal_core_get_property($basetable,$record_id,$property,$cv_name);
+   $rank = 0;
    if(count($prop)>0){ 
       if($update_if_present){
-        return tripal_core_update_property($basetable,$record_id,$property,$cv_name,$value) ;
+         return tripal_core_update_property($basetable,$record_id,$property,$cv_name,$value) ;
       } else {
-        return FALSE;
+         // iterate through the properties returned and get the largest rank
+         foreach($prop as $p){
+            if($p->rank > $rank){
+               $rank = $p->rank;
+            }
+         }
+         // now add 1 to the rank
+         $rank++;
       }
    }
 
@@ -1441,21 +1456,27 @@ function tripal_core_insert_property($basetable, $record_id, $property,
          'is_obsolete' => 0
       ),
       'value' => $value, 
-      'rank' => 0,
+      'rank' => $rank,
    );
+
    return tripal_core_chado_insert($basetable.'prop',$values);
 }
 
 /**
- * Update a property for a given basetable record
+ * Update a property for a given base table record and property name.  This 
+ * function should be used only if one record of the property will be present.
+ * If the property name can have multiple entries (with increasing rank) then
+ * use the function named tripal_core_update_property_by_id
  *
  * @param $basetable
- *   The base table for which the property should be updated. Thus to update a property
- *   for a feature the basetable=feature and property is updated in featureprop
+ *   The base table for which the property should be updated. The property table 
+ *   is constructed using  a combination of the base table name and the suffix 
+ *   'prop' (e.g. basetable = feature then property tabie is featureprop).
  * @param $record_id
- *   The primary key of the basetable to update a property for. This should be in integer.
+ *   The foreign key of the basetable to update a property for. This should be in integer.
+ *   For example, if the basetable is 'feature' then the $record_id should be the feature_id
  * @param $property
- *   The cvterm name describing the type of property to be updated
+ *   The cvterm name of property to be updated
  * @param $cv_name
  *   The name of the cv that the above cvterm is part of
  * @param $value
@@ -1463,7 +1484,7 @@ function tripal_core_insert_property($basetable, $record_id, $property,
  * @param $insert_if_missing
  *   A boolean indicating whether a record should be inserted if one doesn't exist to update
  *
- * Note: The property to be updated is select via theu nique combination of $record_id and
+ * Note: The property to be updated is select via the unique combination of $record_id and
  * $property and then it is updated with the supplied value
  *
  * @return
@@ -1471,8 +1492,8 @@ function tripal_core_insert_property($basetable, $record_id, $property,
  *
  * @ingroup tripal_chado_api
  */
-function tripal_core_update_property($basetable, $record_id,$property,$cv_name,
-   $value,$insert_if_missing = 0)
+function tripal_core_update_property($basetable, $record_id, $property, 
+   $cv_name, $value, $insert_if_missing = 0)
 {
 
    // first see if the property is missing (we can't update a missing property
@@ -1507,7 +1528,55 @@ function tripal_core_update_property($basetable, $record_id,$property,$cv_name,
 }
 
 /**
- * Deletes a property for a given basetable record
+ * Update a property for a given base table record.  This function should be 
+ * used if multiple records of the same property will be present. Also, use this
+ * function to change the property name of an existing property.
+ *
+ * @param $basetable
+ *   The base table for which the property should be updated. The property table 
+ *   is constructed using  a combination of the base table name and the suffix 
+ *   'prop' (e.g. basetable = feature then property tabie is featureprop).
+ * @param $record_id
+ *   The primary key of the base table. This should be in integer.
+ *   For example, if the basetable is 'feature' then the $record_id should be the featureprop_id
+ * @param $property
+ *   The cvterm name of property to be updated
+ * @param $cv_name
+ *   The name of the cv that the above cvterm is part of
+ * @param $value
+ *   The value of the property to be inserted (can be empty)
+ *
+ * @return
+ *   Return True on Update/Insert and False otherwise
+ *
+ * @ingroup tripal_chado_api
+ */
+function tripal_core_update_property_by_id($basetable, $record_id, $property, 
+   $cv_name, $value)
+{
+
+   // get the primary key for this property table
+   $table_desc = module_invoke_all('chado_'.$basetable.'prop_schema');
+   $pkcol = $table_desc['primary key'][0];
+
+   // construct the array that will match the exact record to update
+   $match = array (
+      $pkcol => $record_id,
+   );
+   // construct the array of values to be updated
+   $values = array (      
+      'type_id' => array ( 
+         'cv_id' => array (
+            'name' => $cv_name,
+         ),
+         'name' => $property,
+      ),
+      'value' => $value, 
+   );
+   return tripal_core_chado_update($basetable.'prop',$match,$values);
+}
+/**
+ * Deletes a property for a given base table record using the property name
  *
  * @param $basetable
  *   The base table for which the property should be deleted. Thus to deleted a property
@@ -1519,7 +1588,7 @@ function tripal_core_update_property($basetable, $record_id,$property,$cv_name,
  * @param $cv_name
  *   The name of the cv that the above cvterm is part of
  *
- * Note: The property to be deleted is select via theu nique combination of $record_id and $property 
+ * Note: The property to be deleted is select via the unique combination of $record_id and $property 
  *
  * @return
  *   Return True on Delete and False otherwise
@@ -1544,6 +1613,32 @@ function tripal_core_delete_property($basetable, $record_id,$property,$cv_name){
    return tripal_core_chado_delete($basetable.'prop',$match);
 }
 
+/**
+ * Deletes a property using the property ID
+ *
+ * @param $basetable
+ *   The base table for which the property should be deleted. Thus to deleted a property
+ *   for a feature the basetable=feature and property is deleted from featureprop
+ * @param $record_id
+ *   The primary key of the basetable to delete a property for. This should be in integer.
+ *
+ * @return
+ *   Return True on Delete and False otherwise
+ *
+ * @ingroup tripal_chado_api
+ */
+function tripal_core_delete_property_by_id($basetable, $record_id){
+   // get the foreign key for this property table
+   $table_desc = module_invoke_all('chado_'.$basetable.'prop_schema');
+   $pkcol = $table_desc['primary key'][0];
+
+   // construct the array that will match the exact record to update
+   $match = array (
+      $pkcol => $record_id,
+   );
+   return tripal_core_chado_delete($basetable.'prop',$match);
+}
+
 /**
  * This function is typically used in the '.install' file for a Tripal module
  * Each module should call this function during installation to create

+ 34 - 4
tripal_core/tripal_core.drush.inc

@@ -30,7 +30,7 @@ function tripal_core_drush_command() {
     'examples' => array(
       'Standard example' => 'drush tripal-current-job',
     ),
-    'aliases' => array('trpjob'),
+    'aliases' => array('trpjob-cur'),
   );
   $items['tripal-update-mview'] = array(
     // used by drush help
@@ -48,15 +48,45 @@ function tripal_core_drush_command() {
       'mview_id',
       'table_name'
     ),
-    'alias' => array('trpmv-up')
+    'aliases' => array('trpmv-up')
+  );
+  $items['tripal-launch-jobs'] = array(
+    // used by drush help
+    'description' => dt('Lauches any jobs waiting in the queue.'),
+    'arguments' => array(
+      'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
+    ),
+    'examples' => array(
+      'Normal Job' => 'drush tripal-launch-jobs',
+      'Parallel Job' => 'drush tripal-launch-jobs --parallel=1'
+    ),
+    // supply options
+    'options' => array(
+      'parallel',
+    ),
+    'aliases' => array('trpjob-run')
   );
   return $items;
 }
 
+/**
+ * Executes jobs in the Tripal Jobs Queue
+ * 
+ * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
+ */
+function drush_tripal_core_tripal_launch_jobs () {
+  $parallel = drush_get_option('parallel');   
+  if($parallel){
+    tripal_jobs_launch($do_parallel);
+  } else {
+    tripal_jobs_launch();  
+  }
+}
+
 /**
  * Prints details about the current running job
  * 
- * NOTE: The following code is executed when drush tjob or drush tripal-current-job is called
+ * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  */
 function drush_tripal_core_tripal_current_job () {
   
@@ -121,4 +151,4 @@ function drush_tripal_core_tripal_update_mview () {
     drush_set_error('Update failed.');
   }
   
-}
+}

+ 136 - 170
tripal_feature/tripal_feature-properties.inc

@@ -8,16 +8,15 @@
 function tripal_feature_edit_ALL_properties_page($node) {
   $output = '';
 
-  $properties = tripal_core_chado_select(
-    'featureprop', 
-    array('featureprop_id','type_id','value','rank'), 
-    array(
-      'feature_id' => $node->feature->feature_id
-    ),
-    array('order_by' => array('type_id' => 'ASC','rank'=>'ASC'))
-  );
+  // get the list of properties for this feature
+  $values  = array('feature_id' => $node->feature->feature_id);
+  $options = array('order_by' => array('type_id' => 'ASC','rank'=>'ASC'));
+  $properties = tripal_core_generate_chado_var('featureprop',$values,$options);
+  $properties = tripal_core_expand_chado_vars($properties,'field','featureprop.value');
+  
   $expand_add = (sizeof($properties)) ? FALSE : TRUE;
   
+  // add the appopriate form sections
   $output .= drupal_get_form('tripal_feature_add_ONE_property_form', $node, $expand_add);
   $output .= drupal_get_form('tripal_feature_edit_ALL_properties_form', $node, $properties);
   $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
@@ -45,13 +44,34 @@ function tripal_feature_add_ONE_property_form($form_state, $node, $expand) {
     '#type' => 'hidden',
     '#value' => $node->nid
   );
+  
+  $form['add_properties']['feature_id'] = array(
+    '#type' => 'value',
+    '#value' => $feature_id,
+    '#required' => TRUE
+  );
+  
+  // right now this defaults to the 'feature_property' CV
+  // but in the future it should be more flexible
+  $form['cv_name'] = array(
+    '#type' => 'hidden',
+    '#value' => 'feature_property'
+  );
 	
+  // get the list of property types
   $prop_type_options = array();
-  $results = tripal_core_chado_select('cvterm',array('cvterm_id','name'), array('cv_id' => array('name' => 'feature_property')));
+  $columns = array('cvterm_id','name');
+  $values = array(
+    'cv_id' => array(
+      'name' => $form['cv_name']['#value'],
+    )
+  );
+  $results = tripal_core_chado_select('cvterm',$columns,$values);
   foreach ($results as $r) {
-    $prop_type_options[$r->cvterm_id] = $r->name;
+    $prop_type_options[$r->name] = $r->name;
   }
-  $form['add_properties']['prop_type_id'] = array(
+
+  $form['add_properties']['property'] = array(
     '#type' => 'select',
     '#title' => t('Type of Property'),
     '#options' => $prop_type_options,
@@ -62,12 +82,6 @@ function tripal_feature_add_ONE_property_form($form_state, $node, $expand) {
     '#title' => t('Value'),
   );
 
-  $form['add_properties']['prop_feature_id'] = array(
-    '#type' => 'value',
-    '#value' => $feature_id,
-    '#required' => TRUE
-  );
-
   $form['add_properties']['submit-add'] = array(
     '#type' => 'submit',         
     '#value' => t('Add Property')
@@ -83,31 +97,19 @@ function tripal_feature_add_ONE_property_form($form_state, $node, $expand) {
  */
 function tripal_feature_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 feature
-		if ( $form_state['values']['prop_feature_id'] <= 0 ) {
-			form_set_error('prop_feature_id', 'There is no associated feature.');
-		}
+   // Only Require if Adding Property
+   if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
+   
+      // Check that there is a feature
+      if ( $form_state['values']['feature_id'] <= 0 ) {
+         form_set_error('feature_id', 'There is no associated feature.');
+      }
 		
-    // 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.');
-    } else {
-      // Check that type is in chado
-      $previous_db = tripal_db_set_active('chado');
-      $num_rows = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d", $form_state['values']['prop_type_id']));
-      tripal_db_set_active($previous_db);
-      if ( $num_rows->count != 1) {
-        form_set_error('prop_type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
-      } // end of if more or less than 1 row
-    } // if no prop type
-
-
-
-  } // if add Property
-
+      // Check that Selected a type
+      if ( !$form_state['values']['property']) {
+         form_set_error('property', 'Please select a type of property.');
+      } 
+   } 
 }
 
 /**
@@ -115,30 +117,18 @@ function tripal_feature_add_ONE_property_form_validate($form, &$form_state) {
  *
  * @ingroup tripal_feature
  */
-function tripal_feature_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
-    $max_rank = get_max_chado_rank('featureprop', 
-       array('feature_id'=>array('type'=>'INT','value'=>$form_state['values']['prop_feature_id']), 
-       'type_id'=>array('type'=>'INT','value'=> $form_state['values']['prop_type_id']) ));
-    if ($max_rank == -1) { $rank = 0; 
-    } else { $rank = $max_rank+1; }
-    
-    $previous_db = tripal_db_set_active('chado');
-    db_query(
-      "INSERT INTO featureprop (feature_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)",
-      $form_state['values']['prop_feature_id'],
-      $form_state['values']['prop_type_id'],
-      $form_state['values']['prop_value'],
-      $rank
-    );
-    tripal_db_set_active($previous_db);
-
-    drupal_set_message("Successfully Added Property");
-
-  } //end of if property to add
+function tripal_feature_add_ONE_property_form_submit($form, &$form_state) {	
+   $feature_id = $form_state['values']['feature_id'];
+   $property = $form_state['values']['property'];
+   $value = $form_state['values']['prop_value'];       
+   $cv_name = $form_state['values']['cv_name'];       
+       
+   $succes = tripal_feature_insert_property($feature_id,$property,$value,0,$cv_name);
+   if($succes){
+      drupal_set_message("Successfully Added Property: $property => $value");
+   } else {
+      drupal_set_message("Failed to Add Property: $property => $value");
+   }
 }
 
 /**
@@ -149,52 +139,67 @@ function tripal_feature_add_ONE_property_form_submit($form, &$form_state) {
  */ 
 function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties) {
   $form = array();
-
+  $feature_id = $node->feature->feature_id;
+  
   $form['nid'] = array(
     '#type' => 'hidden',
     '#value' => $node->nid
   );
   
+  $form['add_properties']['feature_id'] = array(
+    '#type' => 'value',
+    '#value' => $feature_id,
+    '#required' => TRUE
+  );
+    
+  // right now this defaults to the 'feature_property' CV
+  // but in the future it should be more flexible
+  $form['cv_name'] = array(
+    '#type' => 'hidden',
+    '#value' => 'feature_property'
+  );
+  
   if (sizeof($properties)) {
     
+    // build the select box options for the property name
     $prop_type_options = array();
-    $results = tripal_core_chado_select('cvterm',array('cvterm_id','name'), array('cv_id' => array('name' => 'feature_property')));
+    $columns = array('cvterm_id','name');
+    $values = array(
+       'cv_id' => array(
+          'name' => $form['cv_name']['#value']
+       )
+    );
+    $results = tripal_core_chado_select('cvterm',$columns, $values);
     foreach ($results as $r) {
-      $prop_type_options[$r->cvterm_id] = $r->name;
+      $prop_type_options[$r->name] = $r->name;
     }
-      
-    foreach ($properties as $i => $property) {
     
-      $form["num-$i"] = array(
-       '#type' => 'fieldset',
-       '#value' => "Property $i"
-      );
-      
-      $form["num-$i"]["id-$i"] = array(
-       '#type' => 'hidden',
-       '#value' => $property->featureprop_id
-      );
-      
-      $default = array_search($property->type, $prop_type_options);  
-      $form["num-$i"]["type-$i"] = array(
-       '#type' => 'select',
-       //'#title' => t('Type of Property'),
-       '#options' => $prop_type_options,
-       '#default_value' => $property->type_id
-      );
-      
-      $form["num-$i"]["value-$i"] = array(
-       '#type' => 'textfield',
-       //'#title' => t('Value'),
-       '#default_value' => $property->value
-      );
-      
-      $form["num-$i"]["delete-$i"] = array(
-       '#type' => 'submit',
-       '#value' => t("Delete"),
-       '#name' => "delete-$i",
-      );
-    }//end of foreach property
+    // iterate through all of the properties and create a set of form elements 
+    foreach ($properties as $i => $property) {
+       $form["num-$i"] = array(
+          '#type' => 'fieldset',
+          '#value' => "Property $i"
+       );      
+       $form["num-$i"]["id-$i"] = array(
+          '#type' => 'hidden',
+          '#value' => $property->featureprop_id
+       );      
+       $default = array_search($property->type, $prop_type_options);  
+       $form["num-$i"]["type-$i"] = array(
+          '#type' => 'select',
+          '#options' => $prop_type_options,
+          '#default_value' => $property->type_id->name
+       );     
+       $form["num-$i"]["value-$i"] = array(
+          '#type' => 'textfield',
+          '#default_value' => $property->value
+       );      
+       $form["num-$i"]["delete-$i"] = array(
+          '#type' => 'submit',
+          '#value' => t("Delete"),
+         '#name' => "delete-$i",
+       );
+    } //end of foreach property
   
     $form['num_properties'] = array(
       '#type' => 'hidden',
@@ -217,85 +222,44 @@ function tripal_feature_edit_ALL_properties_form($form_state, $node, $properties
  */
 function tripal_feature_edit_ALL_properties_form_submit($form, &$form_state) {
 
+  $cv_name = $form_state['values']['cv_name'];  
+  $feature_id = $form_state['values']["feature_id"];
+  $all_good = 1;
+
+  // if the update button was clicked then do the update
   if ($form_state['clicked_button']['#value'] == t('Update All Properties') ) {
-     //Update all
+     // iterate through each of the properties and set each one               
      for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
-       tripal_feature_update_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"], $form_state['values']["preferred-$i"], $form_state['values']["nid"]);
+       $featureprop_id = $form_state['values']["id-$i"];
+       $property = $form_state['values']["type-$i"];
+       $value = $form_state['values']["value-$i"];
+       $success = tripal_feature_update_property_by_id($featureprop_id,$property,$value,$cv_name);       
+       if(!$success){
+          drupal_set_message("Failed to Update Property: $property => $value");
+          $all_good = 0;
+       }
+     }
+     if($all_good){
+        drupal_set_message("Updated all Properties");
      }
-     drupal_set_message("Updated all Properties");
      drupal_goto('node/'.$form_state['values']['nid']);
-  } elseif ( preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) {
+  } 
+  // if the delete button was clicked then remove the property
+  elseif (preg_match('/delete-(\d+)/', $form_state['clicked_button']['#name'], $matches) ) {
      $i = $matches[1];
-     tripal_feature_delete_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"]);
-     drupal_set_message("Deleted Property");
+     $featureprop_id = $form_state['values']["id-$i"];
+     $property = $form_state['values']["type-$i"];
+     $value = $form_state['values']["value-$i"];
+     $success = tripal_feature_delete_property_by_id($featureprop_id);
+     if($success){
+        drupal_set_message("Deleted Property");
+     } else {
+        drupal_set_message("Unable to Delete Property");
+     }
   } else {
     drupal_set_message("Unrecognized Button Pressed",'error');
   }
-
-  
 }
-
-/**
- * 
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_update_property($featureprop_id, $cvterm_id, $value, $preferred, $nid) {
-
-	$previous_db = tripal_db_set_active('chado');
-	$old_obj = db_fetch_object(db_query("SELECT * FROM featureprop WHERE featureprop_id=%d",$featureprop_id));
-	tripal_db_set_active($previous_db);
-	
-	// 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)
-	if ($cvterm_id == $old_obj->type_id) {
-	  $previous_db = tripal_db_set_active('chado');
-  	db_query(
-    	"UPDATE featureprop SET type_id=%d, value='%s' WHERE featureprop_id=%d",
-    	$cvterm_id, 
-    	$value,
-    	$featureprop_id
-  	);
-  	tripal_db_set_active($previous_db);
-  } else {
-      //determine the rank for this property
-    $max_rank = get_max_chado_rank('featureprop', 
-       array('feature_id'=>array('type'=>'INT','value'=> $old_obj->feature_id), 
-    	 'type_id'=>array('type'=>'INT','value'=> $cvterm_id ) ));
-    if ($max_rank == -1) { $rank = 0; 
-    } else { $rank = $max_rank+1; }
-	  $previous_db = tripal_db_set_active('chado');
-  	db_query(
-    	"UPDATE featureprop SET type_id=%d, value='%s', rank=%d WHERE featureprop_id=%d",
-    	$cvterm_id, 
-    	$value,
-    	$rank,
-    	$featureprop_id
-  	);
-  	tripal_db_set_active($previous_db);  	
-  }
-                                                                                                                                                                                                                          
-    module_load_include('inc', 'node', 'node.pages');                                                                                                                                                                                
-    drupal_execute('chado_feature_node_form', $node_form_state, $node);  
-
-}
-
-/**
- * 
- *
- * @ingroup tripal_feature
- */
-function tripal_feature_delete_property($featureprop_id) {
-
-  $previous_db = tripal_db_set_active('chado');
-  db_query(
-    "DELETE FROM featureprop WHERE featureprop_id=%d",
-    $featureprop_id
-  );
-  tripal_db_set_active($previous_db);
-
-}
-
 /**
  * 
  *
@@ -351,3 +315,5 @@ function tripal_feature_list_properties_for_node($properties) {
 
   return $output;
 }
+
+

+ 354 - 0
tripal_feature/tripal_feature.api.inc

@@ -253,3 +253,357 @@ function tripal_feature_chado_feature_synonym_schema() {
 
   return $description;
 }
+
+/**
+ * Retrieve properties of a given type for a given analysis featire
+ *
+ * @param $analysis_id
+ *    The analysis ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with a feature ID.
+ * @param $feature_id
+ *    The feature ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with an analysis ID.
+ * @param $analysisfeature_id
+ *    The analysis feature ID for the analysis feature. This argument is 
+ *    optional and can be used rather than specifying the $analysis_id and 
+ *    $feature_id arguments.  If all three arguments are specified (e.g.
+ *    an $analysis_id, $feature_id and $analysisfeature_id, then the 
+ *    $analysisfeature_id is used and the other two arguments are ignored.
+ * @param $property 
+ *    The cvterm name of the properties to retrieve
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * @return
+ *    An analysisfeature chado variable with the specified properties expanded
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_analysis_get_property($analysis_id = NULL ,$feature_id = NUll, 
+   $analysisfeature_id = NULL, $property, $cv_name = 'tripal')
+{
+   
+   // check that the incoming arguments are correct
+   if(($analysis_id and !$feature_id) or
+       (!$analysis_id and $feature_id)){
+       watchdog('tripal_feature', 
+          'tripal_feature_analysis_get_property: Both an analysis ID and feature ID should be specified', 
+           array(), WATCHDOG_WARNING);       
+   }
+
+   // get the analysisfeature_id if one is not provided
+   if(!$analysisfeature_id){
+      $columns = array('analysisfeature_id');
+      $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
+      $result = tripal_core_chado_select('analysisfeature',$columns,$values);
+      $analysisfeature_id = $result[0]->analysisfeature_id;
+   }
+
+   // get the property.
+   return tripal_core_get_property('analysisfeature',$analysisfeature_id,$property,$cv_name);
+}
+
+/**
+ * Insert a property for an analysis feature
+ *
+ * @param $analysis_id
+ *    The analysis ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with a feature ID.
+ * @param $feature_id
+ *    The feature ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with an analysis ID.
+ * @param $analysisfeature_id
+ *    The analysis feature ID for the analysis feature. This argument is 
+ *    optional and can be used rather than specifying the $analysis_id and 
+ *    $feature_id arguments.  If all three arguments are specified (e.g.
+ *    an $analysis_id, $feature_id and $analysisfeature_id, then the 
+ *    $analysisfeature_id is used and the other two arguments are ignored.
+ * @param $property
+ *   The cvterm name of the property to insert
+ * @param $value
+ *   The value of the property to insert
+ * @param $update_if_present
+ *   A boolean indicated whether to update the record if it's already present
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * @return
+ *   True of success, False otherwise
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_analysis_insert_property($analysis_id = NULL, $feature_id = NUll,
+   $analysisfeature_id = NULL, $property, $value, $update_if_present = 0, 
+   $cv_name = 'tripal')
+{
+   
+   // check that the incoming arguments are correct
+   if(($analysis_id and !$feature_id) or
+       (!$analysis_id and $feature_id)){
+       watchdog('tripal_feature', 
+          'tripal_feature_analysis_insert_property: Both an analysis ID and feature ID should be specified', 
+           array(), WATCHDOG_WARNING);       
+   }
+
+   // get the analysisfeature_id if one is not provided
+   if(!$analysisfeature_id){
+      $columns = array('analysisfeature_id');
+      $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
+      $result = tripal_core_chado_select('analysisfeature',$columns,$values);
+      $analysisfeature_id = $result[0]->analysisfeature_id;
+   }
+
+   // insert the property.
+   return tripal_core_insert_property('analysisfeature',$analysisfeature_id,
+      $property,$cv_name,$value,$update_if_present);
+}
+
+/**
+ * Update a property for an analysis feature
+ *
+ * @param $analysis_id
+ *    The analysis ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with a feature ID.
+ * @param $feature_id
+ *    The feature ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with an analysis ID.
+ * @param $analysisfeature_id
+ *    The analysis feature ID for the analysis feature. This argument is 
+ *    optional and can be used rather than specifying the $analysis_id and 
+ *    $feature_id arguments.  If all three arguments are specified (e.g.
+ *    an $analysis_id, $feature_id and $analysisfeature_id, then the 
+ *    $analysisfeature_id is used and the other two arguments are ignored.
+ * @param $property
+ *   The cvterm name of the property to update
+ * @param $value
+ *   The value of the property to update
+ * @param $insert_if_missing 
+ *   A boolean indicated whether to insert the record if it's absent
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * Note: The property will be identified using the unique combination of the $analysis_id and $property
+ * and then it will be updated with the supplied value
+ *
+ * @return
+ *   True of success, False otherwise
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_analysis_update_property($analysis_id = NULL, $feature_id = NUll, 
+   $analysisfeature_id = NULL, $property, $value, $insert_if_missing = 0, 
+   $cv_name = 'tripal')
+{
+  
+   // check that the incoming arguments are correct
+   if(($analysis_id and !$feature_id) or
+       (!$analysis_id and $feature_id)){
+       watchdog('tripal_feature', 
+          'tripal_feature_analysis_update_property: Both an analysis ID and feature ID should be specified', 
+           array(), WATCHDOG_WARNING);       
+   }
+
+   // get the analysisfeature_id if one is not provided
+   if(!$analysisfeature_id){
+      $columns = array('analysisfeature_id');
+      $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
+      $result = tripal_core_chado_select('analysisfeature',$columns,$values);
+      $analysisfeature_id = $result[0]->analysisfeature_id;
+   }
+
+   // update the property.
+   return tripal_core_update_property('analysisfeature',$analysisfeature_id,$property,$cv_name,$value, $insert_if_missing);
+}
+
+/**
+ * Delete a property for an analysis feature
+ *
+ * @param $analysis_id
+ *    The analysis ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with a feature ID.
+ * @param $feature_id
+ *    The feature ID for the analysis feature. This argument is optional but
+ *    if specified it must also be accompanied with an analysis ID.
+ * @param $analysisfeature_id
+ *    The analysis feature ID for the analysis feature. This argument is 
+ *    optional and can be used rather than specifying the $analysis_id and 
+ *    $feature_id arguments.  If all three arguments are specified (e.g.
+ *    an $analysis_id, $feature_id and $analysisfeature_id, then the 
+ *    $analysisfeature_id is used and the other two arguments are ignored.
+ * @param $property
+ *    The cvterm name of the property to delete
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * Note: The property will be identified using the unique combination of the $analysis_id and $property
+ * and then it will be deleted
+ *
+ * @return
+ *   True of success, False otherwise 
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_analysis_delete_property($analysis_id = NULL, $feature_id = NUll, 
+   $analysisfeature_id = NULL, $property, $cv_name = 'tripal')
+{
+     
+   // check that the incoming arguments are correct
+   if(($analysis_id and !$feature_id) or
+       (!$analysis_id and $feature_id)){
+       watchdog('tripal_feature', 
+          'tripal_feature_analysis_delete_property: Both an analysis ID and feature ID should be specified', 
+           array(), WATCHDOG_WARNING);       
+   }
+
+   // get the analysisfeature_id if one is not provided
+   if(!$analysisfeature_id){
+      $columns = array('analysisfeature_id');
+      $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
+      $result = tripal_core_chado_select('analysisfeature',$columns,$values);
+      $analysisfeature_id = $result[0]->analysisfeature_id;
+   }
+
+   // get the property.   
+   return tripal_core_delete_property('analysisfeature',$analysisfeature_id,$property,$cv_name);
+}
+/**
+ * Retrieve properties of a given type for a given feature
+ *
+ * @param $feature_id
+ *    The feature_id of the properties you would like to retrieve
+ * @param $property
+ *    The cvterm name of the properties to retrieve
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * @return
+ *    A feature chado variable with the specified properties expanded
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_get_property($feature_id,$property,$cv_name='tripal')
+{
+   return tripal_core_get_property('feature',$feature_id,$property,$cv_name);
+}
+
+/**
+ * Insert a given property
+ *
+ * @param $feature_id
+ *   The feature_id of the property to insert
+ * @param $property
+ *   The cvterm name of the property to insert
+ * @param $value
+ *   The value of the property to insert
+ * @param $update_if_present
+ *   A boolean indicated whether to update the record if it's already present
+ * @param $cv_name
+ *    Optional.  The name of the cv to which the property belongs.  By
+ *    default this is the 'tripal' cv.
+ *
+ * @return
+ *   True of success, False otherwise
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_insert_property($feature_id, $property, $value,
+   $update_if_present = 0, $cv_name = 'tripal')
+{
+    return tripal_core_insert_property('feature',$feature_id,$property,$cv_name,$value,$update_if_present);
+}
+/**
+ * Update a feature property using the property name. Only use this
+ * if the property is unique and only exist once for the feature.
+ *
+ * @param $feature_id
+ *   The feature_id of the property to update
+ * @param $property
+ *   The cvterm name of the property to update
+ * @param $value
+ *   The value of the property to update
+ * @param $insert_if_missing 
+ *   A boolean indicated whether to insert the record if it's absent
+ * @param $cv_name
+ *   Optional.  The name of the cv to which the property belongs.  By
+ *   default this is the 'tripal' cv.
+ *
+ * Note: The property will be identified using the unique combination of the $feature_id and $property
+ * and then it will be updated with the supplied value
+ *
+ * @return
+ *   True of success, False otherwise
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_update_property($feature_id, $property, 
+  $value, $insert_if_missing = 0, $cv_name = 'tripal')
+{
+   return tripal_core_update_property('feature',$feature_id,$property,$cv_name,$value, $insert_if_missing);
+}
+
+/**
+ * Update a given feature property using the featureprop_id
+ *
+ * @param $featureprop_id
+ *   The featureprop_id of the property to update
+ * @param $property
+ *   The cvterm name of the property
+ * @param $value
+ *   The value of the property
+ * @param $cv_name
+ *   Optional.  The name of the cv to which the property belongs.  By
+ *   default this is the 'tripal' cv.
+ *
+ * @return
+ *   True of success, False otherwise
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_update_property_by_id($featureprop_id, $property, 
+  $value, $cv_name = 'tripal')
+{
+   return tripal_core_update_property_by_id('feature',$featureprop_id,$property,$cv_name,$value);
+}
+
+/**
+ * Delete a given feature property using the property name.  Only use this
+ * if the property is unique and only exists once for the feature.
+ *
+ * @param $feature_id
+ *   The feature_id of the property to delete
+ * @param $property
+ *   The cvterm name of the property to delete
+ * @param $cv_name
+ *   Optional.  The name of the cv to which the property belongs.  By
+ *   default this is the 'tripal' cv.
+ *
+ * Note: The property will be identified using the unique combination of the $feature_id and $property
+ * and then it will be deleted
+ *
+ * @return
+ *   True of success, False otherwise 
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_delete_property($feature_id,$property,$cv_name='tripal'){
+   return tripal_core_delete_property('feature',$feature_id,$property,$cv_name);
+}
+/**
+ * Delete a given feature property using the featureprop_id
+ *
+ * @param $featureprop_id
+ *   The feature_id of the property to delete
+ *
+ * @return
+ *   True of success, False otherwise 
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_delete_property_by_id($featureprop_id){
+   return tripal_core_delete_property_by_id('feature',$featureprop_id);
+}