tripal_analysis_kegg.module 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?php
  2. /*******************************************************************************
  3. *
  4. ******************************************************************************/
  5. function tripal_analysis_kegg_init(){
  6. // add the tripal_analysis_kegg JS and CSS
  7. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_kegg.js');
  8. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_kegg.css');
  9. // add the jsTree JS and CSS
  10. drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
  11. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
  12. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
  13. }
  14. /*******************************************************************************
  15. * Provide information to drupal about the node types that we're creating
  16. * in this module
  17. */
  18. function tripal_analysis_kegg_node_info() {
  19. $nodes = array();
  20. $nodes['chado_analysis_kegg'] = array(
  21. 'name' => t('Analysis: KEGG'),
  22. 'module' => 'chado_analysis_kegg',
  23. 'description' => t('Results from a KEGG/KAAS analysis'),
  24. 'has_title' => FALSE,
  25. 'title_label' => t('Analysis: KEGG'),
  26. 'has_body' => FALSE,
  27. 'body_label' => t('KEGG Analysis Description'),
  28. 'locked' => TRUE
  29. );
  30. return $nodes;
  31. }
  32. /*******************************************************************************
  33. * Menu items are automatically added for the new node types created
  34. * by this module to the 'Create Content' Navigation menu item. This function
  35. * adds more menu items needed for this module.
  36. */
  37. function tripal_analysis_kegg_menu() {
  38. $items['brite/%'] = array(
  39. 'title' => t('KEGG BRITE'),
  40. 'page callback' => 'tripal_analysis_kegg_brite',
  41. 'page arguments' => array(1, 2),
  42. 'access arguments' => array('access content'),
  43. 'type' => MENU_CALLBACK
  44. );
  45. $items['tripal_analysis_kegg_org_report/%'] = array(
  46. 'path' => 'tripal_analysis_kegg_org_report',
  47. 'title' => t('Analysis KEGG report'),
  48. 'page callback' => 'tripal_analysis_kegg_org_report',
  49. 'page arguments' => array(1),
  50. 'access arguments' => array('access chado_analysis_kegg content'),
  51. 'type' => MENU_CALLBACK
  52. );
  53. return $items;
  54. }
  55. /*******************************************************************************
  56. * Set the permission types that the chado module uses. Essentially we
  57. * want permissionis that protect creation, editing and deleting of chado
  58. * data objects
  59. */
  60. function tripal_analysis_kegg_perm(){
  61. return array(
  62. 'access chado_analysis_kegg content',
  63. 'create chado_analysis_kegg content',
  64. 'delete chado_analysis_kegg content',
  65. 'edit chado_analysis_kegg content',
  66. );
  67. }
  68. /*******************************************************************************
  69. * The following function proves access control for users trying to
  70. * perform actions on data managed by this module
  71. */
  72. function chado_analysis_kegg_access($op, $node, $account){
  73. if ($op == 'create') {
  74. return user_access('create chado_analysis_kegg content', $account);
  75. }
  76. if ($op == 'update') {
  77. if (user_access('edit chado_analysis_kegg content', $account)) {
  78. return TRUE;
  79. }
  80. }
  81. if ($op == 'delete') {
  82. if (user_access('delete chado_analysis_kegg content', $account)) {
  83. return TRUE;
  84. }
  85. }
  86. if ($op == 'view') {
  87. if (user_access('access chado_analysis_kegg content', $account)) {
  88. return TRUE;
  89. }
  90. }
  91. return FALSE;
  92. }
  93. /*******************************************************************************
  94. */
  95. function tripal_analysis_kegg_brite($analysis_id, $type_id, $ajax){
  96. // If not called by ajax
  97. if (!$ajax) {
  98. $content .=
  99. "<div id=\"tripal_kegg_brite_results\" class=\"tripal_kegg_brite-info-box\">
  100. <table>
  101. <tr>
  102. <th>KEGG BRITE</th>
  103. <th id=\"tripal_kegg_brite_header\">Hierarchy:</th>
  104. </tr>
  105. <tr>
  106. <td nowrap valign=\"top\">
  107. ";
  108. // List all BRITE terms on the left
  109. $sql = "SELECT CVT.name, CVT.cvterm_id
  110. FROM {cvterm} CVT
  111. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  112. WHERE AP.analysis_id = %d AND CVT.definition LIKE 'KEGG BRITE term: %'
  113. ORDER BY CVT.cvterm_id";
  114. $previous_db = tripal_db_set_active('chado');
  115. $result = db_query($sql, $analysis_id);
  116. tripal_db_set_active($previous_db);
  117. while ($brite_term = db_fetch_object($result)) {
  118. $url = url("brite/$analysis_id/$brite_term->cvterm_id/1");
  119. $content .= "<li class=\"tripal_kegg_brite_terms\"><a onclick=\"return tripal_update_brite(".
  120. "this,$brite_term->cvterm_id)\" href=\"$url\">
  121. $brite_term->name
  122. </a></li>";
  123. }
  124. // Show the hierarchy tree
  125. $content .="</td>
  126. <td nowrap id=\"tripal_kegg_brite_hierarchy\" valign=\"top\">";
  127. $content .= "<i>Note:</i> Click a BRITE term for its functional hierarchy";
  128. // If called by ajax, generate tree structure
  129. } else {
  130. // Get BRITE term from cvterm table
  131. $previous_db = tripal_db_set_active('chado');
  132. $sql = 'SELECT name FROM {cvterm} WHERE cvterm_id=%d';
  133. $brite_term = db_result(db_query($sql, $type_id));
  134. // Get BRITE hierarchy tree
  135. $sql = "SELECT value
  136. FROM {analysisprop} AP
  137. INNER JOIN CVterm CVT on AP.type_id = CVT.cvterm_id
  138. INNER JOIN CV on CVT.cv_id = CV.cv_id
  139. WHERE CV.name = 'tripal' and CVT.name = '%s'
  140. AND AP.analysis_id = %d";
  141. $result = db_fetch_object(db_query($sql, $brite_term, $analysis_id));
  142. tripal_db_set_active($previous_db);
  143. $content .= "<div class=\"tripal_kegg_brite_tree\" id=\"tripal_kegg_brite_tree_$type_id\">$result->value</div>";
  144. }
  145. if (!$ajax) {
  146. $content .= " </td>
  147. </tr>
  148. </table>
  149. </div>";
  150. }
  151. // since this function provides output for addition into
  152. // an analysis page, as well as an AJAX refresh of content
  153. // within the BRITE hierarchy we need to setup the return
  154. // different depending on the request type
  155. if($ajax){
  156. drupal_json(array('update' => $content,
  157. 'id' => "tripal_kegg_brite_tree_$type_id",
  158. 'brite_term' => "Hierarchy: $brite_term"));
  159. } else {
  160. return $content;
  161. }
  162. }
  163. /*******************************************************************************
  164. * Provide a KEGG Analysis form
  165. */
  166. function chado_analysis_kegg_form ($node){
  167. // add in the default fields
  168. $form = chado_analysis_form($node);
  169. // set the defaults
  170. $kegg = $node->analysis->tripal_analysis_kegg;
  171. $query_re = $kegg->query_re;
  172. $query_type = $kegg->query_type;
  173. $query_uniquename = $kegg->query_uniquename;
  174. $hierfile = $kegg->hierfile;
  175. $moreSettings ['kegg'] = 'KEGG Analysis Settings';
  176. $form['kegg'] = array(
  177. '#title' => t('KEGG Settings'),
  178. '#type' => 'fieldset',
  179. '#description' => t('Specific Settings for KEGG Analysis.'),
  180. '#collapsible' => TRUE,
  181. '#attributes' => array('id' => 'kegg-extra-settings'),
  182. '#weight' => 11
  183. );
  184. $form['kegg']['hierfile'] = array(
  185. '#title' => t('KAAS hier.tar.gz Output File'),
  186. '#type' => 'textfield',
  187. '#description' => t('The full path to the hier.tar.gz file generated by KAAS.
  188. Alternatively, you can input the full path to the directory
  189. that contains decompressed kegg files.'),
  190. '#default_value' => $hierfile,
  191. );
  192. $form['kegg']['query_re'] = array(
  193. '#title' => t('Query Name RE'),
  194. '#type' => 'textfield',
  195. '#description' => t('Enter the regular expression that will extract the '.
  196. 'feature name from the results line in the KEGG heir results. This will be '.
  197. 'the same as the definition line in the query FASTA file used for the analysis. This option is '.
  198. 'is only required when the query does not identically match a feature '.
  199. 'in the database.'),
  200. '#default_value' => $query_re,
  201. );
  202. $form['kegg']['query_type'] = array(
  203. '#title' => t('Query Type'),
  204. '#type' => 'textfield',
  205. '#description' => t('Please enter the Sequence Ontology term that describes '.
  206. 'the query sequences used for KEGG. This is only necessary if two '.
  207. 'or more sequences have the same name.'),
  208. '#default_value' => $query_type,
  209. );
  210. $form['kegg']['query_uniquename'] = array(
  211. '#title' => t('Use Unique Name'),
  212. '#type' => 'checkbox',
  213. '#description' => t('Select this checboxk if the feature name in the KEGG heir file '.
  214. 'matches the uniquename in the database. By default, the feature will '.
  215. 'be mapped to the "name" of the feature.'),
  216. '#default_value' => $query_uniquename,
  217. );
  218. $form['kegg']['keggjob'] = array(
  219. '#type' => 'checkbox',
  220. '#title' => t('Submit a job to parse the kegg output into analysisfeatureprop table'),
  221. '#description' => t('Note: features associated with the KAAS results must '.
  222. 'exist in chado before parsing the file. Otherwise, KEGG '.
  223. 'results that cannot be linked to a feature will be '.
  224. 'discarded.'),
  225. );
  226. return $form;
  227. }
  228. /*******************************************************************************
  229. *
  230. */
  231. function chado_analysis_kegg_insert($node){
  232. // insert the analysis
  233. chado_analysis_insert($node);
  234. // set the type for this analysis
  235. tripal_analysis_insert_property($node->analysis_id,'analysis_type','tripal_analysis_kegg');
  236. // now add in the remaining settings as a single property but separated by bars
  237. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile);
  238. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re);
  239. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type);
  240. tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename);
  241. // Add a job if the user wants to parse the html output
  242. chado_analysis_kegg_submit_job($node);
  243. }
  244. /**
  245. *
  246. */
  247. function chado_analysis_kegg_submit_job($node){
  248. global $user;
  249. if($node->keggjob) {
  250. $job_args[0] = $node->analysis_id;
  251. $job_args[1] = $node->hierfile;
  252. $job_args[2] = base_path();
  253. $job_args[3] = $node->query_re;
  254. $job_args[4] = $node->query_type;
  255. $job_args[5] = $node->query_uniquename;
  256. if (is_readable($node->hierfile)) {
  257. $fname = preg_replace("/.*\/(.*)/", "$1", $node->hierfile);
  258. tripal_add_job("Parse KAAS output: $fname",'tripal_analysis_kegg',
  259. 'tripal_analysis_kegg_parseHierFile', $job_args, $user->uid);
  260. } else {
  261. drupal_set_message("Can not open KAAS hier.tar.gz output file. Job not scheduled.");
  262. }
  263. }
  264. }
  265. /*******************************************************************************
  266. * Delete KEGG anlysis
  267. */
  268. function chado_analysis_kegg_delete($node){
  269. chado_analysis_delete($node);
  270. }
  271. /*******************************************************************************
  272. * Update KEGG analysis
  273. */
  274. function chado_analysis_kegg_update($node){
  275. // insert the analysis
  276. chado_analysis_update($node);
  277. // set the type for this analysis
  278. tripal_analysis_update_property($node->analysis_id,'analysis_type','tripal_analysis_kegg',1);
  279. // now add in the remaining settings as a single property but separated by bars
  280. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile,1);
  281. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re,1);
  282. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type,1);
  283. tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename,1);
  284. // Add a job if the user wants to parse the html output
  285. chado_analysis_kegg_submit_job($node);
  286. }
  287. /*******************************************************************************
  288. * When a node is requested by the user this function is called to allow us
  289. * to add auxiliary data to the node object.
  290. */
  291. function chado_analysis_kegg_load($node){
  292. // load the default set of analysis fields
  293. $additions = chado_analysis_load($node);
  294. // create some variables for easier lookup
  295. $analysis = $additions->analysis;
  296. $analysis_id = $analysis->analysis_id;
  297. // get the heirfile name
  298. $hierfile = tripal_analysis_get_property($analysis_id,'analysis_kegg_settings');
  299. $query_re = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_re');
  300. $query_type = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_type');
  301. $query_uniquename= tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_uniquename');
  302. $analysis->tripal_analysis_kegg->hierfile = $hierfile->value;
  303. $analysis->tripal_analysis_kegg->query_re = $query_re->value;
  304. $analysis->tripal_analysis_kegg->query_type = $query_type->value;
  305. $analysis->tripal_analysis_kegg->query_uniquename= $query_uniquename->value;
  306. return $additions;
  307. }
  308. /**
  309. *
  310. */
  311. function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
  312. // use drupal's default node view:
  313. if (!$teaser) {
  314. $node = node_prepare($node, $teaser);
  315. // When previewing a node submitting form, it shows 'Array' instead of
  316. // correct date format. We need to format the date here
  317. $time = $node->timeexecuted;
  318. if(is_array($time)){
  319. $month = $time['month'];
  320. $day = $time['day'];
  321. $year = $time['year'];
  322. $timestamp = $year.'-'.$month.'-'.$day;
  323. $node->timeexecuted = $timestamp;
  324. }
  325. }
  326. return $node;
  327. }
  328. /********************************************************************************
  329. */
  330. function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path,
  331. $query_re,$query_type,$query_uniquename,$job_id) {
  332. // If user input a file (e.g. hier.tar.gz), decompress it first
  333. if (is_file($hierfile)) {
  334. $data_dir = file_directory_path() . "/tripal/tripal_analysis_kegg";
  335. $stderr = shell_exec("cd $data_dir; tar -zxf $hierfile;");
  336. print "$stderr\n";
  337. $hierdir = $data_dir."/hier";
  338. // Otherwise, treat it as a directory
  339. } else {
  340. $hierdir = $hierfile;
  341. }
  342. $dir_handle = @opendir($hierdir) or die("Unable to open $hierdir");
  343. $total_files = count(glob($hierdir . '/*.*'));
  344. print "There are $total_files keg file(s).\n";
  345. $interval = intval($total_files * 0.01);
  346. $no_file = 0;
  347. while ($file = readdir($dir_handle)) {
  348. if(preg_match("/^.*\.keg/",$file)){
  349. // Update the progress
  350. if ($no_file % $interval == 0) {
  351. $percentage = (int) ($no_file / $total_files * 100);
  352. tripal_job_set_progress($job_id, $percentage);
  353. print $percentage."% ";
  354. }
  355. $no_file ++;
  356. # $type variable will be set in tripal_analysis_kegg_parse_kegg_file()
  357. $content = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
  358. # add the item to the database
  359. if($content){
  360. //------------------------------------------------------
  361. // Insert into analysisprop table
  362. //------------------------------------------------------
  363. // Make sure the same value doesn't exist before inserting into chado
  364. $sql = "SELECT value
  365. FROM {analysisprop}
  366. WHERE analysis_id = %d
  367. AND type_id = (SELECT cvterm_id
  368. FROM {cvterm} CVT
  369. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  370. WHERE CV.name = 'tripal' AND CVT.name = '%s'
  371. )
  372. ";
  373. $previous_db = tripal_db_set_active('chado');
  374. $oldvalue = db_result(db_query($sql, $analysis_id, $type));
  375. tripal_db_set_active($previous_db);
  376. // Insert the content
  377. if ($oldvalue != $content) {
  378. $previous_db = tripal_db_set_active('chado'); // Use chado database
  379. // Get type_id for the BRITE term
  380. $sql = "SELECT cvterm_id
  381. FROM {cvterm} CVT
  382. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  383. WHERE CV.name = 'tripal' AND CVT.name = '%s'";
  384. $brite_cvterm_id = db_result(db_query($sql, $type));
  385. // Replace all single quote as HTML code before insert
  386. $content = preg_replace("/\'/", "&#39;", $content);
  387. // Insert the value
  388. $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value)
  389. VALUES (%d, %d,'$content')";
  390. db_query($sql, $analysis_id, $brite_cvterm_id);
  391. tripal_db_set_active($previous_db); // Use drupal database
  392. }
  393. }
  394. }
  395. }
  396. print "Done.\n";
  397. closedir($dir_handle);
  398. // If user input a file, remove decompressed files after parsing
  399. if (is_file($hierfile)) {
  400. $stderr = shell_exec("rm -r $hierdir;");
  401. print "$stderr\n";
  402. }
  403. return;
  404. }
  405. /*******************************************************************************
  406. * Parse *.keg files.
  407. * Each file has a definition line. BRITE term is extracted * from this line
  408. * and added to chado as a cvterm. Tree structure for this cvterm is then
  409. * generated and saved to analysisfeature and analysisfeatureprop tables.
  410. */
  411. function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename){
  412. $handle = fopen($file,'r');
  413. $depth = array();
  414. $current = '@'; # this is one character below 'A' in the ASCII table
  415. $prev = '@';
  416. $id = 0;
  417. $type_id = 0;
  418. $no_line = 0;
  419. while($line = fgets($handle)){
  420. $no_line ++;
  421. // Find out what kind of file we're looking at.
  422. if(preg_match("/#DEFINITION\s+(.*)\s+.+?$/",$line,$matches)){
  423. // Set type as the matched term in the DEFINITION line and add it as a new cvterm
  424. $type = $matches[1];
  425. // Before inserting, make sure this cvterm doesn't exist
  426. $previous_db = tripal_db_set_active('chado');
  427. $sql = "SELECT cvterm_id
  428. FROM {cvterm} CVT
  429. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  430. WHERE cv.name = 'tripal'
  431. AND CVT.name = '%s'";
  432. $type_id = db_result(db_query($sql, $type));
  433. tripal_db_set_active($previous_db);
  434. if (!$type_id) {
  435. tripal_add_cvterms($type, "KEGG BRITE term: $type");
  436. // Get newly added type_id
  437. $sql = "SELECT cvterm_id
  438. FROM {cvterm} CVT
  439. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  440. WHERE cv.name = 'tripal'
  441. AND CVT.name = '%s'";
  442. $previous_db = tripal_db_set_active('chado');
  443. $type_id = db_result(db_query($sql, $type));
  444. tripal_db_set_active($previous_db);
  445. }
  446. }
  447. // get the depth of the hierarch (e.g. A,B,C or D);
  448. preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
  449. # skip lines that aren't data or are empty
  450. if(!$matches[1] or !$matches[2]){continue;}
  451. $prev = $current;
  452. $current = $matches[1];
  453. for($i = (ord($current) - ord($prev)); $i < 0; $i++){
  454. $content .= "</li></ul>\n";
  455. }
  456. for($i = 0; $i < (ord($current) - ord($prev)); $i++){
  457. $content .= "<ul>\n";
  458. }
  459. // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
  460. $matches[2] = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$matches[2]);
  461. // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
  462. $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
  463. // extract the features that have been mapped to the KEGG IDs
  464. if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$defline)){
  465. // Find cvterm_id for 'kegg_brite_data'
  466. $sql = "SELECT cvterm_id
  467. FROM {cvterm} CVT
  468. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  469. WHERE cv.name = 'tripal'
  470. AND CVT.name = '%s'";
  471. $previous_db = tripal_db_set_active('chado');
  472. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  473. tripal_db_set_active($previous_db);
  474. // get the feature name using the user's regular expression
  475. if ($query_re and preg_match("/$query_re/", $defline[1], $parts)) {
  476. $feature = $parts[1];
  477. }
  478. // If not in above format then pull up to the first space
  479. else {
  480. if (preg_match('/^(.*?)\s.*$/', $defline[1], $parts)) {
  481. $feature = $parts[1];
  482. }
  483. // if no match up to the first space then just use the entire string
  484. else {
  485. $feature = $defline[1];
  486. }
  487. }
  488. // now find the feature in chado
  489. $select = array();
  490. if($query_uniquename){
  491. $select['uniquename'] = $feature;
  492. } else {
  493. $select['name'] = $feature;
  494. }
  495. if($query_type){
  496. $select['type_id'] = array(
  497. 'cv_id' => array(
  498. 'name' => 'sequence'
  499. ),
  500. 'name' => $query_type,
  501. );
  502. }
  503. $feature_arr = tripal_core_chado_select('feature',array('feature_id'),$select);
  504. if(count($feature_arr) > 1){
  505. print "Ambiguous: '$feature' matches more than one feature and is being skipped.\n";
  506. continue;
  507. }
  508. if(count($feature_arr) == 0){
  509. print "Failed: '$feature' cannot find a matching feature in the database.\n";
  510. continue;
  511. }
  512. print "Adding KEGG results for $feature ($feature_id,$analysis_id)\n";
  513. $feature_id = $feature_arr[0]->feature_id;
  514. // get the node ID of the feature if one exists
  515. $sql = "SELECT nid FROM {chado_feture} WHERE feature_id = %d";
  516. $nid = db_result(db_query($sql, $feature_id));
  517. // Get the higest rank for this feature_id in analysisfeatureprop table
  518. $sql = "SELECT MAX(rank) FROM {analysisfeatureprop} AFP ".
  519. "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
  520. "WHERE feature_id=%d ".
  521. "AND analysis_id=%d ".
  522. "AND type_id=%d ";
  523. $previous_db = tripal_db_set_active('chado');
  524. $afp = db_fetch_object(db_query($sql, $feature_id, $analysis_id, $brite_data_type_id));
  525. tripal_db_set_active($prevous_db);
  526. $hi_rank = 0;
  527. if ($afp) {
  528. $hi_rank = $afp->max + 1;
  529. }
  530. //------------------------------------------------------
  531. // Insert into analysisfeature table
  532. //------------------------------------------------------
  533. $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
  534. "VALUES (%d, %d)";
  535. $previous_db = tripal_db_set_active('chado');
  536. db_query ($sql, $feature_id, $analysis_id);
  537. tripal_db_set_active($previous_db);
  538. // Get the newly inserted analysisfeature_id
  539. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
  540. $previous_db = tripal_db_set_active('chado');
  541. $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
  542. tripal_db_set_active($previous_db);
  543. //------------------------------------------------------
  544. // Insert into analysisfeatureprop table
  545. //------------------------------------------------------
  546. // Before inserting, make sure it's not a duplicatefs
  547. $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
  548. $previous_db = tripal_db_set_active('chado');
  549. $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
  550. tripal_db_set_active($previous_db);
  551. $duplicate = 0;
  552. while ($afp_value = db_fetch_object($result)) {
  553. preg_match("/<a.+?>(.+)<\/a>/",$afp_value->value,$old_ids);
  554. preg_match("/<a.+?>(.+)<\/a>/",$mat[2], $new_ids);
  555. if ($old_ids[1] && $old_ids[1] == $new_ids[1]) {
  556. $duplicate = 1;
  557. }
  558. }
  559. if (!$duplicate) {
  560. $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
  561. "VALUES (%d, %d, '%s', %d)";
  562. $previous_db = tripal_db_set_active('chado');
  563. db_query($sql, $analysisfeature_id, $brite_data_type_id, $mat[2], $hi_rank);
  564. tripal_db_set_active($previous_db);
  565. }
  566. // Add link to each matched feature
  567. $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"".url("/node/$nid")."\">"."$1"."</a>"."$2",$matches[2]);
  568. }
  569. $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
  570. $id++;
  571. }
  572. $content .= "</ul>";
  573. fclose($handle);
  574. return $content;
  575. }
  576. /*******************************************************************************
  577. * HOOK: Implementation of hook_nodeapi()
  578. * Display library information for associated features or organisms
  579. * This function also provides contents for indexing
  580. */
  581. function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
  582. switch ($op) {
  583. case 'view':
  584. // add the library to the organism/feature search indexing
  585. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  586. $node->content['tripal_analysis_kegg_search_index'] = array(
  587. '#value' => theme('tripal_analysis_kegg_search_index',$node),
  588. '#weight' => 6,
  589. );
  590. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  591. $node->content['tripal_analysis_kegg_search_result'] = array(
  592. '#value' => theme('tripal_analysis_kegg_search_result',$node),
  593. '#weight' => 6,
  594. );
  595. } else {
  596. switch($node->type){
  597. case 'chado_organism':
  598. // Show KEGG BRITE on an analysis page OR KEGG info box on a feature page
  599. $node->content['tripal_organism_kegg_summary'] = array(
  600. '#value' => theme('tripal_organism_kegg_summary', $node),
  601. '#weight' => 6
  602. );
  603. break;
  604. }
  605. }
  606. break;
  607. }
  608. }
  609. /************************************************************************
  610. * We need to let drupal know about our theme functions and their arguments.
  611. * We create theme functions to allow users of the module to customize the
  612. * look and feel of the output generated in this module
  613. */
  614. function tripal_analysis_kegg_theme () {
  615. return array(
  616. 'tripal_analysis_kegg_search_index' => array (
  617. 'arguments' => array('node'),
  618. ),
  619. 'tripal_analysis_kegg_search_result' => array (
  620. 'arguments' => array('node'),
  621. ),
  622. 'tripal_organism_kegg_summary' => array (
  623. 'arguments' => array('node'=> null),
  624. 'template' => 'tripal_organism_kegg_summary',
  625. ),
  626. 'tripal_analysis_kegg_report' => array (
  627. 'arguments' => array('node'=> null),
  628. 'template' => 'tripal_analysis_kegg_report',
  629. ),
  630. );
  631. }
  632. /**
  633. *
  634. *
  635. * @ingroup tripal_analysis_kegg
  636. */
  637. function tripal_analysis_kegg_preprocess(&$variables){
  638. // if the template file is the default node template file then we want
  639. // to add all of our variables.
  640. if($variables['template_files'][0] == 'node-chado_analysis_kegg'){
  641. $analysis = $variables['node']->analysis;
  642. $report = tripal_analysis_kegg_full_report($analysis->analysis_id);
  643. $analysis->tripal_analysis_kegg->kegg_report = $report;
  644. }
  645. }
  646. /*******************************************************************************
  647. *
  648. */
  649. function tripal_analysis_kegg_preprocess_tripal_organism_kegg_summary(&$variables){
  650. $node = $variables['node'];
  651. $variables['tripal_analysis_kegg']['select_form'] = tripal_analysis_kegg_load_organism_kegg_summary($node);
  652. }
  653. /************************************************************************
  654. */
  655. function theme_tripal_analysis_kegg_search_index($node){
  656. if ($node->type == 'chado_feature') {
  657. // Find cvterm_id for 'kegg_brite_data'
  658. $sql = "SELECT cvterm_id
  659. FROM {cvterm} CVT
  660. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  661. WHERE cv.name = 'tripal'
  662. AND CVT.name = '%s'";
  663. $previous_db = tripal_db_set_active('chado');
  664. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  665. // Get analysis id
  666. $sql = "SELECT analysis_id AS aid
  667. FROM {analysisfeature} AF
  668. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  669. WHERE feature_id = %d
  670. AND AFP.type_id = %d
  671. GROUP BY analysis_id";
  672. $feature = $node->feature;
  673. $feature_id = $feature->feature_id;
  674. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  675. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  676. // Show kegg result ORDER BY time
  677. if ($hasResult) { // If there is any result, show expandable box
  678. $content = "";
  679. while ($ana = db_fetch_object($result)) {
  680. // Show analysis date
  681. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  682. FROM {analysis}
  683. WHERE analysis_id = %d";
  684. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  685. // Find node id for the analysis
  686. tripal_db_set_active($previous_db);
  687. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  688. $ana_url = url("node/".$ana_nid);
  689. $previous_db = tripal_db_set_active('chado');
  690. // Show content
  691. $content .= "$ana_details->name";
  692. // Show Kegg results
  693. $sql = "SELECT AFP.value AS afpvalue
  694. FROM {analysisfeatureprop} AFP
  695. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  696. WHERE AF.analysis_id = %d
  697. AND AF.feature_id = %d
  698. ";
  699. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  700. while ($afp = db_fetch_object($kegg_results)) {
  701. $content .= " $afp->afpvalue";
  702. }
  703. }
  704. }
  705. tripal_db_set_active($previous_db);
  706. return $content;
  707. }
  708. }
  709. /************************************************************************
  710. */
  711. function theme_tripal_analysis_kegg_search_result($node){
  712. $content = theme_tripal_analysis_kegg_node_add($node);
  713. return $content;
  714. }
  715. /************************************************************************
  716. */
  717. function tripal_analysis_kegg_load_organism_kegg_summary($node) {
  718. $organism = $node->organism;
  719. // find analyses that have KEGG terms
  720. $sql = "
  721. SELECT *
  722. FROM {kegg_by_organism} KBO
  723. WHERE organism_id = %d
  724. ORDER BY analysis_id DESC
  725. ";
  726. $previous_db = tripal_db_set_active('chado');
  727. $results = db_fetch_object(db_query($sql,$organism->organism_id));
  728. tripal_db_set_active($previous_db);
  729. $has_results = 0;
  730. if($results){
  731. $has_results = 1;
  732. }
  733. return array (
  734. 'has_results' => $has_results,
  735. 'form' => drupal_get_form('tripal_analysis_kegg_select_form',$node),
  736. );
  737. }
  738. /************************************************************************
  739. // Show Kegg additional information on a KEGG Analysis page
  740. if ($node->type == 'chado_analysis_kegg') {
  741. return tripal_analysis_kegg_full_report($node->analysis_id);
  742. }
  743. // Show Kegg-info-box on a Feature page
  744. else if ($node->type == 'chado_feature') {
  745. return tripal_analysis_kegg_feature_add($node);
  746. }
  747. return $content;
  748. }
  749. */
  750. function tripal_analysis_kegg_org_report($analysis_id){
  751. $content = tripal_analysis_kegg_full_report($analysis_id);
  752. $opt = array($content);
  753. return drupal_json($opt);
  754. }
  755. /************************************************************************
  756. */
  757. function tripal_analysis_kegg_full_report($analysis_id){
  758. // Test if brite data have been parsed into the database
  759. $sql = "SELECT CVT.name, CVT.cvterm_id
  760. FROM {cvterm} CVT
  761. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  762. WHERE AP.analysis_id = %d
  763. AND CVT.definition LIKE 'KEGG BRITE term: %'
  764. ORDER BY CVT.cvterm_id";
  765. $previous_db = tripal_db_set_active('chado');
  766. $result = db_query($sql, $analysis_id);
  767. tripal_db_set_active($previous_db);
  768. if (db_result($result)) {
  769. $content = tripal_analysis_kegg_brite($analysis_id, $type_id, 0);
  770. } else {
  771. $content = "<i>Note:</i> Analysis result is not available. Please schedule and run the job to parse the kegg output.";
  772. }
  773. return $content;
  774. }
  775. /*******************************************************************************
  776. * Tripal Kegg administrative setting form. This function is called by
  777. * tripal_analysis module which asks for an admin form to show on the page
  778. */
  779. function tripal_analysis_kegg_get_settings() {
  780. // Get an array of node types with internal names as keys
  781. $options = node_get_types('names');
  782. // Add 'chado_feature' to allowed content types for showing kegg results
  783. $allowedoptions ['chado_feature'] = "Show KEGG results on feature pages";
  784. $allowedoptions ['chado_analysis_kegg'] = "Show KEGG BRITE results on the analysis page.";
  785. $allowedoptions ['chado_organism'] = "Show KEGG BRITE results on the organism pages.";
  786. $form['description'] = array(
  787. '#type' => 'item',
  788. '#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."),
  789. '#weight' => 0,
  790. );
  791. $form['tripal_analysis_kegg_setting'] = array(
  792. '#type' => 'checkboxes',
  793. '#options' => $allowedoptions,
  794. '#default_value' => variable_get('tripal_analysis_kegg_setting',
  795. array('chado_feature', 'chado_analysis_kegg')),
  796. );
  797. $settings->form = $form;
  798. $settings->title = "Tripal Kegg";
  799. return $settings;
  800. }
  801. /************************************************************************
  802. */
  803. function tripal_analysis_kegg_organism_results($node) {
  804. $node = node_load($node);
  805. return tripal_analysis_kegg_organism_add($node);
  806. }
  807. /************************************************************************
  808. */
  809. function tripal_analysis_kegg_feature_add($node) {
  810. // Find cvterm_id for 'kegg_brite_data'
  811. $sql = "SELECT cvterm_id
  812. FROM {cvterm} CVT
  813. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  814. WHERE cv.name = 'tripal'
  815. AND CVT.name = '%s'";
  816. $previous_db = tripal_db_set_active('chado');
  817. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  818. // Get analysis id
  819. $sql = "SELECT analysis_id AS aid
  820. FROM {analysisfeature} AF
  821. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  822. WHERE feature_id = %d
  823. AND AFP.type_id = %d
  824. GROUP BY analysis_id";
  825. $feature = $node->feature;
  826. $feature_id = $feature->feature_id;
  827. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  828. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  829. // Show kegg result ORDER BY time
  830. if ($hasResult) { // If there is any result, show expandable box
  831. $content .= "<div id=\"tripal_kegg-hits\" class=\"tripal_kegg-info-box\">
  832. <table class=\"tripal_kegg_results_table\">
  833. <tr><td>";
  834. while ($ana = db_fetch_object($result)) {
  835. // Show analysis date
  836. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  837. FROM {analysis}
  838. WHERE analysis_id = %d";
  839. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  840. // Find node id for the analysis
  841. tripal_db_set_active($previous_db);
  842. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  843. $ana_url = url("node/".$ana_nid);
  844. $previous_db = tripal_db_set_active('chado');
  845. // Show content
  846. $content .= "<strong>Analysis Date:</strong> $ana_details->time
  847. (<a href=$ana_url>$ana_details->name</a>)<br>";
  848. // Show Kegg results
  849. $sql = "SELECT AFP.value AS afpvalue
  850. FROM {analysisfeatureprop} AFP
  851. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  852. WHERE AF.analysis_id = %d
  853. AND AF.feature_id = %d
  854. ";
  855. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  856. while ($afp = db_fetch_object($kegg_results)) {
  857. $content .= "$afp->afpvalue<br>";
  858. }
  859. $content .= "<br>";
  860. }
  861. $content .= '</td></tr></table></div>';
  862. }
  863. tripal_db_set_active($previous_db);
  864. return $content;
  865. }
  866. /************************************************************************
  867. */
  868. function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
  869. $form = array();
  870. // find analyses that have KEGG terms
  871. $sql = "
  872. SELECT *
  873. FROM {kegg_by_organism} KBO
  874. WHERE organism_id = %d
  875. ORDER BY analysis_id DESC
  876. ";
  877. $previous_db = tripal_db_set_active('chado');
  878. $results = db_query($sql,$node->organism->organism_id);
  879. tripal_db_set_active($previous_db);
  880. $analyses = array();
  881. $analyses[''] = '';
  882. while($analysis = db_fetch_object($results)){
  883. $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
  884. }
  885. # create the select box
  886. $form['tripal_analysis_kegg_select'] = array(
  887. '#title' => t('Select a KEGG report to view'),
  888. '#description' => t('Any analysis with KEGG results related to this organism are available for viewing. For further information, see the analysis information page.'),
  889. '#type' => 'select',
  890. '#options' => $analyses,
  891. '#attributes' => array (
  892. 'onchange' => 'tripal_analysis_kegg_org_report(this.options[this.selectedIndex].value)'
  893. ),
  894. );
  895. return $form;
  896. }