tripal_library.module 54 KB

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