tripal_organism.module 51 KB

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