#!/usr/local/bin/perl -w

# $Id: search_allele,v 1.3 2006/08/16 15:16:08 liya Exp $

=head1 NAME

search_allele

=head1 DESCRIPTION

This script is for searching and display alleles

=cut

# -------------------------------------------------------

use strict;
use Apache::Request;
use Template;
use Tie::IxHash;

use Gramene::Page;
use Gramene::Config;
use Gramene::Gene::GeneDB;

use constant DETAIL_TEMPLATE  => 'allele_detail.tmpl';
use constant ERROR_TEMPLATE   => 'gene_error.tmpl';

my $apr         = Apache::Request->new( Apache->request );
my $page        = Gramene::Page->new($apr);
my $config      = Gramene::Config->new;
my $gene_config = $config->get('genes');

my ( $template, $html, $db, $pager );

my %args = $apr->args;


# search field maps to term_type in database
my @ordered_term_types = (
    'Trait',
    'Plant Structure',
    'Cereal Plant Growth Stage',
    'Cellular Component',
    'Molecular Function',
    'Biological Process',
    'Enviroment'
    );


	
eval {

    $template = Template->new(
        {
            INCLUDE_PATH => $gene_config->{'template_dir'},
            PRE_CHOMP    => 1,
            POST_CHOMP   => 1,
            TRIM         => 1,

        }
    );

    $db = Gramene::Gene::GeneDB->new();
    $db->connect_to_db();

    my $id = $apr->param('id');
    my $allele_id = $id if $id;

    my $acc = $apr->param('acc');
    $allele_id = $db->get_allele_id($acc) if $acc;

    my $allele_obj = $db->get_allele_general_info($allele_id);    # for allele detail

    if ($allele_obj) {
	my $obj_type = 'allele';
#            #an array of synonyms
#            my $synonyms = $db->get_allele_synonyms($allele_id);
#            $allele_obj->{'synonyms'} = $synonyms if ( $synonyms && scalar($synonyms) > 0 );
#
#
	my $genes = $db->get_allele_related_genes($allele_id);
	$allele_obj->{'genes'} = $genes;
	
	my $studies = $db->get_related_studies($obj_type,$allele_id);
	$allele_obj->{'studies'} = $studies;
	
	my $germplasms = $db->get_related_germplasms($obj_type,$allele_id);
	$allele_obj->{'germplasms'} = $germplasms;

	#a hash with term_type as key and array of term objs as value
	my $ontologys = $db->get_ontology_association($obj_type,$allele_id);
	$allele_obj->{'ontology'} = $ontologys;
	   
#
#	    # a hash of sequence associations 
#	    my %seq_xrefs;
#
#	    
#	    my $xref_db = 'GenBank Nucleotide';
#	    my @dna_seq_xrefs = $db->get_allele_xrefs($obj_type, $allele_id, $xref_db);
#	    $seq_xrefs{'Nucleotide (DNA)'} = { $xref_db => \@dna_seq_xrefs } if @dna_seq_xrefs;
#	    
#	    $xref_db = 'Gramene Protein';
#	    my @gp_seq_xrefs = $db->get_allele_xrefs($obj_type, $allele_id, $xref_db);
#	    $seq_xrefs{'Protein'} = { $xref_db => \@gp_seq_xrefs } if @gp_seq_xrefs;
#
#	    $allele_obj->{'sequence_association'} = \%seq_xrefs;
#
#            #a hash with term_type as key and array of term objs as value
#            my $ontologys = $db->get_allele_ontology_association($allele_id);
#            $allele_obj->{'ontology'} = $ontologys;



        $template->process(
            DETAIL_TEMPLATE,
            {
                gramene_page => $page,
                apr          => $apr,
                allele         => $allele_obj,
		ordered_term_types => \@ordered_term_types,
                title        => "Summary for Allele &quot;"
                  . $allele_obj->{'accession'}
                  . "&quot;"
            },
            \$html
          )
          or $html = $template->error;

    }

};

if ( my $err = $@ ) {
    if ($template) {
        $template->process(
            ERROR_TEMPLATE,
            {
                gramene_page  => $page,
                error_message => $err,
            },
            \$html
          )
          or $html = $template->error;
    }else {
        $html = "Error: $err";
    }
}

$apr->content_type('text/html');
$apr->send_http_header;
$apr->print($html);

# ------------------------------------------------------------

=pod

=head1 AUTHORS

Liya Ren E<lt>ren@cshl.eduE<gt>

=cut

