tripal_analysis.module 32 KB

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