tripal_analysis.module 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. $form = array();
  286. $form['title']= array(
  287. '#type' => 'hidden',
  288. '#default_value' => $node->title,
  289. );
  290. $form['analysis_id']= array(
  291. '#type' => 'hidden',
  292. '#default_value' => $node->analysis->analysis_id,
  293. );
  294. $form['analysisname']= array(
  295. '#type' => 'textfield',
  296. '#title' => t('Analysis Name'),
  297. '#required' => FALSE,
  298. '#default_value' => $node->analysis->name,
  299. '#description' => t("This should be a handy short identifier that
  300. describes the analysis succintly as possible which helps the user find analyses."),
  301. '#weight' => 1
  302. );
  303. $form['program']= array(
  304. '#type' => 'textfield',
  305. '#title' => t('Program'),
  306. '#required' => TRUE,
  307. '#default_value' => $node->analysis->program,
  308. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
  309. '#weight' => 2
  310. );
  311. $form['programversion']= array(
  312. '#type' => 'textfield',
  313. '#title' => t('Program Version'),
  314. '#required' => TRUE,
  315. '#default_value' => $node->analysis->programversion,
  316. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]"),
  317. '#weight' => 3
  318. );
  319. $form['algorithm']= array(
  320. '#type' => 'textfield',
  321. '#title' => t('Algorithm'),
  322. '#required' => FALSE,
  323. '#default_value' => $node->analysis->algorithm,
  324. '#description' => t("Algorithm name, e.g. blast."),
  325. '#weight' => 4
  326. );
  327. $form['sourcename']= array(
  328. '#type' => 'textfield',
  329. '#title' => t('Source Name'),
  330. '#required' => TRUE,
  331. '#default_value' => $node->analysis->sourcename,
  332. '#description' => t('The name of the source data. This could be a file name, data set name or a
  333. small description for how the data was collected. For long descriptions use the description field below'),
  334. '#weight' => 5
  335. );
  336. $form['sourceversion']= array(
  337. '#type' => 'textfield',
  338. '#title' => t('Source Version'),
  339. '#required' => FALSE,
  340. '#default_value' => $node->analysis->sourceversion,
  341. '#description' => t('If the source dataset has a version, include it here'),
  342. '#weight' => 6
  343. );
  344. $form['sourceuri']= array(
  345. '#type' => 'textfield',
  346. '#title' => t('Source URI'),
  347. '#required' => FALSE,
  348. '#default_value' => $node->analysis->sourceuri,
  349. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  350. Someone could recreate the analysis directly by going to this URI and
  351. fetching the source data (e.g. the blast database, or the training model)."),
  352. '#weight' => 7
  353. );
  354. // Get time saved in chado
  355. $default_time = $node->analysis->timeexecuted;
  356. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  357. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  358. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  359. // If the time is not set, use current time
  360. if (!$default_time) {
  361. $default_time = time();
  362. $year = format_date($default_time, 'custom', 'Y');
  363. $month = format_date($default_time, 'custom', 'n');
  364. $day = format_date($default_time, 'custom', 'j');
  365. }
  366. $form['timeexecuted']= array(
  367. '#type' => 'date',
  368. '#title' => t('Time Executed'),
  369. '#required' => TRUE,
  370. '#default_value' => array(
  371. 'year' => $year,
  372. 'month' => $month,
  373. 'day' => $day,
  374. ),
  375. '#weight' => 8
  376. );
  377. $form['description']= array(
  378. '#type' => 'textarea',
  379. '#rows' => 15,
  380. '#title' => t('Description and/or Program Settings'),
  381. '#required' => FALSE,
  382. '#default_value' => $node->analysis->description,
  383. '#description' => t('Please provide all necessary information to allow
  384. someone to recreate the analysis, including materials and methods
  385. for collection of the source data and performing the analysis'),
  386. '#weight' => 9
  387. );
  388. return $form;
  389. }
  390. /*******************************************************************************
  391. * When a node is requested by the user this function is called to allow us
  392. * to add auxiliary data to the node object.
  393. *
  394. * @ingroup tripal_analysis
  395. */
  396. function chado_analysis_load($node){
  397. $additions = new stdClass();
  398. // get the analysis_id for this node:
  399. $sql = "SELECT analysis_id FROM {chado_analysis} WHERE nid = %d";
  400. $ana_node = db_fetch_object(db_query($sql, $node->nid));
  401. if ($ana_node) {
  402. // get analysis information
  403. $sql = "SELECT Analysis_id, name, description, program, ".
  404. " programversion, algorithm, sourcename, sourceversion, ".
  405. " sourceuri, timeexecuted ".
  406. "FROM {Analysis} ".
  407. "WHERE Analysis_id = $ana_node->analysis_id";
  408. $previous_db = tripal_db_set_active('chado'); // use chado database
  409. $additions->analysis = db_fetch_object(db_query($sql));
  410. // get number of features assc with this analysis
  411. // $sql = "SELECT count(feature_id) as featurecount ".
  412. // "FROM {Analysisfeature} ".
  413. // "WHERE Analysis_id = %d";
  414. // $additions->featurecount = db_result(db_query($sql, $ana_node->analysis_id));
  415. tripal_db_set_active($previous_db); // now use drupal database
  416. }
  417. // If the analysis has a name, use it as the node title. If not, construct
  418. // the title using program programversion, and sourcename
  419. if ($additions->analysis->name) {
  420. $additions->title = $additions->analysis->name;
  421. } else {
  422. // Construct node title as "program version (source)
  423. $additions->title = "$additions->analysis->program ($additions->analysis->programversion)";
  424. }
  425. return $additions;
  426. }
  427. /*******************************************************************************
  428. * This function customizes the view of the chado_analysis node. It allows
  429. * us to generate the markup.
  430. *
  431. * @ingroup tripal_analysis
  432. */
  433. function chado_analysis_view ($node, $teaser = FALSE, $page = FALSE) {
  434. // use drupal's default node view:
  435. if (!$teaser) {
  436. $node = node_prepare($node, $teaser);
  437. // When previewing a node submitting form, it shows 'Array' instead of
  438. // correct date format. We need to format the date here
  439. $time = $node->timeexecuted;
  440. if(is_array($time)){
  441. $month = $time['month'];
  442. $day = $time['day'];
  443. $year = $time['year'];
  444. $timestamp = $year.'-'.$month.'-'.$day;
  445. $node->timeexecuted = $timestamp;
  446. }
  447. }
  448. return $node;
  449. }
  450. /*******************************************************************************
  451. * Synchronize analyses from chado to drupal
  452. *
  453. * @ingroup tripal_analysis
  454. */
  455. function tripal_analysis_sync_analyses ($analysis_id = NULL, $job_id = NULL){
  456. global $user;
  457. $page_content = '';
  458. if(!$analysis_id){
  459. $sql = "SELECT Analysis_id, name, description, program, ".
  460. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  461. " timeexecuted ".
  462. "FROM {Analysis} ";
  463. $previous_db = tripal_db_set_active('chado'); // use chado database
  464. $results = db_query($sql);
  465. tripal_db_set_active($previous_db); // now use drupal database
  466. } else {
  467. $sql = "SELECT Analysis_id, name, description, program, ".
  468. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  469. " timeexecuted ".
  470. "FROM {Analysis} ".
  471. "WHERE analysis_id = %d";
  472. $previous_db = tripal_db_set_active('chado'); // use chado database
  473. $results = db_query($sql,$analysis_id);
  474. tripal_db_set_active($previous_db); // now use drupal database
  475. }
  476. // We'll use the following SQL statement for checking if the analysis
  477. // already exists as a drupal node.
  478. $sql = "SELECT * FROM {chado_analysis} ".
  479. "WHERE analysis_id = %d";
  480. while($analysis = db_fetch_object($results)){
  481. print "syncing analysis ";
  482. print $analysis->name;
  483. print ", ";
  484. print $analysis->analysis_id;
  485. print "\n";
  486. // check if this analysis already exists in the drupal database. if it
  487. // does then skip this analysis and go to the next one.
  488. if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
  489. $new_node = new stdClass();
  490. // try to access analysis type for this analysis
  491. $sql = "SELECT * FROM {analysisprop}
  492. WHERE analysis_id = %d
  493. AND type_id =
  494. (SELECT cvterm_id from {cvterm} where name = '%s')
  495. ";
  496. $previous_db = tripal_db_set_active('chado');
  497. $analysis_type = db_fetch_object(db_query($sql, $analysis->analysis_id, "analysis_type"));
  498. tripal_db_set_active($previous_db);
  499. // Get the type of analysis using cvterm_id
  500. // Current possibilities: kegg, unigene, interpro, blast
  501. if ($analysis_type) {
  502. // This is a unigene analysis
  503. if ($analysis_type->value == 'tripal_analysis_unigene') {
  504. $new_node->type = 'chado_analysis_unigene';
  505. // This is a blast analysis
  506. } else if ($analysis_type->value == 'tripal_analysis_blast') {
  507. $new_node->type = 'chado_analysis_blast';
  508. // This is a interpro analysis
  509. } else if ($analysis_type->value == 'tripal_analysis_interpro') {
  510. $new_node->type = 'chado_analysis_interpro';
  511. // This is a kegg analysis
  512. } else if ($analysis_type->value == 'tripal_analysis_kegg' ){
  513. $new_node->type = 'chado_analysis_kegg';
  514. } else {
  515. $new_node->type = 'chado_analysis';
  516. }
  517. // If it doesn't exist, this analysis is generic
  518. } else {
  519. $new_node->type = 'chado_analysis';
  520. }
  521. print "analysis type is $new_node->type\n";
  522. $new_node->uid = $user->uid;
  523. $new_node->analysis_id = $analysis->analysis_id;
  524. $new_node->analysisname = $analysis->name;
  525. $new_node->description = $analysis->description;
  526. $new_node->program = $analysis->program;
  527. $new_node->programversion = $analysis->programversion;
  528. $new_node->algorithm = $analysis->algorithm;
  529. $new_node->sourcename = $analysis->sourcename;
  530. $new_node->sourceversion = $analysis->sourceversion;
  531. $new_node->sourceuri = $analysis->sourceuri;
  532. $new_node->timeexecuted = $analysis->timeexecuted;
  533. // If the analysis has a name, use it as the node title. If not,
  534. // construct the title using program, programversion, and sourcename
  535. if ($new_node->analysisname) {
  536. $new_node->title = $new_node->analysisname;
  537. } else {
  538. //Construct node title as "program (version)"
  539. $new_node->title = "$analysis->program ($analysis->programversion)";
  540. }
  541. node_validate($new_node);
  542. $errors = form_get_errors();
  543. if($errors){
  544. print_r($errors);
  545. }
  546. else{
  547. ##if(!form_get_errors()){
  548. $node = node_submit($new_node);
  549. node_save($node);
  550. if($node->nid){
  551. $page_content .= "Added $new_node->title<br>";
  552. }
  553. }
  554. } else {
  555. $page_content .= "Skipped $new_node->title<br>";
  556. }
  557. }
  558. return $page_content;
  559. }
  560. /**
  561. *
  562. */
  563. function tripal_analysis_validate($node, &$form){
  564. ##dprint_r($node);
  565. // This validation is being used for three activities:
  566. // CASE A: Update a node that exists in both drupal and chado
  567. // CASE B: Synchronizing a node from chado to drupal
  568. // CASE C: Inserting a new node that exists in niether drupal nor chado
  569. // Only nodes being updated will have an nid already
  570. if($node->nid){
  571. //---------------------------------------------------
  572. // CASE A: We are validating a form for updating an existing node
  573. //---------------------------------------------------
  574. // TO DO: check that the new fields don't yield a non-unique primary key in chado
  575. }
  576. else{
  577. // To differentiate if we are syncing or creating a new analysis altogther, see if an
  578. // analysis_id already exists
  579. if($node->analysis_id){
  580. //---------------------------------------------------
  581. // CASE B: Synchronizing a node from chado to drupal
  582. //---------------------------------------------------
  583. }
  584. else{
  585. //---------------------------------------------------
  586. // CASE C: We are validating a form for inserting a new node
  587. //---------------------------------------------------
  588. // The primary key for the chado analysis table is
  589. // program, programversion, sourcename
  590. // Check to see if this analysis really is new -ie, it doesn't have the same
  591. // primary key as any other analysis
  592. $sql = "SELECT analysis_id ".
  593. "FROM {analysis} ".
  594. "WHERE program='%s'".
  595. "AND programversion='%s'".
  596. "AND sourcename='%s'";
  597. $previous_db = tripal_db_set_active('chado');
  598. $analysis_id = db_result(db_query($sql, $node->program, $node->programversion, $node->sourcename));
  599. tripal_db_set_active($previous_db);
  600. if($analysis_id){
  601. //---------------------------------------------------
  602. // this primary key already exists in chado analysis table!
  603. //---------------------------------------------------
  604. // check to see if it has also been synced with drupal
  605. $sql = "SELECT nid FROM {chado_analysis} ".
  606. "WHERE analysis_id = %d";
  607. $node_id = db_result(db_query($sql, $analysis_id));
  608. if($node_id){
  609. //---------------------------------------------------
  610. // the analysis has already been synced with drupal, redirect the user
  611. // to modify that node or start over
  612. //---------------------------------------------------
  613. $error = 'This analysis already exists in the chado database (analysis id ';
  614. $error .= $analysis_id.') and has been synchronized ';
  615. $error .= 'with drupal. See node '.$node_id.' if you wish to update that analysis. ';
  616. $error .= ' For a new analysis, please select a unique primary key ';
  617. $error .= '(primary key consists of sourcename, program and programversion).';
  618. form_set_error('sourcename', t($error));
  619. }
  620. else{
  621. //---------------------------------------------------
  622. // the analysis does not exist in drupal - tell the user
  623. // to sync from chado or create a new unique primary key
  624. //---------------------------------------------------
  625. $error = 'This analysis already exists in the chado database (analysis id ';
  626. $error .= $analysis_id.') but has not been synchronized ';
  627. $error .= 'with drupal. See the tripal admin pages to synchronize. ';
  628. $error .= ' For a new analysis, please select a unique primary key ';
  629. $error .= '(primary key consists of sourcename, program and programversion).';
  630. form_set_error('sourcename', t($error));
  631. }
  632. }
  633. }
  634. }
  635. }
  636. /*******************************************************************************
  637. * Display help and module information
  638. * @param path which path of the site we're displaying help
  639. * @param arg array that holds the current path as would be returned from arg()
  640. * function
  641. * @return help text for the path
  642. *
  643. * @ingroup tripal_analysis
  644. */
  645. function tripal_analysis_help($path, $arg) {
  646. $output = '';
  647. switch ($path) {
  648. case "admin/help#tripal_analysis":
  649. $output = '<p>'.
  650. t("Displays links to nodes created on this date").
  651. '</p>';
  652. break;
  653. }
  654. return $output;
  655. }
  656. /*******************************************************************************
  657. * The following function proves access control for users trying to
  658. * perform actions on data managed by this module
  659. *
  660. * @ingroup tripal_analysis
  661. */
  662. function chado_analysis_access($op, $node, $account){
  663. if ($op == 'create') {
  664. return user_access('create chado_analysis content', $account);
  665. }
  666. if ($op == 'update') {
  667. if (user_access('edit chado_analysis content', $account)) {
  668. return TRUE;
  669. }
  670. }
  671. if ($op == 'delete') {
  672. if (user_access('delete chado_analysis content', $account)) {
  673. return TRUE;
  674. }
  675. }
  676. if ($op == 'view') {
  677. if (user_access('access chado_analysis content', $account)) {
  678. return TRUE;
  679. }
  680. }
  681. return FALSE;
  682. }
  683. /*******************************************************************************
  684. * Set the permission types that the chado module uses. Essentially we
  685. * want permissionis that protect creation, editing and deleting of chado
  686. # data objects
  687. *
  688. * @ingroup tripal_analysis
  689. */
  690. function tripal_analysis_perm(){
  691. return array(
  692. 'access chado_analysis content',
  693. 'create chado_analysis content',
  694. 'delete chado_analysis content',
  695. 'edit chado_analysis content',
  696. );
  697. }
  698. /*******************************************************************************
  699. * We need to let drupal know about our theme functions and their arguments.
  700. * We create theme functions to allow users of the module to customize the
  701. * look and feel of the output generated in this module
  702. *
  703. * @ingroup tripal_analysis
  704. */
  705. function tripal_analysis_theme () {
  706. return array(
  707. 'tripal_analysis_analysis_page' => array (
  708. 'arguments' => array('analyses'),
  709. ),
  710. );
  711. }
  712. /*******************************************************************************
  713. * This function uses analysis_id's of all drupal analysis nodes as input and
  714. * pull the analysis information (name, description, program, programversion,
  715. * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
  716. * chado database. The return type is an object array that stores $analysis
  717. * objects sorted by program
  718. *
  719. * @ingroup tripal_analysis
  720. */
  721. function get_chado_analyses() {
  722. $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
  723. $no_orgs = db_result(db_query($sql_drupal));
  724. if ($no_orgs != 0) {
  725. $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
  726. $result = db_query($sql);
  727. $previous_db = tripal_db_set_active('chado');
  728. $sql = "SELECT Analysis_id, name, description, program,
  729. programversion, algorithm, sourcename, sourceversion,
  730. sourceuri, timeexecuted
  731. FROM {Analysis} WHERE analysis_id=%d";
  732. $analyses = array();
  733. $count = 0;
  734. while ($data = db_fetch_object($result)) {
  735. $analysis = db_fetch_object(db_query($sql, $data->analysis_id));
  736. $analysis->node_id = $data->nid;
  737. $analysis->node_type = $data->type;
  738. // Use node_type as the key so we can sort by node type
  739. // Since node_type is not unique by itself, we need to add
  740. // $count to the key
  741. $sortedBy = $analysis->timeexecuted;
  742. $analyses ["$sortedBy$count"] = $analysis;
  743. $count ++;
  744. }
  745. tripal_db_set_active($previous_db);
  746. //Sort analyses by time, descending order
  747. krsort($analyses, SORT_STRING);
  748. return $analyses;
  749. }
  750. }
  751. /************************************************************************
  752. *
  753. *
  754. * @ingroup tripal_analysis
  755. */
  756. function theme_tripal_analysis_analysis_page($analyses) {
  757. $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>";
  758. foreach($analyses as $analysis){
  759. // Prepare information for html output
  760. $ana_node_url = url("node/$analysis->node_id");
  761. if ($analysis->sourceversion) {
  762. $ver = "($analysis->sourceversion)";
  763. }
  764. $date = preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted);
  765. // Generate html output
  766. $output .= "<div class=\"tripal_chado_analysis-info-box\" style=\"padding:5px\">
  767. <div class=\"tripal_expandableBox\">
  768. <h3>$analysis->name ($date)</h3>
  769. </div>
  770. <div class=\"tripal_expandableBoxContent\">
  771. <span>
  772. <table class=\"tripal_chado_analysis_content\">
  773. <tr><td>
  774. Name: <a href=\"$ana_node_url\">$analysis->name</a>
  775. </td></tr>
  776. <tr><td>
  777. Program: $analysis->program ($analysis->programversion)
  778. </td></tr>
  779. <tr><td>
  780. Algorithm: $analysis->algorithm
  781. </td></tr>
  782. <tr><td>
  783. Source: $analysis->sourcename $ver
  784. </td></tr>
  785. <tr><td>
  786. Source URI: $analysis->sourceuri
  787. </td></tr>
  788. <tr><td>
  789. Executed Time:$date
  790. </td></tr>
  791. <tr><td>
  792. Description: $analysis->description
  793. </td></tr>
  794. </table>
  795. </span>
  796. </div>
  797. </div>";
  798. }
  799. return $output;
  800. }
  801. /************************************************************************
  802. *
  803. *
  804. * @ingroup tripal_analysis
  805. */
  806. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  807. // select each node from node table with chado_analysis as type
  808. // check to make sure it also exists in chado_analysis table, delete if it doesn't
  809. // (this should never, ever happen, but we'll double check anyway)
  810. $sql_drupal_node = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' order by nid";
  811. $sql_drupal_ca = "SELECT * from {chado_analysis} WHERE nid = %d";
  812. $results = db_query($sql_drupal_node);
  813. while($node = db_fetch_object($results)){
  814. $ca_record = db_fetch_object(db_query($sql_drupal_ca, $node->nid));
  815. if(!$ca_record){
  816. node_delete($node->nid);
  817. $message = "Missing in chado_analysis table.... DELETING node: $nid->nid\n";
  818. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  819. }
  820. }
  821. // get nodes from chado_analysis table and load into array, saving chado analysis_id
  822. // as we iterate through, we'll check that they are actual nodes and
  823. // delete if they aren't
  824. // (this should never, ever happen, but we'll double check anyway)
  825. $sql_drupal_ca2 = "SELECT * FROM {chado_analysis}";
  826. $sql_drupal_node2 = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' AND nid = %d";
  827. $results = db_query($sql_drupal_ca2);
  828. $nid2aid = array();
  829. while($ca_record = db_fetch_object($results)){
  830. $node = db_fetch_object(db_query($sql_drupal_node2, $ca_record->nid));
  831. if(!$node){
  832. db_query("DELETE FROM {chado_analysis} WHERE nid = $ca_record->nid");
  833. $message = "chado_analysis missing node.... DELETING chado_analysis record with nid: $ca_record->nid\n";
  834. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  835. }
  836. else{
  837. $nid2aid[$ca_record->nid] = $ca_record->analysis_id;
  838. }
  839. }
  840. // iterate through all of the chado_analysis nodes in drupal
  841. // and delete those that aren't valid in chado
  842. $sql_chado = "SELECT analysis_id from {analysis} WHERE analysis_id = %d";
  843. foreach($nid2aid as $nid => $aid){
  844. $previous_db = tripal_db_set_active('chado');
  845. $chado_record = db_fetch_object(db_query($sql_chado,$aid));
  846. tripal_db_set_active($previous_db);
  847. if(!$chado_record){
  848. node_delete($nid);
  849. $message = "Missing in analysis table in chado.... DELETING node: $nid\n";
  850. watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
  851. }
  852. }
  853. return '';
  854. }
  855. /*******************************************************************************
  856. *
  857. *
  858. * @ingroup tripal_analysis
  859. */
  860. /*
  861. function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
  862. $i = 0;
  863. // if the caller provided a analysis_id then get all of the features
  864. // associated with the analysis. Otherwise get all sequences associated
  865. // with all libraries.
  866. if(!$analysis_id){
  867. $sql = "SELECT Analysis_id, Feature_id ".
  868. "FROM {Analysisfeature} ".
  869. "ORDER BY analysis_id";
  870. $previous_db = tripal_db_set_active('chado'); // use chado database
  871. $results = db_query($sql);
  872. tripal_db_set_active($previous_db); // now use drupal database
  873. } else {
  874. $sql = "SELECT Analysis_id, Feature_id ".
  875. "FROM {Analysisfeature} ".
  876. "WHERE analysis_id = %d";
  877. "ORDER BY analysis_id";
  878. $previous_db = tripal_db_set_active('chado'); // use chado database
  879. $results = db_query($sql,$analysis_id);
  880. tripal_db_set_active($previous_db); // now use drupal database
  881. }
  882. // load into ids array
  883. $count = 0;
  884. $ids = array();
  885. while($id = db_fetch_object($results)){
  886. $ids[$count] = $id->feature_id;
  887. $count++;
  888. }
  889. $interval = intval($count * 0.01);
  890. foreach($ids as $feature_id){
  891. // update the job status every 1% features
  892. if($job_id and $i % interval == 0){
  893. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  894. }
  895. tripal_feature_sync_feature ($feature_id);
  896. $i++;
  897. }
  898. } */
  899. /*******************************************************************************
  900. *
  901. *
  902. * @ingroup tripal_analysis
  903. */
  904. /*
  905. function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
  906. $i = 0;
  907. // if the caller provided a analysis_id then get all of the features
  908. // associated with the analysis. Otherwise get all sequences assoicated
  909. // with all libraries.
  910. if(!$analysis_id){
  911. $sql = "SELECT Analysis_id, Feature_id ".
  912. "FROM {Analysisfeature} ".
  913. "ORDER BY analysis_id";
  914. $previous_db = tripal_db_set_active('chado'); // use chado database
  915. $results = db_query($sql);
  916. tripal_db_set_active($previous_db); // now use drupal database
  917. } else {
  918. $sql = "SELECT Analysis_id, Feature_id ".
  919. "FROM {Analysisfeature} ".
  920. "WHERE analysis_id = %d";
  921. "ORDER BY analysis_id";
  922. $previous_db = tripal_db_set_active('chado'); // use chado database
  923. $results = db_query($sql,$analysis_id);
  924. tripal_db_set_active($previous_db); // now use drupal database
  925. }
  926. // load into ids array
  927. $count = 0;
  928. $ids = array();
  929. while($id = db_fetch_object($results)){
  930. $ids[$count] = $id->feature_id;
  931. $count++;
  932. }
  933. // make sure our vocabularies are set before proceeding
  934. tripal_feature_set_vocabulary();
  935. // use this SQL for getting the nodes
  936. $nsql = "SELECT * FROM {chado_feature} CF ".
  937. " INNER JOIN {node} N ON N.nid = CF.nid ".
  938. "WHERE feature_id = %d";
  939. // iterate through the features and set the taxonomy
  940. $interval = intval($count * 0.01);
  941. foreach($ids as $feature_id){
  942. // update the job status every 1% features
  943. if($job_id and $i % $interval == 0){
  944. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  945. }
  946. $node = db_fetch_object(db_query($nsql,$feature_id));
  947. tripal_feature_set_taxonomy($node,$feature_id);
  948. $i++;
  949. }
  950. }
  951. */
  952. /*************************************************************************
  953. * Implements hook_views_api()
  954. * Purpose: Essentially this hook tells drupal that there is views support for
  955. * for this module which then includes tripal_analysis.views.inc where all the
  956. * views integration code is
  957. *
  958. * @ingroup tripal_analysis
  959. */
  960. function tripal_analysis_views_api() {
  961. return array(
  962. 'api' => 2.0,
  963. );
  964. }