tripal_analysis_kegg.module 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. <?php
  2. /*******************************************************************************
  3. *
  4. ******************************************************************************/
  5. function tripal_analysis_kegg_init(){
  6. // add the tripal_analysis_kegg JS and CSS
  7. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_kegg.js');
  8. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_kegg.css');
  9. // add the jsTree JS and CSS
  10. drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
  11. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
  12. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
  13. }
  14. /*******************************************************************************
  15. * Provide information to drupal about the node types that we're creating
  16. * in this module
  17. */
  18. function tripal_analysis_kegg_node_info() {
  19. $nodes = array();
  20. $nodes['chado_analysis_kegg'] = array(
  21. 'name' => t('Analysis: KEGG'),
  22. 'module' => 'chado_analysis_kegg',
  23. 'description' => t('Results from a KEGG/KAAS analysis'),
  24. 'has_title' => FALSE,
  25. 'title_label' => t('Analysis: KEGG'),
  26. 'has_body' => FALSE,
  27. 'body_label' => t('KEGG Analysis Description'),
  28. 'locked' => TRUE
  29. );
  30. return $nodes;
  31. }
  32. /*******************************************************************************
  33. * Menu items are automatically added for the new node types created
  34. * by this module to the 'Create Content' Navigation menu item. This function
  35. * adds more menu items needed for this module.
  36. */
  37. function tripal_analysis_kegg_menu() {
  38. $items['brite/%'] = array(
  39. 'title' => t('KEGG BRITE'),
  40. 'page callback' => 'tripal_analysis_kegg_brite',
  41. 'page arguments' => array(1, 2),
  42. 'access arguments' => array('access content'),
  43. 'type' => MENU_CALLBACK
  44. );
  45. $items['tripal_analysis_kegg_org_report/%'] = array(
  46. 'path' => 'tripal_analysis_kegg_org_report',
  47. 'title' => t('Analysis KEGG report'),
  48. 'page callback' => 'tripal_analysis_kegg_org_report',
  49. 'page arguments' => array(1),
  50. 'access arguments' => array('access chado_analysis_kegg content'),
  51. 'type' => MENU_CALLBACK
  52. );
  53. return $items;
  54. }
  55. /*******************************************************************************
  56. * Set the permission types that the chado module uses. Essentially we
  57. * want permissionis that protect creation, editing and deleting of chado
  58. * data objects
  59. */
  60. function tripal_analysis_kegg_perm(){
  61. return array(
  62. 'access chado_analysis_kegg content',
  63. 'create chado_analysis_kegg content',
  64. 'delete chado_analysis_kegg content',
  65. 'edit chado_analysis_kegg content',
  66. );
  67. }
  68. /*******************************************************************************
  69. * The following function proves access control for users trying to
  70. * perform actions on data managed by this module
  71. */
  72. function chado_analysis_kegg_access($op, $node, $account){
  73. if ($op == 'create') {
  74. return user_access('create chado_analysis_kegg content', $account);
  75. }
  76. if ($op == 'update') {
  77. if (user_access('edit chado_analysis_kegg content', $account)) {
  78. return TRUE;
  79. }
  80. }
  81. if ($op == 'delete') {
  82. if (user_access('delete chado_analysis_kegg content', $account)) {
  83. return TRUE;
  84. }
  85. }
  86. if ($op == 'view') {
  87. if (user_access('access chado_analysis_kegg content', $account)) {
  88. return TRUE;
  89. }
  90. }
  91. return FALSE;
  92. }
  93. /*******************************************************************************
  94. */
  95. function tripal_analysis_kegg_brite($analysis_id, $type_id, $ajax){
  96. global $base_url;
  97. $theme_dir = drupal_get_path('theme','tripal');
  98. // If not called by ajax
  99. if (!$ajax) {
  100. $content .=
  101. "<div id=\"tripal_kegg_brite_results\" class=\"tripal_kegg_brite-info-box\">
  102. <table>
  103. <tr>
  104. <th>KEGG BRITE</th>
  105. <th id=\"tripal_kegg_brite_header\">Hierarchy:</th>
  106. </tr>
  107. <tr>
  108. <td nowrap valign=\"top\">
  109. ";
  110. // List all BRITE terms on the left
  111. $sql = "SELECT DISTINCT CVT.name, CVT.cvterm_id
  112. FROM {cvterm} CVT
  113. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  114. WHERE AP.analysis_id = %d AND CVT.definition LIKE 'KEGG BRITE term: %'
  115. ORDER BY CVT.cvterm_id";
  116. $previous_db = tripal_db_set_active('chado');
  117. $result = db_query($sql, $analysis_id);
  118. tripal_db_set_active($previous_db);
  119. while ($brite_term = db_fetch_object($result)) {
  120. $url = url("brite/$analysis_id/$brite_term->cvterm_id/1");
  121. $content .= "<li class=\"tripal_kegg_brite_terms\"><a onclick=\"return tripal_update_brite(".
  122. "this,$brite_term->cvterm_id,'$base_url','$theme_dir')\" href=\"$url\">
  123. $brite_term->name
  124. </a></li>";
  125. }
  126. // Show the hierarchy tree
  127. $content .="</td>
  128. <td nowrap id=\"tripal_kegg_brite_hierarchy\" valign=\"top\">";
  129. $content .= "<i>Note:</i> Click a BRITE term for its functional hierarchy";
  130. // If called by ajax, generate tree structure
  131. } else {
  132. // Get BRITE term from cvterm table
  133. $previous_db = tripal_db_set_active('chado');
  134. $sql = 'SELECT name FROM {cvterm} WHERE cvterm_id=%d';
  135. $brite_term = db_result(db_query($sql, $type_id));
  136. // Get BRITE hierarchy tree
  137. $sql = "SELECT value
  138. FROM {analysisprop} AP
  139. INNER JOIN CVterm CVT on AP.type_id = CVT.cvterm_id
  140. INNER JOIN CV on CVT.cv_id = CV.cv_id
  141. WHERE CV.name = 'tripal' and CVT.name = '%s'
  142. AND AP.analysis_id = %d";
  143. $result = db_fetch_object(db_query($sql, $brite_term, $analysis_id));
  144. tripal_db_set_active($previous_db);
  145. $content .= "<div class=\"tripal_kegg_brite_tree\" id=\"tripal_kegg_brite_tree_$type_id\">$result->value</div>";
  146. }
  147. if (!$ajax) {
  148. $content .= " </td>
  149. </tr>
  150. </table>
  151. </div>";
  152. }
  153. // since this function provides output for addition into
  154. // an analysis page, as well as an AJAX refresh of content
  155. // within the BRITE hierarchy we need to setup the return
  156. // different depending on the request type
  157. if($ajax){
  158. drupal_json(array('update' => $content,
  159. 'id' => "tripal_kegg_brite_tree_$type_id",
  160. 'brite_term' => "Hierarchy: $brite_term"));
  161. } else {
  162. return $content;
  163. }
  164. }
  165. /*******************************************************************************
  166. * Provide a KEGG Analysis form
  167. */
  168. function chado_analysis_kegg_form ($node){
  169. // add in the default fields
  170. $form = chado_analysis_form($node);
  171. // set the defaults
  172. $kegg = $node->analysis->tripal_analysis_kegg;
  173. $query_re = $kegg->query_re;
  174. $query_type = $kegg->query_type;
  175. $query_uniquename = $kegg->query_uniquename;
  176. $hierfile = $kegg->hierfile;
  177. $moreSettings ['kegg'] = 'KEGG Analysis Settings';
  178. $form['kegg'] = array(
  179. '#title' => t('KEGG Settings'),
  180. '#type' => 'fieldset',
  181. '#description' => t('Specific Settings for KEGG Analysis.'),
  182. '#collapsible' => TRUE,
  183. '#attributes' => array('id' => 'kegg-extra-settings'),
  184. '#weight' => 11
  185. );
  186. $form['kegg']['hierfile'] = array(
  187. '#title' => t('KAAS hier.tar.gz Output File'),
  188. '#type' => 'textfield',
  189. '#description' => t('The full path to the hier.tar.gz file generated by KAAS.
  190. Alternatively, you can input the full path to the directory
  191. that contains decompressed kegg files.'),
  192. '#default_value' => $hierfile,
  193. );
  194. $form['kegg']['query_re'] = array(
  195. '#title' => t('Query Name RE'),
  196. '#type' => 'textfield',
  197. '#description' => t('Enter the regular expression that will extract the '.
  198. 'feature name from the results line in the KEGG heir results. This will be '.
  199. 'the same as the definition line in the query FASTA file used for the analysis. This option is '.
  200. 'is only required when the query does not identically match a feature '.
  201. 'in the database.'),
  202. '#default_value' => $query_re,
  203. );
  204. $form['kegg']['query_type'] = array(
  205. '#title' => t('Query Type'),
  206. '#type' => 'textfield',
  207. '#description' => t('Please enter the Sequence Ontology term that describes '.
  208. 'the query sequences used for KEGG. This is only necessary if two '.
  209. 'or more sequences have the same name.'),
  210. '#default_value' => $query_type,
  211. );
  212. $form['kegg']['query_uniquename'] = array(
  213. '#title' => t('Use Unique Name'),
  214. '#type' => 'checkbox',
  215. '#description' => t('Select this checboxk if the feature name in the KEGG heir file '.
  216. 'matches the uniquename in the database. By default, the feature will '.
  217. 'be mapped to the "name" of the feature.'),
  218. '#default_value' => $query_uniquename,
  219. );
  220. $form['kegg']['keggjob'] = array(
  221. '#type' => 'checkbox',
  222. '#title' => t('Submit a job to parse the kegg output into Chado'),
  223. '#description' => t('Note: features used in the KAAS analysis must '.
  224. 'exist in chado before parsing the file. Otherwise, KEGG '.
  225. 'results that cannot be linked to a feature will be '.
  226. 'discarded.'),
  227. );
  228. /* $form['kegg']['keggkeywordjob'] = array(
  229. '#type' => 'checkbox',
  230. '#title' => t('Submit a job to extract keywords from the KEGG html output'),
  231. '#description' => t('Note: KEGG results are only searchable after keywords are extracted. Do not run this twice if you have already done so.'),
  232. );
  233. */
  234. return $form;
  235. }
  236. /**
  237. *
  238. */
  239. function chado_analysis_kegg_validate($node, &$form){
  240. // use the analysis parent to validate the node
  241. tripal_analysis_validate($node, $form);
  242. }
  243. /*******************************************************************************
  244. *
  245. */
  246. function chado_analysis_kegg_insert($node){
  247. // insert the analysis
  248. chado_analysis_insert($node);
  249. // set the type for this analysis
  250. tripal_analysis_insert_property($node->analysis_id,'analysis_type','tripal_analysis_kegg');
  251. // now add in the remaining settings as a single property but separated by bars
  252. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile);
  253. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re);
  254. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type);
  255. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename);
  256. // Add a job if the user wants to parse the html output
  257. chado_analysis_kegg_submit_job($node);
  258. }
  259. /**
  260. *
  261. */
  262. function chado_analysis_kegg_submit_job($node){
  263. global $user;
  264. global $base_url;
  265. if($node->keggjob) {
  266. $job_args[0] = $node->analysis_id;
  267. $job_args[1] = $node->hierfile;
  268. $job_args[2] = $base_url;
  269. $job_args[3] = $node->query_re;
  270. $job_args[4] = $node->query_type;
  271. $job_args[5] = $node->query_uniquename;
  272. if (is_readable($node->hierfile)) {
  273. $fname = preg_replace("/.*\/(.*)/", "$1", $node->hierfile);
  274. tripal_add_job("Parse KAAS output: $fname",'tripal_analysis_kegg',
  275. 'tripal_analysis_kegg_parseHierFile', $job_args, $user->uid);
  276. } else {
  277. drupal_set_message("Can not open KAAS hier.tar.gz output file. Job not scheduled.");
  278. }
  279. }
  280. // Add a job if the user wants to the keywords from the HTML output
  281. if ($node->keggkeywordjob) {
  282. $analysis_id =chado_get_id_for_node('analysis', $node);
  283. $job_args[0] = $analysis_id;
  284. tripal_add_job("Extract keywords for search: $node->analysisname",'tripal_analysis_kegg',
  285. 'tripal_analysis_kegg_extract_keywords', $job_args, $user->uid);
  286. }
  287. }
  288. /*******************************************************************************
  289. * Delete KEGG anlysis
  290. */
  291. function chado_analysis_kegg_delete($node){
  292. chado_analysis_delete($node);
  293. }
  294. /*******************************************************************************
  295. * Update KEGG analysis
  296. */
  297. function chado_analysis_kegg_update($node){
  298. // insert the analysis
  299. chado_analysis_update($node);
  300. // set the type for this analysis
  301. tripal_analysis_update_property($node->analysis_id,'analysis_type','tripal_analysis_kegg',1);
  302. // now add in the remaining settings as a single property but separated by bars
  303. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile,1);
  304. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re,1);
  305. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type,1);
  306. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename,1);
  307. // Add a job if the user wants to parse the output
  308. chado_analysis_kegg_submit_job($node);
  309. }
  310. /*******************************************************************************
  311. * When a node is requested by the user this function is called to allow us
  312. * to add auxiliary data to the node object.
  313. */
  314. function chado_analysis_kegg_load($node){
  315. // load the default set of analysis fields
  316. $additions = chado_analysis_load($node);
  317. // create some variables for easier lookup
  318. $analysis = $additions->analysis;
  319. $analysis_id = $analysis->analysis_id;
  320. // get the heirfile name
  321. $hierfile = tripal_analysis_get_property($analysis_id,'analysis_kegg_settings');
  322. $query_re = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_re');
  323. $query_type = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_type');
  324. $query_uniquename= tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_uniquename');
  325. $analysis->tripal_analysis_kegg->hierfile = $hierfile->value;
  326. $analysis->tripal_analysis_kegg->query_re = $query_re->value;
  327. $analysis->tripal_analysis_kegg->query_type = $query_type->value;
  328. $analysis->tripal_analysis_kegg->query_uniquename= $query_uniquename->value;
  329. return $additions;
  330. }
  331. /**
  332. *
  333. */
  334. function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
  335. // use drupal's default node view:
  336. if (!$teaser) {
  337. $node = node_prepare($node, $teaser);
  338. // When previewing a node submitting form, it shows 'Array' instead of
  339. // correct date format. We need to format the date here
  340. $time = $node->timeexecuted;
  341. if(is_array($time)){
  342. $month = $time['month'];
  343. $day = $time['day'];
  344. $year = $time['year'];
  345. $timestamp = $year.'-'.$month.'-'.$day;
  346. $node->timeexecuted = $timestamp;
  347. }
  348. }
  349. return $node;
  350. }
  351. /********************************************************************************
  352. */
  353. function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path,
  354. $query_re,$query_type,$query_uniquename,$job_id) {
  355. // If user input a file (e.g. hier.tar.gz), decompress it first
  356. if (is_file($hierfile)) {
  357. // generate a unique directory name for extracting and parsing the file
  358. $data_dir = sys_get_temp_dir() . "/" . uniqid();
  359. mkdir($data_dir);
  360. $stderr = shell_exec("cd $data_dir; tar -zxf $hierfile;");
  361. print "$stderr\n";
  362. $hierdir = $data_dir . "/hier";
  363. }
  364. // Otherwise, treat it as a directory
  365. else {
  366. $hierdir = $hierfile;
  367. }
  368. $dir_handle = @opendir($hierdir) or die("Unable to open $hierdir");
  369. $total_files = count(glob($hierdir . '/*.*'));
  370. print "There are $total_files keg file(s).\n";
  371. $interval = intval($total_files * 0.01);
  372. if($interval > 1){
  373. $interval = 1;
  374. }
  375. $no_file = 0;
  376. // Remove the analysis features for this analysis
  377. // we will rebuild them from just this parsing
  378. $select = array('analysis_id' => $analysis_id);
  379. if(!tripal_core_chado_delete('analysisfeature',$select)){
  380. print "ERROR: Unable to clear previous results.\n";
  381. exit;
  382. }
  383. while ($file = readdir($dir_handle)) {
  384. if(preg_match("/^.*\.keg/",$file)){
  385. // Update the progress
  386. if ($no_file % $interval == 0) {
  387. $percentage = (int) ($no_file / $total_files * 100);
  388. tripal_job_set_progress($job_id, $percentage);
  389. print $percentage."%\r";
  390. }
  391. $no_file ++;
  392. # $heirarchy variable will be set in tripal_analysis_kegg_parse_kegg_file()
  393. $results = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$heirarchy,
  394. $analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
  395. # add the item to the database
  396. if(count($results) > 0){
  397. print "Loading results for '$heirarchy'\n";
  398. // We want to insert the KEGG heirarchy results into the
  399. // analysisprop table. We insert a separate record for each
  400. // heirarchy. But we need to clear out any prevous results first.
  401. $sql = "DELETE
  402. FROM {analysisprop}
  403. WHERE analysis_id = %d
  404. AND type_id = (SELECT cvterm_id
  405. FROM {cvterm} CVT
  406. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  407. WHERE CV.name = 'tripal' AND CVT.name = '%s'
  408. )
  409. ";
  410. $previous_db = tripal_db_set_active('chado');
  411. db_result(db_query($sql, $analysis_id, $heirarchy));
  412. tripal_db_set_active($previous_db);
  413. $previous_db = tripal_db_set_active('chado'); // Use chado database
  414. // Get type_id for the BRITE term
  415. $sql = "SELECT cvterm_id
  416. FROM {cvterm} CVT
  417. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  418. WHERE CV.name = 'tripal' AND CVT.name = '%s'";
  419. $brite_cvterm_id = db_result(db_query($sql, $heirarchy));
  420. if(!$brite_cvterm_id){
  421. print "ERROR: Cannot find cvterm for '$heirarchy'.\n";
  422. exit;
  423. }
  424. // convert the array to text for saving in the database
  425. // Replace all single quote as HTML code before insert
  426. $i = 0;
  427. $content = '<ul>';
  428. tripal_analysis_kegg_array2list($results,$content,$i);
  429. $content .= '</ul>';
  430. $content = preg_replace("/\'/", "&#39;", $content);
  431. // Insert the value
  432. $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value)
  433. VALUES (%d, %d,'$content')";
  434. if(!db_query($sql, $analysis_id, $brite_cvterm_id)){
  435. print "ERROR: Cannot add '$heirarchy' results to the database.\n";
  436. exit;
  437. }
  438. tripal_db_set_active($previous_db); // Use drupal database
  439. }
  440. }
  441. }
  442. print "Done.\n";
  443. tripal_job_set_progress($job_id,100);
  444. closedir($dir_handle);
  445. // If user input a file, remove decompressed files after parsing
  446. if (is_file($hierfile)) {
  447. $stderr = shell_exec("rm -r $data_dir;");
  448. print "$stderr\n";
  449. }
  450. return;
  451. }
  452. /**
  453. *
  454. */
  455. function tripal_analysis_kegg_array2list($array,&$content,&$i){
  456. foreach($array as $index => $item){
  457. if(is_array($item)){
  458. if(is_numeric($index)){
  459. tripal_analysis_kegg_array2list($item,$content,$i);
  460. } else {
  461. $content .= "<li id=\"term_$i\"><a></a>$index\n<ul>";
  462. $i++;
  463. tripal_analysis_kegg_array2list($item,$content,$i);
  464. $content .= "</ul>\n</li>\n";
  465. }
  466. } else {
  467. $content .= "<li id=\"term_$i\"><a></a>$item</li>\n";
  468. $i++;
  469. }
  470. }
  471. }
  472. /*******************************************************************************
  473. * Parse *.keg files.
  474. * Each file has a definition line. BRITE term is extracted * from this line
  475. * and added to chado as a cvterm. Tree structure for this cvterm is then
  476. * generated and saved to analysisfeature and analysisfeatureprop tables.
  477. */
  478. function tripal_analysis_kegg_parse_kegg_file ($file, &$heirarchy, $analysis_id,
  479. $base_path, $query_re, $query_type, $query_uniquename)
  480. {
  481. print "Parsing $file\n";
  482. // get the 'kegg_brite_data' cvterm
  483. $select = array('name' => 'kegg_brite_data',
  484. 'cv_id' => array('name' => 'tripal'));
  485. $bdt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
  486. $brite_id = $bdt_arr[0]->cvterm_id;
  487. $filepos = 0;
  488. // iterate through the lines of the file and recurse through the various levels
  489. $handle = fopen($file,'r');
  490. while($line = fgets($handle)){
  491. $filepos += strlen($line);
  492. $line = trim($line);
  493. // the first line of the file provides the BRITE heirarchy name
  494. if(preg_match("/#.*nbsp;\s(.*)<\/h2>$/",$line,$matches)){
  495. $heirarchy = $matches[1];
  496. // get the CVterm for the heirarchy. If it doesn't exist then add it
  497. $select = array('name' => $heirarchy,'cv_id' => array('name' => 'tripal'));
  498. $cvt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
  499. if (count($cvt_arr) == 0) {
  500. tripal_add_cvterms($heirarchy, "KEGG BRITE term: $heirarchy");
  501. $cvt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
  502. }
  503. $heirarchy_id = $cvt_arr[0]->cvterm_id;
  504. // now that we have the file type we can recurse
  505. $next = tripal_analysis_kegg_get_next_line($handle,$filepos);
  506. $results = tripal_analysis_kegg_recurse_heir($handle,$next,$query_re,
  507. $query_type,$query_uniquename,$base_path,$analysis_id,
  508. $brite_id,$heirarchy,$filepos);
  509. }
  510. }
  511. fclose($handle);
  512. return $results;
  513. }
  514. /**
  515. *
  516. */
  517. function tripal_analysis_kegg_recurse_heir($handle, $line,$query_re,
  518. $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
  519. $heirarchy,&$filepos)
  520. {
  521. $results = array();
  522. // get the current level and the value
  523. $level = $line[0];
  524. $value = $line[1];
  525. // now get the next line to see what is coming next. If the
  526. // next level is greater then recurse immediately.
  527. $prevpos = $filepos;
  528. while($next = tripal_analysis_kegg_get_next_line($handle,$filepos)){
  529. $next_level = $next[0];
  530. $next_value = $next[1];
  531. // check this line to see if it has a feature we need to keep
  532. $ret = tripal_analysis_kegg_check_line_handle_feature($query_re,
  533. $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
  534. $heirarchy,$value);
  535. if($ret){
  536. $results[] = $ret;
  537. }
  538. // if we're going up a level then recurse immediately and add results to our array
  539. if(ord($next_level) > ord($level)){
  540. // now recurse
  541. $ret = tripal_analysis_kegg_recurse_heir($handle,$next,
  542. $query_re,$query_type, $query_uniquename, $base_path, $analysis_id,
  543. $brite_id,$heirarchy,$filepos);
  544. if(count($ret) > 0){
  545. $results[][$value] = $ret;
  546. }
  547. }
  548. // if we go down a level on the next line then reset the
  549. // filepointer and return
  550. elseif(ord($next_level) < ord($level)){
  551. fseek($handle,$prevpos);
  552. $filepos = $prevpos;
  553. return $results;
  554. }
  555. else {
  556. $line = $next;
  557. $level = $next[0];
  558. $value = $next[1];
  559. }
  560. $prevpos = $filepos;
  561. }
  562. return $results;
  563. }
  564. /**
  565. *
  566. */
  567. function tripal_analysis_kegg_get_next_line($handle,&$filepos){
  568. $good = 0;
  569. // get the next line in the file
  570. $line = fgets($handle);
  571. $filepos += strlen($line);
  572. // we hit the end of the file, so exit with a null
  573. if(!$line){
  574. return null;
  575. }
  576. while(!$good){
  577. $line = trim($line);
  578. preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
  579. $level = $matches[1];
  580. $value = $matches[2];
  581. // skip lines that aren't data or are empty
  582. if($level and $value) {
  583. // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
  584. // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
  585. $value = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$value);
  586. $value = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$value);
  587. // this line is good so let's exit out
  588. $good = 1;
  589. } else {
  590. $line = fgets($handle);
  591. $filepos += strlen($line);
  592. // we hit the end of the file, so exit with a null
  593. if(!$line){
  594. return null;
  595. }
  596. }
  597. }
  598. return array($level,$value);
  599. }
  600. /**
  601. *
  602. */
  603. function tripal_analysis_kegg_check_line_handle_feature($query_re,
  604. $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
  605. $heirarchy,$value)
  606. {
  607. // extract the features that have been mapped to the KEGG IDs
  608. if(preg_match("/^(.*?);\s*(\<a.+)/",$value,$matches)){
  609. $has_feature = 1;
  610. $fname = $matches[1];
  611. $keggterm = $matches[2];
  612. // get the feature name using the user's regular expression
  613. if ($query_re and preg_match("/$query_re/", $fname, $matches)) {
  614. $feature = $matches[1];
  615. }
  616. // If not in above format then pull up to the first space
  617. else {
  618. if (preg_match('/^(.*?)\s.*$/', $fname, $matches)) {
  619. $feature = $matches[1];
  620. }
  621. // if no match up to the first space then just use the entire string
  622. else {
  623. $feature = $fname;
  624. }
  625. }
  626. // now find the feature in chado
  627. $select = array();
  628. if($query_uniquename){
  629. $select['uniquename'] = $feature;
  630. } else {
  631. $select['name'] = $feature;
  632. }
  633. if($query_type){
  634. $select['type_id'] = array(
  635. 'cv_id' => array(
  636. 'name' => 'sequence'
  637. ),
  638. 'name' => $query_type,
  639. );
  640. }
  641. $feature_arr = tripal_core_chado_select('feature',array('feature_id'),$select);
  642. if(count($feature_arr) > 1){
  643. print "Ambiguous: '$feature' matches more than one feature and is being skipped.\n";
  644. return;
  645. }
  646. if(count($feature_arr) == 0){
  647. print "Failed: '$feature' cannot find a matching feature in the databasef. RE: $query_re; LINE: $fname\n";
  648. return;
  649. }
  650. $feature_id = $feature_arr[0]->feature_id;
  651. if($feature_id){
  652. print "Adding KEGG term for $feature ($feature_id,$analysis_id). $heirarchy\n";
  653. // add this term to the analysis feature properties
  654. tripal_analysis_kegg_insert_featureprop($feature_id,$analysis_id,
  655. $brite_id,$keggterm);
  656. // get the node ID of the feature if one exists
  657. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  658. $nid = db_result(db_query($sql, $feature_id));
  659. // Add link to each matched feature
  660. if($nid){
  661. $value = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"$base_path/node/$nid\">"."$1"."</a>"."$2",$value);
  662. }
  663. // if we have a feature match then add this to our results array
  664. return $value;
  665. }
  666. }
  667. return null;
  668. }
  669. /**
  670. *
  671. */
  672. function tripal_analysis_kegg_insert_featureprop ($feature_id, $analysis_id,
  673. $brite_id,$keggterm)
  674. {
  675. // add the analysisfeature record if it doesn't already exist.
  676. $values = array('feature_id' => $feature_id,'analysis_id' => $analysis_id);
  677. $analysisfeature_arr = tripal_core_chado_select('analysisfeature',
  678. array('analysisfeature_id'),$values);
  679. if(count($analysisfeature_arr) == 0){
  680. tripal_core_chado_insert('analysisfeature',$values);
  681. $analysisfeature_arr = tripal_core_chado_select('analysisfeature',
  682. array('analysisfeature_id'),$values);
  683. }
  684. $analysisfeature_id = $analysisfeature_arr[0]->analysisfeature_id;
  685. // Insert into analysisfeatureprop if the value doesn't already exist
  686. // KEGG heir results sometimes have the same record more than once.
  687. if($analysisfeature_id){
  688. // Get the highest rank for this feature_id in analysisfeatureprop table
  689. $sql = "SELECT rank FROM analysisfeatureprop WHERE analysisfeature_id = %d and type_id = %d ORDER BY rank DESC";
  690. $previous_db = tripal_db_set_active('chado');
  691. $result = db_fetch_object(db_query($sql,$analysisfeature_id,$brite_id));
  692. tripal_db_set_active($previous);
  693. $rank = 0;
  694. if ($result and $result->rank > 0) {
  695. $rank = $result->rank + 1;
  696. }
  697. $values = array(
  698. 'analysisfeature_id' => $analysisfeature_id,
  699. 'type_id' => $brite_id,
  700. 'value' => $keggterm,
  701. 'rank' => $rank,
  702. );
  703. return tripal_core_chado_insert('analysisfeatureprop',$values);
  704. }
  705. else {
  706. return 0;
  707. }
  708. }
  709. /**
  710. *
  711. *
  712. * @ingroup tripal_analysis_kegg
  713. */
  714. function tripal_analysis_kegg_block($op = 'list', $delta = 0, $edit=array()){
  715. switch($op) {
  716. case 'list':
  717. $blocks['featurekegg']['info'] = t('Tripal Feature KEGG Terms');
  718. $blocks['featurekegg']['cache'] = BLOCK_NO_CACHE;
  719. $blocks['orgkegg']['info'] = t('Organism KEGG Summary');
  720. $blocks['orgkegg']['cache'] = BLOCK_NO_CACHE;
  721. $blocks['kegg_base']['info'] = t('Analsyis: KEGG Details');
  722. $blocks['kegg_base']['cache'] = BLOCK_NO_CACHE;
  723. return $blocks;
  724. case 'view':
  725. if(user_access('access chado_analysis_kegg content') and arg(0) == 'node' and is_numeric(arg(1))) {
  726. $nid = arg(1);
  727. $node = node_load($nid);
  728. $block = array();
  729. switch($delta){
  730. case 'featurekegg':
  731. $block['subject'] = t('KEGG Terms');
  732. $block['content'] = theme('tripal_feature_kegg_terms',$node);
  733. break;
  734. case 'orgkegg':
  735. $block['subject'] = t('KEGG Terms');
  736. $block['content'] = theme('tripal_organism_kegg_summary',$node);
  737. break;
  738. case 'kegg_base':
  739. $block['subject'] = t('KEGG Terms');
  740. $block['content'] = theme('tripal_analysis_kegg_report',$node);
  741. break;
  742. default :
  743. }
  744. return $block;
  745. }
  746. }
  747. }
  748. /*******************************************************************************
  749. * HOOK: Implementation of hook_nodeapi()
  750. * Display library information for associated features or organisms
  751. * This function also provides contents for indexing
  752. */
  753. function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
  754. switch ($op) {
  755. case 'view':
  756. // add the library to the organism/feature search indexing
  757. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  758. $node->content['tripal_analysis_kegg_search_index'] = array(
  759. '#value' => theme('tripal_analysis_kegg_search_index',$node),
  760. '#weight' => 6,
  761. );
  762. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  763. $node->content['tripal_analysis_kegg_search_result'] = array(
  764. '#value' => theme('tripal_analysis_kegg_search_result',$node),
  765. '#weight' => 6,
  766. );
  767. } else {
  768. switch($node->type){
  769. case 'chado_organism':
  770. // Show KEGG report on the analysis page
  771. $node->content['tripal_organism_kegg_summary'] = array(
  772. '#value' => theme('tripal_organism_kegg_summary', $node),
  773. );
  774. break;
  775. case 'chado_feature':
  776. // Show KEGG terms on a feature page
  777. $node->content['tripal_feature_kegg_terms'] = array(
  778. '#value' => theme('tripal_feature_kegg_terms', $node),
  779. );
  780. }
  781. }
  782. break;
  783. }
  784. }
  785. /************************************************************************
  786. * We need to let drupal know about our theme functions and their arguments.
  787. * We create theme functions to allow users of the module to customize the
  788. * look and feel of the output generated in this module
  789. */
  790. function tripal_analysis_kegg_theme () {
  791. return array(
  792. 'tripal_analysis_kegg_search_index' => array (
  793. 'arguments' => array('node'),
  794. ),
  795. 'tripal_analysis_kegg_search_result' => array (
  796. 'arguments' => array('node'),
  797. ),
  798. 'tripal_organism_kegg_summary' => array (
  799. 'arguments' => array('node'=> null),
  800. 'template' => 'tripal_organism_kegg_summary',
  801. ),
  802. 'tripal_feature_kegg_terms' => array (
  803. 'arguments' => array('node'=> null),
  804. 'template' => 'tripal_feature_kegg_terms',
  805. ),
  806. 'tripal_analysis_kegg_report' => array (
  807. 'arguments' => array('node'=> null),
  808. 'template' => 'tripal_analysis_kegg_report',
  809. ),
  810. );
  811. }
  812. /**
  813. *
  814. *
  815. * @ingroup tripal_analysis_kegg
  816. */
  817. function tripal_analysis_kegg_preprocess(&$variables){
  818. // if the template file is the default node template file then we want
  819. // to add all of our variables.
  820. if($variables['template_files'][0] == 'node-chado_analysis_kegg'){
  821. $analysis = $variables['node']->analysis;
  822. $report = tripal_analysis_kegg_full_report($analysis->analysis_id);
  823. $analysis->tripal_analysis_kegg->kegg_report = $report;
  824. }
  825. }
  826. /*******************************************************************************
  827. *
  828. */
  829. function tripal_analysis_kegg_preprocess_tripal_organism_kegg_summary(&$variables){
  830. $node = $variables['node'];
  831. $organism = $node->organism;
  832. $organism->tripal_analysis_kegg->select_form = tripal_analysis_kegg_load_organism_kegg_summary($node);
  833. }
  834. /**
  835. *
  836. */
  837. function tripal_analysis_kegg_preprocess_tripal_feature_kegg_terms(&$variables){
  838. $node = $variables['node'];
  839. $feature = $node->feature;
  840. $feature->tripal_analysis_kegg->results = tripal_analysis_kegg_load_feature_terms($feature);
  841. }
  842. /************************************************************************
  843. */
  844. function theme_tripal_analysis_kegg_search_index($node){
  845. if ($node->type == 'chado_feature') {
  846. // Find cvterm_id for 'kegg_brite_data'
  847. $sql = "SELECT cvterm_id
  848. FROM {cvterm} CVT
  849. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  850. WHERE cv.name = 'tripal'
  851. AND CVT.name = '%s'";
  852. $previous_db = tripal_db_set_active('chado');
  853. $brite_id = db_result(db_query($sql, 'kegg_brite_data'));
  854. // Get analysis id
  855. $sql = "SELECT analysis_id AS aid
  856. FROM {analysisfeature} AF
  857. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  858. WHERE feature_id = %d
  859. AND AFP.type_id = %d
  860. GROUP BY analysis_id";
  861. $feature = $node->feature;
  862. $feature_id = $feature->feature_id;
  863. $hasResult = db_result(db_query($sql, $feature_id, $brite_id));
  864. $result = db_query($sql, $feature->feature_id, $brite_id);
  865. // Show kegg result ORDER BY time
  866. if ($hasResult) { // If there is any result, show expandable box
  867. $content = "";
  868. while ($ana = db_fetch_object($result)) {
  869. // Show analysis date
  870. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  871. FROM {analysis}
  872. WHERE analysis_id = %d";
  873. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  874. // Find node id for the analysis
  875. tripal_db_set_active($previous_db);
  876. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  877. $ana_url = url("node/".$ana_nid);
  878. $previous_db = tripal_db_set_active('chado');
  879. // Show content
  880. $content .= "$ana_details->name";
  881. // Show Kegg results
  882. $sql = "SELECT AFP.value AS afpvalue
  883. FROM {analysisfeatureprop} AFP
  884. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  885. WHERE AF.analysis_id = %d
  886. AND AF.feature_id = %d
  887. ";
  888. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  889. while ($afp = db_fetch_object($kegg_results)) {
  890. $content .= " $afp->afpvalue";
  891. }
  892. }
  893. }
  894. tripal_db_set_active($previous_db);
  895. return $content;
  896. }
  897. }
  898. /************************************************************************
  899. */
  900. function theme_tripal_analysis_kegg_search_result($node){
  901. $content = theme_tripal_analysis_kegg_node_add($node);
  902. return $content;
  903. }
  904. /************************************************************************
  905. */
  906. function tripal_analysis_kegg_load_organism_kegg_summary($node) {
  907. $organism = $node->organism;
  908. // find analyses that have KEGG terms
  909. $sql = "
  910. SELECT *
  911. FROM {kegg_by_organism} KBO
  912. WHERE organism_id = %d
  913. ORDER BY analysis_id DESC
  914. ";
  915. $previous_db = tripal_db_set_active('chado');
  916. $results = db_fetch_object(db_query($sql,$organism->organism_id));
  917. tripal_db_set_active($previous_db);
  918. $has_results = 0;
  919. if($results){
  920. $has_results = 1;
  921. }
  922. return array (
  923. 'has_results' => $has_results,
  924. 'form' => drupal_get_form('tripal_analysis_kegg_select_form',$node),
  925. );
  926. }
  927. /************************************************************************
  928. // Show Kegg additional information on a KEGG Analysis page
  929. if ($node->type == 'chado_analysis_kegg') {
  930. return tripal_analysis_kegg_full_report($node->analysis_id);
  931. }
  932. // Show Kegg-info-box on a Feature page
  933. else if ($node->type == 'chado_feature') {
  934. return tripal_analysis_kegg_feature_add($node);
  935. }
  936. return $content;
  937. }
  938. */
  939. function tripal_analysis_kegg_org_report($analysis_id){
  940. $content = tripal_analysis_kegg_full_report($analysis_id);
  941. $opt = array($content);
  942. return drupal_json($opt);
  943. }
  944. /************************************************************************
  945. */
  946. function tripal_analysis_kegg_full_report($analysis_id){
  947. // Test if brite data have been parsed into the database
  948. $sql = "SELECT CVT.name, CVT.cvterm_id
  949. FROM {cvterm} CVT
  950. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  951. WHERE AP.analysis_id = %d
  952. AND CVT.definition LIKE 'KEGG BRITE term: %'
  953. ORDER BY CVT.cvterm_id";
  954. $previous_db = tripal_db_set_active('chado');
  955. $result = db_query($sql, $analysis_id);
  956. tripal_db_set_active($previous_db);
  957. if (db_result($result)) {
  958. $content = tripal_analysis_kegg_brite($analysis_id, $type_id, 0);
  959. } else {
  960. $content = "<i>Note:</i> Analysis result is not available. Please schedule and run the job to parse the kegg output.";
  961. }
  962. return $content;
  963. }
  964. /*******************************************************************************
  965. * Tripal Kegg administrative setting form. This function is called by
  966. * tripal_analysis module which asks for an admin form to show on the page
  967. */
  968. function tripal_analysis_kegg_get_settings() {
  969. // Get an array of node types with internal names as keys
  970. $options = node_get_types('names');
  971. // Add 'chado_feature' to allowed content types for showing kegg results
  972. $allowedoptions ['chado_feature'] = "Show KEGG results on feature pages";
  973. $allowedoptions ['chado_analysis_kegg'] = "Show KEGG BRITE results on the analysis page.";
  974. $allowedoptions ['chado_organism'] = "Show KEGG BRITE results on the organism pages.";
  975. $form['description'] = array(
  976. '#type' => 'item',
  977. '#value' => t("Some chado features were analyzed by KEGG automatic annotation server (KAAS). This option allows user to display the kegg analysis results. Please read user manual for storage and display of kegg files. Check the box to enable the analysis results. Uncheck to disable it."),
  978. '#weight' => 0,
  979. );
  980. $form['tripal_analysis_kegg_setting'] = array(
  981. '#type' => 'checkboxes',
  982. '#options' => $allowedoptions,
  983. '#default_value' => variable_get('tripal_analysis_kegg_setting',
  984. array('chado_feature', 'chado_analysis_kegg')),
  985. );
  986. $settings->form = $form;
  987. $settings->title = "Tripal Kegg";
  988. return $settings;
  989. }
  990. /************************************************************************
  991. */
  992. function tripal_analysis_kegg_organism_results($node) {
  993. $node = node_load($node);
  994. return tripal_analysis_kegg_organism_add($node);
  995. }
  996. /************************************************************************
  997. */
  998. function tripal_analysis_kegg_load_feature_terms($feature) {
  999. $feature_id = $feature->feature_id;
  1000. // Get the KEGG results stored using the term 'kegg_brite_data'
  1001. $select = array(
  1002. 'analysisfeature_id' => array(
  1003. 'feature_id' => $feature_id,
  1004. ),
  1005. 'type_id' => array(
  1006. 'name' => 'kegg_brite_data',
  1007. 'cv_id' => array(
  1008. 'name' => 'tripal'
  1009. ),
  1010. ),
  1011. );
  1012. $afeatureprops = tripal_core_chado_select('analysisfeatureprop',array('*'),$select);
  1013. if(!$afeatureprops){
  1014. return;
  1015. }
  1016. // iterate through all of the KEGG properties for this feature
  1017. $results = array();
  1018. foreach ($afeatureprops as $index => $afeatureprop) {
  1019. // get the analysis feature record
  1020. $analysisfeature_arr = tripal_core_chado_select('analysisfeature',array('analysis_id'),
  1021. array('analysisfeature_id' => $afeatureprop->analysisfeature_id));
  1022. $analysisfeature = $analysisfeature_arr[0];
  1023. // get the analysis record and the analysis_id
  1024. $analysis = tripal_core_generate_chado_var('analysis',
  1025. array('analysis_id' => $analysisfeature->analysis_id));
  1026. $analysis_id = $analysis->analysis_id;
  1027. $results[$analysis_id]['analysis'] = $analysis;
  1028. }
  1029. // now get all the terms for each analysis
  1030. foreach($results as $analysis_id => $arr){
  1031. $select = array(
  1032. 'analysisfeature_id' => array(
  1033. 'analysis_id' => $analysis_id,
  1034. 'feature_id' => $feature_id,
  1035. ),
  1036. 'type_id' => array(
  1037. 'name' => 'kegg_brite_data',
  1038. 'cv_id' => array(
  1039. 'name' => 'tripal',
  1040. ),
  1041. ),
  1042. );
  1043. $terms = tripal_core_chado_select('analysisfeatureprop',array('*'),$select);
  1044. foreach ($terms as $term){
  1045. $results[$analysis_id]['terms'][] = $term->value;
  1046. }
  1047. }
  1048. return $results;
  1049. }
  1050. /************************************************************************
  1051. */
  1052. function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
  1053. $form = array();
  1054. // find analyses that have KEGG terms
  1055. $sql = "
  1056. SELECT *
  1057. FROM {kegg_by_organism} KBO
  1058. WHERE organism_id = %d
  1059. ORDER BY analysis_id DESC
  1060. ";
  1061. $previous_db = tripal_db_set_active('chado');
  1062. $results = db_query($sql,$node->organism->organism_id);
  1063. tripal_db_set_active($previous_db);
  1064. $analyses = array();
  1065. $analyses[''] = '';
  1066. while($analysis = db_fetch_object($results)){
  1067. $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
  1068. }
  1069. global $base_url;
  1070. // we need to set some hidden values for the javascript to use
  1071. $form['theme_dir'] = array(
  1072. '#type' => 'hidden',
  1073. '#value' => drupal_get_path('theme', 'tripal'),
  1074. );
  1075. $form['base_url'] = array(
  1076. '#type' => 'hidden',
  1077. '#value' => $base_url,
  1078. );
  1079. // now generate the select box
  1080. global $base_url;
  1081. $theme_dir = drupal_get_path('theme', 'tripal');
  1082. $form['tripal_analysis_kegg_select'] = array(
  1083. '#title' => t('Select a KEGG report to view'),
  1084. '#description' => t('Any analysis with KEGG results related to this organism are available for viewing. For further information, see the analysis information page.'),
  1085. '#type' => 'select',
  1086. '#options' => $analyses,
  1087. '#attributes' => array (
  1088. 'onchange' => "tripal_analysis_kegg_org_report(this.options[this.selectedIndex].value,'".$base_url. "','".$theme_dir."')"
  1089. ),
  1090. );
  1091. return $form;
  1092. }
  1093. /*******************************************************************************
  1094. * Parsing KEGG HTML results that are stored in analysisfeatureprop for
  1095. * searching
  1096. * */
  1097. function tripal_analysis_kegg_extract_keywords ($analysis_id) {
  1098. print "Extracting keywords...\n";
  1099. // Get all interpro output hits except for records with 'No hits reported', 'parent', 'children'.
  1100. $output_type_id = tripal_get_cvterm_id('kegg_brite_data');
  1101. $sql = "SELECT AFP.analysisfeature_id, AFP.value FROM {analysisfeatureprop} AFP
  1102. INNER JOIN {analysisfeature} AF ON AF.analysisfeature_id = AFP.analysisfeature_id
  1103. WHERE type_id = $output_type_id
  1104. AND AF.analysis_id = $analysis_id";
  1105. $results = chado_query($sql);
  1106. $keyword_type_id = tripal_get_cvterm_id('analysis_kegg_output_keywords');
  1107. // Define what to be extracted in the array
  1108. $search = array (
  1109. "'<[/!]*?[^<>]*?>'si", // replace HTML tags with a space
  1110. "'\n'", // replace newlines with a space
  1111. );
  1112. $replace = array (
  1113. " ",
  1114. " ",
  1115. );
  1116. while ($record = db_fetch_object($results)) {
  1117. $af_id = $record->analysisfeature_id;
  1118. $value = $record->value;
  1119. // Retrive keywords for this analysisfeature_id if there is any
  1120. $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id";
  1121. $keywords = db_result(chado_query($sql));
  1122. // Extract new keywords from the interpro html output
  1123. $text = preg_replace($search, $replace, $value);
  1124. $new_keywords = trim(ereg_replace(' +', ' ', $text)); // remove extra spaces
  1125. // Append the new keywords
  1126. if ($keywords) {
  1127. $new_keywords = "$keywords $new_keywords";
  1128. $sql = "UPDATE {analysisfeatureprop} SET value = '$new_keywords' WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id ";
  1129. } else {
  1130. // Insert the keyword into the analysisfeatureprop table
  1131. $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank) VALUES ($af_id, $keyword_type_id, '$new_keywords', 0)";
  1132. }
  1133. chado_query($sql);
  1134. }
  1135. print "Finished.\n";
  1136. }