tripal_library.module 57 KB

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