tripal_cv.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. require_once "charts.php";
  3. require_once "trees.php";
  4. require_once "obo_loader.php";
  5. //
  6. // Copyright 2009 Clemson University
  7. //
  8. /*************************************************************************
  9. *
  10. */
  11. function tripal_cv_init(){
  12. // add the tripal_cv JS and CSS
  13. drupal_add_css(drupal_get_path('theme', 'tripal').
  14. '/css/tripal_cv.css');
  15. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_cv.js');
  16. // add the jsTree JS and CSS
  17. drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
  18. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
  19. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
  20. }
  21. /*************************************************************************
  22. *
  23. */
  24. function tripal_cv_menu() {
  25. $items = array();
  26. $items['admin/tripal/tripal_cv/cvtermpath'] = array(
  27. 'title' => 'Update Chado cvtermpath tables',
  28. 'description' => 'The Chado cvtermpath table provides lineage for terms and is useful for quickly finding any ancestor parent of a term. However, this table must be populated. This page allows for populating of this table one vocabulary at a time',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('tripal_cv_cvtermpath_form'),
  31. 'access arguments' => array('administer site configuration'),
  32. 'type' => MENU_NORMAL_ITEM,
  33. );
  34. $items['admin/tripal/tripal_cv'] = array(
  35. 'title' => 'Controlled Vocabulary Management',
  36. 'description' => 'Manage controlled vocabularies/ontolgoies in Chado ',
  37. 'page callback' => 'tripal_cv_admin_page',
  38. 'access arguments' => array('administer site configuration'),
  39. 'type' => MENU_NORMAL_ITEM,
  40. );
  41. $items['admin/tripal/tripal_cv/new'] = array(
  42. 'title' => 'Add a Controlled Vocabulary',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('tripal_cv_add_form'),
  45. 'access arguments' => array('access administration pages'),
  46. 'type' => MENU_NORMAL_ITEM,
  47. );
  48. $items['admin/tripal/tripal_cv/obo'] = array(
  49. 'title' =>'Upload an OBO File',
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('tripal_cv_obo_form'),
  52. 'access arguments' => array('access administration pages'),
  53. 'type' => MENU_NORMAL_ITEM,
  54. );
  55. $items['admin/tripal/tripal_cv/edit/js'] = array(
  56. 'title' => 'Edit Controlled Vocabularies',
  57. 'page callback' => 'tripal_ajax_cv_edit',
  58. 'access arguments' => array('access administration pages'),
  59. 'type' => MENU_CALLBACK,
  60. );
  61. $items['tripal_cv_chart'] = array(
  62. 'path' => 'tripal_cv_chart',
  63. 'title' => t('CV Chart'),
  64. 'page callback' => 'tripal_cv_chart',
  65. 'page arguments' => array(1),
  66. 'access arguments' => array('access content'),
  67. 'type' => MENU_CALLBACK
  68. );
  69. $items['tripal_cv_tree'] = array(
  70. 'path' => 'tripal_cv_tree',
  71. 'title' => t('CV Term Viewer'),
  72. 'page callback' => 'tripal_cv_tree',
  73. 'page arguments' => array(1),
  74. 'access arguments' => array('access content'),
  75. 'type' => MENU_CALLBACK
  76. );
  77. // menu items for working with the CV module tree browser
  78. $items['cv_browser'] = array(
  79. 'title' => t('CV Relationship Browser'),
  80. 'page callback' => 'tripal_cv_show_browser',
  81. 'access arguments' => array('access chado_cv content'),
  82. 'type' => MENU_CALLBACK
  83. );
  84. $items['tripal_cv_init_browser'] = array(
  85. 'path' => 'tripal_cv_init_browser',
  86. 'title' => t('CV Browser'),
  87. 'page callback' => 'tripal_cv_init_browser',
  88. 'page arguments' => array(1),
  89. 'access arguments' => array('access content'),
  90. 'type' => MENU_CALLBACK
  91. );
  92. // menu item for interaction with the tree
  93. $items['tripal_cv_update_tree'] = array(
  94. 'path' => 'tripal_cv_update_tree',
  95. 'title' => t('CV Tree'),
  96. 'page callback' => 'tripal_cv_update_tree',
  97. 'page arguments' => array(2,3),
  98. 'access arguments' => array('access content'),
  99. 'type' => MENU_CALLBACK
  100. );
  101. // menu items for working with terms
  102. $items['tripal_cv_cvterm_info'] = array(
  103. 'path' => 'tripal_cv_cvterm_info',
  104. 'title' => t('CV Term Viewer'),
  105. 'page callback' => 'tripal_cv_cvterm_info',
  106. 'page arguments' => array(1),
  107. 'access arguments' => array('access content'),
  108. 'type' => MENU_CALLBACK
  109. );
  110. $items['tripal_cv_cvterm_edit'] = array(
  111. 'path' => 'tripal_cv_edit',
  112. 'title' => t('CV Term Editor'),
  113. 'page callback' => 'tripal_cv_cvterm_edit',
  114. 'page arguments' => array(1),
  115. 'access arguments' => array('edit chado_cv content'),
  116. 'type' => MENU_CALLBACK
  117. );
  118. return $items;
  119. }
  120. /*******************************************************************************
  121. * The following function proves access control for users trying to
  122. * perform actions on data managed by this module
  123. */
  124. function chado_cv_access($op, $node, $account){
  125. if ($op == 'create') {
  126. return user_access('create chado_cv content', $account);
  127. }
  128. if ($op == 'update') {
  129. if (user_access('edit chado_cv content', $account)) {
  130. return TRUE;
  131. }
  132. }
  133. if ($op == 'delete') {
  134. if (user_access('delete chado_cv content', $account)) {
  135. return TRUE;
  136. }
  137. }
  138. if ($op == 'view') {
  139. if (user_access('access chado_cv content', $account)) {
  140. return TRUE;
  141. }
  142. }
  143. return FALSE;
  144. }
  145. /*******************************************************************************
  146. * Set the permission types that the chado module uses. Essentially we
  147. * want permissionis that protect creation, editing and deleting of chado
  148. * data objects
  149. */
  150. function tripal_cv_perm(){
  151. return array(
  152. 'access chado_cv content',
  153. 'create chado_cv content',
  154. 'delete chado_cv content',
  155. 'edit chado_cv content',
  156. );
  157. }
  158. /*************************************************************************
  159. *
  160. */
  161. function tripal_cv_admin_page(){
  162. $add_url = url("admin/tripal/tripal_cv/new");
  163. $obo_url = url("admin/tripal/tripal_cv/obo");
  164. $cvtermpath_url = url("admin/tripal/tripal_cv/cvtermpath");
  165. $browser_url = url("cv_browser");
  166. $output = "<a href=\"$add_url\">Add a new controlled vocabulary</a> | ";
  167. $output = "<a href=\"$obo_url\">Upload an OBO file</a> | ";
  168. $output .= "<a href=\"$cvtermpath_url\">Update the cvtermpath table</a> | ";
  169. $output .= "<a href=\"$browser_url\">Browse a vocabulary</a> ";
  170. $output .= drupal_get_form('tripal_cv_select_form');
  171. $output .= '<div id="db-edit-div">Please select a vocabulary above to view or edit</div>';
  172. return $output;
  173. }
  174. /*************************************************************************
  175. *
  176. */
  177. function tripal_cv_select_form(){
  178. $previous_db = db_set_active('chado'); // use chado database
  179. // get a list of db from chado for user to choose
  180. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  181. $results = db_query ($sql);
  182. db_set_active($previous_db); // use drupal database
  183. $cvs = array();
  184. $cvs[] = '';
  185. while ($cv = db_fetch_object($results)){
  186. $cvs[$cv->cv_id] = $cv->name;
  187. }
  188. $form['cvid'] = array(
  189. '#title' => t('Controlled Vocabulary/Ontology Name'),
  190. '#type' => 'select',
  191. '#options' => $cvs,
  192. '#ahah' => array(
  193. 'path' => 'admin/tripal/tripal_cv/edit/js',
  194. 'wrapper' => 'db-edit-div',
  195. 'effect' => 'fade',
  196. 'event' => 'change',
  197. 'method' => 'replace',
  198. ),
  199. );
  200. return $form;
  201. }
  202. /*************************************************************************
  203. *
  204. */
  205. function tripal_ajax_cv_edit (){
  206. // get the database id, build the form and then return the JSON object
  207. $cvid = $_POST['cvid'];
  208. $form = drupal_get_form('tripal_cv_edit_form',$cvid);
  209. drupal_json(array('status' => TRUE, 'data' => $form));
  210. }
  211. /*************************************************************************
  212. *
  213. */
  214. function tripal_cv_add_form(&$form_state = NULL){
  215. $form['cvid'] = array(
  216. '#type' => 'hidden',
  217. '#value' => $cvid
  218. );
  219. $form['name']= array(
  220. '#type' => 'textfield',
  221. '#title' => t("Controlled Vocabulary name"),
  222. '#description' => t('Please enter the name for this vocabulary. This field will be ignored if an OBO file or URL is provided above'),
  223. '#required' => FALSE,
  224. '#default_value' => $default_cv,
  225. '#weight' => 1
  226. );
  227. $form['definition']= array(
  228. '#type' => 'textarea',
  229. '#title' => t('Description'),
  230. '#description' => t('Please enter a description for this vocabulary'),
  231. '#default_value' => $default_desc,
  232. '#weight' => 2
  233. );
  234. $form['add'] = array (
  235. '#type' => 'submit',
  236. '#value' => t('Add'),
  237. '#weight' => 5,
  238. '#executes_submit_callback' => TRUE,
  239. );
  240. $form['#redirect'] = 'admin/tripal/tripal_cv';
  241. return $form;
  242. }
  243. /*************************************************************************
  244. *
  245. */
  246. function tripal_cv_obo_form(&$form_state = NULL){
  247. $form['obo_url']= array(
  248. '#type' => 'textfield',
  249. '#title' => t('Remote OBO v1.2 Definition File'),
  250. '#description' => t('Please enter a URL the points to online OBO file. The file will be downloaded and parsed.
  251. All necessary information for naming of the conrolled vocabulary is contained in the file.
  252. If the specification file contains more than one vocabulary then each will be created.'),
  253. '#default_value' => $default_desc,
  254. '#weight' => -1
  255. );
  256. $form['obo_file']= array(
  257. '#type' => 'textfield',
  258. '#title' => t('Local OBO v1.2 Definition File'),
  259. '#description' => t('Please enter the full path for an OBO definition file. The path must be accessible to the
  260. server on which this Drupal instance is running. All necessary information for naming of the
  261. controlled vocabulary is contained in this file. If the specification file contains more than
  262. one vocabulary then each will be created.'),
  263. '#default_value' => $default_desc,
  264. '#weight' => 0
  265. );
  266. $form['upload'] = array (
  267. '#type' => 'submit',
  268. '#value' => t('Upload'),
  269. '#weight' => 5,
  270. '#executes_submit_callback' => TRUE,
  271. );
  272. $form['#redirect'] = 'admin/tripal/tripal_cv';
  273. return $form;
  274. }
  275. /*************************************************************************
  276. *
  277. */
  278. function tripal_cv_edit_form(&$form_state = NULL,$cvid = NULL){
  279. $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
  280. $previous_db = db_set_active('chado');
  281. $cv = db_fetch_object(db_query($sql,$cvid));
  282. db_set_active($previous_db);
  283. # set the default values. If there is a value set in the
  284. # form_state then let's use that, otherwise, we'll pull
  285. # the values from the database
  286. $default_db = $form_state['values']['name'];
  287. $default_desc = $form_state['values']['description'];
  288. $default_url = $form_state['values']['url'];
  289. $default_urlprefix = $form_state['values']['urlprefix'];
  290. if(!$default_db){
  291. $default_cv = $cv->name;
  292. }
  293. if(!$default_desc){
  294. $default_desc = $cv->definition;
  295. }
  296. $form['cvid'] = array(
  297. '#type' => 'hidden',
  298. '#value' => $cvid
  299. );
  300. $form['name']= array(
  301. '#type' => 'textfield',
  302. '#title' => t("Controlled Vocabulary name"),
  303. '#description' => t('Please enter the name for this vocabulary.'),
  304. '#required' => FALSE,
  305. '#default_value' => $default_cv,
  306. '#weight' => 1
  307. );
  308. $form['definition']= array(
  309. '#type' => 'textarea',
  310. '#title' => t('Description'),
  311. '#description' => t('Please enter a description for this vocabulary'),
  312. '#default_value' => $default_desc,
  313. '#weight' => 2
  314. );
  315. $form['update'] = array (
  316. '#type' => 'submit',
  317. '#value' => t('Update'),
  318. '#weight' => 5,
  319. '#executes_submit_callback' => TRUE,
  320. );
  321. $form['delete'] = array (
  322. '#type' => 'submit',
  323. '#value' => t('Delete'),
  324. '#weight' => 6,
  325. '#executes_submit_callback' => TRUE,
  326. );
  327. $form['#redirect'] = 'admin/tripal/tripal_cv';
  328. return $form;
  329. }
  330. /************************************************************************
  331. *
  332. */
  333. function tripal_cv_edit_form_submit($form, &$form_state){
  334. $name = $form_state['values']['name'];
  335. $desc = $form_state['values']['definition'];
  336. $cvid = $form_state['values']['cvid'];
  337. $op = $form_state['values']['op'];
  338. if(strcmp($op,'Update')==0){
  339. $sql = "
  340. UPDATE {cv} SET
  341. name = '%s',
  342. definition = '%s'
  343. WHERE cv_id = %d
  344. ";
  345. $previous_db = db_set_active('chado');
  346. $db = db_query($sql,$name,$desc,$cvid);
  347. db_set_active($previous_db);
  348. if($db){
  349. drupal_set_message("Controlled vocabulary updated");
  350. } else {
  351. drupal_set_message("Failed to update controlled vocabulary.");
  352. }
  353. }
  354. if(strcmp($op,'Delete')==0){
  355. $sql = "
  356. DELETE FROM {cv}
  357. WHERE cv_id = %d
  358. ";
  359. $previous_db = db_set_active('chado');
  360. $db = db_query($sql,$cvid);
  361. db_set_active($previous_db);
  362. if($db){
  363. drupal_set_message("Controlled vocabulary deleted");
  364. } else {
  365. drupal_set_message("Failed to delete controlled vocabulary.");
  366. }
  367. }
  368. return '';
  369. }
  370. /************************************************************************
  371. *
  372. */
  373. function tripal_cv_add_form_submit($form, &$form_state){
  374. $name = $form_state['values']['name'];
  375. $desc = $form_state['values']['definition'];
  376. $sql = "
  377. INSERT INTO {cv}
  378. (name,definition)
  379. VALUES
  380. ('%s','%s')
  381. ";
  382. $previous_db = db_set_active('chado');
  383. $db = db_query($sql,$name,$desc);
  384. db_set_active($previous_db);
  385. if($db){
  386. drupal_set_message("Controlled vocabulary added");
  387. } else {
  388. drupal_set_message("Failed to add controlled vocabulary.");
  389. }
  390. return '';
  391. }
  392. /************************************************************************
  393. *
  394. */
  395. function tripal_cv_obo_form_submit($form, &$form_state){
  396. global $user;
  397. $obo_url = $form_state['values']['obo_url'];
  398. $obo_file = $form_state['values']['obo_file'];
  399. if($obo_url){
  400. }
  401. elseif($obo_file){
  402. $args = array($obo_file);
  403. tripal_add_job("Load OBO $file",'tripal_cv',
  404. "tripal_cv_load_obo_v1_2",$args,$user->uid);
  405. }
  406. return '';
  407. }
  408. /*************************************************************************
  409. *
  410. */
  411. function tripal_cv_cvtermpath_form () {
  412. $previous_db = db_set_active('chado'); // use chado database
  413. // get a list of db from chado for user to choose
  414. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  415. $results = db_query ($sql);
  416. db_set_active($previous_db); // use drupal database
  417. $cvs = array();
  418. $cvs[] = '';
  419. while ($cv = db_fetch_object($results)){
  420. $cvs[$cv->cv_id] = $cv->name;
  421. }
  422. $form['cvid'] = array(
  423. '#title' => t('Controlled Vocabulary/Ontology Name'),
  424. '#type' => 'select',
  425. '#options' => $cvs,
  426. '#description' => t('Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
  427. );
  428. $form['description'] = array(
  429. '#type' => 'item',
  430. '#value' => t("Submit a job to update chado cvtermpath table."),
  431. '#weight' => 1,
  432. );
  433. $form['button'] = array(
  434. '#type' => 'submit',
  435. '#value' => t('Update cvtermpath'),
  436. '#weight' => 2,
  437. );
  438. return $form;
  439. }
  440. /*************************************************************************
  441. *
  442. */
  443. function tripal_cv_cvtermpath_form_validate($form, &$form_state) {
  444. global $user;
  445. $cvid = $form_state['values']['cvid'];
  446. // first get the controlled vocabulary name:
  447. $previous_db = db_set_active('chado');
  448. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  449. db_set_active($previous_db);
  450. // Submit a job to update cvtermpath
  451. $job_args = array($cvid);
  452. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  453. tripal_add_job("Update cvtermpath: $cv->name",'tripal_cv',
  454. 'tripal_cv_update_cvtermpath',$job_args,$user->uid);
  455. }
  456. }
  457. /***********************************************************
  458. * Update the cvtermpath table
  459. */
  460. function tripal_cv_update_cvtermpath($cvid = NULL, $job_id = NULL) {
  461. // first get the controlled vocabulary name:
  462. $previous_db = db_set_active('chado');
  463. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  464. print "\nUpdating cvtermpath for $cv->name...\n";
  465. // now fill the cvtermpath table
  466. $sql = "SELECT * FROM fill_cvtermpath('%s')";
  467. db_query($sql,$cv->name);
  468. db_set_active($previous_db);
  469. return;
  470. }
  471. /*******************************************************************************
  472. * We need to let drupal know about our theme functions and their arguments.
  473. * We create theme functions to allow users of the module to customize the
  474. * look and feel of the output generated in this module
  475. */
  476. function tripal_cv_theme () {
  477. return array(
  478. 'tripal_cv_cvterm_edit' => array (
  479. 'arguments' => array('cvterm'),
  480. ),
  481. );
  482. }
  483. /*************************************************************************
  484. */
  485. function tripal_cv_get_cv_id($cv_name){
  486. $sql = "
  487. SELECT cv_id FROM {cv} WHERE name = '%s'
  488. ";
  489. $previous_db = db_set_active('chado');
  490. $cv = db_fetch_object(db_query($sql,$cv_name));
  491. db_set_active($previous_db);
  492. return $cv->cv_id;
  493. }
  494. /*************************************************************************
  495. *
  496. */
  497. function tripal_cv_cvterm_edit($cvterm_id){
  498. $sql = "
  499. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname
  500. FROM {CVTerm} CVT
  501. INNER JOIN CV on CVT.cv_id = CV.cv_id
  502. WHERE CVT.cvterm_id = %d
  503. ";
  504. $previous_db = db_set_active('chado');
  505. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  506. db_set_active($previous_db);
  507. return theme('tripal_cv_cvterm_edit',$cvterm);
  508. }
  509. /*************************************************************************
  510. *
  511. */
  512. function theme_tripal_cv_cvterm_edit(&$cvterm){
  513. $output = "
  514. <div id=\"cvterm\">
  515. <table>
  516. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  517. <tr><th>Vocabulary</th><td>$cvterm->cvname</td></tr>
  518. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  519. </table>
  520. </div>
  521. ";
  522. return $output;
  523. }