tripal_analysis.module 33 KB

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