tripal_analysis_kegg.module 47 KB

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