tripal_analysis.module 31 KB

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