tripal_analysis_go.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. function tripal_analysis_go_init(){
  3. // Add style sheet
  4. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_go.css');
  5. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_go.js');
  6. }
  7. /*******************************************************************************
  8. * Menu items are automatically added for the new node types created
  9. * by this module to the 'Create Content' Navigation menu item. This function
  10. * adds more menu items needed for this module.
  11. */
  12. function tripal_analysis_go_menu() {
  13. $items = array();
  14. $items['download_goterm_features'] = array(
  15. 'path' => 'download_goterm_features',
  16. 'title' => t('Get GO Term Features'),
  17. 'page callback' => 'tripal_analysis_go_get_goterm_features',
  18. 'page arguments' => array(1,2),
  19. 'access arguments' => array('access chado_analysis_go content'),
  20. 'type' => MENU_CALLBACK
  21. );
  22. $items['tripal_analysis_go_org_charts'] = array(
  23. 'path' => 'tripal_analysis_go_org_charts',
  24. 'title' => t('Analysis GO Charts'),
  25. 'page callback' => 'tripal_analysis_go_org_charts',
  26. 'page arguments' => array(1),
  27. 'access arguments' => array('access chado_analysis_go content'),
  28. 'type' => MENU_CALLBACK
  29. );
  30. return $items;
  31. }
  32. /*******************************************************************************
  33. * Set the permission types that the chado module uses. Essentially we
  34. * want permissionis that protect creation, editing and deleting of chado
  35. * data objects
  36. */
  37. function tripal_analysis_go_perm(){
  38. return array(
  39. 'access chado_analysis_go content',
  40. 'create chado_analysis_go content',
  41. 'delete chado_analysis_go content',
  42. 'edit chado_analysis_go content',
  43. );
  44. }
  45. /*******************************************************************************
  46. * The following function proves access control for users trying to
  47. * perform actions on data managed by this module
  48. */
  49. function chado_analysis_go_access($op, $node, $account){
  50. if ($op == 'create') {
  51. return user_access('create chado_analysis_go content', $account);
  52. }
  53. if ($op == 'update') {
  54. if (user_access('edit chado_analysis_go content', $account)) {
  55. return TRUE;
  56. }
  57. }
  58. if ($op == 'delete') {
  59. if (user_access('delete chado_analysis_go content', $account)) {
  60. return TRUE;
  61. }
  62. }
  63. if ($op == 'view') {
  64. if (user_access('access chado_analysis_go content', $account)) {
  65. return TRUE;
  66. }
  67. }
  68. return FALSE;
  69. }
  70. /*******************************************************************************
  71. *
  72. */
  73. function tripal_analysis_go_block($op = 'list', $delta = 0, $edit=array()){
  74. switch($op) {
  75. case 'list':
  76. $blocks['tago_org_sum']['info'] = t('Tripal Organism GO Analysis Report');
  77. $blocks['tago_org_sum']['cache'] = BLOCK_NO_CACHE;
  78. return $blocks;
  79. case 'view':
  80. if(user_access('access chado_analysis_go content') and arg(0) == 'node' and is_numeric(arg(1))) {
  81. $nid = arg(1);
  82. $node = node_load($nid);
  83. $block = array();
  84. switch($delta){
  85. case 'tago_org_sum':
  86. $block['subject'] = t('GO Summary');
  87. $block['content'] = theme('tripal_analysis_go_org_summary',$node);
  88. break;
  89. default :
  90. }
  91. return $block;
  92. }
  93. }
  94. }
  95. /*******************************************************************************
  96. * HOOK: Implementation of hook_nodeapi()
  97. * Display library information for associated features or organisms
  98. * This function also provides contents for indexing
  99. */
  100. function tripal_analysis_go_nodeapi(&$node, $op, $teaser, $page) {
  101. switch ($op) {
  102. case 'view':
  103. if ($teaser) {
  104. return;
  105. }
  106. // add the library to the organism/feature search indexing
  107. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  108. $node->content['tripal_analysis_go_search_index'] = array(
  109. '#value' => theme('tripal_analysis_go_search_index',$node),
  110. '#weight' => 5,
  111. );
  112. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  113. $node->content['tripal_analysis_go_search_result'] = array(
  114. '#value' => theme('tripal_analysis_go_search_result',$node),
  115. '#weight' => 5,
  116. );
  117. } else {
  118. // Show go terms if the organism/feature is not at teaser view
  119. switch($node->type){
  120. case 'chado_organism':
  121. $node->content['tripal_organism_go_summary'] = array(
  122. '#value' => theme('tripal_organism_go_summary',$node),
  123. '#weight' => 5,
  124. );
  125. break;
  126. case 'chado_feature':
  127. $node->content['tripal_feature_go_terms'] = array(
  128. '#value' => theme('tripal_feature_go_terms',$node),
  129. '#weight' => 5,
  130. );
  131. break;
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. /************************************************************************
  138. * We need to let drupal know about our theme functions and their arguments.
  139. * We create theme functions to allow users of the module to customize the
  140. * look and feel of the output generated in this module
  141. */
  142. function tripal_analysis_go_theme () {
  143. return array(
  144. 'tripal_analysis_go_search_index' => array (
  145. 'arguments' => array('node'),
  146. ),
  147. 'tripal_analysis_go_search_result' => array (
  148. 'arguments' => array('node'),
  149. ),
  150. 'tripal_organism_go_summary' => array (
  151. 'arguments' => array('node'=> null),
  152. 'template' => 'tripal_organism_go_summary',
  153. ),
  154. 'tripal_feature_go_terms' => array (
  155. 'arguments' => array('node'=> null),
  156. 'template' => 'tripal_feature_go_terms',
  157. )
  158. );
  159. }
  160. /************************************************************************
  161. */
  162. function theme_tripal_analysis_go_search_index($node){
  163. }
  164. /************************************************************************
  165. */
  166. function theme_tripal_analysis_go_search_result($node){
  167. }
  168. /************************************************************************
  169. */
  170. function tripal_analysis_go_preprocess_tripal_feature_go_terms(&$variables){
  171. $node = $variables['node'];
  172. $variables['tripal_analysis_go']['go_terms'] = tripal_analysis_go_feature_add($node);
  173. }
  174. /************************************************************************
  175. */
  176. function tripal_analysis_go_preprocess_tripal_organism_go_summary(&$variables){
  177. $node = $variables['node'];
  178. $variables['tripal_analysis_go']['form'] = tripal_analysis_go_organism_add($node);
  179. }
  180. /************************************************************************
  181. */
  182. function tripal_analysis_go_select_form(&$form_state = NULL,$node){
  183. $form = array();
  184. // find analyses that have GO terms
  185. $sql = "
  186. SELECT DISTINCT A.analysis_id, A.name, GCA.organism_id
  187. FROM {go_count_analysis} GCA
  188. INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
  189. WHERE organism_id = %d
  190. ORDER BY analysis_id DESC
  191. ";
  192. $previous_db = tripal_db_set_active('chado');
  193. $results = db_query($sql,$node->organism_id);
  194. tripal_db_set_active($previous_db);
  195. $analyses = array();
  196. $analyses[''] = '';
  197. while($analysis = db_fetch_object($results)){
  198. $analyses[$analysis->analysis_id."-".$analysis->organism_id] = "$analysis->name";
  199. }
  200. # create the select box
  201. $form['analysis_select'] = array(
  202. '#title' => t('Select a GO report to view'),
  203. '#description' => t('Any analysis with GO results related to this organism are available for viewing. For further information, see the analysis information page.'),
  204. '#type' => 'select',
  205. '#options' => $analyses,
  206. '#attributes' => array (
  207. 'onchange' => 'tripal_analysis_go_org_charts(this.options[this.selectedIndex].value)'
  208. ),
  209. );
  210. return $form;
  211. }
  212. /************************************************************************
  213. */
  214. function tripal_analysis_go_org_charts ($element) {
  215. $analysis_id = preg_replace("/^(\d+)-(\d+)$/","$1",$element);
  216. $organism_id = preg_replace("/^(\d+)-(\d+)$/","$2",$element);
  217. $content = '';
  218. if($analysis_id and $organism_id){
  219. $content = "
  220. <b>Biological Process</b>
  221. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  222. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_bp\"></div>
  223. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_bp\" src=\"\" border=\"0\">
  224. <br><br><br><br>
  225. <b>Cellular Component</b>
  226. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  227. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_cc\"></div>
  228. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_cc\" src=\"\" border=\"0\">
  229. <br><br><br><br>
  230. <b>Molecular Function</b>
  231. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  232. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_mf\"></div>
  233. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_mf\" src=\"\" border=\"0\">
  234. ";
  235. }
  236. $opt = array($content);
  237. return drupal_json($opt);
  238. }
  239. /************************************************************************
  240. */
  241. function tripal_analysis_go_organism_add($node) {
  242. $organism = $node->organism;
  243. // check to see if we have any analyses
  244. $sql = "
  245. SELECT count(*) as cnt
  246. FROM {go_count_analysis} GCA
  247. INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
  248. WHERE organism_id = %d
  249. ";
  250. $previous_db = tripal_db_set_active('chado');
  251. $num_analyses = db_fetch_object(db_query($sql,$organism->organism_id));
  252. tripal_db_set_active($previous_db);
  253. if($num_analyses->cnt > 0){
  254. return array (
  255. 'status' => 1,
  256. 'value' => drupal_get_form('tripal_analysis_go_select_form',$node),
  257. );
  258. } else {
  259. return array(
  260. 'status' => 0,
  261. 'value' => t('There are no GO analysis reports'),
  262. );
  263. }
  264. }
  265. /************************************************************************
  266. *
  267. */
  268. function tripal_analysis_go_cv_chart($chart_id){
  269. // The CV module will create the JSON array necessary for buillding a
  270. // pie chart using jgChart and Google Charts. We have to pass to it
  271. // a table that contains count information, tell it which column
  272. // contains the cvterm_id and provide a filter for getting the
  273. // results we want from the table.
  274. $organism_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$chart_id);
  275. $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  276. $type = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);
  277. $sql = "SELECT * FROM {Analysis} WHERE analysis_id = %d";
  278. $previous_db = tripal_db_set_active('chado'); // use chado database
  279. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  280. tripal_db_set_active($previous_db); // now use drupal database
  281. if(strcmp($type,'mf')==0){
  282. $class = 'molecular_function';
  283. $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  284. }
  285. if(strcmp($type,'cc')==0){
  286. $class = 'cellular_component';
  287. $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  288. }
  289. if(strcmp($type,'bp')==0){
  290. $class = 'biological_process';
  291. $title = "Number of Biological Process Terms From $analysis->name Analysis";
  292. }
  293. $options = array(
  294. count_mview => 'go_count_analysis',
  295. cvterm_id_column => 'cvterm_id',
  296. count_column => 'feature_count',
  297. filter => "
  298. CNT.organism_id = $organism_id AND
  299. CNT.analysis_id = $analysis_id AND
  300. CNT.cvterm_id IN (
  301. SELECT CVTR.subject_id
  302. FROM {CVTerm_relationship} CVTR
  303. INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
  304. INNER JOIN CV on CVT.cv_id = CV.cv_id
  305. WHERE CVT.name = '$class' AND
  306. CV.name = '$class'
  307. )
  308. ",
  309. type => 'p',
  310. size => '680x230',
  311. title => $title,
  312. );
  313. return $options;
  314. }
  315. /************************************************************************
  316. *
  317. */
  318. function tripal_analysis_go_cv_tree($tree_id){
  319. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  320. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  321. $type = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$tree_id);
  322. if(strcmp($type,'mf')==0){
  323. $class = 'molecular_function';
  324. }
  325. if(strcmp($type,'cc')==0){
  326. $class = 'cellular_component';
  327. }
  328. if(strcmp($type,'bp')==0){
  329. $class = 'biological_process';
  330. }
  331. $options = array(
  332. cv_id => tripal_cv_get_cv_id($class),
  333. count_mview => 'go_count_analysis',
  334. cvterm_id_column => 'cvterm_id',
  335. count_column => 'feature_count',
  336. filter => "CNT.organism_id = $organism_id AND CNT.analysis_id = $analysis_id",
  337. label => 'Features',
  338. );
  339. return $options;
  340. }
  341. /************************************************************************
  342. */
  343. function tripal_analysis_go_cvterm_add($cvterm_id,$tree_id){
  344. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  345. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  346. $sql = "
  347. SELECT DBX.accession
  348. FROM {cvterm} CVT
  349. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  350. WHERE cvterm_id = %d
  351. ";
  352. $previous_db = tripal_db_set_active('chado');
  353. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  354. tripal_db_set_active($previous_db);
  355. $link = url("download_goterm_features/$cvterm_id/$tree_id");
  356. $options = array(
  357. 'Download sequences' => "<a href=\"$link\">GO_".$xref->accession.".fasta</a>",
  358. );
  359. return $options;
  360. }
  361. /************************************************************************
  362. */
  363. function tripal_analysis_go_get_goterm_features($cvterm_id,$tree_id){
  364. // get hte accession number for this cvterm and use it in naming the download
  365. $sql = "
  366. SELECT DBX.accession
  367. FROM {cvterm} CVT
  368. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  369. WHERE cvterm_id = %d
  370. ";
  371. $previous_db = tripal_db_set_active('chado');
  372. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  373. tripal_db_set_active($previous_db);
  374. drupal_set_header('Content-Type: text');
  375. drupal_set_header('Content-Disposition: attachment; filename="GO_'.$xref->accession.'.fasta"');
  376. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  377. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  378. $sql = "
  379. SELECT DISTINCT F.name,F.residues,F.feature_id
  380. FROM {cvtermpath} CVTP
  381. INNER JOIN CVTerm CVT1 on CVTP.subject_id = CVT1.cvterm_id
  382. INNER JOIN CVTerm CVT2 on CVTP.object_id = CVT2.cvterm_id
  383. INNER JOIN AnalysisFeatureProp AFP on AFP.type_id = CVTP.subject_id
  384. INNER JOIN AnalysisFeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  385. INNER JOIN Feature F on AF.feature_id = F.feature_id
  386. WHERE CVTP.object_id = %d and F.organism_id = %d and AF.analysis_id = %d
  387. ORDER BY F.name
  388. ";
  389. $previous_db = tripal_db_set_active('chado');
  390. $results = db_query($sql,$cvterm_id,$organism_id,$analysis_id);
  391. tripal_db_set_active($previous_db);
  392. while($feature = db_fetch_object($results)){
  393. // get the go term information for each sequence
  394. $sql = "
  395. SELECT CVT.name,DBX.accession
  396. FROM {Feature_CVTerm} FCVT
  397. INNER JOIN CVTerm CVT on FCVT.cvterm_id = CVT.cvterm_id
  398. INNER JOIN DBXref DBX on CVT.dbxref_id = DBX.dbxref_id
  399. WHERE FCVT.feature_id = %d
  400. ";
  401. $previous_db = tripal_db_set_active('chado');
  402. $terms = db_query($sql,$feature->feature_id);
  403. tripal_db_set_active($previous_db);
  404. $desc = '[';
  405. while($term = db_fetch_object($terms)){
  406. $desc .= "GO:$term->accession $term->name; ";
  407. }
  408. $desc = chop($desc);
  409. $desc = chop($desc,';');
  410. $desc .= ']';
  411. print tripal_feature_return_fasta($feature, $desc);
  412. }
  413. return;
  414. }
  415. /************************************************************************
  416. */
  417. function tripal_analysis_go_feature_add($node) {
  418. $content = "";
  419. $sql = "
  420. SELECT DISTINCT FCVT.feature_id,DBX.accession,CVT.name as goterm,
  421. CVT.cvterm_id as go_id, CV.name as cvname
  422. FROM {Feature_Cvterm} FCVT
  423. INNER JOIN Cvterm CVT ON CVT.cvterm_ID = FCVT.cvterm_ID
  424. INNER JOIN CV ON CV.cv_id = CVT.cv_id
  425. INNER JOIN dbxref DBX ON DBX.dbxref_id = CVT.dbxref_id
  426. WHERE
  427. (CV.name = 'biological_process' OR
  428. CV.name = 'cellular_component' OR
  429. CV.name = 'molecular_function') AND
  430. FCVT.feature_id = %d
  431. ORDER BY CV.name, CVT.name
  432. ";
  433. $previous_db = tripal_db_set_active('chado');
  434. $results = db_query($sql,$node->feature->feature_id);
  435. tripal_db_set_active($previous_db);
  436. $term = db_fetch_object($results); // retrive the first result
  437. if ($term) {
  438. $content .= "<div id=\"tripal_go_box\" class=\"tripal_go-info-box\">";
  439. $content .= "<div class=\"tripal_expandableBox\">".
  440. "<h3>GO terms assigned to this feature</h3>".
  441. "</div>";
  442. $content .= "<div class=\"tripal_expandableBoxContent\">";
  443. $content .= "<table class=\"feature-go-terms\">";
  444. $content .= " <tr>";
  445. $content .= " <th class=\"dbfieldname\">Accession</th>";
  446. $content .= " <th class=\"dbfieldname\">Category</th>";
  447. $content .= " <th class=\"dbfieldname\">Term</th>";
  448. $content .= " </tr>";
  449. do {
  450. $content .= "<tr>";
  451. $content .= " <td>GO:$term->accession</td>";
  452. $content .= " <td>$term->cvname</td>";
  453. $content .= " <td>$term->goterm</td>";
  454. $content .= "</tr>";
  455. } while($term = db_fetch_object($results));
  456. $content .= "</table>";
  457. $content .= "</div></div>";
  458. }
  459. return $content;
  460. }
  461. /*******************************************************************************
  462. * Tripal GO administrative setting form. This function is called by
  463. * tripal_analysis module which asks for an admin form to show on the page
  464. */
  465. function tripal_analysis_go_get_settings() {
  466. // Get an array of node types with internal names as keys
  467. $options = node_get_types('names');
  468. // Add 'chado_feature' to allowed content types for showing unigene results
  469. $allowedoptions ['chado_feature'] = "Show GO terms on feature pages";
  470. $allowedoptions ['chado_organism'] = "Show GO analysis on organism pages";
  471. $form['description'] = array(
  472. '#type' => 'item',
  473. '#value' => t("This option allows user to display the Gene Ontology (GO) ".
  474. "information. For features, this would include all GO terms assigned to a feature ".
  475. "and for organisms this would be statistical pie charts of GO terms for a organism. Check the box to ".
  476. "enable the display of GO information. Uncheck to disable."),
  477. '#weight' => 0,
  478. );
  479. $form['tripal_analysis_go_setting'] = array(
  480. '#type' => 'checkboxes',
  481. '#options' => $allowedoptions,
  482. '#default_value'=>variable_get('tripal_analysis_go_setting',array('chado_feature', 'chado_organism')),
  483. );
  484. $settings->form = $form;
  485. $settings->title = "Tripal GO";
  486. return $settings;
  487. }