tripal_organism.module 50 KB

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