tripal_analysis_go.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. $feature->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. $organism = $node->organism;
  179. $organism->tripal_analysis_go->select_form = tripal_analysis_go_load_organism_go_summary($node);
  180. }
  181. /************************************************************************
  182. */
  183. function tripal_analysis_go_select_form(&$form_state = NULL,$node){
  184. $form = array();
  185. // find analyses that have GO terms
  186. $sql = "
  187. SELECT DISTINCT A.analysis_id, A.name, GCA.organism_id
  188. FROM {go_count_analysis} GCA
  189. INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
  190. WHERE organism_id = %d
  191. ORDER BY analysis_id DESC
  192. ";
  193. $previous_db = tripal_db_set_active('chado');
  194. $results = db_query($sql,$node->organism->organism_id);
  195. tripal_db_set_active($previous_db);
  196. $analyses = array();
  197. $analyses[''] = '';
  198. while($analysis = db_fetch_object($results)){
  199. $analyses[$analysis->analysis_id."-".$analysis->organism_id] = "$analysis->name";
  200. }
  201. # create the select box
  202. $form['tripal_analysis_go_select'] = array(
  203. '#title' => t('Select a GO report to view'),
  204. '#description' => t('Any analysis with GO results related to this organism are available for viewing. For further information, see the analysis information page.'),
  205. '#type' => 'select',
  206. '#options' => $analyses,
  207. '#attributes' => array (
  208. 'onchange' => 'tripal_analysis_go_org_charts(this.options[this.selectedIndex].value)'
  209. ),
  210. );
  211. return $form;
  212. }
  213. /************************************************************************
  214. */
  215. function tripal_analysis_go_org_charts ($element) {
  216. $analysis_id = preg_replace("/^(\d+)-(\d+)$/","$1",$element);
  217. $organism_id = preg_replace("/^(\d+)-(\d+)$/","$2",$element);
  218. $content = '';
  219. if($analysis_id and $organism_id){
  220. $content = "
  221. <b>Biological Process</b>
  222. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  223. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_bp\"></div>
  224. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_bp\" src=\"\" border=\"0\">
  225. <br><br><br><br>
  226. <b>Cellular Component</b>
  227. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  228. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_cc\"></div>
  229. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_cc\" src=\"\" border=\"0\">
  230. <br><br><br><br>
  231. <b>Molecular Function</b>
  232. <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
  233. <div class=\"tripal_cv_tree\" id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_mf\"></div>
  234. <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_mf\" src=\"\" border=\"0\">
  235. ";
  236. }
  237. $opt = array($content);
  238. return drupal_json($opt);
  239. }
  240. /************************************************************************
  241. */
  242. function tripal_analysis_go_load_organism_go_summary($node) {
  243. $organism = $node->organism;
  244. // check to see if we have any analyses
  245. $sql = "
  246. SELECT count(*) as cnt
  247. FROM {go_count_analysis} GCA
  248. INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
  249. WHERE organism_id = %d
  250. ";
  251. $previous_db = tripal_db_set_active('chado');
  252. $results = db_fetch_object(db_query($sql,$organism->organism_id));
  253. tripal_db_set_active($previous_db);
  254. $has_results = 0;
  255. if($results){
  256. $has_results = 1;
  257. }
  258. return array (
  259. 'has_results' => $has_results,
  260. 'form' => drupal_get_form('tripal_analysis_go_select_form',$node),
  261. );
  262. }
  263. /************************************************************************
  264. *
  265. */
  266. function tripal_analysis_go_cv_chart($chart_id){
  267. // The CV module will create the JSON array necessary for buillding a
  268. // pie chart using jgChart and Google Charts. We have to pass to it
  269. // a table that contains count information, tell it which column
  270. // contains the cvterm_id and provide a filter for getting the
  271. // results we want from the table.
  272. $organism_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$chart_id);
  273. $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  274. $type = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);
  275. $sql = "SELECT * FROM {Analysis} WHERE analysis_id = %d";
  276. $previous_db = tripal_db_set_active('chado'); // use chado database
  277. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  278. tripal_db_set_active($previous_db); // now use drupal database
  279. if(strcmp($type,'mf')==0){
  280. $class = 'molecular_function';
  281. $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  282. }
  283. if(strcmp($type,'cc')==0){
  284. $class = 'cellular_component';
  285. $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  286. }
  287. if(strcmp($type,'bp')==0){
  288. $class = 'biological_process';
  289. $title = "Number of Biological Process Terms From $analysis->name Analysis";
  290. }
  291. $options = array(
  292. count_mview => 'go_count_analysis',
  293. cvterm_id_column => 'cvterm_id',
  294. count_column => 'feature_count',
  295. filter => "
  296. CNT.organism_id = $organism_id AND
  297. CNT.analysis_id = $analysis_id AND
  298. CNT.cvterm_id IN (
  299. SELECT CVTR.subject_id
  300. FROM {CVTerm_relationship} CVTR
  301. INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
  302. INNER JOIN CV on CVT.cv_id = CV.cv_id
  303. WHERE CVT.name = '$class' AND
  304. CV.name = '$class'
  305. )
  306. ",
  307. type => 'p',
  308. size => '550x175',
  309. title => $title,
  310. );
  311. return $options;
  312. }
  313. /************************************************************************
  314. *
  315. */
  316. function tripal_analysis_go_cv_tree($tree_id){
  317. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  318. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  319. $type = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$tree_id);
  320. if(strcmp($type,'mf')==0){
  321. $class = 'molecular_function';
  322. }
  323. if(strcmp($type,'cc')==0){
  324. $class = 'cellular_component';
  325. }
  326. if(strcmp($type,'bp')==0){
  327. $class = 'biological_process';
  328. }
  329. $options = array(
  330. cv_id => tripal_cv_get_cv_id($class),
  331. count_mview => 'go_count_analysis',
  332. cvterm_id_column => 'cvterm_id',
  333. count_column => 'feature_count',
  334. filter => "CNT.organism_id = $organism_id AND CNT.analysis_id = $analysis_id",
  335. label => 'Features',
  336. );
  337. return $options;
  338. }
  339. /************************************************************************
  340. */
  341. function tripal_analysis_go_cvterm_add($cvterm_id,$tree_id){
  342. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  343. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  344. $sql = "
  345. SELECT DBX.accession
  346. FROM {cvterm} CVT
  347. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  348. WHERE cvterm_id = %d
  349. ";
  350. $previous_db = tripal_db_set_active('chado');
  351. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  352. tripal_db_set_active($previous_db);
  353. $link = url("download_goterm_features/$cvterm_id/$tree_id");
  354. $options = array(
  355. 'Download sequences' => "<a href=\"$link\">GO_".$xref->accession.".fasta</a>",
  356. );
  357. return $options;
  358. }
  359. /************************************************************************
  360. */
  361. function tripal_analysis_go_get_goterm_features($cvterm_id,$tree_id){
  362. // get hte accession number for this cvterm and use it in naming the download
  363. $sql = "
  364. SELECT DBX.accession
  365. FROM {cvterm} CVT
  366. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  367. WHERE cvterm_id = %d
  368. ";
  369. $previous_db = tripal_db_set_active('chado');
  370. $xref = db_fetch_object(db_query($sql,$cvterm_id));
  371. tripal_db_set_active($previous_db);
  372. drupal_set_header('Content-Type: text');
  373. drupal_set_header('Content-Disposition: attachment; filename="GO_'.$xref->accession.'.fasta"');
  374. $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  375. $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  376. $sql = "
  377. SELECT DISTINCT F.name,F.residues,F.feature_id
  378. FROM {cvtermpath} CVTP
  379. INNER JOIN CVTerm CVT1 on CVTP.subject_id = CVT1.cvterm_id
  380. INNER JOIN CVTerm CVT2 on CVTP.object_id = CVT2.cvterm_id
  381. INNER JOIN AnalysisFeatureProp AFP on AFP.type_id = CVTP.subject_id
  382. INNER JOIN AnalysisFeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
  383. INNER JOIN Feature F on AF.feature_id = F.feature_id
  384. WHERE CVTP.object_id = %d and F.organism_id = %d and AF.analysis_id = %d
  385. ORDER BY F.name
  386. ";
  387. $previous_db = tripal_db_set_active('chado');
  388. $results = db_query($sql,$cvterm_id,$organism_id,$analysis_id);
  389. tripal_db_set_active($previous_db);
  390. while($feature = db_fetch_object($results)){
  391. // get the go term information for each sequence
  392. $sql = "
  393. SELECT CVT.name,DBX.accession
  394. FROM {Feature_CVTerm} FCVT
  395. INNER JOIN CVTerm CVT on FCVT.cvterm_id = CVT.cvterm_id
  396. INNER JOIN DBXref DBX on CVT.dbxref_id = DBX.dbxref_id
  397. WHERE FCVT.feature_id = %d
  398. ";
  399. $previous_db = tripal_db_set_active('chado');
  400. $terms = db_query($sql,$feature->feature_id);
  401. tripal_db_set_active($previous_db);
  402. $desc = '[';
  403. while($term = db_fetch_object($terms)){
  404. $desc .= "GO:$term->accession $term->name; ";
  405. }
  406. $desc = chop($desc);
  407. $desc = chop($desc,';');
  408. $desc .= ']';
  409. print tripal_feature_return_fasta($feature, $desc);
  410. }
  411. return;
  412. }
  413. /************************************************************************
  414. */
  415. function tripal_analysis_go_load_feature_terms($feature) {
  416. $sql = "
  417. SELECT DISTINCT FCVT.feature_id,DBX.accession,CVT.name as goterm,
  418. CVT.cvterm_id as go_id, CV.name as cvname, CVT.definition
  419. FROM {Feature_Cvterm} FCVT
  420. INNER JOIN Cvterm CVT ON CVT.cvterm_ID = FCVT.cvterm_ID
  421. INNER JOIN CV ON CV.cv_id = CVT.cv_id
  422. INNER JOIN dbxref DBX ON DBX.dbxref_id = CVT.dbxref_id
  423. WHERE
  424. (CV.name = 'biological_process' OR
  425. CV.name = 'cellular_component' OR
  426. CV.name = 'molecular_function') AND
  427. FCVT.feature_id = %d
  428. ORDER BY CV.name, CVT.name
  429. ";
  430. $previous_db = tripal_db_set_active('chado');
  431. $results = db_query($sql,$feature->feature_id);
  432. tripal_db_set_active($previous_db);
  433. $i=0;
  434. $terms = array();
  435. while($term = db_fetch_object($results)){
  436. $terms[$i++] = $term;
  437. }
  438. return $terms;
  439. }
  440. /*******************************************************************************
  441. * Tripal GO administrative setting form. This function is called by
  442. * tripal_analysis module which asks for an admin form to show on the page
  443. */
  444. function tripal_analysis_go_get_settings() {
  445. // Get an array of node types with internal names as keys
  446. $options = node_get_types('names');
  447. // Add 'chado_feature' to allowed content types for showing unigene results
  448. $allowedoptions ['chado_feature'] = "Show GO terms on feature pages";
  449. $allowedoptions ['chado_organism'] = "Show GO analysis on organism pages";
  450. $form['description'] = array(
  451. '#type' => 'item',
  452. '#value' => t("This option allows user to display the Gene Ontology (GO) ".
  453. "information. For features, this would include all GO terms assigned to a feature ".
  454. "and for organisms this would be statistical pie charts of GO terms for a organism. Check the box to ".
  455. "enable the display of GO information. Uncheck to disable."),
  456. '#weight' => 0,
  457. );
  458. $form['tripal_analysis_go_setting'] = array(
  459. '#type' => 'checkboxes',
  460. '#options' => $allowedoptions,
  461. '#default_value'=>variable_get('tripal_analysis_go_setting',array('chado_feature', 'chado_organism')),
  462. );
  463. $settings->form = $form;
  464. $settings->title = "Tripal GO";
  465. return $settings;
  466. }