tripal_organism.module 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. <?php
  2. require_once "tripal_organism.api.inc";
  3. /**
  4. * @file
  5. * @defgroup tripal_organism Organism
  6. * @ingroup tripal_modules
  7. */
  8. /**
  9. *
  10. * @ingroup tripal_organism
  11. */
  12. function tripal_organism_init() {
  13. // add the jGCharts JS and CSS
  14. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_organism.js');
  15. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_organism.css');
  16. }
  17. /**
  18. * Provide information to drupal about the node types that we're creating
  19. * in this module
  20. *
  21. * @ingroup tripal_organism
  22. */
  23. function tripal_organism_node_info() {
  24. $nodes = array();
  25. $nodes['chado_organism'] = array(
  26. 'name' => t('Organism'),
  27. 'module' => 'chado_organism',
  28. 'description' => t('An organism from the chado database'),
  29. 'has_title' => FALSE,
  30. 'title_label' => t('Organism'),
  31. 'has_body' => FALSE,
  32. 'body_label' => t('Organism Description'),
  33. 'locked' => TRUE
  34. );
  35. return $nodes;
  36. }
  37. /**
  38. * Display block with organisms
  39. * @param op - parameter to define the phase being called for the block
  40. * @param delta - id of the block to return (ignored when op is list)
  41. * @param edit - when op is save, contains the submitted form data
  42. *
  43. * @ingroup tripal_organism
  44. */
  45. function tripal_organism_block($op = 'list', $delta = '0', $edit = array()) {
  46. switch ($op) {
  47. case 'list':
  48. $blocks['base']['info'] = t('Tripal Organism Details');
  49. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  50. $blocks['description']['info'] = t('Tripal Organism Description');
  51. $blocks['description']['cache'] = BLOCK_NO_CACHE;
  52. $blocks['image']['info'] = t('Tripal Organism Image');
  53. $blocks['image']['cache'] = BLOCK_NO_CACHE;
  54. return $blocks;
  55. case 'view':
  56. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  57. $nid = arg(1);
  58. $node = node_load($nid);
  59. $block = array();
  60. switch ($delta) {
  61. case 'base':
  62. $block['subject'] = t('Organism Details');
  63. $block['content'] = theme('tripal_organism_base', $node);
  64. break;
  65. case 'description':
  66. $block['subject'] = t('Organism Description');
  67. $block['content'] = theme('tripal_organism_description', $node);
  68. break;
  69. case 'image':
  70. $block['subject'] = t('Organism Image');
  71. $block['content'] = theme('tripal_organism_image', $node);
  72. break;
  73. default:
  74. }
  75. return $block;
  76. }
  77. }
  78. }
  79. /**
  80. * Menu items are automatically added for the new node types created
  81. * by this module to the 'Create Content' Navigation menu item. This function
  82. * adds more menu items needed for this module.
  83. *
  84. * @ingroup tripal_organism
  85. */
  86. function tripal_organism_menu() {
  87. $items = array();
  88. $items['organisms'] = array(
  89. 'menu_name' => ('primary-links'), //Enable the 'Organism' primary link
  90. 'title' => 'Organisms',
  91. 'page callback' => 'tripal_organism_show_organisms',
  92. 'access arguments' => array('access chado_organism content'),
  93. 'type' => MENU_NORMAL_ITEM
  94. );
  95. // the administative settings menu
  96. $items['admin/tripal/tripal_organism'] = array(
  97. 'title' => 'Organisms',
  98. 'description' => 'Basic Description of Tripal Organism Module Functionality',
  99. 'page callback' => 'tripal_organism_module_description_page',
  100. 'access arguments' => array('administer site configuration'),
  101. 'type' => MENU_NORMAL_ITEM,
  102. );
  103. $items['admin/tripal/tripal_organism/configuration'] = array(
  104. 'title' => 'Configuration',
  105. 'description' => 'Manage integration of Chado organisms including associated features',
  106. 'page callback' => 'drupal_get_form',
  107. 'page arguments' => array('tripal_organism_admin'),
  108. 'access arguments' => array('administer site configuration'),
  109. 'type' => MENU_NORMAL_ITEM,
  110. );
  111. return $items;
  112. }
  113. /**
  114. * The following function proves access control for users trying to
  115. * perform actions on data managed by this module
  116. *
  117. * @ingroup tripal_organism
  118. */
  119. function chado_organism_access($op, $node, $account) {
  120. if ($op == 'create') {
  121. if (!user_access('create chado_organism content', $account)) {
  122. return FALSE;
  123. }
  124. }
  125. if ($op == 'update') {
  126. if (!user_access('edit chado_organism content', $account)) {
  127. return FALSE;
  128. }
  129. }
  130. if ($op == 'delete') {
  131. if (!user_access('delete chado_organism content', $account)) {
  132. return FALSE;
  133. }
  134. }
  135. if ($op == 'view') {
  136. if (!user_access('access chado_organism content', $account)) {
  137. return FALSE;
  138. }
  139. }
  140. return NULL;
  141. }
  142. /**
  143. * Set the permission types that the chado module uses. Essentially we
  144. * want permissionis that protect creation, editing and deleting of chado
  145. * data objects
  146. *
  147. *
  148. @ingroup tripal_organism
  149. */
  150. function tripal_organism_perm() {
  151. return array(
  152. 'access chado_organism content',
  153. 'create chado_organism content',
  154. 'delete chado_organism content',
  155. 'edit chado_organism content',
  156. );
  157. }
  158. /**
  159. * Purpose: Provide Guidance to new Tripal Admin
  160. *
  161. * @return HTML Formatted text
  162. *
  163. * @ingroup tripal_organism
  164. */
  165. function tripal_organism_module_description_page() {
  166. $text = '';
  167. $text .= '<h3>Tripal Organism Administrative Tools Quick Links:</h3>';
  168. $text .= "<ul>
  169. <li><a href=\"" . url("admin/tripal/tripal_organism/configuration") . "\">Organism Configuration</a></li>
  170. </ul>";
  171. $text .= '<h3>Module Description:</h3>';
  172. $text .= '<p>The Tripal Organism module allows you to add, edit and/or delete chado organisms.
  173. Furthermore, it also provides listing of organisms and details page for each organism.
  174. Basically, the chado organism module is designed to hold information about a given species.
  175. For more information on the chado organism module see the
  176. <a href="http://gmod.org/wiki/Chado_Organism_Module">GMOD wiki page</a></p>';
  177. $text .= '<h3>Setup Instructions:</h3>';
  178. $text .= '<p>After installation of the organism module. The following tasks should be performed.
  179. <ol>
  180. <li><p><b>Set Permissions</b>: The organism module supports the Drupal user permissions interface for
  181. controlling access to organism content and functions. These permissions include viewing,
  182. creating, editing or administering of
  183. organism content. The default is that only the original site administrator has these
  184. permissions. You can <a href="' . url('admin/user/roles') . '">add roles</a> for classifying users,
  185. <a href="' . url('admin/user/user') . '">assign users to roles</a> and
  186. <a href="' . url('admin/user/permissions') . '">assign permissions</a> for the organism content to
  187. those roles. For a simple setup, allow anonymous users access to view organism content and
  188. allow the site administrator all other permissions.</p></li>
  189. <li><p><b>Create Organisms</b>: Organism pages can be created in two ways:
  190. <ol>
  191. <li><b>Sync Organisms</b>: If your organism has been pre-loaded into Chado then you need to sync the organism.
  192. This process is what creates the pages for viewing online. Not all organisms need be synced, only those
  193. that you want shown on the site. Use the the
  194. <a href="' . url('admin/tripal/tripal_organism/configuration') . '">Organism Configuration page</a>
  195. to sync organisms. </li>
  196. <li><b>Manually Add An Organism</b>: If your organism is not already present in the Chado database
  197. you can create an organism using the <a href="' . url('node/add/chado-organism') . '">Create Organism page</a>.
  198. Once saved, the organism will be present in Chado and also "synced".
  199. </ol></p></li>
  200. <li><p><b>Indexing</b>: Once organism pages are ready for public access,
  201. you can index the organism pages for searching if you want to ues the Drupal default search mechanism.
  202. Use the <a href="' . url('admin/tripal/tripal_organism/configuration') . '">Organism Configuration page</a>
  203. to either Index (for the first time) or "Reindex" (after updating data)
  204. the organism pages for searching. Once the site is 100% indexed the pages will be searchable using Drupal\'s
  205. full text searching. You can find the percent indexed for the entire site by visiting the
  206. <a href="' . url('admin/settings/search') . '">Search settings page</a>. Indexing
  207. can take quite a while if you have a lot of data</p></li>
  208. <li><p><b>Set Taxonomy</b>: Drupal provides a mechanism for categorizing content to allow
  209. for advanced searching. Drupal calls this "Taxonomy", but is essentially categorizing the pages.
  210. You can categorize feature pages by the
  211. organism to which they belong. This allows for filtering of search results by organism.
  212. Use the <a href="' . url('admin/tripal/tripal_organism/configuration') . '">Organism Configuration page</a> to
  213. set the Taxonomy.</p></li>
  214. </ol>';
  215. $text .= '<h3>Features of this Module:</h3>';
  216. $text .= '<p>Aside from organism page setup (as described in the Setup section above),
  217. The Tripal organism module also provides the following functionality
  218. <ul>
  219. <li><p><b>Integration with Drupal Views</b>: <a href="http://drupal.org/project/views">Drupal Views</a> is
  220. a powerful tool that allows the site administrator to create lists or basic searching forms of Chado content.
  221. It provides a graphical interface within Drupal to allow the site admin to directly query the Chado database
  222. and create custom lists without PHP programming or customization of Tripal source code. Views can also
  223. be created to filter content that has not yet been synced with Druapl in order to protect access to non
  224. published data (only works if Chado was installed using Tripal). You can see a list of available pre-existing
  225. Views <a href="' . url('admin/build/views/') . '">here</a>, as well as create your own. </p></li>
  226. <li><p><b>Basic Organism List</b>: This module provides a basic <a href="' . url('organisms') . '">organism list</a>
  227. for showing the list of organisms in Chado. <a href="http://drupal.org/project/views">Drupal Views</a> must be
  228. installed. You can use the Views interface to alter the appearance of this list.</p></li>
  229. </ul>
  230. </p>';
  231. $text .= '<h3>Page Customizations</h3>';
  232. $text .= '<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal.
  233. Below is a description of several methods. These methods may be used in conjunction with one another to
  234. provide fine-grained control.
  235. <ul>
  236. <li><p><b>Integration with Drupal Panels</b>: <a href="http://drupal.org/project/views">Drupal Panels</a>
  237. allows for customization of a page layout if you don\'t want to do PHP/Javascript/CSS programming. Tripal comes with pre-set layouts for organism pages. However,
  238. Panels become useful if you prefer a layout that is different from the pre-set layouts. Chado content
  239. is provided to Panels in the form of Drupal "blocks" which you can then place anywhere on a page using the
  240. Panel\'s GUI.</p></li>
  241. <li><p><b>Drupal\'s Content Construction Kit (CCK)</b>: the
  242. <a href="http://drupal.org/project/cck">Content Construction Kit (CCK) </a> is a powerful way to add non-Chado content
  243. to any page without need to edit template files or knowing PHP. You must first download and install CCK.
  244. With CCK, the site administartor can create a new field to appear on the page. For example, currently,
  245. the Chado publication module is not yet supported by Tripal. Therefore, the site administrator can add a text
  246. field to the organism pages. This content is not stored in Chado, but will appear on the organism page. A field
  247. added by CCK will also appear in the form when editing a organism to allow users to manually enter the appropriate
  248. text. If the default pre-set layout and themeing for Tripal is used, it is better to create the CCK element,
  249. indicate that it is not to be shown (using the CCK interface), then manually add the new content type
  250. where desired by editing the templates (as described below). If using Panels, the CCK field can be added to the
  251. location desired using the Panels interface.</p></li>
  252. <li><p><b>Drupal Node Templates</b>: The Tripal packages comes with a "theme_tripal" directory that contains the
  253. themeing for Chado content. The organism module has a template file for organism "nodes" (Tripal organism pages). This file
  254. is named "node-chado_organism.tpl.php", and provides javascript, HTML and PHP code for display of the organism
  255. pages. You can edit this file to control which types of information (or which organism "blocks") are displayed for organisms. Be sure to
  256. copy these template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
  257. future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
  258. for instructions on how to access variables and other Chado content within the template file.</p></li>
  259. <li><p><b>Organism "Block" Templates</b>: In the "theme_tripal" directory is a subdirectory named "tripal_organism".
  260. Inside this directory is a set of templates that control distinct types of information for organisms. For example,
  261. there is a "base" template for displaying of data directly from the Chado organism table, and a "references"
  262. template for showing external site references for a organism (data from the organism_dbxref table). These templates are used both by Drupal blocks
  263. for use in Drupal Panels (as described above) or for use in the default pre-set layout that the node template
  264. provides (also desribed above). You can customize this template as you desire. Be sure to copy the
  265. template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
  266. future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
  267. for instructions on how to access variables and other Chado content within the template files.</p></li>
  268. </li>
  269. <li><p><b>Adding Links to the "Resources" Sidebar</b>: If you use the pre-set default Tripal layout for theming, you
  270. will see a "Resources" sidebar on each page. The links that appear on the sidebar are automatically generated
  271. using Javascript for all of the organism "Blocks" that appear on the page. If you want to add additional links
  272. (e.g. a dynamic link to GBrowse for the organism) and you want that link to appear in the
  273. "Resources" sidebar, simply edit the Drupal Node Template (as described above) and add the link to the
  274. section at the bottom of the template file where the resources section is found.</p></li>
  275. </ul>
  276. </p>';
  277. return $text;
  278. }
  279. /**
  280. * Administrative settings for chado_orgnism
  281. *
  282. * @ingroup tripal_organism
  283. */
  284. function tripal_organism_admin() {
  285. $form = array();
  286. // before proceeding check to see if we have any
  287. // currently processing jobs. If so, we don't want
  288. // to give the opportunity to sync libraries
  289. $active_jobs = FALSE;
  290. if (tripal_get_module_active_jobs('tripal_organism')) {
  291. $active_jobs = TRUE;
  292. }
  293. // add the field set for syncing libraries
  294. if (!$active_jobs) {
  295. get_tripal_organism_admin_form_sync_set($form);
  296. get_tripal_organism_admin_form_reindex_set($form);
  297. get_tripal_organism_admin_form_taxonomy_set($form);
  298. get_tripal_organism_admin_form_cleanup_set($form);
  299. }
  300. else {
  301. $form['notice'] = array(
  302. '#type' => 'fieldset',
  303. '#title' => t('Organism Management Temporarily Unavailable')
  304. );
  305. $form['notice']['message'] = array(
  306. '#value' => t('Currently, organism 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.'),
  307. );
  308. }
  309. return system_settings_form($form);
  310. }
  311. /**
  312. *
  313. *
  314. * @ingroup tripal_organism
  315. */
  316. function get_tripal_organism_admin_form_cleanup_set(&$form) {
  317. $form['cleanup'] = array(
  318. '#type' => 'fieldset',
  319. '#title' => t('Clean Up')
  320. );
  321. $form['cleanup']['description'] = array(
  322. '#type' => 'item',
  323. '#value' => t("With Drupal and chado residing in different databases ".
  324. "it is possible that nodes in Drupal and organisms in Chado become ".
  325. "\"orphaned\". This can occur if an organism node in Drupal is ".
  326. "deleted but the corresponding chado organism is not and/or vice ".
  327. "versa. Click the button below to resolve these discrepancies."),
  328. '#weight' => 1,
  329. );
  330. $form['cleanup']['button'] = array(
  331. '#type' => 'submit',
  332. '#value' => t('Clean up orphaned organisms'),
  333. '#weight' => 2,
  334. );
  335. }
  336. /**
  337. *
  338. *
  339. * @ingroup tripal_organism
  340. */
  341. function get_tripal_organism_admin_form_taxonomy_set(&$form) {
  342. $form['taxonify'] = array(
  343. '#type' => 'fieldset',
  344. '#title' => t('Assign Drupal Taxonomy to Organism Features')
  345. );
  346. // get the list of libraries
  347. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  348. $previous_db = tripal_db_set_active('chado'); // use chado database
  349. $org_rset = db_query($sql);
  350. tripal_db_set_active($previous_db); // now use drupal database
  351. // iterate through all of the libraries
  352. $org_boxes = array();
  353. while ($organism = db_fetch_object($org_rset)) {
  354. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  355. }
  356. $form['taxonify']['description'] = array(
  357. '#type' => 'item',
  358. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  359. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  360. "for setting taxonomy only for features that belong to the selected organisms below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  361. '#weight' => 1,
  362. );
  363. $form['taxonify']['tx-organisms'] = array(
  364. '#title' => t('Organisms'),
  365. '#type' => t('checkboxes'),
  366. '#description' => t("Check the organisms whose features you want to reset taxonomy. Note: this list contains all organisms, even those that may not be synced."),
  367. '#required' => FALSE,
  368. '#prefix' => '<div id="lib_boxes">',
  369. '#suffix' => '</div>',
  370. '#options' => $org_boxes,
  371. '#weight' => 2
  372. );
  373. $form['taxonify']['tx-button'] = array(
  374. '#type' => 'submit',
  375. '#value' => t('Set Feature Taxonomy'),
  376. '#weight' => 3
  377. );
  378. }
  379. /**
  380. *
  381. * @ingroup tripal_organism
  382. */
  383. function get_tripal_organism_admin_form_reindex_set(&$form) {
  384. // define the fieldsets
  385. $form['reindex'] = array(
  386. '#type' => 'fieldset',
  387. '#title' => t('Reindex Organism Features')
  388. );
  389. // get the list of libraries
  390. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  391. $previous_db = tripal_db_set_active('chado'); // use chado database
  392. $org_rset = db_query($sql);
  393. tripal_db_set_active($previous_db); // now use drupal database
  394. // iterate through all of the libraries
  395. $org_boxes = array();
  396. while ($organism = db_fetch_object($org_rset)) {
  397. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  398. }
  399. $form['reindex']['description'] = array(
  400. '#type' => 'item',
  401. '#value' => t("This option allows for reindexing of only those features that belong to the selected organisms below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  402. '#weight' => 1,
  403. );
  404. $form['reindex']['re-organisms'] = array(
  405. '#title' => t('Organisms'),
  406. '#type' => t('checkboxes'),
  407. '#description' => t("Check the organisms whoee features you want to reindex. Note: this list contains all organisms, even those that may not be synced."),
  408. '#required' => FALSE,
  409. '#prefix' => '<div id="lib_boxes">',
  410. '#suffix' => '</div>',
  411. '#options' => $org_boxes,
  412. '#weight' => 2,
  413. );
  414. $form['reindex']['re-button'] = array(
  415. '#type' => 'submit',
  416. '#value' => t('Reindex Features'),
  417. '#weight' => 3,
  418. );
  419. }
  420. /**
  421. *
  422. * @ingroup tripal_organism
  423. */
  424. function get_tripal_organism_admin_form_sync_set(&$form) {
  425. // define the fieldsets
  426. $form['sync'] = array(
  427. '#type' => 'fieldset',
  428. '#title' => t('Sync Organisms')
  429. );
  430. // before proceeding check to see if we have any
  431. // currently processing jobs. If so, we don't want
  432. // to give the opportunity to sync libraries
  433. $active_jobs = FALSE;
  434. if (tripal_get_module_active_jobs('tripal_organism')) {
  435. $active_jobs = TRUE;
  436. }
  437. if (!$active_jobs) {
  438. // get the list of organisms
  439. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  440. $previous_db = tripal_db_set_active('chado'); // use chado database
  441. $org_rset = db_query($sql);
  442. tripal_db_set_active($previous_db); // now use drupal database
  443. // if we've added any organisms to the list that can be synced
  444. // then we want to build the form components to allow the user
  445. // to select one or all of them. Otherwise, just present
  446. // a message stating that all organisms are currently synced.
  447. $org_boxes = array();
  448. $added = 0;
  449. while ($organism = db_fetch_object($org_rset)) {
  450. // check to see if the organism is already present as a node in drupal.
  451. // if so, then skip it.
  452. $sql = "SELECT * FROM {chado_organism} WHERE organism_id = %d";
  453. if (!db_fetch_object(db_query($sql, $organism->organism_id))) {
  454. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  455. $added++;
  456. }
  457. }
  458. // if we have organisms we need to add to the checkbox then
  459. // build that form element
  460. if ($added > 0) {
  461. $org_boxes['all'] = "All Organisms";
  462. $form['sync']['organisms'] = array(
  463. '#title' => t('Available Organisms'),
  464. '#type' => t('checkboxes'),
  465. '#description' => t("Check the organisms you want to sync. Drupal content will be created for each of the organisms listed above. Select 'All Organisms' to sync all of them."),
  466. '#required' => FALSE,
  467. '#prefix' => '<div id="org_boxes">',
  468. '#suffix' => '</div>',
  469. '#options' => $org_boxes,
  470. );
  471. $form['sync']['button'] = array(
  472. '#type' => 'submit',
  473. '#value' => t('Submit Sync Job')
  474. );
  475. }
  476. // we don't have any organisms to select from
  477. else {
  478. $form['sync']['value'] = array(
  479. '#value' => t('All organisms in Chado are currently synced with Drupal.')
  480. );
  481. }
  482. }
  483. // we don't want to present a form since we have an active job running
  484. else {
  485. $form['sync']['value'] = array(
  486. '#value' => t('Currently, jobs exist related to chado organisms. Please check back later for organisms that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  487. );
  488. }
  489. }
  490. /**
  491. *
  492. * @ingroup tripal_organism
  493. */
  494. function tripal_organism_admin_validate($form, &$form_state) {
  495. global $user; // we need access to the user info
  496. $job_args = array();
  497. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  498. // check to see if the user wants to sync chado and drupal. If
  499. // so then we need to register a job to do so with tripal
  500. $organisms = $form_state['values']['organisms'];
  501. $do_all = FALSE;
  502. $to_sync = array();
  503. foreach ($organisms as $organism_id) {
  504. if (preg_match("/^all$/i" , $organism_id)) {
  505. $do_all = TRUE;
  506. }
  507. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  508. // get the list of organisms
  509. $sql = "SELECT * FROM {Organism} WHERE organism_id = %d";
  510. $previous_db = tripal_db_set_active('chado'); // use chado database
  511. $organism = db_fetch_object(db_query($sql, $organism_id));
  512. tripal_db_set_active($previous_db); // now use drupal database
  513. $to_sync[$organism_id] = "$organism->genus $organism->species";
  514. }
  515. }
  516. // submit the job the tripal job manager
  517. if ($do_all) {
  518. tripal_add_job('Sync all organisms' , 'tripal_organism',
  519. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  520. }
  521. else{
  522. foreach ($to_sync as $organism_id => $name) {
  523. $job_args[0] = $organism_id;
  524. tripal_add_job("Sync organism: $name" , 'tripal_organism',
  525. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  526. }
  527. }
  528. }
  529. // -------------------------------------
  530. // Submit the Reindex Job if selected
  531. if ($form_state['values']['op'] == t('Reindex Features')) {
  532. $organisms = $form_state['values']['re-organisms'];
  533. foreach ($organisms as $organism_id) {
  534. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  535. // get the organism info
  536. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  537. $previous_db = tripal_db_set_active('chado'); // use chado database
  538. $organism = db_fetch_object(db_query($sql , $organism_id));
  539. tripal_db_set_active($previous_db); // now use drupal database
  540. $job_args[0] = $organism_id;
  541. tripal_add_job("Reindex features for organism: $organism->genus ".
  542. "$organism->species", 'tripal_organism' ,
  543. 'tripal_organism_reindex_features', $job_args, $user->uid);
  544. }
  545. }
  546. }
  547. // -------------------------------------
  548. // Submit the taxonomy Job if selected
  549. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  550. $organisms = $form_state['values']['tx-organisms'];
  551. foreach ($organisms as $organism_id) {
  552. if ($organism_id and preg_match("/^\d+$/i", $organism_id)) {
  553. // get the organism info
  554. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  555. $previous_db = tripal_db_set_active('chado'); // use chado database
  556. $organism = db_fetch_object(db_query($sql , $organism_id));
  557. tripal_db_set_active($previous_db); // now use drupal database
  558. $job_args[0] = $organism_id;
  559. tripal_add_job("Set taxonomy for features in organism: ".
  560. "$organism->genus $organism->species" , 'tripal_organism',
  561. 'tripal_organism_taxonify_features', $job_args, $user->uid);
  562. }
  563. }
  564. }
  565. // -------------------------------------
  566. // Submit the Cleanup Job if selected
  567. if ($form_state['values']['op'] == t('Clean up orphaned organisms')) {
  568. tripal_add_job('Cleanup orphaned organisms', 'tripal_organism',
  569. 'tripal_organisms_cleanup', $job_args, $user->uid);
  570. }
  571. }
  572. /**
  573. * We need to let drupal know about our theme functions and their arguments.
  574. * We create theme functions to allow users of the module to customize the
  575. * look and feel of the output generated in this module
  576. *
  577. * @ingroup tripal_organism
  578. */
  579. function tripal_organism_theme() {
  580. return array(
  581. 'tripal_organism_organism_page' => array(
  582. 'arguments' => array('organisms'),
  583. ),
  584. 'tripal_organism_base' => array(
  585. 'arguments' => array('node' => NULL),
  586. 'template' => 'tripal_organism_base',
  587. ),
  588. 'tripal_organism_description' => array(
  589. 'arguments' => array('node' => NULL),
  590. 'template' => 'tripal_organism_description',
  591. ),
  592. 'tripal_organism_image' => array(
  593. 'arguments' => array('node' => NULL),
  594. 'template' => 'tripal_organism_image',
  595. ),
  596. );
  597. }
  598. /**
  599. *
  600. * @ingroup tripal_organism
  601. */
  602. function tripal_organism_nodeapi(&$node, $op, $teaser, $page) {
  603. switch ($op) {
  604. case 'view':
  605. switch ($node->type) {
  606. }
  607. }
  608. }
  609. /**
  610. *
  611. * @ingroup tripal_organism
  612. */
  613. function tripal_organism_cron() {
  614. // we want to make sure that any new organisms or features that were
  615. // added to the database external to drupal automatically get new
  616. // nodes created for themselves in drupal.
  617. // tripal_organism_sync_organisms();
  618. }
  619. /**
  620. *
  621. * @ingroup tripal_organism
  622. */
  623. function chado_organism_validate($node, &$form) {
  624. // if this is an update, we want to make sure that a different organism doesn't
  625. // already have this genus and speces
  626. if ($node->organism_id) {
  627. $sql = "SELECT *
  628. FROM {organism} O
  629. WHERE genus = '%s' and species = '%s' AND NOT organism_id = %d";
  630. $previous_db = tripal_db_set_active('chado');
  631. $result = db_fetch_object(db_query($sql, $node->genus, $node->species, $node->organism_id));
  632. tripal_db_set_active($previous_db);
  633. if ($result) {
  634. form_set_error('genus', t("Update cannot proceed. The organism genus
  635. '$node->genus' and species '$node->species' is already present in the database."));
  636. watchdog('tripal_organism',
  637. 'Update organism: genus and species already exists: %values',
  638. array('%values' => "genus = $node->genus, species = $node->species"),
  639. WATCHDOG_WARNING);
  640. }
  641. }
  642. // if this is an insert then check to make sure the genus and species are unique
  643. else {
  644. $values = array(
  645. 'genus' => $node->genus,
  646. 'species' => $node->species,
  647. );
  648. $organism = tripal_core_chado_select('organism', array('organism_id'), $values);
  649. if (sizeof($organism) > 0) {
  650. form_set_error('genus', 'Cannot add the organism with this genus and species.
  651. The organism already exists.');
  652. watchdog('tripal_organism',
  653. 'Insert organism: genus and species already exists: %values',
  654. array('%values' => "genus = $node->genus, species = $node->species"),
  655. WATCHDOG_WARNING);
  656. }
  657. }
  658. }
  659. /**
  660. * When a new chado_organism node is created we also need to add information
  661. * to our chado_organism table. This function is called on insert of a new node
  662. * of type 'chado_organism' and inserts the necessary information.
  663. *
  664. * @ingroup tripal_organism
  665. */
  666. function chado_organism_insert($node) {
  667. $values = array(
  668. 'genus' => $node->genus,
  669. 'species' => $node->species,
  670. 'abbreviation' => $node->abbreviation,
  671. 'common_name' => $node->common_name,
  672. 'comment' => $node->description
  673. );
  674. // if there is an organism_id in the $node object then this must be a sync so
  675. // we can skip adding the organism as it is already there, although
  676. // we do need to proceed with the rest of the insert
  677. if (!$node->organism_id) {
  678. $organism = tripal_core_chado_insert('organism', $values);
  679. if (!$organism) {
  680. drupal_set_message(t('Unable to add organism.', 'warning'));
  681. watchdog('tripal_organism',
  682. 'Insert Organism: Unable to create organism where values:%values',
  683. array('%values' => print_r($values, TRUE)),
  684. WATCHDOG_WARNING
  685. );
  686. return;
  687. }
  688. $organism_id = $organism['organism_id'];
  689. }
  690. else {
  691. $organism_id = $node->organism_id;
  692. }
  693. // Make sure the entry for this organism doesn't already exist in the
  694. // chado_organism table if it doesn't exist then we want to add it.
  695. if (!chado_get_id_for_node('organism', $node) ) {
  696. // next add the item to the drupal table
  697. $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ".
  698. "VALUES (%d, %d, %d)";
  699. chado_query($sql, $node->nid, $node->vid, $organism_id);
  700. }
  701. // set the title for the node
  702. $record = new stdClass();
  703. $record->title = "$node->genus $node->species";
  704. $record->nid = $node->nid;
  705. drupal_write_record('node', $record, 'nid');
  706. drupal_write_record('node_revisions', $record, 'nid');
  707. // add the image
  708. chado_organism_add_image($node);
  709. }
  710. /**
  711. * Update organisms
  712. *
  713. * @ingroup tripal_organism
  714. */
  715. function chado_organism_update($node) {
  716. if ($node->revision) {
  717. // TODO -- decide what to do about revisions
  718. }
  719. else {
  720. $match = array(
  721. 'organism_id' => chado_get_id_for_node('organism', $node),
  722. );
  723. $values = array(
  724. 'genus' => $node->genus,
  725. 'species' => $node->species,
  726. 'abbreviation' => $node->abbreviation,
  727. 'common_name' => $node->common_name,
  728. 'comment' => $node->description
  729. );
  730. $org_status = tripal_core_chado_update('organism', $match, $values);
  731. // set the title for the node
  732. $record = new stdClass();
  733. $record->title = "$node->genus $node->species";
  734. $record->nid = $node->nid;
  735. drupal_write_record('node', $record, 'nid');
  736. drupal_write_record('node_revisions', $record, 'nid');
  737. // add the image
  738. chado_organism_add_image($node);
  739. }
  740. }
  741. /**
  742. * Delete organism from both drupal and chado databases. Check dependency before
  743. * deleting from chado.
  744. *
  745. * @ingroup tripal_organism
  746. */
  747. function chado_organism_delete($node) {
  748. $organism_id = chado_get_id_for_node('organism', $node);
  749. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  750. $sql_del = "DELETE FROM {chado_organism} ".
  751. "WHERE nid = %d ".
  752. "AND vid = %d";
  753. db_query($sql_del, $node->nid, $node->vid);
  754. $sql_del = "DELETE FROM {node} ".
  755. "WHERE nid = %d ".
  756. "AND vid = %d";
  757. db_query($sql_del, $node->nid, $node->vid);
  758. $sql_del = "DELETE FROM {node_revisions} ".
  759. "WHERE nid = %d ".
  760. "AND vid = %d";
  761. db_query($sql_del, $node->nid, $node->vid);
  762. // Test dependency before deleting from chado database. If a library or
  763. // feature depends on this organism, don't delete it
  764. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d";
  765. $previous_db = tripal_db_set_active('chado');
  766. $check_feature = db_result(db_query($sql, $organism_id));
  767. $sql = "SELECT library_id FROM {library} WHERE organism_id = %d";
  768. $check_lib = db_result(db_query($sql, $organism_id));
  769. if ($check_lib == 0 && $check_feature == 0) {
  770. tripal_core_chado_delete('organism', array('organism_id' => $organism_id));
  771. }
  772. else {
  773. drupal_set_message(t("Organism deleted from drupal. Warning: at least one ".
  774. "library or feature depends on this organism. It was ".
  775. "not removed from chado."));
  776. }
  777. tripal_db_set_active($previous_db);
  778. }
  779. /**
  780. *
  781. *
  782. * @ingroup tripal_organism
  783. */
  784. function chado_organism_add_image($node) {
  785. // check to see if a file was uploaded. If so then copy it to the images
  786. // directory for display with the organism
  787. if (isset($_FILES['files']) && $_FILES['files']['name']['organism_image'] &&
  788. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  789. $dest = file_directory_path() . "/tripal/tripal_organism/images";
  790. $validators = array(
  791. 'file_validate_is_image' => array(),
  792. );
  793. file_check_directory($dest, FILE_CREATE_DIRECTORY, 'organism_image');
  794. if (!$file = file_save_upload('organism_image', $validators, $dest)) {
  795. drupal_set_message(t("Organism image was not uploaded."));
  796. }
  797. // move this image into the images directory
  798. file_move($file->filepath, $dest . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  799. }
  800. }
  801. /**
  802. * When editing or creating a new node of type 'chado_organism' we need
  803. * a form. This function creates the form that will be used for this.
  804. *
  805. * @ingroup tripal_organism
  806. */
  807. function chado_organism_form($node, $param) {
  808. $organism = $node->organism;
  809. // add in the comment since it is a text field and may not be included if too big
  810. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  811. // get form defaults
  812. $abbreviation = $node->abbreviation;
  813. if (!$abbreviation) {
  814. $abbreviation = $organism->abbreviation;
  815. }
  816. $genus = $node->genus;
  817. if (!$genus) {
  818. $genus = $organism->genus;
  819. }
  820. $species = $node->species;
  821. if (!$species) {
  822. $species = $organism->species;
  823. }
  824. $common_name = $node->common_name;
  825. if (!$common_name) {
  826. $common_name = $organism->common_name;
  827. }
  828. $description = $node->description;
  829. if (!$description) {
  830. $description = $organism->comment;
  831. }
  832. $organism_image = $node->organism_image;
  833. $form = array();
  834. $form['#attributes']['enctype'] = 'multipart/form-data';
  835. // keep track of the organism id if we have one. If we do have one then
  836. // this would indicate an update as opposed to an insert.
  837. $form['organism_id'] = array(
  838. '#type' => 'value',
  839. '#value' => $organism->organism_id,
  840. );
  841. $form['abbreviation']= array(
  842. '#type' => 'textfield',
  843. '#title' => t('Abbreviation'),
  844. '#required' => TRUE,
  845. '#default_value' => $organism->abbreviation,
  846. '#weight' => 3
  847. );
  848. $form['genus']= array(
  849. '#type' => 'textfield',
  850. '#title' => t('Genus'),
  851. '#required' => TRUE,
  852. '#default_value' => $organism->genus,
  853. '#weight' => 1
  854. );
  855. $form['species']= array(
  856. '#type' => 'textfield',
  857. '#title' => t('Species'),
  858. '#required' => TRUE,
  859. '#default_value' => $organism->species,
  860. '#weight' => 2
  861. );
  862. $form['common_name']= array(
  863. '#type' => 'textfield',
  864. '#title' => t('Common Name'),
  865. '#required' => TRUE,
  866. '#default_value' => $organism->common_name,
  867. '#weight' => 4
  868. );
  869. $form['description']= array(
  870. '#type' => 'textarea',
  871. '#rows' => 15,
  872. '#title' => t('Description'),
  873. '#required' => TRUE,
  874. '#default_value' => $organism->comment,
  875. '#weight' => 5
  876. );
  877. $form['organism_image']= array(
  878. '#type' => 'file',
  879. '#title' => t('Organism Image'),
  880. '#description' => 'Add an image for this organism',
  881. '#weight' => 6
  882. );
  883. return $form;
  884. }
  885. /**
  886. * When a node is requested by the user this function is called to allow us
  887. * to add auxiliary data to the node object.
  888. *
  889. * @ingroup tripal_organism
  890. */
  891. function chado_organism_load($node) {
  892. // find the organism and add in the details
  893. $organism_id = chado_get_id_for_node('organism', $node);
  894. $values = array('organism_id' => $organism_id);
  895. $organism = tripal_core_generate_chado_var('organism', $values);
  896. // add in the description field
  897. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  898. $additions = new stdClass();
  899. $additions->organism = $organism;
  900. return $additions;
  901. }
  902. /**
  903. * This function customizes the view of the chado_organism node. It allows
  904. * us to generate the markup.
  905. *
  906. * @ingroup tripal_organism
  907. */
  908. function chado_organism_view($node, $teaser = FALSE, $page = FALSE) {
  909. // use drupal's default node view:
  910. $node = node_prepare($node, $teaser);
  911. return $node;
  912. }
  913. /**
  914. * Synchronize organisms from chado to drupal
  915. *
  916. * @ingroup tripal_organism
  917. */
  918. function tripal_organism_sync_organisms($organism_id = NULL, $job_id = NULL) {
  919. global $user;
  920. $page_content = '';
  921. if (!$organism_id) {
  922. $sql = "SELECT * FROM {Organism} O";
  923. $previous_db = tripal_db_set_active('chado'); // use chado database
  924. $results = db_query($sql);
  925. tripal_db_set_active($previous_db); // now use drupal database
  926. }
  927. else {
  928. $sql = "SELECT * FROM {Organism} L WHERE organism_id = %d";
  929. $previous_db = tripal_db_set_active('chado'); // use chado database
  930. $results = db_query($sql, $organism_id);
  931. tripal_db_set_active($previous_db); // now use drupal database
  932. }
  933. // We'll use the following SQL statement for checking if the organism
  934. // already exists as a drupal node.
  935. $sql = "SELECT * FROM {chado_organism} ".
  936. "WHERE organism_id = %d";
  937. while ($organism = db_fetch_object($results)) {
  938. // check if this organism already exists in the drupal database. if it
  939. // does then skip this organism and go to the next one.
  940. if (!db_fetch_object(db_query($sql, $organism->organism_id))) {
  941. $new_node = new stdClass();
  942. $new_node->type = 'chado_organism';
  943. $new_node->uid = $user->uid;
  944. $new_node->title = "$organism->genus $organism->species";
  945. $new_node->organism_id = $organism->organism_id;
  946. $new_node->genus = $organism->genus;
  947. $new_node->species = $organism->species;
  948. $new_node->description = '';
  949. node_validate($new_node);
  950. if (!form_get_errors()) {
  951. $node = node_submit($new_node);
  952. node_save($node);
  953. if ($node->nid) {
  954. print "Added $organism->common_name\n";
  955. }
  956. }
  957. else {
  958. print "Failed to insert organism $organism->common_name\n";
  959. }
  960. }
  961. else {
  962. print "Skipped $organism->common_name\n";
  963. }
  964. }
  965. return $page_content;
  966. }
  967. /**
  968. * Display help and module information
  969. * @param path which path of the site we're displaying help
  970. * @param arg array that holds the current path as would be returned from arg()
  971. * function
  972. * @return help text for the path
  973. *
  974. * @ingroup tripal_organism
  975. */
  976. function tripal_organism_help($path, $arg) {
  977. $output = '';
  978. switch ($path) {
  979. case "admin/help#tripal_organism":
  980. $output = '<p>'.
  981. t("Displays links to nodes created on this date") .
  982. '</p>';
  983. break;
  984. }
  985. return $output;
  986. }
  987. /**
  988. * Display the summary view of organisms when click on the 'Organisms'
  989. * primary-link
  990. *
  991. * @ingroup tripal_organism
  992. */
  993. function tripal_organism_show_organisms() {
  994. // Show libraries stored in Drupal's {chado_organism} table
  995. $sql = "SELECT COUNT(organism_id) FROM {chado_organism}";
  996. $no_orgs = db_result(db_query($sql));
  997. if ($no_orgs != 0) {
  998. $organisms = get_chado_organisms();
  999. return theme('tripal_organism_organism_page', $organisms);
  1000. }
  1001. else {
  1002. return t("No organism exists. Please contact administrators to " .
  1003. "synchronize organisms.");
  1004. }
  1005. }
  1006. /**
  1007. *
  1008. * @ingroup tripal_organism
  1009. */
  1010. function theme_tripal_organism_organism_page($organisms) {
  1011. $output = "<br /><a id=\"tripal_expandableBox_toggle_button\" onClick=\"toggleExpandableBoxes()\">[-] Collapse All</a>";
  1012. foreach ($organisms as $organism) {
  1013. // Prepare information for html output
  1014. $org_url = url("node/$organism->node_id");
  1015. // Generate html output
  1016. $output .= "<div class=\"tripal_chado_organism-info-box\" style=\"padding:5px\">
  1017. <div class=\"tripal_expandableBox\">
  1018. <h3>$organism->common_name</h3>
  1019. </div>
  1020. <div class=\"tripal_expandableBoxContent\">
  1021. <span>
  1022. <table class=\"tripal_chado_analysis_content\">
  1023. <tr><td>
  1024. Name: <a href=\"$org_url\">$organism->common_name</a>
  1025. </td></tr>
  1026. <tr><td>
  1027. Genus: $organism->genus
  1028. </td></tr>
  1029. <tr><td>
  1030. Species: $organism->species
  1031. </td></tr>
  1032. <tr><td>
  1033. Description: $organism->comment
  1034. </td></tr>
  1035. </table>
  1036. </span>
  1037. </div>
  1038. </div>";
  1039. }
  1040. return $output;
  1041. }
  1042. /**
  1043. * This function uses organism_id's of all drupal organism nodes as input and
  1044. * pull the organism information (genus, species, common_name, comment) from
  1045. * chado database. The return type is an object array that stores $organism
  1046. * objects sorted by common_name
  1047. *
  1048. * @ingroup tripal_organism
  1049. */
  1050. function get_chado_organisms() {
  1051. $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}";
  1052. $no_orgs = db_result(db_query($sql_drupal));
  1053. if ($no_orgs != 0) {
  1054. $sql = "SELECT organism_id, nid FROM {chado_organism}";
  1055. $result = db_query($sql);
  1056. $previous_db = tripal_db_set_active('chado');
  1057. $sql = "SELECT genus, species, common_name, comment ".
  1058. "FROM {Organism} ".
  1059. "WHERE organism_id=%d";
  1060. $organisms = array();
  1061. $count = 0;
  1062. while ($data = db_fetch_object($result)) {
  1063. $organism = db_fetch_object(db_query($sql, $data->organism_id));
  1064. $organism->node_id = $data->nid;
  1065. // Use common_name plus $count as the key so we can sort by common
  1066. // name later. Since common_name is not unique, we need to add $count
  1067. // to the key
  1068. $key = drupal_strtolower($organism->common_name) . $count;
  1069. $organisms [$key] = $organism;
  1070. $count ++;
  1071. }
  1072. tripal_db_set_active($previous_db);
  1073. //Sort organisms by common name
  1074. ksort($organisms, SORT_STRING);
  1075. return $organisms;
  1076. }
  1077. }
  1078. /**
  1079. *
  1080. * @ingroup tripal_organism
  1081. */
  1082. function tripal_organism_reindex_features($organism_id = NULL, $job_id = NULL) {
  1083. $i = 0;
  1084. if (!$organism_id) {
  1085. return;
  1086. }
  1087. $sql = "SELECT * ".
  1088. "FROM {feature} ".
  1089. "WHERE organism_id = $organism_id ".
  1090. "ORDER BY feature_id";
  1091. $previous_db = tripal_db_set_active('chado'); // use chado database
  1092. $results = db_query($sql);
  1093. tripal_db_set_active($previous_db); // now use drupal databa tripal_db_set_active($previous_db); // now use drupal database
  1094. // load into ids array
  1095. $count = 0;
  1096. $ids = array();
  1097. while ($id = db_fetch_object($results)) {
  1098. $ids[$count] = $id->feature_id;
  1099. $count++;
  1100. }
  1101. $interval = intval($count * 0.01);
  1102. foreach ($ids as $feature_id) {
  1103. // update the job status every 1% features
  1104. if ($job_id and $i % $interval == 0) {
  1105. tripal_job_set_progress($job_id , intval(($i/$count)*100));
  1106. }
  1107. tripal_feature_sync_feature($feature_id);
  1108. $i++;
  1109. }
  1110. }
  1111. /**
  1112. *
  1113. * @ingroup tripal_organism
  1114. */
  1115. function tripal_organism_taxonify_features($organism_id = NULL, $job_id = NULL) {
  1116. $i = 0;
  1117. if (!$organism_id) {
  1118. return;
  1119. }
  1120. $sql = "SELECT * ".
  1121. "FROM {feature} ".
  1122. "WHERE organism_id = $organism_id ".
  1123. "ORDER BY feature_id";
  1124. $previous_db = tripal_db_set_active('chado'); // use chado database
  1125. $results = db_query($sql);
  1126. tripal_db_set_active($previous_db); // now use drupal database
  1127. // load into ids array
  1128. $count = 0;
  1129. $ids = array();
  1130. while ($id = db_fetch_object($results)) {
  1131. $ids[$count] = $id->feature_id;
  1132. $count++;
  1133. }
  1134. // make sure our vocabularies are set before proceeding
  1135. tripal_feature_set_vocabulary();
  1136. // use this SQL for getting the nodes
  1137. $nsql = "SELECT * FROM {chado_feature} CF ".
  1138. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1139. "WHERE feature_id = %d";
  1140. // iterate through the features and set the taxonomy
  1141. $interval = intval($count * 0.01);
  1142. foreach ($ids as $feature_id) {
  1143. // update the job status every 1% features
  1144. if ($job_id and $i % $interval == 0) {
  1145. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  1146. }
  1147. $node = db_fetch_object(db_query($nsql, $feature_id));
  1148. tripal_feature_set_taxonomy($node, $feature_id);
  1149. $i++;
  1150. }
  1151. }
  1152. /**
  1153. *
  1154. * @ingroup tripal_organism
  1155. */
  1156. function tripal_organisms_cleanup($dummy = NULL, $job_id = NULL) {
  1157. // build the SQL statments needed to check if nodes point to valid organisms
  1158. $dsql = "SELECT * FROM {node} WHERE type = 'chado_organism' order by nid";
  1159. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1160. $csql = "SELECT * FROM {chado_organism} where nid = %d ";
  1161. $cosql= "SELECT * FROM {chado_organism}";
  1162. $tsql = "SELECT * FROM {Organism} O ".
  1163. "WHERE organism_id = %d";
  1164. // load into nodes array
  1165. $results = db_query($dsql);
  1166. $count = 0;
  1167. $nodes = array();
  1168. while ($node = db_fetch_object($results)) {
  1169. $nodes[$count] = $node;
  1170. $count++;
  1171. }
  1172. // load the chado_organisms into an array
  1173. $results = db_query($cosql);
  1174. $cnodes = array();
  1175. while ($node = db_fetch_object($results)) {
  1176. $cnodes[$count] = $node;
  1177. $count++;
  1178. }
  1179. $interval = intval($count * 0.01);
  1180. // iterate through all of the chado_organism nodes and delete those that aren't valid
  1181. foreach ($nodes as $nid) {
  1182. // update the job status every 1% organisms
  1183. if ($job_id and $i % $interval == 0) {
  1184. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  1185. }
  1186. // first check to see if the node has a corresponding entry
  1187. // in the chado_organism table. If not then delete the node.
  1188. $organism = db_fetch_object(db_query($csql, $nid->nid));
  1189. if (!$organism) {
  1190. node_delete($nid->nid);
  1191. $message = "Missing in chado_organism table.... DELETING: $nid->nid\n";
  1192. watchdog('tripal_organism', $message, array(), WATCHDOG_WARNING);
  1193. continue;
  1194. }
  1195. $i++;
  1196. }
  1197. // iterate through all of the chado_organism nodes and delete those that aren't valid
  1198. foreach ($cnodes as $nid) {
  1199. // update the job status every 1% organisms
  1200. if ($job_id and $i % $interval == 0) {
  1201. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  1202. }
  1203. $node = db_fetch_object(db_query($nsql, $nid->nid));
  1204. if (!$node) {
  1205. db_query("DELETE FROM {chado_organism} WHERE nid = %d", $nid->nid);
  1206. $message = "chado_organism missing node.... DELETING: $nid->nid\n";
  1207. watchdog('tripal_organism', $message, array(), WATCHDOG_WARNING);
  1208. }
  1209. $i++;
  1210. }
  1211. return '';
  1212. }
  1213. /**
  1214. * Implements hook_views_api()
  1215. * Purpose: Essentially this hook tells drupal that there is views support for
  1216. * for this module which then includes tripal_db.views.inc where all the
  1217. * views integration code is
  1218. *
  1219. * @ingroup tripal_organism
  1220. */
  1221. function tripal_organism_views_api() {
  1222. return array(
  1223. 'api' => 2.0,
  1224. );
  1225. }
  1226. /**
  1227. *
  1228. *
  1229. * @ingroup tripal_organism
  1230. */
  1231. function tripal_organism_job_describe_args($callback, $args) {
  1232. $new_args = array();
  1233. if ($callback == 'tripal_organism_sync_organisms') {
  1234. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1235. $new_args['Organism'] = $organism[0]->genus ." ". $organism[0]->species;
  1236. }
  1237. return $new_args;
  1238. }