tripal_analysis.module 31 KB

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