<?php

/*******************************************************************************
*  Implementation of hook_install();
*/
function tripal_analysis_go_install(){
  
   tripal_analysis_register_child('tripal_analysis_go');
   
   // The following view will create counts of features that are assigned
   // either directly or by ancestry each GO term. The count is organized by
   // organisms.
   $previous_db = tripal_db_set_active('chado');
   if (db_table_exists('go_count_organism')) {
      $sql = "DROP TABLE go_count_organism";
      db_query($sql);
   }
   if (db_table_exists('go_count_analysis')) {
      $sql = "DROP TABLE go_count_analysis";
      db_query($sql);
   }
   tripal_db_set_active($previous_db);
   
   tripal_add_mview(
      // view name
      'go_count_organism',
      // tripal module name
      'tripal_analysis_go',
      // table name
      'go_count_organism',
      // table schema definition
      'cvname character varying(255),
       cvterm_id integer,
       organism_id integer,
       feature_count integer',
      // columns for indexing
      'cvterm_id,organism_id',
      // SQL statement to populate the view
      "SELECT T1.cvname, T1.object_id, T1.organism_id, count(T1.feature_id)
       FROM
         (SELECT DISTINCT CVT3.name as cvname, CVTP.object_id, 
            O.organism_id, F.feature_id
          FROM {cvtermpath} CVTP
            INNER JOIN CVTerm CVT          ON CVTP.subject_id = CVT.cvterm_id
            INNER JOIN CVTerm CVT2         ON CVTP.type_id = CVT2.cvterm_id
            INNER JOIN CVTerm CVT3         ON CVTP.object_id = CVT3.cvterm_id
            INNER JOIN Feature_cvterm FCVT ON FCVT.cvterm_id = CVT.cvterm_id
            INNER JOIN Feature F           ON FCVT.feature_id = F.feature_id
            INNER JOIN CV                  ON CV.cv_id = CVT.cv_id
            INNER JOIN Organism O          ON O.organism_id = F.organism_id
          WHERE (CV.name = 'biological_process' or 
                 CV.name = 'molecular_function' or 
                 CV.name = 'cellular_component')
         ) as T1
       GROUP BY T1.cvname,T1.object_id,T1.organism_id",
      // special index
      ''
   );

   // The following view will create counts of features that are assigned
   // either directly or by ancestry each GO term. The count is organized by
   // tripal_go_analysis analyses.
   tripal_add_mview(
      // view name
      'go_count_analysis',
      // tripal module name
      'tripal_analysis_go',
      // table name
      'go_count_analysis',
      // table schema definition
      'cvname character varying(255),
       cvterm_id integer,
       analysis_id integer,
       organism_id integer,
       feature_count integer',
      // columns for indexing
      'cvterm_id,analysis_id,organism_id',
      // SQL statement to populate the view
      "SELECT T1.cvname,T1.cvterm_id,T1.analysis_id,T1.organism_id,count(*) as feature_count 
       FROM
        (SELECT DISTINCT AF.analysis_id, AF.feature_id,CVT.name as cvname,
           CVT.cvterm_id,CVTP.object_id,CV.name, F.organism_id 
         FROM {analysisfeatureprop} AFP
           INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id
           INNER JOIN feature F          ON AF.feature_id = F.feature_id
           INNER JOIN cvtermpath CVTP    ON CVTP.subject_id = AFP.type_id
           INNER JOIN cvterm CVT         ON CVTP.object_id = CVT.cvterm_id
           INNER JOIN CV                 ON CV.cv_id = CVT.cv_id

         WHERE  
           (CV.name = 'biological_process' or 
            CV.name = 'molecular_function' or 
            CV.name = 'cellular_component')) as T1
       GROUP BY T1.cvname,T1.cvterm_id,T1.analysis_id,T1.organism_id",
      // special index
      ''
   );
}

/*******************************************************************************
* Implementation of hook_uninstall()
*/
function tripal_analysis_go_uninstall(){

   $mview = tripal_mviews_get_mview_id('go_count_organism');
   if($mview){
      tripal_mviews_action('delete',$mview);
   }
   $mview = tripal_mviews_get_mview_id('go_count_analysis');
   if($mview){
      tripal_mviews_action('delete',$mview);
   }
   tripal_analysis_unregister_child('tripal_analysis_go');

}

/*******************************************************************************
 * Implementation of hook_requirements(). Make sure 'Tripal Core' and 'Tripal
 * Analysis' are enabled before installation
 */
function tripal_analysis_go_requirements($phase) {
   $requirements = array();
   if ($phase == 'install') {
      if (!function_exists('tripal_create_moddir') || !function_exists('tripal_analysis_register_child')) {
         $requirements ['tripal_analysis_go'] = array(
            'title' => "tripal_analysis_go",
            'value' => "error. Some required modules are just being installed. Please try again.",
            'severity' => REQUIREMENT_ERROR,
         );
      }
   }
   return $requirements;
}