#!/usr/bin/perl

our $VERSION = '0.02';

# $Id: polymorphic_between_accessions_step1,v 1.5.2.2 2007/06/14 19:04:09 kclark Exp $

use warnings;
use strict;

use FindBin::Real qw(Bin);
use HTML::SearchPage;
use HTML::SearchPage::Param;
use Panzea::FormModifier;
use Panzea::FormConfig;
use Panzea::WebFormUtils;

my $config_file = '/usr/local/gramene-25/conf//html-searchpage.conf'; # MKFILE:Q:CONF_FILE

my $config = Panzea::FormConfig->new($config_file);

my $code2sampstat = Panzea::WebFormUtils->code2sampstat();

# Search page object
my $sp = HTML::SearchPage->new(
    page_title      => 'Polymorphism Between Two Accessions (Step One)',
    header          => $config->cfg('form_header'),
    css             => $config->cfg('form_css'),
    temp_dir        => $config->cfg('temp_dir'),
    temp_dir_eq     => $config->cfg('temp_dir_eq'),
    instructions    => $config->cfg('instructions_poly_search'),
    footer          => $config->cfg('form_footer'),
    base_sql_table  => qq[aux_genotype_by_accession_summary agbas],
    base_sql_fields => [
        'agbas.marker_type',
        qq[CONCAT(agbas.marker_type, ":", agbas.accename)],
        'agbas.source',
        'agbas.sampstat',
        'agbas.genus',
        'agbas.species',
        'agbas.subspecies',
    ],
    distinct            => 1,
    base_output_headers => [
        'Marker Type',
        'Accession1:agbas.accename',
        'Souce:agbas.source',
        'Germplasm Type',
        'Genus:agbas.genus',
        'Species:agbas.species',
        'Subspecies:agbas.subspecies',
    ],
    sort_fields      => 2,
    sort_defaults    => ['asc', 'agbas.accename'],
    method           => $config->cfg('form_method'),
    page_size        => $config->cfg('form_page_size'),
    db_access_params => $config->cfg('db_access_params'),
    debug_level      => $config->cfg('form_debug_level'),
    go_to_results    => $config->cfg('form_go_to_results'),
    show_search_url  => $config->cfg('form_show_search_url'),
    modifier         => Panzea::FormModifier->new(),
); # Displays error page if fails

# Determine sampstats and format for display
my @sampstats;
eval {
    my $statement =
      qq[SELECT distinct sampstat FROM div_passport
         WHERE sampstat IS NOT NULL and sampstat != ""
         ORDER BY sampstat];

    @sampstats = $sp->run_distinct_statement($statement);
};
$sp->display_error_page($@) if $@;

my @formatted_sampstats = map {
        $code2sampstat->{$_}
      ? $_ . ":" . $sp->url_encode($code2sampstat->{$_})
      : $_ . ":"
      . $sp->url_encode($_)
} @sampstats;

# Param fields
my $pf;

$pf = HTML::SearchPage::Param->new(
    -label            => 'Marker Type:',
    -sql_column       => 'agbas.marker_type',
    -form_name        => 'marker_type',
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => ['SNP', 'SSR'
    ],
    -auto_all  => 0,
    -auto_null => 0,
) or $sp->display_error_page($@);

$sp->param_field('marker_type', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Accession 1:',
    -sql_column       => 'agbas.accename',
    -form_name        => 'accession1',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
) or $sp->display_error_page($@);

$sp->param_field('accename', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Source:',
    -sql_column       => 'agbas.source',
    -form_name        => 'source',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('source', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Germplasm Type:',
    -sql_column       => 'agbas.sampstat',
    -form_name        => 'sampstat',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => \@formatted_sampstats,
    -auto_all         => 1,
    -auto_null        => 1,
    -param_default    => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('sampstat', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Genus:',
    -sql_column       => 'agbas.genus',
    -form_name        => 'genus',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT genus from div_taxonomy
         WHERE genus IS NOT NULL AND genus != ""
         ORDER BY genus'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('genus', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Species:',
    -sql_column       => 'agbas.species',
    -form_name        => 'species',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT species from div_taxonomy
         WHERE species IS NOT NULL AND species != ""
         ORDER BY species'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('species', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Subspecies:',
    -sql_column       => 'agbas.subspecies',
    -form_name        => 'subspecies',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT subspecies from div_taxonomy
         WHERE subspecies IS NOT NULL AND subspecies != ""
         ORDER BY subspecies'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('subspecies', $pf);

# Modifications
$sp->add_modification(
    -action => 'add_link',
    -type   => 'polymorphic_between_accessions',
    -column => 1,
);

$sp->add_modification(
    -action => 'add_link',
    -column => 2,
    -type   => 'source'
);

$sp->add_modification(
    -action => 'translate',
    -column => 3,
    -type   => 'code2sampstat'
);

# Call display method
$sp->display_page;

=head1 NAME

polymorphic_between_accessions_step1

=head1 DESCRIPTION

Panzea web display script.

Please refer to documentation of HTML::SearchPage and
HTML::SearchPage::Param for information on script structure.

=head1 AUTHOR

Payan Canaran <canaran@cshl.edu>

=head1 BUGS

=head1 VERSION

Version 0.02

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

Copyright (c) 2005-2007 Cold Spring Harbor Laboratory

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. See DISCLAIMER.txt for
disclaimers of warranty.

=cut

