tripal_library.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. foreach ($lib_rset as $library) {
  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 = $lib_rset->fetchObject()) {
  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. foreach ($lib_rset as $libary) {
  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 = :library_id";
  162. $chado_library = db_query($sql, array(':library_id' => $library->library_id))->fetchObject();
  163. if (!$chado_library) {
  164. $lib_boxes[$library->library_id] = "$library->name";
  165. $added++;
  166. }
  167. }
  168. // if we have libraries we need to add to the checkbox then
  169. // build that form element
  170. if ($added > 0) {
  171. $lib_boxes['all'] = "All Libraries";
  172. $form['reindex']['description'] = array(
  173. '#type' => 'item',
  174. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  175. '#weight' => 1,
  176. );
  177. $form['sync']['libraries'] = array(
  178. '#title' => t('Available Libraries'),
  179. '#type' => t('checkboxes'),
  180. '#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."),
  181. '#required' => FALSE,
  182. '#prefix' => '<div id="lib_boxes">',
  183. '#suffix' => '</div>',
  184. '#options' => $lib_boxes,
  185. '#weight' => 2,
  186. );
  187. $form['sync']['button'] = array(
  188. '#type' => 'submit',
  189. '#value' => t('Sync Libraries'),
  190. '#weight' => 3,
  191. );
  192. }
  193. // we don't have any libraries to select from
  194. else {
  195. $form['sync']['value'] = array(
  196. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  197. );
  198. }
  199. }
  200. /**
  201. *
  202. * @ingroup tripal_library
  203. */
  204. function tripal_library_admin_validate($form, &$form_state) {
  205. global $user; // we need access to the user info
  206. $job_args = array();
  207. // Submit the Sync Job if selected
  208. if ($form_state['values']['op'] == t('Sync Libraries')) {
  209. // check to see if the user wants to sync chado and drupal. If
  210. // so then we need to register a job to do so with tripal
  211. $libraries = $form_state['values']['libraries'];
  212. $do_all = FALSE;
  213. $to_sync = array();
  214. foreach ($libraries as $library_id) {
  215. if (preg_match("/^all$/i", $library_id)) {
  216. $do_all = TRUE;
  217. }
  218. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  219. // get the library info
  220. $sql = "SELECT * FROM {library} WHERE library_id = :library_id";
  221. $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
  222. $to_sync[$library_id] = $library->name;
  223. }
  224. }
  225. // submit the job to the tripal job manager
  226. if ($do_all) {
  227. tripal_add_job('Sync all libraries', 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  228. }
  229. else{
  230. foreach ($to_sync as $library_id => $name) {
  231. $job_args[0] = $library_id;
  232. tripal_add_job("Sync library: $name", 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  233. }
  234. }
  235. }
  236. // -------------------------------------
  237. // Submit the Reindex Job if selected
  238. if ($form_state['values']['op'] == t('Reindex Features')) {
  239. $libraries = $form_state['values']['re-libraries'];
  240. foreach ($libraries as $library_id) {
  241. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  242. // get the library info
  243. $sql = "SELECT * FROM {Library} WHERE library_id = :library_id";
  244. $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
  245. $job_args[0] = $library_id;
  246. tripal_add_job("Reindex features for library: $library->name", 'tripal_library',
  247. 'tripal_library_reindex_features', $job_args, $user->uid);
  248. }
  249. }
  250. }
  251. // -------------------------------------
  252. // Submit the Taxonomy Job if selected
  253. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  254. $libraries = $form_state['values']['tx-libraries'];
  255. foreach ($libraries as $library_id) {
  256. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  257. // get the library info
  258. $sql = "SELECT * FROM {Library} WHERE library_id = :libary_id";
  259. $library = chado_query($sql, array(':library_id' => $library_id))->fetchObject();
  260. $job_args[0] = $library_id;
  261. tripal_add_job("Set taxonomy for features in library: $library->name", 'tripal_library',
  262. 'tripal_library_taxonify_features', $job_args, $user->uid);
  263. }
  264. }
  265. }
  266. // -------------------------------------
  267. // Submit the Cleanup Job if selected
  268. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  269. tripal_add_job('Cleanup orphaned libraries', 'tripal_library',
  270. 'tripal_library_cleanup', $job_args, $user->uid);
  271. }
  272. }
  273. /**
  274. * Add the library as a taxonomy term for associating with library_features
  275. *
  276. * @ingroup tripal_library
  277. */
  278. function tripal_library_add_taxonomy($node, $library_id) {
  279. //include the file containing the required functions. We only have to
  280. // do this because Drupal 6 fails to do this globally for us and
  281. // the drupal_execute function below won't work
  282. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  283. // add the vocabulary
  284. $vocab_form['values']['name'] = 'DNA Libraries';
  285. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  286. $vocab_form['values']['help'] = '';
  287. $vocab_form['values']['module'] = 'taxonomy';
  288. drupal_form_submit('taxonomy_form_vocabulary', $vocab_form);
  289. return;
  290. // make sure this term doesn't already exist. If it doesn't then add it
  291. if ($vid) {
  292. $tree = taxonomy_get_tree($vid);
  293. $found = 0;
  294. foreach ($tree as $term) {
  295. if ($term->name == $node->title) {
  296. $found = 1;
  297. }
  298. }
  299. // add the term to the vocabulary
  300. if (!$found) {
  301. $form_state = array();
  302. $form_state['values']['name'] = $node->title;
  303. $form_state['values']['description'] = $library_id;
  304. drupal_form_submit('taxonomy_form_term', $form_state, $vid);
  305. }
  306. }
  307. }
  308. /**
  309. *
  310. *
  311. * @ingroup tripal_library
  312. */
  313. function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
  314. global $user;
  315. $page_content = '';
  316. // get the list of libraries and create new nodes
  317. if (!$library_id) {
  318. $sql = "SELECT * FROM {library} L";
  319. $results = chado_query($sql);
  320. }
  321. else {
  322. $sql = "SELECT * FROM {library} L WHERE library_id = :library_id";
  323. $results = chado_query($sql, array(':library_id' => $library_id));
  324. }
  325. // We'll use the following SQL statement for checking if the library
  326. // already exists as a drupal node.
  327. $sql = "SELECT * FROM {chado_library} ".
  328. "WHERE library_id = :libary_id";
  329. while ($library = $results->fetchObject()) {
  330. // check if this library already exists in the drupal database. if it
  331. // does then skip this library and go to the next one.
  332. if (!db_query($sql, array(':libary_id' => $library->library_id))->fetchObject()) {
  333. $new_node = new stdClass();
  334. $new_node->type = 'chado_library';
  335. $new_node->uid = $user->uid;
  336. $new_node->title = "$library->name";
  337. $new_node->library_id = $library->library_id;
  338. $new_node->organism_id = $library->organism_id;
  339. $new_node->uniquename = $library->uniquename;
  340. $new_node->type_id = $library->type_id;
  341. node_validate($new_node);
  342. $errors = form_get_errors();
  343. if (!$errors) {
  344. $node = node_submit($new_node);
  345. node_save($node);
  346. if ($node->nid) {
  347. print "Added " . $library->name . "\n";
  348. }
  349. else {
  350. print "ERROR: Unable to create " . $library->name . "\n";
  351. }
  352. }
  353. else {
  354. print "ERROR: Unable to create " . $library->name . "\n" . print_r($errors, TRUE) . "\n";
  355. }
  356. }
  357. else {
  358. print "Skipped " . $library->name . "\n";
  359. }
  360. }
  361. return $page_content;
  362. }
  363. /**
  364. *
  365. * @ingroup tripal_library
  366. */
  367. function tripal_library_feature_set_taxonomy($library_id = NULL) {
  368. //TO DO : return usable error if vocabs don't exist
  369. // get the list of vocabularies and find our two vocabularies of interest
  370. $vocabularies = taxonomy_get_vocabularies();
  371. $vid = NULL;
  372. foreach ($vocabularies as $vocab) {
  373. if ($vocab->name == 'Library') {
  374. $vid = $vocab->vid;
  375. }
  376. }
  377. if (!$vid) {
  378. return;
  379. }
  380. // We'll use the following SQL statement for getting the node info
  381. if ($library_id) {
  382. print "Finding features for library with ID: $library_id\n";
  383. $sql = "
  384. SELECT LF.feature_id, L.library_id, L.name as libname
  385. FROM {library_feature} LF
  386. INNER JOIN {library} L ON LF.library_id = L.library_id
  387. WHERE L.library_id = :library_id
  388. ORDER BY LF.feature_id
  389. ";
  390. $features = chado_query($sql, array(':library_id' => $library_id));
  391. }
  392. else {
  393. print "Finding features for all libraries\n";
  394. $sql = "
  395. SELECT LF.feature_id, L.library_id, L.name as libname
  396. FROM {library_feature} LF
  397. INNER JOIN {library} L ON LF.library_id = L.library_id
  398. ORDER BY LF.feature_id
  399. ";
  400. $features = chado_query($sql);
  401. }
  402. $node_sql = "
  403. SELECT *
  404. FROM {chado_feature} CF
  405. INNER JOIN {node} N ON CF.nid = N.nid
  406. WHERE feature_id = :feature_id
  407. ";
  408. // iterate through the features and add the taxonomy
  409. while ($feature = $features->fetchObject()) {
  410. $node = db_query($node_sql, array(':feature_id' => $feature->feature_id))->fetchObject();
  411. $tags["$vid"] = $feature->libname;
  412. $terms['tags'] = $tags;
  413. taxonomy_node_save($node, $terms);
  414. print "Updated $feature->feature_id as $feature->libname\n";
  415. }
  416. }
  417. /**
  418. *
  419. * @ingroup tripal_library
  420. */
  421. function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
  422. $i = 0;
  423. // if the caller provided a library_id then get all of the features
  424. // associated with the library. Otherwise get all sequences assoicated
  425. // with all libraries.
  426. if ($library_id) {
  427. $sql = "
  428. SELECT LF.feature_id, L.library_id, L.name as libname
  429. FROM {library_feature} LF
  430. INNER JOIN {library} L ON LF.library_id = L.library_id
  431. WHERE L.library_id = :library_id
  432. ORDER BY LF.feature_id
  433. ";
  434. $results = chado_query($sql, array(':library_id' => $library_id));
  435. }
  436. else {
  437. $sql = "
  438. SELECT LF.feature_id, L.library_id, L.name as libname
  439. FROM {library_feature} LF
  440. INNER JOIN {library} L ON LF.library_id = L.library_id
  441. ORDER BY LF.feature_id
  442. ";
  443. $results = chado_query($sql);
  444. }
  445. // load into ids array
  446. $count = 0;
  447. $ids = array();
  448. while ($id = $results->fetchObject()) {
  449. $ids[$count] = $id->feature_id;
  450. $count++;
  451. }
  452. $interval = intval($count * 0.01);
  453. foreach ($ids as $feature_id) {
  454. // update the job status every 1% features
  455. if ($job_id and $i % interval == 0) {
  456. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  457. }
  458. $i++;
  459. }
  460. }
  461. /**
  462. *
  463. * @ingroup tripal_library
  464. */
  465. function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
  466. $i = 0;
  467. // if the caller provided a library_id then get all of the features
  468. // associated with the library. Otherwise get all sequences assoicated
  469. // with all libraries.
  470. if ($library_id) {
  471. $sql = "
  472. SELECT LF.feature_id, L.library_id, L.name as libname
  473. FROM {library_feature} LF
  474. INNER JOIN {library} L ON LF.library_id = L.library_id
  475. WHERE L.library_id = :library_id
  476. ORDER BY LF.feature_id
  477. ";
  478. $results = chado_query($sql, array(':library_id' => $library_id));
  479. }
  480. else {
  481. $sql = "
  482. SELECT LF.feature_id, L.library_id, L.name as libname
  483. FROM {library_feature} LF
  484. INNER JOIN {library} L ON LF.library_id = L.library_id
  485. ORDER BY LF.feature_id
  486. ";
  487. $results = chado_query($sql);
  488. }
  489. // load into ids array
  490. $count = 0;
  491. $ids = array();
  492. while ($id = $results->fetchObject()) {
  493. $ids[$count] = $id->feature_id;
  494. $count++;
  495. }
  496. // make sure our vocabularies are set before proceeding
  497. tripal_feature_set_vocabulary();
  498. // use this SQL for getting the nodes
  499. $nsql = "
  500. SELECT *
  501. FROM {chado_feature} CF
  502. INNER JOIN {node} N ON N.nid = CF.nid
  503. WHERE feature_id = :feature_id
  504. ";
  505. // iterate through the features and set the taxonomy
  506. $interval = intval($count * 0.01);
  507. foreach ($ids as $feature_id) {
  508. // update the job status every 1% features
  509. if ($job_id and $i % interval == 0) {
  510. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  511. }
  512. $node = db_query($nsql, array(':feature_id' => $feature_id))->fetchObject();
  513. tripal_feature_set_taxonomy($node, $feature_id);
  514. $i++;
  515. }
  516. }
  517. /**
  518. * Remove orphaned drupal nodes
  519. *
  520. * @param $dummy
  521. * Not Used -kept for backwards compatibility
  522. * @param $job_id
  523. * The id of the tripal job executing this function
  524. *
  525. * @ingroup tripal_library
  526. */
  527. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  528. return tripal_core_clean_orphaned_nodes('library', $job_id);
  529. }