tripal_analysis_kegg.module 45 KB

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