tripal_analysis_kegg.module 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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 = tripal_db_set_active('chado');
  154. $result = db_query($sql, $analysis_id);
  155. tripal_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 = tripal_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. tripal_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 = tripal_db_set_active('chado');
  340. $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
  341. tripal_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 = tripal_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. tripal_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 = tripal_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. tripal_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 = tripal_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. tripal_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 = tripal_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. tripal_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. print "There are $total_files keg file(s).\n";
  607. $interval = intval($total_files * 0.01);
  608. $no_file = 0;
  609. while ($file = readdir($dir_handle)) {
  610. if(preg_match("/^.*\.keg/",$file)){
  611. // Update the progress
  612. if ($no_file % $interval == 0) {
  613. $percentage = (int) ($no_file / $total_files * 100);
  614. tripal_job_set_progress($job_id, $percentage);
  615. print $percentage."% ";
  616. }
  617. $no_file ++;
  618. # $type variable will be set in tripal_analysis_kegg_parse_keg_file()
  619. $content = tripal_analysis_kegg_parse_keg_file("$hierdir/$file",$type,$analysis_id, $base_path);
  620. # add the item to the database
  621. if($content){
  622. //------------------------------------------------------
  623. // Insert into analysisprop table
  624. //------------------------------------------------------
  625. // Make sure the same value doesn't exist before inserting into chado
  626. $sql = "SELECT value
  627. FROM {analysisprop}
  628. WHERE analysis_id = %d
  629. AND type_id = (SELECT cvterm_id
  630. FROM {cvterm} CVT
  631. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  632. WHERE CV.name = 'tripal' AND CVT.name = '%s'
  633. )
  634. ";
  635. $previous_db = tripal_db_set_active('chado');
  636. $oldvalue = db_result(db_query($sql, $analysis_id, $type));
  637. tripal_db_set_active($previous_db);
  638. // Insert the content
  639. if ($oldvalue != $content) {
  640. $previous_db = tripal_db_set_active('chado'); // Use chado database
  641. // Get type_id for the BRITE term
  642. $sql = "SELECT cvterm_id
  643. FROM {cvterm} CVT
  644. INNER JOIN CV ON CVT.cv_id = CV.cv_id
  645. WHERE CV.name = 'tripal' AND CVT.name = '%s'";
  646. $brite_cvterm_id = db_result(db_query($sql, $type));
  647. // Replace all single quote as HTML code before insert
  648. $content = preg_replace("/\'/", "&#39;", $content);
  649. // Insert the value
  650. $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value)
  651. VALUES (%d, %d,'$content')";
  652. db_query($sql, $analysis_id, $brite_cvterm_id);
  653. tripal_db_set_active($previous_db); // Use drupal database
  654. }
  655. }
  656. }
  657. }
  658. print "Done.\n";
  659. closedir($dir_handle);
  660. // If user input a file, remove decompressed files after parsing
  661. if (is_file($hierfile)) {
  662. $stderr = shell_exec("rm -r $hierdir;");
  663. print "$stderr\n";
  664. }
  665. return;
  666. }
  667. /*******************************************************************************
  668. * Parse *.keg files.
  669. * Each file has a definition line. BRITE term is extracted * from this line
  670. * and added to chado as a cvterm. Tree structure for this cvterm is then
  671. * generated and saved to analysisfeature and analysisfeatureprop tables.
  672. */
  673. function tripal_analysis_kegg_parse_keg_file ($file,&$type,$analysis_id, $base_path){
  674. $handle = fopen($file,'r');
  675. $depth = array();
  676. $current = '@'; # this is one character below 'A' in the ASCII table
  677. $prev = '@';
  678. $id = 0;
  679. $type_id = 0;
  680. $no_line = 0;
  681. $prefix = variable_get('chado_feature_accession_prefix','ID');
  682. while($line = fgets($handle)){
  683. $no_line ++;
  684. // Find out what kind of file we're looking at.
  685. if(preg_match("/#DEFINITION\s+(.*)\s+.+?$/",$line,$matches)){
  686. // Set type as the matched term in the DEFINITION line and add it as a new cvterm
  687. $type = $matches[1];
  688. // Before inserting, make sure this cvterm doesn't exist
  689. $previous_db = tripal_db_set_active('chado');
  690. $sql = "SELECT cvterm_id
  691. FROM {cvterm} CVT
  692. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  693. WHERE cv.name = 'tripal'
  694. AND CVT.name = '%s'";
  695. $type_id = db_result(db_query($sql, $type));
  696. tripal_db_set_active($previous_db);
  697. if (!$type_id) {
  698. tripal_add_cvterms($type, "KEGG BRITE term: $type");
  699. // Get newly added type_id
  700. $sql = "SELECT cvterm_id
  701. FROM {cvterm} CVT
  702. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  703. WHERE cv.name = 'tripal'
  704. AND CVT.name = '%s'";
  705. $previous_db = tripal_db_set_active('chado');
  706. $type_id = db_result(db_query($sql, $type));
  707. tripal_db_set_active($previous_db);
  708. }
  709. }
  710. // get the depth of the hierarch (e.g. A,B,C or D);
  711. preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
  712. # skip lines that aren't data or are empty
  713. if(!$matches[1] or !$matches[2]){continue;}
  714. $prev = $current;
  715. $current = $matches[1];
  716. for($i = (ord($current) - ord($prev)); $i < 0; $i++){
  717. $content .= "</li></ul>\n";
  718. }
  719. for($i = 0; $i < (ord($current) - ord($prev)); $i++){
  720. $content .= "<ul>\n";
  721. }
  722. // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
  723. $matches[2] = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$matches[2]);
  724. // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
  725. $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
  726. // extract the features that have been mapped to the KEGG IDs
  727. if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$mat)){
  728. // Find cvterm_id for 'kegg_brite_data'
  729. $sql = "SELECT cvterm_id
  730. FROM {cvterm} CVT
  731. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  732. WHERE cv.name = 'tripal'
  733. AND CVT.name = '%s'";
  734. $previous_db = tripal_db_set_active('chado');
  735. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  736. tripal_db_set_active($previous_db);
  737. $uniquename = $mat[1];
  738. // Find feature_id using uniquename
  739. $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
  740. $previous_db = tripal_db_set_active('chado');
  741. $feature_id = db_result(db_query($sql, $uniquename)); // retrive first returned feature_id (assuming uniquename is unique)
  742. tripal_db_set_active($previous_db);
  743. // Get the higest rank for this feature_id in analysisfeatureprop table
  744. $sql = "SELECT MAX(rank) FROM {analysisfeatureprop} AFP ".
  745. "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
  746. "WHERE feature_id=%d ".
  747. "AND analysis_id=%d ".
  748. "AND type_id=%d ";
  749. $previous_db = tripal_db_set_active('chado');
  750. $afp = db_fetch_object(db_query($sql, $feature_id, $analysis_id, $brite_data_type_id));
  751. tripal_db_set_active($prevous_db);
  752. $hi_rank = 0;
  753. if ($afp) {
  754. $hi_rank = $afp->max + 1;
  755. }
  756. //------------------------------------------------------
  757. // Insert into analysisfeature table
  758. //------------------------------------------------------
  759. $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
  760. "VALUES (%d, %d)";
  761. $previous_db = tripal_db_set_active('chado');
  762. db_query ($sql, $feature_id, $analysis_id);
  763. tripal_db_set_active($previous_db);
  764. // Get the newly inserted analysisfeature_id
  765. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
  766. $previous_db = tripal_db_set_active('chado');
  767. $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
  768. tripal_db_set_active($previous_db);
  769. //------------------------------------------------------
  770. // Insert into analysisfeatureprop table
  771. //------------------------------------------------------
  772. // Before inserting, make sure it's not a duplicate
  773. $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
  774. $previous_db = tripal_db_set_active('chado');
  775. $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
  776. tripal_db_set_active($previous_db);
  777. $duplicate = 0;
  778. while ($afp_value = db_fetch_object($result)) {
  779. preg_match("/<a.+?>(.+)<\/a>/",$afp_value->value,$old_ids);
  780. preg_match("/<a.+?>(.+)<\/a>/",$mat[2], $new_ids);
  781. if ($old_ids[1] && $old_ids[1] == $new_ids[1]) {
  782. $duplicate = 1;
  783. }
  784. }
  785. if (!$duplicate) {
  786. $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
  787. "VALUES (%d, %d, '%s', %d)";
  788. $previous_db = tripal_db_set_active('chado');
  789. db_query($sql, $analysisfeature_id, $brite_data_type_id, $mat[2], $hi_rank);
  790. tripal_db_set_active($previous_db);
  791. }
  792. // Add link to each matched feature
  793. $feature_url = url("$prefix$feature_id");
  794. $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]);
  795. }
  796. $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
  797. $id++;
  798. }
  799. $content .= "</ul>";
  800. fclose($handle);
  801. return $content;
  802. }
  803. /*******************************************************************************
  804. * HOOK: Implementation of hook_nodeapi()
  805. * Display library information for associated features or organisms
  806. * This function also provides contents for indexing
  807. */
  808. function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
  809. switch ($op) {
  810. // Note that this function only adds library view to an organism/feature
  811. // node. The view of a library node is controled by the theme *.tpl file
  812. case 'view':
  813. // Set the node types for showing library information
  814. $types_to_show = variable_get('tripal_analysis_kegg_setting',
  815. array('chado_feature','chado_analysis_kegg','chado_organism'));
  816. // Abort if this node is not one of the types we should show.
  817. if (!in_array($node->type, $types_to_show, TRUE)) {
  818. // Turn the menu off if it's on
  819. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  820. if (strcmp($box_status,"menu_on")==0 && $node->type =='chado_organism'){
  821. variable_set("tripal_analysis_kegg-box-results","menu_off");
  822. }
  823. break;
  824. }
  825. // Add library to the content item if it's not a teaser
  826. if (!$teaser) {
  827. // add the library to the organism/feature search indexing
  828. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  829. $node->content['tripal_analysis_kegg_search_index'] = array(
  830. '#value' => theme('tripal_analysis_kegg_search_index',$node),
  831. '#weight' => 6,
  832. );
  833. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  834. $node->content['tripal_analysis_kegg_search_result'] = array(
  835. '#value' => theme('tripal_analysis_kegg_search_result',$node),
  836. '#weight' => 6,
  837. );
  838. } else {
  839. // Show KEGG BRITE on an analysis page OR KEGG info box on a feature page
  840. $node->content['tripal_analysis_kegg_node_add'] = array(
  841. '#value' => theme('tripal_analysis_kegg_node_add', $node),
  842. '#weight' => 6
  843. );
  844. }
  845. }
  846. }
  847. }
  848. /************************************************************************
  849. * We need to let drupal know about our theme functions and their arguments.
  850. * We create theme functions to allow users of the module to customize the
  851. * look and feel of the output generated in this module
  852. */
  853. function tripal_analysis_kegg_theme () {
  854. return array(
  855. 'tripal_analysis_kegg_search_index' => array (
  856. 'arguments' => array('node'),
  857. ),
  858. 'tripal_analysis_kegg_search_result' => array (
  859. 'arguments' => array('node'),
  860. ),
  861. 'tripal_analysis_kegg_node_add' => array (
  862. 'arguments' => array('node'),
  863. )
  864. );
  865. }
  866. /************************************************************************
  867. */
  868. function theme_tripal_analysis_kegg_search_index($node){
  869. if ($node->type == 'chado_feature') {
  870. // Find cvterm_id for 'kegg_brite_data'
  871. $sql = "SELECT cvterm_id
  872. FROM {cvterm} CVT
  873. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  874. WHERE cv.name = 'tripal'
  875. AND CVT.name = '%s'";
  876. $previous_db = tripal_db_set_active('chado');
  877. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  878. // Get analysis id
  879. $sql = "SELECT analysis_id AS aid
  880. FROM {analysisfeature} AF
  881. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  882. WHERE feature_id = %d
  883. AND AFP.type_id = %d
  884. GROUP BY analysis_id";
  885. $feature = $node->feature;
  886. $feature_id = $feature->feature_id;
  887. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  888. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  889. // Show kegg result ORDER BY time
  890. if ($hasResult) { // If there is any result, show expandable box
  891. $content = "";
  892. while ($ana = db_fetch_object($result)) {
  893. // Show analysis date
  894. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  895. FROM {analysis}
  896. WHERE analysis_id = %d";
  897. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  898. // Find node id for the analysis
  899. tripal_db_set_active($previous_db);
  900. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  901. $ana_url = url("node/".$ana_nid);
  902. $previous_db = tripal_db_set_active('chado');
  903. // Show content
  904. $content .= "$ana_details->name";
  905. // Show Kegg results
  906. $sql = "SELECT AFP.value AS afpvalue
  907. FROM {analysisfeatureprop} AFP
  908. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  909. WHERE AF.analysis_id = %d
  910. AND AF.feature_id = %d
  911. ";
  912. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  913. while ($afp = db_fetch_object($kegg_results)) {
  914. $content .= " $afp->afpvalue";
  915. }
  916. }
  917. }
  918. tripal_db_set_active($previous_db);
  919. return $content;
  920. }
  921. }
  922. /************************************************************************
  923. */
  924. function theme_tripal_analysis_kegg_search_result($node){
  925. $content = theme_tripal_analysis_kegg_node_add($node);
  926. return $content;
  927. }
  928. /************************************************************************
  929. */
  930. function theme_tripal_analysis_kegg_node_add($node) {
  931. // Show Kegg additional information on a KEGG Analysis page
  932. if ($node->type == 'chado_analysis_kegg') {
  933. return tripal_analysis_kegg_full_report($node->analysis_id);
  934. }
  935. // Show Kegg-info-box on a Feature page
  936. else if ($node->type == 'chado_feature') {
  937. return tripal_analysis_kegg_feature_add($node);
  938. }
  939. // Show Kegg full reports on the organism page
  940. else if ($node->type == 'chado_organism') {
  941. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  942. if (strcmp($box_status,"menu_off")==0){
  943. $content .= tripal_analysis_kegg_organism_add($node);
  944. }
  945. }
  946. return $content;
  947. }
  948. /************************************************************************
  949. */
  950. function tripal_analysis_kegg_org_report($analysis_id){
  951. $content = tripal_analysis_kegg_full_report($analysis_id);
  952. $opt = array($content);
  953. return drupal_json($opt);
  954. }
  955. /************************************************************************
  956. */
  957. function tripal_analysis_kegg_full_report($analysis_id){
  958. // Test if brite data have been parsed into the database
  959. $sql = "SELECT CVT.name, CVT.cvterm_id
  960. FROM {cvterm} CVT
  961. INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
  962. WHERE AP.analysis_id = %d
  963. AND CVT.definition LIKE 'KEGG BRITE term: %'
  964. ORDER BY CVT.cvterm_id";
  965. $previous_db = tripal_db_set_active('chado');
  966. $result = db_query($sql, $analysis_id);
  967. tripal_db_set_active($previous_db);
  968. if (db_result($result)) {
  969. $content = tripal_analysis_kegg_brite($analysis_id, $type_id, 0);
  970. } else {
  971. $content = "<i>Note:</i> Analysis result is not available. Please schedule and run the job to parse the kegg output.";
  972. }
  973. return $content;
  974. }
  975. /*******************************************************************************
  976. * Tripal Kegg administrative setting form. This function is called by
  977. * tripal_analysis module which asks for an admin form to show on the page
  978. */
  979. function tripal_analysis_kegg_get_settings() {
  980. // Get an array of node types with internal names as keys
  981. $options = node_get_types('names');
  982. // Add 'chado_feature' to allowed content types for showing kegg results
  983. $allowedoptions ['chado_feature'] = "Show KEGG results on feature pages";
  984. $allowedoptions ['chado_analysis_kegg'] = "Show KEGG BRITE results on the analysis page.";
  985. $allowedoptions ['chado_organism'] = "Show KEGG BRITE results on the organism pages.";
  986. $form['description'] = array(
  987. '#type' => 'item',
  988. '#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."),
  989. '#weight' => 0,
  990. );
  991. $form['tripal_analysis_kegg_setting'] = array(
  992. '#type' => 'checkboxes',
  993. '#options' => $allowedoptions,
  994. '#default_value' => variable_get('tripal_analysis_kegg_setting',
  995. array('chado_feature', 'chado_analysis_kegg')),
  996. );
  997. $settings->form = $form;
  998. $settings->title = "Tripal Kegg";
  999. return $settings;
  1000. }
  1001. /************************************************************************
  1002. */
  1003. function tripal_analysis_kegg_organism_results($node) {
  1004. $node = node_load($node);
  1005. return tripal_analysis_kegg_organism_add($node);
  1006. }
  1007. /************************************************************************
  1008. */
  1009. function tripal_analysis_kegg_feature_add($node) {
  1010. // Find cvterm_id for 'kegg_brite_data'
  1011. $sql = "SELECT cvterm_id
  1012. FROM {cvterm} CVT
  1013. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  1014. WHERE cv.name = 'tripal'
  1015. AND CVT.name = '%s'";
  1016. $previous_db = tripal_db_set_active('chado');
  1017. $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
  1018. // Get analysis id
  1019. $sql = "SELECT analysis_id AS aid
  1020. FROM {analysisfeature} AF
  1021. INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
  1022. WHERE feature_id = %d
  1023. AND AFP.type_id = %d
  1024. GROUP BY analysis_id";
  1025. $feature = $node->feature;
  1026. $feature_id = $feature->feature_id;
  1027. $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
  1028. $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
  1029. // Show kegg result ORDER BY time
  1030. if ($hasResult) { // If there is any result, show expandable box
  1031. $content .= "<div id=\"tripal_kegg-hits\" class=\"tripal_kegg-info-box\">
  1032. <div class=\"tripal_expandableBox\">
  1033. <h3>KEGG Analysis</h3>
  1034. </div>
  1035. <div class=\"tripal_expandableBoxContent\">
  1036. <span>
  1037. <table class=\"tripal_kegg_results_table\">
  1038. <tr><td>";
  1039. while ($ana = db_fetch_object($result)) {
  1040. // Show analysis date
  1041. $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  1042. FROM {analysis}
  1043. WHERE analysis_id = %d";
  1044. $ana_details = db_fetch_object(db_query($sql, $ana->aid));
  1045. // Find node id for the analysis
  1046. tripal_db_set_active($previous_db);
  1047. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
  1048. $ana_url = url("node/".$ana_nid);
  1049. $previous_db = tripal_db_set_active('chado');
  1050. // Show content
  1051. $content .= "<strong>Analysis Date:</strong> $ana_details->time
  1052. (<a href=$ana_url>$ana_details->name</a>)<br>";
  1053. // Show Kegg results
  1054. $sql = "SELECT AFP.value AS afpvalue
  1055. FROM {analysisfeatureprop} AFP
  1056. INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  1057. WHERE AF.analysis_id = %d
  1058. AND AF.feature_id = %d
  1059. ";
  1060. $kegg_results = db_query($sql, $ana->aid, $feature_id);
  1061. while ($afp = db_fetch_object($kegg_results)) {
  1062. $content .= "$afp->afpvalue<br>";
  1063. }
  1064. $content .= "<br>";
  1065. }
  1066. $content .= '</td></tr></table></span></div></div>';
  1067. }
  1068. tripal_db_set_active($previous_db);
  1069. return $content;
  1070. }
  1071. /************************************************************************
  1072. */
  1073. function tripal_analysis_kegg_organism_add($node) {
  1074. // Show GO information in a expandable box for a organism page.
  1075. // Make sure we have $node->organism_id. In the case of creating a new
  1076. // organism, the organism_id is not created until we save. This will cause
  1077. // an error when users preview the creation without a $node->organism_id
  1078. $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
  1079. if(strcmp($box_status,"menu_off")==0){
  1080. $content .= "
  1081. <div class=\"tripal_kegg_summary-info-box\">
  1082. <div class=\"tripal_expandableBox\">
  1083. <h3>KEGG Analysis Results</h3>
  1084. </div>
  1085. <div class=\"tripal_expandableBoxContent\">
  1086. ";
  1087. }
  1088. $select_analysis = drupal_get_form('tripal_analysis_kegg_select_form',$node);
  1089. $url = url("sites/all/themes/theme_tripal/images/ajax-loader.gif");
  1090. $content .= "
  1091. $select_analysis
  1092. <div id=\"tripal_ajaxLoading\" style=\"display:none\">
  1093. <div id=\"loadingText\">Loading...</div>
  1094. <img src=\"$url\"></div>
  1095. <div id=\"tripal_analysis_kegg_org_report\"></div>
  1096. ";
  1097. if(user_access('access administrative pages')){
  1098. $link = url("tripal_toggle_box_menu/tripal_analysis_kegg/results/$node->nid");
  1099. if(strcmp($box_status,"menu_off")==0){
  1100. $content .= "<br><a href=\"$link\">Show on menu</a>";
  1101. } else {
  1102. $content .= "<br><a href=\"$link\">Remove from menu</a>";
  1103. }
  1104. }
  1105. if(strcmp($box_status,"menu_off")==0){
  1106. $content .= "</div></div>";
  1107. }
  1108. return $content;
  1109. }
  1110. /************************************************************************
  1111. */
  1112. function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
  1113. $form = array();
  1114. // find analyses that have GO terms
  1115. $sql = "
  1116. SELECT *
  1117. FROM {kegg_by_organism} KBO
  1118. WHERE organism_id = %d
  1119. ORDER BY analysis_id DESC
  1120. ";
  1121. $previous_db = tripal_db_set_active('chado');
  1122. $results = db_query($sql,$node->organism_id);
  1123. tripal_db_set_active($previous_db);
  1124. $analyses = array();
  1125. $analyses[''] = '';
  1126. while($analysis = db_fetch_object($results)){
  1127. $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
  1128. }
  1129. # create the select box
  1130. $form['analysis_select'] = array(
  1131. '#title' => t('Select a KEGG report to view'),
  1132. '#description' => t('Any analysis with KEGG results related to this organism are available for viewing. For further information, see the analysis information page.'),
  1133. '#type' => 'select',
  1134. '#options' => $analyses,
  1135. '#attributes' => array (
  1136. 'onchange' => 'tripal_analysis_kegg_org_report(this.options[this.selectedIndex].value)'
  1137. ),
  1138. );
  1139. return $form;
  1140. }