tripal_analysis.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. <?php
  2. // $Id:
  3. // Copyright 2009 Clemson University
  4. /*******************************************************************************
  5. * Note: When we pull information for an analysis from chado database. We use
  6. * 'analysisname' instead of just 'name' to avoid name collision with drupal's
  7. * node->name variable. Therefore, the SQL statement used is 'SELECT name AS
  8. * analysisname FROM Analysis', instead of 'SELECT name FROM Analysis'. All
  9. * other node variables have exact same name as the column name.
  10. ******************************************************************************/
  11. /*************************************************************************
  12. *
  13. */
  14. function tripal_analysis_register_child($modulename){
  15. $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES ('%s')";
  16. db_query($sql, $modulename);
  17. }
  18. /*************************************************************************
  19. *
  20. */
  21. function tripal_analysis_unregister_child($modulename){
  22. if (db_table_exists('tripal_analysis')) {
  23. $sql = "DELETE FROM {tripal_analysis} WHERE modulename = '%s'";
  24. db_query($sql, $modulename);
  25. }
  26. }
  27. /******************************************************************************
  28. * Tripal Analysis lets users display/hide analysis results associated
  29. * with a tripal feature. Load javascript when module initialized
  30. */
  31. function tripal_analysis_init(){
  32. drupal_add_js(drupal_get_path('theme', 'tripal').
  33. '/js/tripal_analysis.js');
  34. }
  35. /*******************************************************************************
  36. * tripal_analysis_menu()
  37. * HOOK: Implementation of hook_menu()
  38. * Entry points and paths of the module
  39. */
  40. function tripal_analysis_menu() {
  41. // Display available analyses
  42. $items['analyses'] = array(
  43. 'menu_name' => ('primary-links'), //Enable the 'Analysis' primary link
  44. 'title' => t('Analyses'),
  45. 'page callback' => 'tripal_analysis_show_analyses',
  46. 'access arguments' => array('access chado_analysis content'),
  47. 'type' => MENU_NORMAL_ITEM
  48. );
  49. //Sync analysis
  50. $items['chado_sync_analyses'] = array(
  51. 'title' => t('Sync Data'),
  52. 'page callback' => 'tripal_analysis_sync_analyses',
  53. 'access arguments' => array('administer site configuration'),
  54. 'type' => MENU_CALLBACK
  55. );
  56. // Tripal Analysis administrative settings
  57. $items['admin/tripal/tripal_analysis'] = array(
  58. 'title' => 'Analyses',
  59. 'description' => 'Settings for the displays of analysis results.',
  60. 'page callback' => 'drupal_get_form',
  61. 'page arguments' => array('tripal_analysis_admin'),
  62. 'access arguments' => array('administer site configuration'),
  63. 'type' => MENU_NORMAL_ITEM,
  64. 'file' => 'tripal_analysis.admin.inc',
  65. );
  66. return $items;
  67. }
  68. /*******************************************************************************
  69. * Display the summary view of analyses when click on the 'Analyses'
  70. * primary-link
  71. */
  72. function tripal_analysis_show_analyses (){
  73. // Show libraries stored in Drupal's {chado_analysis} table
  74. $sql = "SELECT COUNT(analysis_id) FROM {chado_analysis}";
  75. $no_ana = db_result(db_query ($sql));
  76. if($no_ana != 0) {
  77. $analyses = get_chado_analyses ();
  78. return theme('tripal_analysis_analysis_page', $analyses);
  79. } else {
  80. return t("No analysis available at this time.");
  81. }
  82. }
  83. /*******************************************************************************
  84. * Provide information to drupal about the node types that we're creating
  85. * in this module
  86. */
  87. function tripal_analysis_node_info() {
  88. $nodes = array();
  89. $nodes['chado_analysis'] = array(
  90. 'name' => t('Analysis'),
  91. 'module' => 'chado_analysis',
  92. 'description' => t('An analysis from the chado database'),
  93. 'has_title' => FALSE,
  94. 'title_label' => t('Analysis'),
  95. 'has_body' => FALSE,
  96. 'body_label' => t('Analysis Description'),
  97. 'locked' => TRUE
  98. );
  99. return $nodes;
  100. }
  101. /*******************************************************************************
  102. * When a new chado_analysis node is created we also need to add information
  103. * to our chado_analysis table. This function is called on insert of a new
  104. * node of type 'chado_analysis' and inserts the necessary information.
  105. */
  106. function chado_analysis_insert($node){
  107. global $user;
  108. // Create a timestamp so we can insert it into the chado database
  109. $time = $node->timeexecuted;
  110. $month = $time['month'];
  111. $day = $time['day'];
  112. $year = $time['year'];
  113. $timestamp = $month.'/'.$day.'/'.$year;
  114. // If this analysis already exists then don't recreate it in chado
  115. $analysis_id = $node->analysis_id;
  116. if ($analysis_id) {
  117. $sql = "SELECT analysis_id ".
  118. "FROM {Analysis} ".
  119. "WHERE analysis_id = %d ";
  120. $previous_db = db_set_active('chado');
  121. $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
  122. db_set_active($previous_db);
  123. }
  124. // If the analysis doesn't exist then let's create it in chado.
  125. if(!$analysis){
  126. // First add the item to the chado analysis table
  127. $sql = "INSERT INTO {analysis} ".
  128. " (name, description, program, programversion, algorithm, ".
  129. " sourcename, sourceversion, sourceuri, timeexecuted) ".
  130. "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')";
  131. $previous_db = db_set_active('chado'); // use chado database
  132. db_query($sql,$node->analysisname, $node->description,
  133. $node->program,$node->programversion,$node->algorithm,
  134. $node->sourcename, $node->sourceversion, $node->sourceuri,
  135. $timestamp);
  136. db_set_active($previous_db);
  137. }
  138. // Make sure the entry for this analysis doesn't already exist in the
  139. // chado_analysis table if it doesn't exist then we want to add it.
  140. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  141. "WHERE analysis_id = %d";
  142. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  143. if(!$node_check){
  144. // next add the item to the drupal table
  145. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  146. "VALUES (%d, %d, %d)";
  147. db_query($sql,$node->nid,$node->vid,$analysis_id);
  148. // Create a title for the analysis node using the unique keys so when the
  149. // node is saved, it will have a title
  150. $record = new stdClass();
  151. // If the analysis has a name, use it as the node title. If not, construct
  152. // the title using program, programversion, and sourcename
  153. if ($node->analysisname) {
  154. $record->title = $node->analysisname;
  155. } else {
  156. //Construct node title as "program (version)
  157. $record->title = "$node->program ($node->programversion)";
  158. }
  159. $record->nid = $node->nid;
  160. drupal_write_record('node',$record,'nid');
  161. drupal_write_record('node_revisions',$record,'nid');
  162. }
  163. }
  164. /*******************************************************************************
  165. */
  166. function chado_analysis_delete($node){
  167. // Before removing, get analysis_id so we can remove it from chado database
  168. // later
  169. $sql_drupal = "SELECT analysis_id ".
  170. "FROM {chado_analysis} ".
  171. "WHERE nid = %d ".
  172. "AND vid = %d";
  173. $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  174. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  175. $sql_del = "DELETE FROM {chado_analysis} ".
  176. "WHERE nid = %d ".
  177. "AND vid = %d";
  178. db_query($sql_del, $node->nid, $node->vid);
  179. $sql_del = "DELETE FROM {node} ".
  180. "WHERE nid = %d ".
  181. "AND vid = %d";
  182. db_query($sql_del, $node->nid, $node->vid);
  183. $sql_del = "DELETE FROM {node_revisions} ".
  184. "WHERE nid = %d ".
  185. "AND vid = %d";
  186. db_query($sql_del, $node->nid, $node->vid);
  187. //Remove from analysis and analysisprop tables of chado database as well
  188. $previous_db = db_set_active('chado');
  189. db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  190. db_set_active($previous_db);
  191. }
  192. /*******************************************************************************
  193. * Update analyses
  194. */
  195. function chado_analysis_update($node){
  196. global $user;
  197. if($node->revision){
  198. // TODO -- decide what to do about revisions
  199. } else {
  200. // Create a timestamp so we can insert it into the chado database
  201. $time = $node->timeexecuted;
  202. $month = $time['month'];
  203. $day = $time['day'];
  204. $year = $time['year'];
  205. $timestamp = $month.'/'.$day.'/'.$year;
  206. // get the analysis_id for this node:
  207. $sql = "SELECT analysis_id ".
  208. "FROM {chado_analysis} ".
  209. "WHERE vid = %d";
  210. $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
  211. $sql = "UPDATE {analysis} ".
  212. "SET name = '%s', ".
  213. " description = '%s', ".
  214. " program = '%s', ".
  215. " programversion = '%s', ".
  216. " algorithm = '%s', ".
  217. " sourcename = '%s', ".
  218. " sourceversion = '%s', ".
  219. " sourceuri = '%s', ".
  220. " timeexecuted = '%s' ".
  221. "WHERE analysis_id = %d ";
  222. $previous_db = db_set_active('chado'); // use chado database
  223. db_query($sql, $node->analysisname, $node->description, $node->program,
  224. $node->programversion,$node->algorithm,$node->sourcename,
  225. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  226. db_set_active($previous_db); // switch back to drupal database
  227. // Create a title for the analysis node using the unique keys so when the
  228. // node is saved, it will have a title
  229. $record = new stdClass();
  230. // If the analysis has a name, use it as the node title. If not, construct
  231. // the title using program, programversion, and sourcename
  232. if ($node->analysisname) {
  233. $record->title = $node->analysisname;
  234. } else {
  235. //Construct node title as "program (version)
  236. $record->title = "$node->program ($node->programversion)";
  237. }
  238. $record->nid = $node->nid;
  239. drupal_write_record('node',$record,'nid');
  240. drupal_write_record('node_revisions',$record,'nid');
  241. }
  242. }
  243. /*******************************************************************************
  244. * When editing or creating a new node of type 'chado_analysis' we need
  245. * a form. This function creates the form that will be used for this.
  246. */
  247. function chado_analysis_form ($node){
  248. $type = node_get_types('type', $node);
  249. $form = array();
  250. $form['title']= array(
  251. '#type' => 'hidden',
  252. '#default_value' => $node->title,
  253. );
  254. $form['analysisname']= array(
  255. '#type' => 'textfield',
  256. '#title' => t('Analysis Name'),
  257. '#required' => FALSE,
  258. '#default_value' => $node->analysisname,
  259. '#weight' => 1
  260. );
  261. $form['program']= array(
  262. '#type' => 'textfield',
  263. '#title' => t('Program'),
  264. '#required' => TRUE,
  265. '#default_value' => $node->program,
  266. '#weight' => 2
  267. );
  268. $form['programversion']= array(
  269. '#type' => 'textfield',
  270. '#title' => t('Program Version'),
  271. '#required' => TRUE,
  272. '#default_value' => $node->programversion,
  273. '#weight' => 3
  274. );
  275. $form['algorithm']= array(
  276. '#type' => 'textfield',
  277. '#title' => t('Algorithm'),
  278. '#required' => FALSE,
  279. '#default_value' => $node->algorithm,
  280. '#weight' => 4
  281. );
  282. $form['sourcename']= array(
  283. '#type' => 'textfield',
  284. '#title' => t('Source Name'),
  285. '#required' => FALSE,
  286. '#default_value' => $node->sourcename,
  287. '#weight' => 5
  288. );
  289. $form['sourceversion']= array(
  290. '#type' => 'textfield',
  291. '#title' => t('Source Version'),
  292. '#required' => FALSE,
  293. '#default_value' => $node->sourceversion,
  294. '#weight' => 6
  295. );
  296. $form['sourceuri']= array(
  297. '#type' => 'textfield',
  298. '#title' => t('Source URI'),
  299. '#required' => FALSE,
  300. '#default_value' => $node->sourceuri,
  301. '#weight' => 7
  302. );
  303. // Get time saved in chado
  304. $default_time = $node->timeexecuted;
  305. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  306. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  307. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  308. // If the time is not set, use current time
  309. if (!$default_time) {
  310. $default_time = time();
  311. $year = format_date($default_time, 'custom', 'Y');
  312. $month = format_date($default_time, 'custom', 'n');
  313. $day = format_date($default_time, 'custom', 'j');
  314. }
  315. $form['timeexecuted']= array(
  316. '#type' => 'date',
  317. '#title' => t('Time Executed'),
  318. '#required' => TRUE,
  319. '#default_value' => array(
  320. 'year' => $year,
  321. 'month' => $month,
  322. 'day' => $day,
  323. ),
  324. '#weight' => 8
  325. );
  326. $form['description']= array(
  327. '#type' => 'textarea',
  328. '#rows' => 15,
  329. '#title' => t('Description and/or Program Settings'),
  330. '#required' => FALSE,
  331. '#default_value' => check_plain($node->description),
  332. '#weight' => 9
  333. );
  334. return $form;
  335. }
  336. /*******************************************************************************
  337. * When a node is requested by the user this function is called to allow us
  338. * to add auxiliary data to the node object.
  339. */
  340. function chado_analysis_load($node){
  341. // get the analysis_id for this node:
  342. $sql = "SELECT analysis_id FROM {chado_analysis} WHERE vid = %d";
  343. $ana_node = db_fetch_object(db_query($sql, $node->vid));
  344. $additions = new stdClass();
  345. if ($ana_node) {
  346. // get analysis information
  347. $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
  348. " programversion, algorithm, sourcename, sourceversion, ".
  349. " sourceuri, timeexecuted ".
  350. "FROM {Analysis} ".
  351. "WHERE Analysis_id = $ana_node->analysis_id";
  352. $previous_db = db_set_active('chado'); // use chado database
  353. $additions = db_fetch_object(db_query($sql));
  354. db_set_active($previous_db); // now use drupal database
  355. }
  356. // If the analysis has a name, use it as the node title. If not, construct
  357. // the title using program programversion, and sourcename
  358. if ($additions->analysisname) {
  359. $additions->title = $additions->analysisname;
  360. } else {
  361. // Construct node title as "program version (source)
  362. $additions->title = "$additions->program ($additions->programversion)";
  363. }
  364. return $additions;
  365. }
  366. /*******************************************************************************
  367. * This function customizes the view of the chado_analysis node. It allows
  368. * us to generate the markup.
  369. */
  370. function chado_analysis_view ($node, $teaser = FALSE, $page = FALSE) {
  371. // use drupal's default node view:
  372. if (!$teaser) {
  373. $node = node_prepare($node, $teaser);
  374. // When previewing a node submitting form, it shows 'Array' instead of
  375. // correct date format. We need to format the date here
  376. $time = $node->timeexecuted;
  377. if(is_array($time)){
  378. $month = $time['month'];
  379. $day = $time['day'];
  380. $year = $time['year'];
  381. $timestamp = $year.'-'.$month.'-'.$day;
  382. $node->timeexecuted = $timestamp;
  383. }
  384. }
  385. return $node;
  386. }
  387. /*******************************************************************************
  388. * Synchronize analyses from chado to drupal
  389. */
  390. function tripal_analysis_sync_analyses ($analysis_id = NULL, $job_id = NULL){
  391. global $user;
  392. $page_content = '';
  393. if(!$analysis_id){
  394. $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
  395. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  396. " timeexecuted ".
  397. "FROM {Analysis} ".
  398. "ORDER BY analysis_id";
  399. $previous_db = db_set_active('chado'); // use chado database
  400. $results = db_query($sql);
  401. db_set_active($previous_db); // now use drupal database
  402. } else {
  403. $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
  404. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  405. " timeexecuted ".
  406. "FROM {Analysis} ".
  407. "WHERE analysis_id = %d";
  408. "ORDER BY analysis_id";
  409. $previous_db = db_set_active('chado'); // use chado database
  410. $results = db_query($sql,$analysis_id);
  411. db_set_active($previous_db); // now use drupal database
  412. }
  413. // We'll use the following SQL statement for checking if the analysis
  414. // already exists as a drupal node.
  415. $sql = "SELECT * FROM {chado_analysis} ".
  416. "WHERE analysis_id = %d";
  417. while($analysis = db_fetch_object($results)){
  418. // check if this analysis already exists in the drupal database. if it
  419. // does then skip this analysis and go to the next one.
  420. if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
  421. $new_node = new stdClass();
  422. // try to access analysisprop for this analysis
  423. $sql = "SELECT * FROM {analysisprop} WHERE analysis_id = %d";
  424. $previous_db = db_set_active('chado');
  425. $analysisprop = db_fetch_object(db_query($sql, $analysis->analysis_id));
  426. // If analysisprop exists, this analysis can be a blast analysis or
  427. // interpro analysis. Get its type using cvterm_id
  428. if ($analysisprop) {
  429. $sql = "SELECT name, definition FROM {cvterm} WHERE cvterm_id = %d";
  430. $result = db_fetch_object(db_query($sql, $analysisprop->type_id));
  431. // This is a blast analysis
  432. if ($result->name == 'analysis_blast_settings') {
  433. $new_node->type = 'chado_analysis_blast';
  434. // This is a interpro analysis
  435. } else if ($result->name == 'analysis_interpro_settings') {
  436. $new_node->type = 'chado_analysis_interpro';
  437. // This is a kegg analysis
  438. } else if ($result->name == 'analysis_kegg_settings' ||
  439. $result->name == 'kegg_brite_data' ||
  440. preg_match("/KEGG BRITE term:/", $result->definition)) {
  441. $new_node->type = 'chado_analysis_kegg';
  442. } else {
  443. $new_node->type = 'chado_analysis';
  444. }
  445. // If it doesn't exist, this analysis is generic
  446. } else {
  447. $new_node->type = 'chado_analysis';
  448. }
  449. db_set_active($previous_db);
  450. $new_node->uid = $user->uid;
  451. $new_node->analysis_id = $analysis->analysis_id;
  452. $new_node->analysisname = $analysis->analysisname;
  453. $new_node->description = $analysis->description;
  454. $new_node->program = $analysis->program;
  455. $new_node->programversion = $analysis->programversion;
  456. $new_node->algorithm = $analysis->algorithm;
  457. $new_node->sourcename = $analysis->sourcename;
  458. $new_node->sourceversion = $analysis->sourceversion;
  459. $new_node->sourceuri = $analysis->sourceuri;
  460. $new_node->timeexecuted = $analysis->timeexecuted;
  461. // If the analysis has a name, use it as the node title. If not,
  462. // construct the title using program, programversion, and sourcename
  463. if ($new_node->analysisname) {
  464. $new_node->title = $new_node->analysisname;
  465. } else {
  466. //Construct node title as "program (version)
  467. $new_node->title = "$analysis->program ($analysis->programversion)";
  468. }
  469. node_validate($new_node);
  470. if(!form_get_errors()){
  471. $node = node_submit($new_node);
  472. node_save($node);
  473. if($node->nid){
  474. $page_content .= "Added $new_node->title<br>";
  475. }
  476. }
  477. } else {
  478. $page_content .= "Skipped $new_node->title<br>";
  479. }
  480. }
  481. return $page_content;
  482. }
  483. /*******************************************************************************
  484. * Display help and module information
  485. * @param path which path of the site we're displaying help
  486. * @param arg array that holds the current path as would be returned from arg()
  487. * function
  488. * @return help text for the path
  489. */
  490. function tripal_analysis_help($path, $arg) {
  491. $output = '';
  492. switch ($path) {
  493. case "admin/help#tripal_analysis":
  494. $output = '<p>'.
  495. t("Displays links to nodes created on this date").
  496. '</p>';
  497. break;
  498. }
  499. return $output;
  500. }
  501. /*******************************************************************************
  502. * The following function proves access control for users trying to
  503. * perform actions on data managed by this module
  504. */
  505. function chado_analysis_access($op, $node, $account){
  506. if ($op == 'create') {
  507. return user_access('create chado_analysis content', $account);
  508. }
  509. if ($op == 'update') {
  510. if (user_access('edit chado_analysis content', $account)) {
  511. return TRUE;
  512. }
  513. }
  514. if ($op == 'delete') {
  515. if (user_access('delete chado_analysis content', $account)) {
  516. return TRUE;
  517. }
  518. }
  519. if ($op == 'view') {
  520. if (user_access('access chado_analysis content', $account)) {
  521. return TRUE;
  522. }
  523. }
  524. return FALSE;
  525. }
  526. /*******************************************************************************
  527. * Set the permission types that the chado module uses. Essentially we
  528. * want permissionis that protect creation, editing and deleting of chado
  529. # data objects
  530. */
  531. function tripal_analysis_perm(){
  532. return array(
  533. 'access chado_analysis content',
  534. 'create chado_analysis content',
  535. 'delete chado_analysis content',
  536. 'edit chado_analysis content',
  537. );
  538. }
  539. /*******************************************************************************
  540. * We need to let drupal know about our theme functions and their arguments.
  541. * We create theme functions to allow users of the module to customize the
  542. * look and feel of the output generated in this module
  543. */
  544. function tripal_analysis_theme () {
  545. return array(
  546. 'tripal_analysis_analysis_page' => array (
  547. 'arguments' => array('analyses'),
  548. ),
  549. );
  550. }
  551. /*******************************************************************************
  552. * This function uses analysis_id's of all drupal analysis nodes as input and
  553. * pull the analysis information (name, description, program, programversion,
  554. * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
  555. * chado database. The return type is an object array that stores $analysis
  556. * objects sorted by program
  557. */
  558. function get_chado_analyses() {
  559. $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
  560. $no_orgs = db_result(db_query($sql_drupal));
  561. if ($no_orgs != 0) {
  562. $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
  563. $result = db_query($sql);
  564. $previous_db = db_set_active('chado');
  565. $sql = "SELECT Analysis_id, name AS analysisname, description, program,
  566. programversion, algorithm, sourcename, sourceversion,
  567. sourceuri, timeexecuted
  568. FROM {Analysis} WHERE analysis_id=%d";
  569. $analyses = array();
  570. $count = 0;
  571. while ($data = db_fetch_object($result)) {
  572. $analysis = db_fetch_object(db_query($sql, $data->analysis_id));
  573. $analysis->node_id = $data->nid;
  574. $analysis->node_type = $data->type;
  575. // Use node_type as the key so we can sort by node type
  576. // Since node_type is not unique by itself, we need to add
  577. // $count to the key
  578. $sortedBy = $analysis->timeexecuted;
  579. $analyses ["$sortedBy$count"] = $analysis;
  580. $count ++;
  581. }
  582. db_set_active($previous_db);
  583. //Sort analyses by time, descending order
  584. krsort($analyses, SORT_STRING);
  585. return $analyses;
  586. }
  587. }
  588. /************************************************************************
  589. *
  590. */
  591. function theme_tripal_analysis_analysis_page($analyses) {
  592. $output = "<br>Analyses are listed in the descending order of their execution time.<br><a id=\"tripal_expandableBox_toggle_button\" onClick=\"toggleExpandableBoxes()\">[-] Collapse All</a>";
  593. foreach($analyses as $analysis){
  594. // Prepare information for html output
  595. $ana_node_url = url("node/$analysis->node_id");
  596. if ($analysis->sourceversion) {
  597. $ver = "($analysis->sourceversion)";
  598. }
  599. $date = preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted);
  600. // Generate html output
  601. $output .= "<div class=\"tripal_chado_analysis-info-box\" style=\"padding:5px\">
  602. <div class=\"tripal_expandableBox\">
  603. <h3>$analysis->analysisname ($date)</h3>
  604. </div>
  605. <div class=\"tripal_expandableBoxContent\">
  606. <span>
  607. <table class=\"tripal_chado_analysis_content\">
  608. <tr><td>
  609. Name: <a href=\"$ana_node_url\">$analysis->analysisname</a>
  610. </td></tr>
  611. <tr><td>
  612. Program: $analysis->program ($analysis->programversion)
  613. </td></tr>
  614. <tr><td>
  615. Algorithm: $analysis->algorithm
  616. </td></tr>
  617. <tr><td>
  618. Source: $analysis->sourcename $ver
  619. </td></tr>
  620. <tr><td>
  621. Source URI: $analysis->sourceuri
  622. </td></tr>
  623. <tr><td>
  624. Executed Time:$date
  625. </td></tr>
  626. <tr><td>
  627. Description: $analysis->description
  628. </td></tr>
  629. </table>
  630. </span>
  631. </div>
  632. </div>";
  633. }
  634. return $output;
  635. }
  636. /************************************************************************
  637. *
  638. */
  639. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  640. // build the SQL statments needed to check if nodes point to valid analyses
  641. $dsql = "SELECT * FROM {node} WHERE type = 'chado_analysis' order by nid";
  642. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  643. $csql = "SELECT * FROM {chado_analysis} where nid = %d ";
  644. $cosql= "SELECT * FROM {chado_analysis}";
  645. $tsql = "SELECT * FROM {analysis} O ".
  646. "WHERE analysis_id = %d";
  647. // load into nodes array
  648. $results = db_query($dsql);
  649. $count = 0;
  650. $nodes = array();
  651. while($node = db_fetch_object($results)){
  652. $nodes[$count] = $node;
  653. $count++;
  654. }
  655. // load the chado_analyses into an array
  656. $results = db_query($cosql);
  657. $cnodes = array();
  658. while($node = db_fetch_object($results)){
  659. $cnodes[$count] = $node;
  660. $count++;
  661. }
  662. $interval = intval($count * 0.01);
  663. // iterate through all of the chado_analysis nodes and delete those that aren't valid
  664. foreach($nodes as $nid){
  665. // update the job status every 1% analyses
  666. if($job_id and $i % $interval == 0){
  667. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  668. }
  669. // first check to see if the node has a corresponding entry
  670. // in the chado_analysis table. If not then delete the node.
  671. $analysis = db_fetch_object(db_query($csql,$nid->nid));
  672. if(!$analysis){
  673. node_delete($nid->nid);
  674. $message = "Missing in chado_analysis table.... DELETING: $nid->nid\n";
  675. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  676. continue;
  677. }
  678. $i++;
  679. }
  680. // iterate through all of the chado_analysis nodes and delete those that aren't valid
  681. foreach($cnodes as $nid){
  682. // update the job status every 1% analyses
  683. if($job_id and $i % $interval == 0){
  684. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  685. }
  686. $node = db_fetch_object(db_query($nsql,$nid->nid));
  687. if(!$node){
  688. db_query("DELETE FROM {chado_analysis} WHERE nid = $nid->nid");
  689. $message = "chado_analysis missing node.... DELETING: $nid->nid\n";
  690. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  691. }
  692. $i++;
  693. }
  694. return '';
  695. }
  696. /*******************************************************************************
  697. *
  698. */
  699. function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
  700. $i = 0;
  701. // if the caller provided a analysis_id then get all of the features
  702. // associated with the analysis. Otherwise get all sequences assoicated
  703. // with all libraries.
  704. if(!$analysis_id){
  705. $sql = "SELECT Analysis_id, Feature_id ".
  706. "FROM {Analysisfeature} ".
  707. "ORDER BY analysis_id";
  708. $previous_db = db_set_active('chado'); // use chado database
  709. $results = db_query($sql);
  710. db_set_active($previous_db); // now use drupal database
  711. } else {
  712. $sql = "SELECT Analysis_id, Feature_id ".
  713. "FROM {Analysisfeature} ".
  714. "WHERE analysis_id = %d";
  715. "ORDER BY analysis_id";
  716. $previous_db = db_set_active('chado'); // use chado database
  717. $results = db_query($sql,$analysis_id);
  718. db_set_active($previous_db); // now use drupal database
  719. }
  720. // load into ids array
  721. $count = 0;
  722. $ids = array();
  723. while($id = db_fetch_object($results)){
  724. $ids[$count] = $id->feature_id;
  725. $count++;
  726. }
  727. $interval = intval($count * 0.01);
  728. foreach($ids as $feature_id){
  729. // update the job status every 1% features
  730. if($job_id and $i % interval == 0){
  731. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  732. }
  733. tripal_feature_sync_feature ($feature_id);
  734. $i++;
  735. }
  736. }
  737. /*******************************************************************************
  738. *
  739. */
  740. function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
  741. $i = 0;
  742. // if the caller provided a analysis_id then get all of the features
  743. // associated with the analysis. Otherwise get all sequences assoicated
  744. // with all libraries.
  745. if(!$analysis_id){
  746. $sql = "SELECT Analysis_id, Feature_id ".
  747. "FROM {Analysisfeature} ".
  748. "ORDER BY analysis_id";
  749. $previous_db = db_set_active('chado'); // use chado database
  750. $results = db_query($sql);
  751. db_set_active($previous_db); // now use drupal database
  752. } else {
  753. $sql = "SELECT Analysis_id, Feature_id ".
  754. "FROM {Analysisfeature} ".
  755. "WHERE analysis_id = %d";
  756. "ORDER BY analysis_id";
  757. $previous_db = db_set_active('chado'); // use chado database
  758. $results = db_query($sql,$analysis_id);
  759. db_set_active($previous_db); // now use drupal database
  760. }
  761. // load into ids array
  762. $count = 0;
  763. $ids = array();
  764. while($id = db_fetch_object($results)){
  765. $ids[$count] = $id->feature_id;
  766. $count++;
  767. }
  768. // make sure our vocabularies are set before proceeding
  769. tripal_feature_set_vocabulary();
  770. // use this SQL for getting the nodes
  771. $nsql = "SELECT * FROM {chado_feature} CF ".
  772. " INNER JOIN {node} N ON N.nid = CF.nid ".
  773. "WHERE feature_id = %d";
  774. // iterate through the features and set the taxonomy
  775. $interval = intval($count * 0.01);
  776. foreach($ids as $feature_id){
  777. // update the job status every 1% features
  778. if($job_id and $i % interval == 0){
  779. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  780. }
  781. $node = db_fetch_object(db_query($nsql,$feature_id));
  782. tripal_feature_set_taxonomy($node,$feature_id);
  783. $i++;
  784. }
  785. }