tripal_feature-relationships.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /*************************************************************************************************************
  3. * @section
  4. * Deals with Adding Relationships
  5. */
  6. function tripal_feature_add_ALL_relationships_page($node) {
  7. $output = '';
  8. $output .= tripal_feature_implement_add_chado_properties_progress('relationships').'<br>';
  9. $output .= '<b>All Relationships should include the CURRENT Individual ('.$node->uniquename.')</b><br>';
  10. $output .= '<br><b>Current Relationships</b><br>';
  11. $output .= list_relationships_for_node($node->uniquename, $node->subject_relationships, $node->object_relationships);
  12. $output .= '<br><br>';
  13. $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
  14. $output .= '<br>';
  15. $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'relationships', $node->nid);
  16. return $output;
  17. }
  18. /*************************************************************************
  19. * Implements Hook_form()
  20. * Handles adding of Relationships to Features
  21. */
  22. function tripal_feature_add_ONE_relationship_form($form_state, $node) {
  23. $feature_id = $node->feature_id;
  24. $organism_id = $node->organism->organism_id;
  25. $_SESSION['organism'] = $organism_id; //needed for autocomplete enter feature to work
  26. $form['rel_nid'] = array(
  27. '#type' => 'hidden',
  28. '#value' => $node->nid
  29. );
  30. $form['add_relationships'] = array(
  31. '#type' => 'fieldset',
  32. '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  33. );
  34. $form['add_relationships']['description'] = array(
  35. '#type' => 'item',
  36. '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, X part_of Y, where X & Y are genomic features.')
  37. );
  38. $form['add_relationships']['subject_id'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Subject'),
  41. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
  42. );
  43. $cv = tripal_cv_get_cv_by_name('relationship');
  44. $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
  45. $type_options[0] = 'Select a Type';
  46. ksort($type_options);
  47. $form['add_relationships']['type_id'] = array(
  48. '#type' => 'select',
  49. '#title' => t('Type of Relationship'),
  50. '#options' => $type_options
  51. );
  52. $form['add_relationships']['object_id'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('Object'),
  55. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
  56. );
  57. $form['add_relationships']['r_description'] = array(
  58. '#type' => 'textarea',
  59. '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>',
  60. '#description' => t('Should not include Genotypes and Phenotypes'),
  61. );
  62. $form['add_relationships']['submit'] = array(
  63. '#type' => 'submit',
  64. '#value' => t('Add a Relationship')
  65. );
  66. $form['add_relationships']['r_feature_id'] = array(
  67. '#type' => 'value',
  68. '#value' => $feature_id,
  69. '#required' => TRUE
  70. );
  71. $form['add_relationships']['r_feature_uniquename'] = array(
  72. '#type' => 'value',
  73. '#value' => $node->uniquename,
  74. '#required' => TRUE
  75. );
  76. return $form;
  77. }
  78. /*************************************************************************
  79. *
  80. */
  81. function tripal_feature_add_ONE_relationship_form_validate($form, &$form_state) {
  82. //Require Validation if adding
  83. if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
  84. // check valid feature selected for subject
  85. $criteria = array('unknown' => array('value'=> $form_state['values']['subject_id'],
  86. 'columns'=>array('name','uniquename','accession','synonym') ));
  87. $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  88. if (sizeof($subject_results) > 1) {
  89. $links= array();
  90. for ($i=0; $i<sizeof($subject_results); $i++) { $links[] = l($i+1, "node/".$subject_results[$i]->nid); }
  91. $message = "Too many stocks match '".$form_state['values']['subject_id']."'! "
  92. . " Please refine your input to match ONLY ONE stock. <br>"
  93. . "To aid in this process, here are the stocks that match your initial input: "
  94. .join(', ',$links);
  95. form_set_error('subject_id', $message);
  96. } elseif (sizeof($subject_results) < 1) {
  97. form_set_error('subject_id', "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks'));
  98. } elseif (sizeof($subject_results) == 1) {
  99. $form_state['values']['subject_id'] = $subject_results[0]->stock_id;
  100. }
  101. // check valid stock selected for object
  102. $criteria = array('unknown' => array('value'=> $form_state['values']['object_id'],
  103. 'columns'=>array('name','uniquename','accession','synonym') ));
  104. $object_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  105. if (sizeof($object_results) > 1) {
  106. $links= array();
  107. for ($i=0; $i<sizeof($object_results); $i++) { $links[] = l($i+1, "node/".$object_results[$i]->nid); }
  108. $message = "Too many stocks match '".$form_state['values']['object_id']."'! "
  109. . "Please refine your input to match ONLY ONE stock. <br>"
  110. . "To aid in this process, here are the stocks that match your initial input: "
  111. .join(', ',$links);
  112. form_set_error('object_id', $message);
  113. } elseif (sizeof($object_results) < 1) {
  114. form_set_error('object_id', "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks'));
  115. } elseif (sizeof($object_results) == 1) {
  116. $form_state['values']['object_id'] = $object_results[0]->stock_id;
  117. }
  118. // check valid type selected
  119. if ($form_state['values']['type_id'] == 0) {
  120. form_set_error('type_id', 'Please select a type of relationship.');
  121. } else {
  122. $previous_db = tripal_db_set_active('chado');
  123. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']['type_id']));
  124. tripal_db_set_active($previous_db);
  125. if ($tmp_obj->count != 1) {
  126. form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
  127. }
  128. }
  129. // check either subject or object is the current stock
  130. if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
  131. if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
  132. form_set_error('subject_id', 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  133. }
  134. }
  135. } //end of require validation if adding relationship
  136. }
  137. /*************************************************************************
  138. *
  139. */
  140. function tripal_feature_add_ONE_relationship_form_submit($form, &$form_state) {
  141. if ($form_state['values']['subject_id'] > 0) {
  142. $previous_db = db_set_active('chado');
  143. db_query(
  144. "INSERT INTO stock_relationship (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
  145. $form_state['values']['subject_id'],
  146. $form_state['values']['type_id'],
  147. $form_state['values']['object_id'],
  148. $form_state['values']['r_description']
  149. );
  150. db_set_active($previous_db);
  151. drupal_set_message('Successfully Added Relationship.');
  152. } //end of insert relationship
  153. }
  154. /*************************************************************************************************************
  155. * @section
  156. * Deals with Editing and Deleting Properties
  157. */
  158. function tripal_feature_edit_ALL_relationships_page($node) {
  159. $output = '';
  160. $output .= drupal_get_form('tripal_feature_edit_ALL_relationships_form', $node);
  161. $output .= '<br>';
  162. $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
  163. $output .= '<br>';
  164. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  165. return $output;
  166. }
  167. /*************************************************************************
  168. * Implements Hook_form()
  169. * Handles adding of Properties & Synonyms to Stocks
  170. */
  171. function tripal_feature_edit_ALL_relationships_form($form_state, $node) {
  172. $form = array();
  173. $form['nid'] = array(
  174. '#type' => 'hidden',
  175. '#value' => $node->nid
  176. );
  177. $form['r_feature_uniquename'] = array(
  178. '#type' => 'hidden',
  179. '#value' => $node->uniquename
  180. );
  181. $i=0;
  182. $feature = $node->feature;
  183. $orelationships = tripal_feature_load_relationships ($feature->feature_id,'as_object');
  184. $srelationships = tripal_feature_load_relationships ($feature->feature_id,'as_subject');
  185. $relationships = array_merge($orelationships,$srelationships);
  186. if (sizeof($relationships) != 0) {
  187. foreach ($relationships as $r) {
  188. $i++;
  189. $form["num-$i"] = array(
  190. '#type' => 'fieldset',
  191. '#title' => "Relationship $i",
  192. );
  193. $form["num-$i"]["id-$i"] = array(
  194. '#type' => 'hidden',
  195. '#value' => $r->stock_relationship_id
  196. );
  197. //Enter relationship specific fields
  198. if ( !empty($r->subject_id) ) {
  199. $default = $r->subject_uniquename;
  200. $description = l($r->subject_name, 'node/'.$r->subject_nid);
  201. } else {
  202. $default = $node->uniquename;
  203. $description = "Current Feature";
  204. }
  205. $description .= " (".$r->subject_type.")";
  206. $form["num-$i"]["subject_id-$i"] = array(
  207. '#type' => 'textfield',
  208. //'#title' => t('Subject'),
  209. '#required' => TRUE,
  210. '#size' => 30,
  211. '#default_value' => $default,
  212. '#description' => $description,
  213. );
  214. $cv = tripal_cv_get_cv_by_name('relationship');
  215. $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
  216. ksort($type_options);
  217. $form["num-$i"]["type_id-$i"] = array(
  218. '#type' => 'select',
  219. //'#title' => t('Type of Relationship'),
  220. '#options' => $type_options,
  221. '#required' => TRUE,
  222. '#default_value' => $r->relationship_type_id
  223. );
  224. if (!empty($r->object_id) ) {
  225. $default = $r->object_uniquename;
  226. $description = l($r->object_name, 'node/'.$r->object_nid);
  227. } else {
  228. $default = $node->uniquename;
  229. $description = 'Current Feature';
  230. }
  231. $description .= " (".$r->object_type.")";
  232. $form["num-$i"]["object_id-$i"] = array(
  233. '#type' => 'textfield',
  234. //'#title' => t('Object'),
  235. '#required' => TRUE,
  236. '#size' => 30,
  237. '#default_value' => $default,
  238. '#description' => $description
  239. );
  240. $form["num-$i"]["delete-$i"] = array(
  241. '#type' => 'submit',
  242. '#value' => t("Delete"),
  243. '#name' => "delete-$i",
  244. );
  245. } //end of foreach relationship
  246. $form['num_relationships'] = array(
  247. '#type' => 'hidden',
  248. '#value' => $i
  249. );
  250. $form["submit-edits"] = array(
  251. '#type' => 'submit',
  252. '#value' => t('Update All Relationships')
  253. );
  254. } else {
  255. $form["info"] = array(
  256. '#type' => 'markup',
  257. '#value' => t('No relationships currently exist for this feature.')
  258. );
  259. }
  260. return $form;
  261. }
  262. /*************************************************************************
  263. *
  264. */
  265. function tripal_feature_edit_ALL_relationships_form_validate($form, &$form_state) {
  266. // Only Require if Updating Relationships
  267. if ($form_state['clicked_button']['#value'] == t('Update All Relationships') ) {
  268. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  269. // check valid stock selected for subject
  270. $criteria = array('unknown' => array('value'=>$form_state['values']["subject_id-$i"],
  271. 'columns'=>array('name','uniquename','accession','synonym') ));
  272. $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  273. if (sizeof($subject_results) > 1) {
  274. $links= array();
  275. for ($j=0; $j<sizeof($subject_results); $j++) { $links[] = l($j+1, "node/".$subject_results[$j]->nid); }
  276. $message = "Too many stocks match '".$form_state['values']["subject_id-$i"]."'! "
  277. . "Please refine your input to match ONLY ONE stock. <br>"
  278. . "To aid in this process, here are the stocks that match your initial input: "
  279. .join(', ',$links);
  280. form_set_error("subject_id-$i", $message);
  281. } elseif (sizeof($subject_results) < 1) {
  282. form_set_error("subject_id-$i", "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks'));
  283. } elseif (sizeof($subject_results) == 1) {
  284. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id;
  285. }
  286. // check valid stock selected for object
  287. $criteria = array('unknown' => array('value'=> $form_state['values']["object_id-$i"],
  288. 'columns'=>array('name','uniquename','accession','synonym') ));
  289. $object_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  290. if (sizeof($object_results) > 1) {
  291. $links= array();
  292. for ($j=0; $j<sizeof($object_results); $j++) { $links[] = l($j+1, "node/".$object_results[$j]->nid); }
  293. $message = "Too many stocks match '".$form_state['values']["object_id-$i"]."'! "
  294. . "Please refine your input to match ONLY ONE stock. <br>"
  295. . "To aid in this process, here are the stocks that match your initial input: "
  296. .join(', ',$links);
  297. form_set_error("object_id-$i", $message);
  298. } elseif (sizeof($object_results) < 1) {
  299. form_set_error("object_id-$i", "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks'));
  300. } elseif (sizeof($object_results) == 1) {
  301. $form_state['values']["object_id-$i"] = $object_results[0]->stock_id;
  302. }
  303. // check valid type selected
  304. if ($form_state['values']["type_id-$i"] == 0) {
  305. form_set_error('type_id', 'Please select a type of relationship.');
  306. } else {
  307. $previous_db = tripal_db_set_active('chado');
  308. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']["type_id-$i"]));
  309. tripal_db_set_active($previous_db);
  310. if ($tmp_obj->count != 1) {
  311. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  312. }
  313. }
  314. // check either subject or object is the current stock
  315. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  316. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  317. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  318. }
  319. }
  320. } // end of for each relationship
  321. } //end of if updating relationships
  322. }
  323. /*************************************************************************
  324. *
  325. */
  326. function tripal_feature_edit_ALL_relationships_form_submit($form, &$form_state) {
  327. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  328. //Update all
  329. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  330. //process stock textfields
  331. tripal_feature_update_relationship(
  332. $form_state['values']["id-$i"],
  333. $form_state['values']["subject_id-$i"],
  334. $form_state['values']["type_id-$i"],
  335. $form_state['values']["object_id-$i"]
  336. );
  337. }
  338. drupal_set_message("Updated all Relationships");
  339. drupal_goto('node/'.$form_state['values']['nid']);
  340. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  341. $i = $matches[1];
  342. tripal_feature_delete_relationship($form_state['values']["id-$i"]);
  343. drupal_set_message("Deleted Relationship");
  344. } elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  345. drupal_goto('node/'.$form_state['values']['nid']);
  346. } else {
  347. drupal_set_message("Unrecognized Button Pressed",'error');
  348. }
  349. }
  350. /*************************************************************************
  351. *
  352. */
  353. function tripal_feature_update_relationship ($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  354. $previous_db = db_set_active('chado');
  355. db_query(
  356. "UPDATE stock_relationship SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  357. $subject_id,
  358. $cvterm_id,
  359. $object_id,
  360. $stock_relationship_id
  361. );
  362. db_set_active($previous_db);
  363. }
  364. /*************************************************************************
  365. *
  366. */
  367. function tripal_feature_delete_relationship ($stock_relationship_id) {
  368. $previous_db = db_set_active('chado');
  369. db_query(
  370. "DELETE FROM stock_relationship WHERE stock_relationship_id=%d",
  371. $stock_relationship_id
  372. );
  373. db_set_active($previous_db);
  374. }
  375. /*************************************************************************
  376. *
  377. */
  378. function theme_tripal_feature_edit_ALL_relationships_form ($form) {
  379. $output = '';
  380. $output .= '<br><fieldset>';
  381. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  382. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  383. .'the subject and object of the relationship can contain the uniquename, name, database '
  384. .'reference or synonym of a stock of the same organism.</p>';
  385. $output .= '<table>';
  386. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  387. for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
  388. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td>'.
  389. '<td>'.drupal_render($form["subject_id-$i"]).'</td>'.
  390. '<td>'.drupal_render($form["type_id-$i"]).'</td>'.
  391. '<td>'.drupal_render($form["object_id-$i"]).'</td>'.
  392. '<td>'.drupal_render($form["submit-$i"]).'</td></tr>';
  393. }
  394. $output .= '</table><br>';
  395. $output .= drupal_render($form);
  396. $output .= '</fieldset>';
  397. return $output;
  398. }
  399. /*************************************************************************************************************
  400. * @section
  401. * Supplementary Functions
  402. */
  403. /**
  404. *
  405. */
  406. function tripal_feature_list_relationships_for_node($feature_name, $subject_relationships, $object_relationships) {
  407. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  408. $output = '<table>';
  409. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  410. if (!empty($subject_relationships) ) {
  411. foreach ($subject_relationships as $s) {
  412. $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$feature_name.'</td></tr>';
  413. }
  414. }
  415. if (!empty($object_relationships) ) {
  416. foreach ($object_relationships as $o) {
  417. $output .= '<tr><td>'.$feature_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
  418. } // end of foreach property
  419. }
  420. $output .= '</table>';
  421. } else {
  422. $output = 'No Relationships For the Current Feature';
  423. }
  424. return $output;
  425. }