tripal_analysis_go.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. $feature = $variables['node']->feature;
  172. $variables['tripal_analysis_go']['terms'] = tripal_analysis_go_load_feature_terms($feature);
  173. }
  174. /************************************************************************
  175. */
  176. function tripal_analysis_go_preprocess_tripal_organism_go_summary(&$variables){
  177. $node = $variables['node'];
  178. $variables['tripal_analysis_go']['select_form'] = tripal_analysis_go_load_organism_go_summary($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->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['tripal_analysis_go_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_load_organism_go_summary($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. $results = db_fetch_object(db_query($sql,$organism->organism_id));
  252. tripal_db_set_active($previous_db);
  253. $has_results = 0;
  254. if($results){
  255. $has_results = 1;
  256. }
  257. return array (
  258. 'has_results' => $has_results,
  259. 'form' => drupal_get_form('tripal_analysis_go_select_form',$node),
  260. );
  261. }
  262. /************************************************************************
  263. *
  264. */
  265. function tripal_analysis_go_cv_chart($chart_id){
  266. // The CV module will create the JSON array necessary for buillding a
  267. // pie chart using jgChart and Google Charts. We have to pass to it
  268. // a table that contains count information, tell it which column
  269. // contains the cvterm_id and provide a filter for getting the
  270. // results we want from the table.
  271. $organism_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$chart_id);
  272. $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  273. $type = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);
  274. $sql = "SELECT * FROM {Analysis} WHERE analysis_id = %d";
  275. $previous_db = tripal_db_set_active('chado'); // use chado database
  276. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  277. tripal_db_set_active($previous_db); // now use drupal database
  278. if(strcmp($type,'mf')==0){
  279. $class = 'molecular_function';
  280. $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  281. }
  282. if(strcmp($type,'cc')==0){
  283. $class = 'cellular_component';
  284. $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  285. }
  286. if(strcmp($type,'bp')==0){
  287. $class = 'biological_process';
  288. $title = "Number of Biological Process Terms From $analysis->name Analysis";
  289. }
  290. $options = array(
  291. count_mview => 'go_count_analysis',
  292. cvterm_id_column => 'cvterm_id',
  293. count_column => 'feature_count',
  294. filter => "
  295. CNT.organism_id = $organism_id AND
  296. CNT.analysis_id = $analysis_id AND
  297. CNT.cvterm_id IN (
  298. SELECT CVTR.subject_id
  299. FROM {CVTerm_relationship} CVTR
  300. INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
  301. INNER JOIN CV on CVT.cv_id = CV.cv_id
  302. WHERE CVT.name = '$class' AND
  303. CV.name = '$class'
  304. )
  305. ",
  306. type => 'p',
  307. size => '550x175',
  308. title => $title,
  309. );
  310. return $options;
  311. }
  312. /************************************************************************
  313. *
  314. */
  315. function tripal_analysis_go_cv_tree($tree_id){
  316. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  317. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  318. $type = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$tree_id);
  319. if(strcmp($type,'mf')==0){
  320. $class = 'molecular_function';
  321. }
  322. if(strcmp($type,'cc')==0){
  323. $class = 'cellular_component';
  324. }
  325. if(strcmp($type,'bp')==0){
  326. $class = 'biological_process';
  327. }
  328. $options = array(
  329. cv_id => tripal_cv_get_cv_id($class),
  330. count_mview => 'go_count_analysis',
  331. cvterm_id_column => 'cvterm_id',
  332. count_column => 'feature_count',
  333. filter => "CNT.organism_id = $organism_id AND CNT.analysis_id = $analysis_id",
  334. label => 'Features',
  335. );
  336. return $options;
  337. }
  338. /************************************************************************
  339. */
  340. function tripal_analysis_go_cvterm_add($cvterm_id,$tree_id){
  341. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  342. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  343. $sql = "
  344. SELECT DBX.accession
  345. FROM {cvterm} CVT
  346. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  347. WHERE cvterm_id = %d
  348. ";
  349. $previous_db = tripal_db_set_active('chado');
  350. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  351. tripal_db_set_active($previous_db);
  352. $link = url("download_goterm_features/$cvterm_id/$tree_id");
  353. $options = array(
  354. 'Download sequences' => "<a href=\"$link\">GO_".$xref->accession.".fasta</a>",
  355. );
  356. return $options;
  357. }
  358. /************************************************************************
  359. */
  360. function tripal_analysis_go_get_goterm_features($cvterm_id,$tree_id){
  361. // get hte accession number for this cvterm and use it in naming the download
  362. $sql = "
  363. SELECT DBX.accession
  364. FROM {cvterm} CVT
  365. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  366. WHERE cvterm_id = %d
  367. ";
  368. $previous_db = tripal_db_set_active('chado');
  369. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  370. tripal_db_set_active($previous_db);
  371. drupal_set_header('Content-Type: text');
  372. drupal_set_header('Content-Disposition: attachment; filename="GO_'.$xref->accession.'.fasta"');
  373. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  374. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  375. $sql = "
  376. SELECT DISTINCT F.name,F.residues,F.feature_id
  377. FROM {cvtermpath} CVTP
  378. INNER JOIN CVTerm CVT1 on CVTP.subject_id = CVT1.cvterm_id
  379. INNER JOIN CVTerm CVT2 on CVTP.object_id = CVT2.cvterm_id
  380. INNER JOIN AnalysisFeatureProp AFP on AFP.type_id = CVTP.subject_id
  381. INNER JOIN AnalysisFeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  382. INNER JOIN Feature F on AF.feature_id = F.feature_id
  383. WHERE CVTP.object_id = %d and F.organism_id = %d and AF.analysis_id = %d
  384. ORDER BY F.name
  385. ";
  386. $previous_db = tripal_db_set_active('chado');
  387. $results = db_query($sql,$cvterm_id,$organism_id,$analysis_id);
  388. tripal_db_set_active($previous_db);
  389. while($feature = db_fetch_object($results)){
  390. // get the go term information for each sequence
  391. $sql = "
  392. SELECT CVT.name,DBX.accession
  393. FROM {Feature_CVTerm} FCVT
  394. INNER JOIN CVTerm CVT on FCVT.cvterm_id = CVT.cvterm_id
  395. INNER JOIN DBXref DBX on CVT.dbxref_id = DBX.dbxref_id
  396. WHERE FCVT.feature_id = %d
  397. ";
  398. $previous_db = tripal_db_set_active('chado');
  399. $terms = db_query($sql,$feature->feature_id);
  400. tripal_db_set_active($previous_db);
  401. $desc = '[';
  402. while($term = db_fetch_object($terms)){
  403. $desc .= "GO:$term->accession $term->name; ";
  404. }
  405. $desc = chop($desc);
  406. $desc = chop($desc,';');
  407. $desc .= ']';
  408. print tripal_feature_return_fasta($feature, $desc);
  409. }
  410. return;
  411. }
  412. /************************************************************************
  413. */
  414. function tripal_analysis_go_load_feature_terms($feature) {
  415. $sql = "
  416. SELECT DISTINCT FCVT.feature_id,DBX.accession,CVT.name as goterm,
  417. CVT.cvterm_id as go_id, CV.name as cvname, CVT.definition
  418. FROM {Feature_Cvterm} FCVT
  419. INNER JOIN Cvterm CVT ON CVT.cvterm_ID = FCVT.cvterm_ID
  420. INNER JOIN CV ON CV.cv_id = CVT.cv_id
  421. INNER JOIN dbxref DBX ON DBX.dbxref_id = CVT.dbxref_id
  422. WHERE
  423. (CV.name = 'biological_process' OR
  424. CV.name = 'cellular_component' OR
  425. CV.name = 'molecular_function') AND
  426. FCVT.feature_id = %d
  427. ORDER BY CV.name, CVT.name
  428. ";
  429. $previous_db = tripal_db_set_active('chado');
  430. $results = db_query($sql,$feature->feature_id);
  431. tripal_db_set_active($previous_db);
  432. $i=0;
  433. $terms = array();
  434. while($term = db_fetch_object($results)){
  435. $terms[$i++] = $term;
  436. }
  437. return $terms;
  438. }
  439. /*******************************************************************************
  440. * Tripal GO administrative setting form. This function is called by
  441. * tripal_analysis module which asks for an admin form to show on the page
  442. */
  443. function tripal_analysis_go_get_settings() {
  444. // Get an array of node types with internal names as keys
  445. $options = node_get_types('names');
  446. // Add 'chado_feature' to allowed content types for showing unigene results
  447. $allowedoptions ['chado_feature'] = "Show GO terms on feature pages";
  448. $allowedoptions ['chado_organism'] = "Show GO analysis on organism pages";
  449. $form['description'] = array(
  450. '#type' => 'item',
  451. '#value' => t("This option allows user to display the Gene Ontology (GO) ".
  452. "information. For features, this would include all GO terms assigned to a feature ".
  453. "and for organisms this would be statistical pie charts of GO terms for a organism. Check the box to ".
  454. "enable the display of GO information. Uncheck to disable."),
  455. '#weight' => 0,
  456. );
  457. $form['tripal_analysis_go_setting'] = array(
  458. '#type' => 'checkboxes',
  459. '#options' => $allowedoptions,
  460. '#default_value'=>variable_get('tripal_analysis_go_setting',array('chado_feature', 'chado_organism')),
  461. );
  462. $settings->form = $form;
  463. $settings->title = "Tripal GO";
  464. return $settings;
  465. }