tripal_library.module 60 KB

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