tripal_organism.module 50 KB

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