tripal_library.module 61 KB

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