#!/usr/bin/perl

our $VERSION = '0.02';

# $Id: genelocus_search,v 1.7.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;

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

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

# Search page object
my $sp = HTML::SearchPage->new(
    page_title     => 'Gene/Locus Search',
    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_genelocus_search'),
    footer         => $config->cfg('form_footer'),
    base_sql_table => qq[aux_feature_by_marker_type afbmt
                         LEFT JOIN aux_map_info ami ON (ami.cdv_map_feature_id = afbmt.cdv_map_feature_id)
                         ],
    base_sql_fields => [
        qq[CONCAT(IF(ami.ibm2_2005_chr IS NOT NULL AND ami.ibm2_2005_chr != 'conflict', 1, 0), ':', ami.name_gene_locus)],
        'ami.locus_type',
        'ami.unigene_name',
        'ami.cdv_map_feature_id',
        qq[CONCAT(ami.aux_map_info_id, ':', ami.ibm2_2005_chr)],
        'ami.ibm2_2005_position',
        'ami.ibm2_2005_bin',
        qq[CONCAT(ami.aux_map_info_id, ':', ami.fpc_contig)],
        'ami.fpc_chr',
        'ami.fpc_start',
        'ami.fpc_stop',
    ],
    base_output_headers => [
        'Gene/Locus:ami.name_gene_locus',
        'Locus Type:ami.locus_type',
        'Unigene Name:ami.unigene_name',
        'Markers/Assays',
        'IBM2 Chr.:ami.sort_ibm2_2005_chr',
        'IBM2 Position:ami.sort_ibm2_2005_position',
        'IBM2 Bin:ami.sort_ibm2_2005_bin',
        'FPC Contig:ami.sort_fpc_contig',
        'FPC Chr.:ami.sort_fpc_chr',
        'FPC Start:ami.sort_fpc_start',
        'FPC Stop:ami.sort_fpc_stop',
    ],
    sort_fields => 3,
    sort_defaults =>
      ['asc', 'ami.sort_ibm2_2005_chr', 'asc', 'ami.sort_ibm2_2005_position'],
    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

# Param fields
my $pf;

$pf = HTML::SearchPage::Param->new(
    -label            => 'Gene/Locus:',
    -sql_column       => 'ami.name_gene_locus',
    -form_name        => 'gene_locus',
    -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('gene_locus', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Locus Type:',
    -sql_column       => 'ami.locus_type',
    -form_name        => 'locus_type',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        qq[DISTINCT:SELECT distinct locus_type FROM aux_map_info
           WHERE locus_type IS NOT NULL AND locus_type != ""
           ORDER BY locus_type]
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('locus_type', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Containing Marker(s) of Type:',
    -sql_column       => 'afbmt.marker_type_string',
    -form_name        => 'marker_type',
    -operator_list    => ['like_c:contains'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        qq[DISTINCT:SELECT distinct marker_type FROM aux_marker_annotations
           WHERE marker_type IS NOT NULL AND marker_type != "" AND marker_type NOT LIKE "%::%"
           ORDER BY marker_type]
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('marker_type', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'IBM2 Chromosome:',
    -sql_column       => 'ami.ibm2_2005_chr',
    -form_name        => 'ibm2_2005_chr',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        qq[DISTINCT:SELECT distinct ibm2_2005_chr FROM aux_map_info
           WHERE ibm2_2005_chr IS NOT NULL ORDER BY ibm2_2005_chr+0]
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('chromosome', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Position (from cM):',
    -sql_column       => 'ami.ibm2_2005_position',
    -form_name        => 'position_from',
    -operator_list    => ['>=:from'],
    -operator_display => 0,
    -param_type       => 'text:12',
) or $sp->display_error_page($@);

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

$pf = HTML::SearchPage::Param->new(
    -label            => 'Position (to cM):',
    -sql_column       => 'ami.ibm2_2005_position',
    -form_name        => 'position_to',
    -operator_list    => ['<=:to'],
    -operator_display => 0,
    -param_type       => 'text:12',
) or $sp->display_error_page($@);

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

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

$sp->add_modification(
    -action => 'get_assays',
    -type   => 'gene_locus',
    -column => 3,
);

$sp->add_modification(
    -action => 'add_link',
    -type   => 'maizegdb_ibm2n',
    -column => 4,
);

$sp->add_modification(
    -action => 'add_link',
    -type   => 'maizegdb_chr_view',
    -column => 5,
);

$sp->add_modification(
    -action => 'add_link',
    -type   => 'maizegdb_chr_view',
    -column => 6,
);

$sp->add_modification(
    -action => 'add_link',
    -column => 7,
    -type   => 'fpcctg'
);

$sp->add_modification(
    -action => 'add_link',
    -column => 8,
    -type   => 'fpcchr'
);

# Call display method
$sp->display_page;

=head1 NAME

genelocus_search

=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

