tripal_analysis_kegg.module 48 KB

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