tripal_library.module 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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_cleanup_set($form);
  261. get_tripal_library_admin_form_reindex_set($form);
  262. get_tripal_library_admin_form_taxonomy_set($form);
  263. get_tripal_library_admin_form_cleanup_set($form);
  264. }
  265. else {
  266. $form['notice'] = array(
  267. '#type' => 'fieldset',
  268. '#title' => t('Library Management Temporarily Unavailable')
  269. );
  270. $form['notice']['message'] = array(
  271. '#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.'),
  272. );
  273. }
  274. return system_settings_form($form);
  275. }
  276. /**
  277. * Implementation of hook_nodeapi().
  278. * Display library information for associated features or organisms
  279. * This function also provides contents for indexing
  280. *
  281. * @ingroup tripal_library
  282. */
  283. function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
  284. switch ($op) {
  285. // Note that this function only adds library view to an organism/feature
  286. // node.
  287. case 'view':
  288. // add the library to the organism/feature search indexing
  289. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  290. $node->content['tripal_library_index_version'] = array(
  291. '#value' => theme('tripal_library_search_index', $node),
  292. );
  293. }
  294. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  295. $node->content['tripal_library_index_version'] = array(
  296. '#value' => theme('tripal_library_search_result', $node),
  297. );
  298. }
  299. else {
  300. switch ($node->type) {
  301. case 'chado_organism':
  302. // Show library if the organism/feature is not at teaser view
  303. $node->content['tripal_organism_libraries'] = array(
  304. '#value' => theme('tripal_organism_libraries', $node),
  305. );
  306. break;
  307. case 'chado_feature':
  308. // Show library if the organism/feature is not at teaser view
  309. $node->content['tripal_feature_libraries'] = array(
  310. '#value' => theme('tripal_feature_libraries', $node),
  311. );
  312. break;
  313. }
  314. }
  315. break;
  316. }
  317. }
  318. /**
  319. * We need to let drupal know about our theme functions and their arguments.
  320. * We create theme functions to allow users of the module to customize the
  321. * look and feel of the output generated in this module
  322. *
  323. * @ingroup tripal_library
  324. */
  325. function tripal_library_theme() {
  326. return array(
  327. 'tripal_library_library_table' => array(
  328. 'arguments' => array('libraries'),
  329. ),
  330. 'tripal_library_search_index' => array(
  331. 'arguments' => array('node'),
  332. ),
  333. 'tripal_library_search_result' => array(
  334. 'arguments' => array('node'),
  335. ),
  336. 'tripal_organism_libraries' => array(
  337. 'arguments' => array('node' => NULL),
  338. 'template' => 'tripal_organism_libraries',
  339. ),
  340. 'tripal_feature_libraries' => array(
  341. 'arguments' => array('node' => NULL),
  342. 'template' => 'tripal_feature_libraries',
  343. ),
  344. 'tripal_library_base' => array(
  345. 'arguments' => array('node' => NULL),
  346. 'template' => 'tripal_library_base',
  347. ),
  348. 'tripal_library_synonyms' => array(
  349. 'arguments' => array('node' => NULL),
  350. 'template' => 'tripal_library_synonyms',
  351. ),
  352. 'tripal_library_references' => array(
  353. 'arguments' => array('node' => NULL),
  354. 'template' => 'tripal_library_references',
  355. ),
  356. 'tripal_library_properties' => array(
  357. 'arguments' => array('node' => NULL),
  358. 'template' => 'tripal_library_properties',
  359. ),
  360. );
  361. }
  362. /**
  363. * This function is an extension of the chado_feature_view and
  364. * chado_organism_view by providing the markup for the library object
  365. * THAT WILL BE INDEXED.
  366. *
  367. * @ingroup tripal_library
  368. */
  369. function theme_tripal_library_search_index($node) {
  370. if ($node->type == 'chado_organism') {
  371. $content = "";
  372. // get the libraries for the organism
  373. $previous_db = tripal_db_set_active('chado');
  374. $sql = "SELECT * FROM {library} L ".
  375. "WHERE L.organism_id = %d";
  376. $libraries = array();
  377. $results = db_query($sql, $node->organism->organism_id);
  378. while ($library = db_fetch_object($results)) {
  379. // get the description
  380. $sql = "SELECT * FROM {libraryprop} LP ".
  381. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
  382. "WHERE LP.library_id = $library->library_id ".
  383. " AND CVT.name = 'library_description'";
  384. $desc = db_fetch_object(db_query($sql));
  385. $library->description = $desc->value;
  386. $libraries[] = $library;
  387. }
  388. tripal_db_set_active($previous_db);
  389. if (count($libraries) > 0) {
  390. foreach ($libraries as $library) {
  391. $content .= "$library->name ";
  392. $content .= "$library->description";
  393. };
  394. }
  395. // Provide library names to show in a feature page
  396. }
  397. elseif ($node->type == 'chado_feature') {
  398. $content = "";
  399. $organism_id = $node->feature->organism_id;
  400. $previous_db = tripal_db_set_active('chado');
  401. $sql = "SELECT * FROM {library} L ".
  402. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  403. "WHERE LF.feature_id = " . $node->feature->feature_id;
  404. $libraries = array();
  405. $results = db_query($sql);
  406. while ($library = db_fetch_object($results)) {
  407. $libraries[] = $library;
  408. }
  409. tripal_db_set_active($previous_db);
  410. if (count($libraries) > 0) {
  411. $lib_additions = array();
  412. foreach ($libraries as $library) {
  413. $content .= $library->name;
  414. };
  415. }
  416. }
  417. return $content;
  418. }
  419. /**
  420. * This function shows library information on an organism/feature node
  421. *
  422. * @ingroup tripal_library
  423. */
  424. function theme_tripal_library_node_libraries($node) {
  425. $content = "";
  426. // Show library information in a expandable box for a organism page.
  427. // Make sure we have $node->organism_id. In the case of creating a new
  428. // organism, the organism_id is not created until we save. This will cause
  429. // an error when users preview the creation without a $node->organism_id
  430. if ($node->type == 'chado_organism' && $node->organism_id) {
  431. $box_status = variable_get("tripal_library-box-libraries", "menu_off");
  432. if (strcmp($box_status, "menu_off")==0) {
  433. return get_tripal_library_organism_libraries($node->nid);
  434. }
  435. }
  436. // Provide library names to show in a feature page.
  437. // Make sure we have $node->feature->feature_id or there will be an error
  438. // when a feature is previewed at its creation
  439. elseif ($node->type == 'chado_feature' && $node->feature->feature_id) {
  440. $organism_id = $node->feature->organism_id;
  441. $previous_db = tripal_db_set_active('chado');
  442. $sql = "SELECT * FROM {library} L ".
  443. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  444. "WHERE LF.feature_id = " . $node->feature->feature_id;
  445. $libraries = array();
  446. $results = db_query($sql);
  447. while ($library = db_fetch_object($results)) {
  448. $libraries[] = $library;
  449. }
  450. tripal_db_set_active($previous_db);
  451. if (count($libraries) > 0) {
  452. $lib_additions = array();
  453. foreach ($libraries as $library) {
  454. $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
  455. $lib_nid = db_result(db_query($sql, $library->library_id));
  456. if ($lib_nid) {
  457. $lib_url = url("node/$lib_nid");
  458. }
  459. $lib_additions[$lib_url] = $library->name;
  460. };
  461. $node->lib_additions = $lib_additions;
  462. }
  463. }
  464. return $content;
  465. }
  466. /**
  467. *
  468. *
  469. * @ingroup tripal_library
  470. */
  471. function get_tripal_library_admin_form_cleanup_set(&$form) {
  472. $form['cleanup'] = array(
  473. '#type' => 'fieldset',
  474. '#title' => t('Clean Up')
  475. );
  476. $form['cleanup']['description'] = array(
  477. '#type' => 'item',
  478. '#value' => t("With Drupal and chado residing in different databases ".
  479. "it is possible that nodes in Drupal and libraries in Chado become ".
  480. "\"orphaned\". This can occur if an library node in Drupal is ".
  481. "deleted but the corresponding chado library is not and/or vice ".
  482. "versa. Click the button below to resolve these discrepancies."),
  483. '#weight' => 1,
  484. );
  485. $form['cleanup']['button'] = array(
  486. '#type' => 'submit',
  487. '#value' => t('Clean up orphaned libraries'),
  488. '#weight' => 2,
  489. );
  490. }
  491. /**
  492. *
  493. *
  494. * @ingroup tripal_library
  495. */
  496. function get_tripal_library_admin_form_taxonomy_set(&$form) {
  497. $form['taxonify'] = array(
  498. '#type' => 'fieldset',
  499. '#title' => t('Assign Drupal Taxonomy to Library Features')
  500. );
  501. // get the list of libraries
  502. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  503. $previous_db = tripal_db_set_active('chado'); // use chado database
  504. $lib_rset = db_query($sql);
  505. tripal_db_set_active($previous_db); // now use drupal database
  506. // iterate through all of the libraries
  507. $lib_boxes = array();
  508. while ($library = db_fetch_object($lib_rset)) {
  509. $lib_boxes[$library->library_id] = "$library->name";
  510. }
  511. $form['taxonify']['description'] = array(
  512. '#type' => 'item',
  513. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  514. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  515. "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."),
  516. '#weight' => 1,
  517. );
  518. $form['taxonify']['tx-libraries'] = array(
  519. '#title' => t('Libraries'),
  520. '#type' => t('checkboxes'),
  521. '#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."),
  522. '#required' => FALSE,
  523. '#prefix' => '<div id="lib_boxes">',
  524. '#suffix' => '</div>',
  525. '#options' => $lib_boxes,
  526. '#weight' => 2
  527. );
  528. $form['taxonify']['tx-button'] = array(
  529. '#type' => 'submit',
  530. '#value' => t('Set Feature Taxonomy'),
  531. '#weight' => 3
  532. );
  533. }
  534. /**
  535. *
  536. * @ingroup tripal_library
  537. */
  538. function get_tripal_library_admin_form_reindex_set(&$form) {
  539. // define the fieldsets
  540. $form['reindex'] = array(
  541. '#type' => 'fieldset',
  542. '#title' => t('Reindex Library Features')
  543. );
  544. // get the list of libraries
  545. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  546. $previous_db = tripal_db_set_active('chado'); // use chado database
  547. $lib_rset = db_query($sql);
  548. tripal_db_set_active($previous_db); // now use drupal database
  549. // iterate through all of the libraries
  550. $lib_boxes = array();
  551. while ($library = db_fetch_object($lib_rset)) {
  552. $lib_boxes[$library->library_id] = "$library->name";
  553. }
  554. $form['reindex']['description'] = array(
  555. '#type' => 'item',
  556. '#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."),
  557. '#weight' => 1,
  558. );
  559. $form['reindex']['re-libraries'] = array(
  560. '#title' => t('Libraries'),
  561. '#type' => t('checkboxes'),
  562. '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
  563. '#required' => FALSE,
  564. '#prefix' => '<div id="lib_boxes">',
  565. '#suffix' => '</div>',
  566. '#options' => $lib_boxes,
  567. '#weight' => 2,
  568. );
  569. $form['reindex']['re-button'] = array(
  570. '#type' => 'submit',
  571. '#value' => t('Reindex Features'),
  572. '#weight' => 3,
  573. );
  574. }
  575. /**
  576. *
  577. * @ingroup tripal_library
  578. */
  579. function get_tripal_library_admin_form_sync_set(&$form) {
  580. // define the fieldsets
  581. $form['sync'] = array(
  582. '#type' => 'fieldset',
  583. '#title' => t('Sync Libraries')
  584. );
  585. // get the list of libraries
  586. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  587. $previous_db = tripal_db_set_active('chado'); // use chado database
  588. $lib_rset = db_query($sql);
  589. tripal_db_set_active($previous_db); // now use drupal database
  590. // if we've added any libraries to the list that can be synced
  591. // then we want to build the form components to allow the user
  592. // to select one or all of them. Otherwise, just present
  593. // a message stating that all libraries are currently synced.
  594. $lib_boxes = array();
  595. $added = 0;
  596. while ($library = db_fetch_object($lib_rset)) {
  597. // check to see if the library is already present as a node in drupal.
  598. // if so, then skip it.
  599. $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
  600. if (!db_fetch_object(db_query($sql, $library->library_id))) {
  601. $lib_boxes[$library->library_id] = "$library->name";
  602. $added++;
  603. }
  604. }
  605. // if we have libraries we need to add to the checkbox then
  606. // build that form element
  607. if ($added > 0) {
  608. $lib_boxes['all'] = "All Libraries";
  609. $form['reindex']['description'] = array(
  610. '#type' => 'item',
  611. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  612. '#weight' => 1,
  613. );
  614. $form['sync']['libraries'] = array(
  615. '#title' => t('Available Libraries'),
  616. '#type' => t('checkboxes'),
  617. '#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."),
  618. '#required' => FALSE,
  619. '#prefix' => '<div id="lib_boxes">',
  620. '#suffix' => '</div>',
  621. '#options' => $lib_boxes,
  622. '#weight' => 2,
  623. );
  624. $form['sync']['button'] = array(
  625. '#type' => 'submit',
  626. '#value' => t('Sync Libraries'),
  627. '#weight' => 3,
  628. );
  629. }
  630. // we don't have any libraries to select from
  631. else {
  632. $form['sync']['value'] = array(
  633. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  634. );
  635. }
  636. }
  637. /**
  638. *
  639. * @ingroup tripal_library
  640. */
  641. function tripal_library_admin_validate($form, &$form_state) {
  642. global $user; // we need access to the user info
  643. $job_args = array();
  644. // Submit the Sync Job if selected
  645. if ($form_state['values']['op'] == t('Sync Libraries')) {
  646. // check to see if the user wants to sync chado and drupal. If
  647. // so then we need to register a job to do so with tripal
  648. $libraries = $form_state['values']['libraries'];
  649. $do_all = FALSE;
  650. $to_sync = array();
  651. foreach ($libraries as $library_id) {
  652. if (preg_match("/^all$/i", $library_id)) {
  653. $do_all = TRUE;
  654. }
  655. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  656. // get the library info
  657. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  658. $previous_db = tripal_db_set_active('chado'); // use chado database
  659. $library = db_fetch_object(db_query($sql, $library_id));
  660. tripal_db_set_active($previous_db); // now use drupal database
  661. $to_sync[$library_id] = $library->name;
  662. }
  663. }
  664. // submit the job to the tripal job manager
  665. if ($do_all) {
  666. tripal_add_job('Sync all libraries', 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  667. }
  668. else{
  669. foreach ($to_sync as $library_id => $name) {
  670. $job_args[0] = $library_id;
  671. tripal_add_job("Sync library: $name", 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
  672. }
  673. }
  674. }
  675. // -------------------------------------
  676. // Submit the Reindex Job if selected
  677. if ($form_state['values']['op'] == t('Reindex Features')) {
  678. $libraries = $form_state['values']['re-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. $previous_db = tripal_db_set_active('chado'); // use chado database
  684. $library = db_fetch_object(db_query($sql, $library_id));
  685. tripal_db_set_active($previous_db); // now use drupal database
  686. $job_args[0] = $library_id;
  687. tripal_add_job("Reindex features for library: $library->name", 'tripal_library',
  688. 'tripal_library_reindex_features', $job_args, $user->uid);
  689. }
  690. }
  691. }
  692. // -------------------------------------
  693. // Submit the Taxonomy Job if selected
  694. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  695. $libraries = $form_state['values']['tx-libraries'];
  696. foreach ($libraries as $library_id) {
  697. if ($library_id and preg_match("/^\d+$/i", $library_id)) {
  698. // get the library info
  699. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  700. $previous_db = tripal_db_set_active('chado'); // use chado database
  701. $library = db_fetch_object(db_query($sql, $library_id));
  702. tripal_db_set_active($previous_db); // now use drupal database
  703. $job_args[0] = $library_id;
  704. tripal_add_job("Set taxonomy for features in library: $library->name", 'tripal_library',
  705. 'tripal_library_taxonify_features', $job_args, $user->uid);
  706. }
  707. }
  708. }
  709. // -------------------------------------
  710. // Submit the Cleanup Job if selected
  711. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  712. tripal_add_job('Cleanup orphaned libraries', 'tripal_library',
  713. 'tripal_library_cleanup', $job_args, $user->uid);
  714. }
  715. }
  716. /**
  717. *
  718. * @ingroup tripal_library
  719. */
  720. function tripal_library_show_libraries() {
  721. // Show libraries stored in Drupal's {chado_library} table
  722. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  723. $no_libs = db_result(db_query($sql));
  724. if ($no_libs != 0) {
  725. $libraries = get_chado_libraries();
  726. if ($no_libs != count($libraries)) {
  727. drupal_set_message(t("Synchronization needed."));
  728. }
  729. return theme('tripal_library_library_table', $libraries);
  730. }
  731. else {
  732. return t("No library exists. Please contact administrators to ".
  733. "synchronize libraries.");
  734. }
  735. }
  736. /**
  737. *
  738. * @ingroup tripal_library
  739. */
  740. function tripal_library_cron() {
  741. }
  742. /**
  743. *
  744. * CHADO_LIBRARY NODE FUNCTIONS
  745. *
  746. * The following function proves access control for users trying to
  747. * perform actions on data managed by this module
  748. *
  749. * @ingroup tripal_library
  750. */
  751. function tripal_library_library_access($op, $node, $account) {
  752. if ($op == 'create') {
  753. if (!user_access('create chado_library content', $account)) {
  754. return FALSE;
  755. }
  756. }
  757. if ($op == 'update') {
  758. if (!user_access('edit any chado_library content', $account) &&
  759. !user_access('edit own chado_library content', $account)) {
  760. return FALSE;
  761. }
  762. if (user_access('edit own chado_library content', $account) &&
  763. $account->uid != $node->uid) {
  764. return FALSE;
  765. }
  766. }
  767. if ($op == 'delete') {
  768. if (!user_access('delete any chado_library content', $account) &&
  769. !user_access('delete own chado_library content', $account)) {
  770. return FALSE;
  771. }
  772. if (user_access('delete own chado_library content', $account) &&
  773. $account->uid != $node->uid) {
  774. return FALSE;
  775. }
  776. }
  777. return NULL;
  778. }
  779. /**
  780. * validates submission of form when adding or updating a library node
  781. *
  782. * @ingroup tripal_library
  783. */
  784. function chado_library_validate($node) {
  785. $lib = 0;
  786. // check to make sure the unique name on the library is unique
  787. // before we try to insert into chado.
  788. if ($node->library_id) {
  789. $sql = "SELECT * FROM ".
  790. "{Library} WHERE ".
  791. "uniquename = '%s' ".
  792. "AND NOT library_id = %d";
  793. $previous_db = tripal_db_set_active('chado');
  794. $lib = db_fetch_object(db_query($sql, $node->uniquename,
  795. $node->library_id));
  796. tripal_db_set_active($previous_db);
  797. }
  798. else {
  799. $sql = "SELECT * FROM ".
  800. "{Library} ".
  801. "WHERE uniquename = '%s'";
  802. $previous_db = tripal_db_set_active('chado');
  803. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  804. tripal_db_set_active($previous_db);
  805. }
  806. if ($lib) {
  807. form_set_error('uniquename', t('The unique library name already exists. '.
  808. 'Please choose another'));
  809. }
  810. }
  811. /**
  812. * When a new chado_library node is created we also need to add information
  813. * to our chado_library table. This function is called on insert of a new node
  814. * of type 'chado_library' and inserts the necessary information.
  815. *
  816. * @ingroup tripal_library
  817. */
  818. function chado_library_insert($node) {
  819. if ($node->library_id) {
  820. $library['library_id'] = $node->library_id;
  821. }
  822. else {
  823. // get the library type id
  824. $values = array(
  825. 'cv_id' => array(
  826. 'name' => 'tripal'
  827. ),
  828. 'name' => $node->library_type
  829. );
  830. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  831. $values = array(
  832. 'name' => $node->title,
  833. 'uniquename' => $node->uniquename,
  834. 'organism_id' => $node->organism_id,
  835. 'type_id' => $type[0]->cvterm_id
  836. );
  837. $library = tripal_core_chado_insert('library', $values);
  838. }
  839. if ($library) {
  840. // add the description property
  841. tripal_library_insert_property($library['library_id'], 'library_description', $node->library_description);
  842. // make sure the entry for this feature doesn't already exist in the chado_library table
  843. // if it doesn't exist then we want to add it.
  844. $library_id = chado_get_id_for_node('library', $node) ;
  845. if (!$library_id) {
  846. // next add the item to the drupal table
  847. $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
  848. "VALUES (%d, %d, %d)";
  849. db_query($sql, $node->nid, $node->vid, $library['library_id']);
  850. }
  851. }
  852. else {
  853. drupal_set_message(t('Unable to add library.', 'warning'));
  854. watchdog('tripal_library',
  855. 'Insert feature: Unable to create library where values: %values',
  856. array('%values' => print_r($values, TRUE)),
  857. WATCHDOG_WARNING
  858. );
  859. }
  860. }
  861. /**
  862. * Update nodes
  863. *
  864. * @ingroup tripal_library
  865. */
  866. function chado_library_update($node) {
  867. if ($node->revision) {
  868. // TODO -- decide what to do about revisions
  869. }
  870. else {
  871. $library_id = chado_get_id_for_node('library', $node) ;
  872. // get the library type id
  873. $values = array(
  874. 'cv_id' => array(
  875. 'name' => 'tripal'
  876. ),
  877. 'name' => $node->library_type
  878. );
  879. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  880. // update the library record
  881. $match = array(
  882. 'library_id' => $library_id,
  883. );
  884. $values = array(
  885. 'name' => $node->title,
  886. 'uniquename' => $node->uniquename,
  887. 'organism_id' => $node->organism_id,
  888. 'type_id' => $type[0]->cvterm_id
  889. );
  890. $status = tripal_core_chado_update('library', $match, $values);
  891. tripal_library_update_property($library_id, 'library_description', $node->library_description);
  892. }
  893. }
  894. /**
  895. * Add the library as a taxonomy term for associating with library_features
  896. *
  897. * @ingroup tripal_library
  898. */
  899. function tripal_library_add_taxonomy($node, $library_id) {
  900. //include the file containing the required functions. We only have to
  901. // do this because Drupal 6 fails to do this globally for us and
  902. // the drupal_execute function below won't work
  903. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  904. /* // get the vocabulary id
  905. $vocabularies = taxonomy_get_vocabularies();
  906. $vid = NULL;
  907. foreach($vocabularies as $vocab){
  908. if($vocab->name == 'DNA Libraries'){
  909. $vid = $vocab->vid;
  910. }
  911. }
  912. if(!$vid){ */
  913. // add the vocabulary
  914. $vocab_form['values']['name'] = 'DNA Libraries';
  915. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  916. $vocab_form['values']['help'] = '';
  917. $vocab_form['values']['module'] = 'taxonomy';
  918. drupal_execute('taxonomy_form_vocabulary', $vocab_form);
  919. return;
  920. // }
  921. // make sure this term doesn't already exist. If it doesn't then add it
  922. if ($vid) {
  923. $tree = taxonomy_get_tree($vid);
  924. $found = 0;
  925. foreach ($tree as $term) {
  926. if ($term->name == $node->title) {
  927. $found = 1;
  928. }
  929. }
  930. // add the term to the vocabulary
  931. if (!$found) {
  932. $form_state = array();
  933. $form_state['values']['name'] = $node->title;
  934. $form_state['values']['description'] = $library_id;
  935. drupal_execute('taxonomy_form_term', $form_state, $vid);
  936. }
  937. }
  938. }
  939. /**
  940. * When editing or creating a new node of type 'chado_library' we need
  941. * a form. This function creates the form that will be used for this.
  942. *
  943. * @ingroup tripal_library
  944. */
  945. function chado_library_form($node) {
  946. $form = array();
  947. $library = $node->library;
  948. // get the default values
  949. $uniquename = $node->uniquename;
  950. if (!$uniquename) {
  951. $uniquename = $library->uniquename;
  952. }
  953. $library_type = $node->library_type;
  954. if (!$library_type) {
  955. $library_type = $library->type_id->name;
  956. }
  957. $organism_id = $node->organism_id;
  958. if (!$organism_id) {
  959. $organism_id = $library->organism_id->organism_id;
  960. }
  961. $library_description = $node->library_description;
  962. if (!$library_description) {
  963. $libprop = tripal_library_get_property($library->library_id, 'library_description');
  964. $library_description = $libprop->value;
  965. }
  966. // keep track of the library id if we have. If we do have one then
  967. // this is an update as opposed to an insert.
  968. $form['library_id'] = array(
  969. '#type' => 'value',
  970. '#value' => $library->library_id,
  971. );
  972. $form['title']= array(
  973. '#type' => 'textfield',
  974. '#title' => t('Library Title'),
  975. '#description' => t('Please enter the title for this library. '.
  976. 'This appears at the top of the library page.'),
  977. '#required' => TRUE,
  978. '#default_value' => $node->title,
  979. '#weight' => 1
  980. );
  981. $form['uniquename']= array(
  982. '#type' => 'textfield',
  983. '#title' => t('Unique Library Name'),
  984. '#description' => t('Please enter a unique name for this library'),
  985. '#required' => TRUE,
  986. '#default_value' => $uniquename,
  987. '#weight' => 2
  988. );
  989. // These library types should not be hard coded, but for now the are...
  990. $library_types = array();
  991. $library_types[''] = '';
  992. $library_types['cdna_library'] = 'cDNA Library';
  993. $library_types['bac_library'] = 'BAC Library';
  994. $library_types['fosmid_library'] = 'FOSMID Library';
  995. $library_types['cosmid_library'] = 'COSMID Library';
  996. $library_types['yac_library'] = 'YAC Library';
  997. $form['library_type'] = array(
  998. '#title' => t('Library Type'),
  999. '#type' => t('select'),
  1000. '#description' => t("Choose the library type."),
  1001. '#required' => TRUE,
  1002. '#default_value' => $library_type,
  1003. '#options' => $library_types,
  1004. '#weight' => 3
  1005. );
  1006. // get the list of organisms
  1007. $sql = "SELECT * FROM {Organism}";
  1008. $previous_db = tripal_db_set_active('chado'); // use chado database
  1009. $org_rset = db_query($sql);
  1010. tripal_db_set_active($previous_db); // now use drupal database
  1011. $organisms = array();
  1012. $organisms[''] = '';
  1013. while ($organism = db_fetch_object($org_rset)) {
  1014. $organisms[$organism->organism_id] =
  1015. "$organism->genus $organism->species ($organism->common_name)";
  1016. }
  1017. $form['organism_id'] = array(
  1018. '#title' => t('Organism'),
  1019. '#type' => t('select'),
  1020. '#description' => t("Choose the organism with which this library is ".
  1021. "associated."),
  1022. '#required' => TRUE,
  1023. '#default_value' => $organism_id,
  1024. '#options' => $organisms,
  1025. '#weight' => 4,
  1026. );
  1027. $form['library_description']= array(
  1028. '#type' => 'textarea',
  1029. '#title' => t('Library Description'),
  1030. '#description' => t('A brief description of the library'),
  1031. '#required' => TRUE,
  1032. '#default_value' => $library_description,
  1033. '#weight' => 5
  1034. );
  1035. return $form;
  1036. }
  1037. /**
  1038. *
  1039. *
  1040. * @ingroup tripal_library
  1041. */
  1042. function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
  1043. global $user;
  1044. $page_content = '';
  1045. // get the list of libraries and create new nodes
  1046. if (!$library_id) {
  1047. $sql = "SELECT * FROM {Library} L";
  1048. $previous_db = tripal_db_set_active('chado'); // use chado database
  1049. $results = db_query($sql);
  1050. tripal_db_set_active($previous_db); // now use drupal database
  1051. }
  1052. else {
  1053. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  1054. $previous_db = tripal_db_set_active('chado'); // use chado database
  1055. $results = db_query($sql, $library_id);
  1056. tripal_db_set_active($previous_db); // now use drupal database
  1057. }
  1058. // We'll use the following SQL statement for checking if the library
  1059. // already exists as a drupal node.
  1060. $sql = "SELECT * FROM {chado_library} ".
  1061. "WHERE library_id = %d";
  1062. while ($library = db_fetch_object($results)) {
  1063. // check if this library already exists in the drupal database. if it
  1064. // does then skip this library and go to the next one.
  1065. if (!db_fetch_object(db_query($sql, $library->library_id))) {
  1066. $new_node = new stdClass();
  1067. $new_node->type = 'chado_library';
  1068. $new_node->uid = $user->uid;
  1069. $new_node->title = "$library->name";
  1070. $new_node->library_id = $library->library_id;
  1071. $new_node->organism_id = $library->organism_id;
  1072. $new_node->uniquename = $library->uniquename;
  1073. $new_node->type_id = $library->type_id;
  1074. node_validate($new_node);
  1075. $errors = form_get_errors();
  1076. if (!$errors) {
  1077. $node = node_submit($new_node);
  1078. node_save($node);
  1079. if ($node->nid) {
  1080. print "Added " . $library->name . "\n";
  1081. }
  1082. else {
  1083. print "ERROR: Unable to create " . $library->name . "\n";
  1084. }
  1085. }
  1086. else {
  1087. print "ERROR: Unable to create " . $library->name . "\n" . print_r($errors, TRUE) . "\n";
  1088. }
  1089. }
  1090. else {
  1091. print "Skipped " . $library->name . "\n";
  1092. }
  1093. }
  1094. return $page_content;
  1095. }
  1096. /**
  1097. * When a node is requested by the user this function is called to allow us
  1098. * to add auxiliary data to the node object.
  1099. *
  1100. * @ingroup tripal_library
  1101. */
  1102. function chado_library_load($node) {
  1103. // get the feature details from chado
  1104. $library_id = chado_get_id_for_node('library', $node);
  1105. $values = array('library_id' => $library_id);
  1106. $library = tripal_core_generate_chado_var('library', $values);
  1107. $additions = new stdClass();
  1108. $additions->library = $library;
  1109. return $additions;
  1110. }
  1111. /**
  1112. * This function customizes the view of the chado_library node. It allows
  1113. * us to generate the markup. This function is required for node [Preview]
  1114. *
  1115. * @ingroup tripal_library
  1116. */
  1117. function chado_library_view($node, $teaser = FALSE, $page = FALSE) {
  1118. // use drupal's default node view:
  1119. if (!$teaser) {
  1120. $node = node_prepare($node, $teaser);
  1121. // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
  1122. // but not genus/species/common_name. We need to get those from chado
  1123. // database so they will show up in preview
  1124. if (!$node->genus) {
  1125. $previous_db = tripal_db_set_active('chado');
  1126. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  1127. $data = db_fetch_object(db_query($sql, $node->organism_id));
  1128. $node->genus = $data->genus;
  1129. $node->species = $data->species;
  1130. $node->common_name = $data->common_name;
  1131. tripal_db_set_active($previous_db);
  1132. }
  1133. }
  1134. return $node;
  1135. }
  1136. /**
  1137. * This function creates the html markup for the library table.
  1138. * It can be overridden in the theme for the site by adding a php
  1139. * method in the template.php file named
  1140. * [theme_name]_tripal_library_library_table(&$libraries)
  1141. *
  1142. * @ingroup tripal_library
  1143. */
  1144. function theme_tripal_library_library_table($libraries) {
  1145. // cycle through the libraries and build the libraries page
  1146. $output = "<div id=\"libraries\">";
  1147. $output .= '<table>';
  1148. $output .= "<tr>";
  1149. $output .= "<th>Name</th>";
  1150. $output .= "<th>Type</th>";
  1151. $output .= "<th>Organism</th>";
  1152. $output .= "<th>Description</th>";
  1153. $output .= "</tr>";
  1154. foreach ($libraries as $library) {
  1155. $lib_url = url("node/$library->node_id");
  1156. $output .= "<tr>";
  1157. $output .= "<td>".
  1158. " <a href=\"$lib_url\">$library->name</a>".
  1159. "</td>";
  1160. if (strcasecmp($library->type_name, 'bac_library') == 0) {
  1161. $output .= "<td>BAC</td>";
  1162. }
  1163. elseif (strcasecmp($library->type_name, 'cdna_library') == 0) {
  1164. $output .= "<td>cDNA</td>";
  1165. }
  1166. else{
  1167. $output .= "<td>$library->type_name</td>";
  1168. }
  1169. $output .= "<td nowrap>".
  1170. " $library->common_name".
  1171. "</td>";
  1172. $description = $library->library_description;
  1173. $output .= "<td>$description</td>";
  1174. $output .= "</tr>";
  1175. }
  1176. $output .= "</table>";
  1177. $output .= "</div>";
  1178. return $output;
  1179. }
  1180. /**
  1181. *
  1182. * @ingroup tripal_library
  1183. */
  1184. function tripal_library_feature_set_taxonomy($library_id = NULL) {
  1185. //TO DO : return usable error if vocabs don't exist
  1186. // get the list of vocabularies and find our two vocabularies of interest
  1187. $vocabularies = taxonomy_get_vocabularies();
  1188. $vid = NULL;
  1189. foreach ($vocabularies as $vocab) {
  1190. if ($vocab->name == 'Library') {
  1191. $vid = $vocab->vid;
  1192. }
  1193. }
  1194. if (!$vid) {
  1195. return;
  1196. }
  1197. // We'll use the following SQL statement for getting the node info
  1198. if ($library_id) {
  1199. print "Finding features for library with ID: $library_id\n";
  1200. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1201. "FROM {library_feature} LF ".
  1202. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1203. "WHERE L.library_id = $library_id ".
  1204. "ORDER BY LF.feature_id";
  1205. $previous_db = tripal_db_set_active('chado'); // use chado database
  1206. $features = db_query($sql);
  1207. tripal_db_set_active($previous_db); // now use drupal database
  1208. }
  1209. else {
  1210. print "Finding features for all libraries\n";
  1211. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1212. "FROM {library_feature} LF ".
  1213. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1214. "ORDER BY LF.feature_id";
  1215. $previous_db = tripal_db_set_active('chado'); // use chado database
  1216. $features = db_query($sql);
  1217. tripal_db_set_active($previous_db); // now use drupal database
  1218. }
  1219. $node_sql = "SELECT * FROM {chado_feature} CF ".
  1220. " INNER JOIN {node} N ON CF.nid = N.nid ".
  1221. "WHERE feature_id = %d";
  1222. // iterate through the features and add the taxonomy
  1223. while ($feature = db_fetch_object($features)) {
  1224. $node = db_fetch_object(db_query($node_sql, $feature->feature_id));
  1225. $tags["$vid"] = $feature->libname;
  1226. $terms['tags'] = $tags;
  1227. taxonomy_node_save($node, $terms);
  1228. print "Updated $feature->feature_id as $feature->libname\n";
  1229. }
  1230. }
  1231. /**
  1232. *
  1233. * @ingroup tripal_library
  1234. */
  1235. function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
  1236. $i = 0;
  1237. // if the caller provided a library_id then get all of the features
  1238. // associated with the library. Otherwise get all sequences assoicated
  1239. // with all libraries.
  1240. if ($library_id) {
  1241. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1242. " FROM {library_feature} LF ".
  1243. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1244. "WHERE L.library_id = $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. else {
  1251. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1252. " FROM {library_feature} LF ".
  1253. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1254. "ORDER BY LF.feature_id";
  1255. $previous_db = tripal_db_set_active('chado'); // use chado database
  1256. $results = db_query($sql);
  1257. tripal_db_set_active($previous_db); // now use drupal database
  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. $interval = intval($count * 0.01);
  1267. foreach ($ids as $feature_id) {
  1268. // update the job status every 1% features
  1269. if ($job_id and $i % interval == 0) {
  1270. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  1271. }
  1272. tripal_feature_sync_feature($feature_id);
  1273. $i++;
  1274. }
  1275. }
  1276. /**
  1277. *
  1278. * @ingroup tripal_library
  1279. */
  1280. function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
  1281. $i = 0;
  1282. // if the caller provided a library_id then get all of the features
  1283. // associated with the library. Otherwise get all sequences assoicated
  1284. // with all libraries.
  1285. if ($library_id) {
  1286. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1287. " FROM {library_feature} LF ".
  1288. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1289. "WHERE L.library_id = $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. else {
  1296. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1297. " FROM {library_feature} LF ".
  1298. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1299. "ORDER BY LF.feature_id";
  1300. $previous_db = tripal_db_set_active('chado'); // use chado database
  1301. $results = db_query($sql);
  1302. tripal_db_set_active($previous_db); // now use drupal database
  1303. }
  1304. // load into ids array
  1305. $count = 0;
  1306. $ids = array();
  1307. while ($id = db_fetch_object($results)) {
  1308. $ids[$count] = $id->feature_id;
  1309. $count++;
  1310. }
  1311. // make sure our vocabularies are set before proceeding
  1312. tripal_feature_set_vocabulary();
  1313. // use this SQL for getting the nodes
  1314. $nsql = "SELECT * FROM {chado_feature} CF ".
  1315. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1316. "WHERE feature_id = %d";
  1317. // iterate through the features and set the taxonomy
  1318. $interval = intval($count * 0.01);
  1319. foreach ($ids as $feature_id) {
  1320. // update the job status every 1% features
  1321. if ($job_id and $i % interval == 0) {
  1322. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  1323. }
  1324. $node = db_fetch_object(db_query($nsql, $feature_id));
  1325. tripal_feature_set_taxonomy($node, $feature_id);
  1326. $i++;
  1327. }
  1328. }
  1329. /**
  1330. * Delete data from drupal and chado databases when a node is deleted
  1331. * @ingroup tripal_library
  1332. */
  1333. function chado_library_delete(&$node) {
  1334. $library_id = chado_get_id_for_node('library', $node);
  1335. // if we don't have a library id for this node then this isn't a node of
  1336. // type chado_library or the entry in the chado_library table was lost.
  1337. if (!$library_id) {
  1338. return;
  1339. }
  1340. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  1341. // drupal database
  1342. $sql_del = "DELETE FROM {chado_library} ".
  1343. "WHERE nid = %d ".
  1344. "AND vid = %d";
  1345. db_query($sql_del, $node->nid, $node->vid);
  1346. $sql_del = "DELETE FROM {node} ".
  1347. "WHERE nid = %d ".
  1348. "AND vid = %d";
  1349. db_query($sql_del, $node->nid, $node->vid);
  1350. $sql_del = "DELETE FROM {node_revisions} ".
  1351. "WHERE nid = %d ".
  1352. "AND vid = %d";
  1353. db_query($sql_del, $node->nid, $node->vid);
  1354. // Remove data from library and libraryprop tables of chado database as well
  1355. $previous_db = tripal_db_set_active('chado');
  1356. db_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
  1357. db_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
  1358. tripal_db_set_active($previous_db);
  1359. }
  1360. /**
  1361. * Display block with libraries
  1362. * @param op - parameter to define the phase being called for the block
  1363. * @param delta - id of the block to return (ignored when op is list)
  1364. * @param edit - when op is save, contains the submitted form data
  1365. *
  1366. * @ingroup tripal_library
  1367. */
  1368. function tripal_library_block($op = 'list', $delta = '0', $edit = array()) {
  1369. switch ($op) {
  1370. case 'list':
  1371. $blocks['libreferences']['info'] = t('Tripal Library References');
  1372. $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
  1373. $blocks['libbase']['info'] = t('Tripal Library Details');
  1374. $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
  1375. $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
  1376. $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
  1377. $blocks['libproperties']['info'] = t('Tripal Library Properties');
  1378. $blocks['libproperties']['cache'] = BLOCK_NO_CACHE;
  1379. $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
  1380. $blocks['featurelibs']['cache'] = BLOCK_NO_CACHE;
  1381. $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
  1382. $blocks['orglibs']['cache'] = BLOCK_NO_CACHE;
  1383. return $blocks;
  1384. case 'view':
  1385. if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
  1386. $nid = arg(1);
  1387. $node = node_load($nid);
  1388. $block = array();
  1389. switch ($delta) {
  1390. case 'libreferences':
  1391. $block['subject'] = t('References');
  1392. $block['content'] = theme('tripal_library_references', $node);
  1393. break;
  1394. case 'libbase':
  1395. $block['subject'] = t('Library Details');
  1396. $block['content'] = theme('tripal_library_base', $node);
  1397. break;
  1398. case 'libsynonyms':
  1399. $block['subject'] = t('Synonyms');
  1400. $block['content'] = theme('tripal_library_synonyms', $node);
  1401. break;
  1402. case 'libproperties':
  1403. $block['subject'] = t('Properties');
  1404. $block['content'] = theme('tripal_library_properties', $node);
  1405. break;
  1406. case 'featurelibs':
  1407. $block['subject'] = t('Libraries');
  1408. $block['content'] = theme('tripal_feature_libraries', $node);
  1409. break;
  1410. case 'orglibs':
  1411. $block['subject'] = t('Libraries');
  1412. $block['content'] = theme('tripal_organism_libraries', $node);
  1413. break;
  1414. default :
  1415. }
  1416. return $block;
  1417. }
  1418. }
  1419. }
  1420. /**
  1421. * This function uses library_id's of all drupal library nodes as input and
  1422. * pull the library information (name, uniquename, type, genus, species,
  1423. * common_name, description) from chado database. The return type is an object
  1424. * array that stores sorted $library objects
  1425. *
  1426. * This function is DEPRECATED
  1427. * Please use tripal_core_chado_generate_vars
  1428. *
  1429. * @ingroup tripal_library_api
  1430. */
  1431. function get_chado_libraries() {
  1432. $sql_drupal = "SELECT COUNT (library_id) FROM {chado_library}";
  1433. $no_libs = db_result(db_query($sql_drupal));
  1434. if ($no_libs != 0) {
  1435. // Get library_id's from drupal
  1436. $sql = "SELECT library_id, nid FROM {chado_library}";
  1437. $result = db_query($sql);
  1438. $previous_db = tripal_db_set_active('chado');
  1439. // Get library info from chado's library, organism, and cvterm tables
  1440. $sql_info = "SELECT L.name, uniquename, genus, species, common_name, ".
  1441. " CVT.name as type_name ".
  1442. "FROM {Library} L ".
  1443. "INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1444. "INNER JOIN CVTerm CVT ON L.type_id = CVT.cvterm_id ".
  1445. "WHERE library_id=%d";
  1446. // Get library description from libraryprop if there is any
  1447. $sql_desc = "SELECT value ".
  1448. "FROM {Libraryprop} LP ".
  1449. "INNER JOIN CVTerm CVT ON LP.type_id = CVT.cvterm_id ".
  1450. "WHERE library_id = %d AND CVT.name = 'library_description'";
  1451. $libraries = array();
  1452. while ($data = db_fetch_object($result)) {
  1453. $library = db_fetch_object(db_query($sql_info, $data->library_id));
  1454. $library->node_id = $data->nid;
  1455. $desc = db_fetch_object(db_query($sql_desc, $data->library_id))->value;
  1456. if ($desc) {
  1457. $library->library_description = $desc;
  1458. }
  1459. else {
  1460. $library->library_description = "NA";
  1461. }
  1462. // Use uniquename as the key so we can sort by uniquename later
  1463. $key = drupal_strtolower($library->uniquename);
  1464. $libraries [$key] = $library;
  1465. }
  1466. tripal_db_set_active($previous_db);
  1467. //Sort libraries by uniquename
  1468. ksort($libraries, SORT_STRING);
  1469. return $libraries;
  1470. }
  1471. }
  1472. /**
  1473. * Remove orphaned drupal nodes
  1474. *
  1475. * @param $dummy
  1476. * Not Used -kept for backwards compatibility
  1477. * @param $job_id
  1478. * The id of the tripal job executing this function
  1479. *
  1480. * @ingroup tripal_library
  1481. */
  1482. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  1483. return tripal_core_clean_orphaned_nodes('library', $job_id);
  1484. }
  1485. /************************************************************************
  1486. */
  1487. function theme_tripal_library_search_result($node) {
  1488. }