tripal_library.module 61 KB

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