tripal_analysis_kegg.module 45 KB

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