tripal_analysis.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. $previous_db = db_set_active('chado'); // use chado database
  399. $results = db_query($sql);
  400. db_set_active($previous_db); // now use drupal database
  401. } else {
  402. $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
  403. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  404. " timeexecuted ".
  405. "FROM {Analysis} ".
  406. "WHERE analysis_id = %d";
  407. $previous_db = db_set_active('chado'); // use chado database
  408. $results = db_query($sql,$analysis_id);
  409. db_set_active($previous_db); // now use drupal database
  410. }
  411. ##REMOVE
  412. print "results:\n";
  413. print $results;
  414. print "\n";
  415. // We'll use the following SQL statement for checking if the analysis
  416. // already exists as a drupal node.
  417. $sql = "SELECT * FROM {chado_analysis} ".
  418. "WHERE analysis_id = %d";
  419. while($analysis = db_fetch_object($results)){
  420. // check if this analysis already exists in the drupal database. if it
  421. // does then skip this analysis and go to the next one.
  422. if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
  423. $new_node = new stdClass();
  424. // try to access analysisprop for this analysis
  425. $sql = "SELECT * FROM {analysisprop} WHERE analysis_id = %d";
  426. $previous_db = db_set_active('chado');
  427. $analysisprop = db_fetch_object(db_query($sql, $analysis->analysis_id));
  428. db_set_active($previous_db);
  429. // Get the type of analysis using cvterm_id
  430. // Current possibilities: kegg, unigene, interpro, blast
  431. if ($analysisprop) {
  432. $sql = "SELECT name, definition FROM {cvterm} WHERE cvterm_id = %d";
  433. $previous_db = db_set_active('chado');
  434. $result = db_fetch_object(db_query($sql, $analysisprop->type_id));
  435. db_set_active($previous_db);
  436. // This is a unigene analysis
  437. if ($result->name == 'analysis_unigene_settings') {
  438. $new_node->type = 'chado_unigene_blast';
  439. // This is a blast analysis
  440. } else if ($result->name == 'analysis_blast_settings') {
  441. $new_node->type = 'chado_analysis_blast';
  442. // This is a interpro analysis
  443. } else if ($result->name == 'analysis_interpro_settings') {
  444. $new_node->type = 'chado_analysis_interpro';
  445. // This is a kegg analysis
  446. } else if ($result->name == 'analysis_kegg_settings' ||
  447. $result->name == 'kegg_brite_data' ||
  448. preg_match("/KEGG BRITE term:/", $result->definition)) {
  449. $new_node->type = 'chado_analysis_kegg';
  450. } else {
  451. $new_node->type = 'chado_analysis';
  452. }
  453. // If it doesn't exist, this analysis is generic
  454. } else {
  455. $new_node->type = 'chado_analysis';
  456. }
  457. $new_node->uid = $user->uid;
  458. $new_node->analysis_id = $analysis->analysis_id;
  459. $new_node->analysisname = $analysis->analysisname;
  460. $new_node->description = $analysis->description;
  461. $new_node->program = $analysis->program;
  462. $new_node->programversion = $analysis->programversion;
  463. $new_node->algorithm = $analysis->algorithm;
  464. $new_node->sourcename = $analysis->sourcename;
  465. $new_node->sourceversion = $analysis->sourceversion;
  466. $new_node->sourceuri = $analysis->sourceuri;
  467. $new_node->timeexecuted = $analysis->timeexecuted;
  468. // If the analysis has a name, use it as the node title. If not,
  469. // construct the title using program, programversion, and sourcename
  470. if ($new_node->analysisname) {
  471. $new_node->title = $new_node->analysisname;
  472. } else {
  473. //Construct node title as "program (version)"
  474. $new_node->title = "$analysis->program ($analysis->programversion)";
  475. }
  476. node_validate($new_node);
  477. $errors = form_get_errors();
  478. if($errors){
  479. print_r($errors);
  480. }
  481. else{
  482. ##if(!form_get_errors()){
  483. $node = node_submit($new_node);
  484. node_save($node);
  485. if($node->nid){
  486. $page_content .= "Added $new_node->title<br>";
  487. }
  488. }
  489. } else {
  490. $page_content .= "Skipped $new_node->title<br>";
  491. }
  492. }
  493. return $page_content;
  494. }
  495. /*******************************************************************************
  496. * Display help and module information
  497. * @param path which path of the site we're displaying help
  498. * @param arg array that holds the current path as would be returned from arg()
  499. * function
  500. * @return help text for the path
  501. */
  502. function tripal_analysis_help($path, $arg) {
  503. $output = '';
  504. switch ($path) {
  505. case "admin/help#tripal_analysis":
  506. $output = '<p>'.
  507. t("Displays links to nodes created on this date").
  508. '</p>';
  509. break;
  510. }
  511. return $output;
  512. }
  513. /*******************************************************************************
  514. * The following function proves access control for users trying to
  515. * perform actions on data managed by this module
  516. */
  517. function chado_analysis_access($op, $node, $account){
  518. if ($op == 'create') {
  519. return user_access('create chado_analysis content', $account);
  520. }
  521. if ($op == 'update') {
  522. if (user_access('edit chado_analysis content', $account)) {
  523. return TRUE;
  524. }
  525. }
  526. if ($op == 'delete') {
  527. if (user_access('delete chado_analysis content', $account)) {
  528. return TRUE;
  529. }
  530. }
  531. if ($op == 'view') {
  532. if (user_access('access chado_analysis content', $account)) {
  533. return TRUE;
  534. }
  535. }
  536. return FALSE;
  537. }
  538. /*******************************************************************************
  539. * Set the permission types that the chado module uses. Essentially we
  540. * want permissionis that protect creation, editing and deleting of chado
  541. # data objects
  542. */
  543. function tripal_analysis_perm(){
  544. return array(
  545. 'access chado_analysis content',
  546. 'create chado_analysis content',
  547. 'delete chado_analysis content',
  548. 'edit chado_analysis content',
  549. );
  550. }
  551. /*******************************************************************************
  552. * We need to let drupal know about our theme functions and their arguments.
  553. * We create theme functions to allow users of the module to customize the
  554. * look and feel of the output generated in this module
  555. */
  556. function tripal_analysis_theme () {
  557. return array(
  558. 'tripal_analysis_analysis_page' => array (
  559. 'arguments' => array('analyses'),
  560. ),
  561. );
  562. }
  563. /*******************************************************************************
  564. * This function uses analysis_id's of all drupal analysis nodes as input and
  565. * pull the analysis information (name, description, program, programversion,
  566. * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
  567. * chado database. The return type is an object array that stores $analysis
  568. * objects sorted by program
  569. */
  570. function get_chado_analyses() {
  571. $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
  572. $no_orgs = db_result(db_query($sql_drupal));
  573. if ($no_orgs != 0) {
  574. $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
  575. $result = db_query($sql);
  576. $previous_db = db_set_active('chado');
  577. $sql = "SELECT Analysis_id, name AS analysisname, description, program,
  578. programversion, algorithm, sourcename, sourceversion,
  579. sourceuri, timeexecuted
  580. FROM {Analysis} WHERE analysis_id=%d";
  581. $analyses = array();
  582. $count = 0;
  583. while ($data = db_fetch_object($result)) {
  584. $analysis = db_fetch_object(db_query($sql, $data->analysis_id));
  585. $analysis->node_id = $data->nid;
  586. $analysis->node_type = $data->type;
  587. // Use node_type as the key so we can sort by node type
  588. // Since node_type is not unique by itself, we need to add
  589. // $count to the key
  590. $sortedBy = $analysis->timeexecuted;
  591. $analyses ["$sortedBy$count"] = $analysis;
  592. $count ++;
  593. }
  594. db_set_active($previous_db);
  595. //Sort analyses by time, descending order
  596. krsort($analyses, SORT_STRING);
  597. return $analyses;
  598. }
  599. }
  600. /************************************************************************
  601. *
  602. */
  603. function theme_tripal_analysis_analysis_page($analyses) {
  604. $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>";
  605. foreach($analyses as $analysis){
  606. // Prepare information for html output
  607. $ana_node_url = url("node/$analysis->node_id");
  608. if ($analysis->sourceversion) {
  609. $ver = "($analysis->sourceversion)";
  610. }
  611. $date = preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted);
  612. // Generate html output
  613. $output .= "<div class=\"tripal_chado_analysis-info-box\" style=\"padding:5px\">
  614. <div class=\"tripal_expandableBox\">
  615. <h3>$analysis->analysisname ($date)</h3>
  616. </div>
  617. <div class=\"tripal_expandableBoxContent\">
  618. <span>
  619. <table class=\"tripal_chado_analysis_content\">
  620. <tr><td>
  621. Name: <a href=\"$ana_node_url\">$analysis->analysisname</a>
  622. </td></tr>
  623. <tr><td>
  624. Program: $analysis->program ($analysis->programversion)
  625. </td></tr>
  626. <tr><td>
  627. Algorithm: $analysis->algorithm
  628. </td></tr>
  629. <tr><td>
  630. Source: $analysis->sourcename $ver
  631. </td></tr>
  632. <tr><td>
  633. Source URI: $analysis->sourceuri
  634. </td></tr>
  635. <tr><td>
  636. Executed Time:$date
  637. </td></tr>
  638. <tr><td>
  639. Description: $analysis->description
  640. </td></tr>
  641. </table>
  642. </span>
  643. </div>
  644. </div>";
  645. }
  646. return $output;
  647. }
  648. /************************************************************************
  649. *
  650. */
  651. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  652. // select each node from node table with chado_analysis as type
  653. // check to make sure it also exists in chado_analysis table, delete if it doesn't
  654. // (this should never, ever happen, but we'll double check anyway)
  655. $sql_drupal_node = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' order by nid";
  656. $sql_drupal_ca = "SELECT * from {chado_analysis} WHERE nid = %d";
  657. $results = db_query($sql_drupal_node);
  658. while($node = db_fetch_object($results)){
  659. $ca_record = db_fetch_object(db_query($sql_drupal_ca, $node->nid));
  660. if(!$ca_record){
  661. node_delete($node->nid);
  662. $message = "Missing in chado_analysis table.... DELETING node: $nid->nid\n";
  663. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  664. }
  665. }
  666. // get nodes from chado_analysis table and load into array, saving chado analysis_id
  667. // as we iterate through, we'll check that they are actual nodes and
  668. // delete if they aren't
  669. // (this should never, ever happen, but we'll double check anyway)
  670. $sql_drupal_ca2 = "SELECT * FROM {chado_analysis}";
  671. $sql_drupal_node2 = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' AND nid = %d";
  672. $results = db_query($sql_drupal_ca2);
  673. $nid2aid = array();
  674. while($ca_record = db_fetch_object($results)){
  675. $node = db_fetch_object(db_query($sql_drupal_node2, $ca_record->nid));
  676. if(!$node){
  677. db_query("DELETE FROM {chado_analysis} WHERE nid = $ca_record->nid");
  678. $message = "chado_analysis missing node.... DELETING chado_analysis record with nid: $ca_record->nid\n";
  679. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  680. }
  681. else{
  682. $nid2aid[$ca_record->nid] = $ca_record->analysis_id;
  683. }
  684. }
  685. // iterate through all of the chado_analysis nodes in drupal
  686. // and delete those that aren't valid in chado
  687. $sql_chado = "SELECT analysis_id from {analysis} WHERE analysis_id = %d";
  688. foreach($nid2aid as $nid => $aid){
  689. $previous_db = db_set_active('chado');
  690. $chado_record = db_fetch_object(db_query($sql_chado,$aid));
  691. db_set_active($previous_db);
  692. if(!$chado_record){
  693. node_delete($nid);
  694. $message = "Missing in analysis table in chado.... DELETING node: $nid\n";
  695. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  696. }
  697. }
  698. return '';
  699. }
  700. /*******************************************************************************
  701. *
  702. */
  703. function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
  704. $i = 0;
  705. // if the caller provided a analysis_id then get all of the features
  706. // associated with the analysis. Otherwise get all sequences assoicated
  707. // with all libraries.
  708. if(!$analysis_id){
  709. $sql = "SELECT Analysis_id, Feature_id ".
  710. "FROM {Analysisfeature} ".
  711. "ORDER BY analysis_id";
  712. $previous_db = db_set_active('chado'); // use chado database
  713. $results = db_query($sql);
  714. db_set_active($previous_db); // now use drupal database
  715. } else {
  716. $sql = "SELECT Analysis_id, Feature_id ".
  717. "FROM {Analysisfeature} ".
  718. "WHERE analysis_id = %d";
  719. "ORDER BY analysis_id";
  720. $previous_db = db_set_active('chado'); // use chado database
  721. $results = db_query($sql,$analysis_id);
  722. db_set_active($previous_db); // now use drupal database
  723. }
  724. // load into ids array
  725. $count = 0;
  726. $ids = array();
  727. while($id = db_fetch_object($results)){
  728. $ids[$count] = $id->feature_id;
  729. $count++;
  730. }
  731. $interval = intval($count * 0.01);
  732. foreach($ids as $feature_id){
  733. // update the job status every 1% features
  734. if($job_id and $i % interval == 0){
  735. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  736. }
  737. tripal_feature_sync_feature ($feature_id);
  738. $i++;
  739. }
  740. }
  741. /*******************************************************************************
  742. *
  743. */
  744. function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
  745. $i = 0;
  746. // if the caller provided a analysis_id then get all of the features
  747. // associated with the analysis. Otherwise get all sequences assoicated
  748. // with all libraries.
  749. if(!$analysis_id){
  750. $sql = "SELECT Analysis_id, Feature_id ".
  751. "FROM {Analysisfeature} ".
  752. "ORDER BY analysis_id";
  753. $previous_db = db_set_active('chado'); // use chado database
  754. $results = db_query($sql);
  755. db_set_active($previous_db); // now use drupal database
  756. } else {
  757. $sql = "SELECT Analysis_id, Feature_id ".
  758. "FROM {Analysisfeature} ".
  759. "WHERE analysis_id = %d";
  760. "ORDER BY analysis_id";
  761. $previous_db = db_set_active('chado'); // use chado database
  762. $results = db_query($sql,$analysis_id);
  763. db_set_active($previous_db); // now use drupal database
  764. }
  765. // load into ids array
  766. $count = 0;
  767. $ids = array();
  768. while($id = db_fetch_object($results)){
  769. $ids[$count] = $id->feature_id;
  770. $count++;
  771. }
  772. // make sure our vocabularies are set before proceeding
  773. tripal_feature_set_vocabulary();
  774. // use this SQL for getting the nodes
  775. $nsql = "SELECT * FROM {chado_feature} CF ".
  776. " INNER JOIN {node} N ON N.nid = CF.nid ".
  777. "WHERE feature_id = %d";
  778. // iterate through the features and set the taxonomy
  779. $interval = intval($count * 0.01);
  780. foreach($ids as $feature_id){
  781. // update the job status every 1% features
  782. if($job_id and $i % $interval == 0){
  783. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  784. }
  785. $node = db_fetch_object(db_query($nsql,$feature_id));
  786. tripal_feature_set_taxonomy($node,$feature_id);
  787. $i++;
  788. }
  789. }