tripal_library.module 58 KB

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