tripal_library.module 56 KB

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