package DBsearch;

##########################################################
#                                                        #
# dictyBase Extension of DBsearch                              #
#                                                        #
##########################################################

use DBsearch_base;
use CGI qw/:all/;

BEGIN { %DBsearch:: = %DBsearch_base:: }

###########################################################################
sub displayLocus {
###########################################################################
    my ($self) = shift;

    my $title = "$descText for Loci";
    if ($self->{'_type'} =~ /phenotype/i ||
        $self->{'_type'} =~ /go_term/i) {
        $title = "$title and Features";
    }
    &printStartPage($self->database, $title, $self->help);

    my $id = $query->param('id');
    my $sth;
    if ($self->{'_type'} =~ /phenotype/i) {
        $sth = $dbh->prepare("
             SELECT unique uniquename, cvt.name
             FROM   cgm_chado.cvterm cvt, cgm_chado.feature ft, CGM_DDB.locus_pheno LP
             WHERE  cvt.cvterm_id = ?
             AND    cvt.cvterm_id = LP.phenotype_no
             AND    LP.locus_no = ft.feature_id
        ");
        my $found;
        if ($sth->execute($id)) {
            while(my($locus, $phenotype) = $sth->fetchrow()) {
                $found++;
                if ($found == 1) {
                    print "<b><font color=red>$phenotype</font>:<p>";
                }
                print a({-href=>$configUrl->dictyBaseCGIRoot."gene_page.pl?gene_name=$locus",
                         -target=>'info'}, $locus);
                print br;
            }
        }
        ######################
        $sth = $dbh->prepare("
             SELECT unique feature_name, phenotype
             FROM   CGM_DDB.phenotype P, CGM_DDB.feature F, CGM_DDB.feat_pheno FP
             WHERE  P.phenotype_no = ?
             AND    P.phenotype_no = FP.phenotype_no
             AND    FP.feature_no = F.feature_no
        ");
        if ($sth->execute($id)) {
            while(my($feature, $phenotype) = $sth->fetchrow()) {
                $found++;
                print a({-href=>$configUrl->dictyBaseCGIRoot."gene_page.pl?gene_name=$feature",
                         -target=>'info'}, $feature);
                print br;
            }
        }
        if ($found) {
            print "</b><p>";
        }
    }
    elsif ($self->{'_type'} =~ /go_term/i) {
        $sth = $dbh->prepare("
        SELECT  UNIQUE L.LOCUS_NAME, g.go_term
        FROM    CGM_DDB.LOCUS L, CGM_DDB.GO_LOCUS_GOEV GLG, cgm_chado.V_GENE_FEATURES VNF, CGM_DDB.go G
        WHERE   GLG.GOID = ?
        AND GLG.LOCUS_NO = L.LOCUS_NO
        AND VNF.FEATURE_ID = L.LOCUS_NO
        AND G.goid = GLG.goid
        ORDER BY UPPER(L.LOCUS_NAME)
        ");
        my $found;
        if ($sth->execute($id)) {
            while(my($locus, $goTerm) = $sth->fetchrow()) {
                $found++;
                if(param('term'))
                {
                    $goTerm = param('term');
                }
                if ($found == 1) {
                    print "<b><font color=red>$goTerm</font>:<p>";
                }
                print a({-href=>$configUrl->dictyBaseCGIRoot."gene_page.pl?gene_name=$locus",
                         -target=>'info'}, $locus);
                print br;

            }
        }
        ######################
        $sth = $dbh->prepare("
             SELECT unique feature_name, go_term
             FROM   CGM_DDB.go G, CGM_DDB.feature F, CGM_DDB.go_feat_goev GFG
             WHERE  G.goid = ?
             AND    G.goid = GFG.goid
             AND    GFG.feature_no = F.feature_no
        ");
        if ($sth->execute($id)) {
            while(my($feature, $goTerm) = $sth->fetchrow()) {
                $found++;
                print a({-href=>$configUrl->dictyBaseCGIRoot."gene_page.pl?gene_name=$feature",
                         -target=>'info'}, $feature);
                print br;
            }
        }
        if ($found) {
            print "</b><p>";
        }
    }
    else {
        $sth = $dbh->prepare("
             SELECT gene_product, locus_name
             FROM   CGM_DDB.gene_product GP, CGM_DDB.locus L, CGM_DDB.locus_gp LGP
             WHERE  GP.gene_product_no = ?
             AND    GP.gene_product_no = LGP.gene_product_no
             AND    LGP.locus_no = L.locus_no
        ");
        my $locusNum;
        if ($sth->execute($id)) {
            while(my($gp, $locus) = $sth->fetchrow()) {
                $locusNum++;
                if ($locusNum == 1) {
                    print "<b><font color=red>$gp</font>:<p>";
                }
                print a({-href=>$configUrl->dictyBaseCGIRoot."gene_page.pl?gene_name=$locus",
                         -target=>'info'}, $locus);
                print br;
            }
            if ($locusNum) {
                print "</b><p>";
            }
        }
    }
    print hr;
    print "<div align=center>\n";
    print "<form>\n";
    print "<input type=button value=\"    Close    \" onClick=\"self.close()\">\n";
    print "</form>";

#    &printEndPage;
}
1;