tripal_analysis.module 35 KB

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