tripal_analysis_kegg.module 47 KB

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