<?php

/**
 * @file
 * Contains hooks to handle installation of this module.
 *
 * Specifically, a database table (blastdb) is created to store additional information
 * related to blast database nodes such as the name/path to the NCBI BLAST database files
 * and the type (protein or nucleotide) of the database.
 */


/**
 * Implements hook_install().
 */
function blast_ui_install() {
   tripal_create_files_dir('tripal_blast');
}

/**
 * Implements hook_schema().
 * Create the blastdb database table for storing addditional info related to blastdb nodes.
 *
 * NOTE: This hook is called via Drupal magic during the installation process and no longer
 * needs to be called explicitly in hook_install().
 */
function blast_ui_schema(){

  $schema['blastdb'] = array(
    'description' => t('The base table for blastdb node'),
    'fields' => array(
      'nid' => array(
        'description' => t('The primary identifier for a node.'),
        'type' => 'serial',
        'unsigned' => true,
        'not null' => true,
      ),
      'name' => array(
        'description' => t('The human-readable name of the blast database.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => true,
      ),
      'path' => array(
        'description' => t('The full path and filename prefix of the blast database.'),
        'type' => 'varchar',
        'length' => 1023,
        'not null' => true,
      ),
      'dbtype' => array(
        'description' => t('Type of the blast database. Should be either n for nucleotide or p for protein.'),
        'type' => 'varchar',
        'length' => 15,
        'not null' => true,
      ),
      'dbxref_id_regex' => array(
        'description' => t('The Regular Expression to use to extract the id from the FASTA header of the BLAST database hit.'),
        'type' => 'text',
      ),
      'dbxref_db_id' => array(
        'description' => t('The Database records from this BLAST Database reference.'),
        'type' => 'int',
      ),
    ),
    'indexes' => array(
      'name' => array('name'),
    ),
    'primary key' => array('nid'),
    'unique keys' => array(
       'nid' => array('nid'),
    ),
  );

  return $schema;

}

/**
 * Make BlastDB type more readable & support Link-outs for BLAST Hits.
 */
function blast_ui_update_7101() {

  // Changing the length of the type field to allow it to be more readable.
  db_change_field('blastdb', 'dbtype', 'dbtype',
    array(
        'description' => t('Type of the blast database. Should be either n for nucleotide or p for protein.'),
        'type' => 'varchar',
        'length' => 15,
        'not null' => true,
    )
  );

  // Add fields related to Link-outs
  db_add_field(
    'blastdb',
    'dbxref_id_regex',
    array(
      'description' => t('The Regular Expression to use to extract the id from the FASTA header of the BLAST database hit.'),
      'type' => 'text',
    )
  );
  db_add_field(
    'blastdb',
    'dbxref_db_id',
    array(
      'description' => t('The Database records from this BLAST Database reference.'),
      'type' => 'int',
    )
  );

}