<?php

/**
 * @file
 * The main file for the blast UI module.
 */

// BLAST Submission Forms
require_once 'includes/blast_ui.form_common.inc';
require_once 'includes/blast_ui.form_advanced_options.inc';
// NOTE: The forms themeselves are included using hook_menu to ensure they
// are only inlcuded when needed.

// BLAST DB Node functionality
require_once 'includes/blast_ui.node.inc';

// BLAST Link-out functionality.
require_once 'includes/blast_ui.linkouts.inc';

// Functions specific to themeing (ie: preprocess)
require_once 'theme/blast_ui.theme.inc';

// Application Programmers Interface
require_once 'api/blast_ui.api.inc';

// Administration
require_once 'includes/blast_ui.admin.inc';

/**
 * Implements hook_menu().
 */
function blast_ui_menu() {

  // BLAST DB Node
  $items['node__blastdb'] = array(
    'template' => 'node--blastdb',
    'render element' => 'node',
    'base hook' => 'node',
    'path' => 'theme',
  );

  // Single All-in-One BLAST submission form
  $items['blast'] = array(
    'title' => 'BLAST',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_all_in_one_form'),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_single.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  // Per Query Type BLAST submission forms
  $items['blast/nucleotide'] = array(
    'title' => 'Nucleotide Query',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_query_type_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_query_type.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  $items['blast/protein'] = array(
    'title' => 'Protein Query',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_query_type_form', 1),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_query_type.inc',
    'type' => MENU_NORMAL_ITEM,
    'expanded' => TRUE,
  );

  // Per BLAST-program submission forms
  $items['blast/nucleotide/nucleotide'] = array(
    'title' => 'BLASTn',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/nucleotide/protein'] = array(
    'title' => 'BLASTx',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/protein/nucleotide'] = array(
    'title' => 'tBLASTn',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  $items['blast/protein/protein'] = array(
    'title' => 'BLASTp',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_per_blast_program_form', 1, 2),
    'access arguments' => array('access content'),
    'file' => 'includes/blast_ui.form_per_program.inc',
    'type' => MENU_NORMAL_ITEM
  );

  // BLAST Results page
  $items['blast/report/%'] = array(
    'title' => 'BLAST Results',
    'page callback' => 'show_blast_output',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  // BLAST Admin
  $items['admin/tripal/extension/tripal_blast'] = array(
    'title' => 'Tripal BLAST User Interface',
    'description' => 'Provides an interface allowing users to execute their own BLASTs.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('blast_ui_admin_form'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/tripal/extension/tripal_blast/blast_ui'] = array(
    'title' => 'BLAST UI',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  $items['admin/tripal/extension/tripal_blast/help'] = array(
    'title' => 'Help',
    'page callback' => 'theme',
    'page arguments' => array('blast_help'),
    'access arguments' => array('administer tripal'),
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

/**
 * Implements hook_theme().
 */
function blast_ui_theme() {

  $items = array();
  $path = drupal_get_path('module', 'blast_ui');

  // Displays the BLAST results for each job
  $items['show_blast_report'] = array(
    'template' => 'blast_report',
    'path' => "$path/theme",
  );

  // Displays the BLAST results for each job
  $items['blast_report_pending'] = array(
    'template' => 'blast_report_pending',
    'path' => "$path/theme",
  );

  // Themes the alignments in a BLAST result display
  $items['blast_report_alignment_row'] = array(
    'template' => 'blast_report_alignment_row',
    'variables' => array('hsps' => NULL),
    'path' => "$path/theme",
  );

  // Module Help
  $items['blast_help'] = array(
    'template' => 'blast_help',
    'path' => "$path/theme",
  );

  // Menu Information Pages
  // These are only seen if the user has disabled the all-in-one
  // or by query type forms.
  $items['blast_user_menupage'] = array(
    'template' => 'blast_user_menupage',
    'path' => "$path/theme",
  );
  $items['blast_nucleotide_user_menupage'] = array(
    'template' => 'blast_nucleotide_user_menupage',
    'path' => "$path/theme",
  );
  $items['blast_protein_user_menupage'] = array(
    'template' => 'blast_protein_user_menupage',
    'path' => "$path/theme",
  );

  return $items;
}

/**
 * Implements hook_help().
 */
function blast_ui_help($path, $arg) {
  if ($path == 'admin/help#blast_ui') {
    return theme('blast_help');
  }
}

/**
 * Facilitate presenting the result of the blast search
 *
 * @param $job_id
 *  The tripal job_id of the BLAST job previously submitted
 *
 * @return $result
 *  Return HTML output of the BLAST results to be displayed to the user
 *
 */
function show_blast_output($job_id) {

  // BLASTs are run as a Tripal job. As such we need to determine whether the current
  // BLAST is in the queue, running or complete in order to determine what to show the user
  //decode the job_id
  $job_id = base64_decode($job_id);
	$job = tripal_get_job($job_id);

  // 1) Job is in the Queue
  if ($job->start_time === NULL AND $job->end_time == NULL) {

    return theme('blast_report_pending', array('status_code' => 0, 'status' => 'Pending'));
  }
  // 2) Job has been Cancelled
  elseif ($job->status == 'Cancelled') {

    return theme('blast_report_pending', array('status_code' => 999, 'status' => 'Cancelled'));
  }
  // 3) Job is Complete
  elseif ($job->end_time !== NULL) {

    // Return the Results :)
    return theme('show_blast_report', array('job_id' => $job_id));

  }
  // 4) Job is in Progress
  else {

    return theme('blast_report_pending', array('status_code' => 1, 'status' => 'Running'));
  }


  return '';
}

/**
 *
 */
function ajax_blast_ui_example_sequence_callback($form, $form_state) {

  // First, set a default example sequence in case administrators have not yet
  // bothered to set their own.
  $sequence_type = $form_state['values']['query_type'];
  if ($sequence_type == 'nucleotide') {
    $default_example_sequence = '>partial lipoxygenase Glyma15g03040
TTTCGTATGA GATTAAAATG TGTGAAATTT TGTTTGATAG GACATGGGAA
AGGAAAAGTT GGAAAGGCTA CAAATTTAAG AGGACAAGTG TCGTTACCAA
CCTTGGGAGC TGGCGAAGAT GCATACGATG TTCATTTTGA ATGGGACAGT
GACTTCGGAA TTCCCGGTGC ATTTTACATT AAGAACTTCA TGCAAGTTGA
GTTCTATCTC AAGTCTCTAA CTCTCGAAGA CATTCCAAAC CACGGAACCA
TTCACTTCGT ATGCAACTCC TGGGTTTACA ACTCAAAATC CTACCATTCT
GATCGCATTT TCTTTGCCAA CAATGTAAGC TACTTAAATA CTGTTATACA
TTGTCTAACA TCTTGTTAGA GTCTTGCATG ATGTGTACCG TTTATTGTTG
TTGTTGAACT TTACCACATG GCATGGATGC AAAAGTTGTT ATACACATAA
ATTATAATGC AGACATATCT TCCAAGCGAG ACACCGGCTC CACTTGTCAA
GTACAGAGAA GAAGAATTGA AGAATGTAAG AGGGGATGGA ACTGGTGAGC
GCAAGGAATG GGATAGGATC TATGATTATG ATGTCTACAA TGACTTGGGC
GATCCAGATA AGGGTGAAAA GTATGCACGC CCCGTTCTTG GAGGTTCTGC
CTTACCTTAC CCTCGCAGAG GAAGAACCGG AAGAGGAAAA ACTAGAAAAG
GTTTCTCACT AGTCACTAAT TTATTACTTT TTAATGTTTG TTTTTAGGCA
TCTTTTCTGA TGAAATGTAT ACTTTTGATG TTTTTTTGTT TTAGCATAAC
TGAATTAGTA AAGTGTGTTG TGTTCCTTAG AAGTTAGAAA AGTACTAAGT
ATAAGGTCTT TGAGTTGTCG TCTTTATCTT AACAGATCCC AACAGTGAGA
AGCCCAGTGA TTTTGTTTAC CTTCCGAGAG ATGAAGCATT TGGTCACTTG
AAGTCATCAG ATTTTCTCGT TTATGGAATC AAATCAGTGG CTCAAGACGT
CTTGCCCGTG TTGACTGATG CGTTTGATGG CAATCTTTTG AGCCTTGAGT
TTGATAACTT TGCTGAAGTG CGCAAACTCT ATGAAGGTGG AGTTACACTA
CCTACAAACT TTCTTAGCAA GATCGCCCCT ATACCAGTGG TCAAGGAAAT
TTTTCGAACT GATGGCGAAC AGTTCCTCAA GTATCCACCA CCTAAAGTGA
TGCAGGGTAT GCTACATATT TTGAATATGT AGAATATTAT CAATATACTC
CTGTTTTTAT TCAACATATT TAATCACATG GATGAATTTT TGAACTGTTA';
  }
  elseif ($sequence_type == 'protein') {
    $default_example_sequence = '>gi|166477|gb|AAA96434.1| resveratrol synthase [Arachis hypogaea]
MVSVSGIRKVQRAEGPATVLAIGTANPPNCIDQSTYADYYFRVTNSEHMTDLKKKFQRICERTQIKNRHM
YLTEEILKENPNMCAYKAPSLDAREDMMIREVPRVGKEAATKAIKEWGQPMSKITHLIFCTTSGVALPGV
DYELIVLLGLDPCVKRYMMYHQGCFAGGTVLRLAKDLAENNKDARVLIVCSENTAVTFRGPSETDMDSLV
GQALFADGAAAIIIGSDPVPEVEKPIFELVSTDQKLVPGSHGAIGGLLREVGLTFYLNKSVPDIISQNIN
DALNKAFDPLGISDYNSIFWIAHPGGRAILDQVEQKVNLKPEKMKATRDVLSNYGNMSSACVFFIMDLMR
KRSLEEGLKTTGEGLDWGVLFGFGPGLTIETVVLRSVAI';
  }
  else {
    $default_example_sequence = '';
  }

  // If the Show Example checkbox is true then put the example in the textfield
  if ($form_state['values']['example_sequence']) {
    // Set the value to be the example sequence (either set by the administrator
    // or the default set above).
    $form['query']['FASTA']['#value'] = variable_get(
      'blast_ui_' . $sequence_type . '_example_sequence',
      $default_example_sequence
    );
  }
  // Otherwise we want to remove the already displayed example.
  else {
    $form['query']['FASTA']['#value'] = '';
  }

  return $form['query']['FASTA'];

}