tripal_library.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 = 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. 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 = %d";
  221. $library = db_fetch_object(chado_query($sql, $library_id));
  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 = %d";
  244. $library = db_fetch_object(chado_query($sql, $library_id));
  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 = %d";
  259. $library = db_fetch_object(chado_query($sql, $library_id));
  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. /* // get the vocabulary id
  284. $vocabularies = taxonomy_get_vocabularies();
  285. $vid = NULL;
  286. foreach($vocabularies as $vocab){
  287. if($vocab->name == 'DNA Libraries'){
  288. $vid = $vocab->vid;
  289. }
  290. }
  291. if(!$vid){ */
  292. // add the vocabulary
  293. $vocab_form['values']['name'] = 'DNA Libraries';
  294. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  295. $vocab_form['values']['help'] = '';
  296. $vocab_form['values']['module'] = 'taxonomy';
  297. drupal_execute('taxonomy_form_vocabulary', $vocab_form);
  298. return;
  299. // }
  300. // make sure this term doesn't already exist. If it doesn't then add it
  301. if ($vid) {
  302. $tree = taxonomy_get_tree($vid);
  303. $found = 0;
  304. foreach ($tree as $term) {
  305. if ($term->name == $node->title) {
  306. $found = 1;
  307. }
  308. }
  309. // add the term to the vocabulary
  310. if (!$found) {
  311. $form_state = array();
  312. $form_state['values']['name'] = $node->title;
  313. $form_state['values']['description'] = $library_id;
  314. drupal_execute('taxonomy_form_term', $form_state, $vid);
  315. }
  316. }
  317. }
  318. /**
  319. *
  320. *
  321. * @ingroup tripal_library
  322. */
  323. function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
  324. global $user;
  325. $page_content = '';
  326. // get the list of libraries and create new nodes
  327. if (!$library_id) {
  328. $sql = "SELECT * FROM {Library} L";
  329. $results = chado_query($sql);
  330. }
  331. else {
  332. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  333. $results = chado_query($sql, $library_id);
  334. }
  335. // We'll use the following SQL statement for checking if the library
  336. // already exists as a drupal node.
  337. $sql = "SELECT * FROM {chado_library} ".
  338. "WHERE library_id = %d";
  339. while ($library = db_fetch_object($results)) {
  340. // check if this library already exists in the drupal database. if it
  341. // does then skip this library and go to the next one.
  342. if (!db_fetch_object(db_query($sql, $library->library_id))) {
  343. $new_node = new stdClass();
  344. $new_node->type = 'chado_library';
  345. $new_node->uid = $user->uid;
  346. $new_node->title = "$library->name";
  347. $new_node->library_id = $library->library_id;
  348. $new_node->organism_id = $library->organism_id;
  349. $new_node->uniquename = $library->uniquename;
  350. $new_node->type_id = $library->type_id;
  351. node_validate($new_node);
  352. $errors = form_get_errors();
  353. if (!$errors) {
  354. $node = node_submit($new_node);
  355. node_save($node);
  356. if ($node->nid) {
  357. print "Added " . $library->name . "\n";
  358. }
  359. else {
  360. print "ERROR: Unable to create " . $library->name . "\n";
  361. }
  362. }
  363. else {
  364. print "ERROR: Unable to create " . $library->name . "\n" . print_r($errors, TRUE) . "\n";
  365. }
  366. }
  367. else {
  368. print "Skipped " . $library->name . "\n";
  369. }
  370. }
  371. return $page_content;
  372. }
  373. /**
  374. *
  375. * @ingroup tripal_library
  376. */
  377. function tripal_library_feature_set_taxonomy($library_id = NULL) {
  378. //TO DO : return usable error if vocabs don't exist
  379. // get the list of vocabularies and find our two vocabularies of interest
  380. $vocabularies = taxonomy_get_vocabularies();
  381. $vid = NULL;
  382. foreach ($vocabularies as $vocab) {
  383. if ($vocab->name == 'Library') {
  384. $vid = $vocab->vid;
  385. }
  386. }
  387. if (!$vid) {
  388. return;
  389. }
  390. // We'll use the following SQL statement for getting the node info
  391. if ($library_id) {
  392. print "Finding features for library with ID: $library_id\n";
  393. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  394. "FROM {library_feature} LF ".
  395. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  396. "WHERE L.library_id = $library_id ".
  397. "ORDER BY LF.feature_id";
  398. $features = chado_query($sql);
  399. }
  400. else {
  401. print "Finding features for all libraries\n";
  402. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  403. "FROM {library_feature} LF ".
  404. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  405. "ORDER BY LF.feature_id";
  406. $features = chado_query($sql);
  407. }
  408. $node_sql = "SELECT * FROM {chado_feature} CF ".
  409. " INNER JOIN {node} N ON CF.nid = N.nid ".
  410. "WHERE feature_id = %d";
  411. // iterate through the features and add the taxonomy
  412. while ($feature = db_fetch_object($features)) {
  413. $node = db_fetch_object(db_query($node_sql, $feature->feature_id));
  414. $tags["$vid"] = $feature->libname;
  415. $terms['tags'] = $tags;
  416. taxonomy_node_save($node, $terms);
  417. print "Updated $feature->feature_id as $feature->libname\n";
  418. }
  419. }
  420. /**
  421. *
  422. * @ingroup tripal_library
  423. */
  424. function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
  425. $i = 0;
  426. // if the caller provided a library_id then get all of the features
  427. // associated with the library. Otherwise get all sequences assoicated
  428. // with all libraries.
  429. if ($library_id) {
  430. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  431. " FROM {library_feature} LF ".
  432. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  433. "WHERE L.library_id = $library_id ".
  434. "ORDER BY LF.feature_id";
  435. $results = chado_query($sql);
  436. }
  437. else {
  438. $sql = "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. $results = chado_query($sql);
  443. }
  444. // load into ids array
  445. $count = 0;
  446. $ids = array();
  447. while ($id = db_fetch_object($results)) {
  448. $ids[$count] = $id->feature_id;
  449. $count++;
  450. }
  451. $interval = intval($count * 0.01);
  452. foreach ($ids as $feature_id) {
  453. // update the job status every 1% features
  454. if ($job_id and $i % interval == 0) {
  455. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  456. }
  457. tripal_feature_sync_feature($feature_id);
  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 = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  472. " FROM {library_feature} LF ".
  473. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  474. "WHERE L.library_id = $library_id ".
  475. "ORDER BY LF.feature_id";
  476. $results = chado_query($sql);
  477. }
  478. else {
  479. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  480. " FROM {library_feature} LF ".
  481. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  482. "ORDER BY LF.feature_id";
  483. $results = chado_query($sql);
  484. }
  485. // load into ids array
  486. $count = 0;
  487. $ids = array();
  488. while ($id = db_fetch_object($results)) {
  489. $ids[$count] = $id->feature_id;
  490. $count++;
  491. }
  492. // make sure our vocabularies are set before proceeding
  493. tripal_feature_set_vocabulary();
  494. // use this SQL for getting the nodes
  495. $nsql = "SELECT * FROM {chado_feature} CF ".
  496. " INNER JOIN {node} N ON N.nid = CF.nid ".
  497. "WHERE feature_id = %d";
  498. // iterate through the features and set the taxonomy
  499. $interval = intval($count * 0.01);
  500. foreach ($ids as $feature_id) {
  501. // update the job status every 1% features
  502. if ($job_id and $i % interval == 0) {
  503. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  504. }
  505. $node = db_fetch_object(db_query($nsql, $feature_id));
  506. tripal_feature_set_taxonomy($node, $feature_id);
  507. $i++;
  508. }
  509. }
  510. /**
  511. * Remove orphaned drupal nodes
  512. *
  513. * @param $dummy
  514. * Not Used -kept for backwards compatibility
  515. * @param $job_id
  516. * The id of the tripal job executing this function
  517. *
  518. * @ingroup tripal_library
  519. */
  520. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  521. return tripal_core_clean_orphaned_nodes('library', $job_id);
  522. }