tripal_analysis.module 34 KB

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