tripal_library.module 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. <?php
  2. require('tripal_library.api.inc');
  3. /*******************************************************************************
  4. * Display help and module information
  5. * @param path which path of the site we're displaying help
  6. * @param arg array that holds the current path as would be returned from arg()
  7. * function
  8. * @return help text for the path
  9. */
  10. function tripal_library_help($path, $arg) {
  11. $output = '';
  12. switch ($path) {
  13. case "admin/help#tripal_library":
  14. $output = '<p>'.
  15. t("Displays links to nodes created on this date").
  16. '</p>';
  17. break;
  18. }
  19. return $output;
  20. }
  21. /*******************************************************************************
  22. * Provide information to drupal about the node types that we're creating
  23. * in this module
  24. */
  25. function tripal_library_node_info() {
  26. $nodes = array();
  27. $nodes['chado_library'] = array(
  28. 'name' => t('Library'),
  29. 'module' => 'chado_library',
  30. 'description' => t('A library from the chado database'),
  31. 'has_title' => FALSE,
  32. 'title_label' => t('Library'),
  33. 'has_body' => FALSE,
  34. 'body_label' => t('Library Description'),
  35. 'locked' => TRUE
  36. );
  37. return $nodes;
  38. }
  39. /*******************************************************************************
  40. * Set the permission types that the chado module uses. Essentially we
  41. * want permissionis that protect creation, editing and deleting of chado
  42. * data objects
  43. */
  44. function tripal_library_perm(){
  45. return array(
  46. 'access chado_library content',
  47. 'create chado_library content',
  48. 'delete chado_library content',
  49. 'edit chado_library content',
  50. );
  51. }
  52. /************************************************************************
  53. * Set the permission types that the module uses.
  54. */
  55. function chado_library_access($op, $node, $account) {
  56. if ($op == 'create') {
  57. return user_access('create chado_library content', $account);
  58. }
  59. if ($op == 'update') {
  60. if (user_access('edit chado_library content', $account)) {
  61. return TRUE;
  62. }
  63. }
  64. if ($op == 'delete') {
  65. if (user_access('delete chado_library content', $account)) {
  66. return TRUE;
  67. }
  68. }
  69. if ($op == 'view') {
  70. if (user_access('access chado_library content', $account)) {
  71. return TRUE;
  72. }
  73. }
  74. return FALSE;
  75. }
  76. /*******************************************************************************
  77. * Menu items are automatically added for the new node types created
  78. * by this module to the 'Create Content' Navigation menu item. This function
  79. * adds more menu items needed for this module.
  80. */
  81. function tripal_library_menu() {
  82. $items = array();
  83. // The administative settings menu
  84. $items['admin/tripal/tripal_library'] = array(
  85. 'title' => 'Libraries',
  86. 'description' => 'Basic Description of Tripal Library Module Functionality',
  87. 'page callback' => 'tripal_library_module_description_page',
  88. 'access arguments' => array('access administration pages'),
  89. 'type' => MENU_NORMAL_ITEM,
  90. );
  91. $items['admin/tripal/tripal_library/configuration'] = array(
  92. 'title' => 'Configuration',
  93. 'description' => 'Manage integration of Chado libraries including associated features.',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_library_admin'),
  96. 'access arguments' => array('access administration pages'),
  97. 'type' => MENU_NORMAL_ITEM,
  98. );
  99. // Synchronizing libraries from Chado to Drupal
  100. $items['chado_sync_libraries'] = array(
  101. 'title' => t('Sync Data'),
  102. 'page callback' => 'tripal_library_sync_libraries',
  103. 'access arguments' => array('access administration pages'),
  104. 'type' => MENU_CALLBACK
  105. );
  106. // Displaying libraries
  107. $items['libraries'] = array(
  108. 'menu_name' => ('primary-links'), //Enable the 'Library' primary link
  109. 'title' => t('DNA Libraries'),
  110. 'page callback' => 'tripal_library_show_libraries',
  111. 'access arguments' => array('access chado_library content'),
  112. 'type' => MENU_NORMAL_ITEM
  113. );
  114. return $items;
  115. }
  116. /*************************************************************************
  117. * Implements hook_views_api()
  118. * Purpose: Essentially this hook tells drupal that there is views support for
  119. * for this module which then includes tripal_db.views.inc where all the
  120. * views integration code is
  121. */
  122. function tripal_library_views_api() {
  123. return array(
  124. 'api' => 2.0,
  125. );
  126. }
  127. /*************************************************************************
  128. * Purpose: Provide Guidance to new Tripal Admin
  129. *
  130. * @return HTML Formatted text
  131. */
  132. function tripal_library_module_description_page() {
  133. $text = '';
  134. $text .= '<h3>Description:</h3>';
  135. $text .= '<p>TODO: Basic Description of this module including mention/link to the chado module</p>';
  136. $text .= '<h3>Post Installation Instructions:</h3>';
  137. $text .= '<p>TODO: Describe any post installation intructions here. You shouldalways include setting user permissions.</p>';
  138. $text .= '<h3>Features of this Module:</h3>';
  139. $text .= '<p>TODO: Discuss the Features of this module including links. Some features to consider are creating content, details pages/node content, editing/deleteing, basic listings and vies integration. See admin/tripal/tripal_stock for an example.</p>';
  140. return $text;
  141. }
  142. /*******************************************************************************
  143. * Administrative settings form
  144. */
  145. function tripal_library_admin () {
  146. $form = array();
  147. // before proceeding check to see if we have any
  148. // currently processing jobs. If so, we don't want
  149. // to give the opportunity to sync libraries
  150. $active_jobs = FALSE;
  151. if(tripal_get_module_active_jobs('tripal_library')){
  152. $active_jobs = TRUE;
  153. }
  154. // add the field set for syncing libraries
  155. if(!$active_jobs){
  156. get_tripal_library_admin_form_sync_set ($form);
  157. get_tripal_library_admin_form_reindex_set($form);
  158. get_tripal_library_admin_form_taxonomy_set($form);
  159. get_tripal_library_admin_form_cleanup_set($form);
  160. get_tripal_library_admin_form_menu_set($form);
  161. } else {
  162. $form['notice'] = array(
  163. '#type' => 'fieldset',
  164. '#title' => t('Library Management Temporarily Unavailable')
  165. );
  166. $form['notice']['message'] = array(
  167. '#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.'),
  168. );
  169. }
  170. return system_settings_form($form);
  171. }
  172. /*******************************************************************************
  173. * HOOK: Implementation of hook_nodeapi()
  174. * Display library information for associated features or organisms
  175. * This function also provides contents for indexing
  176. */
  177. function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
  178. switch ($op) {
  179. // Note that this function only adds library view to an organism/feature
  180. // node.
  181. case 'view':
  182. // add the library to the organism/feature search indexing
  183. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  184. $node->content['tripal_library_index_version'] = array(
  185. '#value' => theme('tripal_library_search_index',$node),
  186. );
  187. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  188. $node->content['tripal_library_index_version'] = array(
  189. '#value' => theme('tripal_library_search_result',$node),
  190. );
  191. } else {
  192. switch($node->type){
  193. case 'chado_organism':
  194. // Show library if the organism/feature is not at teaser view
  195. $node->content['tripal_organism_libraries'] = array(
  196. '#value' => theme('tripal_organism_libraries', $node),
  197. );
  198. break;
  199. case 'chado_feature':
  200. // Show library if the organism/feature is not at teaser view
  201. $node->content['tripal_feature_libraries'] = array(
  202. '#value' => theme('tripal_feature_libraries', $node),
  203. );
  204. break;
  205. }
  206. }
  207. break;
  208. }
  209. }
  210. /************************************************************************
  211. * We need to let drupal know about our theme functions and their arguments.
  212. * We create theme functions to allow users of the module to customize the
  213. * look and feel of the output generated in this module
  214. */
  215. function tripal_library_theme () {
  216. return array(
  217. 'tripal_library_library_table' => array (
  218. 'arguments' => array('libraries'),
  219. ),
  220. 'tripal_library_search_index' => array (
  221. 'arguments' => array('node'),
  222. ),
  223. 'tripal_library_search_result' => array (
  224. 'arguments' => array('node'),
  225. ),
  226. 'tripal_organism_libraries' => array (
  227. 'arguments' => array('node'=> null),
  228. 'template' => 'tripal_organism_libraries',
  229. ),
  230. 'tripal_feature_libraries' => array (
  231. 'arguments' => array('node'=> null),
  232. 'template' => 'tripal_feature_libraries',
  233. )
  234. );
  235. }
  236. /************************************************************************
  237. * This function is an extension of the chado_feature_view and
  238. * chado_organism_view by providing the markup for the library object
  239. * THAT WILL BE INDEXED.
  240. */
  241. function theme_tripal_library_search_index ($node) {
  242. if ($node->type == 'chado_organism') {
  243. $content = "";
  244. // get the libraries for the organism
  245. $previous_db = tripal_db_set_active('chado');
  246. $sql = "SELECT * FROM {library} L ".
  247. "WHERE L.Organism_id = $node->organism_id";
  248. $libraries = array();
  249. $results = db_query($sql);
  250. while($library = db_fetch_object($results)){
  251. // get the description
  252. $sql = "SELECT * FROM {libraryprop} LP ".
  253. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
  254. "WHERE LP.library_id = $library->library_id ".
  255. " AND CVT.name = 'library_description'";
  256. $desc = db_fetch_object(db_query($sql));
  257. $library->description = $desc->value;
  258. $libraries[] = $library;
  259. }
  260. tripal_db_set_active($previous_db);
  261. if(count($libraries) > 0){
  262. foreach ($libraries as $library){
  263. $content .= "$library->name ";
  264. $content .= "$library->description";
  265. };
  266. }
  267. // Provide library names to show in a feature page
  268. } else if ($node->type == 'chado_feature') {
  269. $content = "";
  270. $organism_id = $node->feature->organism_id;
  271. $previous_db = tripal_db_set_active('chado');
  272. $sql = "SELECT * FROM {library} L ".
  273. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  274. "WHERE LF.feature_id = " . $node->feature->feature_id;
  275. $libraries = array();
  276. $results = db_query($sql);
  277. while($library = db_fetch_object($results)){
  278. $libraries[] = $library;
  279. }
  280. tripal_db_set_active($previous_db);
  281. if(count($libraries) > 0){
  282. $lib_additions = array();
  283. foreach ($libraries as $library){
  284. $content .= $library->name;
  285. };
  286. }
  287. }
  288. return $content;
  289. }
  290. /*******************************************************************************
  291. * This function shows library information on an organism/feature node
  292. */
  293. function theme_tripal_library_node_libraries($node) {
  294. $content = "";
  295. // Show library information in a expandable box for a organism page.
  296. // Make sure we have $node->organism_id. In the case of creating a new
  297. // organism, the organism_id is not created until we save. This will cause
  298. // an error when users preview the creation without a $node->organism_id
  299. if ($node->type == 'chado_organism' && $node->organism_id) {
  300. $box_status = variable_get("tripal_library-box-libraries","menu_off");
  301. if(strcmp($box_status,"menu_off")==0){
  302. return get_tripal_library_organism_libraries($node->nid);
  303. }
  304. }
  305. // Provide library names to show in a feature page.
  306. // Make sure we have $node->feature->feature_id or there will be an error
  307. // when a feature is previewed at its creation
  308. else if ($node->type == 'chado_feature' && $node->feature->feature_id) {
  309. $organism_id = $node->feature->organism_id;
  310. $previous_db = tripal_db_set_active('chado');
  311. $sql = "SELECT * FROM {library} L ".
  312. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  313. "WHERE LF.feature_id = " . $node->feature->feature_id;
  314. $libraries = array();
  315. $results = db_query($sql);
  316. while($library = db_fetch_object($results)){
  317. $libraries[] = $library;
  318. }
  319. tripal_db_set_active($previous_db);
  320. if(count($libraries) > 0){
  321. $lib_additions = array();
  322. foreach ($libraries as $library){
  323. $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
  324. $lib_nid = db_result(db_query($sql, $library->library_id));
  325. if ($lib_nid) {
  326. $lib_url = url("node/$lib_nid");
  327. }
  328. $lib_additions[$lib_url] = $library->name;
  329. };
  330. $node->lib_additions = $lib_additions;
  331. }
  332. }
  333. return $content;
  334. }
  335. /************************************************************************
  336. *
  337. */
  338. function get_tripal_library_admin_form_menu_set(&$form){
  339. $form['menu'] = array(
  340. '#type' => 'fieldset',
  341. '#title' => t('Set Menu')
  342. );
  343. $types = array(
  344. 'tripal_organism' => t('Organism Page'),
  345. 'tripal_feature' => t('Feature Page'),
  346. );
  347. $defaults = variable_get('tripal_lib_menu_node_types','');
  348. $form['menu']['tripal_lib_menu_node_types'] = array(
  349. '#title' => 'Page Types',
  350. '#type' => 'checkboxes',
  351. '#description' => t("Libraries can be associated with other data types ".
  352. "such as organisms, features, analyses, etc. The library module ".
  353. "will add to the respective pages a box or menu item with a ".
  354. "list of libraries associated with the data type. This list ".
  355. "will appear in a box on the page by default. To place this ".
  356. "list as a menu item rather than a box, check the appropriate boxes ".
  357. "above"),
  358. '#options' => $types,
  359. '#default_value' => $defaults,
  360. '#weight' => 1,
  361. );
  362. }
  363. /************************************************************************
  364. *
  365. */
  366. function get_tripal_library_admin_form_cleanup_set(&$form) {
  367. $form['cleanup'] = array(
  368. '#type' => 'fieldset',
  369. '#title' => t('Clean Up')
  370. );
  371. $form['cleanup']['description'] = array(
  372. '#type' => 'item',
  373. '#value' => t("With Drupal and chado residing in different databases ".
  374. "it is possible that nodes in Drupal and libraries in Chado become ".
  375. "\"orphaned\". This can occur if an library node in Drupal is ".
  376. "deleted but the corresponding chado library is not and/or vice ".
  377. "versa. Click the button below to resolve these discrepancies."),
  378. '#weight' => 1,
  379. );
  380. $form['cleanup']['button'] = array(
  381. '#type' => 'submit',
  382. '#value' => t('Clean up orphaned libraries'),
  383. '#weight' => 2,
  384. );
  385. }
  386. /************************************************************************
  387. *
  388. */
  389. function get_tripal_library_admin_form_taxonomy_set(&$form) {
  390. $form['taxonify'] = array(
  391. '#type' => 'fieldset',
  392. '#title' => t('Assign Drupal Taxonomy to Library Features')
  393. );
  394. // get the list of libraries
  395. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  396. $previous_db = tripal_db_set_active('chado'); // use chado database
  397. $lib_rset = db_query($sql);
  398. tripal_db_set_active($previous_db); // now use drupal database
  399. // iterate through all of the libraries
  400. $lib_boxes = array();
  401. while($library = db_fetch_object($lib_rset)){
  402. $lib_boxes[$library->library_id] = "$library->name";
  403. }
  404. $form['taxonify']['description'] = array(
  405. '#type' => 'item',
  406. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  407. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  408. "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."),
  409. '#weight' => 1,
  410. );
  411. $form['taxonify']['tx-libraries'] = array (
  412. '#title' => t('Libraries'),
  413. '#type' => t('checkboxes'),
  414. '#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."),
  415. '#required' => FALSE,
  416. '#prefix' => '<div id="lib_boxes">',
  417. '#suffix' => '</div>',
  418. '#options' => $lib_boxes,
  419. '#weight' => 2
  420. );
  421. $form['taxonify']['tx-button'] = array(
  422. '#type' => 'submit',
  423. '#value' => t('Set Feature Taxonomy'),
  424. '#weight' => 3
  425. );
  426. }
  427. /************************************************************************
  428. *
  429. */
  430. function get_tripal_library_admin_form_reindex_set(&$form) {
  431. // define the fieldsets
  432. $form['reindex'] = array(
  433. '#type' => 'fieldset',
  434. '#title' => t('Reindex Library Features')
  435. );
  436. // get the list of libraries
  437. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  438. $previous_db = tripal_db_set_active('chado'); // use chado database
  439. $lib_rset = db_query($sql);
  440. tripal_db_set_active($previous_db); // now use drupal database
  441. // iterate through all of the libraries
  442. $lib_boxes = array();
  443. while($library = db_fetch_object($lib_rset)){
  444. $lib_boxes[$library->library_id] = "$library->name";
  445. }
  446. $form['reindex']['description'] = array(
  447. '#type' => 'item',
  448. '#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."),
  449. '#weight' => 1,
  450. );
  451. $form['reindex']['re-libraries'] = array (
  452. '#title' => t('Libraries'),
  453. '#type' => t('checkboxes'),
  454. '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
  455. '#required' => FALSE,
  456. '#prefix' => '<div id="lib_boxes">',
  457. '#suffix' => '</div>',
  458. '#options' => $lib_boxes,
  459. '#weight' => 2,
  460. );
  461. $form['reindex']['re-button'] = array(
  462. '#type' => 'submit',
  463. '#value' => t('Reindex Features'),
  464. '#weight' => 3,
  465. );
  466. }
  467. /************************************************************************
  468. *
  469. */
  470. function get_tripal_library_admin_form_sync_set (&$form) {
  471. // define the fieldsets
  472. $form['sync'] = array(
  473. '#type' => 'fieldset',
  474. '#title' => t('Sync Libraries')
  475. );
  476. // get the list of libraries
  477. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  478. $previous_db = tripal_db_set_active('chado'); // use chado database
  479. $lib_rset = db_query($sql);
  480. tripal_db_set_active($previous_db); // now use drupal database
  481. // if we've added any libraries to the list that can be synced
  482. // then we want to build the form components to allow the user
  483. // to select one or all of them. Otherwise, just present
  484. // a message stating that all libraries are currently synced.
  485. $lib_boxes = array();
  486. $added = 0;
  487. while($library = db_fetch_object($lib_rset)){
  488. // check to see if the library is already present as a node in drupal.
  489. // if so, then skip it.
  490. $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
  491. if(!db_fetch_object(db_query($sql,$library->library_id))){
  492. $lib_boxes[$library->library_id] = "$library->name";
  493. $added++;
  494. }
  495. }
  496. // if we have libraries we need to add to the checkbox then
  497. // build that form element
  498. if($added > 0){
  499. $lib_boxes['all'] = "All Libraries";
  500. $form['reindex']['description'] = array(
  501. '#type' => 'item',
  502. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  503. '#weight' => 1,
  504. );
  505. $form['sync']['libraries'] = array (
  506. '#title' => t('Available Libraries'),
  507. '#type' => t('checkboxes'),
  508. '#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."),
  509. '#required' => FALSE,
  510. '#prefix' => '<div id="lib_boxes">',
  511. '#suffix' => '</div>',
  512. '#options' => $lib_boxes,
  513. '#weight' => 2,
  514. );
  515. $form['sync']['button'] = array(
  516. '#type' => 'submit',
  517. '#value' => t('Sync Libraries'),
  518. '#weight' => 3,
  519. );
  520. }
  521. // we don't have any libraries to select from
  522. else {
  523. $form['sync']['value'] = array(
  524. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  525. );
  526. }
  527. }
  528. /************************************************************************
  529. *
  530. */
  531. function tripal_library_admin_validate($form, &$form_state) {
  532. global $user; // we need access to the user info
  533. $job_args = array();
  534. // Submit the Sync Job if selected
  535. if ($form_state['values']['op'] == t('Sync Libraries')) {
  536. // check to see if the user wants to sync chado and drupal. If
  537. // so then we need to register a job to do so with tripal
  538. $libraries = $form_state['values']['libraries'];
  539. $do_all = FALSE;
  540. $to_sync = array();
  541. foreach ($libraries as $library_id){
  542. if(preg_match("/^all$/i",$library_id)){
  543. $do_all = TRUE;
  544. }
  545. if($library_id and preg_match("/^\d+$/i",$library_id)){
  546. // get the library info
  547. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  548. $previous_db = tripal_db_set_active('chado'); // use chado database
  549. $library = db_fetch_object(db_query($sql,$library_id));
  550. tripal_db_set_active($previous_db); // now use drupal database
  551. $to_sync[$library_id] = $library->name;
  552. }
  553. }
  554. // submit the job to the tripal job manager
  555. if($do_all){
  556. tripal_add_job('Sync all libraries','tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  557. }
  558. else{
  559. foreach($to_sync as $library_id => $name){
  560. $job_args[0] = $library_id;
  561. tripal_add_job("Sync library: $name",'tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  562. }
  563. }
  564. }
  565. // -------------------------------------
  566. // Submit the Reindex Job if selected
  567. if ($form_state['values']['op'] == t('Reindex Features')) {
  568. $libraries = $form_state['values']['re-libraries'];
  569. foreach ($libraries as $library_id){
  570. if($library_id and preg_match("/^\d+$/i",$library_id)){
  571. // get the library info
  572. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  573. $previous_db = tripal_db_set_active('chado'); // use chado database
  574. $library = db_fetch_object(db_query($sql,$library_id));
  575. tripal_db_set_active($previous_db); // now use drupal database
  576. $job_args[0] = $library_id;
  577. tripal_add_job("Reindex features for library: $library->name",'tripal_library',
  578. 'tripal_library_reindex_features',$job_args,$user->uid);
  579. }
  580. }
  581. }
  582. // -------------------------------------
  583. // Submit the Taxonomy Job if selected
  584. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  585. $libraries = $form_state['values']['tx-libraries'];
  586. foreach ($libraries as $library_id){
  587. if($library_id and preg_match("/^\d+$/i",$library_id)){
  588. // get the library info
  589. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  590. $previous_db = tripal_db_set_active('chado'); // use chado database
  591. $library = db_fetch_object(db_query($sql,$library_id));
  592. tripal_db_set_active($previous_db); // now use drupal database
  593. $job_args[0] = $library_id;
  594. tripal_add_job("Set taxonomy for features in library: $library->name",'tripal_library',
  595. 'tripal_library_taxonify_features',$job_args,$user->uid);
  596. }
  597. }
  598. }
  599. // -------------------------------------
  600. // Submit the Cleanup Job if selected
  601. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  602. tripal_add_job('Cleanup orphaned libraries','tripal_library',
  603. 'tripal_library_cleanup',$job_args,$user->uid);
  604. }
  605. }
  606. /*******************************************************************************
  607. *
  608. */
  609. function tripal_library_show_libraries (){
  610. // Show libraries stored in Drupal's {chado_library} table
  611. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  612. $no_libs = db_result(db_query ($sql));
  613. if($no_libs != 0) {
  614. $libraries = get_chado_libraries ();
  615. if($no_libs != count($libraries)) {
  616. drupal_set_message("Synchronization needed.");
  617. }
  618. return theme('tripal_library_library_table', &$libraries);
  619. } else {
  620. return t("No library exists. Please contact administrators to ".
  621. "synchronize libraries.");
  622. }
  623. }
  624. /*******************************************************************************
  625. *
  626. */
  627. function tripal_library_cron (){
  628. }
  629. /*******************************************************************************
  630. *
  631. * CHADO_LIBRARY NODE FUNCTIONS
  632. *
  633. * The following function proves access control for users trying to
  634. * perform actions on data managed by this module
  635. */
  636. function tripal_library_library_access($op, $node, $account){
  637. if ($op == 'create') {
  638. return user_access('create chado_library content', $account);
  639. }
  640. if ($op == 'update') {
  641. if (user_access('edit any chado_library content', $account) ||
  642. (user_access('edit own chado_library content', $account) &&
  643. ($account->uid == $node->uid))){
  644. return TRUE;
  645. }
  646. }
  647. if ($op == 'delete') {
  648. if (user_access('delete any chado_library content', $account) ||
  649. (user_access('delete own chado_library content', $account) &&
  650. ($account->uid == $node->uid))) {
  651. return TRUE;
  652. }
  653. }
  654. }
  655. /*******************************************************************************
  656. * validates submission of form when adding or updating a library node
  657. */
  658. function chado_library_validate($node){
  659. $lib = 0;
  660. // check to make sure the unique name on the library is unique
  661. // before we try to insert into chado.
  662. if($node->library_id){
  663. $sql = "SELECT * FROM ".
  664. "{Library} WHERE ".
  665. "uniquename = '%s' ".
  666. "AND NOT library_id = %d";
  667. $previous_db = tripal_db_set_active('chado');
  668. $lib = db_fetch_object(db_query($sql, $node->uniquename,
  669. $node->library_id));
  670. tripal_db_set_active($previous_db);
  671. } else {
  672. $sql = "SELECT * FROM ".
  673. "{Library} ".
  674. "WHERE uniquename = '%s'";
  675. $previous_db = tripal_db_set_active('chado');
  676. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  677. tripal_db_set_active($previous_db);
  678. }
  679. if($lib){
  680. form_set_error('uniquename',t('The unique library name already exists. '.
  681. 'Please choose another'));
  682. }
  683. }
  684. /*******************************************************************************
  685. * When a new chado_library node is created we also need to add information
  686. * to our chado_library table. This function is called on insert of a new node
  687. * of type 'chado_library' and inserts the necessary information.
  688. */
  689. function chado_library_insert($node){
  690. // If this library already exists then don't create it in chado
  691. // e.g. when tripal_library_sync_libraries call this function
  692. $l_sql= "SELECT library_id ".
  693. "FROM {Library} ".
  694. "WHERE organism_id = %d ".
  695. "AND uniquename = '%s' ".
  696. "AND type_id = %d";
  697. $previous_db = tripal_db_set_active('chado');
  698. $library = db_fetch_object(db_query($l_sql, $node->organism_id,
  699. $node->uniquename, $node->type_id));
  700. $libid = $node->library_id;
  701. // Create the chado library entry only if it doesn't exist
  702. if (!$library) {
  703. $sql = "INSERT INTO {library} (organism_id, name, uniquename, type_id)".
  704. "VALUES(%d,'%s','%s', ".
  705. " (SELECT cvterm_id ".
  706. " FROM {CVTerm} CVT ".
  707. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  708. " WHERE CV.name = 'tripal' and CVT.name = '%s'))";
  709. db_query($sql,$node->organism_id,$node->title,$node->uniquename,
  710. $node->library_type);
  711. // get the library id for this library
  712. $sql = "SELECT library_id ".
  713. "FROM {library} ".
  714. "WHERE uniquename = '%s'";
  715. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  716. $libid = $lib->library_id;
  717. }
  718. // Now use drupal database
  719. tripal_db_set_active($previous_db);
  720. // Next add the node to the drupal table
  721. $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
  722. "VALUES (%d, %d, %d)";
  723. db_query($sql, $node->nid, $node->vid, $libid);
  724. // Finally add the remaining properties if the library doesn't exist in Chado
  725. if (!$library) {
  726. tripal_library_add_properties($node,$libid);
  727. }
  728. }
  729. /*******************************************************************************
  730. * Update nodes
  731. */
  732. function chado_library_update($node){
  733. if($node->revision){
  734. // TODO -- decide what to do about revisions
  735. } else {
  736. // Get the library_id for this node:
  737. $sql = "SELECT library_id ".
  738. "FROM {chado_library} ".
  739. "WHERE vid = %d";
  740. $lib = db_fetch_object(db_query($sql, $node->vid));
  741. // Update the chado library table
  742. $sql = "UPDATE {library} ".
  743. " SET name = '%s', uniquename = '%s', organism_id = %d, ".
  744. " type_id = (SELECT cvterm_id ".
  745. " FROM {CVTerm} CVT ".
  746. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  747. " WHERE CV.name = 'local' and CVT.name = '%s') ".
  748. "WHERE library_id = %d ";
  749. $previous_db = tripal_db_set_active('chado'); // use chado database
  750. db_query($sql, $node->title, $node->uniquename, $node->organism_id,
  751. $node->library_type, $lib->library_id);
  752. tripal_db_set_active($previous_db); // now use drupal database
  753. tripal_library_add_properties($node,$lib->library_id);
  754. tripal_library_add_taxonomy($node,$lib->library_id);
  755. }
  756. }
  757. /*******************************************************************************
  758. * Add properties to the specified library.
  759. */
  760. function tripal_library_add_properties ($node,$library_id){
  761. // Use chado database
  762. $previous_db = tripal_db_set_active('chado');
  763. // Now remove any properties for this library and add the new properties
  764. // provided by the this posting.
  765. $sql = "DELETE FROM {libraryprop} ".
  766. "WHERE library_id = %d";
  767. db_query($sql,$library_id);
  768. // Now add each property one at a time
  769. $sql = "INSERT INTO {libraryprop} (library_id, type_id, value) ".
  770. "VALUES (%d,".
  771. " (SELECT cvterm_id ".
  772. " FROM {CVTerm} CVT ".
  773. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  774. " WHERE CV.name = 'tripal' and CVT.name = '%s'), ".
  775. " '%s')";
  776. if($node->library_description){
  777. db_query($sql,$library_id,'library_description',
  778. $node->library_description);
  779. }
  780. // now use drupal database
  781. tripal_db_set_active($previous_db);
  782. }
  783. /*******************************************************************************
  784. * Add the library as a taxonomy term for associating with library_features
  785. */
  786. function tripal_library_add_taxonomy ($node,$library_id){
  787. //include the file containing the required functions. We only have to
  788. // do this because Drupal 6 fails to do this globally for us and
  789. // the drupal_execute function below won't work
  790. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  791. /* // get the vocabulary id
  792. $vocabularies = taxonomy_get_vocabularies();
  793. $vid = NULL;
  794. foreach($vocabularies as $vocab){
  795. if($vocab->name == 'DNA Libraries'){
  796. $vid = $vocab->vid;
  797. }
  798. }
  799. if(!$vid){ */
  800. // add the vocabulary
  801. $vocab_form['values']['name'] = 'DNA Libraries';
  802. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  803. $vocab_form['values']['help'] = '';
  804. $vocab_form['values']['module'] = 'taxonomy';
  805. drupal_execute('taxonomy_form_vocabulary',$vocab_form);
  806. return;
  807. // }
  808. // make sure this term doesn't already exist. If it doesn't then add it
  809. if($vid){
  810. $tree = taxonomy_get_tree($vid);
  811. $found = 0;
  812. foreach ($tree as $term) {
  813. if($term->name == $node->title){
  814. $found = 1;
  815. }
  816. }
  817. // add the term to the vocabulary
  818. if(!$found){
  819. $form_state = array();
  820. $form_state['values']['name'] = $node->title;
  821. $form_state['values']['description'] = $library_id;
  822. drupal_execute('taxonomy_form_term',$form_state,$vid);
  823. }
  824. }
  825. }
  826. /*******************************************************************************
  827. * When editing or creating a new node of type 'chado_library' we need
  828. * a form. This function creates the form that will be used for this.
  829. */
  830. function chado_library_form ($node){
  831. $type = node_get_types('type',$node);
  832. $form = array();
  833. // keep track of the library id if we have. If we do have one then
  834. // this is an update as opposed to an insert.
  835. $form['library_id'] = array(
  836. '#type' => 'value',
  837. '#value' => $node->library_id,
  838. );
  839. $form['title']= array(
  840. '#type' => 'textfield',
  841. '#title' => t('Library Title'),
  842. '#description' => t('Please enter the title for this library. '.
  843. 'This appears at the top of the library page.'),
  844. '#required' => TRUE,
  845. '#default_value' => $node->title,
  846. '#weight' => 1
  847. );
  848. $form['uniquename']= array(
  849. '#type' => 'textfield',
  850. '#title' => t('Unique Library Name'),
  851. '#description' => t('Please enter a unique name for this library'),
  852. '#required' => TRUE,
  853. '#default_value' => $node->uniquename,
  854. '#weight' => 2
  855. );
  856. // These library types should not be hard coded, but for now the are...
  857. $library_types = array();
  858. $library_types[''] = '';
  859. $library_types['cdna_library'] = 'cDNA Library';
  860. $library_types['bac_library'] = 'BAC Library';
  861. $library_types['fosmid_library'] = 'FOSMID Library';
  862. $library_types['cosmid_library'] = 'COSMID Library';
  863. $library_types['yac_library'] = 'YAC Library';
  864. $form['library_type'] = array (
  865. '#title' => t('Library Type'),
  866. '#type' => t('select'),
  867. '#description' => t("Choose the library type."),
  868. '#required' => TRUE,
  869. '#default_value' => $node->library_type,
  870. '#options' => $library_types,
  871. '#weight' => 3
  872. );
  873. // get the list of organisms
  874. $sql = "SELECT * FROM {Organism}";
  875. $previous_db = tripal_db_set_active('chado'); // use chado database
  876. $org_rset = db_query($sql);
  877. tripal_db_set_active($previous_db); // now use drupal database
  878. $organisms = array();
  879. $organisms[''] = '';
  880. while($organism = db_fetch_object($org_rset)){
  881. $organisms[$organism->organism_id] =
  882. "$organism->genus $organism->species ($organism->common_name)";
  883. }
  884. $form['organism_id'] = array (
  885. '#title' => t('Organism'),
  886. '#type' => t('select'),
  887. '#description' => t("Choose the organism with which this library is ".
  888. "associated."),
  889. '#required' => TRUE,
  890. '#default_value' => $node->organism_id,
  891. '#options' => $organisms,
  892. '#weight' => 4,
  893. );
  894. $form['library_description']= array(
  895. '#type' => 'textarea',
  896. '#title' => t('Library Description'),
  897. '#description' => t('A brief description of the library'),
  898. '#required' => TRUE,
  899. '#default_value' => $node->library_description,
  900. '#weight' => 5
  901. );
  902. //Save the number of EST so it will show up in preview
  903. $form['sequence_num'] = array(
  904. '#type' => 'value',
  905. '#value' => $node->sequence_num,
  906. );
  907. return $form;
  908. }
  909. /************************************************************************
  910. *
  911. */
  912. function tripal_library_sync_libraries ($library_id = NULL, $job_id = NULL){
  913. global $user;
  914. $page_content = '';
  915. // get the list of libraries and create new nodes
  916. if(!$library_id){
  917. $sql = "SELECT * FROM {Library} L";
  918. $previous_db = tripal_db_set_active('chado'); // use chado database
  919. $results = db_query($sql);
  920. tripal_db_set_active($previous_db); // now use drupal database
  921. } else {
  922. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  923. $previous_db = tripal_db_set_active('chado'); // use chado database
  924. $results = db_query($sql,$library_id);
  925. tripal_db_set_active($previous_db); // now use drupal database
  926. }
  927. // We'll use the following SQL statement for checking if the library
  928. // already exists as a drupal node.
  929. $sql = "SELECT * FROM {chado_library} ".
  930. "WHERE library_id = %d";
  931. while($library = db_fetch_object($results)){
  932. // check if this library already exists in the drupal database. if it
  933. // does then skip this library and go to the next one.
  934. if(!db_fetch_object(db_query($sql,$library->library_id))){
  935. $new_node = new stdClass();
  936. $new_node->type = 'chado_library';
  937. $new_node->uid = $user->uid;
  938. $new_node->title = "$library->name";
  939. $new_node->library_id = $library->library_id;
  940. $new_node->organism_id = $library->organism_id;
  941. $new_node->uniquename = $library->uniquename;
  942. $new_node->type_id = $library->type_id;
  943. node_validate($new_node);
  944. if(!form_get_errors()){
  945. $node = node_submit($new_node);
  946. node_save($node);
  947. if($node->nid){
  948. $page_content .= "Added $library->name<br>";
  949. }
  950. }
  951. } else {
  952. $page_content .= "Skipped $library->name<br>";
  953. }
  954. }
  955. return $page_content;
  956. }
  957. /************************************************************************
  958. * When a node is requested by the user this function is called to allow us
  959. * to add auxiliary data to the node object.
  960. */
  961. function chado_library_load($node){
  962. $sql = 'SELECT library_id FROM {chado_library} WHERE vid = %d';
  963. $map = db_fetch_object(db_query($sql, $node->vid));
  964. $additions->library_id = $map->library_id;
  965. $previous_db = tripal_db_set_active('chado'); // use chado database
  966. // get information about this library
  967. $sql = "SELECT L.organism_id, L.uniquename, CVT.name as type_name ".
  968. "FROM {Library} L ".
  969. "INNER JOIN CVTerm CVT ON CVT.cvterm_id = L.type_id ".
  970. "WHERE library_id = %d";
  971. $library = db_fetch_object(db_query($sql, $map->library_id));
  972. $additions->uniquename = $library->uniquename;
  973. $additions->library_type = $library->type_name;
  974. $additions->organism_id = $library->organism_id;
  975. $sql = "SELECT genus, species, common_name ".
  976. "FROM {organism} O ".
  977. "INNER JOIN library L ".
  978. "ON L.organism_id = O.organism_id ".
  979. "WHERE L.library_id = %d";
  980. $organism = db_fetch_object(db_query($sql,$map->library_id));
  981. $additions->genus = $organism->genus;
  982. $additions->species = $organism->species;
  983. $additions->common_name = $organism->common_name;
  984. $sql = "SELECT LP.value ".
  985. "FROM {libraryProp} LP ".
  986. "WHERE LP.library_id = %d ";
  987. $desc = db_fetch_object(db_query($sql, $map->library_id))->value;
  988. if ($desc) {
  989. $additions->library_description = $desc;
  990. } else {
  991. $additions->library_description = "NA";
  992. }
  993. // get the feature counts. This is dependent on a materialized view
  994. // installed with the library module
  995. if ($map->library_id) {
  996. $sql = "SELECT num_features FROM {library_feature_count} ".
  997. "WHERE Library_id = $map->library_id";
  998. $additions->num_features = db_fetch_object(db_query($sql, $map->library_id))->num_features;
  999. }
  1000. tripal_db_set_active($previous_db); // now use drupal database
  1001. return $additions;
  1002. }
  1003. /*******************************************************************************
  1004. * This function customizes the view of the chado_library node. It allows
  1005. * us to generate the markup. This function is required for node [Preview]
  1006. */
  1007. function chado_library_view ($node, $teaser = FALSE, $page = FALSE) {
  1008. // use drupal's default node view:
  1009. if (!$teaser) {
  1010. $node = node_prepare($node, $teaser);
  1011. // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
  1012. // but not genus/species/common_name. We need to get those from chado
  1013. // database so they will show up in preview
  1014. if(!$node->genus) {
  1015. $previous_db = tripal_db_set_active('chado');
  1016. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  1017. $data = db_fetch_object(db_query($sql, $node->organism_id));
  1018. $node->genus = $data->genus;
  1019. $node->species = $data->species;
  1020. $node->common_name = $data->common_name;
  1021. tripal_db_set_active($previous_db);
  1022. }
  1023. }
  1024. return $node;
  1025. }
  1026. /************************************************************************
  1027. ** This function creates the html markup for the library table.
  1028. ** It can be overridden in the theme for the site by adding a php
  1029. ** method in the template.php file named
  1030. ** <theme_name>_tripal_library_library_table(&$libraries)
  1031. */
  1032. function theme_tripal_library_library_table ($libraries) {
  1033. // cycle through the libraries and build the libraries page
  1034. $output = "<div id=\"libraries\">";
  1035. $output .= '<table>';
  1036. $output .= "<tr>";
  1037. $output .= "<th>Name</th>";
  1038. $output .= "<th>Type</th>";
  1039. $output .= "<th>Organism</th>";
  1040. $output .= "<th>Description</th>";
  1041. $output .= "</tr>";
  1042. foreach($libraries as $library){
  1043. $lib_url = url("node/$library->node_id");
  1044. $output .= "<tr>";
  1045. $output .= "<td>".
  1046. " <a href=\"$lib_url\">$library->name</a>".
  1047. "</td>";
  1048. if(strcasecmp($library->type_name, 'bac_library') == 0){
  1049. $output .= "<td>BAC</td>";
  1050. }
  1051. elseif(strcasecmp($library->type_name, 'cdna_library') == 0){
  1052. $output .= "<td>cDNA</td>";
  1053. }
  1054. else{
  1055. $output .= "<td>$library->type_name</td>";
  1056. }
  1057. $output .= "<td nowrap>".
  1058. " $library->common_name".
  1059. "</td>";
  1060. $description = $library->library_description;
  1061. $output .= "<td>$description</td>";
  1062. $output .= "</tr>";
  1063. }
  1064. $output .= "</table>";
  1065. $output .= "</div>";
  1066. return $output;
  1067. }
  1068. /************************************************************************
  1069. *
  1070. */
  1071. function tripal_library_feature_set_taxonomy($library_id = NULL){
  1072. //TO DO : return usable error if vocabs don't exist
  1073. // get the list of vocabularies and find our two vocabularies of interest
  1074. $vocabularies = taxonomy_get_vocabularies();
  1075. $vid = NULL;
  1076. foreach($vocabularies as $vocab){
  1077. if($vocab->name == 'Library'){
  1078. $vid = $vocab->vid;
  1079. }
  1080. }
  1081. if(!$vid){
  1082. return;
  1083. }
  1084. // We'll use the following SQL statement for getting the node info
  1085. if($library_id){
  1086. print "Finding features for library with ID: $library_id\n";
  1087. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1088. "FROM {library_feature} LF ".
  1089. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1090. "WHERE L.library_id = $library_id ".
  1091. "ORDER BY LF.feature_id";
  1092. $previous_db = tripal_db_set_active('chado'); // use chado database
  1093. $features = db_query($sql);
  1094. tripal_db_set_active($previous_db); // now use drupal database
  1095. } else {
  1096. print "Finding features for all libraries\n";
  1097. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1098. "FROM {library_feature} LF ".
  1099. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1100. "ORDER BY LF.feature_id";
  1101. $previous_db = tripal_db_set_active('chado'); // use chado database
  1102. $features = db_query($sql);
  1103. tripal_db_set_active($previous_db); // now use drupal database
  1104. }
  1105. $node_sql = "SELECT * FROM {chado_feature} CF ".
  1106. " INNER JOIN {node} N ON CF.nid = N.nid ".
  1107. "WHERE feature_id = %d";
  1108. // iterate through the features and add the taxonomy
  1109. while($feature = db_fetch_object($features)){
  1110. $node = db_fetch_object(db_query($node_sql,$feature->feature_id));
  1111. $tags["$vid"] = $feature->libname;
  1112. $terms['tags'] = $tags;
  1113. taxonomy_node_save($node,$terms);
  1114. print "Updated $feature->feature_id as $feature->libname\n";
  1115. }
  1116. }
  1117. /*******************************************************************************
  1118. *
  1119. */
  1120. function tripal_library_reindex_features ($library_id = NULL, $job_id = NULL){
  1121. $i = 0;
  1122. // if the caller provided a library_id then get all of the features
  1123. // associated with the library. Otherwise get all sequences assoicated
  1124. // with all libraries.
  1125. if($library_id){
  1126. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1127. " FROM {library_feature} LF ".
  1128. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1129. "WHERE L.library_id = $library_id ".
  1130. "ORDER BY LF.feature_id";
  1131. $previous_db = tripal_db_set_active('chado'); // use chado database
  1132. $results = db_query($sql);
  1133. tripal_db_set_active($previous_db); // now use drupal database
  1134. }
  1135. else {
  1136. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1137. " FROM {library_feature} LF ".
  1138. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1139. "ORDER BY LF.feature_id";
  1140. $previous_db = tripal_db_set_active('chado'); // use chado database
  1141. $results = db_query($sql);
  1142. tripal_db_set_active($previous_db); // now use drupal database
  1143. }
  1144. // load into ids array
  1145. $count = 0;
  1146. $ids = array();
  1147. while($id = db_fetch_object($results)){
  1148. $ids[$count] = $id->feature_id;
  1149. $count++;
  1150. }
  1151. $interval = intval($count * 0.01);
  1152. foreach($ids as $feature_id){
  1153. // update the job status every 1% features
  1154. if($job_id and $i % interval == 0){
  1155. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1156. }
  1157. tripal_feature_sync_feature ($feature_id);
  1158. $i++;
  1159. }
  1160. }
  1161. /*******************************************************************************
  1162. *
  1163. */
  1164. function tripal_library_taxonify_features ($library_id = NULL, $job_id = NULL){
  1165. $i = 0;
  1166. // if the caller provided a library_id then get all of the features
  1167. // associated with the library. Otherwise get all sequences assoicated
  1168. // with all libraries.
  1169. if($library_id){
  1170. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1171. " FROM {library_feature} LF ".
  1172. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1173. "WHERE L.library_id = $library_id ".
  1174. "ORDER BY LF.feature_id";
  1175. $previous_db = tripal_db_set_active('chado'); // use chado database
  1176. $results = db_query($sql);
  1177. tripal_db_set_active($previous_db); // now use drupal database
  1178. }
  1179. else {
  1180. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1181. " FROM {library_feature} LF ".
  1182. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1183. "ORDER BY LF.feature_id";
  1184. $previous_db = tripal_db_set_active('chado'); // use chado database
  1185. $results = db_query($sql);
  1186. tripal_db_set_active($previous_db); // now use drupal database
  1187. }
  1188. // load into ids array
  1189. $count = 0;
  1190. $ids = array();
  1191. while($id = db_fetch_object($results)){
  1192. $ids[$count] = $id->feature_id;
  1193. $count++;
  1194. }
  1195. // make sure our vocabularies are set before proceeding
  1196. tripal_feature_set_vocabulary();
  1197. // use this SQL for getting the nodes
  1198. $nsql = "SELECT * FROM {chado_feature} CF ".
  1199. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1200. "WHERE feature_id = %d";
  1201. // iterate through the features and set the taxonomy
  1202. $interval = intval($count * 0.01);
  1203. foreach($ids as $feature_id){
  1204. // update the job status every 1% features
  1205. if($job_id and $i % interval == 0){
  1206. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1207. }
  1208. $node = db_fetch_object(db_query($nsql,$feature_id));
  1209. tripal_feature_set_taxonomy($node,$feature_id);
  1210. $i++;
  1211. }
  1212. }
  1213. /*******************************************************************************
  1214. * Delete data from drupal and chado databases when a node is deleted
  1215. */
  1216. function chado_library_delete(&$node){
  1217. // Before removing, get library_id so we can remove it from chado database
  1218. // later
  1219. $sql_drupal = "SELECT library_id ".
  1220. "FROM {chado_library} ".
  1221. "WHERE nid = %d AND vid = %d";
  1222. $library_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  1223. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  1224. // drupal database
  1225. $sql_del = "DELETE FROM {chado_library} ".
  1226. "WHERE nid = %d ".
  1227. "AND vid = %d";
  1228. db_query($sql_del, $node->nid, $node->vid);
  1229. $sql_del = "DELETE FROM {node} ".
  1230. "WHERE nid = %d ".
  1231. "AND vid = %d";
  1232. db_query($sql_del, $node->nid, $node->vid);
  1233. $sql_del = "DELETE FROM {node_revisions} ".
  1234. "WHERE nid = %d ".
  1235. "AND vid = %d";
  1236. db_query($sql_del, $node->nid, $node->vid);
  1237. // Remove data from library and libraryprop tables of chado database as well
  1238. $previous_db = tripal_db_set_active('chado');
  1239. db_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
  1240. db_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
  1241. tripal_db_set_active($previous_db);
  1242. }
  1243. /************************************************************************
  1244. * Display block with libraries
  1245. * @param op - parameter to define the phase being called for the block
  1246. * @param delta - id of the block to return (ignored when op is list)
  1247. * @param edit - when op is save, contains the submitted form data
  1248. */
  1249. function tripal_library_block($op = 'list', $delta = '0', $edit = array()){
  1250. switch($op){
  1251. case 'list':
  1252. $blocks[0]['info'] = t('Libraries');
  1253. return $blocks;
  1254. case 'view':
  1255. if(user_access('access chado_library content')){
  1256. // Show libraries stored in Drupal's {chado_library} table
  1257. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  1258. $no_libs = db_result(db_query ($sql));
  1259. if($no_libs != 0) {
  1260. $libraries = get_chado_libraries();
  1261. foreach($libraries as $library){
  1262. // get the node id for this organism
  1263. $items[] = l($library->name, 'node/' . $library->node_id);
  1264. }
  1265. } else {
  1266. $items[] = t("No library exists.");
  1267. }
  1268. $block['subject'] = t('Libraries');
  1269. //We theme our array of links as an unordered list
  1270. $block['content'] = theme('item_list', $items);
  1271. }
  1272. return $block;
  1273. }
  1274. }
  1275. /*******************************************************************************
  1276. * This function uses library_id's of all drupal library nodes as input and
  1277. * pull the library information (name, uniquename, type, genus, species,
  1278. * common_name, description) from chado database. The return type is an object
  1279. * array that stores sorted $library objects
  1280. */
  1281. function get_chado_libraries() {
  1282. $sql_drupal = "SELECT COUNT (library_id) FROM {chado_library}";
  1283. $no_libs = db_result(db_query($sql_drupal));
  1284. if ($no_libs != 0) {
  1285. // Get library_id's from drupal
  1286. $sql = "SELECT library_id, nid FROM {chado_library}";
  1287. $result = db_query($sql);
  1288. $previous_db = tripal_db_set_active('chado');
  1289. // Get library info from chado's library, organism, and cvterm tables
  1290. $sql_info = "SELECT L.name, uniquename, genus, species, common_name, ".
  1291. " CVT.name as type_name ".
  1292. "FROM {Library} L ".
  1293. "INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1294. "INNER JOIN CVTerm CVT ON L.type_id = CVT.cvterm_id ".
  1295. "WHERE library_id=%d";
  1296. // Get library description from libraryprop if there is any
  1297. $sql_desc = "SELECT value ".
  1298. "FROM {Libraryprop} LP ".
  1299. "INNER JOIN CVTerm CVT ON LP.type_id = CVT.cvterm_id ".
  1300. "WHERE library_id = %d AND CVT.name = 'library_description'";
  1301. $libraries = array();
  1302. while ($data = db_fetch_object($result)) {
  1303. $library = db_fetch_object(db_query($sql_info, $data->library_id));
  1304. $library->node_id = $data->nid;
  1305. $desc = db_fetch_object(db_query($sql_desc, $data->library_id))->value;
  1306. if ($desc) {
  1307. $library->library_description = $desc;
  1308. } else {
  1309. $library->library_description = "NA";
  1310. }
  1311. // Use uniquename as the key so we can sort by uniquename later
  1312. $key = strtolower($library->uniquename);
  1313. $libraries [$key] = $library;
  1314. }
  1315. tripal_db_set_active($previous_db);
  1316. //Sort libraries by uniquename
  1317. ksort($libraries, SORT_STRING);
  1318. return $libraries;
  1319. }
  1320. }
  1321. /************************************************************************
  1322. *
  1323. */
  1324. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  1325. // build the SQL statments needed to check if nodes point to valid analyses
  1326. $dsql = "SELECT * FROM {node} WHERE type = 'chado_library' order by nid";
  1327. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1328. $csql = "SELECT * FROM {chado_library} where nid = %d ";
  1329. $cosql= "SELECT * FROM {chado_library}";
  1330. $tsql = "SELECT * FROM {library} L WHERE library_id = %d";
  1331. // load into nodes array
  1332. $results = db_query($dsql);
  1333. $count = 0;
  1334. $nodes = array();
  1335. while($node = db_fetch_object($results)){
  1336. $nodes[$count] = $node;
  1337. $count++;
  1338. }
  1339. // load the chado_analyses into an array
  1340. $results = db_query($cosql);
  1341. $cnodes = array();
  1342. while($node = db_fetch_object($results)){
  1343. $cnodes[$count] = $node;
  1344. $count++;
  1345. }
  1346. $interval = intval($count * 0.01);
  1347. // iterate through all of the chado_library nodes and delete those that aren't valid
  1348. foreach($nodes as $nid){
  1349. // update the job status every 1% analyses
  1350. if($job_id and $i % $interval == 0){
  1351. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1352. }
  1353. // first check to see if the node has a corresponding entry
  1354. // in the chado_library table. If not then delete the node.
  1355. $library = db_fetch_object(db_query($csql,$nid->nid));
  1356. if(!$library){
  1357. node_delete($nid->nid);
  1358. $message = "Missing in chado_library table.... DELETING: $nid->nid\n";
  1359. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1360. continue;
  1361. }
  1362. $i++;
  1363. }
  1364. // iterate through all of the chado_library nodes and delete those that aren't valid
  1365. foreach($cnodes as $nid){
  1366. // update the job status every 1% analyses
  1367. if($job_id and $i % $interval == 0){
  1368. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1369. }
  1370. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1371. if(!$node){
  1372. db_query("DELETE FROM {chado_library} WHERE nid = $nid->nid");
  1373. $message = "chado_library missing node.... DELETING: $nid->nid\n";
  1374. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1375. }
  1376. $i++;
  1377. }
  1378. return '';
  1379. }