<?php

require_once "gaf_loader.inc";

function tripal_analysis_go_init(){
   // Add style sheet
   drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_go.css');

   drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_go.js');
}

/*******************************************************************************
 * Menu items are automatically added for the new node types created
 * by this module to the 'Create Content' Navigation menu item.  This function
 * adds more menu items needed for this module.
 */
function tripal_analysis_go_menu() {
   $items = array();

   $items['download_goterm_features'] = array(
      'path' => 'download_goterm_features',
      'title' => t('Get GO Term Features'),
      'page callback' => 'tripal_analysis_go_get_goterm_features',
      'page arguments' => array(1,2),
      'access arguments' => array('access chado_analysis_go content'),
      'type' => MENU_CALLBACK
   );
   $items['tripal_analysis_go_org_charts'] = array(
      'path' => 'tripal_analysis_go_org_charts',
      'title' => t('Analysis GO Charts'),
      'page callback' => 'tripal_analysis_go_org_charts',
      'page arguments' => array(1),
      'access arguments' => array('access chado_analysis_go content'),
      'type' => MENU_CALLBACK
   );
   $items['admin/tripal/tripal_analysis_go'] = array(
     'title' => 'Gene Ontology',
     'description' => 'Administrative tools for managing Gene Ontology data.',
     'page callback' => 'tripal_analysis_go_module_description_page',
     'access arguments' => array('administer site configuration'),
     'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_analysis_go/gaf_load'] = array(
     'title' => t('Import GO terms with GAF file'),
     'description' => t("Import GO terms into Chado using the Gene Ontology's GAF 2.0 file format"),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_analysis_go_gaf_load_form'),
     'access arguments' => array('access administration pages'),
     'type' => MENU_NORMAL_ITEM,
   );

   return $items;
}
/*************************************************************************
 * Purpose: Provide Guidance to new Tripal Admin
 *
 * @return HTML Formatted text
 */
function tripal_analysis_go_module_description_page() {
  $text = '';
  
  $text .= '<h3>Description:</h3>';
  $text .= '<p>TODO: Basic Description of this module including mention/link to the chado module</p>';

  $text .= '<h3>Post Installation Instructions:</h3>';
  $text .= '<p>TODO: Describe any post installation intructions here. You shouldalways include setting user permissions.</p>';
  
  
  $text .= '<h3>Features of this Module:</h3>';
  $text .= '<p>TODO: Discuss the Features of this module including links. Some features to consider are creating content, details pages/node content, editing/deleteing, basic listings and vies integration. See admin/tripal/tripal_stock for an example.</p>';
  
  return $text;
}
/*******************************************************************************
 * Set the permission types that the chado module uses.  Essentially we
 * want permissionis that protect creation, editing and deleting of chado
 * data objects
 */
function tripal_analysis_go_perm(){
   return array(
      'access chado_analysis_go content',
      'create chado_analysis_go content',
      'delete chado_analysis_go content',
      'edit chado_analysis_go content',
   );
}
/*******************************************************************************
 *  The following function proves access control for users trying to
 *  perform actions on data managed by this module
 */
function chado_analysis_go_access($op, $node, $account){
   if ($op == 'create') {
      return user_access('create chado_analysis_go content', $account);
   }

   if ($op == 'update') {
      if (user_access('edit chado_analysis_go content', $account)) {
         return TRUE;
      }
   }
   if ($op == 'delete') {
      if (user_access('delete chado_analysis_go content', $account)) {
         return TRUE;
      }
   }
   if ($op == 'view') {
      if (user_access('access chado_analysis_go content', $account)) {
         return TRUE;
      }
   }
   return FALSE;
}
/*******************************************************************************
 *
 */
