tripal_library.module 53 KB

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