Browse Source

Updates to the Feature module

spficklin 14 years ago
parent
commit
492099386d

+ 1 - 1
tripal_feature/fasta_loader.php

@@ -334,7 +334,7 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
             $uname = trim($matches[1]);
          } else {
             preg_match("/^(.*?)[\s\|].*$/",$line,$matches);
-            $name = trim($matches[1]);
+            $uname = trim($matches[1]);
          }         
          preg_match("/$re_accession/",$line,$matches);
          $accession = trim($matches[1]);

+ 124 - 131
tripal_feature/tripal_feature-db_references.inc

@@ -19,50 +19,47 @@ function tripal_feature_add_ALL_dbreferences_page($node) {
   return $output;
 }
 
-
-/**
+/*******************************************************************************
  * Implements Hook_form()
- * Handles adding of Database References to Stocks
+ * Handles adding of Database References to features
  */
 function tripal_feature_add_ONE_dbreference_form($form_state, $node) {
 
-  $stock_id = $node->stock_id;
-
-  $form['db_nid'] = array(
+  $form['nid'] = array(
     '#type' => 'hidden',
     '#value' => $node->nid
   );
 
+  $form['feature_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->feature->feature_id,
+  );
+
   $form['add_dbreference'] = array(
     '#type' => 'fieldset',
     '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
   );
 
-  $form['add_properties']['db_stock_id'] = array(
-    '#type' => 'value',
-    '#value' => $stock_id,
-    '#required' => TRUE
-
-  );
-
   $form['add_dbreference']['accession'] = array(
     '#type' => 'textfield',
-    '#title' => t('Accession Number'),
+    '#title' => t('Accession'),
+    '#required' => TRUE,
   );
 
-  $form['add_dbreference']['db_description'] = array(
+  $form['add_dbreference']['description'] = array(
     '#type' => 'textarea',
-    '#title' => t('Description of Database Reference') . '<span class="form-optional" title="This field is optional">+</span>',
-    '#description' => t('Optionally enter a description about the database accession.')
+    '#title' => t('Description of Reference') . '<span class="form-optional" title="This field is optional">+</span>',
+    '#description' => t('Optionally enter a description about the database accession.'),
   );
 
   $db_options = tripal_db_get_db_options();
   $db_options[0] = 'Select a Database';
   ksort($db_options);
-  $form['add_dbreference']['database'] = array(
+  $form['add_dbreference']['db_id'] = array(
     '#type' => 'select',
     '#title' => t('Database'),
     '#options' => $db_options,
+    '#required' => TRUE,
   );
 
   $form['add_dbreference']['submit'] = array(
@@ -72,79 +69,67 @@ function tripal_feature_add_ONE_dbreference_form($form_state, $node) {
 
   return $form;
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_add_ONE_dbreference_form_validate($form, &$form_state) {
 
-  // Only ensure db reference valid if adding
-  if ($form_state['clicked_button']['#value'] == t('Add Database Reference') ) {
-    //Do work of required validators
-    if ($form_state['values']['accession'] == '') {
-      form_set_error('accession', 'Accession field is Required.');
-    }
-
-    // Check database is valid db_id in chado
-    if ( $form_state['values']['database'] > 0) {
-    	$previous_db = tripal_db_set_active('chado');
-    	$tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM db WHERE db_id=%d",$form_state['values']['database']));
-    	tripal_db_set_active($previous_db);
-    	
-      if ($tmp_obj->count != 1) {
-        form_set_error('database', 'The database you selected is not valid. Please choose another one.'); 
-      }
-    } else {
-      form_set_error('database', 'Please select a database');
-    }
-
-    // Check Accession is unique for database
-    $previous_db = tripal_db_set_active('chado');
-    $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM dbxref WHERE accession='%s'",$form_state['values']['accession']));
-    tripal_db_set_active($previous_db);
-    	
-    if ($tmp_obj->count > 0) {
-      form_set_error('accession', 'This accession has already been assigned to another stock.'); 
-    }
-
-  } //end of if adding
+   $db_id = $form_state['values']['db_id'];
+   $accession = $form_state['values']['accession'];
+   $description = $form_state['values']['description'];
+   $feature_id = $form_state['values']['feature_id'];
+   $nid = $form_state['values']['nid'];
+
+   // Check database is valid db_id in chado
+   $previous_db = tripal_db_set_active('chado');
+   $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM db WHERE db_id=%d",$db_id));
+   tripal_db_set_active($previous_db);    	
+   if ($tmp_obj->count != 1) {
+      form_set_error('database', 'The database you selected is not valid. Please choose another one.'); 
+   }
+
+   // Check Accession is unique for database
+   $previous_db = tripal_db_set_active('chado');
+   $sql = "SELECT count(*) as count FROM dbxref WHERE accession='%s' and db_id = %d";
+   $tmp_obj = db_fetch_object(db_query($sql,$accession,$db_id));
+   tripal_db_set_active($previous_db);
+
+   if ($tmp_obj->count > 0) {
+      form_set_error('accession', 'This accession has already been assigned to another feature in the selected database.'); 
+   }
 
 } 
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_add_ONE_dbreference_form_submit($form, &$form_state) {
 
-  // FIX: Sometimes on programatic submission of form (drupal_execute) values in the form state get lost
-  // 	  however, the post values always seem to be correct
-  if (empty($form_state['values']['db_stock_id'])) { $form_state['values']['db_stock_id'] = $form_state['clicked_button']['#post']['db_stock_id']; }
-
-  // Only Create if valid
-  if ($form_state['values']['database'] > 0) {
-    
-
-    // create dbxref
-    $previous_db = tripal_db_set_active('chado');
-    db_query(
-      "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
-      $form_state['values']['database'],
-      $form_state['values']['accession'],
-      $form_state['values']['db_description']
-    );
-    tripal_db_set_active($previous_db);
-
-    //create stock_dbxref
-    $dbxref = tripal_db_get_dbxref( array('db_id'=>array('type'=>'INT','value'=>$form_state['values']['database']), 
-    																	'accession'=>array('type'=>'STRING','exact'=>TRUE,'value'=>$form_state['values']['accession']) ) );
-		if (!empty($dbxref->dbxref_id)) {
-			$previous_db = tripal_db_set_active('chado');
-    	db_query(
-      	"INSERT INTO stock_dbxref (stock_id, dbxref_id) VALUES (%d, %d)",
-      	$form_state['values']['db_stock_id'],
-      	$dbxref->dbxref_id
-    	);
-			tripal_db_set_active($previous_db);
-
+   $db_id = $form_state['values']['db_id'];
+   $accession = $form_state['values']['accession'];
+   $description = $form_state['values']['description'];
+   $feature_id = $form_state['values']['feature_id'];
+   $nid = $form_state['values']['nid'];
+
+   // create dbxref
+   $previous_db = tripal_db_set_active('chado');
+   $isql =  "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')";
+   db_query($isql,$db_id,$accession,$description);
+   tripal_db_set_active($previous_db);
+
+   //create feature_dbxref
+   $dbxref = tripal_db_get_dbxref( array('db_id'=>array('type'=>'INT','value'=>$form_state['values']['db_id']), 
+ 											'accession'=>array('type'=>'STRING','exact'=>TRUE,'value'=>$form_state['values']['accession']) ) );
+
+	if (!empty($dbxref->dbxref_id)) {
+		$previous_db = tripal_db_set_active('chado');
+      $isql = "INSERT INTO feature_dbxref (feature_id, dbxref_id) VALUES (%d, %d)";
+ 	   db_query($isql,$feature_id,$dbxref->dbxref_id);
+		tripal_db_set_active($previous_db);
     	drupal_set_message('Successfully Added Database Reference');
-    } else {
-    	drupal_set_message('Database reference NOT successfully created...','error');
-    } //end of if dbxref was created successfully
-  } //end of if valid db reference
+      drupal_goto('node/'.$nid);
+   } else {
+ 	   drupal_set_message('Database reference NOT successfully created...','error');
+   } //end of if dbxref was created successfully
 
 }
 
@@ -160,14 +145,13 @@ function tripal_feature_edit_ALL_dbreferences_page($node) {
   $output .= '<br>';
   $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
   $output .= '<br>';
-  $output .= drupal_get_form('tripal_feature_implement_back_to_stock_button', $node->nid);
+  $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
 
   return $output;
 }
-                                               
-/**                                            
+/*******************************************************************************                                           
  * Implements Hook_form()                      
- * Handles adding of Properties & Synonyms to Stocks
+ * Handles adding of Properties & Synonyms to Features
  */                                        
 function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
   $form = array();
@@ -178,8 +162,14 @@ function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
   );
 
   $i=0;
-  if (sizeof($node->db_references) != 0) {
-  foreach ($node->db_references as $ref) {
+
+  // pre populate the database options
+  $db_options = tripal_db_get_db_options();
+  $db_options[0] = 'Select a Database';
+  ksort($db_options);
+
+  if (sizeof($node->references) != 0) {
+  foreach ($node->references as $ref) {
     $i++;
     $form["num-$i"] = array(
       '#type' => 'item',
@@ -188,24 +178,21 @@ function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
 
     $form["accession-$i"] = array(
       '#type' => 'textfield',
-      //'#title' => t('Accession'),
+      '#title' => t('Accession'),
       '#size' => 30,
       '#required' => TRUE,
       '#default_value' => $ref->accession
     );
 
-    $db_options = tripal_db_get_db_options();
-    $db_options[0] = 'Select a Database';
-    ksort($db_options);
-    $form["database-$i"] = array( 
+    $form["db_id-$i"] = array( 
       '#type' => 'select', 
-      //'#title' => t('Database'),
+      '#title' => t('Database'),
       '#options' => $db_options, 
       '#default_value' => $ref->db_id
     );
 
 
-    $form["id-$i"] = array(
+    $form["dbxref_id-$i"] = array(
       '#type' => 'hidden',
       '#value' => $ref->dbxref_id
     );
@@ -224,75 +211,81 @@ function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
 
   $form["submit-edits"] = array(
     '#type' => 'submit',
-    '#value' => t('Update DB References')
+    '#value' => t('Update References')
   );
 
   return $form;
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_edit_ALL_db_references_form_submit($form, &$form_state) {
 
-  if ($form_state['clicked_button']['#value'] == t('Update DB References') ) {
+  $num_refs = $form_state['values']['num_db_references'];
+  $action = $form_state['clicked_button']['#value'];
+  $nid = $form_state['values']['nid'];
 
-     //Update all
-     for ($i=1; $i<=$form_state['values']['num_db_references']; $i++) {
-       tripal_feature_update_db_reference(
-					$form_state['values']["id-$i"], 
-					$form_state['values']["database-$i"], 
-					$form_state['values']["accession-$i"]
-			);
+  if (strcmp($action,'Update References')==0) {
+     for ($i=1; $i<=$num_refs; $i++) {
+		 $dbxref_id = $form_state['values']["dbxref_id-$i"]; 
+		 $db_id = $form_state['values']["db_id-$i"];
+		 $accession = $form_state['values']["accession-$i"];
+       tripal_feature_update_db_reference($dbxref_id,$db_id,$accession);
      }
      drupal_set_message("Updated all Database References");
-     drupal_goto('node/'.$form_state['values']['nid']);
-
-  } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
-
+     drupal_goto('node/'.$nid);
+  } 
+  elseif ( preg_match('/Delete #(\d+)/', $action, $matches) ) {
      $i = $matches[1];
-     tripal_feature_delete_db_reference($form_state['values']["id-$i"]);
+	  $dbxref_id = $form_state['values']["dbxref_id-$i"]; 
+     tripal_feature_delete_db_reference($dbxref_id);
      drupal_set_message("Deleted Database Reference");
-
-  } else {
+     drupal_goto('node/'.$nid);
+  } 
+  else {
     drupal_set_message("Unrecognized Button Pressed",'error');
   }
 
 }
+/*******************************************************************************
+*
+*/
+function tripal_feature_update_db_reference($dbxref_id, $db_id, $accession) {
 
-function tripal_feature_update_db_reference($dbxref_id, $database_id, $accession) {
-
-  $previous_db = db_set_active('chado');
-  db_query(
-    "UPDATE dbxref SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
-    $database_id, 
-    $accession,
-    $dbxref_id
-  );
-  db_set_active($previous_db);
+  $previous_db = tripal_db_set_active('chado');
+  $sql =  "UPDATE dbxref SET db_id=%d, accession='%s' WHERE dbxref_id=%d";
+  db_query($sql,$db_id,$accession,$dbxref_id);
+  tripal_db_set_active($previous_db);
 
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_delete_db_reference($dbxref_id) {
 
-  $previous_db = db_set_active('chado');
+  $previous_db = tripal_db_set_active('chado');
   db_query(
     "DELETE FROM dbxref WHERE dbxref_id=%d",
     $dbxref_id
   );
 
   db_query(
-    "DELETE FROM stock_dbxref WHERE dbxref_id=%d",
+    "DELETE FROM feature_dbxref WHERE dbxref_id=%d",
     $dbxref_id
   );
-  db_set_active($previous_db);
+  tripal_db_set_active($previous_db);
 
 }
-
+/*******************************************************************************
+*
+*/
 function theme_tripal_feature_edit_ALL_db_references_form ($form) {
   $output = '';
 
   $output .= '<br><fieldset>';
   $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
   $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
-  	     .'is a unique identifier for this stock in the specified database.</p>';
+  	     .'is a unique identifier for this feature in the specified database.</p>';
   $output .= '<table>';
   $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
 
@@ -331,7 +324,7 @@ function list_dbreferences_for_node($db_references) {
     $output .= '</table>'; 
 
   } else {
-    $output = 'No Database References Added to the Current Stock';
+    $output = 'No Database References Added to the Current Feature';
   }              
 
   return $output;

+ 94 - 86
tripal_feature/tripal_feature-relationships.inc

@@ -20,13 +20,13 @@ function tripal_feature_add_ALL_relationships_page($node) {
 }
 /*************************************************************************
  * Implements Hook_form()
- * Handles adding of Relationships to Stocks
+ * Handles adding of Relationships to Features
  */
 function tripal_feature_add_ONE_relationship_form($form_state, $node) {
 
-  $stock_id = $node->stock_id;
+  $feature_id = $node->feature_id;
   $organism_id = $node->organism->organism_id;
-  $_SESSION['organism'] = $organism_id; //needed for autocomplete enter stock to work
+  $_SESSION['organism'] = $organism_id; //needed for autocomplete enter feature to work
 
   $form['rel_nid'] = array(
     '#type' => 'hidden', 
@@ -40,13 +40,13 @@ function tripal_feature_add_ONE_relationship_form($form_state, $node) {
 
   $form['add_relationships']['description'] = array(
     '#type' => 'item',
-    '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, Fred is_the_paternal_parent_of Matty, where Fred & Matty are both stocks.')
+    '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, X part_of Y, where X & Y are genomic features.')
   );
 
   $form['add_relationships']['subject_id'] = array(
     '#type' => 'textfield',
     '#title' => t('Subject'),
-    '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
+    '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
   );
 
   $cv = tripal_cv_get_cv_by_name('relationship');
@@ -63,7 +63,7 @@ function tripal_feature_add_ONE_relationship_form($form_state, $node) {
   $form['add_relationships']['object_id'] = array(
     '#type' => 'textfield',
     '#title' => t('Object'),
-    '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
+    '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
   );
 
   $form['add_relationships']['r_description'] = array(
@@ -77,14 +77,14 @@ function tripal_feature_add_ONE_relationship_form($form_state, $node) {
     '#value' => t('Add a Relationship')
   );
 
-  $form['add_relationships']['r_stock_id'] = array(
+  $form['add_relationships']['r_feature_id'] = array(
     '#type' => 'value',
-    '#value' => $stock_id,
+    '#value' => $feature_id,
     '#required' => TRUE
 
   );
 
-  $form['add_relationships']['r_stock_uniquename'] = array( 
+  $form['add_relationships']['r_feature_uniquename'] = array( 
     '#type' => 'value',
     '#value' => $node->uniquename,
     '#required' => TRUE
@@ -100,7 +100,7 @@ function tripal_feature_add_ONE_relationship_form_validate($form, &$form_state)
 
   //Require Validation if adding
   if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
-    // check valid stock selected for subject
+    // check valid feature selected for subject
     $criteria = array('unknown' => array('value'=> $form_state['values']['subject_id'], 
       																		'columns'=>array('name','uniquename','accession','synonym') ));
     $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
@@ -190,7 +190,7 @@ function tripal_feature_edit_ALL_relationships_page($node) {
   $output .= '<br>';
   $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
   $output .= '<br>';
-  $output .= drupal_get_form('tripal_feature_implement_back_to_stock_button', $node->nid);
+  $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
 
   return $output;
 }
@@ -221,74 +221,82 @@ function tripal_feature_edit_ALL_relationships_form($form_state, $node) {
      $relationships = array_merge($relationships,$node->subject_relationships);
   }
   if (sizeof($relationships) != 0) {
-  foreach ($relationships as $r) {
-
-    $i++;
-    $form["num-$i"] = array(
-      '#type' => 'item',
-      '#value' => $i.'.'
-    );
-
-    $form["id-$i"] = array(
-      '#type' => 'hidden',
-      '#value' => $r->stock_relationship_id
-    );
-
-    //Enter relationship specific fields
-    if ( !empty($r->subject_id) ) { 
-      $default = $r->subject_uniquename;
-      $description = l($r->subject_name, 'node/'.$r->subject_nid); 
-    } else { 
-       $default = $node->uniquename; $description = 'Current Feature'; 
-    }
-    $form["subject_id-$i"] = array(
-      '#type' => 'textfield',      
-      //'#title' => t('Subject'), 
-      '#required'   => TRUE,
-      '#size' => 30,
-      '#default_value' => $default,
-      '#description' => $description,
-    ); 
-
-    $type_options = tripal_cv_get_cvterm_options( variable_get('chado_feature_relationship_cv', 'null') );
-    ksort($type_options);          
-    $form["type_id-$i"] = array(  
-      '#type' => 'select',    
-      //'#title' => t('Type of Relationship'), 
-      '#options' => $type_options,
-      '#required' => TRUE,
-      '#default_value' => $r->relationship_type_id
-    );
-
-    if ( !empty($r->object_id) ) { 
-      $default = $r->object_uniquename;
-      $description = l($r->object_name, 'node/'.$r->object_nid);
-    } else { $default = $node->uniquename; $description = 'Current Feature'; }
-    $form["object_id-$i"] = array(
-      '#type' => 'textfield',          
-      //'#title' => t('Object'),      
-      '#required'   => TRUE,
-      '#size' => 30,
-      '#default_value' => $default,
-      '#description' => $description
-    );
-
-    $form["submit-$i"] = array(
-      '#type' => 'submit',
-      '#value' => t("Delete #$i")
-    );
-
-  }} //end of foreach relationship
+     foreach ($relationships as $r) {
+
+       $i++;
+       $form["num-$i"] = array(
+         '#type' => 'item',
+         '#value' => $i.'.'
+       );
+
+       $form["id-$i"] = array(
+         '#type' => 'hidden',
+         '#value' => $r->stock_relationship_id
+       );
+
+       //Enter relationship specific fields
+       if ( !empty($r->subject_id) ) { 
+         $default = $r->subject_uniquename;
+         $description = l($r->subject_name, 'node/'.$r->subject_nid); 
+       } else { 
+          $default = $node->uniquename; $description = 'Current Feature'; 
+       }
+       $form["subject_id-$i"] = array(
+         '#type' => 'textfield',      
+         //'#title' => t('Subject'), 
+         '#required'   => TRUE,
+         '#size' => 30,
+         '#default_value' => $default,
+         '#description' => $description,
+       ); 
+
+       $cv = tripal_cv_get_cv_by_name('relationship');
+       $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
+       ksort($type_options);          
+       $form["type_id-$i"] = array(  
+         '#type' => 'select',    
+         //'#title' => t('Type of Relationship'), 
+         '#options' => $type_options,
+         '#required' => TRUE,
+         '#default_value' => $r->relationship_type_id
+       );
+
+       if ( !empty($r->object_id) ) { 
+         $default = $r->object_uniquename;
+         $description = l($r->object_name, 'node/'.$r->object_nid);
+       } else { $default = $node->uniquename; $description = 'Current Feature'; }
+       $form["object_id-$i"] = array(
+         '#type' => 'textfield',          
+         //'#title' => t('Object'),      
+         '#required'   => TRUE,
+         '#size' => 30,
+         '#default_value' => $default,
+         '#description' => $description
+       );
+
+       $form["submit-$i"] = array(
+         '#type' => 'submit',
+         '#value' => t("Delete #$i")
+       );
+
+     } //end of foreach relationship
+     $form['num_relationships'] = array(
+       '#type' => 'hidden',
+       '#value' => $i
+     );
+
+     $form["submit-edits"] = array(
+       '#type' => 'submit',
+       '#value' => t('Update Relationships')
+     );
+  } else {
+     $form["info"] = array(
+       '#type' => 'markup',
+       '#value' => t('No relationships currently exist for this feature.')
+     );
+  } 
 
-  $form['num_relationships'] = array(
-    '#type' => 'hidden',
-    '#value' => $i
-  );
 
-  $form["submit-edits"] = array(
-    '#type' => 'submit',
-    '#value' => t('Update Relationships')
-  );
 
   return $form;
 }
@@ -439,11 +447,11 @@ function theme_tripal_feature_edit_ALL_relationships_form ($form) {
   $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
 
   for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
-    $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
-    	       .drupal_render($form["subject_id-$i"]).'</td><td>'
-	       .drupal_render($form["type_id-$i"]).'</td><td>'
-	       .drupal_render($form["object_id-$i"]).'</td><td>'
-	       .drupal_render($form["submit-$i"]).'</td></tr>';
+    $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td>'.
+      	      '<td>'.drupal_render($form["subject_id-$i"]).'</td>'.
+	            '<td>'.drupal_render($form["type_id-$i"]).'</td>'.
+     	         '<td>'.drupal_render($form["object_id-$i"]).'</td>'.
+	            '<td>'.drupal_render($form["submit-$i"]).'</td></tr>';
   }
 
   $output .= '</table><br>';
@@ -461,7 +469,7 @@ function theme_tripal_feature_edit_ALL_relationships_form ($form) {
 /**
  *
  */
-function list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
+function tripal_feature_list_relationships_for_node($feature_name, $subject_relationships, $object_relationships) {
 
   if (!empty($subject_relationships) OR !empty($object_relationships) ) {
     $output = '<table>';
@@ -469,19 +477,19 @@ function list_relationships_for_node($stock_name, $subject_relationships, $objec
 
     if (!empty($subject_relationships) ) {
       foreach ($subject_relationships as $s) {
-        $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$stock_name.'</td></tr>';
+        $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$feature_name.'</td></tr>';
       }
     }
 
     if (!empty($object_relationships) ) {
       foreach ($object_relationships as $o) {
-        $output .= '<tr><td>'.$stock_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
+        $output .= '<tr><td>'.$feature_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
       } // end of foreach property
     }
 
     $output .= '</table>';
   } else {
-    $output = 'No Relationships Involving the Current Stock'; 
+    $output = 'No Relationships For the Current Feature'; 
   }
 
   return $output;

+ 20 - 10
tripal_feature/tripal_feature-secondary_tables.inc

@@ -1,7 +1,7 @@
 <?php
 // $Id$
 
-function tripal_feature_implement_back_to_stock_button($form_state, $nid) {
+function tripal_feature_implement_back_to_feature_button($form_state, $nid) {
   $form = array();
 
   $form['nid'] = array(
@@ -16,16 +16,20 @@ function tripal_feature_implement_back_to_stock_button($form_state, $nid) {
 
   return $form;
 }
-
-function tripal_feature_implement_back_to_stock_button_submit($form, $form_state) {
+/*******************************************************************************
+*
+*/
+function tripal_feature_implement_back_to_feature_button_submit($form, $form_state) {
   drupal_goto('node/'.$form_state['values']['nid']);
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_implement_add_chado_properties_progress($current) {
 
     $value = '<div class="form_progress"><div class="form_progress-text">'; 
 
-    if ($current == 'main') { $value .= '<span id="form-step-current">Create Basic Stock</span>'; } 
+    if ($current == 'main') { $value .= '<span id="form-step-current">Create Basic Feature</span>'; } 
     else { $value .= '<span id="form-step">Create Basic Stock</span>'; }
 
     $value .= '<span id="form-segway">  >>  </span>';
@@ -48,7 +52,9 @@ function tripal_feature_implement_add_chado_properties_progress($current) {
     return $value;
 
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_implement_add_chado_properties_navigate($form_state, $step, $nid) {
   $form = array();
 
@@ -109,7 +115,9 @@ function tripal_feature_implement_add_chado_properties_navigate($form_state, $st
 
   return $form;
 }
-
+/*******************************************************************************
+*
+*/
 function tripal_feature_implement_add_chado_properties_navigate_submit($form, $form_state) {
 
   $raw_steps = preg_split('/::/', $form_state['values']['steps']); 
@@ -161,7 +169,7 @@ function tripal_feature_implement_add_chado_properties_navigate_submit($form, $f
  * Implements Hook_form()
  * Handles setting the is_obsolete property of stocks
  */
-function tripal_stock_is_obsolete_form($node, $stock_id) {
+function tripal_feature_is_obsolete_form($node, $stock_id) {
 
   $form['make_obsolete'] = array(
     '#type' => 'submit',
@@ -176,8 +184,10 @@ function tripal_stock_is_obsolete_form($node, $stock_id) {
 
   return $form;
 }
-
-function tripal_stock_is_obsolete_form_submit($form, &$form_state) {
+/*******************************************************************************
+*
+*/
+function tripal_feature_is_obsolete_form_submit($form, &$form_state) {
 
   $previous_db = db_set_active('chado');
   db_query(

+ 136 - 13
tripal_feature/tripal_feature.module

@@ -21,6 +21,9 @@ function tripal_feature_init(){
    // add the jGCharts JS and CSS
    drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js'); 
    drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js'); 
+
+   drupal_add_css(drupal_get_path('theme', 'tripal').
+                                  '/css/tripal_feature.css');
 }
 
 /*************************************************************************
@@ -137,7 +140,7 @@ function tripal_feature_menu() {
    );
 
  // Adding Secondary Properties-----------------
-  $items['node/%/properties'] = array(       
+  $items['node/%tripal_feature_node/properties'] = array(       
     'title' => t('Add Properties & Synonyms'),                         
     'description' => t('Settings for Features'),
     'page callback' => 'tripal_feature_add_ALL_property_page',           
@@ -146,7 +149,7 @@ function tripal_feature_menu() {
     'type' => MENU_CALLBACK
   ); 
 
-  $items['node/%/db_references'] = array(                        
+  $items['node/%tripal_feature_node/db_references'] = array(                        
     'title' => t('Add Database References'),                   
     'description' => t('Settings for Features'),              
     'page callback' => 'tripal_feature_add_ALL_dbreferences_page',                         
@@ -155,7 +158,7 @@ function tripal_feature_menu() {
     'type' => MENU_CALLBACK
   ); 
 
-  $items['node/%/relationships'] = array(                      
+  $items['node/%tripal_feature_node/relationships'] = array(                      
     'title' => t('Add Relationships'),                      
     'description' => t('Settings for Features'),               
     'page callback' => 'tripal_feature_add_ALL_relationships_page',                          
@@ -164,7 +167,7 @@ function tripal_feature_menu() {
     'type' => MENU_CALLBACK
   );
   //Edit/Deleting Secondary Properties-------------
-  $items['node/%/edit_properties'] = array(
+  $items['node/%tripal_feature_node/edit_properties'] = array(
     'title' => t('Edit Properties'),
     'description' => t('Settings for Features'),
     'page callback' => 'tripal_feature_edit_ALL_properties_page',
@@ -174,7 +177,7 @@ function tripal_feature_menu() {
     'weight' => 8,
   );
 
-    $items['node/%/edit_relationships'] = array(
+    $items['node/%tripal_feature_node/edit_relationships'] = array(
     'title' => t('Edit Relationships'),
     'description' => t('Settings for Feature'), 
     'page callback' => 'tripal_feature_edit_ALL_relationships_page',
@@ -184,8 +187,8 @@ function tripal_feature_menu() {
     'weight' => 9,
   );
 
-  $items['node/%/edit_db_references'] = array(
-    'title' => t('Edit DB References'),
+  $items['node/%tripal_feature_node/edit_db_references'] = array(
+    'title' => t('Edit References'),
     'description' => t('Settings for Feature'),
     'page callback' => 'tripal_feature_edit_ALL_dbreferences_page',
     'page arguments' => array(1),
@@ -196,7 +199,22 @@ function tripal_feature_menu() {
 
    return $items;
 }
-
+/*************************************************************************
+ * Implements Menu wildcard_load hook
+ * Purpose: Allows the node ID of a chado stock to be dynamically 
+ *   pulled from the path. The node is loaded from this node ID
+ *   and supplied to the page as an arguement
+ */
+function tripal_feature_node_load($nid) {
+  if (is_numeric($nid)) {
+    $node = node_load($nid);
+    if ($node->type == 'chado_feature') {
+      return $node;
+    }
+  }
+
+  return FALSE;
+}
 /************************************************************************
  *  When a new chado_feature node is created we also need to add information
  *  to our chado_feature table.  This function is called on insert of a new node
@@ -721,7 +739,7 @@ function chado_feature_load($node){
 
    // add feature references in external databases
    $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
-          "   DB.db_id, DB.name as db_name, DB.urlprefix ".
+          "   DB.db_id, DB.name as db_name, DB.urlprefix,DBX.dbxref_id ".
           "FROM {Feature} F ".
           "  INNER JOIN Feature_dbxref FDBX on F.feature_id = FDBX.feature_id ".
           "  INNER JOIN Dbxref DBX on DBX.dbxref_id = FDBX.dbxref_id ".
@@ -742,17 +760,17 @@ function chado_feature_load($node){
    $sql = "SELECT 
              FS.name as subject_name, 
              FS.uniquename as subject_uniquename, 
-             CVTS.name as subject_type,
              FS.residues as subject_residues,
+             CVTS.name as subject_type,
              FR.subject_id,          
-             FR.type_id,
+             FR.type_id as relationship_type_id,
              CVT.name as rel_type,     
              FO.name as object_name, 
              FO.uniquename as object_uniquename, 
              CVTO.name as object_type,
-             FO.residues as object_residues,
              FR.object_id,
              FLS.name as src_name,
+             FLS.uniquename as src_uniquename,
              FLS.feature_id as src_featureid,
              CVTR.name as src_cvname, 
              CVTR.cvterm_id as src_type_id,
@@ -789,6 +807,8 @@ function chado_feature_load($node){
    $orelationships = array();
    $i=0;
    $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
+   $rel_locs;  // holds relationship locations
+   $rel_loc;   // a single relationship location
    while($rel = db_fetch_object($oresults)){
      $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
      if($node){
@@ -798,7 +818,32 @@ function chado_feature_load($node){
      if($node){
         $rel->object_nid = $node->nid;
      }  
-     $orelationships[$i++] = $rel;      
+     $orelationships[$i] = $rel; 
+     if($rel->src_uniquename){
+        $rel_loc = $rel->src_uniquename ."|".$rel->src_type_id;
+        $rel_locs[$rel_loc]['uname'] = $rel->src_uniquename;
+        $rel_locs[$rel_loc]['type'] = $rel->src_type_id;
+        if(!$rel_locs[$rel_loc]['source_min']){
+           $rel_locs[$rel_loc]['source_min'] = 99999999999;
+        }
+        if($rel->fmin < $rel_locs[$rel_loc]['source_min']){
+           $rel_locs[$rel_loc]['source_min'] = $rel->fmin;
+        }     
+        if($rel->fmax > $rel_locs[$rel_loc]['source_max']){
+           $rel_locs[$rel_loc]['source_max'] = $rel->fmax;
+        }
+     }
+     $i++;
+   }
+   // build the parts array needed for coloring the relationsihps in the
+   // reference sequence
+   foreach($orelationships as $index => $rel){
+      if($rel->src_uniquename){
+         $rel_loc = $rel->src_uniquename ."|".$rel->src_type_id;
+         $rel_locs[$rel_loc]['parts'][$index]['type'] = $rel->subject_type;
+         $rel_locs[$rel_loc]['parts'][$index]['start'] = $rel->fmin - $rel_locs[$rel_loc]['source_min'];
+         $rel_locs[$rel_loc]['parts'][$index]['end'] = $rel->fmax - $rel_locs[$rel_loc]['source_min'];
+      }
    }
    $i=0;
    while($rel = db_fetch_object($sresults)){
@@ -815,6 +860,25 @@ function chado_feature_load($node){
    $additions->object_relationships = $orelationships;
    $additions->subject_relationships = $srelationships;
 
+   // now get the sequence from the reference for those feature as a 
+   // subject relationship to this feature.
+   $object_context = array();
+   $i=0;
+   foreach ($rel_locs as $rel_loc => $attrs){
+      if($attrs['uname']){
+         $sql = "SELECT residues 
+                 FROM feature 
+                 WHERE uniquename = '%s' AND organism_id = %d and type_id = %d";
+         $context = db_fetch_object(db_query($sql,$attrs['uname'],$additions->feature->organism_id,$attrs['type']));
+         $context->residues = substr($context->residues,$attrs['source_min'],$attrs['source_max'] - $attrs['source_min']);
+         $context->source = $attrs['uname'];
+         $context->fmin = $attrs['source_min'];
+         $context->fmax = $attrs['source_max'];
+         $context->residues = tripal_feature_color_sequence ($context->residues,$attrs['parts']);
+      }
+      $object_context[$i] = $context;
+   }
+   $additions->relationship_object_info = $object_context;
 
   // get the locations for this feature
    $sql = "SELECT F.name,F.feature_id, F.uniquename,CVT.name as cvname, CVT.cvterm_id,
@@ -855,6 +919,65 @@ function chado_feature_load($node){
    return $additions;
 }
 
+/************************************************************************
+ *  
+ */
+function tripal_feature_color_sequence ($sequence,$parts){
+
+watchdog('tripal_feature',print_r($parts,1));
+   $types = array();
+
+   // first get the list of types so we can create a color legend
+   foreach ($parts as $index => $child){ 
+      $type = $child['type'];
+      if(!in_array($type,$types)){
+         $types[] = $type;
+      }
+   }
+ 
+   $newseq .= "<div id=\"tripal_feature-sequence-legend\">Legend: ";
+   foreach($types as $type){
+      $newseq .= "<span class=\"tripal_feature-sequence-$type\">$type</span>";
+   }
+   $newseq .= "</div>";
+   
+
+   // set the background color of the rows based on the type
+   $pos = 0;
+   $newseq .= "<pre id=\"tripal_feature-sequence\">";
+   foreach ($parts as $index => $child){
+      $type = $child['type'];
+      $start = $child['start'];
+      $end = $child['end'];
+      
+      $class = "class=\"tripal_feature-sequence-$type\"";
+
+      // iterate through the sequence up to the end of the child
+      for ($i = $pos; $i < $end; $i++){ 
+    
+         // if we're at the beginning of the child sequence then set the
+         // appropriate text color
+         if($pos == $start){
+            $newseq .= "<span $class>";
+            $func = 'uc';  // nucleotides within the child should be uppercase
+         }
+         $newseq .= $sequence{$pos};
+         $seqcount++;
+
+         if($seqcount % 100 == 0){
+            $newseq .= "\n";
+         } 
+         $pos++;  
+         if($pos == $end){
+            $newseq .= "</span>";
+            $func = 'lc';
+         }
+      }
+   }
+
+   $newseq .= "</pre>";
+   return $newseq;
+}
 /************************************************************************
  *  This function customizes the view of the chado_feature node.  It allows
  *  us to generate the markup.