tripal_analysis.module 34 KB

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