tripal_library.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /**
  3. * Administrative settings form
  4. *
  5. * @ingroup tripal_library
  6. */
  7. function tripal_library_admin() {
  8. $form = array();
  9. // before proceeding check to see if we have any
  10. // currently processing jobs. If so, we don't want
  11. // to give the opportunity to sync libraries
  12. $active_jobs = FALSE;
  13. if (tripal_get_module_active_jobs('tripal_library')) {
  14. $active_jobs = TRUE;
  15. }
  16. // add the field set for syncing libraries
  17. if (!$active_jobs) {
  18. get_tripal_library_admin_form_sync_set($form);
  19. get_tripal_library_admin_form_reindex_set($form);
  20. get_tripal_library_admin_form_taxonomy_set($form);
  21. get_tripal_library_admin_form_cleanup_set($form);
  22. }
  23. else {
  24. $form['notice'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('Library Management Temporarily Unavailable')
  27. );
  28. $form['notice']['message'] = array(
  29. '#value' => t('Currently, library management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  30. );
  31. }
  32. return system_settings_form($form);
  33. }
  34. /**
  35. *
  36. *
  37. * @ingroup tripal_library
  38. */
  39. function get_tripal_library_admin_form_cleanup_set(&$form) {
  40. $form['cleanup'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => t('Clean Up')
  43. );
  44. $form['cleanup']['description'] = array(
  45. '#type' => 'item',
  46. '#value' => t("With Drupal and chado residing in different databases ".
  47. "it is possible that nodes in Drupal and libraries in Chado become ".
  48. "\"orphaned\". This can occur if an library node in Drupal is ".
  49. "deleted but the corresponding chado library is not and/or vice ".
  50. "versa. Click the button below to resolve these discrepancies."),
  51. '#weight' => 1,
  52. );
  53. $form['cleanup']['button'] = array(
  54. '#type' => 'submit',
  55. '#value' => t('Clean up orphaned libraries'),
  56. '#weight' => 2,
  57. );
  58. }
  59. /**
  60. *
  61. *
  62. * @ingroup tripal_library
  63. */
  64. function get_tripal_library_admin_form_taxonomy_set(&$form) {
  65. $form['taxonify'] = array(
  66. '#type' => 'fieldset',
  67. '#title' => t('Assign Drupal Taxonomy to Library Features')
  68. );
  69. // get the list of libraries
  70. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  71. $lib_rset = chado_query($sql);
  72. // iterate through all of the libraries
  73. $lib_boxes = array();
  74. while ($library = db_fetch_object($lib_rset)) {
  75. $lib_boxes[$library->library_id] = "$library->name";
  76. }
  77. $form['taxonify']['description'] = array(
  78. '#type' => 'item',
  79. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  80. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  81. "for setting taxonomy only for features that belong to the selected libraries below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  82. '#weight' => 1,
  83. );
  84. $form['taxonify']['tx-libraries'] = array(
  85. '#title' => t('Libraries'),
  86. '#type' => t('checkboxes'),
  87. '#description' => t("Check the libraries whose features you want to reset taxonomy. Note: this list contains all libraries, even those that may not be synced."),
  88. '#required' => FALSE,
  89. '#prefix' => '<div id="lib_boxes">',
  90. '#suffix' => '</div>',
  91. '#options' => $lib_boxes,
  92. '#weight' => 2
  93. );
  94. $form['taxonify']['tx-button'] = array(
  95. '#type' => 'submit',
  96. '#value' => t('Set Feature Taxonomy'),
  97. '#weight' => 3
  98. );
  99. }
  100. /**
  101. *
  102. * @ingroup tripal_library
  103. */
  104. function get_tripal_library_admin_form_reindex_set(&$form) {
  105. // define the fieldsets
  106. $form['reindex'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Reindex Library Features')
  109. );
  110. // get the list of libraries
  111. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  112. $lib_rset = chado_query($sql);
  113. // iterate through all of the libraries
  114. $lib_boxes = array();
  115. while ($library = db_fetch_object($lib_rset)) {
  116. $lib_boxes[$library->library_id] = "$library->name";
  117. }
  118. $form['reindex']['description'] = array(
  119. '#type' => 'item',
  120. '#value' => t("This option allows for reindexing of only those features that belong to the selected libraries below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  121. '#weight' => 1,
  122. );
  123. $form['reindex']['re-libraries'] = array(
  124. '#title' => t('Libraries'),
  125. '#type' => t('checkboxes'),
  126. '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
  127. '#required' => FALSE,
  128. '#prefix' => '<div id="lib_boxes">',
  129. '#suffix' => '</div>',
  130. '#options' => $lib_boxes,
  131. '#weight' => 2,
  132. );
  133. $form['reindex']['re-button'] = array(
  134. '#type' => 'submit',
  135. '#value' => t('Reindex Features'),
  136. '#weight' => 3,
  137. );
  138. }
  139. /**
  140. *
  141. * @ingroup tripal_library
  142. */
  143. function get_tripal_library_admin_form_sync_set(&$form) {
  144. // define the fieldsets
  145. $form['sync'] = array(
  146. '#type' => 'fieldset',
  147. '#title' => t('Sync Libraries')
  148. );
  149. // get the list of libraries
  150. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  151. $lib_rset = chado_query($sql);
  152. // if we've added any libraries to the list that can be synced
  153. // then we want to build the form components to allow the user
  154. // to select one or all of them. Otherwise, just present
  155. // a message stating that all libraries are currently synced.
  156. $lib_boxes = array();
  157. $added = 0;
  158. while ($library = db_fetch_object($lib_rset)) {
  159. // check to see if the library is already present as a node in drupal.
  160. // if so, then skip it.
  161. $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
  162. if (!db_fetch_object(db_query($sql, $library->library_id))) {
  163. $lib_boxes[$library->library_id] = "$library->name";
  164. $added++;
  165. }
  166. }
  167. // if we have libraries we need to add to the checkbox then
  168. // build that form element
  169. if ($added > 0) {
  170. $lib_boxes['all'] = "All Libraries";
  171. $form['reindex']['description'] = array(
  172. '#type' => 'item',
  173. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  174. '#weight' => 1,
  175. );
  176. $form['sync']['libraries'] = array(
  177. '#title' => t('Available Libraries'),
  178. '#type' => t('checkboxes'),
  179. '#description' => t("Check the libraries you want to sync. Drupal content will be created for each of the libraries listed above. Select 'All Libraries' to sync all of them."),
  180. '#required' => FALSE,
  181. '#prefix' => '<div id="lib_boxes">',
  182. '#suffix' => '</div>',
  183. '#options' => $lib_boxes,
  184. '#weight' => 2,
  185. );
  186. $form['sync']['button'] = array(
  187. '#type' => 'submit',
  188. '#value' => t('Sync Libraries'),
  189. '#weight' => 3,
  190. );
  191. }
  192. // we don't have any libraries to select from
  193. else {
  194. $form['sync']['value'] = array(
  195. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  196. );
  197. }
  198. }
  199. /**
  200. *
  201. * @ingroup tripal_library
  202. */
  203. function tripal_library_admin_validate($form, &$form_state) {
  204. global $user; // we need access to the user info
  205. $job_args = array();
  206. // Submit the Sync Job if selected
  207. if ($form_state['values']['op'] == t('Sync Libraries')) {
  208. // check to see if the user wants to sync chado and drupal. If
  209. // so then we need to register a job to do so with tripal
  210. $libraries = $form_state['values']['libraries'];
  211. $do_all = FALSE;
  212. $to_sync = array();
  213. foreach ($libraries as $library_id) {
  214. if (preg_match("/^all$/i", $library_id)) {
  215. $do_all = TRUE;
  216. }
  217. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  218. // get the library info
  219. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  220. $library = db_fetch_object(chado_query($sql, $library_id));
  221. $to_sync[$library_id] = $library->name;
  222. }
  223. }
  224. // submit the job to the tripal job manager
  225. if ($do_all) {
  226. tripal_add_job('Sync all libraries', 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  227. }
  228. else{
  229. foreach ($to_sync as $library_id => $name) {
  230. $job_args[0] = $library_id;
  231. tripal_add_job("Sync library: $name", 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  232. }
  233. }
  234. }
  235. // -------------------------------------
  236. // Submit the Reindex Job if selected
  237. if ($form_state['values']['op'] == t('Reindex Features')) {
  238. $libraries = $form_state['values']['re-libraries'];
  239. foreach ($libraries as $library_id) {
  240. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  241. // get the library info
  242. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  243. $library = db_fetch_object(chado_query($sql, $library_id));
  244. $job_args[0] = $library_id;
  245. tripal_add_job("Reindex features for library: $library->name", 'tripal_library',
  246. 'tripal_library_reindex_features', $job_args, $user->uid);
  247. }
  248. }
  249. }
  250. // -------------------------------------
  251. // Submit the Taxonomy Job if selected
  252. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  253. $libraries = $form_state['values']['tx-libraries'];
  254. foreach ($libraries as $library_id) {
  255. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  256. // get the library info
  257. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  258. $library = db_fetch_object(chado_query($sql, $library_id));
  259. $job_args[0] = $library_id;
  260. tripal_add_job("Set taxonomy for features in library: $library->name", 'tripal_library',
  261. 'tripal_library_taxonify_features', $job_args, $user->uid);
  262. }
  263. }
  264. }
  265. // -------------------------------------
  266. // Submit the Cleanup Job if selected
  267. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  268. tripal_add_job('Cleanup orphaned libraries', 'tripal_library',
  269. 'tripal_library_cleanup', $job_args, $user->uid);
  270. }
  271. }
  272. /**
  273. * Add the library as a taxonomy term for associating with library_features
  274. *
  275. * @ingroup tripal_library
  276. */
  277. function tripal_library_add_taxonomy($node, $library_id) {
  278. //include the file containing the required functions. We only have to
  279. // do this because Drupal 6 fails to do this globally for us and
  280. // the drupal_execute function below won't work
  281. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  282. /* // get the vocabulary id
  283. $vocabularies = taxonomy_get_vocabularies();
  284. $vid = NULL;
  285. foreach($vocabularies as $vocab){
  286. if($vocab->name == 'DNA Libraries'){
  287. $vid = $vocab->vid;
  288. }
  289. }
  290. if(!$vid){ */
  291. // add the vocabulary
  292. $vocab_form['values']['name'] = 'DNA Libraries';
  293. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  294. $vocab_form['values']['help'] = '';
  295. $vocab_form['values']['module'] = 'taxonomy';
  296. drupal_execute('taxonomy_form_vocabulary', $vocab_form);
  297. return;
  298. // }
  299. // make sure this term doesn't already exist. If it doesn't then add it
  300. if ($vid) {
  301. $tree = taxonomy_get_tree($vid);
  302. $found = 0;
  303. foreach ($tree as $term) {
  304. if ($term->name == $node->title) {
  305. $found = 1;
  306. }
  307. }
  308. // add the term to the vocabulary
  309. if (!$found) {
  310. $form_state = array();
  311. $form_state['values']['name'] = $node->title;
  312. $form_state['values']['description'] = $library_id;
  313. drupal_execute('taxonomy_form_term', $form_state, $vid);
  314. }
  315. }
  316. }
  317. /**
  318. *
  319. *
  320. * @ingroup tripal_library
  321. */
  322. function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
  323. global $user;
  324. $page_content = '';
  325. // get the list of libraries and create new nodes
  326. if (!$library_id) {
  327. $sql = "SELECT * FROM {Library} L";
  328. $results = chado_query($sql);
  329. }
  330. else {
  331. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  332. $results = chado_query($sql, $library_id);
  333. }
  334. // We'll use the following SQL statement for checking if the library
  335. // already exists as a drupal node.
  336. $sql = "SELECT * FROM {chado_library} ".
  337. "WHERE library_id = %d";
  338. while ($library = db_fetch_object($results)) {
  339. // check if this library already exists in the drupal database. if it
  340. // does then skip this library and go to the next one.
  341. if (!db_fetch_object(db_query($sql, $library->library_id))) {
  342. $new_node = new stdClass();
  343. $new_node->type = 'chado_library';
  344. $new_node->uid = $user->uid;
  345. $new_node->title = "$library->name";
  346. $new_node->library_id = $library->library_id;
  347. $new_node->organism_id = $library->organism_id;
  348. $new_node->uniquename = $library->uniquename;
  349. $new_node->type_id = $library->type_id;
  350. node_validate($new_node);
  351. $errors = form_get_errors();
  352. if (!$errors) {
  353. $node = node_submit($new_node);
  354. node_save($node);
  355. if ($node->nid) {
  356. print "Added " . $library->name . "\n";
  357. }
  358. else {
  359. print "ERROR: Unable to create " . $library->name . "\n";
  360. }
  361. }
  362. else {
  363. print "ERROR: Unable to create " . $library->name . "\n" . print_r($errors, TRUE) . "\n";
  364. }
  365. }
  366. else {
  367. print "Skipped " . $library->name . "\n";
  368. }
  369. }
  370. return $page_content;
  371. }
  372. /**
  373. *
  374. * @ingroup tripal_library
  375. */
  376. function tripal_library_feature_set_taxonomy($library_id = NULL) {
  377. //TO DO : return usable error if vocabs don't exist
  378. // get the list of vocabularies and find our two vocabularies of interest
  379. $vocabularies = taxonomy_get_vocabularies();
  380. $vid = NULL;
  381. foreach ($vocabularies as $vocab) {
  382. if ($vocab->name == 'Library') {
  383. $vid = $vocab->vid;
  384. }
  385. }
  386. if (!$vid) {
  387. return;
  388. }
  389. // We'll use the following SQL statement for getting the node info
  390. if ($library_id) {
  391. print "Finding features for library with ID: $library_id\n";
  392. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  393. "FROM {library_feature} LF ".
  394. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  395. "WHERE L.library_id = $library_id ".
  396. "ORDER BY LF.feature_id";
  397. $features = chado_query($sql);
  398. }
  399. else {
  400. print "Finding features for all libraries\n";
  401. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  402. "FROM {library_feature} LF ".
  403. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  404. "ORDER BY LF.feature_id";
  405. $features = chado_query($sql);
  406. }
  407. $node_sql = "SELECT * FROM {chado_feature} CF ".
  408. " INNER JOIN {node} N ON CF.nid = N.nid ".
  409. "WHERE feature_id = %d";
  410. // iterate through the features and add the taxonomy
  411. while ($feature = db_fetch_object($features)) {
  412. $node = db_fetch_object(db_query($node_sql, $feature->feature_id));
  413. $tags["$vid"] = $feature->libname;
  414. $terms['tags'] = $tags;
  415. taxonomy_node_save($node, $terms);
  416. print "Updated $feature->feature_id as $feature->libname\n";
  417. }
  418. }
  419. /**
  420. *
  421. * @ingroup tripal_library
  422. */
  423. function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
  424. $i = 0;
  425. // if the caller provided a library_id then get all of the features
  426. // associated with the library. Otherwise get all sequences assoicated
  427. // with all libraries.
  428. if ($library_id) {
  429. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  430. " FROM {library_feature} LF ".
  431. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  432. "WHERE L.library_id = $library_id ".
  433. "ORDER BY LF.feature_id";
  434. $results = chado_query($sql);
  435. }
  436. else {
  437. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  438. " FROM {library_feature} LF ".
  439. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  440. "ORDER BY LF.feature_id";
  441. $results = chado_query($sql);
  442. }
  443. // load into ids array
  444. $count = 0;
  445. $ids = array();
  446. while ($id = db_fetch_object($results)) {
  447. $ids[$count] = $id->feature_id;
  448. $count++;
  449. }
  450. $interval = intval($count * 0.01);
  451. foreach ($ids as $feature_id) {
  452. // update the job status every 1% features
  453. if ($job_id and $i % interval == 0) {
  454. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  455. }
  456. $i++;
  457. }
  458. }
  459. /**
  460. *
  461. * @ingroup tripal_library
  462. */
  463. function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
  464. $i = 0;
  465. // if the caller provided a library_id then get all of the features
  466. // associated with the library. Otherwise get all sequences assoicated
  467. // with all libraries.
  468. if ($library_id) {
  469. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  470. " FROM {library_feature} LF ".
  471. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  472. "WHERE L.library_id = $library_id ".
  473. "ORDER BY LF.feature_id";
  474. $results = chado_query($sql);
  475. }
  476. else {
  477. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  478. " FROM {library_feature} LF ".
  479. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  480. "ORDER BY LF.feature_id";
  481. $results = chado_query($sql);
  482. }
  483. // load into ids array
  484. $count = 0;
  485. $ids = array();
  486. while ($id = db_fetch_object($results)) {
  487. $ids[$count] = $id->feature_id;
  488. $count++;
  489. }
  490. // make sure our vocabularies are set before proceeding
  491. tripal_feature_set_vocabulary();
  492. // use this SQL for getting the nodes
  493. $nsql = "SELECT * FROM {chado_feature} CF ".
  494. " INNER JOIN {node} N ON N.nid = CF.nid ".
  495. "WHERE feature_id = %d";
  496. // iterate through the features and set the taxonomy
  497. $interval = intval($count * 0.01);
  498. foreach ($ids as $feature_id) {
  499. // update the job status every 1% features
  500. if ($job_id and $i % interval == 0) {
  501. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  502. }
  503. $node = db_fetch_object(db_query($nsql, $feature_id));
  504. tripal_feature_set_taxonomy($node, $feature_id);
  505. $i++;
  506. }
  507. }
  508. /**
  509. * Remove orphaned drupal nodes
  510. *
  511. * @param $dummy
  512. * Not Used -kept for backwards compatibility
  513. * @param $job_id
  514. * The id of the tripal job executing this function
  515. *
  516. * @ingroup tripal_library
  517. */
  518. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  519. return tripal_core_clean_orphaned_nodes('library', $job_id);
  520. }