Tripal v1.0 (6.x-1.0)
tripal_feature-relationships.inc
Go to the documentation of this file.
00001 <?php
00012 function tripal_feature_add_ALL_relationships_page($node) {
00013   $output = '';
00014 
00015   $output .= tripal_feature_implement_add_chado_properties_progress('relationships') . '<br />';
00016   $output .= '<b>All Relationships should include the CURRENT Individual (' . $node->uniquename . ')</b><br />';
00017   $output .= '<br /><b>Current Relationships</b><br />';
00018   $output .= list_relationships_for_node($node->uniquename, $node->subject_relationships, $node->object_relationships);
00019   $output .= '<br /><br />';
00020   $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
00021   $output .= '<br />';
00022   $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'relationships', $node->nid);
00023   return $output;
00024 }
00025 
00032 function tripal_feature_add_ONE_relationship_form($form_state, $node) {
00033 
00034   $feature_id = $node->feature_id;
00035   $organism_id = $node->organism->organism_id;
00036   $_SESSION['organism'] = $organism_id; //needed for autocomplete enter feature to work
00037 
00038   $form['rel_nid'] = array(
00039     '#type' => 'hidden',
00040     '#value' => $node->nid
00041   );
00042 
00043   $form['add_relationships'] = array(
00044     '#type' => 'fieldset',
00045     '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>',
00046   );
00047 
00048   $form['add_relationships']['description'] = array(
00049     '#type' => 'item',
00050     '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, X part_of Y, where X & Y are genomic features.')
00051   );
00052 
00053   $form['add_relationships']['subject_id'] = array(
00054     '#type' => 'textfield',
00055     '#title' => t('Subject'),
00056     '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
00057   );
00058 
00059   $cv = tripal_cv_get_cv_by_name('relationship');
00060   $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
00061   $type_options[0] = 'Select a Type';
00062   ksort($type_options);
00063   $form['add_relationships']['type_id'] = array(
00064     '#type' => 'select',
00065     '#title' => t('Type of Relationship'),
00066     '#options' => $type_options
00067 
00068   );
00069 
00070   $form['add_relationships']['object_id'] = array(
00071     '#type' => 'textfield',
00072     '#title' => t('Object'),
00073     '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
00074   );
00075 
00076   $form['add_relationships']['r_description'] = array(
00077     '#type' => 'textarea',
00078     '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>',
00079     '#description' => t('Should not include Genotypes and Phenotypes'),
00080   );
00081 
00082   $form['add_relationships']['submit'] = array(
00083     '#type' => 'submit',
00084     '#value' => t('Add a Relationship')
00085   );
00086 
00087   $form['add_relationships']['r_feature_id'] = array(
00088     '#type' => 'value',
00089     '#value' => $feature_id,
00090     '#required' => TRUE
00091 
00092   );
00093 
00094   $form['add_relationships']['r_feature_uniquename'] = array(
00095     '#type' => 'value',
00096     '#value' => $node->uniquename,
00097     '#required' => TRUE
00098   );
00099 
00100   return $form;
00101 
00102 }
00103 
00109 function tripal_feature_add_ONE_relationship_form_validate($form, &$form_state) {
00110 
00111   //Require Validation if adding
00112   if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
00113 
00114     // check valid feature selected for subject
00115     $criteria = array('unknown' => array('value' => $form_state['values']['subject_id'],
00116                                           'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
00117     $subject_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
00118     if (sizeof($subject_results) > 1) {
00119       $links= array();
00120       for ($i=0; $i<sizeof($subject_results); $i++) {
00121       $links[] = l($i+1, "node/" . $subject_results[$i]->nid); }
00122       $message = t("Too many features match '%object'! Please refine your input to match
00123         ONLY ONE feature. <br />To aid in this process, here are the features that
00124         match your initial input: %features",
00125         array('%object' => $form_state['values']['subject_id'],
00126           '%features' => join(', ', $links)
00127         )
00128       );
00129       form_set_error('subject_id', $message);
00130     }
00131     elseif (sizeof($subject_results) < 1) {
00132       form_set_error('subject_id', t("There are no features matching your input. Please check your input for typos and/or lookup the feature <a href='!url'>here</a>", array('!url' => url('features'))));
00133     }
00134     elseif (sizeof($subject_results) == 1) {
00135       $form_state['values']['subject_id'] = $subject_results[0]->stock_id;
00136     }
00137 
00138     // check valid stock selected for object
00139     $criteria = array('unknown' => array('value' => $form_state['values']['object_id'],
00140                                           'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
00141     $object_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
00142     if (sizeof($object_results) > 1) {
00143       $links= array();
00144       for ($i=0; $i<sizeof($object_results); $i++) {
00145       $links[] = l($i+1, "node/" . $object_results[$i]->nid); }
00146       $message = t("Too many features match '%object'! Please refine your input to match
00147         ONLY ONE stock. <br />To aid in this process, here are the stocks that match
00148         your initial input: %features",
00149         array('%object' => $form_state['values']['object_id'],
00150         '%features' => join(', ', $links)
00151         )
00152       );
00153       form_set_error('object_id', $message);
00154     }
00155     elseif (sizeof($object_results) < 1) {
00156       form_set_error('object_id', t("There are no features matching your input. Please check your input for typos and/or lookup the feature <a href='!url'>here</a>", array('!url' => url('features'))));
00157     }
00158     elseif (sizeof($object_results) == 1) {
00159       $form_state['values']['object_id'] = $object_results[0]->stock_id;
00160     }
00161 
00162     // check valid type selected
00163     if ($form_state['values']['type_id'] == 0) {
00164       form_set_error('type_id', 'Please select a type of relationship.');
00165     }
00166     else {
00167       $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
00168 
00169       if ($tmp_obj->count != 1) {
00170         form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
00171       }
00172     }
00173 
00174     // check either subject or object is the current stock
00175     if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
00176       if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
00177         form_set_error('subject_id', 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
00178       }
00179     }
00180   } //end of require validation if adding relationship
00181 }
00182 
00188 function tripal_feature_add_ONE_relationship_form_submit($form, &$form_state) {
00189 
00190   if ($form_state['values']['subject_id'] > 0) {
00191     $previous_db = db_set_active('chado');
00192     db_query(
00193       "INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
00194       $form_state['values']['subject_id'],
00195       $form_state['values']['type_id'],
00196       $form_state['values']['object_id'],
00197       $form_state['values']['r_description']
00198     );
00199     db_set_active($previous_db);
00200 
00201     drupal_set_message(t('Successfully Added Relationship.'));
00202   } //end of insert relationship
00203 
00204 }
00205 
00211 function tripal_feature_edit_ALL_relationships_page($node) {
00212   $output = '';
00213 
00214   $output .= drupal_get_form('tripal_feature_edit_ALL_relationships_form', $node);
00215   $output .= '<br />';
00216   $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
00217   $output .= '<br />';
00218   $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
00219 
00220   return $output;
00221 }
00222 
00229 function tripal_feature_edit_ALL_relationships_form($form_state, $node) {
00230   $form = array();
00231 
00232   $form['nid'] = array(
00233     '#type' => 'hidden',
00234     '#value' => $node->nid
00235   );
00236 
00237   $form['r_feature_uniquename'] = array(
00238     '#type' => 'hidden',
00239     '#value' => $node->uniquename
00240   );
00241 
00242   $i=0;
00243 
00244   $feature = $node->feature;
00245   $orelationships = tripal_feature_load_relationships($feature->feature_id, 'as_object');
00246   $srelationships = tripal_feature_load_relationships($feature->feature_id, 'as_subject');
00247   $relationships = array_merge($orelationships, $srelationships);
00248 
00249   if (sizeof($relationships) != 0) {
00250     foreach ($relationships as $r) {
00251 
00252       $i++;
00253       $form["num-$i"] = array(
00254         '#type' => 'fieldset',
00255         '#title' => t("Relationship %i", array('%i' => $i)),
00256       );
00257 
00258       $form["num-$i"]["id-$i"] = array(
00259         '#type' => 'hidden',
00260         '#value' => $r->stock_relationship_id
00261       );
00262 
00263       //Enter relationship specific fields
00264       if ( !empty($r->subject_id) ) {
00265         $default = $r->subject_uniquename;
00266         $description = l($r->subject_name, 'node/' . $r->subject_nid);
00267       }
00268       else {
00269         $default = $node->uniquename;
00270         $description = "Current Feature";
00271       }
00272       $description .= " (" . $r->subject_type . ")";
00273       $form["num-$i"]["subject_id-$i"] = array(
00274         '#type' => 'textfield',
00275         //'#title' => t('Subject'),
00276         '#required'   => TRUE,
00277         '#size' => 30,
00278         '#default_value' => $default,
00279         '#description' => t('%description', array('%description' => $description)),
00280       );
00281 
00282       $cv = tripal_cv_get_cv_by_name('relationship');
00283       $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
00284       ksort($type_options);
00285       $form["num-$i"]["type_id-$i"] = array(
00286         '#type' => 'select',
00287         //'#title' => t('Type of Relationship'),
00288         '#options' => $type_options,
00289         '#required' => TRUE,
00290         '#default_value' => $r->relationship_type_id
00291       );
00292 
00293       if (!empty($r->object_id) ) {
00294         $default = $r->object_uniquename;
00295         $description = l($r->object_name, 'node/' . $r->object_nid);
00296       }
00297       else {
00298         $default = $node->uniquename;
00299         $description = 'Current Feature';
00300       }
00301       $description .= " (" . $r->object_type . ")";
00302       $form["num-$i"]["object_id-$i"] = array(
00303         '#type' => 'textfield',
00304         //'#title' => t('Object'),
00305         '#required'   => TRUE,
00306         '#size' => 30,
00307         '#default_value' => $default,
00308         '#description' => $description
00309       );
00310 
00311       $form["num-$i"]["delete-$i"] = array(
00312         '#type' => 'submit',
00313         '#value' => t("Delete"),
00314         '#name' => "delete-$i",
00315       );
00316 
00317     } //end of foreach relationship
00318     $form['num_relationships'] = array(
00319       '#type' => 'hidden',
00320       '#value' => $i
00321     );
00322 
00323     $form["submit-edits"] = array(
00324       '#type' => 'submit',
00325       '#value' => t('Update All Relationships')
00326     );
00327   }
00328   else {
00329     $form["info"] = array(
00330       '#type' => 'markup',
00331       '#value' => t('No relationships currently exist for this feature.')
00332     );
00333   }
00334 
00335   return $form;
00336 }
00337 
00343 function tripal_feature_edit_ALL_relationships_form_validate($form, &$form_state) {
00344 
00345   // Only Require if Updating Relationships
00346   if ($form_state['clicked_button']['#value'] == t('Update All Relationships') ) {
00347 
00348     for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
00349 
00350       // check valid stock selected for subject
00351       $criteria = array('unknown' => array('value' => $form_state['values']["subject_id-$i"],
00352                                           'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
00353       $subject_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
00354       if (sizeof($subject_results) > 1) {
00355         $links= array();
00356         for ($j=0; $j<sizeof($subject_results); $j++) {
00357         $links[] = l($j+1, "node/" . $subject_results[$j]->nid); }
00358         $message = t("Too many features match '%subject'!  Please refine your input to
00359           match ONLY ONE feature. <br /> To aid in this process, here are the features that
00360           match your initial input: %features",
00361           array('%subject' => $form_state['values']["subject_id-$i"],
00362             '%features' => join(', ', $links)
00363           )
00364         );
00365         form_set_error("subject_id-$i", $message);
00366       }
00367       elseif (sizeof($subject_results) < 1) {
00368         form_set_error("subject_id-$i", t("There are no features matching your input. Please check your input for typos and/or lookup the <a href='!url'>here</a>", array('!url' => url('features'))));
00369       }
00370       elseif (sizeof($subject_results) == 1) {
00371         $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id;
00372       }
00373 
00374       // check valid stock selected for object
00375       $criteria = array('unknown' => array('value' => $form_state['values']["object_id-$i"],
00376                                           'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
00377       $object_results = get_chado_stocks($criteria, 'ANY' , $_SESSION['organism']);
00378       if (sizeof($object_results) > 1) {
00379         $links= array();
00380         for ($j=0; $j<sizeof($object_results); $j++) {
00381         $links[] = l($j+1, "node/" . $object_results[$j]->nid); }
00382         $message = "Too many stocks match '" . $form_state['values']["object_id-$i"] . "'! "
00383                  . "Please refine your input to match ONLY ONE stock. <br />"
00384                  . "To aid in this process, here are the stocks that match your initial input: "
00385                  . join(', ', $links);
00386         form_set_error("object_id-$i", $message);
00387       }
00388       elseif (sizeof($object_results) < 1) {
00389         form_set_error("object_id-$i", t("There are no features matching your input. Please check your input for typos and/or lookup the <a href='!url'>here</a>", array('!url' => url('features'))));
00390       }
00391       elseif (sizeof($object_results) == 1) {
00392         $form_state['values']["object_id-$i"] = $object_results[0]->stock_id;
00393       }
00394 
00395       // check valid type selected
00396       if ($form_state['values']["type_id-$i"] == 0) {
00397         form_set_error('type_id', 'Please select a type of relationship.');
00398       }
00399       else {
00400         $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d" , $form_state['values']["type_id-$i"]));
00401 
00402         if ($tmp_obj->count != 1) {
00403           form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
00404         }
00405       }
00406 
00407       // check either subject or object is the current stock
00408       if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
00409         if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
00410           form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
00411         }
00412       }
00413 
00414     } // end of for each relationship
00415   } //end of if updating relationships
00416 
00417 }
00418 
00424 function tripal_feature_edit_ALL_relationships_form_submit($form, &$form_state) {
00425 
00426   if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
00427     //Update all
00428     for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
00429 
00430       //process stock textfields
00431       tripal_feature_update_relationship(
00432         $form_state['values']["id-$i"],
00433         $form_state['values']["subject_id-$i"],
00434         $form_state['values']["type_id-$i"],
00435         $form_state['values']["object_id-$i"]
00436       );
00437     }
00438     drupal_set_message(t("Updated all Relationships"));
00439     drupal_goto('node/' . $form_state['values']['nid']);
00440 
00441   }
00442   elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
00443 
00444     $i = $matches[1];
00445     tripal_feature_delete_relationship($form_state['values']["id-$i"]);
00446     drupal_set_message(t("Deleted Relationship"));
00447 
00448   }
00449   elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
00450     drupal_goto('node/' . $form_state['values']['nid']);
00451   }
00452   else {
00453     drupal_set_message(t("Unrecognized Button Pressed"), 'error');
00454   }
00455 
00456 }
00457 
00463 function tripal_feature_update_relationship($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
00464 
00465   $previous_db = db_set_active('chado');
00466   db_query(
00467     "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
00468     $subject_id,
00469     $cvterm_id,
00470     $object_id,
00471     $stock_relationship_id
00472   );
00473   db_set_active($previous_db);
00474 
00475 }
00476 
00482 function tripal_feature_delete_relationship($stock_relationship_id) {
00483 
00484   $previous_db = db_set_active('chado');
00485   db_query(
00486     "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
00487     $stock_relationship_id
00488   );
00489   db_set_active($previous_db);
00490 
00491 }
00492 
00498 function theme_tripal_feature_edit_ALL_relationships_form($form) {
00499   $output = '';
00500 
00501   $output .= '<br /><fieldset>';
00502   $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
00503   $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
00504         .'the subject and object of the relationship can contain the uniquename, name, database '
00505       .'reference or synonym of a stock of the same organism.</p>';
00506   $output .= '<table>';
00507   $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
00508 
00509   for ($i=1; $i <= $form['num_relationships']['#value']; $i++) {
00510     $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td>' .
00511               '<td>' . drupal_render($form["subject_id-$i"]) . '</td>' .
00512               '<td>' . drupal_render($form["type_id-$i"]) . '</td>' .
00513                 '<td>' . drupal_render($form["object_id-$i"]) . '</td>' .
00514               '<td>' . drupal_render($form["submit-$i"]) . '</td></tr>';
00515   }
00516 
00517   $output .= '</table><br />';
00518   $output .= drupal_render($form);
00519   $output .= '</fieldset>';
00520 
00521   return $output;
00522 }
00523 
00529 function tripal_feature_list_relationships_for_node($feature_name, $subject_relationships, $object_relationships) {
00530 
00531   if (!empty($subject_relationships) OR !empty($object_relationships) ) {
00532     $output = '<table>';
00533     $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
00534 
00535     if (!empty($subject_relationships) ) {
00536       foreach ($subject_relationships as $s) {
00537         $output .= '<tr><td>' . $s->subject_name . '</td><td>' . $s->relationship_type . '</td><td>' . $feature_name . '</td></tr>';
00538       }
00539     }
00540 
00541     if (!empty($object_relationships) ) {
00542       foreach ($object_relationships as $o) {
00543         $output .= '<tr><td>' . $feature_name . '</td><td>' . $o->relationship_type . '</td><td>' . $o->object_name . '</td></tr>';
00544       } // end of foreach property
00545     }
00546 
00547     $output .= '</table>';
00548   }
00549   else {
00550     $output = 'No Relationships For the Current Feature';
00551   }
00552 
00553   return $output;
00554 
00555 }
 All Classes Files Functions Variables