tripal_analysis.module 35 KB

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