function tripal_analysis_go_block($op = 'list', $delta = 0, $edit=array()){
   switch($op) {
      case 'list':
         $blocks['tago_org_sum']['info'] = t('Tripal Organism GO Analysis Report');
         $blocks['tago_org_sum']['cache'] = BLOCK_NO_CACHE;

         $blocks['featurego']['info'] = t('Tripal Feature GO Terms');
         $blocks['featurego']['cache'] = BLOCK_NO_CACHE;

         return $blocks;


      case 'view':
         if(user_access('access chado_analysis_go content') and arg(0) == 'node' and is_numeric(arg(1))) {
            $nid = arg(1);
            $node = node_load($nid);
 
            $block = array();
            switch($delta){
                 case 'tago_org_sum':
                  $block['subject'] = t('GO Summary');
                  $block['content'] = theme('tripal_organism_go_summary',$node);
                  break;
               case 'featurego':
                  $block['subject'] = t('GO Terms');
                  $block['content'] = theme('tripal_feature_go_terms',$node);
                  break;
               default :
            }
            return $block;
         }

   }
}
/*******************************************************************************
 * HOOK: Implementation of hook_nodeapi()
 * Display library information for associated features or organisms
 * This function also provides contents for indexing
 */
function tripal_analysis_go_nodeapi(&$node, $op, $teaser, $page) {

   switch ($op) {
      case 'view':
     
         if ($teaser) {
            return;
         }
         // add the library to the organism/feature search indexing
         if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
            $node->content['tripal_analysis_go_search_index'] = array(
					'#value' => theme('tripal_analysis_go_search_index',$node),
					'#weight' => 5,
            );
         } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
            $node->content['tripal_analysis_go_search_result'] = array(
					'#value' => theme('tripal_analysis_go_search_result',$node),
					'#weight' => 5,
            );
         } else {
            // Show go terms if the organism/feature is not at teaser view
            switch($node->type){
               case 'chado_organism':    
                  $node->content['tripal_organism_go_summary'] = array(
   				      '#value' => theme('tripal_organism_go_summary',$node),
		      			'#weight' => 5,
                  );
                  break;
               case 'chado_feature':
                  $node->content['tripal_feature_go_terms'] = array(
				         '#value' => theme('tripal_feature_go_terms',$node),
      					'#weight' => 5,
                  );
                  break;
            }
         }
         break;
   }
}

/************************************************************************
 *  We need to let drupal know about our theme functions and their arguments.
 *  We create theme functions to allow users of the module to customize the
 *  look and feel of the output generated in this module
 */
function tripal_analysis_go_theme () {
   return array(
   	  
      'tripal_analysis_go_search_index' => array (
         'arguments' => array('node'),
      ),
      'tripal_analysis_go_search_result' => array (
         'arguments' => array('node'),
      ),
      'tripal_organism_go_summary' => array (
         'arguments' => array('node'=> null),
         'template' => 'tripal_organism_go_summary',
      ),
      'tripal_feature_go_terms' => array (
         'arguments' => array('node'=> null),
         'template' => 'tripal_feature_go_terms',
      )
   );
}
/************************************************************************
*/
function theme_tripal_analysis_go_search_index($node){

}

