<?php
//$Id:

/*******************************************************************************
 * Implementation of hook_install().
 */
function tripal_analysis_blast_install() {
   // create the module's data directory
   tripal_create_moddir('tripal_analysis_blast');

   // We need to register to tripal_analysis module so it can provide a control
   // for our blast result. Basically the registration is done by inserting
   // modulename into the drupal {tripal_analysis} table AND inserting required
   // information to the chado Analysis table. Also in tripal_analysis_blast.module,
   // we need to define HOOK_get_settings() for the module to work properly.   
   // Register the analysis type
   tripal_analysis_register_child('tripal_analysis_blast');
   
   // Add cvterms
   tripal_analysis_blast_add_cvterms();
   
   // Create a tripal_analysis_blast table to store parsers
   drupal_install_schema('tripal_analysis_blast');
   
   // Create default parser for swissprot, DB:genbank, and go-seqdb
   $sql_db = "SELECT db_id, name FROM {db} WHERE name like '%s'";
   $sql_parser = "INSERT INTO {tripal_analysis_blast} ".
                 "  (db_id, displayname, regex_hit_id, regex_hit_def, regex_hit_accession, genbank_style) ".
                 "VALUES (%d, '%s', '%s', '%s', '%s', %d)";
   
   // Add swissprot parser
   $previous_db = tripal_db_set_active ('chado');
   $results = db_query($sql_db, "%swissprot%");
   tripal_db_set_active($previous_db);
   while ($db = db_fetch_object($results)) {
      db_query($sql_parser, $db->db_id, 'ExPASy Swissprot', '^sp\|.*?\|(.*?)\s.*?$', '^sp\|.*?\|.*?\s(.*)$', 'sp\|(.*?)\|.*?\s.*?$', 0);
   }
   
   // Add trembl parser
   $previous_db = tripal_db_set_active ('chado');
   $results = db_query($sql_db, "%trembl%");
   tripal_db_set_active($previous_db);
   while ($db = db_fetch_object($results)) {
      db_query($sql_parser, $db->db_id, 'ExPASy TrEMBL', '^.*?\|(.*?)\s.*?$', '^.*?\|.*?\s(.*)$', '^(.*?)\|.*?\s.*?$', 0);
   }
   
   // Add NCBI genbank parser
   $previous_db = tripal_db_set_active ('chado');
   $results = db_query($sql_db, "%genbank%");
   tripal_db_set_active($previous_db);
   while ($db = db_fetch_object($results)) {
      db_query($sql_parser, $db->db_id, 'Genbank', '', '', '', 1);
   }
}

/*******************************************************************************
 * Implementation of hook_uninstall().
 */
function tripal_analysis_blast_uninstall() {
   
   // Delete all information associate with the module
   // Drupal complains when the user tries to uninstall tripal_analysis 
   // and tripal_analysis_blast at the same time. This is because Drupal drops
   // the {tripal_analysis} table before we can delete anything from it. Thus,
   // we perform a db_table_exists() check before the deletion
   
   //Delete the settings from {tripal_analysis} table
   tripal_analysis_unregister_child('tripal_analysis_blast');

   // Delete module's variables from variables table.
   db_query("DELETE FROM {variable} WHERE name='%s'",
            'tripal_analysis_blast_setting');
            
   // Delete a tripal_analysis_blast table
   drupal_uninstall_schema('tripal_analysis_blast');
}

/*******************************************************************************
* Implementation of hook_schema(). This table stores the parsers for blast xml
* xml results.
*/
function tripal_analysis_blast_schema() {
   $schema = array();
   $schema['tripal_analysis_blast'] = array(
      'fields' => array(
         'db_id' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0
         ),
         'displayname' => array(
            'type' => 'varchar',
            'length' => 100,
            'not null' => TRUE,
         ),
         'regex_hit_id' => array(
            'type' => 'varchar',
            'length' => 30,
         ),
         'regex_hit_def' => array(
            'type' => 'varchar',
            'length' => 30,
         ),
         'regex_hit_accession' => array(
            'type' => 'varchar',
            'length' => 30,
         ),
         'genbank_style' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'default' => 0
         ),
      ),
      'indexes' => array(
         'db_id' => array('db_id')
      ),
      'primary key' => array('db_id'),
  );
  return $schema;
}

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

/*******************************************************************************
 * Provide update script for adding new cvterms
 */
function tripal_analysis_blast_update_6000(){

   // we have some new cvterms to add
   tripal_analysis_blast_add_cvterms();

   $ret = array(
      '#finished' => 1,
   );
   
   return $ret;
}

/*******************************************************************************
 * Function for adding cvterms
 */
function tripal_analysis_blast_add_cvterms () {
	
   // Add the cvterms for the anlysisprop table
   tripal_add_cvterms('analysis_blast_settings', 'Settings of a blast analysis, '.
      'including db_id, output file, and run parameters separated by a bar |');
   tripal_add_cvterms('analysis_blast_blastdb','The database used for blasting');
   tripal_add_cvterms('analysis_blast_blastfile','The input file or directory that contains the XML blast output');
   tripal_add_cvterms('analysis_blast_blastparameters','The parameters used when performing blast.');
   tripal_add_cvterms('analysis_blast_no_parsed','Indicates if the input file should be parsed to map results to features');
   tripal_add_cvterms('analysis_blast_query_re','The regular expression for finding the feature name in the query definition line of the blast results');
   tripal_add_cvterms('analysis_blast_query_type','The feature type (e.g. mRNA, polypeptide) of the query input file. This type is used to identify the query feature when multiple features have the same name');
   tripal_add_cvterms('analysis_blast_query_uniquename','Indicates if the matched name in the query definition line of the blast results is feature uniquename');
   tripal_add_cvterms('analysis_blast_blastfile_ext','Indicates the extension of the blast files. This is required if the blastfile setting is a directory.');
   tripal_add_cvterms('analysis_blast_is_concat','Indicates if the blast results XML file is a series of concatenated XML results.  Such is the case, for example, if Blast2GO was used to perform the analysis.');

   // Add cvterm 'analysis_blast_output_iteration_hits' for inserting into featureprop table
   tripal_add_cvterms('analysis_blast_output_iteration_hits', 'Hits of a blast '.
      'output iteration. Each iteration corresponds to a chado feature, and is '.
      'the content between <iteration> and </iteration> tags in the blast xml '.
      'output file. This cvterm represents all hits in the iteration');
   
   tripal_add_cvterms('analysis_blast_besthit_query', 'query name of the best hit 
   associated with a feature and a blast analysis ');

   tripal_add_cvterms('analysis_blast_besthit_match', 'match name of the best hit 
   associated with a feature and a blast analysis ');
    
   tripal_add_cvterms('analysis_blast_besthit_description', 'description of the best hit 
   associated with a feature and a blast analysis ');
   
   tripal_add_cvterms('analysis_blast_besthit_evalue', 'evalue of the best hit 
   associated with a feature and a blast analysis ');
    
   tripal_add_cvterms('analysis_blast_besthit_identity', 'identity (%) of the best hit 
   associated with a feature and a blast analysis ');
   
   tripal_add_cvterms('analysis_blast_besthit_length', 'length of the best hit 
   associated with a feature and a blast analysis ');
   
}