tripal_library.admin.inc 19 KB

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