/************************************************************************
*/
function theme_tripal_analysis_go_search_result($node){

}
/************************************************************************
*/
function tripal_analysis_go_preprocess_tripal_feature_go_terms(&$variables){
   $feature = $variables['node']->feature;
   $feature->tripal_analysis_go->terms = tripal_analysis_go_load_feature_terms($feature);
}
/************************************************************************
*/
function tripal_analysis_go_preprocess_tripal_organism_go_summary(&$variables){
   $node = $variables['node'];
   $organism = $node->organism;
   $organism->tripal_analysis_go->select_form = tripal_analysis_go_load_organism_go_summary($node);
}
/************************************************************************
*/
function tripal_analysis_go_select_form(&$form_state = NULL,$node){

   $form = array();
   // find analyses that have GO terms
   $sql = "
     SELECT DISTINCT A.analysis_id, A.name, GCA.organism_id
     FROM {go_count_analysis} GCA
       INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
     WHERE organism_id = %d
     ORDER BY analysis_id DESC
   ";
   $previous_db = tripal_db_set_active('chado');
   $results = db_query($sql,$node->organism->organism_id);
   tripal_db_set_active($previous_db);

   $analyses = array();
   $analyses[''] = '';
   while($analysis = db_fetch_object($results)){
      $analyses[$analysis->analysis_id."-".$analysis->organism_id] = "$analysis->name";
   }
  
   # create the select box
   $form['tripal_analysis_go_select'] = array(
      '#title' => t('Select a GO report to view'),
      '#description' => t('Any analysis with GO results related to this organism are available for viewing. For further information, see the analysis information page.'),
      '#type'  => 'select',
      '#options' => $analyses,
      '#attributes' => array (
         'onchange' => 'tripal_analysis_go_org_charts(this.options[this.selectedIndex].value)'
      ),
   );
   return $form;
}
/************************************************************************
*/
function tripal_analysis_go_org_charts ($element) {
   $analysis_id = preg_replace("/^(\d+)-(\d+)$/","$1",$element);
   $organism_id = preg_replace("/^(\d+)-(\d+)$/","$2",$element);
   $content = '';

   if($analysis_id and $organism_id){
      $content = "

        <b>Biological Process</b>
        <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
        <div class=\"tripal_cv_tree\"  id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_bp\"></div>
	<br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_bp\" src=\"\" border=\"0\">	   
	<br><br><br><br>

        <b>Cellular Component</b>
        <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
        <div class=\"tripal_cv_tree\"  id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_cc\"></div>
        <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_cc\" src=\"\" border=\"0\">

        <br><br><br><br>
        <b>Molecular Function</b>       
        <br><i>Expand the tree to browse term counts. Click a term to view term details.</i>
        <div class=\"tripal_cv_tree\"  id=\"tripal_analysis_go_cv_tree_".$organism_id."-".$analysis_id."_mf\"></div>      
        <br><br><img class=\"tripal_cv_chart\" id=\"tripal_analysis_go_cv_chart_".$organism_id."-".$analysis_id."_mf\" src=\"\" border=\"0\">
      ";
   }
   $opt = array($content);
   return drupal_json($opt);
}
/************************************************************************
*/
function tripal_analysis_go_load_organism_go_summary($node) {
   $organism = $node->organism;

   // check to see if we have any analyses
   $sql = "
     SELECT count(*) as cnt
     FROM {go_count_analysis} GCA
       INNER JOIN Analysis A on GCA.analysis_id = A.analysis_id
     WHERE organism_id = %d
   ";
   $previous_db = tripal_db_set_active('chado');
   $results = db_fetch_object(db_query($sql,$organism->organism_id));
   tripal_db_set_active($previous_db);

   $has_results = 0;
   if($results->cnt > 0){
      $has_results = 1;
   }
   return array (
      'has_results' => $has_results,
      'form' => drupal_get_form('tripal_analysis_go_select_form',$node),
   );
}
/************************************************************************
 *
 */
function tripal_analysis_go_cv_chart($chart_id){

  // The CV module will create the JSON array necessary for buillding a
  // pie chart using jgChart and Google Charts.  We have to pass to it
  // a table that contains count information, tell it which column 
  // contains the cvterm_id and provide a filter for getting the
  // results we want from the table.
  $organism_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$chart_id);
  $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  $type        = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);

  $sql = "SELECT * FROM {Analysis} WHERE analysis_id = %d";
  $previous_db = tripal_db_set_active('chado');  // use chado database
  $analysis = db_fetch_object(db_query($sql,$analysis_id));
  tripal_db_set_active($previous_db);  // now use drupal database  
 
  if(strcmp($type,'mf')==0){
     $class = 'molecular_function';
     $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  }
  if(strcmp($type,'cc')==0){
     $class = 'cellular_component';
     $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  }
  if(strcmp($type,'bp')==0){
     $class = 'biological_process';
     $title = "Number of Biological Process Terms From $analysis->name Analysis";
  }
  $options = array(
     count_mview      => 'go_count_analysis',
     cvterm_id_column => 'cvterm_id',
     count_column     => 'feature_count',
     filter           => "
        CNT.organism_id = $organism_id AND 
        CNT.analysis_id = $analysis_id AND 
        CNT.cvterm_id IN ( 
          SELECT CVTR.subject_id 
          FROM {CVTerm_relationship} CVTR 
            INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
            INNER JOIN CV on CVT.cv_id = CV.cv_id
          WHERE CVT.name = '$class' AND  
                 CV.name = '$class'
        )
     ",
     type             => 'p',
     size             => '550x175',
     title            => $title,
  );
  return $options;
}
/************************************************************************
 *
 */
