tripal_analysis.module 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. <?php
  2. // $Id:
  3. /**
  4. * @file
  5. * Contains all the main hook implementations for the tripal_analysis module
  6. */
  7. /**
  8. * @defgroup tripal_analysis Analysis Module
  9. * @{
  10. * Provides functions for managing chado analysis' including creating details pages for each one
  11. * @}
  12. */
  13. require('tripal_analysis.api.inc');
  14. /*************************************************************************
  15. *
  16. *
  17. * @ingroup tripal_analysis
  18. */
  19. function tripal_analysis_register_child($modulename){
  20. $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES ('%s')";
  21. db_query($sql, $modulename);
  22. }
  23. /*************************************************************************
  24. *
  25. *
  26. * @ingroup tripal_analysis
  27. */
  28. function tripal_analysis_unregister_child($modulename){
  29. if (db_table_exists('tripal_analysis')) {
  30. $sql = "DELETE FROM {tripal_analysis} WHERE modulename = '%s'";
  31. db_query($sql, $modulename);
  32. }
  33. }
  34. /******************************************************************************
  35. *
  36. * @ingroup tripal_analysis
  37. */
  38. function tripal_analysis_init(){
  39. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis.js');
  40. }
  41. /*******************************************************************************
  42. * tripal_analysis_menu()
  43. * HOOK: Implementation of hook_menu()
  44. * Entry points and paths of the module
  45. *
  46. * @ingroup tripal_analysis
  47. */
  48. function tripal_analysis_menu() {
  49. // Display available analyses
  50. $items['analyses'] = array(
  51. 'menu_name' => ('primary-links'), //Enable the 'Analysis' primary link
  52. 'title' => t('Analyses'),
  53. 'page callback' => 'tripal_analysis_show_analyses',
  54. 'access arguments' => array('access chado_analysis content'),
  55. 'type' => MENU_NORMAL_ITEM
  56. );
  57. //Sync analysis
  58. $items['chado_sync_analyses'] = array(
  59. 'title' => t('Sync Data'),
  60. 'page callback' => 'tripal_analysis_sync_analyses',
  61. 'access arguments' => array('administer site configuration'),
  62. 'type' => MENU_CALLBACK
  63. );
  64. // Tripal Analysis administrative settings
  65. $items['admin/tripal/tripal_analysis'] = array(
  66. 'title' => 'Analyses',
  67. 'description' => 'Basic Description of Tripal Analysis Module Functionality.',
  68. 'page callback' => 'tripal_analysis_module_description_page',
  69. 'access arguments' => array('administer site configuration'),
  70. 'type' => MENU_NORMAL_ITEM,
  71. 'file' => 'tripal_analysis.admin.inc',
  72. );
  73. $items['admin/tripal/tripal_analysis/configuration'] = array(
  74. 'title' => 'Configuration',
  75. 'description' => 'Settings for the displays of analysis results.',
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('tripal_analysis_admin'),
  78. 'access arguments' => array('administer site configuration'),
  79. 'type' => MENU_NORMAL_ITEM,
  80. 'file' => 'tripal_analysis.admin.inc',
  81. );
  82. return $items;
  83. }
  84. /*******************************************************************************
  85. * Display the summary view of analyses when click on the 'Analyses'
  86. * primary-link
  87. *
  88. * @ingroup tripal_analysis
  89. */
  90. function tripal_analysis_show_analyses (){
  91. // Show libraries stored in Drupal's {chado_analysis} table
  92. $sql = "SELECT COUNT(analysis_id) FROM {chado_analysis}";
  93. $no_ana = db_result(db_query ($sql));
  94. if($no_ana != 0) {
  95. $analyses = get_chado_analyses ();
  96. return theme('tripal_analysis_analysis_page', $analyses);
  97. } else {
  98. return t("No analysis available at this time.");
  99. }
  100. }
  101. /*******************************************************************************
  102. * Provide information to drupal about the node types that we're creating
  103. * in this module
  104. *
  105. * @ingroup tripal_analysis
  106. */
  107. function tripal_analysis_node_info() {
  108. $nodes = array();
  109. $nodes['chado_analysis'] = array(
  110. 'name' => t('Analysis'),
  111. 'module' => 'chado_analysis',
  112. 'description' => t('An analysis from the chado database'),
  113. 'has_title' => FALSE,
  114. 'title_label' => t('Analysis'),
  115. 'has_body' => FALSE,
  116. 'body_label' => t('Analysis Description'),
  117. 'locked' => TRUE
  118. );
  119. return $nodes;
  120. }
  121. /*******************************************************************************
  122. * When a new chado_analysis node is created we also need to add information
  123. * to our chado_analysis table. This function is called on insert of a new
  124. * node of type 'chado_analysis' and inserts the necessary information.
  125. *
  126. * @ingroup tripal_analysis
  127. */
  128. function chado_analysis_insert($node){
  129. global $user;
  130. // Create a timestamp so we can insert it into the chado database
  131. $time = $node->timeexecuted;
  132. $month = $time['month'];
  133. $day = $time['day'];
  134. $year = $time['year'];
  135. $timestamp = $month.'/'.$day.'/'.$year;
  136. // If this analysis already exists then don't recreate it in chado
  137. $analysis_id = $node->analysis_id;
  138. if ($analysis_id) {
  139. $sql = "SELECT analysis_id ".
  140. "FROM {Analysis} ".
  141. "WHERE analysis_id = %d ";
  142. $previous_db = tripal_db_set_active('chado');
  143. $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
  144. tripal_db_set_active($previous_db);
  145. }
  146. // If the analysis doesn't exist then let's create it in chado.
  147. if(!$analysis){
  148. // insert and then get the newly inserted analysis record
  149. $values = array(
  150. 'name' => $node->analysisname,
  151. 'description' => $node->description,
  152. 'program' => $node->program,
  153. 'programversion' => $node->programversion,
  154. 'algorithm' => $node->algorithm,
  155. 'sourcename' => $node->sourcename,
  156. 'sourceversion' => $node->sourceversion,
  157. 'sourceuri' => $node->sourceuri,
  158. 'timeexecuted' => $timestamp
  159. );
  160. if(tripal_core_chado_insert('analysis',$values)){
  161. $analysis = tripal_core_chado_select('analysis',array('*'),$values);
  162. $analysis_id = $analysis[0]->analysis_id;
  163. }
  164. }
  165. // Make sure the entry for this analysis doesn't already exist in the
  166. // chado_analysis table if it doesn't exist then we want to add it.
  167. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  168. "WHERE analysis_id = %d";
  169. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  170. if(!$node_check){
  171. // next add the item to the drupal table
  172. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  173. "VALUES (%d, %d, %d)";
  174. db_query($sql,$node->nid,$node->vid,$analysis_id);
  175. // Create a title for the analysis node using the unique keys so when the
  176. // node is saved, it will have a title
  177. $record = new stdClass();
  178. // If the analysis has a name, use it as the node title. If not, construct
  179. // the title using program, programversion, and sourcename
  180. if ($node->analysisname) {
  181. $record->title = $node->analysisname;
  182. } else {
  183. //Construct node title as "program (version)
  184. $record->title = "$node->program ($node->programversion)";
  185. }
  186. $record->nid = $node->nid;
  187. drupal_write_record('node',$record,'nid');
  188. drupal_write_record('node_revisions',$record,'nid');
  189. }
  190. // add the analysis to the node object for
  191. // use by other analysis modules that may be using this function
  192. $node->analysis = $analysis;
  193. $node->analysis_id = $analysis_id; // we need to set this for children
  194. }
  195. /*******************************************************************************
  196. *
  197. * @ingroup tripal_analysis
  198. */
  199. function chado_analysis_delete($node){
  200. // Before removing, get analysis_id so we can remove it from chado database
  201. // later
  202. $sql_drupal = "SELECT analysis_id ".
  203. "FROM {chado_analysis} ".
  204. "WHERE nid = %d ".
  205. "AND vid = %d";
  206. $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  207. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  208. $sql_del = "DELETE FROM {chado_analysis} ".
  209. "WHERE nid = %d ".
  210. "AND vid = %d";
  211. db_query($sql_del, $node->nid, $node->vid);
  212. $sql_del = "DELETE FROM {node} ".
  213. "WHERE nid = %d ".
  214. "AND vid = %d";
  215. db_query($sql_del, $node->nid, $node->vid);
  216. $sql_del = "DELETE FROM {node_revisions} ".
  217. "WHERE nid = %d ".
  218. "AND vid = %d";
  219. db_query($sql_del, $node->nid, $node->vid);
  220. //Remove from analysis and analysisprop tables of chado database as well
  221. $previous_db = tripal_db_set_active('chado');
  222. db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  223. tripal_db_set_active($previous_db);
  224. }
  225. /*******************************************************************************
  226. * Update analyses
  227. *
  228. * @ingroup tripal_analysis
  229. */
  230. function chado_analysis_update($node){
  231. global $user;
  232. if($node->revision){
  233. // TODO -- decide what to do about revisions
  234. } else {
  235. // Create a timestamp so we can insert it into the chado database
  236. $time = $node->timeexecuted;
  237. $month = $time['month'];
  238. $day = $time['day'];
  239. $year = $time['year'];
  240. $timestamp = $month.'/'.$day.'/'.$year;
  241. // get the analysis_id for this node:
  242. $sql = "SELECT analysis_id ".
  243. "FROM {chado_analysis} ".
  244. "WHERE vid = %d";
  245. $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
  246. $sql = "UPDATE {analysis} ".
  247. "SET name = '%s', ".
  248. " description = '%s', ".
  249. " program = '%s', ".
  250. " programversion = '%s', ".
  251. " algorithm = '%s', ".
  252. " sourcename = '%s', ".
  253. " sourceversion = '%s', ".
  254. " sourceuri = '%s', ".
  255. " timeexecuted = '%s' ".
  256. "WHERE analysis_id = %d ";
  257. $previous_db = tripal_db_set_active('chado'); // use chado database
  258. db_query($sql, $node->analysisname, $node->description, $node->program,
  259. $node->programversion,$node->algorithm,$node->sourcename,
  260. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  261. tripal_db_set_active($previous_db); // switch back to drupal database
  262. // Create a title for the analysis node using the unique keys so when the
  263. // node is saved, it will have a title
  264. $record = new stdClass();
  265. // If the analysis has a name, use it as the node title. If not, construct
  266. // the title using program, programversion, and sourcename
  267. if ($node->analysisname) {
  268. $record->title = $node->analysisname;
  269. } else {
  270. //Construct node title as "program (version)
  271. $record->title = "$node->program ($node->programversion)";
  272. }
  273. $record->nid = $node->nid;
  274. drupal_write_record('node',$record,'nid');
  275. drupal_write_record('node_revisions',$record,'nid');
  276. }
  277. }
  278. /*******************************************************************************
  279. * When editing or creating a new node of type 'chado_analysis' we need
  280. * a form. This function creates the form that will be used for this.
  281. *
  282. * @ingroup tripal_analysis
  283. */
  284. function chado_analysis_form ($node){
  285. $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. );
  302. $form['program']= array(
  303. '#type' => 'textfield',
  304. '#title' => t('Program'),
  305. '#required' => TRUE,
  306. '#default_value' => $node->analysis->program,
  307. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
  308. );
  309. $form['programversion']= array(
  310. '#type' => 'textfield',
  311. '#title' => t('Program Version'),
  312. '#required' => TRUE,
  313. '#default_value' => $node->analysis->programversion,
  314. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]"),
  315. );
  316. $form['algorithm']= array(
  317. '#type' => 'textfield',
  318. '#title' => t('Algorithm'),
  319. '#required' => FALSE,
  320. '#default_value' => $node->analysis->algorithm,
  321. '#description' => t("Algorithm name, e.g. blast."),
  322. );
  323. $form['sourcename']= array(
  324. '#type' => 'textfield',
  325. '#title' => t('Source Name'),
  326. '#required' => TRUE,
  327. '#default_value' => $node->analysis->sourcename,
  328. '#description' => t('The name of the source data. This could be a file name, data set name or a
  329. small description for how the data was collected. For long descriptions use the description field below'),
  330. );
  331. $form['sourceversion']= array(
  332. '#type' => 'textfield',
  333. '#title' => t('Source Version'),
  334. '#required' => FALSE,
  335. '#default_value' => $node->analysis->sourceversion,
  336. '#description' => t('If the source dataset has a version, include it here'),
  337. );
  338. $form['sourceuri']= array(
  339. '#type' => 'textfield',
  340. '#title' => t('Source URI'),
  341. '#required' => FALSE,
  342. '#default_value' => $node->analysis->sourceuri,
  343. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  344. Someone could recreate the analysis directly by going to this URI and
  345. fetching the source data (e.g. the blast database, or the training model)."),
  346. );
  347. // Get time saved in chado
  348. $default_time = $node->analysis->timeexecuted;
  349. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  350. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  351. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  352. // If the time is not set, use current time
  353. if (!$default_time) {
  354. $default_time = time();
  355. $year = format_date($default_time, 'custom', 'Y');
  356. $month = format_date($default_time, 'custom', 'n');
  357. $day = format_date($default_time, 'custom', 'j');
  358. }
  359. $form['timeexecuted']= array(
  360. '#type' => 'date',
  361. '#title' => t('Time Executed'),
  362. '#required' => TRUE,
  363. '#default_value' => array(
  364. 'year' => $year,
  365. 'month' => $month,
  366. 'day' => $day,
  367. ),
  368. );
  369. $form['description']= array(
  370. '#type' => 'textarea',
  371. '#rows' => 15,
  372. '#title' => t('Description and/or Program Settings'),
  373. '#required' => FALSE,
  374. '#default_value' => $node->analysis->description,
  375. '#description' => t('Please provide all necessary information to allow
  376. someone to recreate the analysis, including materials and methods
  377. for collection of the source data and performing the analysis'),
  378. );
  379. return $form;
  380. }
  381. /*******************************************************************************
  382. * When a node is requested by the user this function is called to allow us
  383. * to add auxiliary data to the node object.
  384. *
  385. * @ingroup tripal_analysis
  386. */
  387. function chado_analysis_load($node){
  388. $additions = new stdClass();
  389. // get the analysis_id for this node:
  390. $sql = "SELECT analysis_id FROM {chado_analysis} WHERE nid = %d";
  391. $ana_node = db_fetch_object(db_query($sql, $node->nid));
  392. if ($ana_node) {
  393. // get analysis information
  394. $sql = "SELECT Analysis_id, name, description, program, ".
  395. " programversion, algorithm, sourcename, sourceversion, ".
  396. " sourceuri, timeexecuted ".
  397. "FROM {Analysis} ".
  398. "WHERE Analysis_id = $ana_node->analysis_id";
  399. $previous_db = tripal_db_set_active('chado'); // use chado database
  400. $additions->analysis = db_fetch_object(db_query($sql));
  401. // get number of features assc with this analysis
  402. // $sql = "SELECT count(feature_id) as featurecount ".
  403. // "FROM {Analysisfeature} ".
  404. // "WHERE Analysis_id = %d";
  405. // $additions->featurecount = db_result(db_query($sql, $ana_node->analysis_id));
  406. tripal_db_set_active($previous_db); // now use drupal database
  407. }
  408. // If the analysis has a name, use it as the node title. If not, construct
  409. // the title using program programversion, and sourcename
  410. if ($additions->analysis->name) {
  411. $additions->title = $additions->analysis->name;
  412. } else {
  413. // Construct node title as "program version (source)
  414. $additions->title = "$additions->analysis->program ($additions->analysis->programversion)";
  415. }
  416. return $additions;
  417. }
  418. /*******************************************************************************
  419. * This function customizes the view of the chado_analysis node. It allows
  420. * us to generate the markup.
  421. *
  422. * @ingroup tripal_analysis
  423. */
  424. function chado_analysis_view ($node, $teaser = FALSE, $page = FALSE) {
  425. // use drupal's default node view:
  426. if (!$teaser) {
  427. $node = node_prepare($node, $teaser);
  428. // When previewing a node submitting form, it shows 'Array' instead of
  429. // correct date format. We need to format the date here
  430. $time = $node->timeexecuted;
  431. if(is_array($time)){
  432. $month = $time['month'];
  433. $day = $time['day'];
  434. $year = $time['year'];
  435. $timestamp = $year.'-'.$month.'-'.$day;
  436. $node->timeexecuted = $timestamp;
  437. }
  438. }
  439. return $node;
  440. }
  441. /*******************************************************************************
  442. * Synchronize analyses from chado to drupal
  443. *
  444. * @ingroup tripal_analysis
  445. */
  446. function tripal_analysis_sync_analyses ($analysis_id = NULL, $job_id = NULL){
  447. global $user;
  448. $page_content = '';
  449. if(!$analysis_id){
  450. $sql = "SELECT Analysis_id, name, description, program, ".
  451. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  452. " timeexecuted ".
  453. "FROM {Analysis} ";
  454. $previous_db = tripal_db_set_active('chado'); // use chado database
  455. $results = db_query($sql);
  456. tripal_db_set_active($previous_db); // now use drupal database
  457. } else {
  458. $sql = "SELECT Analysis_id, name, description, program, ".
  459. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  460. " timeexecuted ".
  461. "FROM {Analysis} ".
  462. "WHERE analysis_id = %d";
  463. $previous_db = tripal_db_set_active('chado'); // use chado database
  464. $results = db_query($sql,$analysis_id);
  465. tripal_db_set_active($previous_db); // now use drupal database
  466. }
  467. // We'll use the following SQL statement for checking if the analysis
  468. // already exists as a drupal node.
  469. $sql = "SELECT * FROM {chado_analysis} ".
  470. "WHERE analysis_id = %d";
  471. while($analysis = db_fetch_object($results)){
  472. print "syncing analysis ";
  473. print $analysis->name;
  474. print ", ";
  475. print $analysis->analysis_id;
  476. print "\n";
  477. // check if this analysis already exists in the drupal database. if it
  478. // does then skip this analysis and go to the next one.
  479. if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
  480. $new_node = new stdClass();
  481. // try to access analysis type for this analysis
  482. $sql = "SELECT * FROM {analysisprop}
  483. WHERE analysis_id = %d
  484. AND type_id =
  485. (SELECT cvterm_id from {cvterm} where name = '%s')
  486. ";
  487. $previous_db = tripal_db_set_active('chado');
  488. $analysis_type = db_fetch_object(db_query($sql, $analysis->analysis_id, "analysis_type"));
  489. tripal_db_set_active($previous_db);
  490. // Get the type of analysis using cvterm_id
  491. // Current possibilities: kegg, unigene, interpro, blast
  492. if ($analysis_type) {
  493. // This is a unigene analysis
  494. if ($analysis_type->value == 'tripal_analysis_unigene') {
  495. $new_node->type = 'chado_analysis_unigene';
  496. // This is a blast analysis
  497. } else if ($analysis_type->value == 'tripal_analysis_blast') {
  498. $new_node->type = 'chado_analysis_blast';
  499. // This is a interpro analysis
  500. } else if ($analysis_type->value == 'tripal_analysis_interpro') {
  501. $new_node->type = 'chado_analysis_interpro';
  502. // This is a kegg analysis
  503. } else if ($analysis_type->value == 'tripal_analysis_kegg' ){
  504. $new_node->type = 'chado_analysis_kegg';
  505. } else {
  506. $new_node->type = 'chado_analysis';
  507. }
  508. // If it doesn't exist, this analysis is generic
  509. } else {
  510. $new_node->type = 'chado_analysis';
  511. }
  512. print "analysis type is $new_node->type\n";
  513. $new_node->uid = $user->uid;
  514. $new_node->analysis_id = $analysis->analysis_id;
  515. $new_node->analysisname = $analysis->name;
  516. $new_node->description = $analysis->description;
  517. $new_node->program = $analysis->program;
  518. $new_node->programversion = $analysis->programversion;
  519. $new_node->algorithm = $analysis->algorithm;
  520. $new_node->sourcename = $analysis->sourcename;
  521. $new_node->sourceversion = $analysis->sourceversion;
  522. $new_node->sourceuri = $analysis->sourceuri;
  523. $new_node->timeexecuted = $analysis->timeexecuted;
  524. // If the analysis has a name, use it as the node title. If not,
  525. // construct the title using program, programversion, and sourcename
  526. if ($new_node->analysisname) {
  527. $new_node->title = $new_node->analysisname;
  528. } else {
  529. //Construct node title as "program (version)"
  530. $new_node->title = "$analysis->program ($analysis->programversion)";
  531. }
  532. node_validate($new_node);
  533. $errors = form_get_errors();
  534. if($errors){
  535. print_r($errors);
  536. }
  537. else{
  538. ##if(!form_get_errors()){
  539. $node = node_submit($new_node);
  540. node_save($node);
  541. if($node->nid){
  542. $page_content .= "Added $new_node->title<br>";
  543. }
  544. }
  545. } else {
  546. $page_content .= "Skipped $new_node->title<br>";
  547. }
  548. }
  549. return $page_content;
  550. }
  551. /**
  552. *
  553. */
  554. function tripal_analysis_validate($node, &$form){
  555. ##dprint_r($node);
  556. // This validation is being used for three activities:
  557. // CASE A: Update a node that exists in both drupal and chado
  558. // CASE B: Synchronizing a node from chado to drupal
  559. // CASE C: Inserting a new node that exists in niether drupal nor chado
  560. // Only nodes being updated will have an nid already
  561. if($node->nid){
  562. //---------------------------------------------------
  563. // CASE A: We are validating a form for updating an existing node
  564. //---------------------------------------------------
  565. // TO DO: check that the new fields don't yield a non-unique primary key in chado
  566. }
  567. else{
  568. // To differentiate if we are syncing or creating a new analysis altogther, see if an
  569. // analysis_id already exists
  570. if($node->analysis_id){
  571. //---------------------------------------------------
  572. // CASE B: Synchronizing a node from chado to drupal
  573. //---------------------------------------------------
  574. }
  575. else{
  576. //---------------------------------------------------
  577. // CASE C: We are validating a form for inserting a new node
  578. //---------------------------------------------------
  579. // The primary key for the chado analysis table is
  580. // program, programversion, sourcename
  581. // Check to see if this analysis really is new -ie, it doesn't have the same
  582. // primary key as any other analysis
  583. $sql = "SELECT analysis_id ".
  584. "FROM {analysis} ".
  585. "WHERE program='%s'".
  586. "AND programversion='%s'".
  587. "AND sourcename='%s'";
  588. $previous_db = tripal_db_set_active('chado');
  589. $analysis_id = db_result(db_query($sql, $node->program, $node->programversion, $node->sourcename));
  590. tripal_db_set_active($previous_db);
  591. if($analysis_id){
  592. //---------------------------------------------------
  593. // this primary key already exists in chado analysis table!
  594. //---------------------------------------------------
  595. // check to see if it has also been synced with drupal
  596. $sql = "SELECT nid FROM {chado_analysis} ".
  597. "WHERE analysis_id = %d";
  598. $node_id = db_result(db_query($sql, $analysis_id));
  599. if($node_id){
  600. //---------------------------------------------------
  601. // the analysis has already been synced with drupal, redirect the user
  602. // to modify that node or start over
  603. //---------------------------------------------------
  604. $error = 'This analysis already exists in the chado database (analysis id ';
  605. $error .= $analysis_id.') and has been synchronized ';
  606. $error .= 'with drupal. See node '.$node_id.' if you wish to update that analysis. ';
  607. $error .= ' For a new analysis, please select a unique primary key ';
  608. $error .= '(primary key consists of sourcename, program and programversion).';
  609. form_set_error('sourcename', t($error));
  610. }
  611. else{
  612. //---------------------------------------------------
  613. // the analysis does not exist in drupal - tell the user
  614. // to sync from chado or create a new unique primary key
  615. //---------------------------------------------------
  616. $error = 'This analysis already exists in the chado database (analysis id ';
  617. $error .= $analysis_id.') but has not been synchronized ';
  618. $error .= 'with drupal. See the tripal admin pages to synchronize. ';
  619. $error .= ' For a new analysis, please select a unique primary key ';
  620. $error .= '(primary key consists of sourcename, program and programversion).';
  621. form_set_error('sourcename', t($error));
  622. }
  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. *
  848. *
  849. * @ingroup tripal_analysis
  850. */
  851. /*
  852. function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
  853. $i = 0;
  854. // if the caller provided a analysis_id then get all of the features
  855. // associated with the analysis. Otherwise get all sequences associated
  856. // with all libraries.
  857. if(!$analysis_id){
  858. $sql = "SELECT Analysis_id, Feature_id ".
  859. "FROM {Analysisfeature} ".
  860. "ORDER BY analysis_id";
  861. $previous_db = tripal_db_set_active('chado'); // use chado database
  862. $results = db_query($sql);
  863. tripal_db_set_active($previous_db); // now use drupal database
  864. } else {
  865. $sql = "SELECT Analysis_id, Feature_id ".
  866. "FROM {Analysisfeature} ".
  867. "WHERE analysis_id = %d";
  868. "ORDER BY analysis_id";
  869. $previous_db = tripal_db_set_active('chado'); // use chado database
  870. $results = db_query($sql,$analysis_id);
  871. tripal_db_set_active($previous_db); // now use drupal database
  872. }
  873. // load into ids array
  874. $count = 0;
  875. $ids = array();
  876. while($id = db_fetch_object($results)){
  877. $ids[$count] = $id->feature_id;
  878. $count++;
  879. }
  880. $interval = intval($count * 0.01);
  881. foreach($ids as $feature_id){
  882. // update the job status every 1% features
  883. if($job_id and $i % interval == 0){
  884. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  885. }
  886. tripal_feature_sync_feature ($feature_id);
  887. $i++;
  888. }
  889. } */
  890. /*******************************************************************************
  891. *
  892. *
  893. * @ingroup tripal_analysis
  894. */
  895. /*
  896. function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
  897. $i = 0;
  898. // if the caller provided a analysis_id then get all of the features
  899. // associated with the analysis. Otherwise get all sequences assoicated
  900. // with all libraries.
  901. if(!$analysis_id){
  902. $sql = "SELECT Analysis_id, Feature_id ".
  903. "FROM {Analysisfeature} ".
  904. "ORDER BY analysis_id";
  905. $previous_db = tripal_db_set_active('chado'); // use chado database
  906. $results = db_query($sql);
  907. tripal_db_set_active($previous_db); // now use drupal database
  908. } else {
  909. $sql = "SELECT Analysis_id, Feature_id ".
  910. "FROM {Analysisfeature} ".
  911. "WHERE analysis_id = %d";
  912. "ORDER BY analysis_id";
  913. $previous_db = tripal_db_set_active('chado'); // use chado database
  914. $results = db_query($sql,$analysis_id);
  915. tripal_db_set_active($previous_db); // now use drupal database
  916. }
  917. // load into ids array
  918. $count = 0;
  919. $ids = array();
  920. while($id = db_fetch_object($results)){
  921. $ids[$count] = $id->feature_id;
  922. $count++;
  923. }
  924. // make sure our vocabularies are set before proceeding
  925. tripal_feature_set_vocabulary();
  926. // use this SQL for getting the nodes
  927. $nsql = "SELECT * FROM {chado_feature} CF ".
  928. " INNER JOIN {node} N ON N.nid = CF.nid ".
  929. "WHERE feature_id = %d";
  930. // iterate through the features and set the taxonomy
  931. $interval = intval($count * 0.01);
  932. foreach($ids as $feature_id){
  933. // update the job status every 1% features
  934. if($job_id and $i % $interval == 0){
  935. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  936. }
  937. $node = db_fetch_object(db_query($nsql,$feature_id));
  938. tripal_feature_set_taxonomy($node,$feature_id);
  939. $i++;
  940. }
  941. }
  942. */
  943. /*************************************************************************
  944. * Implements hook_views_api()
  945. * Purpose: Essentially this hook tells drupal that there is views support for
  946. * for this module which then includes tripal_analysis.views.inc where all the
  947. * views integration code is
  948. *
  949. * @ingroup tripal_analysis
  950. */
  951. function tripal_analysis_views_api() {
  952. return array(
  953. 'api' => 2.0,
  954. );
  955. }