tripal_analysis_kegg.module 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. if (is_readable($node->hierfile)) {
  382. $fname = preg_replace("/.*\/(.*)/", "$1", $node->hierfile);
  383. tripal_add_job("Parse KAAS output: $fname",'tripal_analysis_kegg',
  384. 'tripal_analysis_kegg_parseHierFile', $job_args, $user->uid);
  385. } else {
  386. drupal_set_message("Can not open KAAS hier.tar.gz output file. Job not scheduled.");
  387. }
  388. }
  389. }
  390. // Make sure the entry for this analysis doesn't already exist in the
  391. // chado_analysis table if it doesn't exist then we want to add it.
  392. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  393. "WHERE analysis_id = %d";
  394. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  395. if(!$node_check){
  396. // next add the item to the drupal table
  397. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  398. "VALUES (%d, %d, %d)";
  399. db_query($sql,$node->nid,$node->vid,$analysis_id);
  400. // Create a title for the analysis node using the unique keys so when the
  401. // node is saved, it will have a title
  402. $record = new stdClass();
  403. // If the analysis has a name, use it as the node title. If not, construct
  404. // the title using program, programversion, and sourcename
  405. if ($node->analysisname) {
  406. $record->title = $node->analysisname;
  407. } else {
  408. //Construct node title as "program (version)
  409. $record->title = "$node->program ($node->programversion)";
  410. }
  411. $record->nid = $node->nid;
  412. drupal_write_record('node',$record,'nid');
  413. drupal_write_record('node_revisions',$record,'nid');
  414. }
  415. }
  416. /*******************************************************************************
  417. * Delete KEGG anlysis
  418. */
  419. function chado_analysis_kegg_delete($node){
  420. // Before removing, get analysis_id so we can remove it from chado database
  421. // later
  422. $sql_drupal = "SELECT analysis_id ".
  423. "FROM {chado_analysis} ".
  424. "WHERE nid = %d ".
  425. "AND vid = %d";
  426. $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  427. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  428. $sql_del = "DELETE FROM {chado_analysis} ".
  429. "WHERE nid = %d ".
  430. "AND vid = %d";
  431. db_query($sql_del, $node->nid, $node->vid);
  432. $sql_del = "DELETE FROM {node} ".
  433. "WHERE nid = %d ".
  434. "AND vid = %d";
  435. db_query($sql_del, $node->nid, $node->vid);
  436. $sql_del = "DELETE FROM {node_revisions} ".
  437. "WHERE nid = %d ".
  438. "AND vid = %d";
  439. db_query($sql_del, $node->nid, $node->vid);
  440. //Remove from analysisfeatureprop, analysisfeature, analysis, and analysisprop tables
  441. $previous_db = db_set_active('chado');
  442. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id=%d";
  443. $results = db_query($sql, $analysis_id);
  444. while ($af = db_fetch_object($results)) {
  445. db_query("DELETE FROM {analysisfeatureprop} WHERE analysisfeature_id = %d", $af->analysisfeature_id);
  446. }
  447. db_query("DELETE FROM {analysisfeature} WHERE analysis_id = %d", $analysis_id);
  448. db_query("DELETE FROM {analysisprop} WHERE analysis_id = %d", $analysis_id);
  449. db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  450. db_set_active($previous_db);
  451. }
  452. /*******************************************************************************
  453. * Update KEGG analysis
  454. */
  455. function chado_analysis_kegg_update($node){
  456. global $user;
  457. if($node->revision){
  458. // TODO -- decide what to do about revisions
  459. } else {
  460. // Create a timestamp so we can insert it into the chado database
  461. $time = $node->timeexecuted;
  462. $month = $time['month'];
  463. $day = $time['day'];
  464. $year = $time['year'];
  465. $timestamp = $month.'/'.$day.'/'.$year;
  466. // get the analysis_id for this node:
  467. $sql = "SELECT analysis_id ".
  468. "FROM {chado_analysis} ".
  469. "WHERE vid = %d";
  470. $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
  471. $sql = "UPDATE {analysis} ".
  472. "SET name = '%s', ".
  473. " description = '%s', ".
  474. " program = '%s', ".
  475. " programversion = '%s', ".
  476. " algorithm = '%s', ".
  477. " sourcename = '%s', ".
  478. " sourceversion = '%s', ".
  479. " sourceuri = '%s', ".
  480. " timeexecuted = '%s' ".
  481. "WHERE analysis_id = %d ";
  482. $previous_db = db_set_active('chado'); // use chado database
  483. db_query($sql, $node->analysisname, $node->description, $node->program,
  484. $node->programversion,$node->algorithm,$node->sourcename,
  485. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  486. // Get cvterm_id for 'analysis_kegg_settings'
  487. $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
  488. "INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
  489. "WHERE CVT.name = 'analysis_kegg_settings' ".
  490. "AND CV.name = 'tripal'";
  491. $type_id = db_result(db_query($sql));
  492. $sql = "UPDATE {analysisprop} ".
  493. "SET value = '%s' ".
  494. "WHERE analysis_id = %d AND type_id = %d";
  495. $keggsettings = $node->hierfile;
  496. db_query($sql, $keggsettings, $analysis_id, $type_id);
  497. db_set_active($previous_db); // switch back to drupal database
  498. // Add a job if the user wants to parse the html output
  499. if($node->keggjob) {
  500. $job_args[0] = $analysis_id;
  501. $job_args[1] = $node->hierfile;
  502. if (is_readable($node->hierfile)) {
  503. $fname = preg_replace("/.*\/(.*)/", "$1", $node->hierfile);
  504. tripal_add_job("Parse KAAS output: $fname",'tripal_analysis_kegg',
  505. 'tripal_analysis_kegg_parseHierFile', $job_args, $user->uid);
  506. } else {
  507. drupal_set_message("Can not open KAAS hier.tar.gz output file. Job not scheduled.");
  508. }
  509. }
  510. // Create a title for the analysis node using the unique keys so when the
  511. // node is saved, it will have a title
  512. $record = new stdClass();
  513. // If the analysis has a name, use it as the node title. If not, construct
  514. // the title using program, programversion, and sourcename
  515. if ($node->analysisname) {
  516. $record->title = $node->analysisname;
  517. } else {
  518. //Construct node title as "program (version)
  519. $record->title = "$node->program ($node->programversion)";
  520. }
  521. $record->nid = $node->nid;
  522. drupal_write_record('node',$record,'nid');
  523. drupal_write_record('node_revisions',$record,'nid');
  524. }
  525. }
  526. /*******************************************************************************
  527. * When a node is requested by the user this function is called to allow us
  528. * to add auxiliary data to the node object.
  529. */
  530. function chado_analysis_kegg_load($node){
  531. // get the analysis_id for this node:
  532. $sql = "SELECT analysis_id FROM {chado_analysis} WHERE vid = %d";
  533. $ana_node = db_fetch_object(db_query($sql, $node->vid));
  534. $additions = new stdClass();
  535. if ($ana_node) {
  536. // get analysis information
  537. $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
  538. " programversion, algorithm, sourcename, sourceversion, ".
  539. " sourceuri, timeexecuted ".
  540. "FROM {Analysis} ".
  541. "WHERE Analysis_id = $ana_node->analysis_id";
  542. $previous_db = db_set_active('chado'); // use chado database
  543. $additions = db_fetch_object(db_query($sql));
  544. // get cvterm_id for 'analysis_kegg_settings'
  545. $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
  546. "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
  547. "WHERE CVT.name = 'analysis_kegg_settings' ".
  548. "AND CV.name = 'tripal'";
  549. $type_id = db_result(db_query($sql));
  550. // get analysisprop information
  551. $sql = "SELECT value FROM {analysisprop} ".
  552. "WHERE analysis_id = %d ".
  553. "AND type_id = %d";
  554. $analysisprop = db_result(db_query($sql, $ana_node->analysis_id, $type_id));
  555. $additions->hierfile = $analysisprop;
  556. db_set_active($previous_db); // now use drupal database
  557. }
  558. // If the analysis has a name, use it as the node title. If not, construct
  559. // the title using program programversion, and sourcename
  560. if ($additions->analysisname) {
  561. $additions->title = $additions->analysisname;
  562. } else {
  563. // Construct node title as "program version (source)
  564. $additions->title = "$additions->program ($additions->programversion)";
  565. }
  566. return $additions;
  567. }
  568. /*******************************************************************************
  569. * This function customizes the view of the chado_analysis node. It allows
  570. * us to generate the markup.
  571. */
  572. function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
  573. // use drupal's default node view:
  574. if (!$teaser) {
  575. $node = node_prepare($node, $teaser);
  576. // When previewing a node submitting form, it shows 'Array' instead of
  577. // correct date format. We need to format the date here
  578. $time = $node->timeexecuted;
  579. if(is_array($time)){
  580. $month = $time['month'];
  581. $day = $time['day'];
  582. $year = $time['year'];
  583. $timestamp = $year.'-'.$month.'-'.$day;
  584. $node->timeexecuted = $timestamp;
  585. }
  586. }
  587. return $node;
  588. }
  589. /********************************************************************************
  590. */
  591. function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $job_id) {
  592. // If user input a file (e.g. hier.tar.gz), decompress it first
  593. if (is_file($hierfile)) {
  594. $data_dir = file_directory_path() . "/tripal/tripal_analysis_kegg";
  595. $stderr = shell_exec("cd $data_dir; tar -zxf $hierfile;");
  596. print "$stderr\n";
  597. $hierdir = $data_dir."/hier";
  598. // Otherwise, treat it as a directory
  599. } else {
  600. $hierdir = $hierfile;
  601. }
  602. $dir_handle = @opendir($hierdir) or die("Unable to open $hierdir");
  603. $total_files = count(glob($hierdir . '/*.*'));
  604. $interval = intval($total_files * 0.01);
  605. $no_file = 0;
  606. while ($file = readdir($dir_handle)) {
  607. if(preg_match("/^.*\.keg/",$file)){
  608. // Update the progress
  609. if ($no_file % $interval == 0) {
  610. $percentage = (int) ($no_file / $total_files * 100);
  611. tripal_job_set_progress($job_id, $percentage);
  612. print $percentage."% ";
  613. }
  614. $no_file ++;
  615. # print "Parsing $hierdir/$file\n";
  616. $content = tripal_analysis_kegg_parse_keg_file("$hierdir/$file",$type,$analysis_id);
  617. # add the item to the database
  618. if($content){
  619. //------------------------------------------------------
  620. // Insert into analysisprop table
  621. //------------------------------------------------------
  622. // Make sure the same value doesn't exist before inserting into chado
  623. $sql = "SELECT value
  624. FROM {analysisprop} WHERE analysis_id = %d
  625. AND type_id = (SELECT cvterm_id
  626. FROM {cvterm} CVT
  627. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  628. WHERE CV.name = 'tripal' AND CVT.name = '%s'
  629. )
  630. ";
  631. $previous_db = db_set_active('chado');
  632. $oldvalue = db_result(db_query($sql, $analysis_id, $type));
  633. db_set_active($previous_db);
  634. if ($oldvalue != $content) {
  635. $sql = "
  636. INSERT INTO {analysisprop} (analysis_id, type_id, value) VALUES
  637. (%d,
  638. (SELECT cvterm_id
  639. FROM {cvterm} CVT
  640. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  641. WHERE CV.name = 'tripal' AND CVT.name = '%s'),
  642. '%s')";
  643. $previous_db = db_set_active('chado');
  644. db_query($sql,$analysis_id,$type,$content);
  645. db_set_active($previous_db);
  646. }
  647. }
  648. }
  649. }
  650. print "Done.\n";
  651. closedir($dir_handle);
  652. // If user input a file, remove decompressed files after parsing
  653. if (is_file($hierfile)) {
  654. $stderr = shell_exec("rm -r $hierdir;");
  655. print "$stderr\n";
  656. }
  657. return;
  658. }
  659. /*******************************************************************************
  660. *
  661. */
  662. function tripal_analysis_kegg_parse_keg_file ($file,&$type,$analysis_id){
  663. $handle = fopen($file,'r');
  664. $depth = array();
  665. $current = '@'; # this is one character below 'A' in the ASCII table
  666. $prev = '@';
  667. $id = 0;
  668. $type_id = 0;
  669. $no_line = 0;
  670. $prefix = variable_get('chado_feature_accession_prefix','ID');
  671. while($line = fgets($handle)){
  672. $no_line ++;
  673. // Find out what kind of file we're looking at.
  674. if(preg_match("/#DEFINITION\s+(.*)\s+.+?$/",$line,$matches)){
  675. // Set type as the matched term in the DEFINITION line and add it as a new cvterm
  676. $type = $matches[1];
  677. // Before inserting, make sure this cvterm doesn't exist
  678. $previous_db = db_set_active('chado');
  679. $sql = "SELECT cvterm_id
  680. FROM {cvterm} CVT
  681. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  682. WHERE cv.name = 'tripal'
  683. AND CVT.name = '%s'";
  684. $type_id = db_result(db_query($sql, $type));
  685. db_set_active($previous_db);
  686. if (!$type_id) {
  687. tripal_add_cvterms($type, "KEGG BRITE term: $type");
  688. // Get newly added type_id
  689. $sql = "SELECT cvterm_id
  690. FROM {cvterm} CVT
  691. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  692. WHERE cv.name = 'tripal'
  693. AND CVT.name = '%s'";
  694. $previous_db = db_set_active('chado');
  695. $type_id = db_result(db_query($sql, $type));
  696. db_set_active($previous_db);
  697. }
  698. }
  699. // get the depth of the hierarch (e.g. A,B,C or D);
  700. preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
  701. # skip lines that aren't data or are empty
  702. if(!$matches[1] or !$matches[2]){continue;}
  703. $prev = $current;
  704. $current = $matches[1];
  705. for($i = (ord($current) - ord($prev)); $i < 0; $i++){
  706. $content .= "</li></ul>\n";
  707. }
  708. for($i = 0; $i < (ord($current) - ord($prev)); $i++){
  709. $content .= "<ul>\n";
  710. }
  711. // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
  712. $matches[2] = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$matches[2]);
  713. // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
  714. $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
  715. // extract the features that have been mapped to the various KEGG IDs
  716. if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$mat)){
  717. // Find cvterm_id for 'kegg_brite_data'
  718. $sql = "SELECT cvterm_id
  719. FROM {cvterm} CVT
  720. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  721. WHERE cv.name = 'tripal'
  722. AND CVT.name = '%s'";
  723. $previous_db = db_set_active('chado');
  724. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  725. db_set_active($previous_db);
  726. $uniquename = $mat[1];
  727. // Find feature_id using uniquename
  728. $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
  729. $previous_db = db_set_active('chado');
  730. $feature_id = db_result(db_query($sql, $uniquename)); // retrive first returned feature_id (assuming uniquename is unique)
  731. db_set_active($previous_db);
  732. // Get the higest rank for this feature_id in analysisfeatureprop table
  733. $sql = "SELECT MAX(rank) FROM {analysisfeatureprop} AFP ".
  734. "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
  735. "WHERE feature_id=%d ".
  736. "AND analysis_id=%d ".
  737. "AND type_id=%d ";
  738. $previous_db = db_set_active('chado');
  739. $afp = db_fetch_object(db_query($sql, $feature_id, $analysis_id, $brite_data_type_id));
  740. db_set_active($prevous_db);
  741. $hi_rank = 0;
  742. if ($afp) {
  743. $hi_rank = $afp->max + 1;
  744. }
  745. //------------------------------------------------------
  746. // Insert into analysisfeature table
  747. //------------------------------------------------------
  748. $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
  749. "VALUES (%d, %d)";
  750. $previous_db = db_set_active('chado');
  751. db_query ($sql, $feature_id, $analysis_id);
  752. db_set_active($previous_db);
  753. // Get the newly inserted analysisfeature_id
  754. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
  755. $previous_db = db_set_active('chado');
  756. $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
  757. db_set_active($previous_db);
  758. //------------------------------------------------------
  759. // Insert into analysisfeatureprop table
  760. //------------------------------------------------------
  761. // Before inserting, make sure it's not a duplicate
  762. $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
  763. $previous_db = db_set_active('chado');
  764. $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
  765. db_set_active($previous_db);
  766. $duplicate = 0;
  767. while ($afp_value = db_fetch_object($result)) {
  768. preg_match("/<a.+?>(.+)<\/a>/",$afp_value->value,$old_ids);
  769. preg_match("/<a.+?>(.+)<\/a>/",$mat[2], $new_ids);
  770. if ($old_ids[1] && $old_ids[1] == $new_ids[1]) {
  771. $duplicate = 1;
  772. }
  773. }
  774. if (!$duplicate) {
  775. $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
  776. "VALUES (%d, %d, '%s', %d)";
  777. $previous_db = db_set_active('chado');
  778. db_query($sql, $analysisfeature_id, $brite_data_type_id, $mat[2], $hi_rank);
  779. db_set_active($previous_db);
  780. }
  781. // Add link to each matched feature
  782. $feature_url = url("$prefix$feature_id");
  783. $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"/$prefix$feature_id\">"."$1"."</a>"."$2",$matches[2]);
  784. }
  785. $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
  786. $id++;
  787. }
  788. $content .= "</ul>";
  789. fclose($handle);
  790. return $content;
  791. }
  792. /*******************************************************************************
  793. * HOOK: Implementation of hook_nodeapi()
  794. * Display library information for associated features or organisms
  795. * This function also provides contents for indexing
  796. */
  797. function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
  798. switch ($op) {
  799. // Note that this function only adds library view to an organism/feature
  800. // node. The view of a library node is controled by the theme *.tpl file
  801. case 'view':
  802. // Set the node types for showing library information
  803. $types_to_show = variable_get('tripal_analysis_kegg_setting',
  804. array('chado_feature','chado_analysis_kegg','chado_organism'));
  805. // Abort if this node is not one of the types we should show.
  806. if (!in_array($node->type, $types_to_show, TRUE)) {
  807. // Turn the menu off if it's on
  808. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  809. if (strcmp($box_status,"menu_on")==0 && $node->type =='chado_organism'){
  810. variable_set("tripal_analysis_kegg-box-results","menu_off");
  811. }
  812. break;
  813. }
  814. // Add library to the content item if it's not a teaser
  815. if (!$teaser) {
  816. // add the library to the organism/feature search indexing
  817. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  818. $node->content['tripal_analysis_kegg_search_index'] = array(
  819. '#value' => theme('tripal_analysis_kegg_search_index',$node),
  820. '#weight' => 6,
  821. );
  822. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  823. $node->content['tripal_analysis_kegg_search_result'] = array(
  824. '#value' => theme('tripal_analysis_kegg_search_result',$node),
  825. '#weight' => 6,
  826. );
  827. } else {
  828. // Show KEGG BRITE on an analysis page OR KEGG info box on a feature page
  829. $node->content['tripal_analysis_kegg_node_add'] = array(
  830. '#value' => theme('tripal_analysis_kegg_node_add', $node),
  831. '#weight' => 6
  832. );
  833. }
  834. }
  835. }
  836. }
  837. /************************************************************************
  838. * We need to let drupal know about our theme functions and their arguments.
  839. * We create theme functions to allow users of the module to customize the
  840. * look and feel of the output generated in this module
  841. */
  842. function tripal_analysis_kegg_theme () {
  843. return array(
  844. 'tripal_analysis_kegg_search_index' => array (
  845. 'arguments' => array('node'),
  846. ),
  847. 'tripal_analysis_kegg_search_result' => array (
  848. 'arguments' => array('node'),
  849. ),
  850. 'tripal_analysis_kegg_node_add' => array (
  851. 'arguments' => array('node'),
  852. )
  853. );
  854. }
  855. /************************************************************************
  856. */
  857. function theme_tripal_analysis_kegg_search_index($node){
  858. if ($node->type == 'chado_feature') {
  859. // Find cvterm_id for 'kegg_brite_data'
  860. $sql = "SELECT cvterm_id
  861. FROM {cvterm} CVT
  862. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  863. WHERE cv.name = 'tripal'
  864. AND CVT.name = '%s'";
  865. $previous_db = db_set_active('chado');
  866. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  867. // Get analysis id
  868. $sql = "SELECT analysis_id AS aid
  869. FROM {analysisfeature} AF
  870. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  871. WHERE feature_id = %d
  872. AND AFP.type_id = %d
  873. GROUP BY analysis_id";
  874. $feature = $node->feature;
  875. $feature_id = $feature->feature_id;
  876. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  877. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  878. // Show kegg result ORDER BY time
  879. if ($hasResult) { // If there is any result, show expandable box
  880. $content = "";
  881. while ($ana = db_fetch_object($result)) {
  882. // Show analysis date
  883. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  884. FROM {analysis}
  885. WHERE analysis_id = %d";
  886. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  887. // Find node id for the analysis
  888. db_set_active($previous_db);
  889. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  890. $ana_url = url("node/".$ana_nid);
  891. $previous_db = db_set_active('chado');
  892. // Show content
  893. $content .= "$ana_details->name";
  894. // Show Kegg results
  895. $sql = "SELECT AFP.value AS afpvalue
  896. FROM {analysisfeatureprop} AFP
  897. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  898. WHERE AF.analysis_id = %d
  899. AND AF.feature_id = %d
  900. ";
  901. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  902. while ($afp = db_fetch_object($kegg_results)) {
  903. $content .= " $afp->afpvalue";
  904. }
  905. }
  906. }
  907. db_set_active($previous_db);
  908. return $content;
  909. }
  910. }
  911. /************************************************************************
  912. */
  913. function theme_tripal_analysis_kegg_search_result($node){
  914. $content = theme_tripal_analysis_kegg_node_add($node);
  915. return $content;
  916. }
  917. /************************************************************************
  918. */
  919. function theme_tripal_analysis_kegg_node_add($node) {
  920. // Show Kegg additional information on a KEGG Analysis page
  921. if ($node->type == 'chado_analysis_kegg') {
  922. return tripal_analysis_kegg_full_report($node->analysis_id);
  923. }
  924. // Show Kegg-info-box on a Feature page
  925. else if ($node->type == 'chado_feature') {
  926. return tripal_analysis_kegg_feature_add($node);
  927. }
  928. // Show Kegg full reports on the organism page
  929. else if ($node->type == 'chado_organism') {
  930. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  931. if (strcmp($box_status,"menu_off")==0){
  932. $content .= tripal_analysis_kegg_organism_add($node);
  933. }
  934. }
  935. return $content;
  936. }
  937. /************************************************************************
  938. */
  939. function tripal_analysis_kegg_org_report($analysis_id){
  940. $content = tripal_analysis_kegg_full_report($analysis_id);
  941. $opt = array($content);
  942. return drupal_json($opt);
  943. }
  944. /************************************************************************
  945. */
  946. function tripal_analysis_kegg_full_report($analysis_id){
  947. // Test if brite data have been parsed into the database
  948. $sql = "SELECT CVT.name, CVT.cvterm_id
  949. FROM {cvterm} CVT
  950. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  951. WHERE AP.analysis_id = %d
  952. AND CVT.definition LIKE 'KEGG BRITE term: %'
  953. ORDER BY CVT.cvterm_id";
  954. $previous_db = db_set_active('chado');
  955. $result = db_query($sql, $analysis_id);
  956. db_set_active($previous_db);
  957. if (db_result($result)) {
  958. $content = tripal_analysis_kegg_brite($analysis_id, $type_id, 0);
  959. } else {
  960. $content = "<i>Note:</i> Analysis result is not available. Please schedule and run the job to parse the kegg output.";
  961. }
  962. return $content;
  963. }
  964. /*******************************************************************************
  965. * Tripal Kegg administrative setting form. This function is called by
  966. * tripal_analysis module which asks for an admin form to show on the page
  967. */
  968. function tripal_analysis_kegg_get_settings() {
  969. // Get an array of node types with internal names as keys
  970. $options = node_get_types('names');
  971. // Add 'chado_feature' to allowed content types for showing kegg results
  972. $allowedoptions ['chado_feature'] = "Show KEGG results on feature pages";
  973. $allowedoptions ['chado_analysis_kegg'] = "Show KEGG BRITE results on the analysis page.";
  974. $allowedoptions ['chado_organism'] = "Show KEGG BRITE results on the organism pages.";
  975. $form['description'] = array(
  976. '#type' => 'item',
  977. '#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."),
  978. '#weight' => 0,
  979. );
  980. $form['tripal_analysis_kegg_setting'] = array(
  981. '#type' => 'checkboxes',
  982. '#options' => $allowedoptions,
  983. '#default_value' => variable_get('tripal_analysis_kegg_setting',
  984. array('chado_feature', 'chado_analysis_kegg')),
  985. );
  986. $settings->form = $form;
  987. $settings->title = "Tripal Kegg";
  988. return $settings;
  989. }
  990. /************************************************************************
  991. */
  992. function tripal_analysis_kegg_organism_results($node) {
  993. $node = node_load($node);
  994. return tripal_analysis_kegg_organism_add($node);
  995. }
  996. /************************************************************************
  997. */
  998. function tripal_analysis_kegg_feature_add($node) {
  999. // Find cvterm_id for 'kegg_brite_data'
  1000. $sql = "SELECT cvterm_id
  1001. FROM {cvterm} CVT
  1002. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  1003. WHERE cv.name = 'tripal'
  1004. AND CVT.name = '%s'";
  1005. $previous_db = db_set_active('chado');
  1006. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  1007. // Get analysis id
  1008. $sql = "SELECT analysis_id AS aid
  1009. FROM {analysisfeature} AF
  1010. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  1011. WHERE feature_id = %d
  1012. AND AFP.type_id = %d
  1013. GROUP BY analysis_id";
  1014. $feature = $node->feature;
  1015. $feature_id = $feature->feature_id;
  1016. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  1017. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  1018. // Show kegg result ORDER BY time
  1019. if ($hasResult) { // If there is any result, show expandable box
  1020. $content .= "<div id=\"tripal_kegg-hits\" class=\"tripal_kegg-info-box\">
  1021. <div class=\"tripal_expandableBox\">
  1022. <h3>KEGG Analysis</h3>
  1023. </div>
  1024. <div class=\"tripal_expandableBoxContent\">
  1025. <span>
  1026. <table class=\"tripal_kegg_results_table\">
  1027. <tr><td>";
  1028. while ($ana = db_fetch_object($result)) {
  1029. // Show analysis date
  1030. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  1031. FROM {analysis}
  1032. WHERE analysis_id = %d";
  1033. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  1034. // Find node id for the analysis
  1035. db_set_active($previous_db);
  1036. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  1037. $ana_url = url("node/".$ana_nid);
  1038. $previous_db = db_set_active('chado');
  1039. // Show content
  1040. $content .= "<strong>Analysis Date:</strong> $ana_details->time
  1041. (<a href=$ana_url>$ana_details->name</a>)<br>";
  1042. // Show Kegg results
  1043. $sql = "SELECT AFP.value AS afpvalue
  1044. FROM {analysisfeatureprop} AFP
  1045. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  1046. WHERE AF.analysis_id = %d
  1047. AND AF.feature_id = %d
  1048. ";
  1049. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  1050. while ($afp = db_fetch_object($kegg_results)) {
  1051. $content .= "$afp->afpvalue<br>";
  1052. }
  1053. $content .= "<br>";
  1054. }
  1055. $content .= '</td></tr></table></span></div></div>';
  1056. }
  1057. db_set_active($previous_db);
  1058. return $content;
  1059. }
  1060. /************************************************************************
  1061. */
  1062. function tripal_analysis_kegg_organism_add($node) {
  1063. // Show GO information in a expandable box for a organism page.
  1064. // Make sure we have $node->organism_id. In the case of creating a new
  1065. // organism, the organism_id is not created until we save. This will cause
  1066. // an error when users preview the creation without a $node->organism_id
  1067. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  1068. if(strcmp($box_status,"menu_off")==0){
  1069. $content .= "
  1070. <div class=\"tripal_kegg_summary-info-box\">
  1071. <div class=\"tripal_expandableBox\">
  1072. <h3>KEGG Analysis Results</h3>
  1073. </div>
  1074. <div class=\"tripal_expandableBoxContent\">
  1075. ";
  1076. }
  1077. $select_analysis = drupal_get_form('tripal_analysis_kegg_select_form',$node);
  1078. $url = url("sites/all/themes/theme_tripal/images/ajax-loader.gif");
  1079. $content .= "
  1080. $select_analysis
  1081. <div id=\"tripal_ajaxLoading\" style=\"display:none\">
  1082. <div id=\"loadingText\">Loading...</div>
  1083. <img src=\"$url\"></div>
  1084. <div id=\"tripal_analysis_kegg_org_report\"></div>
  1085. ";
  1086. if(user_access('access administrative pages')){
  1087. $link = url("tripal_toggle_box_menu/tripal_analysis_kegg/results/$node->nid");
  1088. if(strcmp($box_status,"menu_off")==0){
  1089. $content .= "<br><a href=\"$link\">Show on menu</a>";
  1090. } else {
  1091. $content .= "<br><a href=\"$link\">Remove from menu</a>";
  1092. }
  1093. }
  1094. if(strcmp($box_status,"menu_off")==0){
  1095. $content .= "</div></div>";
  1096. }
  1097. return $content;
  1098. }
  1099. /************************************************************************
  1100. */
  1101. function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
  1102. $form = array();
  1103. // find analyses that have GO terms
  1104. $sql = "
  1105. SELECT *
  1106. FROM {kegg_by_organism} KBO
  1107. WHERE organism_id = %d
  1108. ORDER BY analysis_id DESC
  1109. ";
  1110. $previous_db = db_set_active('chado');
  1111. $results = db_query($sql,$node->organism_id);
  1112. db_set_active($previous_db);
  1113. $analyses = array();
  1114. $analyses[''] = '';
  1115. while($analysis = db_fetch_object($results)){
  1116. $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
  1117. }
  1118. # create the select box
  1119. $form['analysis_select'] = array(
  1120. '#title' => t('Select a KEGG report to view'),
  1121. '#description' => t('Any analysis with KEGG results related to this organism are available for viewing. For further information, see the analysis information page.'),
  1122. '#type' => 'select',
  1123. '#options' => $analyses,
  1124. '#attributes' => array (
  1125. 'onchange' => 'tripal_analysis_kegg_org_report(this.options[this.selectedIndex].value)'
  1126. ),
  1127. );
  1128. return $form;
  1129. }