function tripal_analysis_go_cv_tree($tree_id){

  $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  $type        = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$tree_id);

  if(strcmp($type,'mf')==0){
     $class = 'molecular_function';
  }
  if(strcmp($type,'cc')==0){
     $class = 'cellular_component';
  }
  if(strcmp($type,'bp')==0){
     $class = 'biological_process';
  }

  $options = array(
     cv_id            => tripal_cv_get_cv_id($class),
     count_mview      => 'go_count_analysis',
     cvterm_id_column => 'cvterm_id',
     count_column     => 'feature_count',
     filter           => "CNT.organism_id = $organism_id AND CNT.analysis_id = $analysis_id",
     label            => 'Features',
  );
  return $options;
}
/************************************************************************
*/
function tripal_analysis_go_cvterm_add($cvterm_id,$tree_id){
  $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);

  $sql = "
     SELECT DBX.accession
     FROM {cvterm} CVT
       INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
     WHERE cvterm_id = %d
  ";
  $previous_db = tripal_db_set_active('chado');
  $xref = db_fetch_object(db_query($sql,$cvterm_id));
  tripal_db_set_active($previous_db);

  $link = url("download_goterm_features/$cvterm_id/$tree_id");
  $options = array(
     'Download sequences' => "<a href=\"$link\">GO_".$xref->accession.".fasta</a>",
  );
  return $options;
}
/************************************************************************
*/
function tripal_analysis_go_get_goterm_features($cvterm_id,$tree_id){

   // get hte accession number for this cvterm and use it in naming the download
   $sql = "
      SELECT DBX.accession
      FROM {cvterm} CVT
        INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
      WHERE cvterm_id = %d
   ";
   $previous_db = tripal_db_set_active('chado');
   $xref = db_fetch_object(db_query($sql,$cvterm_id));
   tripal_db_set_active($previous_db);

   drupal_set_header('Content-Type: text');
   drupal_set_header('Content-Disposition: attachment; filename="GO_'.$xref->accession.'.fasta"');

   $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
   $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
   $sql = "
      SELECT DISTINCT F.name,F.residues,F.feature_id
      FROM {cvtermpath} CVTP
         INNER JOIN CVTerm CVT1 on CVTP.subject_id = CVT1.cvterm_id
         INNER JOIN CVTerm CVT2 on CVTP.object_id = CVT2.cvterm_id
         INNER JOIN AnalysisFeatureProp AFP on AFP.type_id = CVTP.subject_id 
         INNER JOIN AnalysisFeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
         INNER JOIN Feature F on AF.feature_id = F.feature_id
      WHERE CVTP.object_id = %d and F.organism_id = %d and AF.analysis_id = %d
      ORDER BY F.name
   ";
   $previous_db = tripal_db_set_active('chado');
   $results = db_query($sql,$cvterm_id,$organism_id,$analysis_id);
   tripal_db_set_active($previous_db);
   while($feature = db_fetch_object($results)){
      // get the go term information for each sequence
      $sql = "
         SELECT CVT.name,DBX.accession 
         FROM {Feature_CVTerm} FCVT 
           INNER JOIN CVTerm CVT on FCVT.cvterm_id = CVT.cvterm_id
           INNER JOIN DBXref DBX on CVT.dbxref_id = DBX.dbxref_id
         WHERE FCVT.feature_id = %d
      ";

      $previous_db = tripal_db_set_active('chado');
      $terms = db_query($sql,$feature->feature_id);
      tripal_db_set_active($previous_db);
      $desc = '[';
      while($term = db_fetch_object($terms)){
         $desc .= "GO:$term->accession $term->name; ";
      }
      $desc = chop($desc);
      $desc = chop($desc,';');
      $desc .= ']';
      print tripal_feature_return_fasta($feature, $desc);   
   }
   return;
}
/************************************************************************
*/
function tripal_analysis_go_load_feature_terms($feature) {
   $sql = "
       SELECT DISTINCT FCVT.feature_id,DBX.accession,CVT.name as goterm, 
          CVT.cvterm_id as go_id, CV.name as cvname, CVT.definition
       FROM {Feature_Cvterm} FCVT  
          INNER JOIN Cvterm CVT           ON CVT.cvterm_ID = FCVT.cvterm_ID
          INNER JOIN CV                   ON CV.cv_id = CVT.cv_id  
          INNER JOIN dbxref DBX           ON DBX.dbxref_id = CVT.dbxref_id
        WHERE      
          (CV.name = 'biological_process' OR 
           CV.name = 'cellular_component' OR 
           CV.name = 'molecular_function') AND 
          FCVT.feature_id = %d
        ORDER BY CV.name, CVT.name
   ";

   $previous_db = tripal_db_set_active('chado');
   $results = db_query($sql,$feature->feature_id);
   tripal_db_set_active($previous_db);
   $i=0;
   $terms = array();
   while($term = db_fetch_object($results)){

      $terms[$i++] = $term;
   }
   return $terms;
}

