tripal_library.module 60 KB

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