/*******************************************************************************
 * Tripal GO administrative setting form. This function is called by
 * tripal_analysis module which asks for an admin form to show on the page
 */
function tripal_analysis_go_get_settings() {
   // Get an array of node types with internal names as keys
   $options = node_get_types('names');
   // Add 'chado_feature' to allowed content types for showing unigene results
   $allowedoptions ['chado_feature'] = "Show GO terms on feature pages";
   $allowedoptions ['chado_organism'] = "Show GO analysis on organism pages";

   $form['description'] = array(
       '#type' => 'item',
       '#value' => t("This option allows user to display the Gene Ontology (GO) ".
          "information. For features, this would include all GO terms assigned to a feature ".
          "and for organisms this would be statistical pie charts of GO terms for a organism. Check the box to ".
          "enable the display of GO information. Uncheck to disable."),
       '#weight' => 0,
   );

   $form['tripal_analysis_go_setting'] = array(
      '#type' => 'checkboxes',
      '#options' => $allowedoptions,
      '#default_value'=>variable_get('tripal_analysis_go_setting',array('chado_feature', 'chado_organism')),
   );
   $settings->form = $form;
   $settings->title = "Tripal GO";
   return $settings;
}
/**
 *
 *
 * @ingroup tripal_feature
 */
function tripal_analysis_go_job_describe_args($callback,$args){

   $new_args = array();
   if($callback == 'tripal_analysis_go_load_gaf'){
      
      $new_args['GAF 2.0 file'] = $args[0];

      $organism = tripal_core_chado_select('organism',array('genus','species'),array('organism_id' => $args[1]));
      $new_args['Organism'] = $organism[0]->genus." ". $organism[0]->species;
      $new_args['Sequence Type'] = $args[8];

      // add in the analysis 
      if($args[2]){
         $analysis = tripal_core_chado_select('analysis',array('name'),array('analysis_id' => $args[2]));
      }
      $new_args['Analysis'] = $analysis[0]->name;

      if($args[3]){
        $new_args['Function to perform'] = 'Add GO terms';
      }
      if($args[4]){
        $new_args['Function to perform'] = 'Replace GO terms';
      }
      if($args[5]){
        $new_args['Function to perform'] = 'Delete GO terms';
      }

      $new_args['Regular expression for the feature name'] = $args[6];
      $new_args['Regular expression for the feature unique name'] = $args[7];


   }
   return $new_args;
}